Skip to content

Commit a770218

Browse files
authored
refactor: consolidate duplicate helpers (#121431)
* refactor(agents): consolidate session slug creation * refactor(commands): consolidate config validation * refactor(infra): consolidate stale pid termination * refactor(tui): reuse canonical sleep helper * chore(scripts): burn resolved entries from collision baseline * fix(refactor): preserve cross-platform helper contracts * fix(plugin-sdk): keep approval helper private * chore(scripts): burn resolved wrapper shadowing entry * chore(scripts): refresh wrapper shadowing baseline * test(agents): target canonical session slug helper * chore(plugin-sdk): refresh API baseline
1 parent cef6e69 commit a770218

29 files changed

Lines changed: 112 additions & 139 deletions

‎docs/.generated/plugin-sdk-api-baseline.jsonl‎

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎scripts/lib/export-name-collision-baseline.json‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,6 @@
261261
"src/agents/tools/transcripts-tool-runtime.ts"
262262
]
263263
},
264-
{
265-
"name": "createSessionSlug",
266-
"files": [
267-
"src/agents/bash-process-registry.ts",
268-
"src/agents/session-slug.ts"
269-
]
270-
},
271264
{
272265
"name": "createSuiteTempRootTracker",
273266
"files": [
@@ -1533,14 +1526,6 @@
15331526
],
15341527
"sdk": true
15351528
},
1536-
{
1537-
"name": "sleep",
1538-
"files": [
1539-
"src/tui/tui-pty-test-support.ts",
1540-
"src/utils/sleep.ts"
1541-
],
1542-
"sdk": true
1543-
},
15441529
{
15451530
"name": "startGatewayServer",
15461531
"files": [

‎scripts/lib/wrapper-shadowing-baseline.json‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@
5959
"wrapped": "src/shared/deferred.ts",
6060
"wrapper": "src/plugin-sdk/extension-shared.ts"
6161
},
62-
{
63-
"name": "createSessionSlug",
64-
"wrapped": "src/agents/session-slug.ts",
65-
"wrapper": "src/agents/bash-process-registry.ts"
66-
},
6762
{
6863
"name": "DEFAULT_PROGRESS_DRAFT_LABELS",
6964
"wrapped": "src/shared/progress-labels.ts",
@@ -350,11 +345,6 @@
350345
"wrapped": "src/config/sessions/store-entry.ts",
351346
"wrapper": "src/plugin-sdk/session-store-runtime.ts"
352347
},
353-
{
354-
"name": "resolveSessionTranscriptActiveLeafEntryId",
355-
"wrapped": "src/config/sessions/session-accessor.sqlite-message-cut.ts",
356-
"wrapper": "src/config/sessions/session-accessor.message-cut.ts"
357-
},
358348
{
359349
"name": "resolveStorePath",
360350
"wrapped": "src/config/sessions/paths.ts",

‎src/agents/bash-process-registry.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import {
1010
acknowledgeNotifyOnExit,
1111
addSession,
1212
appendOutput,
13-
createSessionSlug,
1413
deleteSession,
1514
drainFinishedSession,
1615
drainSession,
1716
getActiveBackgroundExecSessionCount,
1817
getFinishedSession,
1918
getFinishedSessionForProcess,
19+
isProcessSessionIdTaken,
2020
listFinishedSessions,
2121
listRunningSessions,
2222
markBackgrounded,
@@ -28,6 +28,7 @@ import {
2828
} from "./bash-process-registry.js";
2929
import { createProcessSessionFixture } from "./bash-process-registry.test-helpers.js";
3030
import { resetProcessRegistryForTests } from "./bash-process-registry.test-support.js";
31+
import { createSessionSlug } from "./session-slug.js";
3132

3233
const randomMocks = vi.hoisted(() => ({
3334
generateSecureInt: vi.fn(() => 0),
@@ -370,11 +371,11 @@ describe("bash process registry", () => {
370371
addSession(session);
371372
markBackgrounded(session);
372373
deleteSession(session.id);
373-
expect(createSessionSlug()).toBe("amber-atlas-2");
374+
expect(createSessionSlug(isProcessSessionIdTaken)).toBe("amber-atlas-2");
374375

375376
session.backgrounded = false;
376377
markExited(session, 0, null, "completed");
377-
expect(createSessionSlug()).toBe("amber-atlas");
378+
expect(createSessionSlug(isProcessSessionIdTaken)).toBe("amber-atlas");
378379
});
379380

380381
it("clears background activity in the test reset", () => {

‎src/agents/bash-process-registry.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type { EventSessionRoutingPolicy } from "../infra/event-session-routing.j
99
import type { TerminationReason } from "../process/supervisor/types.js";
1010
import type { DeliveryContext } from "../utils/delivery-context.types.js";
1111
import { readEnvInt } from "./bash-tools.shared.js";
12-
import { createSessionSlug as createSessionSlugId } from "./session-slug.js";
1312

1413
const DEFAULT_JOB_TTL_MS = 30 * 60 * 1000; // 30 minutes
1514
const MIN_JOB_TTL_MS = 60 * 1000; // 1 minute
@@ -132,17 +131,13 @@ let finishedSessionOutputChars = 0;
132131

133132
let sweeper: NodeJS.Timeout | null = null;
134133

135-
function isSessionIdTaken(id: string) {
134+
/** Return whether a process session id is live, retained, or reserved for notification. */
135+
export function isProcessSessionIdTaken(id: string): boolean {
136136
return (
137137
runningSessions.has(id) || finishedSessions.has(id) || activeBackgroundExecSessionIds.has(id)
138138
);
139139
}
140140

141-
/** Creates a unique short session id that avoids running and retained sessions. */
142-
export function createSessionSlug(): string {
143-
return createSessionSlugId(isSessionIdTaken);
144-
}
145-
146141
/** Adds a running session and starts retention sweeping if needed. */
147142
export function addSession(session: ProcessSession) {
148143
runningSessions.set(session.id, session);

‎src/agents/bash-tools.exec-runtime.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ describe("runExecProcess cursor tracking", () => {
193193
describe("sandbox exec preparation failures", () => {
194194
it("settles the registered session once when buildExecSpec rejects", async () => {
195195
const registry = await import("./bash-process-registry.js");
196+
const sessionSlugs = await import("./session-slug.js");
196197
const sessionId = "sandbox-preparation-failure";
197-
const sessionSlug = vi.spyOn(registry, "createSessionSlug").mockReturnValue(sessionId);
198+
const sessionSlug = vi.spyOn(sessionSlugs, "createSessionSlug").mockReturnValue(sessionId);
198199
const preparation =
199200
createDeferred<Awaited<ReturnType<NonNullable<BashSandboxConfig["buildExecSpec"]>>>>();
200201
const finalizeExec = vi.fn<NonNullable<BashSandboxConfig["finalizeExec"]>>(async () => {});

‎src/agents/bash-tools.exec-runtime.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { resolveSafeTimeoutDelayMs } from "../utils/timer-delay.js";
4343
import {
4444
addSession,
4545
appendOutput,
46-
createSessionSlug,
46+
isProcessSessionIdTaken,
4747
markExited,
4848
recordNotifyOnExitRemoval,
4949
tail,
@@ -60,6 +60,7 @@ import {
6060
readEnvInt,
6161
} from "./bash-tools.shared.js";
6262
import { buildCursorPositionResponse, stripDsrRequests } from "./pty-dsr.js";
63+
import { createSessionSlug } from "./session-slug.js";
6364
import { maybeWrapCommandWithShellSnapshot } from "./shell-snapshot.js";
6465
import { createStreamingBinaryOutputSanitizer, getShellConfig } from "./shell-utils.js";
6566

@@ -639,7 +640,7 @@ export async function runExecProcess(opts: {
639640
onSettledBeforeNotify?: (outcome: ExecProcessOutcome) => void;
640641
}): Promise<ExecProcessHandle> {
641642
const startedAt = Date.now();
642-
const sessionId = createSessionSlug();
643+
const sessionId = createSessionSlug(isProcessSessionIdTaken);
643644
const execCommand = opts.execCommand ?? opts.command;
644645
const diagnosticTarget = opts.sandbox ? "sandbox" : "host";
645646
const supervisor = getProcessSupervisor();

‎src/cli/channels-list-catalog-row-discovery.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ vi.mock("./command-execution-startup.js", () => ({
3838

3939
vi.mock("../commands/channels/shared.js", () => ({
4040
formatChannelAccountLabel: vi.fn(),
41-
requireValidConfig: vi.fn(async () => testState.config),
41+
requireValidChannelConfig: vi.fn(async () => testState.config),
4242
}));
4343

4444
vi.mock("../commands/channel-setup/trusted-catalog.js", () => ({

‎src/cli/channels-list-route-cold-imports.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ vi.mock("./command-execution-startup.js", () => ({
2424

2525
vi.mock("../commands/channels/shared.js", () => ({
2626
formatChannelAccountLabel: vi.fn(),
27-
requireValidConfig: vi.fn(async () => testState.config),
27+
requireValidChannelConfig: vi.fn(async () => testState.config),
2828
}));
2929

3030
vi.mock("../commands/channel-setup/trusted-catalog.js", () => ({

‎src/cli/daemon-cli/restart-health.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type {
3838
GatewayRestartSnapshot,
3939
GatewayRestartWaitOutcome,
4040
} from "./restart-health.types.js";
41-
export { terminateStaleGatewayPids } from "./restart-stale-pids.js";
41+
export { terminateStaleGatewayPids } from "../../infra/restart-stale-pids.js";
4242

4343
const STARTUP_MIGRATION_ACTIVITY_POLL_MS = 5_000;
4444
const STOPPED_FREE_EARLY_EXIT_GRACE_MS = 10_000;

0 commit comments

Comments
 (0)