File size: 5,145 Bytes
eb3f11e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import { afterEach, describe, expect, it, vi } from "vitest";
import { resetAllLanes } from "../../process/command-queue.js";
import { resetCommandQueueStateForTest } from "../../process/command-queue.test-support.js";
import { drainGlobalSingletonLifecycleState } from "../../shared/global-singleton.js";
import {
getTaskFlowById,
reloadTaskFlowRegistryFromStore,
} from "../../tasks/task-flow-registry.js";
import {
getTaskById,
listTasksForOwnerKey,
reloadTaskRegistryFromStore,
} from "../../tasks/task-registry.js";
import {
configureTaskRegistryMaintenance,
resetTaskRegistryMaintenanceRuntimeForTests,
runTaskRegistryMaintenance,
} from "../../tasks/task-registry.maintenance.js";
import {
resetTaskFlowRegistryForTests,
resetTaskRegistryForTests,
} from "../../tasks/task-runtime.test-helpers.js";
import { withStateDirEnv } from "../../test-helpers/state-dir-env.js";
import { runContextEngineMaintenance } from "./context-engine-maintenance.js";
const CONTEXT_ENGINE_TURN_MAINTENANCE_TASK_KIND = "context_engine_turn_maintenance";
afterEach(async () => {
resetTaskRegistryMaintenanceRuntimeForTests();
resetCommandQueueStateForTest();
resetTaskRegistryForTests({ persist: false });
resetTaskFlowRegistryForTests({ persist: false });
await drainGlobalSingletonLifecycleState("close");
vi.useRealTimers();
});
describe("deferred context-engine maintenance lifecycle", () => {
it("retains live work across restart and loses it after the owning process closes", async () => {
await withStateDirEnv("openclaw-context-maintenance-lifecycle-", async () => {
vi.useFakeTimers();
resetCommandQueueStateForTest();
resetTaskRegistryForTests({ persist: false });
resetTaskFlowRegistryForTests({ persist: false });
configureTaskRegistryMaintenance({ runtimeAuthoritative: true });
const sessionKey = "agent:main:context-maintenance-lifecycle";
let releaseMaintenance: (() => void) | undefined;
let deferredMaintenance: Promise<void> | undefined;
await runContextEngineMaintenance({
contextEngine: {
info: {
id: "lifecycle-test",
name: "Lifecycle test",
turnMaintenanceMode: "background",
},
ingest: async () => ({ ingested: true }),
assemble: async ({ messages }) => ({ messages, estimatedTokens: 0 }),
compact: async () => ({ ok: true, compacted: false }),
maintain: async () => {
await new Promise<void>((resolve) => {
releaseMaintenance = resolve;
});
return { changed: false, bytesFreed: 0, rewrittenEntries: 0 };
},
},
sessionId: "context-maintenance-lifecycle",
sessionKey,
sessionFile: "/tmp/context-maintenance-lifecycle.jsonl",
reason: "turn",
onDeferredMaintenance: (promise) => {
deferredMaintenance = promise;
},
});
await vi.advanceTimersByTimeAsync(11_000);
const task = listTasksForOwnerKey(sessionKey).find(
(candidate) =>
candidate.taskKind === CONTEXT_ENGINE_TURN_MAINTENANCE_TASK_KIND &&
candidate.parentFlowId,
);
if (!task?.parentFlowId) {
throw new Error("expected visible deferred maintenance with a TaskFlow");
}
const flowId = task.parentFlowId;
expect(getTaskFlowById(flowId)?.status).toBe("running");
await vi.advanceTimersByTimeAsync(5 * 60_000 + 1);
resetAllLanes();
await drainGlobalSingletonLifecycleState("restart");
expect(await runTaskRegistryMaintenance()).toMatchObject({ reconciled: 0 });
expect(getTaskById(task.taskId)?.status).toBe("running");
expect(getTaskFlowById(flowId)?.status).toBe("running");
await drainGlobalSingletonLifecycleState("close");
expect(await runTaskRegistryMaintenance()).toMatchObject({ reconciled: 1 });
const lostTask = getTaskById(task.taskId);
const lostFlow = getTaskFlowById(flowId);
expect(lostTask).toMatchObject({
status: "lost",
error: "owning process exited",
endedAt: expect.any(Number),
lastEventAt: expect.any(Number),
cleanupAfter: expect.any(Number),
});
expect(lostFlow).toMatchObject({
status: "lost",
endedAt: lostTask?.endedAt,
updatedAt: lostTask?.endedAt,
});
reloadTaskRegistryFromStore();
reloadTaskFlowRegistryFromStore();
const durableTask = getTaskById(task.taskId);
const durableFlow = getTaskFlowById(flowId);
expect(await runTaskRegistryMaintenance()).toMatchObject({ reconciled: 0 });
expect(getTaskById(task.taskId)).toMatchObject({
status: "lost",
endedAt: durableTask?.endedAt,
lastEventAt: durableTask?.lastEventAt,
cleanupAfter: durableTask?.cleanupAfter,
});
expect(getTaskFlowById(flowId)).toMatchObject({
status: "lost",
endedAt: durableFlow?.endedAt,
updatedAt: durableFlow?.updatedAt,
});
releaseMaintenance?.();
await deferredMaintenance;
});
});
});
|