Skip to content

Commit e92b638

Browse files
Inject db into TrueFoundryAgentStore for advisory locks
1 parent 284762f commit e92b638

5 files changed

Lines changed: 104 additions & 78 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@truefoundry/trueforge": patch
3+
---
4+
5+
Use injected `db` for TrueFoundryAgentStore advisory-lock transactions.

‎packages/trueforge/src/db/postgres/agent-store/PostgresAgentStore.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ export class PostgresAgentStore implements IAgentStore<Transaction<Database>> {
8181
return row === undefined ? undefined : toRecord(row);
8282
}
8383

84-
withTransaction<T>(fn: (transaction: Transaction<Database>) => Promise<T>): Promise<T> {
85-
return this.#db.transaction().execute(fn);
86-
}
87-
8884
async createAgent(input: CreateAgentInput, transaction?: Transaction<Database>): Promise<AgentRecord> {
8985
const db = transaction ?? this.#db;
9086
try {

‎packages/trueforge/src/main.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
type TurnStreamingEvent,
5252
} from '@truefoundry/trueforge-core/agent-session';
5353
import { RequestReplyExecutor, RequestReplyRouter } from '@truefoundry/trueforge-core/request-reply';
54-
import type { Transaction } from 'kysely';
54+
import type { Kysely, Transaction } from 'kysely';
5555
import type { RedisClientType } from 'redis';
5656
import type { Logger } from 'winston';
5757

@@ -195,8 +195,9 @@ function buildResolveMcpServerStore<TTransaction>(options: {
195195
function buildResolveAgentStore(options: {
196196
persistenceStore: PostgresAgentStore;
197197
client: TrueFoundryServiceFoundryServerClient | undefined;
198+
db: Kysely<PostgresDatabase>;
198199
}): (c?: Context) => IAgentStore<Transaction<PostgresDatabase>> {
199-
const { persistenceStore, client } = options;
200+
const { persistenceStore, client, db } = options;
200201
if (!client) {
201202
return () => persistenceStore;
202203
}
@@ -207,6 +208,7 @@ function buildResolveAgentStore(options: {
207208
inner: persistenceStore,
208209
client,
209210
accessToken: requireRequestCredentialToken(c),
211+
db,
210212
})
211213
: persistenceStore;
212214
}
@@ -346,6 +348,7 @@ async function createDistributedPersistence(options: {
346348
resolveAgentStore: buildResolveAgentStore({
347349
persistenceStore: agentStore,
348350
client: serviceFoundryClient,
351+
db,
349352
}),
350353
withTransaction: callback => db.transaction().execute(callback),
351354
tokenStore,

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { AgentSpec } from '@truefoundry/trueforge-core/agent-session';
2-
import { sql, type Transaction } from 'kysely';
2+
import { sql, type Kysely, type Transaction } from 'kysely';
33
import {
44
type AgentRecord,
55
type CreateAgentInput,
@@ -34,57 +34,55 @@ function toPutRemoteAgentPayload({
3434
};
3535
}
3636

37-
type TrueFoundryAgentInner<TTransaction> = IAgentStore<TTransaction> & {
38-
withTransaction<T>(fn: (transaction: TTransaction) => Promise<T>): Promise<T>;
39-
};
40-
4137
/**
4238
* create: createDB(null) → putRemote → updateDB(external_id) | on put/update fail → deleteDB (+ deleteRemote if put ok)
4339
* update: lock → get → putRemote(new) → updateDB | on DB fail → putRemote(old) | both fail → AggregateError
4440
* delete: lock → get → deleteRemote(404 ok) → deleteDB
4541
*/
46-
export class TrueFoundryAgentStore<
47-
TTransaction extends Transaction<Database> = Transaction<Database>,
48-
> implements IAgentStore<TTransaction> {
49-
readonly #inner: TrueFoundryAgentInner<TTransaction>;
42+
export class TrueFoundryAgentStore implements IAgentStore<Transaction<Database>> {
43+
readonly #inner: IAgentStore<Transaction<Database>>;
5044
readonly #client: TrueFoundryServiceFoundryServerClient;
5145
readonly #accessToken: string;
46+
readonly #db: Kysely<Database>;
5247

5348
constructor(input: {
54-
inner: TrueFoundryAgentInner<TTransaction>;
49+
inner: IAgentStore<Transaction<Database>>;
5550
client: TrueFoundryServiceFoundryServerClient;
5651
accessToken: string;
52+
db: Kysely<Database>;
5753
}) {
5854
this.#inner = input.inner;
5955
this.#client = input.client;
6056
this.#accessToken = input.accessToken;
57+
this.#db = input.db;
6158
}
6259

63-
listAgents(input: ListAgentsInput, transaction?: TTransaction): Promise<AgentRecord[]> {
60+
listAgents(input: ListAgentsInput, transaction?: Transaction<Database>): Promise<AgentRecord[]> {
6461
return this.#inner.listAgents(input, transaction);
6562
}
6663

67-
getAgent(input: GetAgentInput, transaction?: TTransaction): Promise<AgentRecord | undefined> {
64+
getAgent(input: GetAgentInput, transaction?: Transaction<Database>): Promise<AgentRecord | undefined> {
6865
return this.#inner.getAgent(input, transaction);
6966
}
7067

7168
// Serializes updates for this tenant ID and agent id.
7269
// Prevents concurrent MCP writes from desyncing the remote agent.
70+
// Opens a txn via db when the caller omits transaction (does not require one).
7371
#withUpdateLock<T>(
7472
input: { tenant_id: string; id: string },
75-
transaction: TTransaction | undefined,
76-
fn: (transaction: TTransaction) => Promise<T>,
73+
transaction: Transaction<Database> | undefined,
74+
fn: (transaction: Transaction<Database>) => Promise<T>,
7775
): Promise<T> {
78-
const run = async (txn: TTransaction) => {
76+
const run = async (txn: Transaction<Database>) => {
7977
const key = `tf:agent:${input.tenant_id}:${input.id}`;
8078
await sql`SELECT pg_advisory_xact_lock(hashtext(${key}))`.execute(txn);
8179
return fn(txn);
8280
};
83-
return transaction !== undefined ? run(transaction) : this.#inner.withTransaction(run);
81+
return transaction !== undefined ? run(transaction) : this.#db.transaction().execute(run);
8482
}
8583

86-
async createAgent(input: CreateAgentInput, transaction?: TTransaction): Promise<AgentRecord> {
87-
// Lets the DB unique constraint pick one winner for this tenant ID and name.
84+
async createAgent(input: CreateAgentInput, transaction?: Transaction<Database>): Promise<AgentRecord> {
85+
// Unique (tenant_id, name) picks one winner even when transaction is omitted (auto-commit inserts).
8886
// Prevents concurrent requests from both creating the same remote agent.
8987
const created = await this.#inner.createAgent({ ...input, external_id: null }, transaction);
9088

@@ -123,7 +121,7 @@ export class TrueFoundryAgentStore<
123121
}
124122
}
125123

126-
async updateAgent(input: UpdateAgentInput, transaction?: TTransaction): Promise<AgentRecord | undefined> {
124+
async updateAgent(input: UpdateAgentInput, transaction?: Transaction<Database>): Promise<AgentRecord | undefined> {
127125
const nextManifest = input.manifest;
128126
if (nextManifest === undefined) {
129127
// No manifest means only `external_id` changed; pass through to the inner store.
@@ -169,7 +167,7 @@ export class TrueFoundryAgentStore<
169167
});
170168
}
171169

172-
async deleteAgent(input: DeleteAgentInput, transaction?: TTransaction): Promise<void> {
170+
async deleteAgent(input: DeleteAgentInput, transaction?: Transaction<Database>): Promise<void> {
173171
return this.#withUpdateLock(input, transaction, async txn => {
174172
const previous = await this.#inner.getAgent({ tenant_id: input.tenant_id, id: input.id }, txn);
175173
if (previous?.external_id) {

0 commit comments

Comments
 (0)