From a4e2113e1b0fd84d2e057e47d911effbabe19e8d Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 20 Jun 2026 15:46:52 +0800 Subject: [PATCH] chore(deadcode): remove unused infra consumers --- src/infra/exec-approvals-effective.ts | 16 ----- src/infra/exec-approvals-policy.test.ts | 38 ++++++---- src/infra/restart-handoff.test.ts | 93 +------------------------ src/infra/restart-handoff.ts | 50 ------------- src/infra/restart-sentinel.test.ts | 10 +-- src/infra/restart-sentinel.ts | 12 ---- 6 files changed, 26 insertions(+), 193 deletions(-) diff --git a/src/infra/exec-approvals-effective.ts b/src/infra/exec-approvals-effective.ts index 7fb55b08f8e..6048ce81b05 100644 --- a/src/infra/exec-approvals-effective.ts +++ b/src/infra/exec-approvals-effective.ts @@ -66,8 +66,6 @@ export type ExecPolicyScopeSnapshot = { allowedDecisions: readonly ExecApprovalDecision[]; }; -type ExecPolicyScopeSummary = Omit; - type ExecPolicyRequestedField = "security" | "ask"; function resolveRequestedHost(params: { @@ -327,20 +325,6 @@ export function collectExecPolicyScopeSnapshots(params: { return snapshots; } -export function resolveExecPolicyScopeSummary(params: { - approvals: ExecApprovalsFile; - scopeExecConfig?: ExecPolicyConfig | undefined; - globalExecConfig?: ExecPolicyConfig | undefined; - configPath: string; - scopeLabel: string; - agentId?: string; - hostPath?: string; -}): ExecPolicyScopeSummary { - const snapshot = resolveExecPolicyScopeSnapshot(params); - const { allowedDecisions: _allowedDecisions, ...summary } = snapshot; - return summary; -} - export function resolveExecPolicyScopeSnapshot(params: { approvals: ExecApprovalsFile; scopeExecConfig?: ExecPolicyConfig | undefined; diff --git a/src/infra/exec-approvals-policy.test.ts b/src/infra/exec-approvals-policy.test.ts index 9bd5a49bade..ad729c9fac5 100644 --- a/src/infra/exec-approvals-policy.test.ts +++ b/src/infra/exec-approvals-policy.test.ts @@ -13,7 +13,7 @@ vi.unmock("./exec-approvals.js"); vi.unmock("./exec-approvals-effective.js"); let collectExecPolicyScopeSnapshots: typeof import("./exec-approvals-effective.js").collectExecPolicyScopeSnapshots; -let resolveExecPolicyScopeSummary: typeof import("./exec-approvals-effective.js").resolveExecPolicyScopeSummary; +let resolveExecPolicyScopeSnapshot: typeof import("./exec-approvals-effective.js").resolveExecPolicyScopeSnapshot; let evaluateExecAllowlist: typeof import("./exec-approvals.js").evaluateExecAllowlist; let hasDurableExecApproval: typeof import("./exec-approvals.js").hasDurableExecApproval; let maxAsk: typeof import("./exec-approvals.js").maxAsk; @@ -40,7 +40,7 @@ async function loadActualExecApprovalModules(): Promise { "./exec-approvals-effective.js", ); collectExecPolicyScopeSnapshots = effective.collectExecPolicyScopeSnapshots; - resolveExecPolicyScopeSummary = effective.resolveExecPolicyScopeSummary; + resolveExecPolicyScopeSnapshot = effective.resolveExecPolicyScopeSnapshot; evaluateExecAllowlist = execApprovals.evaluateExecAllowlist; hasDurableExecApproval = execApprovals.hasDurableExecApproval; maxAsk = execApprovals.maxAsk; @@ -62,6 +62,14 @@ async function loadActualExecApprovalModules(): Promise { resolveExecPolicyForMode = execApprovals.resolveExecPolicyForMode; } +function summarizeExecPolicyScopeSnapshot( + params: Parameters[0], +): Omit, "allowedDecisions"> { + const { allowedDecisions: _allowedDecisions, ...summary } = + resolveExecPolicyScopeSnapshot(params); + return summary; +} + function expectFields(value: unknown, expected: Record): void { if (!value || typeof value !== "object") { throw new Error("expected fields object"); @@ -84,7 +92,7 @@ function expectMalformedAgentAskUsesDefaults(agentAsk: unknown): void { }, }, } as unknown as ExecApprovalsFile; - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals, globalExecConfig: { ask: "off", @@ -410,7 +418,7 @@ describe("exec approvals policy helpers", () => { }); it("explains stricter host security and ask precedence", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, defaults: { @@ -448,7 +456,7 @@ describe("exec approvals policy helpers", () => { }); it("maps normalized requested mode into policy snapshots", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, }, @@ -478,7 +486,7 @@ describe("exec approvals policy helpers", () => { }); it("lets narrower legacy policy override a global normalized mode in snapshots", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, }, @@ -508,7 +516,7 @@ describe("exec approvals policy helpers", () => { }); it("preserves mode-derived siblings for partial narrower legacy policy snapshots", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, }, @@ -538,7 +546,7 @@ describe("exec approvals policy helpers", () => { }); it("reports full plus on-miss as full because on-miss only gates allowlist misses", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, }, @@ -568,7 +576,7 @@ describe("exec approvals policy helpers", () => { }); it("uses the actual approvals path when reporting host sources", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, defaults: { @@ -599,7 +607,7 @@ describe("exec approvals policy helpers", () => { const stateDir = path.join(process.cwd(), ".tmp-openclaw-state"); process.env.OPENCLAW_STATE_DIR = stateDir; try { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, defaults: { @@ -626,7 +634,7 @@ describe("exec approvals policy helpers", () => { }); it("does not let host ask=off suppress a stricter requested ask", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, defaults: { @@ -649,7 +657,7 @@ describe("exec approvals policy helpers", () => { }); it("clamps askFallback to the effective security", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, defaults: { @@ -685,7 +693,7 @@ describe("exec approvals policy helpers", () => { }); it("attributes host policy to wildcard agent entries before defaults", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, defaults: { @@ -725,7 +733,7 @@ describe("exec approvals policy helpers", () => { }); it("inherits requested agent policy from global tools.exec config", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, agents: { @@ -759,7 +767,7 @@ describe("exec approvals policy helpers", () => { }); it("reports askFallback from the OpenClaw default when approvals omit it", () => { - const summary = resolveExecPolicyScopeSummary({ + const summary = summarizeExecPolicyScopeSnapshot({ approvals: { version: 1, agents: {}, diff --git a/src/infra/restart-handoff.test.ts b/src/infra/restart-handoff.test.ts index eacd4b62c5b..7d84616efc3 100644 --- a/src/infra/restart-handoff.test.ts +++ b/src/infra/restart-handoff.test.ts @@ -4,7 +4,6 @@ import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { - consumeGatewayRestartHandoffForExitedProcessSync, formatGatewayRestartHandoffDiagnostic, GATEWAY_SUPERVISOR_RESTART_HANDOFF_FILENAME, GATEWAY_SUPERVISOR_RESTART_HANDOFF_KIND, @@ -124,74 +123,6 @@ describe("gateway restart handoff", () => { }); }); - it("consumes a fresh handoff by exited pid instead of current process pid", () => { - const env = createHandoffEnv(); - - expectWrittenHandoff({ - env, - pid: process.pid + 1, - reason: "update.run", - restartKind: "update-process", - supervisorMode: "systemd", - createdAt: 2_000, - }); - - const consumed = consumeGatewayRestartHandoffForExitedProcessSync({ - env, - exitedPid: process.pid + 1, - now: 2_001, - }); - expect(consumed?.pid).toBe(process.pid + 1); - expect(consumed?.source).toBe("gateway-update"); - expect(consumed?.restartKind).toBe("update-process"); - expect(consumed?.supervisorMode).toBe("systemd"); - expect(fs.existsSync(handoffPath(env))).toBe(false); - }); - - it("rejects handoffs for a different exited pid and clears them", () => { - const env = createHandoffEnv(); - - expectWrittenHandoff({ - env, - pid: 111, - restartKind: "full-process", - supervisorMode: "external", - createdAt: 1_000, - }); - - expect( - consumeGatewayRestartHandoffForExitedProcessSync({ - env, - exitedPid: 222, - now: 1_001, - }), - ).toBeNull(); - expect(fs.existsSync(handoffPath(env))).toBe(false); - }); - - it("rejects a handoff when the supplied process instance does not match", () => { - const env = createHandoffEnv(); - - expectWrittenHandoff({ - env, - pid: 111, - processInstanceId: "gateway-instance-1", - restartKind: "full-process", - supervisorMode: "external", - createdAt: 1_000, - }); - - expect( - consumeGatewayRestartHandoffForExitedProcessSync({ - env, - exitedPid: 111, - processInstanceId: "gateway-instance-2", - now: 1_001, - }), - ).toBeNull(); - expect(fs.existsSync(handoffPath(env))).toBe(false); - }); - it("rejects malformed handoff payloads", () => { const env = createHandoffEnv(); @@ -229,14 +160,7 @@ describe("gateway restart handoff", () => { expect(readGatewayRestartHandoffSync(env, 2_001)).toBeNull(); fs.writeFileSync(handoffPath(env), "x".repeat(8192), { encoding: "utf8", mode: 0o600 }); - expect( - consumeGatewayRestartHandoffForExitedProcessSync({ - env, - exitedPid: 111, - now: 2_001, - }), - ).toBeNull(); - expect(fs.existsSync(handoffPath(env))).toBe(false); + expect(readGatewayRestartHandoffSync(env, 2_001)).toBeNull(); }); it("rejects persisted handoffs with a ttl longer than the supported window", () => { @@ -259,14 +183,6 @@ describe("gateway restart handoff", () => { ); expect(readGatewayRestartHandoffSync(env, 1_001)).toBeNull(); - expect( - consumeGatewayRestartHandoffForExitedProcessSync({ - env, - exitedPid: 111, - now: 1_001, - }), - ).toBeNull(); - expect(fs.existsSync(handoffPath(env))).toBe(false); }); it("does not follow an existing handoff-path symlink when writing", () => { @@ -288,12 +204,7 @@ describe("gateway restart handoff", () => { expect(fs.readFileSync(targetPath, "utf8")).toBe("keep"); expect(fs.lstatSync(handoffPath(env)).isSymbolicLink()).toBe(false); - expect( - consumeGatewayRestartHandoffForExitedProcessSync({ - env, - exitedPid: 12_345, - })?.pid, - ).toBe(12_345); + expect(readGatewayRestartHandoffSync(env)?.pid).toBe(12_345); }); it("formats a concise diagnostic line for status surfaces", () => { diff --git a/src/infra/restart-handoff.ts b/src/infra/restart-handoff.ts index 78aaadefd4c..a71a1a2e8d7 100644 --- a/src/infra/restart-handoff.ts +++ b/src/infra/restart-handoff.ts @@ -114,11 +114,6 @@ function unlinkRegularFileSync(filePath: string): boolean { } } -/** Remove the restart handoff file when it is a regular single-link file. */ -export function clearGatewayRestartHandoffSync(env: NodeJS.ProcessEnv = process.env): void { - unlinkRegularFileSync(resolveGatewayRestartHandoffPath(env)); -} - function normalizePid(pid: number | undefined): number | null { return typeof pid === "number" && Number.isSafeInteger(pid) && pid > 0 ? pid : null; } @@ -373,48 +368,3 @@ export function readGatewayRestartHandoffSync( } return payload; } - -/** Consume a handoff only when it belongs to the just-exited process. */ -export function consumeGatewayRestartHandoffForExitedProcessSync(opts: { - env?: NodeJS.ProcessEnv; - exitedPid?: number; - processInstanceId?: string; - now?: number; -}): GatewayRestartHandoff | null { - const env = opts.env ?? process.env; - const handoffPath = resolveGatewayRestartHandoffPath(env); - let raw: string | null = null; - try { - const stat = fs.lstatSync(handoffPath); - // Consume uses the same regular-file guard as reads, then clears the file - // even if parsing fails so stale handoffs do not repeat. - if (!stat.isFile() || stat.nlink > 1 || stat.size > GATEWAY_RESTART_HANDOFF_MAX_BYTES) { - return null; - } - raw = fs.readFileSync(handoffPath, "utf8"); - } catch { - return null; - } finally { - clearGatewayRestartHandoffSync(env); - } - - const payload = raw ? parseGatewayRestartHandoff(raw) : null; - const exitedPid = normalizePid(opts.exitedPid); - if (!payload || exitedPid === null || payload.pid !== exitedPid) { - return null; - } - - const expectedProcessInstanceId = normalizeText( - opts.processInstanceId, - MAX_PROCESS_INSTANCE_ID_LENGTH, - ); - if (expectedProcessInstanceId && payload.processInstanceId !== expectedProcessInstanceId) { - return null; - } - - const now = opts.now ?? Date.now(); - if (now < payload.createdAt || now > payload.expiresAt) { - return null; - } - return payload; -} diff --git a/src/infra/restart-sentinel.test.ts b/src/infra/restart-sentinel.test.ts index c8cf93e368a..6c58af0273b 100644 --- a/src/infra/restart-sentinel.test.ts +++ b/src/infra/restart-sentinel.test.ts @@ -6,7 +6,6 @@ import { withTempDir } from "../test-helpers/temp-dir.js"; import { withEnvAsync } from "../test-utils/env.js"; import { buildRestartSuccessContinuation, - consumeRestartSentinel, finalizeUpdateRestartSentinelRunningVersion, formatDoctorNonInteractiveHint, formatRestartSentinelMessage, @@ -51,7 +50,7 @@ async function expectPathMissing(targetPath: string): Promise { } describe("restart sentinel", () => { - it("writes and consumes a sentinel", async () => { + it("writes and reads a sentinel", async () => { await withRestartSentinelStateDir(async () => { const payload = { kind: "update" as const, @@ -70,13 +69,6 @@ describe("restart sentinel", () => { const read = await readRestartSentinel(); expect(read?.payload.kind).toBe("update"); expect(read?.payload.continuation).toEqual(payload.continuation); - - const consumed = await consumeRestartSentinel(); - expect(consumed?.payload.sessionKey).toBe(payload.sessionKey); - expect(consumed?.payload.continuation).toEqual(payload.continuation); - - const empty = await readRestartSentinel(); - expect(empty).toBeNull(); }); }); diff --git a/src/infra/restart-sentinel.ts b/src/infra/restart-sentinel.ts index 470007624b6..0547f1200d8 100644 --- a/src/infra/restart-sentinel.ts +++ b/src/infra/restart-sentinel.ts @@ -197,18 +197,6 @@ export async function readRestartSentinel( } } -export async function consumeRestartSentinel( - env: NodeJS.ProcessEnv = process.env, -): Promise { - const filePath = resolveRestartSentinelPath(env); - const parsed = await readRestartSentinel(env); - if (!parsed) { - return null; - } - await removeRestartSentinelFile(filePath); - return parsed; -} - export function formatRestartSentinelMessage(payload: RestartSentinelPayload): string { const message = payload.message?.trim(); if (message && (!payload.stats || payload.kind === "config-auto-recovery")) {