Skip to content

Commit c163394

Browse files
author
hzq
committed
Publish profile edits atomically and reject stale writers
1 parent 8b775b4 commit c163394

17 files changed

Lines changed: 514 additions & 31 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ jobs:
5353
- run: npm run check
5454
- run: npm run format:check
5555
- run: npm run build
56-
- run: node --import ./dist/test/setup-locale.js --test --test-concurrency=1 dist/test/release-plan.test.js dist/test/claude-sdk.test.js dist/test/profile-init.test.js
56+
- run: node --import ./dist/test/setup-locale.js --test --test-concurrency=1 dist/test/release-plan.test.js dist/test/claude-sdk.test.js dist/test/profile-init.test.js dist/test/profile-update.test.js

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ English · [简体中文](CHANGELOG.zh-CN.md)
55
## Unreleased
66

77
- After an interrupted first setup, `oaa init` can finish with the original private owner key. It refuses unsafe or incomplete keys and never overwrites an existing configuration or issues a replacement key for it.
8+
- Desktop startup, quickstart, model connection and local backend configuration now stage edits before replacing the profile. A failed write keeps the previous configuration; competing OAA writers reject stale edits rather than silently overwriting them.
89

910
## 0.0.72 (2026-11-24)
1011

‎CHANGELOG.zh-CN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 未发布
66

77
- 首次初始化中断后,`oaa init` 可以沿用已经写好的私有管理员密钥,补齐配置。残缺或权限不安全的密钥不会被采用,已有配置也不会被覆盖或被补发新密钥。
8+
- 桌面启动、快速启动、连接模型和本机后端设置现在先写好新配置再替换。写入失败时保留原配置;两个 OAA 进程同时修改时,后提交的旧内容会被拒绝,而不会吞掉另一个修改。
89

910
## 0.0.72(2026-11-24)
1011

