Skip to content

Commit 1a5b818

Browse files
comments addressed
1 parent dfc3adc commit 1a5b818

2 files changed

Lines changed: 60 additions & 33 deletions

File tree

‎packages/trueforge/src/truefoundry/TrueFoundryAgentStore.ts‎

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
AgentNameConflictError,
23
type AgentRecord,
34
type CreateAgentInput,
45
type DeleteAgentInput,
@@ -14,21 +15,7 @@ function asError(value: unknown): Error {
1415
}
1516

1617
/**
17-
* Wraps DB agent persistence and keeps ServiceFoundry in sync. Remote work always runs
18-
* before the local write so a ServiceFoundry/network failure leaves the DB unchanged.
19-
* The remote agent id is stored on the row as `external_id`.
20-
*
21-
* Create — put remote agent, then insert locally with that id; if the insert fails, delete
22-
* the remote agent; if that cleanup also fails, throw AggregateError (both failures).
23-
*
24-
* Update (manifest) — put remote with the new manifest, then patch the local row (and
25-
* external_id if it changed); if the local write fails, put remote again with the previous
26-
* manifest; if that restore also fails, throw AggregateError. Metadata-only updates skip SF.
27-
*
28-
* Delete — delete remote first (404 counts as already gone), then delete the local row; if
29-
* remote delete fails, the local row stays.
30-
*
31-
* create: putRemote → createDB(external_id) | on DB fail → deleteRemote | both fail → AggregateError
18+
* create: getByName → putRemote → createDB(external_id) | on non-conflict DB fail → deleteRemote
3219
* update: putRemote(new) → updateDB | on DB fail → putRemote(old) | both fail → AggregateError
3320
* delete: deleteRemote(404 ok) → deleteDB
3421
*/
@@ -56,29 +43,30 @@ export class TrueFoundryAgentStore<TTransaction = never> implements IAgentStore<
5643
}
5744

5845
async createAgent(input: CreateAgentInput, transaction?: TTransaction): Promise<AgentRecord> {
46+
// SF PUT upserts by name — skip if local name exists (avoids overwrite/delete of e.g. research→sf-1).
47+
const existing = await this.#inner.getAgent({ tenant_id: input.tenant_id, name: input.name }, transaction);
48+
if (existing !== undefined) {
49+
throw new AgentNameConflictError({ tenant_id: input.tenant_id, name: input.name });
50+
}
51+
5952
const { remoteAgentId } = await this.#client.putRemoteAgent({
6053
accessToken: this.#accessToken,
6154
...toPutRemoteAgentPayload({ name: input.name, manifest: input.manifest }),
6255
});
6356
try {
64-
return await this.#inner.createAgent(
65-
{
66-
tenant_id: input.tenant_id,
67-
name: input.name,
68-
manifest: input.manifest,
69-
external_id: remoteAgentId,
70-
},
71-
transaction,
72-
);
57+
return await this.#inner.createAgent({ ...input, external_id: remoteAgentId }, transaction);
7358
} catch (error) {
74-
try {
75-
await this.#client.deleteRemoteAgent({ accessToken: this.#accessToken, remoteAgentId });
76-
} catch (cleanupError) {
77-
throw new AggregateError(
78-
[asError(error), asError(cleanupError)],
79-
'createAgent failed and ServiceFoundry cleanup also failed',
80-
{ cause: cleanupError },
81-
);
59+
// Race: peer create won the name and owns this remote (1:1) — do not delete it.
60+
if (!(error instanceof AgentNameConflictError)) {
61+
try {
62+
await this.#client.deleteRemoteAgent({ accessToken: this.#accessToken, remoteAgentId });
63+
} catch (cleanupError) {
64+
throw new AggregateError(
65+
[asError(error), asError(cleanupError)],
66+
'createAgent failed and ServiceFoundry cleanup also failed',
67+
{ cause: cleanupError },
68+
);
69+
}
8270
}
8371
throw error;
8472
}

‎packages/trueforge/tests/unit/truefoundry/TrueFoundryAgentStore.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AgentSpecSchema } from '@truefoundry/trueforge-core/agent-session';
22

