fix(cli): persist first-run Codex defaults
Summary
- Write first-run
agent-orchestrator.yamlas flat behavior-only project config instead of wrappedprojects:YAML. - Persist the selected first-run runtime/agent/workspace/notifiers/port into the global config after project registration.
- Add regression coverage for Codex first-run selection resolving through the generated global project id.
Closes #1948
Tests
pnpm buildpnpm --filter @aoagents/ao-cli test -- __tests__/commands/start.test.tspnpm --filter @aoagents/ao-cli typecheckpnpm typecheckpnpm lint(passes with existing warnings)pnpm test(fails locally in pre-existing environment-sensitive tests:packages/integration-tests/src/notifier-desktop.integration.test.tsexpectsosascriptwhile this macOS host picksterminal-notifier;packages/cli/__tests__/lib/path-equality.test.tsreports/private/varvs/varHOME realpath mismatch)
Test Coverage Report
| Metric | Value |
|---|---|
| Lines covered | 1148/1567 |
| Lines not covered | 419/1567 |
| Overall coverage | 73.3% |
Uncovered lines
packages/cli/src/commands/start.ts: L140-L141, L182-L184, L187-L189, L191, L193-L197, L200-L201, L203, L205-L209, L211-L218, L220-L222, L262-L263, L268-L286, L288-L295, L297-L298, L304-L310, L312, L343-L344, L358-L360, L369-L370, L373-L397, L442-L456, L458-L461, L463-L477, L505, L516, L540-L541, L544-L545, L561-L566, L607-L610, L625-L628, L671-L672, L679-L689, L692-L693, L717-L718, L721-L725, L733-L740, L747-L748, L785-L788, L803-L808, L825-L827, L829-L836, L838-L840, L842-L847, L849-L850, L961-L964, L1011, L1044-L1063, L1133-L1134, L1144-L1146, L1218-L1219, L1225-L1226, L1228-L1229, L1232-L1239, L1241, L1289-L1294, L1327-L1328, L1334-L1335, L1359-L1363, L1368-L1373, L1376-L1381, L1388-L1394, L1396-L1403, L1405-L1411, L1460, L1480-L1481, L1586-L1587, L1639-L1656, L1720, L1754-L1756, L1779-L1786, L1813-L1821, L1842-L1843, L1877-L1878, L1886-L1897, L1900, L1928, L1934-L1935, L1938-L1946, L1956-L1957, L1966-L1967, L2014-L2016, L2045-L2047, L2125-L2130, L2160-L2171, L2204-L2205
Greptile Summary
This PR fixes the Codex first-run experience by (1) writing the local agent-orchestrator.yaml as a flat behavior-only file (no projects: wrapper), (2) introducing persistFirstRunGlobalDefaults to persist the selected runtime/agent/workspace/port into the global config, and (3) restructuring the error-handling path in autoCreateConfig so the local config is only removed when registration itself fails.
- New
persistFirstRunGlobalDefaultshelper reads the global config, merges first-run defaults (preserving existing notifiers when a prior global config exists), and saves the result; called afterregisterProjectInGlobalConfiginside a sharedtryblock. - Error-recovery logic is split: local config is deleted only when
registeredProjectIdis stillnull(registration failed), not when the subsequent defaults-persistence step fails. - Four tests updated/added to cover flat-config generation, Codex first-run persistence, existing-notifier preservation, and the new partial-failure cleanup behavior.
Confidence Score: 3/5
The core registration logic is sound, but persistFirstRunGlobalDefaults writes the global config outside the file lock that every other write in the codebase uses, making it possible for a concurrent ao start to silently erase a freshly-registered project.
The new persistFirstRunGlobalDefaults function does a read-modify-write of the global config without acquiring withFileLockSync, breaking the locking invariant that every other global-config write in the codebase relies on. In CI pipelines or monorepos where multiple ao start invocations run in parallel, this window allows one process to overwrite a project entry registered by another, silently corrupting the global registry.
packages/cli/src/commands/start.ts — specifically the new persistFirstRunGlobalDefaults function and its missing file lock.
Important Files Changed
| Filename | Overview |
|---|---|
| packages/cli/src/commands/start.ts | Introduces persistFirstRunGlobalDefaults which does a read-modify-write of the global config without the file lock used by registerProjectInGlobalConfig, creating a TOCTOU race that can silently drop concurrent project registrations. |
| packages/cli/tests/commands/start.test.ts | Adds three new focused tests for flat-config generation, Codex-first-run notifier preservation, and partial-failure cleanup; spy isolation for saveGlobalConfig relies on ESM live-binding behavior which is consistent with the Vitest mock setup. |
Sequence Diagram
sequenceDiagram
participant CLI as autoCreateConfig
participant FS as File System
participant Reg as registerProjectInGlobalConfig (withFileLockSync)
participant Persist as persistFirstRunGlobalDefaults (no lock)
participant GC as Global Config File
CLI->>GC: loadGlobalConfig (line 602)
CLI->>FS: writeLocalProjectConfig
CLI->>Reg: registerProjectInGlobalConfig
Reg-->>GC: withFileLockSync → saveGlobalConfig (project added)
CLI->>Persist: persistFirstRunGlobalDefaults
Persist->>GC: loadGlobalConfig (second read, no lock)
Note over Persist,GC: Race window: another process can call registerProjectInGlobalConfig here
Persist->>GC: saveGlobalConfig (overwrites — no lock)
Note over Persist,GC: Any project registered in the race window is silently lost
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
packages/cli/src/commands/start.ts:147-175
`persistFirstRunGlobalDefaults` does a bare `loadGlobalConfig` → `saveGlobalConfig` cycle with no file lock. Every other write to the global registry (`registerProjectInGlobalConfig`, the internal `migrateLegacyGlobalConfigOnLoad` rewrite) holds `withFileLockSync` for the entire read-modify-write. Without a lock here, a concurrent `ao start` in another directory can interleave: the other process's `registerProjectInGlobalConfig` saves its project after this function's read but before its write, and this function's subsequent `saveGlobalConfig` will silently overwrite the global config with a stale snapshot, dropping the other project from the registry.
```suggestion
function persistFirstRunGlobalDefaults(params: {
port: number;
runtime: string;
agent: string;
workspace: string;
notifiers: string[];
preserveExistingNotifiers: boolean;
}): void {
const globalConfigPath = getGlobalConfigPath();
withFileLockSync(globalConfigLockPath(globalConfigPath), () => {
const existing = loadGlobalConfig(globalConfigPath, { alreadyLocked: true });
const baseConfig = existing ?? createDefaultGlobalConfig();
saveGlobalConfig(
{
...baseConfig,
port: params.port,
defaults: {
...baseConfig.defaults,
runtime: params.runtime,
agent: params.agent,
workspace: params.workspace,
notifiers:
params.preserveExistingNotifiers && existing
? [...baseConfig.defaults.notifiers]
: [...params.notifiers],
},
},
globalConfigPath,
);
});
}
```
### Issue 2 of 2
packages/cli/src/commands/start.ts:604-610
When `findFreePort` can't locate any free port it returns `null`. The fallback `port ?? requestedDashboardPort` silently continues with the originally-requested port, but the `if (port !== null && ...)` guard means no warning is printed in this case. The persisted config ends up with a port that was never confirmed to be available, and `ao` will likely fail to bind on next start with no hint about why.
```suggestion
const port = await findFreePort(requestedDashboardPort);
const dashboardPort = port ?? requestedDashboardPort;
if (port === null) {
console.log(
chalk.yellow(
` ⚠ Could not find a free port starting from ${requestedDashboardPort} — using it anyway; adjust 'port' in global config if startup fails.`,
),
);
} else if (port !== requestedDashboardPort) {
console.log(
chalk.yellow(` ⚠ Port ${requestedDashboardPort} is busy — using ${port} instead.`),
);
}
```
Reviews (4): Last reviewed commit: "fix(cli): persist first-run Codex defaul..." | Re-trigger Greptile