|
1 | 1 | import type { AgentSpec } from '@truefoundry/trueforge-core/agent-session'; |
| 2 | +import { sql, type Transaction } from 'kysely'; |
2 | 3 | import { |
3 | 4 | type AgentRecord, |
4 | 5 | type CreateAgentInput, |
|
8 | 9 | type ListAgentsInput, |
9 | 10 | type UpdateAgentInput, |
10 | 11 | } from '../db/agentStore'; |
11 | | -import type { WithAgentUpdateLock } from '../db/agentUpdateLock'; |
| 12 | +import type { Database } from '../db/postgres/types'; |
12 | 13 | import { |
13 | 14 | TrueFoundryServiceFoundryServerClient, |
14 | 15 | type PutRemoteAgentInput, |
@@ -38,33 +39,44 @@ function toPutRemoteAgentPayload({ |
38 | 39 | * update: lock → get → putRemote(new) → updateDB | on DB fail → putRemote(old) | both fail → AggregateError |
39 | 40 | * delete: lock → get → deleteRemote(404 ok) → deleteDB |
40 | 41 | */ |
41 | | -export class TrueFoundryAgentStore<TTransaction = never> implements IAgentStore<TTransaction> { |
| 42 | +export class TrueFoundryAgentStore< |
| 43 | + TTransaction extends Transaction<Database> = Transaction<Database>, |
| 44 | +> implements IAgentStore<TTransaction> { |
42 | 45 | readonly #inner: IAgentStore<TTransaction>; |
43 | 46 | readonly #client: TrueFoundryServiceFoundryServerClient; |
44 | 47 | readonly #accessToken: string; |
45 | | - readonly #withUpdateLock: WithAgentUpdateLock<TTransaction>; |
46 | 48 |
|
47 | 49 | constructor(input: { |
48 | 50 | inner: IAgentStore<TTransaction>; |
49 | 51 | client: TrueFoundryServiceFoundryServerClient; |
50 | 52 | accessToken: string; |
51 | | - withUpdateLock: WithAgentUpdateLock<TTransaction>; |
52 | 53 | }) { |
53 | 54 | this.#inner = input.inner; |
54 | 55 | this.#client = input.client; |
55 | 56 | this.#accessToken = input.accessToken; |
56 | | - this.#withUpdateLock = input.withUpdateLock; |
57 | 57 | } |
58 | 58 |
|
59 | | - listAgents(input: ListAgentsInput, transaction?: TTransaction): Promise<AgentRecord[]> { |
| 59 | + listAgents(input: ListAgentsInput, transaction: TTransaction): Promise<AgentRecord[]> { |
60 | 60 | return this.#inner.listAgents(input, transaction); |
61 | 61 | } |
62 | 62 |
|
63 | | - getAgent(input: GetAgentInput, transaction?: TTransaction): Promise<AgentRecord | undefined> { |
| 63 | + getAgent(input: GetAgentInput, transaction: TTransaction): Promise<AgentRecord | undefined> { |
64 | 64 | return this.#inner.getAgent(input, transaction); |
65 | 65 | } |
66 | 66 |
|
67 | | - async createAgent(input: CreateAgentInput, transaction?: TTransaction): Promise<AgentRecord> { |
| 67 | + // Serializes updates for this tenant ID and agent id. |
| 68 | + // Prevents concurrent MCP writes from desyncing the remote agent. |
| 69 | + async #withUpdateLock<T>( |
| 70 | + input: { tenant_id: string; id: string }, |
| 71 | + transaction: TTransaction, |
| 72 | + fn: (transaction: TTransaction) => Promise<T>, |
| 73 | + ): Promise<T> { |
| 74 | + const key = `tf:agent:${input.tenant_id}:${input.id}`; |
| 75 | + await sql`SELECT pg_advisory_xact_lock(hashtext(${key}))`.execute(transaction); |
| 76 | + return fn(transaction); |
| 77 | + } |
| 78 | + |
| 79 | + async createAgent(input: CreateAgentInput, transaction: TTransaction): Promise<AgentRecord> { |
68 | 80 | // Lets the DB unique constraint pick one winner for this tenant ID and name. |
69 | 81 | // Prevents concurrent requests from both creating the same remote agent. |
70 | 82 | const created = await this.#inner.createAgent({ ...input, external_id: null }, transaction); |
@@ -104,16 +116,14 @@ export class TrueFoundryAgentStore<TTransaction = never> implements IAgentStore< |
104 | 116 | } |
105 | 117 | } |
106 | 118 |
|
107 | | - async updateAgent(input: UpdateAgentInput, transaction?: TTransaction): Promise<AgentRecord | undefined> { |
| 119 | + async updateAgent(input: UpdateAgentInput, transaction: TTransaction): Promise<AgentRecord | undefined> { |
108 | 120 | const nextManifest = input.manifest; |
109 | 121 | if (nextManifest === undefined) { |
110 | 122 | // No manifest means only `external_id` changed; pass through to the inner store. |
111 | 123 | return this.#inner.updateAgent(input, transaction); |
112 | 124 | } |
113 | 125 |
|
114 | | - // Serialize same-agent updates (incl. SF HTTP) so concurrent MCP writes cannot desync. |
115 | | - return this.#withUpdateLock({ tenant_id: input.tenant_id, id: input.id }, async lockedTransaction => { |
116 | | - const txn = lockedTransaction ?? transaction; |
| 126 | + return this.#withUpdateLock(input, transaction, async txn => { |
117 | 127 | const previous = await this.#inner.getAgent({ tenant_id: input.tenant_id, id: input.id }, txn); |
118 | 128 | if (previous === undefined) { |
119 | 129 | return undefined; |
@@ -152,10 +162,8 @@ export class TrueFoundryAgentStore<TTransaction = never> implements IAgentStore< |
152 | 162 | }); |
153 | 163 | } |
154 | 164 |
|
155 | | - async deleteAgent(input: DeleteAgentInput, transaction?: TTransaction): Promise<void> { |
156 | | - // Same lock as update so delete cannot remove the remote between update's get and putRemote. |
157 | | - return this.#withUpdateLock({ tenant_id: input.tenant_id, id: input.id }, async lockedTransaction => { |
158 | | - const txn = lockedTransaction ?? transaction; |
| 165 | + async deleteAgent(input: DeleteAgentInput, transaction: TTransaction): Promise<void> { |
| 166 | + return this.#withUpdateLock(input, transaction, async txn => { |
159 | 167 | const previous = await this.#inner.getAgent({ tenant_id: input.tenant_id, id: input.id }, txn); |
160 | 168 | if (previous?.external_id) { |
161 | 169 | await this.#client.deleteRemoteAgent({ |
|
0 commit comments