fix(orchestration-v2): guard Claude activation generations
This commit is contained in:
1 parent
e4a4c60121
commit
d45c3e7f9a
2 files changed
+85
-16
No files matched your search
@@ -57,6 +57,7 @@ import {
|
||||
CLAUDE_T3_MCP_TOOL_WILDCARD,
|
||||
ClaudeProviderCapabilitiesV2,
|
||||
ClaudeAgentSdkQueryRunnerError,
|
||||
claudeSubagentStatusPinsSession,
|
||||
claudeEffectiveQueryPolicyKey,
|
||||
claudeMcpQueryOverrides,
|
||||
claudeQueryMessages,
|
||||
@@ -83,16 +84,27 @@ const CLAUDE_TEST_RUNTIME_POLICY = ProviderAdapterV2RuntimePolicy.make({
|
||||
cwd: "/workspace",
|
||||
});
|
||||
|
||||
type TestClaudeSubagentRegistryEntry = {
|
||||
readonly task: { readonly status: "running" | "waiting" | "completed" };
|
||||
readonly activation: { readonly id: string };
|
||||
};
|
||||
|
||||
function testRegistryEntry(
|
||||
status: TestClaudeSubagentRegistryEntry["task"]["status"],
|
||||
activationId: string,
|
||||
): TestClaudeSubagentRegistryEntry {
|
||||
return { task: { status }, activation: { id: activationId } };
|
||||
}
|
||||
|
||||
describe("ClaudeAdapterV2 subagent registry", () => {
|
||||
it.effect("rejects an active update that loses the registry race to a terminal update", () =>
|
||||
Effect.gen(function* () {
|
||||
type RegistryEntry = {
|
||||
readonly task: { readonly status: "running" | "waiting" | "completed" };
|
||||
};
|
||||
const observed: RegistryEntry = { task: { status: "running" } };
|
||||
const terminal: RegistryEntry = { task: { status: "completed" } };
|
||||
const stale: RegistryEntry = { task: { status: "waiting" } };
|
||||
const registry = yield* Ref.make(new Map<string, RegistryEntry>([["task-race", terminal]]));
|
||||
const observed = testRegistryEntry("running", "activation-1");
|
||||
const terminal = testRegistryEntry("completed", "activation-1");
|
||||
const stale = testRegistryEntry("waiting", "activation-1");
|
||||
const registry = yield* Ref.make(
|
||||
new Map<string, TestClaudeSubagentRegistryEntry>([["task-race", terminal]]),
|
||||
);
|
||||
|
||||
const committed = yield* commitClaudeSubagentRegistryEntry({
|
||||
registry,
|
||||
@@ -110,13 +122,12 @@ describe("ClaudeAdapterV2 subagent registry", () => {
|
||||
|
||||
it.effect("rejects a stale terminal update after the subagent has resumed", () =>
|
||||
Effect.gen(function* () {
|
||||
type RegistryEntry = {
|
||||
readonly task: { readonly status: "running" | "completed" };
|
||||
};
|
||||
const observed: RegistryEntry = { task: { status: "running" } };
|
||||
const resumed: RegistryEntry = { task: { status: "running" } };
|
||||
const staleTerminal: RegistryEntry = { task: { status: "completed" } };
|
||||
const registry = yield* Ref.make(new Map<string, RegistryEntry>([["task-race", resumed]]));
|
||||
const observed = testRegistryEntry("running", "activation-1");
|
||||
const resumed = testRegistryEntry("running", "activation-2");
|
||||
const staleTerminal = testRegistryEntry("completed", "activation-1");
|
||||
const registry = yield* Ref.make(
|
||||
new Map<string, TestClaudeSubagentRegistryEntry>([["task-race", resumed]]),
|
||||
);
|
||||
|
||||
const committed = yield* commitClaudeSubagentRegistryEntry({
|
||||
registry,
|
||||
@@ -131,6 +142,51 @@ describe("ClaudeAdapterV2 subagent registry", () => {
|
||||
assert.strictEqual((yield* Ref.get(registry)).get("task-race"), resumed);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("accepts a terminal update after same-activation progress wins the race", () =>
|
||||
Effect.gen(function* () {
|
||||
const observed = testRegistryEntry("running", "activation-1");
|
||||
const progressed = testRegistryEntry("running", "activation-1");
|
||||
const terminal = testRegistryEntry("completed", "activation-1");
|
||||
const registry = yield* Ref.make(
|
||||
new Map<string, TestClaudeSubagentRegistryEntry>([["task-race", progressed]]),
|
||||
);
|
||||
|
||||
const committed = yield* commitClaudeSubagentRegistryEntry({
|
||||
registry,
|
||||
taskId: "task-race",
|
||||
observed,
|
||||
candidate: terminal,
|
||||
activeStart: false,
|
||||
isReopen: false,
|
||||
});
|
||||
|
||||
assert.isTrue(committed);
|
||||
assert.strictEqual((yield* Ref.get(registry)).get("task-race"), terminal);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("ClaudeAdapterV2 background-work status", () => {
|
||||
it("pins the session for every live subagent status", () => {
|
||||
assert.deepEqual(
|
||||
[
|
||||
"pending",
|
||||
"running",
|
||||
"waiting",
|
||||
"idle",
|
||||
"completed",
|
||||
"failed",
|
||||
"cancelled",
|
||||
"interrupted",
|
||||
].map((status) =>
|
||||
claudeSubagentStatusPinsSession(
|
||||
status as Parameters<typeof claudeSubagentStatusPinsSession>[0],
|
||||
),
|
||||
),
|
||||
[true, true, true, false, false, false, false, false],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
function makeClaudeTestAppThread(input: {
|
||||
|
||||
@@ -2200,6 +2200,7 @@ interface ActiveClaudeProviderRetry {
|
||||
|
||||
interface ClaudeSubagentRegistryEntry {
|
||||
readonly task: Pick<OrchestrationV2Subagent, "status">;
|
||||
readonly activation: { readonly id: string };
|
||||
}
|
||||
|
||||
export const commitClaudeSubagentRegistryEntry = Effect.fnUntraced(function* <
|
||||
@@ -2219,8 +2220,14 @@ export const commitClaudeSubagentRegistryEntry = Effect.fnUntraced(function* <
|
||||
(registered.task.status === "completed" ||
|
||||
registered.task.status === "failed" ||
|
||||
registered.task.status === "cancelled");
|
||||
const sameObservedActivation =
|
||||
registered !== undefined &&
|
||||
input.observed !== undefined &&
|
||||
registered.activation.id === input.observed.activation.id;
|
||||
if (
|
||||
(!input.activeStart && registered !== input.observed) ||
|
||||
(!input.activeStart &&
|
||||
registered !== input.observed &&
|
||||
(registeredSettled || !sameObservedActivation)) ||
|
||||
(registeredSettled && input.activeStart && !(input.isReopen && registered === input.observed))
|
||||
) {
|
||||
return [false, current] as const;
|
||||
@@ -2229,6 +2236,12 @@ export const commitClaudeSubagentRegistryEntry = Effect.fnUntraced(function* <
|
||||
});
|
||||
});
|
||||
|
||||
export function claudeSubagentStatusPinsSession(
|
||||
status: OrchestrationV2Subagent["status"],
|
||||
): boolean {
|
||||
return status === "pending" || status === "running" || status === "waiting";
|
||||
}
|
||||
|
||||
interface ActiveClaudeSubagent {
|
||||
task: OrchestrationV2Subagent;
|
||||
activation: OrchestrationV2SubagentActivation;
|
||||
@@ -5146,7 +5159,7 @@ export function makeClaudeAdapterV2(
|
||||
}
|
||||
}
|
||||
for (const subagent of (yield* Ref.get(sessionSubagentsByTaskId)).values()) {
|
||||
if (subagent.task.status === "running") {
|
||||
if (claudeSubagentStatusPinsSession(subagent.task.status)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user