‎desktop/cross-platform/main.cjs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const fsSync = require('node:fs');
1818
const fs = require('node:fs/promises');
1919
const os = require('node:os');
2020
const path = require('node:path');
21+
const { pathToFileURL } = require('node:url');
2122
const net = require('node:net');
2223
const { spawn, execFileSync } = require('node:child_process');
2324
const strings = require('./strings.cjs');
@@ -331,8 +332,10 @@ async function bootstrap() {
331332

332333
const configFile = path.join(data, 'config.json');
333334
let config = null;
335+
let configText;
334336
try {
335-
config = JSON.parse(await fs.readFile(configFile, 'utf8'));
337+
configText = await fs.readFile(configFile, 'utf8');
338+
config = JSON.parse(configText);
336339
} catch (error) {
337340
if (error.code !== 'ENOENT') {
338341
throw new StartupProblemError({ kind: 'configUnavailable', dir: data });
@@ -346,7 +349,8 @@ async function bootstrap() {
346349
}
347350
await cli(['init', '--dir', data]);
348351
try {
349-
config = JSON.parse(await fs.readFile(configFile, 'utf8'));
352+
configText = await fs.readFile(configFile, 'utf8');
353+
config = JSON.parse(configText);
350354
} catch {
351355
throw new StartupProblemError({ kind: 'configUnavailable', dir: data });
352356
}
@@ -370,7 +374,12 @@ async function bootstrap() {
370374
});
371375
if (patch.modified) {
372376
config = patch.json;
373-
await fs.writeFile(configFile, `${JSON.stringify(config, null, 2)}\n`, 'utf8');
377+
const { replaceProfileConfig } = await import(
378+
pathToFileURL(path.join(root, 'dist/src/config/profile-update.js')).href
379+
);
380+
await replaceProfileConfig(configFile, configText, `${JSON.stringify(config, null, 2)}\n`, {
381+
allowSymlink: true,
382+
});
374383
}
375384

376385
origin = localConfiguration(config);

‎docs/protocol.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ Error codes and response shapes do not depend on language; only the text of `mes
980980
| `insecure_endpoint` | 400 | Remote endpoints must use TLS and must not contain credentials |
981981
| `invalid_credential` | 500 | A service or node token is shorter than 24 characters (configuration error) |
982982
| `profile_key_conflict` | 409 | First setup cannot verify a pre-existing owner key; the key is not replaced |
983+
| `profile_config_conflict` | 409 | The configuration changed, another writer is busy, or the original protections cannot be preserved; reload before retrying |
983984
| `key_not_found` | 404 | No API key with that ID exists for this owner |
984985
| `node_authentication_denied` | — | Worker side: the gateway rejected the node credential; reconnecting stopped |
985986
| `node_connection_failed` | — | Worker side: could not connect to the gateway |

‎docs/quickstart.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ npm run oaa -- quickstart
2323

2424
If first setup stopped after writing `admin.token` but before creating `config.json`, rerun `quickstart` with the same `--dir`. It can reuse a complete, private owner key in the original format. Do not delete the key. If the key is unsafe or the configuration is damaged, keep the profile for recovery and use a new private directory to start separately; `oaa init` does not overwrite a configuration.
2525

26+
Updates to an existing profile are staged on the same volume. On a write conflict, reload the configuration and retry; do not delete `.oaa-profile-writer`. If a process is killed while staging, a private `.oaa-profile-edit-*` directory may remain. It is not the active configuration and is never used for recovery.
27+
2628
---
2729

2830
## Step 1: Publish Your First Agent

‎docs/zh-CN/protocol.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ HTTP 错误响应默认使用形状 `{"error":{"code":"…","message":"…","typ
10041004
| `insecure_endpoint` | 400 | 远程端点必须使用 TLS 且不得包含凭据 |
10051005
| `invalid_credential` | 500 | 服务或节点令牌少于 24 个字符(配置错误) |
10061006
| `profile_key_conflict` | 409 | 首次初始化无法确认已有管理员密钥完整且私有,不会替换密钥 |
1007+
| `profile_config_conflict` | 409 | 配置已更改、写入方正忙或不能保留原有权限;重新读取后再试 |
10071008
| `key_not_found` | 404 | 该 owner 下找不到 API 密钥 |
10081009
| `node_authentication_denied` | — | worker 侧:网关拒绝节点凭据,停止重连 |
10091010
| `node_connection_failed` | — | worker 侧:连接网关失败 |

‎docs/zh-CN/quickstart.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ npm run oaa -- quickstart
2323

2424
如果首次启动在写好 `admin.token` 后中断,且还没有 `config.json`,用同一个 `--dir` 再跑一次 `quickstart` 即可。完整、私有且符合原格式的管理员密钥可以继续使用,不要删除。如果密钥权限不安全或已有配置损坏,请保留原目录供恢复,另选一个私有目录开始;`oaa init` 不会覆盖已有配置。
2525

26+
更新已有配置时,新内容会在同一磁盘上写好后再替换。遇到写入冲突,重新读取当前配置后再试,不要删除 `.oaa-profile-writer`。强制结束正在写入的进程可能留下私有的 `.oaa-profile-edit-*` 临时目录;它不是生效配置,重试也不会加载它。
27+
2628
---
2729

2830
## 第一步:发布你的第一个智能体

‎src/client/connect-model.ts‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* print the model and a curl line. It never calls a paid model; `--test` sends one minimal request
77
* through the gateway, and only for loopback endpoints.
88
*/
9-
import { lstat, readFile, writeFile } from 'node:fs/promises';
9+
import { lstat, readFile } from 'node:fs/promises';
10+
import { replaceProfileConfig } from '../config/profile-update.js';
1011
import path from 'node:path';
1112
import { loadConfig, providerSchema, type GatewayConfig } from '../config/config.js';
1213
import { bi, pick, processLocale, type Locale } from '../core/locale.js';
@@ -280,15 +281,14 @@ export async function connectModel(o: ModelConnectOptions): Promise<void> {
280281
const { preset, name, agentId, baseUrl } = resolveTarget(o, t);
281282
const keyFile = o.keyFile ? await checkKeyFile(o.keyFile) : undefined;
282283
const file = path.join(path.resolve(o.dir), 'config.json');
283-
const raw = JSON.parse(
284-
await readFile(file, 'utf8').catch(() => {
285-
throw new Fault(
286-
'not_running',
287-
bi(`没有 ${file}。先运行 oaa quickstart。`, `No ${file}. Run oaa quickstart first.`),
288-
404,
289-
);
290-
}),
291-
) as RawConfig;
284+
const original = await readFile(file, 'utf8').catch(() => {
285+
throw new Fault(
286+
'not_running',
287+
bi(`没有 ${file}。先运行 oaa quickstart。`, `No ${file}. Run oaa quickstart first.`),
288+
404,
289+
);
290+
});
291+
const raw = JSON.parse(original) as RawConfig;
292292

293293
// A conflicting provider of the same name is refused before anything is probed or written.
294294
const entry = presetProviderEntry(preset, { baseUrl, keyEnv: o.keyEnv, keyFile });
@@ -327,7 +327,9 @@ export async function connectModel(o: ModelConnectOptions): Promise<void> {
327327
);
328328
else {
329329
providers[name] = entry;
330-
await writeFile(file, JSON.stringify(raw, null, 2) + '\n', { mode: 0o600 });
330+
await replaceProfileConfig(file, original, JSON.stringify(raw, null, 2) + '\n', {
331+
allowSymlink: true,
332+
});
331333
changed = true;
332334
out(
333335
t(

‎src/client/doctor.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { discoverAcpAgents, discoveredAdapterEntry } from '../config/agent-prese
1111
import { exposureHint } from '../config/security-profile.js';
1212
import { gatewaySchema, loadConfig } from '../config/config.js';
1313
import { pick } from '../core/locale.js';
14-
import { Fault, writePrivate } from '../core/util.js';
14+
import { Fault } from '../core/util.js';
15+
import { replaceProfileConfig } from '../config/profile-update.js';
1516
import { claudeSdkStatus } from '../engines/claude-sdk.js';
1617
import { CLI_TEXT } from './cli-text.js';
1718

@@ -73,7 +74,8 @@ export async function doctorReport(file: string, write: boolean): Promise<Record
7374
let written: string[] | undefined;
7475
if (write) {
7576
if (!loaded?.localWorker) throw new Fault('worker_required', CLI_TEXT.doctorWriteNeedsWorker);
76-
const raw = JSON.parse(await readFile(file, 'utf8')) as {
77+
const original = await readFile(file, 'utf8');
78+
const raw = JSON.parse(original) as {
7779
localWorker: { adapters?: Record<string, unknown> };
7880
};
7981
const adapters = (raw.localWorker.adapters ??= {});
@@ -85,11 +87,7 @@ export async function doctorReport(file: string, write: boolean): Promise<Record
8587
}
8688
if (written.length) {
8789
gatewaySchema.parse(raw);
88-
await writePrivate(
89-
path.dirname(file),
90-
path.basename(file),
91-
JSON.stringify(raw, null, 2) + '\n',
92-
);
90+
await replaceProfileConfig(file, original, JSON.stringify(raw, null, 2) + '\n');
9391
}
9492
}
9593
return {

0 commit comments

Comments
 (0)