Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(provider): publish agent mutations after persistence succeeds (#2217)

0xken15838cc313de34
diff --git a/provider/example-provider/src/workspace.spec.ts b/provider/example-provider/src/workspace.spec.ts
new file mode 100644
index 000000000..8bc9c9595
--- /dev/null
+++ b/provider/example-provider/src/workspace.spec.ts
@@ -0,0 +1,49 @@
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import { mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import type { Config } from './config.js'
+import { createAgent, deleteAgent, readAgentsFile, renameAgent } from './workspace.js'
+
+let root: string
+let config: Config
+let originalId: string
+
+beforeEach(() => {
+ root = mkdtempSync(join(tmpdir(), 'provider-workspace-'))
+ config = {
+ agents: [], workspaceRoot: root, agentsFile: join(root, 'agents.json'),
+ claudeBin: 'unused-test-binary', model: 'test', port: 0,
+ stateFile: join(root, 'sessions.json'), claudeProjectsDir: join(root, 'projects'),
+ recapModel: 'test', recapDisabled: true,
+ }
+ originalId = createAgent(config, 'Original').id
+})
+afterEach(() => rmSync(root, { recursive: true, force: true }))
+
+const mutations: Array<[string, (config: Config, id: string) => unknown]> = [
+ ['create', (config) => createAgent(config, 'Second')],
+ ['rename', (config, id) => renameAgent(config, id, 'Changed')],
+ ['delete', (config, id) => deleteAgent(config, id)],
+]
+
+describe.each(['write', 'rename'])('when the agents-file %s fails', (stage) => {
+ it.each(mutations)('keeps %s unpublished until it can be saved', (_name, mutate) => {
+ const before = config.agents.map((agent) => ({ ...agent }))
+ const backup = join(root, 'saved-agents.json')
+ const obstruction = stage === 'write' ? `${config.agentsFile}.tmp` : config.agentsFile
+ if (stage === 'rename') renameSync(config.agentsFile, backup)
+ mkdirSync(obstruction)
+
+ expect(() => mutate(config, originalId)).toThrow()
+ expect(config.agents).toEqual(before)
+ const saved = stage === 'rename' ? backup : config.agentsFile
+ expect(JSON.parse(readFileSync(saved, 'utf8'))).toEqual(before)
+
+ // Once persistence works again, the same request must be retryable.
+ rmSync(obstruction, { recursive: true })
+ if (stage === 'rename') renameSync(backup, config.agentsFile)
+ mutate(config, originalId)
+ expect(readAgentsFile(config)).toEqual(config.agents)
+ })
+})
diff --git a/provider/example-provider/src/workspace.ts b/provider/example-provider/src/workspace.ts
index 659d9e16e..76177b139 100644
--- a/provider/example-provider/src/workspace.ts
+++ b/provider/example-provider/src/workspace.ts
@@ -38,16 +38,16 @@ export function createAgent(config: Config, name: string, description = ''): Age
mkdirSync(cwd, { recursive: true })
const entry: AgentEntry = { id, name: trimmed, description: description.trim(), cwd }
- config.agents.push(entry)
- persist(config)
+ persist(config, [...config.agents, entry])
return entry
}
export function deleteAgent(config: Config, id: string): void {
const index = config.agents.findIndex((a) => a.id === id)
if (index === -1) throw new WorkspaceError('unknown agent')
- const [removed] = config.agents.splice(index, 1)
- persist(config)
+ const agents = config.agents.slice()
+ const [removed] = agents.splice(index, 1)
+ persist(config, agents)
// The DIRECTORY is left alone on purpose. Removing an agent from the list is reversible; deleting
// whatever work it produced is not, and an example provider is the last place to be destructive.
void removed
@@ -58,17 +58,18 @@ export function renameAgent(config: Config, id: string, name: string): AgentEntr
if (!entry) throw new WorkspaceError('unknown agent')
const trimmed = name.trim()
if (!trimmed) throw new WorkspaceError('name is required')
- entry.name = trimmed
- persist(config)
- return entry
+ const renamed = { ...entry, name: trimmed }
+ persist(config, config.agents.map((agent) => agent === entry ? renamed : agent))
+ return renamed
}
-/** Write-then-rename: a crash mid-write must not leave a truncated agents.json. */
-function persist(config: Config): void {
- const rows = config.agents.map(({ id, name, description, cwd }) => ({ id, name, description, cwd }))
+/** Write-then-rename keeps the file whole; publish memory only after the replacement succeeds. */
+function persist(config: Config, agents: AgentEntry[]): void {
+ const rows = agents.map(({ id, name, description, cwd }) => ({ id, name, description, cwd }))
const tmp = `${config.agentsFile}.tmp`
writeFileSync(tmp, `${JSON.stringify(rows, null, 2)}\n`)
renameSync(tmp, config.agentsFile)
+ config.agents = agents
}
/** Used by the tests to read back what was persisted. */