mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-12 21:53:00 -06:00
4273ca9dbd
* refactor(sessions): keep helper transcripts in memory * refactor(sessions): remove file-era transcript storage * test(sessions): use SQLite identity in attempt persistence * test(codex): isolate legacy transcript fixtures * fix(sessions): preserve SQLite transcript identity * fix(sessions): harden transcript lifecycle invariants * fix(sessions): validate transcript identities * fix(sessions): close identity compatibility gaps * fix(sessions): preserve leaf and plugin identities * fix(sessions): retain dispatch transcript targets * fix(sessions): preserve active transcript context * fix(sessions): isolate artifact accounting * fix(sessions): bound SQLite usage accounting * fix(sessions): retain bounded latest usage * fix(sessions): align rebased transcript targets * test(sessions): align accessor scope fixture * fix(telegram): derive SQLite transcript identity * refactor(sessions): remove file-era compaction residue * chore(sessions): lower max-lines baseline * fix(sessions): preserve structured transcript identity * test(sessions): align doctor identity assertions * fix(sessions): isolate default SDK database * refactor(sessions): remove dead file-era exports * fix(sessions): reconcile SQLite transcript identity * fix(sessions): pass checkpoint identity explicitly * test(sessions): make entry field probe explicit * test(sessions): satisfy transcript cleanup lint * test(sessions): align diagnostics identity proof * fix(sessions): finish transcript runtime teardown * fix(sessions): preserve transcript identity invariants * fix(sessions): harden transcript compatibility edges * fix(sessions): preserve checkpoint transcript anchors * fix(sessions): preserve SQLite lifecycle invariants * fix(sessions): retarget compaction successors * test(sessions): preserve transcript fixture semantics * feat(plugin-sdk): add command transcript targets * fix(sessions): serialize transcript rewrites * fix(sessions): validate legacy successor identity * fix(sessions): normalize compaction ownership * fix(sessions): validate successor identity before adoption * fix(sessions): preserve plugin transcript ownership * fix(sessions): carry transcript identity through commands * fix(sessions): import legacy checkpoint artifacts into SQLite * fix(sessions): preserve successor transcript ownership * fix(sessions): align transcript consumers with target identity * fix(sessions): scope transcript token estimates * fix(sessions): retain agent identity across lifecycle hooks * fix(sessions): resolve scoped SQLite targets * fix(sessions): isolate lifecycle transcript targets * fix(sessions): validate compaction agent ownership * fix(sessions): preserve reset and cleanup lifecycle * fix(sessions): serialize prompt cleanup lifecycle * fix(sessions): remove stale lock import * fix(sessions): preserve reset target context * fix(sessions): fence prompt reload takeover * fix(sessions): unblock abort and default lifecycle reads * fix(sessions): validate legacy successor scope * fix(sessions): reject metadata-only runtime rows * fix(sessions): propagate custom transcript stores * fix(sessions): preserve adopted retry targets * fix(sessions): allow unkeyed usage reads * fix(sessions): harden runtime target boundaries * fix(sessions): serialize retry transcript writes * fix(sessions): bound prompt reload disposal * fix(sessions): complete retry marker identity * fix(sessions): keep legacy marker identity minimal * test(sessions): tighten teardown fixture types * fix(sessions): preserve compatibility target identity * test(sessions): persist post-checkpoint boundary turn * test(sessions): align runtime store mock contracts * style(sessions): simplify persisted identity guard * fix(sessions): prefer complete typed targets * fix(sessions): recover legacy marker targets * test(sessions): align marker lookup fixture scope * fix(sessions): validate partial transcript targets * fix(sessions): reconcile partial transcript identities * fix(sessions): canonicalize compatibility identities * test(sessions): cover compatibility aliases * fix(sessions): adopt legacy successor identity * fix(sessions): preserve usage read identity * fix(sessions): preserve partial marker compatibility * fix(sessions): validate legacy successor mappings * fix(sessions): reconcile marker store mappings * fix(sessions): preserve legacy fallback identity * fix(sessions): harden marker alias resolution * fix(sessions): prefer verified successor aliases * fix(sessions): resolve preferred marker aliases * fix(sessions): serialize cleanup admission * fix(sessions): align marker lookup scopes * fix(codex): type marker alias summaries * style(sessions): satisfy changed lint * test(sessions): align structured target assertions * fix(sessions): reconcile latest identity contracts * fix(sessions): validate transcript identity boundaries * docs(sessions): explain stable registry keys * fix(sessions): harden compatibility target round trips * fix(sessions): port usage identity to split modules * test(sessions): align subagent transcript identity * fix(sessions): finish transcript identity migration * fix(agents): route subagent completion capture through transcript targets * fix(agents): settle SQLite prompt handoff during cleanup * chore: shrink max-lines baseline after teardown * fix(sessions): port teardown across split runtime owners * fix(sessions): carry transcript targets through split owners * test(agents): use SQLite compaction target in abort coverage * chore: retain unrelated max-lines suppressions * chore: shrink max-lines baseline after main splits * style(agents): const compaction checkpoint locals * fix(sessions): harden SQLite teardown boundaries * test(sessions): use typed metadata in predicate isolation fixture * test(agents): cover malformed settlement rejections lint-safely * fix(sessions): close remaining SQLite identity races * fix(agents): fail closed on incomplete successor targets * fix(sessions): preserve transcript identity fallbacks * fix(agents): preserve session-key abort admission * fix(trajectory): validate incomplete export targets * test(sessions): drop retired pricing cache imports * fix(sessions): validate partial transcript identities * fix(sessions): close transcript identity edge cases * fix(plugins): reserve retired transcript locator slot * fix(sessions): scope transcript locks by target * style(sessions): simplify SDK initialization error * fix(sessions): preserve initialized transcript state * fix(codex): verify mirrored history session keys * fix(sessions): reject stale transcript ownership * fix(sessions): anchor asynchronous transcript ownership * fix(sessions): measure active transcript state * fix(sessions): preserve scoped transcript compaction * fix(sessions): harden transcript identity and lifecycle * fix(sessions): resolve scoped command transcript stores * fix(sessions): make transcript appends failure-atomic * fix(sessions): enforce scoped transcript ownership * fix(sessions): reject cross-owner transcript handoffs * fix(sessions): fence cleanup transcript ownership * fix(sessions): retire stale write ownership contexts * fix(sessions): preserve pending session migration state * fix(sessions): validate migrated transcript ownership * fix(sessions): validate usage transcript targets * fix(sessions): clear predecessor transcript metadata * fix(sessions): align durable session event targets * fix(sessions): fence late prompt handoffs * fix(sessions): fence lifecycle transcript fallbacks * fix(sessions): bound zero-length memory capture * fix(sessions): preserve transcript teardown ownership * fix(sessions): reject duplicate cleanup ownership * fix(sessions): serialize runtime writes with sqlite leases * fix(sessions): close sqlite teardown concurrency gaps * fix(sessions): preserve nested lifecycle failures * fix(sessions): canonicalize sqlite transcript ownership * fix(sessions): settle disposed prompt handoffs * fix(sessions): resolve canonical attempt lock targets * test(sessions): align canonical target fixtures * test(sessions): retire redundant jsonl parser coverage * refactor(sessions): split active transcript cursors * test(memory): retire legacy marker fixture * fix(sessions): preserve canonical transcript access after rebase * fix(sessions): fence prompt lease and return transcript targets * fix(sessions): colocate transcript leases with target store * fix(sessions): canonicalize transcript lease and worker targets * fix(sessions): preserve plugin and fork identity markers * fix(sessions): complete sqlite transcript target migration * fix(sessions): integrate canonical followup identity * fix(sessions): preserve bounded transcript topology * fix(sessions): validate transcript identity boundaries * fix(context): separate caller and successor targets * test(sessions): split persistence compatibility coverage * test(sessions): preserve fixture topology efficiently * chore(sdk): refresh plugin api baseline * test(agents): align compaction lock target mocks * test(sessions): seed malformed transcript fixtures directly * fix(agents): canonicalize transcript compatibility inputs * fix(agents): type optional tool result ids * test(ci): stabilize loaded process timing * test(tui): wait for collect queue admission
279 lines
9.8 KiB
TypeScript
279 lines
9.8 KiB
TypeScript
// Coverage for external cancellation, timeout, and session-lock release paths.
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import type { EmbeddedAgentQueueHandle } from "../runs.js";
|
|
import {
|
|
createEmbeddedAttemptExternalAbortController,
|
|
createEmbeddedAttemptRunAbort,
|
|
type EmbeddedAttemptAbortStatePort,
|
|
} from "./attempt-abort.js";
|
|
import { SESSIONS_YIELD_ABORT_REASON } from "./attempt.sessions-yield.js";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
countActiveToolExecutions: vi.fn(() => 0),
|
|
markActiveEmbeddedRunAbandoned: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("../../embedded-agent-subscribe.handlers.tools.js", () => ({
|
|
countActiveToolExecutions: mocks.countActiveToolExecutions,
|
|
}));
|
|
|
|
vi.mock("../runs.js", () => ({
|
|
markActiveEmbeddedRunAbandoned: mocks.markActiveEmbeddedRunAbandoned,
|
|
}));
|
|
|
|
function createAbortState() {
|
|
let timedOutDuringCompaction = false;
|
|
const markAborted = vi.fn();
|
|
const markExternalAbort = vi.fn();
|
|
const markTimedOut = vi.fn();
|
|
const markTimedOutDuringCompaction = vi.fn(() => {
|
|
timedOutDuringCompaction = true;
|
|
});
|
|
const markTimedOutDuringToolExecution = vi.fn();
|
|
const readTimedOutDuringCompaction = vi.fn(() => timedOutDuringCompaction);
|
|
const setPromptError = vi.fn();
|
|
const port: EmbeddedAttemptAbortStatePort = {
|
|
markAborted,
|
|
markExternalAbort,
|
|
markTimedOut,
|
|
markTimedOutDuringCompaction,
|
|
markTimedOutDuringToolExecution,
|
|
readTimedOutDuringCompaction,
|
|
setPromptError,
|
|
};
|
|
return {
|
|
port,
|
|
markAborted,
|
|
markExternalAbort,
|
|
markTimedOut,
|
|
markTimedOutDuringCompaction,
|
|
markTimedOutDuringToolExecution,
|
|
setPromptError,
|
|
};
|
|
}
|
|
|
|
beforeEach(() => {
|
|
mocks.countActiveToolExecutions.mockReset().mockReturnValue(0);
|
|
mocks.markActiveEmbeddedRunAbandoned.mockReset();
|
|
});
|
|
|
|
describe("createEmbeddedAttemptExternalAbortController", () => {
|
|
it("aborts setup state before the live run handler is installed", () => {
|
|
const source = new AbortController();
|
|
const runAbortController = new AbortController();
|
|
const state = createAbortState();
|
|
const abortActiveSession = vi.fn(async () => {});
|
|
const controller = createEmbeddedAttemptExternalAbortController({
|
|
abortSignal: source.signal,
|
|
cleanupAfterEarlyAbort: vi.fn(async () => {}),
|
|
runAbortController,
|
|
runId: "run-external",
|
|
state: state.port,
|
|
});
|
|
controller.setActiveSessionAbort(abortActiveSession);
|
|
controller.arm();
|
|
const reason = new Error("cancelled");
|
|
|
|
source.abort(reason);
|
|
|
|
expect(state.markExternalAbort).toHaveBeenCalledTimes(1);
|
|
expect(state.markAborted).toHaveBeenCalledTimes(1);
|
|
expect(state.setPromptError).toHaveBeenCalledWith(reason);
|
|
expect(runAbortController.signal.reason).toBe(reason);
|
|
expect(abortActiveSession).toHaveBeenCalledTimes(1);
|
|
controller.dispose();
|
|
});
|
|
|
|
it("classifies timeout during compaction without also blaming a tool", () => {
|
|
const source = new AbortController();
|
|
const runAbortController = new AbortController();
|
|
const state = createAbortState();
|
|
mocks.countActiveToolExecutions.mockReturnValue(1);
|
|
const controller = createEmbeddedAttemptExternalAbortController({
|
|
abortSignal: source.signal,
|
|
cleanupAfterEarlyAbort: vi.fn(async () => {}),
|
|
runAbortController,
|
|
runId: "run-compaction-timeout",
|
|
state: state.port,
|
|
});
|
|
controller.setCompactionState({
|
|
isPendingOrRetrying: () => true,
|
|
isInFlight: () => false,
|
|
});
|
|
controller.arm();
|
|
const reason = new Error("deadline");
|
|
reason.name = "TimeoutError";
|
|
|
|
source.abort(reason);
|
|
|
|
expect(state.markTimedOutDuringCompaction).toHaveBeenCalledTimes(1);
|
|
expect(state.markTimedOut).toHaveBeenCalledTimes(1);
|
|
expect(state.markTimedOutDuringToolExecution).not.toHaveBeenCalled();
|
|
expect(runAbortController.signal.reason).toBe(reason);
|
|
controller.dispose();
|
|
});
|
|
|
|
it("hands cancellation to the live run handler once installed", () => {
|
|
const source = new AbortController();
|
|
const state = createAbortState();
|
|
const abortRun = vi.fn();
|
|
const controller = createEmbeddedAttemptExternalAbortController({
|
|
abortSignal: source.signal,
|
|
cleanupAfterEarlyAbort: vi.fn(async () => {}),
|
|
runAbortController: new AbortController(),
|
|
runId: "run-live",
|
|
state: state.port,
|
|
});
|
|
controller.setRunAbort(abortRun);
|
|
controller.arm();
|
|
const reason = new Error("cancelled live run");
|
|
|
|
source.abort(reason);
|
|
|
|
expect(state.markExternalAbort).toHaveBeenCalledTimes(1);
|
|
expect(abortRun).toHaveBeenCalledWith(false, reason);
|
|
expect(state.markAborted).not.toHaveBeenCalled();
|
|
expect(state.setPromptError).not.toHaveBeenCalled();
|
|
controller.dispose();
|
|
});
|
|
|
|
it("cleans prepared resources before rejecting a pre-fired signal", async () => {
|
|
const source = new AbortController();
|
|
const reason = new Error("cancelled during setup");
|
|
source.abort(reason);
|
|
const cleanupAfterEarlyAbort = vi.fn(async () => {});
|
|
const state = createAbortState();
|
|
const controller = createEmbeddedAttemptExternalAbortController({
|
|
abortSignal: source.signal,
|
|
cleanupAfterEarlyAbort,
|
|
runAbortController: new AbortController(),
|
|
runId: "run-setup",
|
|
state: state.port,
|
|
});
|
|
|
|
await expect(controller.throwIfFiredAfterPrepCleanup()).rejects.toBe(reason);
|
|
|
|
expect(cleanupAfterEarlyAbort).toHaveBeenCalledTimes(1);
|
|
expect(state.markAborted).toHaveBeenCalledTimes(1);
|
|
expect(state.markExternalAbort).toHaveBeenCalledTimes(1);
|
|
expect(state.setPromptError).toHaveBeenCalledWith(reason);
|
|
});
|
|
});
|
|
|
|
describe("createEmbeddedAttemptRunAbort", () => {
|
|
it("releases session ownership non-terminally for sessions_yield handoff", async () => {
|
|
const releaseHeldLockForAbort = vi.fn(async () => {});
|
|
const abortRun = createEmbeddedAttemptRunAbort({
|
|
abortActiveSession: vi.fn(async () => {}),
|
|
activeSession: { abortCompaction: vi.fn(), isCompacting: false },
|
|
attempt: {
|
|
runId: "run-yield",
|
|
sessionFile: "agent:main:main",
|
|
sessionId: "session-yield",
|
|
sessionKey: "agent:main:main",
|
|
},
|
|
getQueueHandle: () => undefined,
|
|
isProbeSession: false,
|
|
log: { warn: vi.fn() },
|
|
runAbortController: new AbortController(),
|
|
sessionLockController: { releaseHeldLockForAbort },
|
|
state: createAbortState().port,
|
|
});
|
|
|
|
abortRun(false, SESSIONS_YIELD_ABORT_REASON);
|
|
await vi.waitFor(() => {
|
|
expect(releaseHeldLockForAbort).toHaveBeenCalledWith({ terminal: false });
|
|
});
|
|
});
|
|
|
|
it("settles timeout state, session work, queue ownership, and the lock", async () => {
|
|
const state = createAbortState();
|
|
const timeoutReason = new Error("attempt deadline");
|
|
timeoutReason.name = "TimeoutError";
|
|
const abortCompaction = vi.fn();
|
|
const abortActiveSession = vi.fn(async () => {});
|
|
const onAttemptTimeout = vi.fn();
|
|
const releaseHeldLockForAbort = vi.fn(async () => {});
|
|
const queueHandle = {} as EmbeddedAgentQueueHandle;
|
|
const runAbortController = new AbortController();
|
|
mocks.countActiveToolExecutions.mockReturnValue(1);
|
|
const abortRun = createEmbeddedAttemptRunAbort({
|
|
abortActiveSession,
|
|
activeSession: { abortCompaction, isCompacting: true },
|
|
attempt: {
|
|
onAttemptTimeout,
|
|
runId: "run-timeout",
|
|
sessionFile: "/tmp/session.jsonl",
|
|
sessionId: "session-timeout",
|
|
sessionKey: "agent:main",
|
|
},
|
|
getQueueHandle: () => queueHandle,
|
|
isProbeSession: false,
|
|
log: { warn: vi.fn() },
|
|
runAbortController,
|
|
sessionLockController: { releaseHeldLockForAbort },
|
|
state: state.port,
|
|
});
|
|
|
|
abortRun(true, timeoutReason);
|
|
await Promise.resolve();
|
|
|
|
expect(state.markAborted).toHaveBeenCalledTimes(1);
|
|
expect(state.markTimedOut).toHaveBeenCalledTimes(1);
|
|
expect(state.markTimedOutDuringToolExecution).toHaveBeenCalledTimes(1);
|
|
expect(onAttemptTimeout).toHaveBeenCalledWith(timeoutReason);
|
|
expect(runAbortController.signal.reason).toBe(timeoutReason);
|
|
expect(abortCompaction).toHaveBeenCalledTimes(1);
|
|
expect(abortActiveSession).toHaveBeenCalledTimes(1);
|
|
expect(mocks.markActiveEmbeddedRunAbandoned).toHaveBeenCalledWith({
|
|
sessionId: "session-timeout",
|
|
handle: queueHandle,
|
|
sessionKey: "agent:main",
|
|
sessionFile: "/tmp/session.jsonl",
|
|
reason: "timeout",
|
|
});
|
|
expect(releaseHeldLockForAbort).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("logs lock release failures without replacing the manual abort reason", async () => {
|
|
// Abort cleanup must not replace the original timeout/manual-abort reason
|
|
// with a secondary lock-release failure.
|
|
const state = createAbortState();
|
|
const abortReason = new Error("manual abort");
|
|
const releaseError = new Error("locked");
|
|
const releaseHeldLockForAbort = vi.fn(async () => {
|
|
throw releaseError;
|
|
});
|
|
const warn = vi.fn();
|
|
const runAbortController = new AbortController();
|
|
const abortRun = createEmbeddedAttemptRunAbort({
|
|
abortActiveSession: vi.fn(async () => {}),
|
|
activeSession: { abortCompaction: vi.fn(), isCompacting: false },
|
|
attempt: {
|
|
onAttemptTimeout: vi.fn(),
|
|
runId: "run-manual",
|
|
sessionFile: "/tmp/session.jsonl",
|
|
sessionId: "session-manual",
|
|
sessionKey: "agent:main",
|
|
},
|
|
getQueueHandle: () => undefined,
|
|
isProbeSession: false,
|
|
log: { warn },
|
|
runAbortController,
|
|
sessionLockController: { releaseHeldLockForAbort },
|
|
state: state.port,
|
|
});
|
|
|
|
abortRun(false, abortReason);
|
|
|
|
await Promise.resolve();
|
|
await Promise.resolve();
|
|
|
|
expect(runAbortController.signal.reason).toBe(abortReason);
|
|
expect(releaseHeldLockForAbort).toHaveBeenCalledTimes(1);
|
|
expect(warn).toHaveBeenCalledWith(
|
|
"failed to release session lock on abort: runId=run-manual Error: locked",
|
|
);
|
|
});
|
|
});
|