Skip to content

Commit 16158d3

Browse files
t3dotggclaude
andcommitted
Merge origin/main into fix/agent-scope-detached-daemons
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2 parents 3431ad8 + d7a8d6f commit 16158d3

20 files changed

Lines changed: 674 additions & 64 deletions

File tree

‎apps/server/integration/transferBudgetV2.integration.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ const layerManagement = Layer.unwrap(
8989
projections.getThreadSnapshotWindow(id, options).pipe(Effect.orDie),
9090
getThreadShell: (id) => projections.getThreadShell(id).pipe(Effect.orDie),
9191
getShellSnapshot: (options) => projections.getShellSnapshot(options).pipe(Effect.orDie),
92+
listActiveSubagentThreadIds: () =>
93+
projections.listActiveSubagentThreadIds().pipe(Effect.orDie),
9294
readShellSnapshot: (options) =>
9395
projections.readShellSnapshot(options).pipe(Effect.map(Effect.orDie), Effect.orDie),
9496
streamStoredEventsFrom: (input) =>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ export interface OrchestratorV2Shape {
371371
readonly getShellSnapshot: (
372372
options?: ShellSnapshotOptions,
373373
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, OrchestratorV2Error>;
374+
readonly listActiveSubagentThreadIds: () => Effect.Effect<
375+
ReadonlyArray<ThreadId>,
376+
OrchestratorV2Error
377+
>;
374378
readonly readShellSnapshot: (
375379
options?: ShellSnapshotOptions,
376380
) => Effect.Effect<
@@ -12237,6 +12241,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1223712241
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause }))),
1223812242
getShellSnapshot: (options) =>
1223912243
projectionStore.getShellSnapshot(options).pipe(Effect.mapError(shellProjectionError)),
12244+
listActiveSubagentThreadIds: () =>
12245+
projectionStore.listActiveSubagentThreadIds().pipe(Effect.mapError(shellProjectionError)),
1224012246
readShellSnapshot: (options) =>
1224112247
projectionStore
1224212248
.readShellSnapshot(options)
@@ -12382,6 +12388,13 @@ const layerUnavailable: Layer.Layer<OrchestratorV2> = Layer.succeed(
1238212388
cause: "Orchestration V2 live runtime is not configured.",
1238312389
}),
1238412390
),
12391+
listActiveSubagentThreadIds: () =>
12392+
Effect.fail(
12393+
new OrchestratorProjectionError({
12394+
threadId: ThreadId.make("thread:shell"),
12395+
cause: "Orchestration V2 live runtime is not configured.",
12396+
}),
12397+
),
1238512398
readShellSnapshot: () =>
1238612399
Effect.fail(
1238712400
new OrchestratorProjectionError({

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,36 @@ const assertProjectScopedShells = Effect.fn("assertProjectScopedShells")(functio
11051105
return { projectId, activeThreadId, forkThreadId, otherThreadId };
11061106
});
11071107

1108+
it.effect("lists only active subagent IDs without loading shell data", () =>
1109+
Effect.gen(function* () {
1110+
const store = yield* ProjectionStore.ProjectionStoreV2;
1111+
const sql = yield* SqlClient.SqlClient;
1112+
const createdAt = "2026-01-05T00:00:00.000Z";
1113+
for (const [id, relationship, archivedAt, deletedAt] of [
1114+
["parent", null, null, null],
1115+
["active-subagent", "subagent", null, null],
1116+
["archived-subagent", "subagent", createdAt, null],
1117+
["deleted-subagent", "subagent", null, createdAt],
1118+
] as const) {
1119+
yield* sql`
1120+
INSERT INTO orchestration_v2_projection_threads (
1121+
thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
1122+
created_at, updated_at, archived_at, deleted_at, payload_json
1123+
) VALUES (
1124+
${id}, 'project:test', ${id}, 'codex', 'full-access', 'default',
1125+
${createdAt}, ${createdAt}, ${archivedAt}, ${deletedAt},
1126+
${JSON.stringify({ lineage: { relationshipToParent: relationship }, archivedAt })}
1127+
)
1128+
`;
1129+
}
1130+
1131+
assert.sameMembers(
1132+
[...(yield* store.listActiveSubagentThreadIds())],
1133+
[ThreadId.make("active-subagent")],
1134+
);
1135+
}).pipe(Effect.provide(layerTest)),
1136+
);
1137+
11081138
it.layer(layerTest)("ProjectionStoreV2", (it) => {
11091139
// regression: #21969 keep corrupt plan JSON in the typed read error
11101140
it.effect("returns a typed read error for malformed stored plan JSON", () =>

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ export interface ProjectionStoreV2Shape {
424424
readonly getShellSnapshot: (
425425
options?: ShellSnapshotOptions,
426426
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, ProjectionStoreV2Error>;
427+
readonly listActiveSubagentThreadIds: () => Effect.Effect<
428+
ReadonlyArray<ThreadId>,
429+
ProjectionStoreV2Error
430+
>;
427431
/**
428432
* Callers that combine the shell with other reads run this inside their own
429433
* transaction and the returned decode after it commits, so the shared
@@ -6430,6 +6434,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64306434
.withTransaction(readShellSnapshotRows(options))
64316435
.pipe(Effect.mapError(shellSnapshotReadError), Effect.flatMap(decodeShellSnapshot));
64326436

6437+
const listActiveSubagentThreadIds = Effect.fn("ProjectionStore.listActiveSubagentThreadIds")(
6438+
function* () {
6439+
const rows = yield* sql<{ readonly thread_id: string }>`
6440+
SELECT thread_id
6441+
FROM orchestration_v2_projection_threads
6442+
WHERE deleted_at IS NULL
6443+
AND json_extract(payload_json, '$.archivedAt') IS NULL
6444+
AND json_extract(payload_json, '$.lineage.relationshipToParent') = 'subagent'
6445+
`;
6446+
return rows.map((row) => ThreadId.make(row.thread_id));
6447+
},
6448+
Effect.mapError(shellSnapshotReadError),
6449+
);
6450+
64336451
const getThreadShell: ProjectionStoreV2Shape["getThreadShell"] = (threadId) =>
64346452
sql
64356453
.withTransaction(
@@ -6522,6 +6540,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
65226540
return {
65236541
apply,
65246542
getShellSnapshot: readOnReader(getShellSnapshot),
6543+
listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
65256544
readShellSnapshot: readOnReader(readShellSnapshot),
65266545
getThreadShell: readOnReader(getThreadShell),
65276546
getThread: readOnReader(getThread),
@@ -6630,6 +6649,19 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
66306649
archivedThreads: visible.filter((thread) => thread.archivedAt !== null),
66316650
};
66326651
}),
6652+
listActiveSubagentThreadIds: Effect.fn("ProjectionStore.listActiveSubagentThreadIds")(
6653+
function* () {
6654+
const existing = (yield* Ref.get(replayState)).projections;
6655+
return [...existing.values()]
6656+
.filter(
6657+
({ thread }) =>
6658+
thread.deletedAt === null &&
6659+
thread.archivedAt === null &&
6660+
thread.lineage.relationshipToParent === "subagent",
6661+
)
6662+
.map(({ thread }) => thread.id);
6663+
},
6664+
),
66336665
readShellSnapshot: (options) =>
66346666
service.getShellSnapshot(options).pipe(Effect.map(Effect.succeed)),
66356667
getThreadShell: Effect.fn("ProjectionStore.getThreadShell")(function* (threadId) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ it.effect(
211211
apply: () => Effect.void,
212212
getLimitRecoveryCandidates: () => Effect.die("unused getLimitRecoveryCandidates"),
213213
getShellSnapshot: () => Effect.die("unused getShellSnapshot"),
214+
listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"),
214215
readShellSnapshot: () => Effect.die("unused readShellSnapshot"),
215216
getThreadShell: () => Effect.die("unused getThreadShell"),
216217
getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ export interface ThreadManagementServiceShape {
362362
readonly getShellSnapshot: (options?: {
363363
readonly location?: "active" | "archive";
364364
}) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, Orchestrator.OrchestratorV2Error>;
365+
readonly listActiveSubagentThreadIds: Orchestrator.OrchestratorV2["Service"]["listActiveSubagentThreadIds"];
365366
readonly readShellSnapshot: (options?: {
366367
readonly location?: "active" | "archive";
367368
}) => Effect.Effect<
@@ -1173,6 +1174,7 @@ const make = Effect.gen(function* () {
11731174
getProjectThreadRecords,
11741175
getProjectThread,
11751176
getShellSnapshot: orchestrator.getShellSnapshot,
1177+
listActiveSubagentThreadIds: orchestrator.listActiveSubagentThreadIds,
11761178
readShellSnapshot: orchestrator.readShellSnapshot,
11771179
getThreadShell: orchestrator.getThreadShell,
11781180
listProjectThreads,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
187187
catchUp.shellSnapshotReads += 1;
188188
return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };
189189
}),
190+
listActiveSubagentThreadIds: unused,
190191
readShellSnapshot: unused,
191192
ensureLegacyTranscript: unused,
192193
dispatch: unused,

‎apps/server/src/ws.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ it.live.each(["thread", "shell"] as const)(
252252
const items = yield* subscription.pipe(
253253
Effect.provideService(ThreadManagement.ThreadManagementService, {
254254
ensureLegacyTranscript: () => Effect.void,
255-
getShellSnapshot: () => Effect.succeed({ threads: [] }),
255+
getShellSnapshot: () => Effect.die("shell resume must not load a full snapshot"),
256+
listActiveSubagentThreadIds: () => Effect.succeed([]),
256257
streamStoredEventsFrom: (
257258
input: Parameters<
258259
ThreadManagement.ThreadManagementService["Service"]["streamStoredEventsFrom"]

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,12 +1172,7 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
11721172
});
11731173
}
11741174

1175-
const currentShell = yield* threadManagement.getShellSnapshot({ location: "active" });
1176-
subagentThreadIds = new Set(
1177-
currentShell.threads
1178-
.filter((thread) => thread.lineage.relationshipToParent === "subagent")
1179-
.map((thread) => thread.id),
1180-
);
1175+
subagentThreadIds = new Set(yield* threadManagement.listActiveSubagentThreadIds());
11811176

11821177
const loaded = yield* loadProjectMetadataSnapshot(highWater);
11831178
const replay = toShellStream(

‎packages/provider-core/src/server/managedProvider.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as Layer from "effect/Layer";
1313
import * as PubSub from "effect/PubSub";
1414
import * as Ref from "effect/Ref";
1515
import * as Stream from "effect/Stream";
16+
import * as TestClock from "effect/testing/TestClock";
1617

1718
import * as ProviderHost from "./ProviderHost.ts";
1819
import { makeManagedServerProvider } from "./managedProvider.ts";
@@ -179,6 +180,80 @@ describe("makeManagedServerProvider", () => {
179180
).pipe(Effect.provide(layerAlwaysRunTest)),
180181
);
181182

183+
it.effect("skips periodic provider refreshes without foreground provider-status demand", () =>
184+
Effect.scoped(
185+
Effect.gen(function* () {
186+
const checkCalls = yield* Ref.make(0);
187+
const initialCheckDone = yield* Deferred.make<void>();
188+
yield* makeManagedServerProvider<TestSettings>({
189+
resolveMaintenance: () => Effect.succeed(maintenanceCapabilities),
190+
getSettings: Effect.succeed({ enabled: true }),
191+
streamSettings: Stream.empty,
192+
haveSettingsChanged: (previous, next) => previous.enabled !== next.enabled,
193+
refreshOnInterval: () => true,
194+
initialSnapshot: () => Effect.succeed(initialSnapshot),
195+
checkProvider: Ref.updateAndGet(checkCalls, (count) => count + 1).pipe(
196+
Effect.tap((count) =>
197+
count === 1
198+
? Deferred.succeed(initialCheckDone, undefined).pipe(Effect.ignore)
199+
: Effect.void,
200+
),
201+
Effect.as(refreshedSnapshot),
202+
),
203+
refreshInterval: "1 second",
204+
});
205+
206+
yield* Deferred.await(initialCheckDone);
207+
yield* TestClock.adjust("1 second");
208+
yield* Effect.yieldNow;
209+
210+
assert.strictEqual(yield* Ref.get(checkCalls), 1);
211+
}),
212+
).pipe(
213+
Effect.provide(
214+
Layer.mergeAll(layerProviderHost({ runBackgroundWork: false }), TestClock.layer()),
215+
),
216+
),
217+
);
218+
219+
it.effect("stops periodic recovery checks once the snapshot is ready", () =>
220+
Effect.scoped(
221+
Effect.gen(function* () {
222+
const checkCalls = yield* Ref.make(0);
223+
const releaseInitialCheck = yield* Deferred.make<void>();
224+
const failedSnapshot = { ...refreshedSnapshot, status: "error" as const };
225+
const provider = yield* makeManagedServerProvider<TestSettings>({
226+
resolveMaintenance: () => Effect.succeed(maintenanceCapabilities),
227+
getSettings: Effect.succeed({ enabled: true }),
228+
streamSettings: Stream.empty,
229+
haveSettingsChanged: (previous, next) => previous.enabled !== next.enabled,
230+
refreshOnInterval: (snapshot) => snapshot.status === "error",
231+
initialSnapshot: () => Effect.succeed(initialSnapshot),
232+
checkProvider: Deferred.await(releaseInitialCheck).pipe(
233+
Effect.andThen(Ref.updateAndGet(checkCalls, (count) => count + 1)),
234+
Effect.map((count) => (count === 1 ? failedSnapshot : refreshedSnapshot)),
235+
),
236+
refreshInterval: "1 second",
237+
});
238+
const updates = yield* provider.streamChanges.pipe(
239+
Stream.take(2),
240+
Stream.runCollect,
241+
Effect.forkChild,
242+
);
243+
yield* Effect.yieldNow;
244+
yield* Deferred.succeed(releaseInitialCheck, undefined);
245+
yield* TestClock.adjust("1 second");
246+
247+
assert.deepStrictEqual(Array.from(yield* Fiber.join(updates)), [
248+
failedSnapshot,
249+
refreshedSnapshot,
250+
]);
251+
yield* TestClock.adjust("3 seconds");
252+
assert.strictEqual(yield* Ref.get(checkCalls), 2);
253+
}),
254+
).pipe(Effect.provide(Layer.mergeAll(layerAlwaysRunTest, TestClock.layer()))),
255+
);
256+
182257
it.effect("reruns the provider check when streamed settings change", () =>
183258
Effect.scoped(
184259
Effect.gen(function* () {

0 commit comments

Comments
 (0)