File size: 9,023 Bytes
76289e7 | 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | // Diagnostic session context tests cover session context capture for diagnostics.
import fs from "node:fs";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
appendTranscriptMessageSync,
readLatestTranscriptAssistantText,
replaceSessionEntry,
} from "../config/sessions/session-accessor.js";
import { saveCronStore } from "../cron/store.js";
import {
createOpenClawTestState,
type OpenClawTestState,
} from "../test-utils/openclaw-test-state.js";
import {
formatCronSessionDiagnosticFields,
formatStoppedCronSessionDiagnosticFields,
resolveCronSessionDiagnosticContext,
} from "./diagnostic-session-context.js";
let tempDir: string | undefined;
let testState: OpenClawTestState | undefined;
async function seedSessionTranscript(params: {
agentId: string;
sessionId: string;
sessionKey: string;
messages: unknown[];
incognito?: boolean;
}) {
const { agentId, sessionId, sessionKey, messages, incognito } = params;
await replaceSessionEntry(
{ agentId, sessionKey },
{ sessionId, updatedAt: 1, ...(incognito ? { incognito: true } : {}) },
);
for (const message of messages) {
appendTranscriptMessageSync({ agentId, sessionId, sessionKey }, { message });
}
}
describe("diagnostic session context", () => {
beforeEach(async () => {
testState = await createOpenClawTestState({
layout: "state-only",
prefix: "openclaw-diagnostic-session-",
});
tempDir = testState.stateDir;
});
afterEach(async () => {
await testState?.cleanup();
testState = undefined;
tempDir = undefined;
});
it("parses cron run session keys", () => {
expect(
resolveCronSessionDiagnosticContext({
sessionKey: "agent:clawblocker:cron:job-123:run:run-456",
}),
).toMatchObject({ agentId: "clawblocker", cronJobId: "job-123", cronRunId: "run-456" });
});
it("formats cron job and last assistant context for stalled session logs", async () => {
const stateDir = tempDir!;
await saveCronStore(path.join(stateDir, "cron", "jobs.json"), {
version: 1,
jobs: [
{
id: "job-123",
name: "Twitter Mention Moderation Agent",
enabled: true,
createdAtMs: 1_700_000_000_000,
updatedAtMs: 1_700_000_000_000,
schedule: { kind: "every", everyMs: 60_000 },
sessionTarget: "main",
wakeMode: "next-heartbeat",
payload: { kind: "systemEvent", text: "tick" },
state: {},
},
],
});
const sessionKey = "agent:clawblocker:cron:job-123:run:run-456";
await seedSessionTranscript({
agentId: "clawblocker",
sessionId: "run-456",
sessionKey,
messages: [
{ role: "user", content: "run" },
{
role: "assistant",
content: [{ type: "text", text: "There are 40\ncached mentions ready." }],
},
],
});
const context = resolveCronSessionDiagnosticContext({
sessionKey,
activeSessionId: "run-456",
});
expect(formatCronSessionDiagnosticFields(context)).toContain("cronJobId=job-123");
expect(formatCronSessionDiagnosticFields(context)).toContain("cronRunId=run-456");
expect(formatCronSessionDiagnosticFields(context)).toContain(
'cronJob="Twitter Mention Moderation Agent"',
);
expect(formatCronSessionDiagnosticFields(context)).toContain(
'lastAssistant="There are 40 cached mentions ready."',
);
expect(formatStoppedCronSessionDiagnosticFields(context)).toContain(
'stopped="Twitter Mention Moderation Agent"',
);
});
it("reads the latest visible app-agent assistant text from its SQLite transcript", async () => {
const sessionKey = "agent:oauth-agent:main";
await seedSessionTranscript({
agentId: "oauth-agent",
sessionId: "oauth-session",
sessionKey,
messages: [
{ role: "assistant", content: "older reply" },
{ role: "user", content: "later user" },
{
role: "assistant",
content: [
{
type: "text",
text: "internal reasoning",
textSignature: JSON.stringify({ v: 1, id: "reasoning", phase: "commentary" }),
},
{
type: "text",
text: "latest visible reply",
textSignature: JSON.stringify({ v: 1, id: "reply", phase: "final_answer" }),
},
],
},
],
});
const context = resolveCronSessionDiagnosticContext({
sessionKey,
activeSessionId: "oauth-session",
});
expect(context.lastAssistant).toBe("latest visible reply");
expect(formatCronSessionDiagnosticFields(context)).toBe('lastAssistant="latest visible reply"');
});
it.each(["dashboard", "subagent", "internal-session-effects"])(
"never exposes a %s incognito assistant reply to durable diagnostics",
async (sessionSurface) => {
const sessionKey = `agent:main:${sessionSurface}:incognito-private`;
const sessionId = `incognito-${sessionSurface}`;
const privateReply = `memory-only ${sessionSurface} reply`;
await seedSessionTranscript({
agentId: "main",
incognito: true,
sessionId,
sessionKey,
messages: [{ role: "assistant", content: privateReply }],
});
expect(
readLatestTranscriptAssistantText({ agentId: "main", sessionId, sessionKey })?.text,
).toBe(privateReply);
expect(
resolveCronSessionDiagnosticContext({ sessionKey, activeSessionId: sessionId }),
).toEqual({});
expect(
fs.existsSync(path.join(tempDir!, "agents", "main", "agent", "openclaw-agent.sqlite")),
).toBe(false);
},
);
it("requires the authoritative current session id before reading transcript text", async () => {
const sessionKey = "agent:oauth-agent:main";
await seedSessionTranscript({
agentId: "oauth-agent",
sessionId: "current-session",
sessionKey,
messages: [{ role: "assistant", content: "current private reply" }],
});
expect(
resolveCronSessionDiagnosticContext({
sessionKey,
activeSessionId: "rotated-session",
}).lastAssistant,
).toBeUndefined();
expect(resolveCronSessionDiagnosticContext({ sessionKey }).lastAssistant).toBeUndefined();
});
it("keeps identical session ids isolated to their owning agents", async () => {
await seedSessionTranscript({
agentId: "main",
sessionId: "shared-session-id",
sessionKey: "agent:main:main",
messages: [{ role: "assistant", content: "main private reply" }],
});
await seedSessionTranscript({
agentId: "oauth-agent",
sessionId: "shared-session-id",
sessionKey: "agent:oauth-agent:main",
messages: [{ role: "assistant", content: "oauth private reply" }],
});
expect(
resolveCronSessionDiagnosticContext({
sessionKey: "agent:oauth-agent:main",
activeSessionId: "shared-session-id",
}).lastAssistant,
).toBe("oauth private reply");
});
it("rejects malformed agent ids before resolving a default agent transcript", async () => {
await seedSessionTranscript({
agentId: "main",
sessionId: "main-session",
sessionKey: "agent:main:main",
messages: [{ role: "assistant", content: "main private reply" }],
});
expect(
resolveCronSessionDiagnosticContext({
sessionKey: "agent:../main:main",
activeSessionId: "main-session",
}),
).toEqual({});
});
it("does not treat channel-owned cron segments as cron metadata", async () => {
const sessionKey = "agent:oauth-agent:slack:cron:job-123:run:run-456";
await seedSessionTranscript({
agentId: "oauth-agent",
sessionId: "slack-session",
sessionKey,
messages: [{ role: "assistant", content: "channel reply" }],
});
const context = resolveCronSessionDiagnosticContext({
sessionKey,
activeSessionId: "slack-session",
});
expect(context.lastAssistant).toBe("channel reply");
expect(context.cronJobId).toBeUndefined();
expect(context.cronRunId).toBeUndefined();
});
it("keeps bounded quoted fields UTF-16 safe", () => {
const prefix = "a".repeat(136);
expect(
formatCronSessionDiagnosticFields({ cronJobName: `${prefix}😀${"b".repeat(140)}` }),
).toBe(`cronJob="${prefix}..."`);
});
it("does not create an agent database when its session store is missing", () => {
const databasePath = path.join(
tempDir!,
"agents",
"missing-agent",
"agent",
"openclaw-agent.sqlite",
);
expect(fs.existsSync(databasePath)).toBe(false);
expect(
resolveCronSessionDiagnosticContext({
sessionKey: "agent:missing-agent:main",
activeSessionId: "missing",
}).lastAssistant,
).toBeUndefined();
expect(fs.existsSync(databasePath)).toBe(false);
});
});
|