Skip to content

Commit f5aba54

Browse files
authored
refactor(agents): split Claude live sessions by concept (#121566)
* refactor(agents): split claude-live-session into concept modules * refactor(agents): delete duplicated live-session helpers * fix(agents): fence live-session close during pending spawn * chore(lint): ratchet max-lines baseline after live-session split * style(agents): satisfy lint on live-session split modules * fix(agents): fence live session close state * refactor(agents): extract Claude live turn timeouts * fix(agents): preserve exec policy after Claude live split * test(agents): complete Claude live policy session fixture
1 parent 17854d4 commit f5aba54

34 files changed

Lines changed: 6222 additions & 5747 deletions

‎config/max-lines-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ src/agents/cli-auth-epoch.test.ts
352352
src/agents/cli-runner.reliability.test.ts
353353
src/agents/cli-runner.spawn.test.ts
354354
src/agents/cli-runner.ts
355-
src/agents/cli-runner/claude-live-session.ts
356355
src/agents/cli-runner/execute.supervisor-capture.test.ts
357356
src/agents/cli-runner/prepare.test.ts
358357
src/agents/cli-runner/prepare.ts

‎scripts/lib/ci-node-test-plan.mts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ const COMPACT_GROUP_SECONDS_HINTS = new Map<string, number>([
125125
// Reliability's runtime-free provider check dropped its wall time from
126126
// ~245s to ~5s; the narrow anthropic cli-api artifact removes the same
127127
// full-barrel evaluation for the remaining facade importers (spawn).
128+
// The live-session extraction rebalanced these stripes without changing the
129+
// fleet-scale import wall that dominates each compact group.
128130
["agentic-agents-core-runner-cli-1", 8],
129-
["agentic-agents-core-runner-cli-2", 9],
131+
["agentic-agents-core-runner-cli-2", 8],
130132
["agentic-agents-core-runner-cli-3", 8],
131133
["agentic-agents-core-runner-commands", 27],
132134
["agentic-agents-core-runner-embedded", 20],
@@ -143,7 +145,7 @@ const COMPACT_GROUP_SECONDS_HINTS = new Map<string, number>([
143145
["agentic-agents-embedded-incomplete-turn", 146],
144146
["agentic-agents-embedded-overflow-compaction", 150],
145147
["agentic-agents-embedded-run", 30],
146-
["agentic-agents-support", 105],
148+
["agentic-agents-support", 110],
147149
["agentic-agents-tools", 42],
148150
["agentic-cli", 72],
149151
["agentic-command-support", 41],
@@ -228,7 +230,7 @@ const STRIPE_FILE_SECONDS_HINTS = new Map<string, number>([
228230
["src/agents/cli-runner.context-engine.test.ts", 6],
229231
// Fresh profile: 5.1s total, 3.8s import; retain a conservative packing hint.
230232
["src/agents/cli-runner.reliability.test.ts", 8],
231-
["src/agents/cli-runner.spawn.test.ts", 18],
233+
["src/agents/cli-runner.spawn.test.ts", 45],
232234
["src/auto-reply/reply/commands-export-session.test.ts", 8],
233235
["src/auto-reply/reply/commands-gating.test.ts", 6],
234236
["src/auto-reply/reply/commands-learn.test.ts", 8],

‎src/agents/cli-runner.before-agent-reply-cron.test.ts‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const {
3838
runBeforeAgentRunMock,
3939
executePreparedCliRunMock,
4040
prepareCliRunContextMock,
41-
closeClaudeLiveSessionForContextMock,
41+
closeClaudeSessionMock,
4242
closeMcpLoopbackServerMock,
4343
retireSessionMcpRuntimeForSessionKeyMock,
4444
retireSessionMcpRuntimeMock,
@@ -55,7 +55,7 @@ const {
5555
(_context: unknown, _cliSessionIdToUse?: string) => Promise<CliOutput>
5656
>(async () => ({ text: "" })),
5757
prepareCliRunContextMock: vi.fn(),
58-
closeClaudeLiveSessionForContextMock: vi.fn(),
58+
closeClaudeSessionMock: vi.fn(),
5959
closeMcpLoopbackServerMock: vi.fn(),
6060
retireSessionMcpRuntimeForSessionKeyMock: vi.fn(),
6161
retireSessionMcpRuntimeMock: vi.fn(),
@@ -80,11 +80,14 @@ vi.mock("./cli-runner/execute.runtime.js", () => ({
8080
executePreparedCliRun: executePreparedCliRunMock,
8181
}));
8282

83-
vi.mock("./cli-runner/claude-live-session.js", () => ({
84-
closeClaudeLiveSessionForContext: closeClaudeLiveSessionForContextMock,
85-
getClaudeLiveSessionGenerationForOwner: vi.fn(() => undefined),
86-
hasClaudeLiveSessionForOwner: vi.fn(() => false),
87-
shouldUseClaudeLiveSession: vi.fn(() => false),
83+
vi.mock("./cli-runner/claude-live-registry.js", () => ({
84+
closeClaudeSession: closeClaudeSessionMock,
85+
getClaudeGeneration: vi.fn(() => undefined),
86+
hasClaudeSession: vi.fn(() => false),
87+
}));
88+
89+
vi.mock("./cli-runner/claude-live-session-policy.js", () => ({
90+
acceptsClaudeLive: vi.fn(() => false),
8891
}));
8992

9093
vi.mock("../gateway/mcp-http.js", () => ({
@@ -167,7 +170,7 @@ beforeEach(() => {
167170
prepareCliRunContextMock.mockImplementation(async (params) =>
168171
makeStubContext(params as typeof baseRunParams & { trigger?: string }),
169172
);
170-
closeClaudeLiveSessionForContextMock.mockReset();
173+
closeClaudeSessionMock.mockReset();
171174
closeMcpLoopbackServerMock.mockReset();
172175
retireSessionMcpRuntimeForSessionKeyMock.mockReset();
173176
retireSessionMcpRuntimeForSessionKeyMock.mockResolvedValue(true);
@@ -493,7 +496,7 @@ describe("runCliAgent before_agent_reply seam", () => {
493496
});
494497

495498
expect(error).toMatchObject({ message: "CLI process failed" });
496-
expect(closeClaudeLiveSessionForContextMock).toHaveBeenCalledTimes(1);
499+
expect(closeClaudeSessionMock).toHaveBeenCalledTimes(1);
497500
expect(events.find((event) => event.type === "harness.run.error")).toMatchObject({
498501
type: "harness.run.error",
499502
phase: "send",
@@ -520,9 +523,7 @@ describe("runCliAgent before_agent_reply seam", () => {
520523

521524
it("classifies a surfaced outer cleanup failure as cleanup", async () => {
522525
executePreparedCliRunMock.mockResolvedValueOnce({ text: "real Claude reply" });
523-
closeClaudeLiveSessionForContextMock.mockRejectedValueOnce(
524-
new Error("managed session cleanup failed"),
525-
);
526+
closeClaudeSessionMock.mockRejectedValueOnce(new Error("managed session cleanup failed"));
526527

527528
const { error, events } = await captureRejectedClaudeRun({
528529
...baseRunParams,
@@ -822,12 +823,13 @@ describe("runCliAgent before_agent_reply seam", () => {
822823
await runCliAgent({ ...baseRunParams, cleanupCliLiveSessionOnRunEnd: true });
823824

824825
expect(executePreparedCliRunMock).toHaveBeenCalledTimes(1);
825-
expect(closeClaudeLiveSessionForContextMock).toHaveBeenCalledTimes(1);
826-
expect(closeClaudeLiveSessionForContextMock).toHaveBeenCalledWith(
826+
expect(closeClaudeSessionMock).toHaveBeenCalledTimes(1);
827+
expect(closeClaudeSessionMock).toHaveBeenCalledWith(
827828
await expectDefined(
828829
prepareCliRunContextMock.mock.results[0],
829830
"prepareCliRunContextMock.mock.results[0] test invariant",
830831
).value,
832+
"restart",
831833
);
832834
});
833835

‎src/agents/cli-runner.helpers.test.ts‎

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { escapeRegExp } from "../shared/regexp.js";
1313
import { captureEnv, setTestEnvValue } from "../test-utils/env.js";
1414
import {
1515
buildCliArgs,
16-
buildClaudeOwnerKey,
1716
prepareCliPromptImagePayload,
1817
resolveCliRunQueueKey,
1918
writeCliSystemPromptFile,
@@ -740,32 +739,3 @@ describe("resolveCliRunQueueKey", () => {
740739
).toBe("claude-cli:owner:abcd1234");
741740
});
742741
});
743-
744-
describe("buildClaudeOwnerKey", () => {
745-
it("is deterministic and distinguishes session keys", () => {
746-
const base = {
747-
agentAccountId: "acct-1",
748-
agentId: "agent-main",
749-
authProfileId: "profile-a",
750-
sessionId: "sess-1",
751-
sessionKey: "key-a",
752-
};
753-
const a1 = buildClaudeOwnerKey(base);
754-
const a2 = buildClaudeOwnerKey(base);
755-
expect(a1).toBe(a2);
756-
const b = buildClaudeOwnerKey({ ...base, sessionKey: "key-b" });
757-
expect(a1).not.toBe(b);
758-
});
759-
760-
it("matches the legacy buildClaudeLiveKey hash for a frozen fixture (DO NOT EDIT — splits queue from live-session map)", () => {
761-
expect(
762-
buildClaudeOwnerKey({
763-
agentAccountId: "acct-1",
764-
agentId: "agent-main",
765-
authProfileId: "profile-a",
766-
sessionId: "sess-1",
767-
sessionKey: "key-a",
768-
}),
769-
).toBe("718b9a6cf473526c3c357883dfc8f1da1cf90b709d9ed38d675b52314abe6800");
770-
});
771-
});

0 commit comments

Comments
 (0)