mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-27 12:56:01 -06:00
8885be4756
* fix(codex): route compact through session pipeline Route /codex compact through host-owned manual compaction so native completion and token snapshots update session meters, and report the terminal outcome to the user. * test(codex): cover unavailable compact outcome * fix(plugins): fence command compaction capability Bind compactCurrent to one command invocation and captured session generation so retained or stale callbacks fail closed without running compaction. * fix(plugins): lazy-load command session revalidation Keep compaction session freshness checks at the auto-reply owner without pulling the session accessor into generic plugin command module initialization. * fix(plugins): keep session revalidation lazy Avoid loading the session accessor through generic plugin command initialization while preserving pre-compaction session-generation checks. * fix(compaction): fence session lifecycle admission Revalidate the exact session id and lifecycle revision immediately before native compaction and again before accounting so resets and rebinds fail closed across awaited work. * fix(commands): fence plugin compaction authority * fix(compaction): require accounting commit * test(compaction): keep regression under lint cap * fix(codex): preserve compact admission * fix(codex): preserve compaction target identity * fix(compaction): bind admitted target * fix(compaction): fence accounting commit
302 lines
11 KiB
TypeScript
302 lines
11 KiB
TypeScript
// Tests session update lifecycle ordering and active-session state transitions.
|
|
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import type { OpenClawConfig } from "../../config/config.js";
|
|
import type { SessionEntry } from "../../config/sessions.js";
|
|
import {
|
|
applySessionEntryLifecycleMutation,
|
|
loadSessionEntry,
|
|
replaceSessionEntry,
|
|
} from "../../config/sessions/session-accessor.js";
|
|
import type { HookRunner } from "../../plugins/hooks.js";
|
|
import {
|
|
getActiveGatewayRootWorkCount,
|
|
markGatewayRestartDraining,
|
|
resetGatewayWorkAdmission,
|
|
tryBeginGatewayRootWorkAdmission,
|
|
} from "../../process/gateway-work-admission.js";
|
|
|
|
const hookRunnerMocks = vi.hoisted(() => ({
|
|
hasHooks: vi.fn<HookRunner["hasHooks"]>(),
|
|
runSessionEnd: vi.fn<HookRunner["runSessionEnd"]>(),
|
|
runSessionStart: vi.fn<HookRunner["runSessionStart"]>(),
|
|
}));
|
|
|
|
let incrementCompactionCount: typeof import("./session-updates.js").incrementCompactionCount;
|
|
const tempDirs: string[] = [];
|
|
|
|
async function createFixture() {
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-updates-"));
|
|
tempDirs.push(root);
|
|
const storePath = path.join(root, "sessions.json");
|
|
const sessionKey = "agent:main:forum:direct:compaction";
|
|
const transcriptPath = path.join(root, "s1.jsonl");
|
|
await fs.writeFile(transcriptPath, '{"type":"message"}\n', "utf-8");
|
|
const entry = {
|
|
sessionId: "s1",
|
|
sessionFile: transcriptPath,
|
|
updatedAt: Date.now(),
|
|
compactionCount: 0,
|
|
} as SessionEntry;
|
|
const sessionStore: Record<string, SessionEntry> = {
|
|
[sessionKey]: entry,
|
|
};
|
|
await replaceSessionEntry({ storePath, sessionKey }, entry);
|
|
return { storePath, sessionKey, sessionStore, entry, transcriptPath };
|
|
}
|
|
|
|
function firstSessionEndCall() {
|
|
return hookRunnerMocks.runSessionEnd.mock.calls[0] ?? [];
|
|
}
|
|
|
|
function firstSessionStartCall() {
|
|
return hookRunnerMocks.runSessionStart.mock.calls[0] ?? [];
|
|
}
|
|
|
|
describe("session-updates lifecycle hooks", () => {
|
|
beforeEach(async () => {
|
|
resetGatewayWorkAdmission();
|
|
vi.resetModules();
|
|
vi.doMock("../../plugins/hook-runner-global.js", () => ({
|
|
getGlobalHookRunner: () =>
|
|
({
|
|
hasHooks: hookRunnerMocks.hasHooks,
|
|
runSessionEnd: hookRunnerMocks.runSessionEnd,
|
|
runSessionStart: hookRunnerMocks.runSessionStart,
|
|
}) as unknown as HookRunner,
|
|
}));
|
|
hookRunnerMocks.hasHooks.mockReset();
|
|
hookRunnerMocks.runSessionEnd.mockReset();
|
|
hookRunnerMocks.runSessionStart.mockReset();
|
|
hookRunnerMocks.hasHooks.mockImplementation(
|
|
(hookName) => hookName === "session_end" || hookName === "session_start",
|
|
);
|
|
hookRunnerMocks.runSessionEnd.mockResolvedValue(undefined);
|
|
hookRunnerMocks.runSessionStart.mockResolvedValue(undefined);
|
|
({ incrementCompactionCount } = await import("./session-updates.js"));
|
|
});
|
|
|
|
afterEach(async () => {
|
|
resetGatewayWorkAdmission();
|
|
vi.restoreAllMocks();
|
|
await Promise.all(
|
|
tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })),
|
|
);
|
|
});
|
|
|
|
it("emits compaction lifecycle hooks when newSessionId replaces the session", async () => {
|
|
const { storePath, sessionKey, sessionStore, entry, transcriptPath } = await createFixture();
|
|
const cfg = { session: { store: storePath } } as OpenClawConfig;
|
|
|
|
await incrementCompactionCount({
|
|
cfg,
|
|
sessionEntry: entry,
|
|
sessionStore,
|
|
sessionKey,
|
|
storePath,
|
|
newSessionId: "s2",
|
|
});
|
|
|
|
expect(hookRunnerMocks.runSessionEnd).toHaveBeenCalledTimes(1);
|
|
expect(hookRunnerMocks.runSessionStart).toHaveBeenCalledTimes(1);
|
|
|
|
const [endEvent, endContext] = firstSessionEndCall();
|
|
const [startEvent, startContext] = firstSessionStartCall();
|
|
|
|
expect(endEvent?.sessionId).toBe("s1");
|
|
expect(endEvent?.sessionKey).toBe(sessionKey);
|
|
expect(endEvent?.reason).toBe("compaction");
|
|
expect(endEvent?.transcriptArchived).toBe(false);
|
|
expect(endEvent?.sessionFile).toBe(await fs.realpath(transcriptPath));
|
|
expect(endContext?.sessionId).toBe("s1");
|
|
expect(endContext?.sessionKey).toBe(sessionKey);
|
|
expect(endContext?.agentId).toBe("main");
|
|
expect(endEvent?.nextSessionId).toBe(startEvent?.sessionId);
|
|
expect(startEvent?.sessionId).toBe("s2");
|
|
expect(startEvent?.sessionKey).toBe(sessionKey);
|
|
expect(startEvent?.resumedFrom).toBe("s1");
|
|
expect(startContext?.sessionId).toBe("s2");
|
|
expect(startContext?.sessionKey).toBe(sessionKey);
|
|
expect(startContext?.agentId).toBe("main");
|
|
});
|
|
|
|
it("keeps compaction lifecycle hooks root-admitted until both settle", async () => {
|
|
const { storePath, sessionKey, sessionStore, entry } = await createFixture();
|
|
const releases: Array<() => void> = [];
|
|
const heldHook = () =>
|
|
new Promise<void>((resolve) => {
|
|
releases.push(resolve);
|
|
});
|
|
hookRunnerMocks.runSessionEnd.mockImplementationOnce(heldHook);
|
|
hookRunnerMocks.runSessionStart.mockImplementationOnce(heldHook);
|
|
|
|
await incrementCompactionCount({
|
|
cfg: { session: { store: storePath } } as OpenClawConfig,
|
|
sessionEntry: entry,
|
|
sessionStore,
|
|
sessionKey,
|
|
storePath,
|
|
newSessionId: "s2",
|
|
});
|
|
|
|
await vi.waitFor(() => expect(getActiveGatewayRootWorkCount()).toBe(2));
|
|
await vi.waitFor(() => expect(releases).toHaveLength(2));
|
|
for (const release of releases) {
|
|
release();
|
|
}
|
|
await vi.waitFor(() => expect(getActiveGatewayRootWorkCount()).toBe(0));
|
|
});
|
|
|
|
it("hands compaction lifecycle hooks off after restart drain closes admission", async () => {
|
|
const { storePath, sessionKey, sessionStore, entry } = await createFixture();
|
|
const releases: Array<() => void> = [];
|
|
const heldHook = () =>
|
|
new Promise<void>((resolve) => {
|
|
releases.push(resolve);
|
|
});
|
|
hookRunnerMocks.runSessionEnd.mockImplementationOnce(heldHook);
|
|
hookRunnerMocks.runSessionStart.mockImplementationOnce(heldHook);
|
|
const admission = tryBeginGatewayRootWorkAdmission();
|
|
expect(admission).not.toBeNull();
|
|
|
|
await admission?.run(async () => {
|
|
markGatewayRestartDraining();
|
|
await incrementCompactionCount({
|
|
cfg: { session: { store: storePath } } as OpenClawConfig,
|
|
sessionEntry: entry,
|
|
sessionStore,
|
|
sessionKey,
|
|
storePath,
|
|
newSessionId: "s2",
|
|
});
|
|
await vi.waitFor(() => expect(releases).toHaveLength(2));
|
|
expect(getActiveGatewayRootWorkCount()).toBe(3);
|
|
});
|
|
|
|
admission?.release();
|
|
expect(getActiveGatewayRootWorkCount()).toBe(2);
|
|
for (const release of releases) {
|
|
release();
|
|
}
|
|
await vi.waitFor(() => expect(getActiveGatewayRootWorkCount()).toBe(0));
|
|
expect(hookRunnerMocks.runSessionEnd).toHaveBeenCalledTimes(1);
|
|
expect(hookRunnerMocks.runSessionStart).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("recreates a complete persisted row when compaction updates a missing store row", async () => {
|
|
const { storePath, sessionKey, sessionStore, entry } = await createFixture();
|
|
await applySessionEntryLifecycleMutation({
|
|
storePath,
|
|
removals: [{ sessionKey }],
|
|
skipMaintenance: true,
|
|
});
|
|
|
|
await incrementCompactionCount({
|
|
sessionEntry: entry,
|
|
sessionStore,
|
|
sessionKey,
|
|
storePath,
|
|
newSessionId: "s2",
|
|
tokensAfter: 123,
|
|
now: 456,
|
|
});
|
|
|
|
const persisted = loadSessionEntry({ storePath, sessionKey });
|
|
expect(sessionStore[sessionKey]?.sessionId).toBe("s2");
|
|
expect(sessionStore[sessionKey]).not.toHaveProperty("sessionFile");
|
|
expect(sessionStore[sessionKey]?.usageFamilyKey).toBe(sessionKey);
|
|
expect(sessionStore[sessionKey]?.usageFamilySessionIds).toEqual(["s1", "s2"]);
|
|
expect(sessionStore[sessionKey]?.compactionCount).toBe(1);
|
|
expect(sessionStore[sessionKey]?.totalTokens).toBe(123);
|
|
expect(sessionStore[sessionKey]?.updatedAt).toBeGreaterThanOrEqual(entry.updatedAt);
|
|
expect(persisted?.sessionId).toBe("s2");
|
|
expect(persisted).not.toHaveProperty("sessionFile");
|
|
expect(persisted?.usageFamilyKey).toBe(sessionKey);
|
|
expect(persisted?.usageFamilySessionIds).toEqual(["s1", "s2"]);
|
|
expect(persisted?.compactionCount).toBe(1);
|
|
expect(persisted?.totalTokens).toBe(123);
|
|
expect(persisted?.updatedAt).toBeGreaterThanOrEqual(entry.updatedAt);
|
|
});
|
|
|
|
it("does not account compaction against a replaced session", async () => {
|
|
const { storePath, sessionKey, sessionStore, entry } = await createFixture();
|
|
const replacement = { ...entry, sessionId: "s2", lifecycleRevision: "revision-2" };
|
|
await replaceSessionEntry({ storePath, sessionKey }, replacement);
|
|
|
|
const result = await incrementCompactionCount({
|
|
sessionEntry: entry,
|
|
sessionStore,
|
|
sessionKey,
|
|
storePath,
|
|
tokensAfter: 123,
|
|
expectedSession: { sessionId: "s1", lifecycleRevision: undefined },
|
|
});
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(sessionStore[sessionKey]).toBe(entry);
|
|
expect(loadSessionEntry({ storePath, sessionKey })).toMatchObject({
|
|
sessionId: "s2",
|
|
lifecycleRevision: "revision-2",
|
|
compactionCount: 0,
|
|
});
|
|
expect(loadSessionEntry({ storePath, sessionKey })).not.toHaveProperty("totalTokens");
|
|
});
|
|
|
|
it("does not account compaction after invocation authority closes", async () => {
|
|
const { storePath, sessionKey, sessionStore, entry } = await createFixture();
|
|
|
|
const result = await incrementCompactionCount({
|
|
sessionEntry: entry,
|
|
sessionStore,
|
|
sessionKey,
|
|
storePath,
|
|
tokensAfter: 123,
|
|
expectedSession: entry,
|
|
authorize: () => false,
|
|
});
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(sessionStore[sessionKey]).toBe(entry);
|
|
expect(loadSessionEntry({ storePath, sessionKey })).toMatchObject({
|
|
sessionId: "s1",
|
|
compactionCount: 0,
|
|
});
|
|
expect(loadSessionEntry({ storePath, sessionKey })).not.toHaveProperty("totalTokens");
|
|
});
|
|
|
|
it("does not commit accounting when authority closes after the queued updater", async () => {
|
|
const { storePath, sessionKey, sessionStore, entry } = await createFixture();
|
|
let authorized = true;
|
|
let authorizeCalls = 0;
|
|
|
|
const result = await incrementCompactionCount({
|
|
sessionEntry: entry,
|
|
sessionStore,
|
|
sessionKey,
|
|
storePath,
|
|
tokensAfter: 123,
|
|
expectedSession: entry,
|
|
authorize: () => {
|
|
authorizeCalls += 1;
|
|
if (authorizeCalls === 2) {
|
|
queueMicrotask(() => {
|
|
authorized = false;
|
|
});
|
|
}
|
|
return authorized;
|
|
},
|
|
});
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(authorizeCalls).toBe(3);
|
|
expect(sessionStore[sessionKey]).toBe(entry);
|
|
expect(loadSessionEntry({ storePath, sessionKey })).toMatchObject({
|
|
sessionId: "s1",
|
|
compactionCount: 0,
|
|
});
|
|
expect(loadSessionEntry({ storePath, sessionKey })).not.toHaveProperty("totalTokens");
|
|
});
|
|
});
|