File size: 6,482 Bytes
e249c6d | 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import type { InternalSessionEntry as SessionEntry } from "../../config/sessions/types.js";
const hoisted = vi.hoisted(() => ({
store: {} as Record<string, SessionEntry>,
terminalTranscriptNewer: false,
}));
vi.mock("../../config/sessions/session-accessor.js", () => ({
loadExactSessionEntryReadOnly: ({ sessionKey }: { sessionKey: string }) => {
const entry = hoisted.store[sessionKey];
return entry ? { sessionKey, entry: structuredClone(entry) } : undefined;
},
listSessionEntriesReadOnly: () =>
Object.entries(hoisted.store).map(([sessionKey, entry]) => ({
sessionKey,
entry,
})),
}));
vi.mock("../../config/sessions/paths.js", () => ({
resolveSessionStorePathCore: () => "/stores/main.json",
}));
vi.mock("../../config/sessions/lifecycle.js", async () => {
const actual = await vi.importActual<typeof import("../../config/sessions/lifecycle.js")>(
"../../config/sessions/lifecycle.js",
);
return {
...actual,
hasTerminalMainSessionTranscriptNewerThanRegistrySync: () => hoisted.terminalTranscriptNewer,
};
});
const { resolveSession } = await import("./session.js");
const DAY_MS = 24 * 60 * 60 * 1000;
function seedProviderOwned(sessionKey: string): void {
const startedAt = Date.now() - DAY_MS;
hoisted.store = {
[sessionKey]: {
sessionId: "old-session-id",
updatedAt: startedAt,
sessionStartedAt: startedAt,
lastInteractionAt: startedAt,
model: "claude-opus-4-6",
modelProvider: "claude-cli",
cliSessionBindings: { "claude-cli": { sessionId: "cli-conversation-xyz" } },
},
};
}
describe("command resolveSession provider-owned daily reset", () => {
beforeEach(() => {
hoisted.terminalTranscriptNewer = false;
});
it("keeps a provider-owned CLI session with the default reset policy", () => {
const sessionKey = "agent:main:cli";
seedProviderOwned(sessionKey);
const result = resolveSession({
cfg: { session: {} } as OpenClawConfig,
sessionKey,
agentId: "main",
});
expect(result.isNewSession).toBe(false);
expect(result.sessionId).toBe("old-session-id");
});
it("still rotates a non-provider-owned session across the daily boundary", () => {
const sessionKey = "agent:main:cli";
const startedAt = Date.now() - DAY_MS;
hoisted.store = {
[sessionKey]: {
sessionId: "old-session-id",
updatedAt: startedAt,
sessionStartedAt: startedAt,
lastInteractionAt: startedAt,
pendingTranscriptRepair: [
{ id: "predecessor-repair", text: "old reply", createdAt: startedAt },
],
lastRunId: "settled-old-run",
},
};
const result = resolveSession({
cfg: { session: { reset: { mode: "daily" } } } as OpenClawConfig,
sessionKey,
agentId: "main",
});
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe("old-session-id");
expect(result.sessionEntry?.pendingTranscriptRepair).toBeUndefined();
expect(result.sessionEntry?.lastRunId).toBeUndefined();
});
it("keeps a model-locked session across the daily boundary", () => {
const sessionKey = "agent:main:codex-supervised";
const startedAt = Date.now() - DAY_MS;
hoisted.store = {
[sessionKey]: {
sessionId: "locked-session-id",
updatedAt: startedAt,
sessionStartedAt: startedAt,
lastInteractionAt: startedAt,
agentHarnessId: "codex",
modelSelectionLocked: true,
},
};
hoisted.terminalTranscriptNewer = true;
const result = resolveSession({
cfg: { session: {} } as OpenClawConfig,
sessionKey,
agentId: "main",
});
expect(result.isNewSession).toBe(false);
expect(result.sessionId).toBe("locked-session-id");
});
it("carries stored thinking and verbose preferences during terminal transcript recovery", () => {
const sessionKey = "agent:main:cli";
const now = Date.now();
hoisted.store = {
[sessionKey]: {
sessionId: "recovering-session-id",
updatedAt: now,
sessionStartedAt: now,
lastInteractionAt: now,
thinkingLevel: "high",
verboseLevel: "full",
},
};
hoisted.terminalTranscriptNewer = true;
const result = resolveSession({
cfg: { session: {} } as OpenClawConfig,
sessionKey,
agentId: "main",
});
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe("recovering-session-id");
expect(result.persistedThinking).toBe("high");
expect(result.persistedVerbose).toBe("full");
});
it("carries preferences across a daily reset", () => {
const sessionKey = "agent:main:cli";
const startedAt = Date.now() - 2 * DAY_MS;
hoisted.store = {
[sessionKey]: {
sessionId: "daily-expired-session-id",
updatedAt: startedAt,
sessionStartedAt: startedAt,
lastInteractionAt: startedAt,
thinkingLevel: "high",
verboseLevel: "full",
},
};
const result = resolveSession({
cfg: { session: { reset: { mode: "daily" } } } as OpenClawConfig,
sessionKey,
agentId: "main",
});
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe("daily-expired-session-id");
expect(result.persistedThinking).toBe("high");
expect(result.persistedVerbose).toBe("full");
});
it("carries preferences when terminal recovery overlaps an idle reset", () => {
const sessionKey = "agent:main:cli";
const now = Date.now();
const lastInteractionAt = now - 60 * 60 * 1000;
hoisted.store = {
[sessionKey]: {
sessionId: "idle-expired-session-id",
updatedAt: lastInteractionAt,
sessionStartedAt: now,
lastInteractionAt,
thinkingLevel: "high",
verboseLevel: "on",
},
};
hoisted.terminalTranscriptNewer = true;
const result = resolveSession({
cfg: {
session: {
reset: { mode: "idle", idleMinutes: 30 },
},
} as OpenClawConfig,
sessionKey,
agentId: "main",
});
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe("idle-expired-session-id");
expect(result.persistedThinking).toBe("high");
expect(result.persistedVerbose).toBe("on");
});
});
|