Skip to content

Commit 5a0b8c6

Browse files
committed
perf(server): narrow agent awareness startup reads
- Add a focused projection snapshot for agent awareness. - Avoid loading full thread shell data during relay catch-up.
1 parent dd69b31 commit 5a0b8c6

7 files changed

Lines changed: 184 additions & 34 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,12 @@ export interface OrchestratorV2Shape {
444444
Effect.Success<ReturnType<ProjectionStoreV2["Service"]["getPullRequestSnapshot"]>>,
445445
OrchestratorV2Error
446446
>;
447+
readonly getAgentAwarenessSnapshot: (
448+
threadId?: ThreadId,
449+
) => Effect.Effect<
450+
Effect.Success<ReturnType<ProjectionStoreV2["Service"]["getAgentAwarenessSnapshot"]>>,
451+
OrchestratorV2Error
452+
>;
447453
readonly getShellSnapshot: (
448454
options?: ShellSnapshotOptions,
449455
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, OrchestratorV2Error>;
@@ -14188,6 +14194,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1418814194
.getPullRequestSnapshot(options)
1418914195
.pipe(Effect.mapError(shellProjectionError));
1419014196
}),
14197+
getAgentAwarenessSnapshot: Effect.fn("OrchestratorV2.getAgentAwarenessSnapshot")(
14198+
function (threadId) {
14199+
return projectionStore
14200+
.getAgentAwarenessSnapshot(threadId)
14201+
.pipe(Effect.mapError(shellProjectionError));
14202+
},
14203+
),
1419114204
getShellSnapshot: Effect.fn("OrchestratorV2.getShellSnapshot")(function (options) {
1419214205
return projectionStore.getShellSnapshot(options).pipe(Effect.mapError(shellProjectionError));
1419314206
}),

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
TurnItemId,
2929
type ThreadPullRequestLink,
3030
isLiveSubagentShell,
31+
isListedThreadLineage,
3132
} from "@t3tools/contracts";
3233
import * as DateTime from "effect/DateTime";
3334
import * as Effect from "effect/Effect";
@@ -96,8 +97,40 @@ const traceSqlStatements = () => {
9697
/** The shell index flags every live shell and matches its shells on the fields subscriber trims read. */
9798
const assertShellIndexCoversShells = Effect.fnUntraced(function* () {
9899
const store = yield* ProjectionStore.ProjectionStoreV2;
99-
const shells = (yield* store.getShellSnapshot({ location: "active" })).threads;
100+
const snapshot = yield* store.getShellSnapshot();
101+
const shells = snapshot.threads;
100102
const index = yield* store.listActiveShellIndex();
103+
const { tracer, statements, queryTransactions } = traceSqlStatements();
104+
const activity = yield* store.getAgentAwarenessSnapshot().pipe(Effect.withTracer(tracer));
105+
assert.equal(activity.snapshotSequence, snapshot.snapshotSequence);
106+
const awareness = (thread: Parameters<typeof projectThreadAwarenessV2>[0]["thread"]) =>
107+
projectThreadAwarenessV2({
108+
environmentId: EnvironmentId.make("test"),
109+
project: { title: "Test" },
110+
thread,
111+
});
112+
assert.deepEqual(
113+
activity.threads.map(awareness),
114+
shells.filter((thread) => isListedThreadLineage(thread.lineage)).map(awareness),
115+
);
116+
assert.deepEqual(
117+
activity.threads.map((thread) => [thread.id, thread.projectId, thread.latestRunCompletedAt]),
118+
shells
119+
.filter((thread) => isListedThreadLineage(thread.lineage))
120+
.map((thread) => [thread.id, thread.projectId, thread.latestRunCompletedAt]),
121+
);
122+
assert.isFalse(statements.some((statement) => /COUNT\(\*\)|GROUP BY/i.test(statement)));
123+
assert.isTrue(queryTransactions.every((transaction) => transaction !== null));
124+
assert.equal(new Set(queryTransactions).size, 1);
125+
for (const shell of [...shells, ...snapshot.archivedThreads]) {
126+
const targeted = yield* store
127+
.getAgentAwarenessSnapshot(shell.id)
128+
.pipe(Effect.withTracer(tracer));
129+
assert.deepEqual(targeted.threads.map(awareness), [awareness(shell)]);
130+
assert.deepEqual(targeted.threads[0]?.archivedAt, shell.archivedAt);
131+
}
132+
assert.isFalse(statements.some((statement) => /COUNT\(\*\)|GROUP BY/i.test(statement)));
133+
101134
const iso = (value: DateTime.Utc | null | undefined) =>
102135
value == null ? null : DateTime.formatIso(value);
103136
const trimmed = (
@@ -250,6 +283,7 @@ it.effect("routes full projection reads to readers and retains uncommitted write
250283
},
251284
});
252285
const { queries, trace } = traceSqlRouting();
286+
yield* store.getAgentAwarenessSnapshot().pipe(trace);
253287
const projection = yield* store.getThreadProjection(threadId).pipe(trace);
254288
assert.equal(projection.thread.id, threadId);
255289
assert.deepEqual(
@@ -309,6 +343,12 @@ it.effect("routes full projection reads to readers and retains uncommitted write
309343
SET payload_json = json_set(payload_json, '$.title', 'Uncommitted title')
310344
WHERE thread_id = ${threadId}
311345
`;
346+
assert.equal(
347+
(yield* store.getAgentAwarenessSnapshot()).threads.find(
348+
(thread) => thread.id === threadId,
349+
)?.title,
350+
"Uncommitted title",
351+
);
312352
assert.equal(
313353
(yield* store.getThreadProjection(threadId)).thread.title,
314354
"Uncommitted title",
@@ -8013,6 +8053,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
80138053
});
80148054
const project = { title: "Project" };
80158055
const environmentId = EnvironmentId.make("environment:held-completion");
8056+
yield* assertShellIndexCoversShells();
80168057
const held = yield* store.getThreadShell(threadId);
80178058
assert.equal(
80188059
held && projectThreadAwarenessV2({ environmentId, project, thread: held })?.phase,
@@ -8028,6 +8069,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
80288069
occurredAt: releasedAt,
80298070
payload: { ...providerThread, pendingBackgroundTasks: [], updatedAt: releasedAt },
80308071
});
8072+
yield* assertShellIndexCoversShells();
80318073
const released = yield* store.getThreadShell(threadId);
80328074
const state =
80338075
released && projectThreadAwarenessV2({ environmentId, project, thread: released });

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ProjectThreadAwarenessV2Input } from "@t3tools/shared/agentAwareness";
12
import { pendingGuestMessageCount } from "@t3tools/contracts";
23
import { applyThreadPullRequestLinkUpdate } from "../sourceControl/threadPullRequests.ts";
34
import { projectTurnItemForWire } from "./WireProjection.ts";
@@ -491,6 +492,12 @@ export interface ProjectionStoreV2Shape {
491492
},
492493
ProjectionStoreV2Error
493494
>;
495+
readonly getAgentAwarenessSnapshot: (
496+
threadId?: ThreadId,
497+
) => Effect.Effect<
498+
{ readonly snapshotSequence: number; readonly threads: ReadonlyArray<AgentAwarenessThread> },
499+
ProjectionStoreV2Error
500+
>;
494501
readonly getShellSnapshot: (
495502
options?: ShellSnapshotOptions,
496503
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, ProjectionStoreV2Error>;
@@ -980,6 +987,17 @@ type ShellPendingTurnItemRow = PayloadRow & {
980987
readonly owning_provider_instance_id: string | null;
981988
};
982989

990+
export type AgentAwarenessThread = ProjectThreadAwarenessV2Input["thread"] &
991+
Pick<
992+
OrchestrationV2ThreadShell,
993+
| "projectId"
994+
| "latestRunCompletedAt"
995+
| "archivedAt"
996+
| "activeRunId"
997+
| "latestRunId"
998+
| "hasActionableProposedPlan"
999+
>;
1000+
9831001
type ShellThreadRow = {
9841002
readonly thread_id: string;
9851003
readonly payload_json: string;
@@ -6077,7 +6095,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60776095
familyOf,
60786096
worktreePath,
60796097
threadIds,
6080-
}: ShellSnapshotOptions = {},
6098+
listedOnly = false,
6099+
}: ShellSnapshotOptions & { readonly listedOnly?: boolean } = {},
60816100
) =>
60826101
sql<ShellThreadRow>`
60836102
SELECT
@@ -6259,7 +6278,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62596278
candidate.ordinal DESC, candidate.run_id DESC
62606279
LIMIT 1
62616280
) AND blocked.status = 'failed'
6262-
WHERE t.deleted_at IS NULL${threadId === undefined ? sql`` : sql` AND t.thread_id = ${threadId}`}${
6281+
WHERE t.deleted_at IS NULL${
6282+
listedOnly
6283+
? sql` AND json_extract(t.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent'
6284+
AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1`
6285+
: sql``
6286+
}${threadId === undefined ? sql`` : sql` AND t.thread_id = ${threadId}`}${
62636287
projectId === undefined ? sql`` : sql` AND t.project_id = ${projectId}`
62646288
}${
62656289
location === "active"
@@ -6990,6 +7014,74 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
69907014
return { snapshotSequence: sequenceRows[0]?.snapshot_sequence ?? 0, threads };
69917015
}, Effect.mapError(shellSnapshotReadError));
69927016

7017+
const getAgentAwarenessSnapshot = Effect.fn("ProjectionStore.getAgentAwarenessSnapshot")(
7018+
function* (threadId?: ThreadId) {
7019+
const { threadRows, providerThreadRows, pendingTurnItemRows, sequenceRows } =
7020+
yield* sql.withTransaction(
7021+
Effect.gen(function* () {
7022+
const threadRows = yield* selectShellThreadRows(
7023+
threadId,
7024+
threadId === undefined ? { location: "active", listedOnly: true } : {},
7025+
);
7026+
const ids = threadRows.map((row) => ThreadId.make(row.thread_id));
7027+
const providerThreadRows =
7028+
ids.length === 0 ? [] : yield* selectShellProviderThreadRows(ids);
7029+
const pendingTurnItemRows =
7030+
ids.length === 0
7031+
? []
7032+
: yield* selectShellPendingTurnItemRows(
7033+
ids,
7034+
threadRows.flatMap((row) =>
7035+
row.active_run_id !== null && row.active_run_status === "running"
7036+
? [row.active_run_id]
7037+
: [],
7038+
),
7039+
);
7040+
const sequenceRows = yield* sql<{ readonly snapshot_sequence: number | null }>`
7041+
SELECT MAX(sequence) AS snapshot_sequence FROM orchestration_events
7042+
WHERE application_event_version = 2 AND aggregate_kind = 'thread'
7043+
`;
7044+
return { threadRows, providerThreadRows, pendingTurnItemRows, sequenceRows };
7045+
}),
7046+
);
7047+
const { providerThreadsByThreadId, pendingTurnItemsByThreadId } =
7048+
yield* pendingBackgroundDataByThreadId({ providerThreadRows, pendingTurnItemRows });
7049+
const threads = yield* Effect.forEach(threadRows, (row) =>
7050+
shellThreadStateFromRow({
7051+
row,
7052+
// Awareness never consumes timeline counts or inherited fork history.
7053+
runOrdinalsByThreadId: new Map(),
7054+
itemCountsByThreadId: new Map(),
7055+
shellItemCountsByThreadId: new Map(),
7056+
providerThreadsByThreadId,
7057+
pendingTurnItemsByThreadId,
7058+
}).pipe(
7059+
Effect.map((state): AgentAwarenessThread => ({
7060+
id: state.thread.id,
7061+
projectId: state.thread.projectId,
7062+
archivedAt: state.thread.archivedAt,
7063+
activeRunId: state.activeRunId,
7064+
latestRunId: state.latestRunId,
7065+
hasActionableProposedPlan: state.hasActionableProposedPlan,
7066+
title: state.thread.title,
7067+
lineage: state.thread.lineage,
7068+
modelSelection: state.thread.modelSelection,
7069+
settledOverride: state.thread.settledOverride,
7070+
updatedAt: state.updatedAt,
7071+
status: state.latestRunStatus,
7072+
activityRunStatus: state.activityRunStatus,
7073+
latestRunCompletedAt: state.latestRunCompletedAt,
7074+
latestRunStopRequested: state.latestRunStopRequested,
7075+
pendingRuntimeRequest: state.pendingRuntimeRequest,
7076+
pendingBackgroundTasks: state.pendingBackgroundTasks,
7077+
})),
7078+
),
7079+
);
7080+
return { snapshotSequence: sequenceRows[0]?.snapshot_sequence ?? 0, threads };
7081+
},
7082+
Effect.mapError(shellSnapshotReadError),
7083+
);
7084+
69937085
const getShellSnapshot: ProjectionStoreV2Shape["getShellSnapshot"] = (options) =>
69947086
sql
69957087
.withTransaction(readShellSnapshotRows(options))
@@ -7257,6 +7349,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
72577349

72587350
return {
72597351
apply,
7352+
getAgentAwarenessSnapshot: readOnReader(getAgentAwarenessSnapshot),
72607353
getShellSnapshot: readOnReader(getShellSnapshot),
72617354
getPullRequestSnapshot: readOnReader(getPullRequestSnapshot),
72627355
listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),

‎apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ it.effect(
212212
getLimitRecoveryCandidates: () => Effect.die("unused getLimitRecoveryCandidates"),
213213
getLimitRecoveryWakeAt: () => Effect.die("unused getLimitRecoveryWakeAt"),
214214
getOpenRemoteDelegatedTasks: Effect.die("unused getOpenRemoteDelegatedTasks"),
215+
getAgentAwarenessSnapshot: () => Effect.die("unused awareness snapshot"),
215216
getShellSnapshot: () => Effect.die("unused getShellSnapshot"),
216217
listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"),
217218
countThreads: () => Effect.die("unused countThreads"),

‎apps/server/src/orchestration-v2/ThreadManagementService.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ export interface ThreadManagementServiceShape {
402402
ProjectionRecords<K>,
403403
ThreadManagementProjectionLoadError | ThreadManagementThreadNotFoundError
404404
>;
405+
readonly getAgentAwarenessSnapshot: Orchestrator.OrchestratorV2["Service"]["getAgentAwarenessSnapshot"];
405406
readonly getShellSnapshot: (
406407
options?: Pick<
407408
ShellSnapshotOptions,
@@ -1520,6 +1521,7 @@ const make = Effect.gen(function* () {
15201521
getThreadTranscript,
15211522
getThreadSnapshotWindow,
15221523
getProjectThreadRecords,
1524+
getAgentAwarenessSnapshot: orchestrator.getAgentAwarenessSnapshot,
15231525
getShellSnapshot: orchestrator.getShellSnapshot,
15241526
listActiveSubagentThreadIds: orchestrator.listActiveSubagentThreadIds,
15251527
countThreads: orchestrator.countThreads,

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,23 @@ const makeTestRelay = Effect.fnUntraced(function* (
178178
});
179179
const currentShell = yield* Ref.make<OrchestrationV2ThreadShell | null>(shell());
180180
const shellReads: ThreadId[] = [];
181-
const catchUp = { shellSnapshotReads: 0 };
181+
const catchUp = { activitySnapshotReads: 0 };
182182
const threads = ThreadManagementService.ThreadManagementService.of({
183-
getThreadShell: (threadId) =>
184-
Effect.sync(() => shellReads.push(threadId)).pipe(
185-
Effect.andThen(options.readShell?.(threadId) ?? Ref.get(currentShell)),
186-
),
187-
getShellSnapshot: () =>
188-
Effect.sync(() => {
189-
catchUp.shellSnapshotReads += 1;
190-
return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };
191-
}),
183+
getThreadShell: unused,
184+
getShellSnapshot: unused,
185+
getAgentAwarenessSnapshot: (threadId) =>
186+
threadId === undefined
187+
? Effect.sync(() => {
188+
catchUp.activitySnapshotReads += 1;
189+
return { snapshotSequence: 1, threads: [] };
190+
})
191+
: Effect.sync(() => shellReads.push(threadId)).pipe(
192+
Effect.andThen(options.readShell?.(threadId) ?? Ref.get(currentShell)),
193+
Effect.map((thread) => ({
194+
snapshotSequence: 1,
195+
threads: thread === null ? [] : [thread],
196+
})),
197+
),
192198
listActiveSubagentThreadIds: unused,
193199
listAppOwnedChildThreadIds: unused,
194200
countThreads: unused,
@@ -834,21 +840,21 @@ it.effect("catches up after startup without polling after success", () =>
834840
const { relay, secrets, secretReads, catchUp } = yield* makeTestRelay();
835841
yield* relay.start();
836842
yield* TestClock.adjust("1 second");
837-
assert.equal(catchUp.shellSnapshotReads, 1);
843+
assert.equal(catchUp.activitySnapshotReads, 1);
838844

839845
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false"));
840846
yield* relay.requestCatchUp();
841847
yield* TestClock.adjust("1 second");
842-
assert.equal(catchUp.shellSnapshotReads, 1);
848+
assert.equal(catchUp.activitySnapshotReads, 1);
843849

844850
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true"));
845851
yield* relay.requestCatchUp();
846852
yield* TestClock.adjust("1 second");
847-
assert.equal(catchUp.shellSnapshotReads, 2);
853+
assert.equal(catchUp.activitySnapshotReads, 2);
848854

849855
const readsBeforeIdle = secretReads.length;
850856
yield* TestClock.adjust("10 minutes");
851-
assert.equal(catchUp.shellSnapshotReads, 2);
857+
assert.equal(catchUp.activitySnapshotReads, 2);
852858
assert.equal(secretReads.length, readsBeforeIdle);
853859

854860
yield* secrets.set(
@@ -857,6 +863,6 @@ it.effect("catches up after startup without polling after success", () =>
857863
);
858864
yield* relay.requestCatchUp();
859865
yield* TestClock.adjust("1 second");
860-
assert.equal(catchUp.shellSnapshotReads, 3);
866+
assert.equal(catchUp.activitySnapshotReads, 3);
861867
}).pipe(Effect.scoped),
862868
);

0 commit comments

Comments
 (0)