33
import type { AgentRecord, IAgentStore } from '../../../src/db/agentStore';
4+
import { AgentNameConflictError } from '../../../src/db/agentStore';
45
import { TrueFoundryAgentStore } from '../../../src/truefoundry/TrueFoundryAgentStore';
56
import {
67
TrueFoundryServiceFoundryServerClient,
@@ -98,9 +99,10 @@ describe('TrueFoundryAgentStore', () => {
9899
});
99100
return { remoteAgentId: 'sf-1' };
100101
});
102+
const getAgent = jest.fn(async () => undefined);
101103
const createAgent = jest.fn(async () => created);
102104
const store = new TrueFoundryAgentStore({
103-
inner: mockInner({ createAgent }),
105+
inner: mockInner({ getAgent, createAgent }),
104106
client: mockClient({ putRemoteAgent }),
105107
accessToken: TOKEN,
106108
});
@@ -112,6 +114,7 @@ describe('TrueFoundryAgentStore', () => {
112114
manifest: manifest({ mcp_servers: [{ name: 'slack' }] }),
113115
}),
114116
).resolves.toBe(created);
117+
expect(getAgent).toHaveBeenCalledWith({ tenant_id: TENANT, name: 'research' }, undefined);
115118
expect(createAgent).toHaveBeenCalledWith(
116119
expect.objectContaining({
117120
tenant_id: TENANT,
@@ -122,6 +125,25 @@ describe('TrueFoundryAgentStore', () => {
122125
);
123126
});
124127

128+
it('createAgent rejects a duplicate local name before calling ServiceFoundry', async () => {
129+
const getAgent = jest.fn(async () => record({ external_id: 'sf-existing' }));
130+
const createAgent = jest.fn();
131+
const putRemoteAgent = jest.fn();
132+
const deleteRemoteAgent = jest.fn();
133+
const store = new TrueFoundryAgentStore({
134+
inner: mockInner({ getAgent, createAgent }),
135+
client: mockClient({ putRemoteAgent, deleteRemoteAgent }),
136+
accessToken: TOKEN,
137+
});
138+
139+
await expect(
140+
store.createAgent({ tenant_id: TENANT, name: 'research', manifest: manifest() }),
141+
).rejects.toMatchObject({ name: 'AgentNameConflictError' });
142+
expect(putRemoteAgent).not.toHaveBeenCalled();
143+
expect(createAgent).not.toHaveBeenCalled();
144+
expect(deleteRemoteAgent).not.toHaveBeenCalled();
145+
});
146+
125147
it('createAgent uses agent name as description when instructions are omitted', async () => {
126148
const putRemoteAgent = jest.fn(async (input: PutRemoteAgentInput) => {
127149
expect(input.description).toBe('research');
@@ -159,6 +181,23 @@ describe('TrueFoundryAgentStore', () => {
159181
expect(deleteRemoteAgent).toHaveBeenCalledWith({ accessToken: TOKEN, remoteAgentId: 'sf-1' });
160182
});
161183

184+
it('createAgent does not delete remote on local name conflict after put', async () => {
185+
const deleteRemoteAgent = jest.fn();
186+
const createAgent = jest.fn(async () => {
187+
throw new AgentNameConflictError({ tenant_id: TENANT, name: 'research' });
188+
});
189+
const store = new TrueFoundryAgentStore({
190+
inner: mockInner({ createAgent }),
191+
client: mockClient({ deleteRemoteAgent }),
192+
accessToken: TOKEN,
193+
});
194+
195+
await expect(
196+
store.createAgent({ tenant_id: TENANT, name: 'research', manifest: manifest() }),
197+
).rejects.toBeInstanceOf(AgentNameConflictError);
198+
expect(deleteRemoteAgent).not.toHaveBeenCalled();
199+
});
200+
162201
it('createAgent still throws when SF rollback fails', async () => {
163202
const deleteRemoteAgent = jest.fn(async () => {
164203
throw new Error('cleanup failed');

0 commit comments

Comments
 (0)