Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
feat(openclaw-plugin): add archive-aware context assembly and async session commit (#938)
Mingjian Que08b278da8141
diff --git a/examples/openclaw-plugin/__tests__/context-engine-assemble.test.ts b/examples/openclaw-plugin/__tests__/context-engine-assemble.test.tsnew file mode 100644index 000000000..0dcfa0c17--- /dev/null+++ b/examples/openclaw-plugin/__tests__/context-engine-assemble.test.ts@@ -0,0 +1,282 @@+import { describe, expect, it, vi } from "vitest";++import type { OpenVikingClient } from "../client.js";+import { memoryOpenVikingConfigSchema } from "../config.js";+import { createMemoryOpenVikingContextEngine } from "../context-engine.js";++const cfg = memoryOpenVikingConfigSchema.parse({+ mode: "remote",+ baseUrl: "http://127.0.0.1:1933",+ autoCapture: false,+ autoRecall: false,+ ingestReplyAssist: false,+});++function roughEstimate(messages: unknown[]): number {+ return Math.ceil(JSON.stringify(messages).length / 4);+}++function makeLogger() {+ return {+ info: vi.fn(),+ warn: vi.fn(),+ error: vi.fn(),+ };+}++function makeStats() {+ return {+ totalArchives: 0,+ includedArchives: 0,+ droppedArchives: 0,+ failedArchives: 0,+ activeTokens: 0,+ archiveTokens: 0,+ };+}++function makeEngine(contextResult: unknown) {+ const logger = makeLogger();+ const client = {+ getContextForAssemble: vi.fn().mockResolvedValue(contextResult),+ } as unknown as OpenVikingClient;+ const getClient = vi.fn().mockResolvedValue(client);+ const resolveAgentId = vi.fn((sessionId: string) => `agent:${sessionId}`);++ const engine = createMemoryOpenVikingContextEngine({+ id: "openviking",+ name: "Context Engine (OpenViking)",+ version: "test",+ cfg,+ logger,+ getClient,+ resolveAgentId,+ });++ return {+ engine,+ client: client as unknown as { getContextForAssemble: ReturnType<typeof vi.fn> },+ getClient,+ logger,+ resolveAgentId,+ };+}++describe("context-engine assemble()", () => {+ it("assembles archives and completed tool parts into agent messages", async () => {+ const { engine, client, resolveAgentId } = makeEngine({+ archives: [+ {+ index: 2,+ overview: "# Session Summary\nPreviously discussed repository setup.",+ abstract: "Previously discussed repository setup.",+ },+ ],+ messages: [+ {+ id: "msg_1",+ role: "assistant",+ created_at: "2026-03-24T00:00:00Z",+ parts: [+ { type: "text", text: "I checked the latest context." },+ { type: "context", abstract: "User prefers concise answers." },+ {+ type: "tool",+ tool_id: "tool_123",+ tool_name: "read_file",+ tool_input: { path: "src/app.ts" },+ tool_output: "export const value = 1;",+ tool_status: "completed",+ },+ ],+ },+ ],+ estimatedTokens: 321,+ stats: {+ ...makeStats(),+ totalArchives: 1,+ includedArchives: 1,+ archiveTokens: 40,+ activeTokens: 281,+ },+ });++ const liveMessages = [{ role: "user", content: "fallback live message" }];+ const result = await engine.assemble({+ sessionId: "session-1",+ messages: liveMessages,+ tokenBudget: 4096,+ });++ expect(resolveAgentId).toHaveBeenCalledWith("session-1");+ expect(client.getContextForAssemble).toHaveBeenCalledWith("session-1", 4096, "agent:session-1");+ expect(result.estimatedTokens).toBe(321);+ expect(result.systemPromptAddition).toContain("Compressed Context");+ expect(result.messages).toEqual([+ {+ role: "user",+ content: "# Session Summary\nPreviously discussed repository setup.",+ },+ {+ role: "assistant",+ content: [+ { type: "text", text: "I checked the latest context." },+ { type: "text", text: "User prefers concise answers." },+ {+ type: "toolUse",+ id: "tool_123",+ name: "read_file",+ input: { path: "src/app.ts" },+ },+ ],+ },+ {+ role: "toolResult",+ toolCallId: "tool_123",+ toolName: "read_file",+ content: [{ type: "text", text: "export const value = 1;" }],+ isError: false,+ },+ ]);+ });++ it("emits a non-error toolResult for a running tool (not a synthetic error)", async () => {+ const { engine } = makeEngine({+ archives: [],+ messages: [+ {+ id: "msg_2",+ role: "assistant",+ created_at: "2026-03-24T00:00:00Z",+ parts: [+ {+ type: "tool",+ tool_id: "tool_running",+ tool_name: "bash",+ tool_input: { command: "npm test" },+ tool_output: "",+ tool_status: "running",+ },+ ],+ },+ ],+ estimatedTokens: 88,+ stats: {+ ...makeStats(),+ activeTokens: 88,+ },+ });++ const result = await engine.assemble({+ sessionId: "session-running",+ messages: [],+ });++ expect(result.systemPromptAddition).toBeUndefined();+ expect(result.messages).toHaveLength(2);+ expect(result.messages[0]).toEqual({+ role: "assistant",+ content: [+ {+ type: "toolUse",+ id: "tool_running",+ name: "bash",+ input: { command: "npm test" },+ },+ ],+ });+ expect(result.messages[1]).toMatchObject({+ role: "toolResult",+ toolCallId: "tool_running",+ toolName: "bash",+ isError: false,+ });+ const text = (result.messages[1] as any).content?.[0]?.text ?? "";+ expect(text).toContain("interrupted");+ expect((result.messages[1] as { content: Array<{ text: string }> }).content[0]?.text).toContain(+ "missing tool result",+ );+ });++ it("degrades tool parts without tool_id into assistant text blocks", async () => {+ const { engine } = makeEngine({+ archives: [],+ messages: [+ {+ id: "msg_3",+ role: "assistant",+ created_at: "2026-03-24T00:00:00Z",+ parts: [+ { type: "text", text: "Tool state snapshot:" },+ {+ type: "tool",+ tool_id: "",+ tool_name: "grep",+ tool_input: { pattern: "TODO" },+ tool_output: "src/app.ts:17 TODO refine this",+ tool_status: "completed",+ },+ ],+ },+ ],+ estimatedTokens: 71,+ stats: {+ ...makeStats(),+ activeTokens: 71,+ },+ });++ const result = await engine.assemble({+ sessionId: "session-missing-id",+ messages: [],+ });++ expect(result.messages).toEqual([+ {+ role: "assistant",+ content: [+ { type: "text", text: "Tool state snapshot:" },+ {+ type: "text",+ text: "[grep] (completed)\nInput: {\"pattern\":\"TODO\"}\nOutput: src/app.ts:17 TODO refine this",+ },+ ],+ },+ ]);+ });++ it("falls back to live messages when assembled active messages look truncated", async () => {+ const { engine } = makeEngine({+ archives: [],+ messages: [+ {+ id: "msg_4",+ role: "user",+ created_at: "2026-03-24T00:00:00Z",+ parts: [{ type: "text", text: "Only one stored message" }],+ },+ ],+ estimatedTokens: 12,+ stats: {+ ...makeStats(),+ activeTokens: 12,+ },+ });++ const liveMessages = [+ { role: "user", content: "message one" },+ { role: "assistant", content: [{ type: "text", text: "message two" }] },+ ];++ const result = await engine.assemble({+ sessionId: "session-fallback",+ messages: liveMessages,+ tokenBudget: 1024,+ });++ expect(result).toEqual({+ messages: liveMessages,+ estimatedTokens: roughEstimate(liveMessages),+ });+ });+});diff --git a/examples/openclaw-plugin/client.ts b/examples/openclaw-plugin/client.tsindex 11187dd96..df5943f16 100644--- a/examples/openclaw-plugin/client.ts+++ b/examples/openclaw-plugin/client.ts@@ -35,6 +35,33 @@ export type PendingClientEntry = {reject: (err: unknown) => void;};+export type CommitSessionResult = {+ session_id: string;+ /** "accepted" (async), "completed", "failed", or "timeout" (wait mode). */+ status: string;+ task_id?: string;+ archive_uri?: string;+ archived?: boolean;+ /** Present when wait=true and extraction completed. Keyed by category. */+ memories_extracted?: Record<string, number>;+ error?: string;+};++export type TaskResult = {+ task_id: string;+ task_type: string;+ status: string;+ created_at: number;+ updated_at: number;+ resource_id?: string;+ result?: Record<string, unknown>;+ error?: string;+};++function sleep(ms: number): Promise<void> {+ return new Promise((resolve) => setTimeout(resolve, ms));+}+export const localClientCache = new Map<string, LocalClientCacheEntry>();// Module-level pending promise map: shared across all plugin registrations so@@ -260,9 +287,21 @@ export class OpenVikingClient {);}- /** GET session — server auto-creates if absent; also loads messages from storage before extract. */- async getSession(sessionId: string, agentId?: string): Promise<{ message_count?: number }> {- return this.request<{ message_count?: number }>(+ /** GET session — server auto-creates if absent; returns session meta including message stats and token usage. */+ async getSession(sessionId: string, agentId?: string): Promise<{+ message_count?: number;+ commit_count?: number;+ last_commit_at?: string;+ pending_tokens?: number;+ llm_token_usage?: { prompt_tokens: number; completion_tokens: number; total_tokens: number };+ }> {+ return this.request<{+ message_count?: number;+ commit_count?: number;+ last_commit_at?: string;+ pending_tokens?: number;+ llm_token_usage?: { prompt_tokens: number; completion_tokens: number; total_tokens: number };+ }>(`/api/v1/sessions/${encodeURIComponent(sessionId)}`,{ method: "GET" },agentId,@@ -271,38 +310,84 @@ export class OpenVikingClient {/*** Commit a session: archive (Phase 1) and extract memories (Phase 2).- * wait=false (default): Phase 2 runs in background, returns task_id for polling.- * wait=true: blocks until Phase 2 completes, returns memories_extracted count.+ *+ * wait=false (default): returns immediately after Phase 1 with task_id.+ * wait=true: after Phase 1, polls GET /tasks/{task_id} until Phase 2+ * completes (or times out), then returns the merged result.*/async commitSession(sessionId: string,- options?: { wait?: boolean; agentId?: string },+ options?: { wait?: boolean; timeoutMs?: number; agentId?: string },+ ): Promise<CommitSessionResult> {+ const result = await this.request<CommitSessionResult>(+ `/api/v1/sessions/${encodeURIComponent(sessionId)}/commit`,+ { method: "POST", body: JSON.stringify({}) },+ options?.agentId,+ );++ if (!options?.wait || !result.task_id) {+ return result;+ }++ // Client-side poll until Phase 2 finishes+ const deadline = Date.now() + (options.timeoutMs ?? 120_000);+ const pollInterval = 500;+ while (Date.now() < deadline) {+ await sleep(pollInterval);+ const task = await this.getTask(result.task_id, options.agentId).catch(() => null);+ if (!task) break;+ if (task.status === "completed") {+ const taskResult = (task.result ?? {}) as Record<string, unknown>;+ result.status = "completed";+ result.memories_extracted = (taskResult.memories_extracted ?? {}) as Record<string, number>;+ return result;+ }+ if (task.status === "failed") {+ result.status = "failed";+ result.error = task.error;+ return result;+ }+ }+ result.status = "timeout";+ return result;+ }++ /** Poll a background task by ID. */+ async getTask(taskId: string, agentId?: string): Promise<TaskResult> {+ return this.request<TaskResult>(+ `/api/v1/tasks/${encodeURIComponent(taskId)}`,+ { method: "GET" },+ agentId,+ );+ }++ async getContextForAssemble(+ sessionId: string,+ tokenBudget: number = 128_000,+ agentId?: string,): Promise<{- session_id: string;- status: string;- task_id?: string;- archive_uri?: string;- archived?: boolean;- memories_extracted?: number;+ archives: Array<{ index: number; overview: string; abstract: string }>;+ messages: Array<{ id: string; role: string; parts: unknown[]; created_at: string }>;+ estimatedTokens: number;+ stats: {+ totalArchives: number;+ includedArchives: number;+ droppedArchives: number;+ failedArchives: number;+ activeTokens: number;+ archiveTokens: number;+ };}> {- const wait = options?.wait ?? false;- return this.request<{- session_id: string;- status: string;- task_id?: string;- archive_uri?: string;- archived?: boolean;- memories_extracted?: number;- }>(`/api/v1/sessions/${encodeURIComponent(sessionId)}/commit?wait=${wait}`, {- method: "POST",- body: JSON.stringify({}),- }, options?.agentId);+ return this.request(+ `/api/v1/sessions/${encodeURIComponent(sessionId)}/context-for-assemble?token_budget=${tokenBudget}`,+ { method: "GET" },+ agentId,+ );}async deleteSession(sessionId: string, agentId?: string): Promise<void> {await this.request(`/api/v1/sessions/${encodeURIComponent(sessionId)}`, { method: "DELETE" }, agentId);}-async deleteUri(uri: string, agentId?: string): Promise<void> {await this.request(`/api/v1/fs?uri=${encodeURIComponent(uri)}&recursive=false`, {method: "DELETE",diff --git a/examples/openclaw-plugin/config.ts b/examples/openclaw-plugin/config.tsindex 90100593f..90532c602 100644--- a/examples/openclaw-plugin/config.ts+++ b/examples/openclaw-plugin/config.ts@@ -23,6 +23,7 @@ export type MemoryOpenVikingConfig = {recallMaxContentChars?: number;recallPreferAbstract?: boolean;recallTokenBudget?: number;+ commitTokenThreshold?: number;ingestReplyAssist?: boolean;ingestReplyAssistMinSpeakerTurns?: number;ingestReplyAssistMinChars?: number;@@ -39,6 +40,7 @@ const DEFAULT_RECALL_SCORE_THRESHOLD = 0.15;const DEFAULT_RECALL_MAX_CONTENT_CHARS = 500;const DEFAULT_RECALL_PREFER_ABSTRACT = true;const DEFAULT_RECALL_TOKEN_BUDGET = 2000;+const DEFAULT_COMMIT_TOKEN_THRESHOLD = 2000;const DEFAULT_INGEST_REPLY_ASSIST = true;const DEFAULT_INGEST_REPLY_ASSIST_MIN_SPEAKER_TURNS = 2;const DEFAULT_INGEST_REPLY_ASSIST_MIN_CHARS = 120;@@ -118,6 +120,7 @@ export const memoryOpenVikingConfigSchema = {"recallMaxContentChars","recallPreferAbstract","recallTokenBudget",+ "commitTokenThreshold","ingestReplyAssist","ingestReplyAssistMinSpeakerTurns","ingestReplyAssistMinChars",@@ -181,6 +184,10 @@ export const memoryOpenVikingConfigSchema = {100,Math.min(50000, Math.floor(toNumber(cfg.recallTokenBudget, DEFAULT_RECALL_TOKEN_BUDGET))),),+ commitTokenThreshold: Math.max(+ 0,+ Math.min(100_000, Math.floor(toNumber(cfg.commitTokenThreshold, DEFAULT_COMMIT_TOKEN_THRESHOLD))),+ ),ingestReplyAssist: cfg.ingestReplyAssist !== false,ingestReplyAssistMinSpeakerTurns: Math.max(1,@@ -292,6 +299,12 @@ export const memoryOpenVikingConfigSchema = {advanced: true,help: "Maximum estimated tokens for auto-recall memory injection. Injection stops when budget is exhausted.",},+ commitTokenThreshold: {+ label: "Commit Token Threshold",+ placeholder: String(DEFAULT_COMMIT_TOKEN_THRESHOLD),+ advanced: true,+ help: "Minimum estimated pending tokens before auto-commit triggers. Set to 0 to commit every turn.",+ },ingestReplyAssist: {label: "Ingest Reply Assist",help: "When transcript-like memory ingestion is detected, add a lightweight reply instruction to reduce NO_REPLY.",diff --git a/examples/openclaw-plugin/context-engine.ts b/examples/openclaw-plugin/context-engine.tsindex 738dd2281..cd40dedcf 100644--- a/examples/openclaw-plugin/context-engine.ts+++ b/examples/openclaw-plugin/context-engine.ts@@ -8,6 +8,7 @@ import {trimForLog,toJsonLog,} from "./memory-ranking.js";+import { sanitizeToolUseResultPairing } from "./session-transcript-repair.js";type AgentMessage = {role?: string;@@ -91,6 +92,145 @@ function estimateTokens(messages: AgentMessage[]): number {return Math.max(1, messages.length * 80);}+function roughEstimate(messages: AgentMessage[]): number {+ return Math.ceil(JSON.stringify(messages).length / 4);+}++function totalExtractedMemories(memories?: Record<string, number>): number {+ if (!memories || typeof memories !== "object") {+ return 0;+ }+ return Object.values(memories).reduce((sum, count) => sum + (count ?? 0), 0);+}++function validTokenBudget(raw: unknown): number | undefined {+ if (typeof raw === "number" && Number.isFinite(raw) && raw > 0) {+ return raw;+ }+ return undefined;+}++/**+ * Convert an OpenViking stored message (parts-based format) into one or more+ * OpenClaw AgentMessages (content-blocks format).+ *+ * For assistant messages with ToolParts, this produces:+ * 1. The assistant message with toolUse blocks in its content array+ * 2. A separate toolResult message per ToolPart (carrying tool_output)+ */+function convertToAgentMessages(msg: { role: string; parts: unknown[] }): AgentMessage[] {+ const parts = msg.parts ?? [];+ const contentBlocks: Record<string, unknown>[] = [];+ const toolResults: AgentMessage[] = [];++ for (const part of parts) {+ if (!part || typeof part !== "object") continue;+ const p = part as Record<string, unknown>;++ if (p.type === "text" && typeof p.text === "string") {+ contentBlocks.push({ type: "text", text: p.text });+ } else if (p.type === "context") {+ if (typeof p.abstract === "string" && p.abstract) {+ contentBlocks.push({ type: "text", text: p.abstract });+ }+ } else if (p.type === "tool" && msg.role === "assistant") {+ const toolId = typeof p.tool_id === "string" ? p.tool_id : "";+ const toolName = typeof p.tool_name === "string" ? p.tool_name : "unknown";++ if (toolId) {+ contentBlocks.push({+ type: "toolUse",+ id: toolId,+ name: toolName,+ input: p.tool_input ?? {},+ });++ const status = typeof p.tool_status === "string" ? p.tool_status : "";+ const output = typeof p.tool_output === "string" ? p.tool_output : "";++ if (status === "completed" || status === "error") {+ toolResults.push({+ role: "toolResult",+ toolCallId: toolId,+ toolName,+ content: [{ type: "text", text: output || "(no output)" }],+ isError: status === "error",+ } as unknown as AgentMessage);+ } else {+ toolResults.push({+ role: "toolResult",+ toolCallId: toolId,+ toolName,+ content: [{ type: "text", text: "(interrupted — tool did not complete)" }],+ isError: false,+ } as unknown as AgentMessage);+ }+ } else {+ // No tool_id: degrade to text block to preserve information.+ // Cannot emit toolUse/toolResult without a valid id.+ const status = typeof p.tool_status === "string" ? p.tool_status : "unknown";+ const output = typeof p.tool_output === "string" ? p.tool_output : "";+ const segments = [`[${toolName}] (${status})`];+ if (p.tool_input) {+ try {+ segments.push(`Input: ${JSON.stringify(p.tool_input)}`);+ } catch {+ // non-serializable input, skip+ }+ }+ if (output) {+ segments.push(`Output: ${output}`);+ }+ contentBlocks.push({ type: "text", text: segments.join("\n") });+ }+ }+ }++ const result: AgentMessage[] = [];++ if (msg.role === "assistant") {+ result.push({ role: msg.role, content: contentBlocks });+ result.push(...toolResults);+ } else {+ const texts = contentBlocks+ .filter((b) => b.type === "text")+ .map((b) => b.text as string);+ result.push({ role: msg.role, content: texts.join("\n") || "" });+ }++ return result;+}++function normalizeAssistantContent(messages: AgentMessage[]): void {+ for (let i = 0; i < messages.length; i++) {+ const msg = messages[i];+ if (msg?.role === "assistant" && typeof msg.content === "string") {+ messages[i] = {+ ...msg,+ content: [{ type: "text", text: msg.content }],+ };+ }+ }+}++function buildSystemPromptAddition(): string {+ return [+ "## Compressed Context",+ "",+ "The conversation history above includes compressed session summaries",+ '(marked as "# Session Summary"). These summaries contain condensed',+ "information from earlier parts of the conversation.",+ "",+ "**Important:**",+ "- Summaries are compressed context — maps to details, not the details",+ " themselves.",+ "- For precision questions (exact commands, file paths, timestamps,",+ " config values): state that the information comes from a summary and",+ " may need verification.",+ "- Do not fabricate specific details from compressed summaries.",+ ].join("\n");+}+async function tryLegacyCompact(params: {sessionId: string;sessionFile: string;@@ -134,6 +274,34 @@ function warnOrInfo(logger: Logger, message: string): void {logger.info(message);}+function formatMessagesForLog(label: string, messages: AgentMessage[]): string {+ const lines: string[] = [`===== ${label} (${messages.length} msgs) =====`];+ for (let i = 0; i < messages.length; i++) {+ const msg = messages[i] as Record<string, unknown>;+ const role = msg.role ?? "?";+ const raw = msg.content;+ let text: string;+ if (typeof raw === "string") {+ text = raw;+ } else if (Array.isArray(raw)) {+ text = (raw as Record<string, unknown>[])+ .map((b) => {+ if (b.type === "text") return b.text;+ if (b.type === "toolUse") return `[toolUse: ${b.name}]`;+ if (b.type === "toolResult") return `[toolResult]`;+ return `[${b.type}]`;+ })+ .join("\n");+ } else {+ text = JSON.stringify(raw, null, 2);+ }+ lines.push(`--- [${i}] ${role} ---`);+ lines.push(String(text));+ }+ lines.push(`===== /${label} =====`);+ return lines.join("\n");+}+export function createMemoryOpenVikingContextEngine(params: {id: string;name: string;@@ -159,7 +327,7 @@ export function createMemoryOpenVikingContextEngine(params: {const agentId = resolveAgentId(sessionKey);const commitResult = await client.commitSession(sessionKey, { wait: true, agentId });logger.info(- `openviking: committed OV session for sessionKey=${sessionKey}, archived=${commitResult.archived ?? false}, memories=${commitResult.memories_extracted ?? 0}, task_id=${commitResult.task_id ?? "none"}`,+ `openviking: committed OV session for sessionKey=${sessionKey}, archived=${commitResult.archived ?? false}, memories=${totalExtractedMemories(commitResult.memories_extracted)}, task_id=${commitResult.task_id ?? "none"}`,);await client.deleteSession(sessionKey, agentId).catch(() => {});} catch (err) {@@ -203,10 +371,61 @@ export function createMemoryOpenVikingContextEngine(params: {},async assemble(assembleParams): Promise<AssembleResult> {- return {- messages: assembleParams.messages,- estimatedTokens: estimateTokens(assembleParams.messages),- };+ const { messages } = assembleParams;+ const tokenBudget = validTokenBudget(assembleParams.tokenBudget) ?? 128_000;++ const originalTokens = roughEstimate(messages);+ logger.info(formatMessagesForLog(`ORIGINAL CONTEXT (openclaw raw) msgs=${messages.length} ~${originalTokens} tokens`, messages));++ try {+ const client = await getClient();+ const OVSessionId = assembleParams.sessionId;+ const agentId = resolveAgentId(OVSessionId);+ const ctx = await client.getContextForAssemble(+ OVSessionId,+ tokenBudget,+ agentId,+ );+ const archiveCount = ctx?.archives?.length ?? 0;+ const activeCount = ctx?.messages?.length ?? 0;+ logger.info(`openviking: assemble OV ctx archives=${archiveCount} active=${activeCount}`);++ if (!ctx || (archiveCount === 0 && activeCount === 0)) {+ logger.info("openviking: assemble passthrough (no OV data)");+ return { messages, estimatedTokens: roughEstimate(messages) };+ }++ if (ctx.archives.length === 0 && ctx.messages.length < messages.length) {+ logger.info(`openviking: assemble passthrough (OV msgs=${ctx.messages.length} < input msgs=${messages.length})`);+ return { messages, estimatedTokens: roughEstimate(messages) };+ }++ const assembled: AgentMessage[] = [+ ...ctx.archives.map((a) => ({ role: "user" as const, content: a.overview })),+ ...ctx.messages.flatMap((m) => convertToAgentMessages(m)),+ ];++ normalizeAssistantContent(assembled);+ const sanitized = sanitizeToolUseResultPairing(assembled as never[]) as AgentMessage[];++ if (sanitized.length === 0 && messages.length > 0) {+ logger.info("openviking: assemble passthrough (sanitized=0, falling back to original)");+ return { messages, estimatedTokens: roughEstimate(messages) };+ }++ const assembledTokens = roughEstimate(sanitized);+ logger.info(formatMessagesForLog(`ASSEMBLED CONTEXT (openviking) ~${assembledTokens} tokens (ovEstimate=${ctx.estimatedTokens})`, sanitized));++ return {+ messages: sanitized,+ estimatedTokens: ctx.estimatedTokens,+ ...(ctx.archives.length > 0+ ? { systemPromptAddition: buildSystemPromptAddition() }+ : {}),+ };+ } catch {+ return { messages, estimatedTokens: roughEstimate(messages) };+ }},async afterTurn(afterTurnParams): Promise<void> {@@ -216,11 +435,12 @@ export function createMemoryOpenVikingContextEngine(params: {try {const sessionKey = extractSessionKey(afterTurnParams.runtimeContext);- const agentId = resolveAgentId(sessionKey ?? afterTurnParams.sessionId);+ const OVSessionId = sessionKey ?? afterTurnParams.sessionId;+ const agentId = resolveAgentId(OVSessionId);const messages = afterTurnParams.messages ?? [];if (messages.length === 0) {- logger.info("openviking: auto-capture skipped (messages=0)");+ logger.info("openviking: afterTurn skipped (messages=0)");return;}@@ -233,35 +453,58 @@ export function createMemoryOpenVikingContextEngine(params: {const { texts: newTexts, newCount } = extractNewTurnTexts(messages, start);if (newTexts.length === 0) {- logger.info("openviking: auto-capture skipped (no new user/assistant messages)");+ logger.info("openviking: afterTurn skipped (no new user/assistant messages)");return;}+ // Always store messages into OV session so assemble can retrieve them.+ // Capture decision only controls whether we trigger commit (archive+extract).+ const client = await getClient();const turnText = newTexts.join("\n");+ const sanitized = turnText.replace(/<relevant-memories>[\s\S]*?<\/relevant-memories>/gi, " ").replace(/\s+/g, " ").trim();++ if (sanitized) {+ await client.addSessionMessage(OVSessionId, "user", sanitized, agentId);+ logger.info(+ `openviking: afterTurn stored ${newCount} msgs in session=${OVSessionId} (${sanitized.length} chars)`,+ );+ } else {+ logger.info("openviking: afterTurn skipped store (sanitized text empty)");+ return;+ }++ // Capture decision: controls commit (archive + memory extraction)const decision = getCaptureDecision(turnText, cfg.captureMode, cfg.captureMaxLength);- const preview = turnText.length > 80 ? `${turnText.slice(0, 80)}...` : turnText;logger.info(- "openviking: capture-check " +- `shouldCapture=${String(decision.shouldCapture)} ` +- `reason=${decision.reason} newMsgCount=${newCount} text=\"${preview}\"`,+ `openviking: capture-check shouldCapture=${String(decision.shouldCapture)} reason=${decision.reason}`,);if (!decision.shouldCapture) {- logger.info("openviking: auto-capture skipped (capture decision rejected)");+ logger.info("openviking: afterTurn skipped commit (capture decision rejected)");return;}- const client = await getClient();- const OVSessionId = sessionKey ?? afterTurnParams.sessionId;- await client.addSessionMessage(OVSessionId, "user", decision.normalizedText, agentId);- const commitResult = await client.commitSession(OVSessionId, { wait: true, agentId });+ const session = await client.getSession(OVSessionId, agentId);+ const pendingTokens = session.pending_tokens ?? 0;++ if (pendingTokens < cfg.commitTokenThreshold) {+ logger.info(+ `openviking: pending_tokens=${pendingTokens}/${cfg.commitTokenThreshold} in session=${OVSessionId}, deferring commit`,+ );+ return;+ }++ logger.info(+ `openviking: committing session=${OVSessionId} (wait=false), pendingTokens=${pendingTokens}, threshold=${cfg.commitTokenThreshold}`,+ );+ const commitResult = await client.commitSession(OVSessionId, { wait: false, agentId });logger.info(- `openviking: committed ${newCount} messages in session=${OVSessionId}, ` +- `archived=${commitResult.archived ?? false}, memories=${commitResult.memories_extracted ?? 0}, ` ++ `openviking: committed session=${OVSessionId}, ` ++ `status=${commitResult.status}, archived=${commitResult.archived ?? false}, ` +`task_id=${commitResult.task_id ?? "none"} ${toJsonLog({ captured: [trimForLog(turnText, 260)] })}`,);} catch (err) {- warnOrInfo(logger, `openviking: auto-capture failed: ${String(err)}`);+ warnOrInfo(logger, `openviking: afterTurn failed: ${String(err)}`);}},diff --git a/examples/openclaw-plugin/index.ts b/examples/openclaw-plugin/index.tsindex 70fcd09ab..deed6528b 100644--- a/examples/openclaw-plugin/index.ts+++ b/examples/openclaw-plugin/index.ts@@ -5,7 +5,7 @@ import { Type } from "@sinclair/typebox";import { memoryOpenVikingConfigSchema } from "./config.js";import { OpenVikingClient, localClientCache, localClientPendingPromises, isMemoryUri } from "./client.js";-import type { FindResultItem, PendingClientEntry } from "./client.js";+import type { FindResultItem, PendingClientEntry, CommitSessionResult } from "./client.js";import {isTranscriptLikeIngest,extractLatestUserText,@@ -72,6 +72,12 @@ const MAX_OPENVIKING_STDERR_LINES = 200;const MAX_OPENVIKING_STDERR_CHARS = 256_000;const AUTO_RECALL_TIMEOUT_MS = 5_000;+function totalCommitMemories(r: CommitSessionResult): number {+ const m = r.memories_extracted;+ if (!m || typeof m !== "object") return 0;+ return Object.values(m).reduce((sum, n) => sum + (n ?? 0), 0);+}+const contextEnginePlugin = {id: "openviking",name: "Context Engine (OpenViking)",@@ -256,22 +262,53 @@ const contextEnginePlugin = {let sessionId = sessionIdIn;let usedMappedSession = false;- const storeAgentId = sessionKeyIn ? resolveAgentId(sessionKeyIn) : undefined;+ let usedTempSession = false;try {const c = await getClient();+ const storeAgentId = resolveAgentId(sessionKeyIn ?? sessionIdIn ?? "");if (!sessionId && sessionKeyIn && contextEngineRef) {sessionId = await contextEngineRef.resolveOVSession(sessionKeyIn);usedMappedSession = true;}if (!sessionId) {- return {- content: [{ type: "text", text: "Either sessionKey or sessionId is required to store memory." }],- details: { action: "rejected", reason: "missing_session_identifier" },- };+ sessionId = `memory-store-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;+ usedTempSession = true;}await c.addSessionMessage(sessionId, role, text, storeAgentId);const commitResult = await c.commitSession(sessionId, { wait: true, agentId: storeAgentId });- const memoriesCount = commitResult.memories_extracted ?? 0;+ const memoriesCount = totalCommitMemories(commitResult);+ if (commitResult.status === "failed") {+ api.logger.warn(+ `openviking: memory_store commit failed (sessionId=${sessionId}): ${commitResult.error ?? "unknown"}`,+ );+ return {+ content: [{ type: "text", text: `Memory extraction failed for session ${sessionId}: ${commitResult.error ?? "unknown"}` }],+ details: {+ action: "failed",+ sessionId,+ status: "failed",+ error: commitResult.error,+ usedMappedSession,+ usedTempSession,+ },+ };+ }+ if (commitResult.status === "timeout") {+ api.logger.warn(+ `openviking: memory_store commit timed out (sessionId=${sessionId}), task_id=${commitResult.task_id ?? "none"}. Memories may still be extracting in background.`,+ );+ return {+ content: [{ type: "text", text: `Memory extraction timed out for session ${sessionId}. It may still complete in the background (task_id=${commitResult.task_id ?? "none"}).` }],+ details: {+ action: "timeout",+ sessionId,+ status: "timeout",+ taskId: commitResult.task_id,+ usedMappedSession,+ usedTempSession,+ },+ };+ }if (memoriesCount === 0) {api.logger.warn(`openviking: memory_store committed but 0 memories extracted (sessionId=${sessionId}). ` +@@ -287,7 +324,15 @@ const contextEnginePlugin = {text: `Stored in OpenViking session ${sessionId} and committed ${memoriesCount} memories.`,},],- details: { action: "stored", sessionId, memoriesCount, archived: commitResult.archived ?? false, usedMappedSession },+ details: {+ action: "stored",+ sessionId,+ memoriesCount,+ status: commitResult.status,+ archived: commitResult.archived ?? false,+ usedMappedSession,+ usedTempSession,+ },};} catch (err) {api.logger.warn(`openviking: memory_store failed: ${String(err)}`);@@ -561,11 +606,11 @@ const contextEnginePlugin = {rememberSessionAgentId(ctx ?? {});});api.on("before_reset", async (_event: unknown, ctx?: HookAgentContext) => {- const sessionKey = ctx?.sessionKey;- if (sessionKey && contextEngineRef) {+ const sessionKeyOrId = ctx?.sessionKey ?? ctx?.sessionId;+ if (sessionKeyOrId && contextEngineRef) {try {- await contextEngineRef.commitOVSession(sessionKey);- api.logger.info(`openviking: committed OV session on reset for sessionKey=${sessionKey}`);+ await contextEngineRef.commitOVSession(sessionKeyOrId);+ api.logger.info(`openviking: committed OV session on reset for session=${sessionKeyOrId}`);} catch (err) {api.logger.warn(`openviking: failed to commit OV session on reset: ${String(err)}`);}@@ -589,7 +634,7 @@ const contextEnginePlugin = {return contextEngineRef;});api.logger.info(- "openviking: registered context-engine (before_prompt_build=auto-recall, afterTurn=auto-capture, sessionKey=1:1 mapping)",+ "openviking: registered context-engine (before_prompt_build=auto-recall, afterTurn=auto-capture, assemble=archive+active, sessionKey=1:1 mapping)",);} else {api.logger.warn(diff --git a/examples/openclaw-plugin/session-transcript-repair.ts b/examples/openclaw-plugin/session-transcript-repair.tsnew file mode 100644index 000000000..ec5ab6b0d--- /dev/null+++ b/examples/openclaw-plugin/session-transcript-repair.ts@@ -0,0 +1,530 @@+/**+ * Tool use/result pairing repair for assembled context.+ *+ * Copied from openclaw core (src/agents/session-transcript-repair.ts).+ * Only change: replaced `import type { AgentMessage } from "@mariozechner/pi-agent-core"`+ * with import from local tool-call-id.ts to avoid the external dependency.+ */++import type { AgentMessage } from "./tool-call-id.js";+import { extractToolCallsFromAssistant, extractToolResultId } from "./tool-call-id.js";++const TOOL_CALL_NAME_MAX_CHARS = 64;+const TOOL_CALL_NAME_RE = /^[A-Za-z0-9_-]+$/;++type RawToolCallBlock = {+ type?: unknown;+ id?: unknown;+ name?: unknown;+ input?: unknown;+ arguments?: unknown;+};++function isRawToolCallBlock(block: unknown): block is RawToolCallBlock {+ if (!block || typeof block !== "object") {+ return false;+ }+ const type = (block as { type?: unknown }).type;+ return (+ typeof type === "string" &&+ (type === "toolCall" || type === "toolUse" || type === "functionCall")+ );+}++function hasToolCallInput(block: RawToolCallBlock): boolean {+ const hasInput = "input" in block ? block.input !== undefined && block.input !== null : false;+ const hasArguments =+ "arguments" in block ? block.arguments !== undefined && block.arguments !== null : false;+ return hasInput || hasArguments;+}++function hasNonEmptyStringField(value: unknown): boolean {+ return typeof value === "string" && value.trim().length > 0;+}++function hasToolCallId(block: RawToolCallBlock): boolean {+ return hasNonEmptyStringField(block.id);+}++function normalizeAllowedToolNames(allowedToolNames?: Iterable<string>): Set<string> | null {+ if (!allowedToolNames) {+ return null;+ }+ const normalized = new Set<string>();+ for (const name of allowedToolNames) {+ if (typeof name !== "string") {+ continue;+ }+ const trimmed = name.trim();+ if (trimmed) {+ normalized.add(trimmed.toLowerCase());+ }+ }+ return normalized.size > 0 ? normalized : null;+}++function hasToolCallName(block: RawToolCallBlock, allowedToolNames: Set<string> | null): boolean {+ if (typeof block.name !== "string") {+ return false;+ }+ const trimmed = block.name.trim();+ if (!trimmed) {+ return false;+ }+ if (trimmed.length > TOOL_CALL_NAME_MAX_CHARS || !TOOL_CALL_NAME_RE.test(trimmed)) {+ return false;+ }+ if (!allowedToolNames) {+ return true;+ }+ return allowedToolNames.has(trimmed.toLowerCase());+}++function redactSessionsSpawnAttachmentsArgs(value: unknown): unknown {+ if (!value || typeof value !== "object") {+ return value;+ }+ const rec = value as Record<string, unknown>;+ const raw = rec.attachments;+ if (!Array.isArray(raw)) {+ return value;+ }+ const next = raw.map((item) => {+ if (!item || typeof item !== "object") {+ return item;+ }+ const a = item as Record<string, unknown>;+ if (!Object.hasOwn(a, "content")) {+ return item;+ }+ const { content: _content, ...rest } = a;+ return { ...rest, content: "__OPENCLAW_REDACTED__" };+ });+ return { ...rec, attachments: next };+}++function sanitizeToolCallBlock(block: RawToolCallBlock): RawToolCallBlock {+ const rawName = typeof block.name === "string" ? block.name : undefined;+ const trimmedName = rawName?.trim();+ const hasTrimmedName = typeof trimmedName === "string" && trimmedName.length > 0;+ const normalizedName = hasTrimmedName ? trimmedName : undefined;+ const nameChanged = hasTrimmedName && rawName !== trimmedName;++ const isSessionsSpawn = normalizedName?.toLowerCase() === "sessions_spawn";++ if (!isSessionsSpawn) {+ if (!nameChanged) {+ return block;+ }+ return { ...(block as Record<string, unknown>), name: normalizedName } as RawToolCallBlock;+ }++ // Redact large/sensitive inline attachment content from persisted transcripts.+ // Apply redaction to both `.arguments` and `.input` properties since block structures can vary+ const nextArgs = redactSessionsSpawnAttachmentsArgs(block.arguments);+ const nextInput = redactSessionsSpawnAttachmentsArgs(block.input);+ if (nextArgs === block.arguments && nextInput === block.input && !nameChanged) {+ return block;+ }++ const next = { ...(block as Record<string, unknown>) };+ if (nameChanged && normalizedName) {+ next.name = normalizedName;+ }+ if (nextArgs !== block.arguments || Object.hasOwn(block, "arguments")) {+ next.arguments = nextArgs;+ }+ if (nextInput !== block.input || Object.hasOwn(block, "input")) {+ next.input = nextInput;+ }+ return next as RawToolCallBlock;+}++function makeMissingToolResult(params: {+ toolCallId: string;+ toolName?: string;+}): Extract<AgentMessage, { role: "toolResult" }> {+ return {+ role: "toolResult",+ toolCallId: params.toolCallId,+ toolName: params.toolName ?? "unknown",+ content: [+ {+ type: "text",+ text: "[openclaw] missing tool result in session history; inserted synthetic error result for transcript repair.",+ },+ ],+ isError: true,+ timestamp: Date.now(),+ } as Extract<AgentMessage, { role: "toolResult" }>;+}++function trimNonEmptyString(value: unknown): string | undefined {+ if (typeof value !== "string") {+ return undefined;+ }+ const trimmed = value.trim();+ return trimmed || undefined;+}++function normalizeToolResultName(+ message: Extract<AgentMessage, { role: "toolResult" }>,+ fallbackName?: string,+): Extract<AgentMessage, { role: "toolResult" }> {+ const rawToolName = (message as { toolName?: unknown }).toolName;+ const normalizedToolName = trimNonEmptyString(rawToolName);+ if (normalizedToolName) {+ if (rawToolName === normalizedToolName) {+ return message;+ }+ return { ...message, toolName: normalizedToolName };+ }++ const normalizedFallback = trimNonEmptyString(fallbackName);+ if (normalizedFallback) {+ return { ...message, toolName: normalizedFallback };+ }++ if (typeof rawToolName === "string") {+ return { ...message, toolName: "unknown" };+ }+ return message;+}++export { makeMissingToolResult };++export type ToolCallInputRepairReport = {+ messages: AgentMessage[];+ droppedToolCalls: number;+ droppedAssistantMessages: number;+};++export type ToolCallInputRepairOptions = {+ allowedToolNames?: Iterable<string>;+};++export type ToolUseResultPairingOptions = {+ preserveErroredAssistantResults?: boolean;+};++export function stripToolResultDetails(messages: AgentMessage[]): AgentMessage[] {+ let touched = false;+ const out: AgentMessage[] = [];+ for (const msg of messages) {+ if (!msg || typeof msg !== "object" || (msg as { role?: unknown }).role !== "toolResult") {+ out.push(msg);+ continue;+ }+ if (!("details" in msg)) {+ out.push(msg);+ continue;+ }+ const sanitized = { ...(msg as object) } as { details?: unknown };+ delete sanitized.details;+ touched = true;+ out.push(sanitized as unknown as AgentMessage);+ }+ return touched ? out : messages;+}++export function repairToolCallInputs(+ messages: AgentMessage[],+ options?: ToolCallInputRepairOptions,+): ToolCallInputRepairReport {+ let droppedToolCalls = 0;+ let droppedAssistantMessages = 0;+ let changed = false;+ const out: AgentMessage[] = [];+ const allowedToolNames = normalizeAllowedToolNames(options?.allowedToolNames);++ for (const msg of messages) {+ if (!msg || typeof msg !== "object") {+ out.push(msg);+ continue;+ }++ if (msg.role !== "assistant" || !Array.isArray(msg.content)) {+ out.push(msg);+ continue;+ }++ const nextContent: typeof msg.content = [];+ let droppedInMessage = 0;+ let messageChanged = false;++ for (const block of msg.content) {+ if (+ isRawToolCallBlock(block) &&+ (!hasToolCallInput(block) ||+ !hasToolCallId(block) ||+ !hasToolCallName(block, allowedToolNames))+ ) {+ droppedToolCalls += 1;+ droppedInMessage += 1;+ changed = true;+ messageChanged = true;+ continue;+ }+ if (isRawToolCallBlock(block)) {+ if (+ (block as { type?: unknown }).type === "toolCall" ||+ (block as { type?: unknown }).type === "toolUse" ||+ (block as { type?: unknown }).type === "functionCall"+ ) {+ // Only sanitize (redact) sessions_spawn blocks; all others are passed through+ // unchanged to preserve provider-specific shapes (e.g. toolUse.input for Anthropic).+ const blockName =+ typeof (block as { name?: unknown }).name === "string"+ ? (block as { name: string }).name.trim()+ : undefined;+ if (blockName?.toLowerCase() === "sessions_spawn") {+ const sanitized = sanitizeToolCallBlock(block);+ if (sanitized !== block) {+ changed = true;+ messageChanged = true;+ }+ nextContent.push(sanitized as typeof block);+ } else {+ if (typeof (block as { name?: unknown }).name === "string") {+ const rawName = (block as { name: string }).name;+ const trimmedName = rawName.trim();+ if (rawName !== trimmedName && trimmedName) {+ const renamed = { ...(block as object), name: trimmedName } as typeof block;+ nextContent.push(renamed);+ changed = true;+ messageChanged = true;+ } else {+ nextContent.push(block);+ }+ } else {+ nextContent.push(block);+ }+ }+ continue;+ }+ } else {+ nextContent.push(block);+ }+ }++ if (droppedInMessage > 0) {+ if (nextContent.length === 0) {+ droppedAssistantMessages += 1;+ changed = true;+ continue;+ }+ out.push({ ...msg, content: nextContent });+ continue;+ }++ if (messageChanged) {+ out.push({ ...msg, content: nextContent });+ continue;+ }++ out.push(msg);+ }++ return {+ messages: changed ? out : messages,+ droppedToolCalls,+ droppedAssistantMessages,+ };+}++export function sanitizeToolCallInputs(+ messages: AgentMessage[],+ options?: ToolCallInputRepairOptions,+): AgentMessage[] {+ return repairToolCallInputs(messages, options).messages;+}++export function sanitizeToolUseResultPairing(+ messages: AgentMessage[],+ options?: ToolUseResultPairingOptions,+): AgentMessage[] {+ return repairToolUseResultPairing(messages, options).messages;+}++export type ToolUseRepairReport = {+ messages: AgentMessage[];+ added: Array<Extract<AgentMessage, { role: "toolResult" }>>;+ droppedDuplicateCount: number;+ droppedOrphanCount: number;+ moved: boolean;+};++export function repairToolUseResultPairing(+ messages: AgentMessage[],+ options?: ToolUseResultPairingOptions,+): ToolUseRepairReport {+ // Anthropic (and Cloud Code Assist) reject transcripts where assistant tool calls are not+ // immediately followed by matching tool results. Session files can end up with results+ // displaced (e.g. after user turns) or duplicated. Repair by:+ // - moving matching toolResult messages directly after their assistant toolCall turn+ // - inserting synthetic error toolResults for missing ids+ // - dropping duplicate toolResults for the same id (anywhere in the transcript)+ const out: AgentMessage[] = [];+ const added: Array<Extract<AgentMessage, { role: "toolResult" }>> = [];+ const seenToolResultIds = new Set<string>();+ let droppedDuplicateCount = 0;+ let droppedOrphanCount = 0;+ let moved = false;+ let changed = false;++ const pushToolResult = (msg: Extract<AgentMessage, { role: "toolResult" }>) => {+ const id = extractToolResultId(msg);+ if (id && seenToolResultIds.has(id)) {+ droppedDuplicateCount += 1;+ changed = true;+ return;+ }+ if (id) {+ seenToolResultIds.add(id);+ }+ out.push(msg);+ };++ for (let i = 0; i < messages.length; i += 1) {+ const msg = messages[i];+ if (!msg || typeof msg !== "object") {+ out.push(msg);+ continue;+ }++ const role = (msg as { role?: unknown }).role;+ if (role !== "assistant") {+ // Tool results must only appear directly after the matching assistant tool call turn.+ // Any "free-floating" toolResult entries in session history can make strict providers+ // (Anthropic-compatible APIs, MiniMax, Cloud Code Assist) reject the entire request.+ if (role !== "toolResult") {+ out.push(msg);+ } else {+ droppedOrphanCount += 1;+ changed = true;+ }+ continue;+ }++ const assistant = msg as Extract<AgentMessage, { role: "assistant" }>;++ const toolCalls = extractToolCallsFromAssistant(assistant);+ if (toolCalls.length === 0) {+ out.push(msg);+ continue;+ }++ const toolCallIds = new Set(toolCalls.map((t) => t.id));+ const toolCallNamesById = new Map(toolCalls.map((t) => [t.id, t.name] as const));++ const spanResultsById = new Map<string, Extract<AgentMessage, { role: "toolResult" }>>();+ const remainder: AgentMessage[] = [];++ let j = i + 1;+ for (; j < messages.length; j += 1) {+ const next = messages[j];+ if (!next || typeof next !== "object") {+ remainder.push(next);+ continue;+ }++ const nextRole = (next as { role?: unknown }).role;+ if (nextRole === "assistant") {+ break;+ }++ if (nextRole === "toolResult") {+ const toolResult = next as Extract<AgentMessage, { role: "toolResult" }>;+ const id = extractToolResultId(toolResult);+ if (id && toolCallIds.has(id)) {+ if (seenToolResultIds.has(id)) {+ droppedDuplicateCount += 1;+ changed = true;+ continue;+ }+ const normalizedToolResult = normalizeToolResultName(+ toolResult,+ toolCallNamesById.get(id),+ );+ if (normalizedToolResult !== toolResult) {+ changed = true;+ }+ if (!spanResultsById.has(id)) {+ spanResultsById.set(id, normalizedToolResult);+ }+ continue;+ }+ }++ // Drop tool results that don't match the current assistant tool calls.+ if (nextRole !== "toolResult") {+ remainder.push(next);+ } else {+ droppedOrphanCount += 1;+ changed = true;+ }+ }++ // Aborted/errored assistant turns should never synthesize missing tool results, but+ // the replay sanitizer can still legitimately retain real tool results for surviving+ // tool calls in the same turn after malformed siblings are dropped.+ const stopReason = (assistant as { stopReason?: string }).stopReason;+ if (stopReason === "error" || stopReason === "aborted") {+ out.push(msg);+ if (options?.preserveErroredAssistantResults) {+ for (const toolCall of toolCalls) {+ const result = spanResultsById.get(toolCall.id);+ if (!result) {+ continue;+ }+ pushToolResult(result);+ }+ }+ for (const rem of remainder) {+ out.push(rem);+ }+ i = j - 1;+ continue;+ }++ out.push(msg);++ if (spanResultsById.size > 0 && remainder.length > 0) {+ moved = true;+ changed = true;+ }++ for (const call of toolCalls) {+ const existing = spanResultsById.get(call.id);+ if (existing) {+ pushToolResult(existing);+ } else {+ const missing = makeMissingToolResult({+ toolCallId: call.id,+ toolName: call.name,+ });+ added.push(missing);+ changed = true;+ pushToolResult(missing);+ }+ }++ for (const rem of remainder) {+ if (!rem || typeof rem !== "object") {+ out.push(rem);+ continue;+ }+ out.push(rem);+ }+ i = j - 1;+ }++ const changedOrMoved = changed || moved;+ return {+ messages: changedOrMoved ? out : messages,+ added,+ droppedDuplicateCount,+ droppedOrphanCount,+ moved: changedOrMoved,+ };+}diff --git a/examples/openclaw-plugin/test-memory-chain.py b/examples/openclaw-plugin/test-memory-chain.pynew file mode 100644index 000000000..486281d61--- /dev/null+++ b/examples/openclaw-plugin/test-memory-chain.py@@ -0,0 +1,952 @@+#!/usr/bin/env python3+"""+OpenClaw 记忆链路完整测试脚本++验证 OpenViking 记忆插件重构后的端到端链路:+1. afterTurn: 本轮消息无损写入 OpenViking session,sessionId 一致+2. commit: 归档消息 + 提取长期记忆 + .meta.json 写入+3. assemble: 同用户继续对话时, 从 archives + active messages 重组上下文+4. assemble budget trimming: 小 token budget 下归档被裁剪+5. sessionId 一致性: 整条链路使用统一的 sessionId (无 sessionKey)+6. 新用户记忆召回: 验证 before_prompt_build auto-recall++测试流程:+Phase 1: 多轮对话 (12 轮) — afterTurn 写入+Phase 2: afterTurn 验证 — 检查 OV session 内部状态+Phase 3: Commit 验证 — 触发 commit, 检查归档结构+Phase 4: Assemble 验证 — 同用户继续对话, 验证上下文重组+Phase 5: SessionId 一致性验证+Phase 6: 新用户记忆召回++前提:+- OpenViking 服务已启动 (默认 http://127.0.0.1:8000)+- OpenClaw Gateway 已启动并配置了 OpenViking 插件++用法:+ python test-memory-chain.py+ python test-memory-chain.py --gateway http://127.0.0.1:18790 --openviking http://127.0.0.1:8000+ python test-memory-chain.py --phase chat+ python test-memory-chain.py --phase afterTurn+ python test-memory-chain.py --phase commit+ python test-memory-chain.py --phase assemble+ python test-memory-chain.py --phase session-id+ python test-memory-chain.py --phase recall+ python test-memory-chain.py --verbose++依赖:+ pip install requests rich+"""++import argparse+import json+import time+import uuid+from datetime import datetime+from typing import Any++import requests+from rich.console import Console+from rich.markdown import Markdown+from rich.panel import Panel+from rich.table import Table+from rich.tree import Tree++# ── 常量 ───────────────────────────────────────────────────────────────────++USER_ID = f"test-chain-{uuid.uuid4().hex[:8]}"+DISPLAY_NAME = "测试用户"+DEFAULT_GATEWAY = "http://127.0.0.1:18790"+DEFAULT_OPENVIKING = "http://127.0.0.1:8000"+AGENT_ID = "openclaw"++console = Console()++# ── 测试结果收集 ──────────────────────────────────────────────────────────++assertions: list[dict] = []+++def check(label: str, condition: bool, detail: str = ""):+ """记录一个断言结果。"""+ assertions.append({"label": label, "ok": condition, "detail": detail})+ icon = "[green]✓[/green]" if condition else "[red]✗[/red]"+ msg = f" {icon} {label}"+ if detail:+ msg += f" [dim]({detail})[/dim]"+ console.print(msg)+++# ── 对话数据 ──────────────────────────────────────────────────────────────++CHAT_MESSAGES = [+ "你好,我是一个软件工程师,我叫张明,在一家科技公司工作。我主要负责后端服务开发,使用的技术栈是 Python 和 Go。最近我们在重构一个订单系统,遇到了不少挑战。",+ "关于订单系统的问题,主要是性能瓶颈。我们发现在高峰期,数据库连接池经常被耗尽。目前用的是 PostgreSQL,连接池大小设置的是100,但每秒峰值请求量有5000。你有什么建议吗?",+ "谢谢你的建议。我还想问一下,我们目前的缓存策略用的是 Redis,但缓存击穿的问题很严重。热点数据过期后,大量请求直接打到数据库。我们尝试过加互斥锁,但性能下降很多。",+ "对了,关于代码风格,我们团队更倾向于使用函数式编程的思想,尽量避免副作用。变量命名用 snake_case,文档用中文写。代码审查很严格,每个 PR 至少需要两人 review。",+ "说到工作流程,我们每天早上9点站会,周三下午技术分享会。我一般上午写代码,下午处理 code review 和会议。晚上如果不加班,会看看技术书籍或者写写博客。",+ "我最近在学习分布式系统的设计,正在看《数据密集型应用系统设计》这本书。之前看完了《深入理解计算机系统》,收获很大。你有什么好的分布式系统学习资料推荐吗?",+ "目前订单系统重构的进度大概完成了60%,还剩下支付模块和库存同步模块。支付模块比较复杂,需要对接多个支付渠道。我们打算用消息队列来解耦库存同步。",+ "消息队列我们在 Kafka 和 RabbitMQ 之间犹豫。Kafka 吞吐量高,但运维复杂;RabbitMQ 功能丰富,但性能稍差。我们的消息量大概每天1000万条,你觉得选哪个好?",+ "我们团队有8个人,3个后端、2个前端、1个测试、1个运维,还有1个产品经理。后端老王经验最丰富,遇到难题都找他。测试小李很细心,bug检出率很高。",+ "对了,跟我聊天的时候注意几点:我喜欢简洁直接的回答,不要太啰嗦;技术问题最好带代码示例;如果不确定的问题要说明,不要瞎编。谢谢!",+ "补充一下,我们的监控用的是 Prometheus + Grafana,日志用 ELK Stack。最近在考虑引入链路追踪,OpenTelemetry 看起来不错,但不知道跟现有系统集成麻不麻烦。",+ "昨天线上出了个诡异的 bug,某个接口偶发超时,但日志里看不出什么问题。后来发现是下游服务的连接数满了,但监控指标没配好,没报警。这种问题怎么预防比较好?",+]++# assemble 阶段: 同用户继续对话,用于验证 assemble 是否携带了归档上下文+ASSEMBLE_FOLLOWUP_MESSAGES = [+ {+ "question": "对了,我之前提到的订单系统重构进展到哪了?支付模块开始了吗?",+ "anchor_keywords": ["订单系统", "支付模块", "60%"],+ "hook": "assemble — archives 重组",+ },+ {+ "question": "我们团队消息队列最终选了什么?之前我跟你讨论过 Kafka 和 RabbitMQ 的取舍。",+ "anchor_keywords": ["Kafka", "RabbitMQ", "消息队列"],+ "hook": "assemble — archives 重组",+ },+]++# 新用户记忆召回+RECALL_QUESTIONS = [+ {+ "question": "我是做什么工作的?用什么技术栈?请简洁回答",+ "expected_keywords": ["软件工程师", "Python", "Go"],+ },+ {+ "question": "我最近在做什么项目?遇到了什么技术挑战?请简洁回答",+ "expected_keywords": ["订单系统", "性能瓶颈", "缓存"],+ },+ {+ "question": "跟我聊天有什么注意事项?请简洁回答",+ "expected_keywords": ["简洁", "代码示例"],+ },+]+++# ── Gateway / OpenViking API ─────────────────────────────────────────────+++def send_message(gateway_url: str, message: str, user_id: str) -> dict:+ """通过 OpenClaw Responses API 发送消息。"""+ resp = requests.post(+ f"{gateway_url}/v1/responses",+ json={"model": "openclaw", "input": message, "user": user_id},+ timeout=300,+ )+ resp.raise_for_status()+ return resp.json()+++def extract_reply_text(data: dict) -> str:+ """从 Responses API 响应中提取助手回复文本。"""+ for item in data.get("output", []):+ if item.get("type") == "message" and item.get("role") == "assistant":+ for part in item.get("content", []):+ if part.get("type") in ("text", "output_text"):+ return part.get("text", "")+ return "(无回复)"+++class OpenVikingInspector:+ """OpenViking 内部状态检查器。"""++ def __init__(self, base_url: str, api_key: str = "", agent_id: str = AGENT_ID):+ self.base_url = base_url.rstrip("/")+ self.api_key = api_key+ self.agent_id = agent_id++ def _headers(self) -> dict:+ h: dict[str, str] = {"Content-Type": "application/json"}+ if self.api_key:+ h["X-API-Key"] = self.api_key+ if self.agent_id:+ h["X-OpenViking-Agent"] = self.agent_id+ return h++ def _get(self, path: str, timeout: int = 10) -> dict | None:+ try:+ resp = requests.get(f"{self.base_url}{path}", headers=self._headers(), timeout=timeout)+ if resp.status_code == 200:+ data = resp.json()+ return data.get("result", data)+ return None+ except Exception as e:+ console.print(f"[dim]GET {path} 失败: {e}[/dim]")+ return None++ def _post(self, path: str, body: dict | None = None, timeout: int = 30) -> dict | None:+ try:+ resp = requests.post(+ f"{self.base_url}{path}",+ headers=self._headers(),+ json=body or {},+ timeout=timeout,+ )+ if resp.status_code == 200:+ data = resp.json()+ return data.get("result", data)+ return None+ except Exception as e:+ console.print(f"[dim]POST {path} 失败: {e}[/dim]")+ return None++ def health_check(self) -> bool:+ try:+ resp = requests.get(f"{self.base_url}/health", timeout=5)+ return resp.status_code == 200+ except Exception:+ return False++ def get_session(self, session_id: str) -> dict | None:+ return self._get(f"/api/v1/sessions/{session_id}")++ def get_session_messages(self, session_id: str) -> list | None:+ result = self._get(f"/api/v1/sessions/{session_id}/messages")+ if isinstance(result, list):+ return result+ if isinstance(result, dict):+ return result.get("messages", [])+ return None++ def get_context_for_assemble(self, session_id: str, token_budget: int = 128000) -> dict | None:+ return self._get(+ f"/api/v1/sessions/{session_id}/context-for-assemble?token_budget={token_budget}"+ )++ def commit_session(self, session_id: str, wait: bool = True) -> dict | None:+ result = self._post(f"/api/v1/sessions/{session_id}/commit", timeout=120)+ if not result:+ return None++ if wait and result.get("task_id"):+ task_id = result["task_id"]+ deadline = time.time() + 120+ while time.time() < deadline:+ time.sleep(0.5)+ task = self._get(f"/api/v1/tasks/{task_id}")+ if not task:+ continue+ if task.get("status") == "completed":+ result["status"] = "completed"+ result["memories_extracted"] = (+ task.get("result", {}).get("memories_extracted", {})+ )+ return result+ if task.get("status") == "failed":+ result["status"] = "failed"+ result["error"] = task.get("error")+ return result++ return result++ def search_memories(+ self, query: str, target_uri: str = "viking://user/memories", limit: int = 10+ ) -> list:+ result = self._post(+ "/api/v1/search/find",+ {"query": query, "target_uri": target_uri, "limit": limit},+ )+ if isinstance(result, dict):+ return result.get("memories", [])+ return []++ def list_fs(self, uri: str) -> list:+ result = self._get(f"/api/v1/fs/ls?uri={uri}&output=original")+ return result if isinstance(result, list) else []++ def read_fs(self, uri: str) -> str | None:+ """读取 fs 中某个文件的内容。"""+ result = self._get(f"/api/v1/content/read?uri={uri}")+ if isinstance(result, str):+ return result+ if isinstance(result, dict):+ return result.get("content")+ return None+++# ── 渲染函数 ──────────────────────────────────────────────────────────────+++def render_reply(text: str, title: str = "回复"):+ lines = text.split("\n")+ if len(lines) > 25:+ text = "\n".join(lines[:25]) + f"\n\n... (共 {len(lines)} 行,已截断)"+ console.print(Panel(Markdown(text), title=f"[green]{title}[/green]", border_style="green"))+++def render_json(data: Any, title: str = "JSON"):+ console.print(+ Panel(json.dumps(data, indent=2, ensure_ascii=False, default=str)[:2000], title=title)+ )+++def render_session_info(info: dict, title: str = "Session 信息"):+ table = Table(title=title, show_header=True)+ table.add_column("属性", style="cyan")+ table.add_column("值", style="green")+ for key, value in info.items():+ if isinstance(value, (dict, list)):+ value = json.dumps(value, ensure_ascii=False)+ table.add_row(str(key), str(value)[:120])+ console.print(table)+++# ── Phase 1: 多轮对话 ────────────────────────────────────────────────────+++def run_phase_chat(gateway_url: str, user_id: str, delay: float, verbose: bool) -> tuple[int, int]:+ """Phase 1: 多轮对话 — 测试 afterTurn 写入。"""+ console.print()+ console.rule(f"[bold]Phase 1: 多轮对话 ({len(CHAT_MESSAGES)} 轮) — afterTurn 写入[/bold]")+ console.print(f"[yellow]用户ID:[/yellow] {user_id}")+ console.print(f"[yellow]Gateway:[/yellow] {gateway_url}")+ console.print()++ total = len(CHAT_MESSAGES)+ ok = fail = 0++ for i, msg in enumerate(CHAT_MESSAGES, 1):+ console.rule(f"[dim]Turn {i}/{total}[/dim]", style="dim")+ console.print(+ Panel(+ msg[:200] + ("..." if len(msg) > 200 else ""),+ title=f"[bold cyan]用户 [{i}/{total}][/bold cyan]",+ border_style="cyan",+ )+ )+ try:+ data = send_message(gateway_url, msg, user_id)+ reply = extract_reply_text(data)+ render_reply(reply[:500] + ("..." if len(reply) > 500 else ""))+ ok += 1+ except Exception as e:+ console.print(f"[red][ERROR][/red] {e}")+ fail += 1++ if i < total:+ time.sleep(delay)++ console.print()+ console.print(f"[yellow]对话完成:[/yellow] {ok} 成功, {fail} 失败")++ wait = max(delay * 2, 5)+ console.print(f"[yellow]等待 {wait:.0f}s 让 afterTurn 处理完成...[/yellow]")+ time.sleep(wait)++ return ok, fail+++# ── Phase 2: afterTurn 验证 ──────────────────────────────────────────────+++def run_phase_after_turn(openviking_url: str, user_id: str, verbose: bool) -> bool:+ """Phase 2: afterTurn 验证 — 检查 OV session 内部状态确认消息已写入。"""+ console.print()+ console.rule("[bold]Phase 2: afterTurn 验证 — 检查 OV session 消息写入[/bold]")+ console.print()+ console.print("[dim]验证点:[/dim]")+ console.print("[dim]- afterTurn 应将每轮消息写入 OV session[/dim]")+ console.print("[dim]- session.message_count > 0[/dim]")+ console.print("[dim]- pending_tokens > 0 (消息尚未 commit)[/dim]")+ console.print("[dim]- sessionId 应为 OpenClaw 传入的 user_id[/dim]")+ console.print()++ inspector = OpenVikingInspector(openviking_url)++ # 2.1 健康检查+ console.print("[bold]2.1 OpenViking 健康检查[/bold]")+ healthy = inspector.health_check()+ check("OpenViking 服务可达", healthy)+ if not healthy:+ return False++ # 2.2 Session 存在且有消息+ console.print("\n[bold]2.2 Session 存在性 & 消息计数[/bold]")+ session_info = inspector.get_session(user_id)+ check("Session 存在", session_info is not None, f"session_id={user_id}")++ if not session_info:+ console.print("[red]Session 不存在,无法继续验证[/red]")+ return False++ if verbose:+ render_session_info(session_info, f"Session: {user_id}")++ msg_count = session_info.get("message_count", 0)+ check(+ "message_count > 0 (afterTurn 写入成功)",+ msg_count > 0,+ f"message_count={msg_count}",+ )++ # pending_tokens 表示尚未 commit 的 token 数+ pending = session_info.get("pending_tokens", 0)+ check(+ "pending_tokens > 0 (有待 commit 的内容)",+ pending > 0,+ f"pending_tokens={pending}",+ )++ # 2.3 检查消息内容: 至少部分对话内容能在 OV 消息中找到+ console.print("\n[bold]2.3 消息内容抽样校验[/bold]")+ messages = inspector.get_session_messages(user_id)+ if messages is not None:+ check("能获取到 session 消息列表", True, f"共 {len(messages)} 条消息")++ # 取第一条用户消息的特征文本做匹配+ sample_text = "张明"+ all_text = json.dumps(messages, ensure_ascii=False)+ check(+ f"消息内容包含特征文本「{sample_text}」",+ sample_text in all_text,+ "验证 afterTurn 写入的内容与发送一致",+ )++ sample_text_2 = "PostgreSQL"+ check(+ f"消息内容包含特征文本「{sample_text_2}」",+ sample_text_2 in all_text,+ "验证多轮消息写入",+ )+ else:+ check("能获取到 session 消息列表", False, "GET messages 返回 None")++ # 2.4 context-for-assemble 在 commit 前应返回 messages+ console.print("\n[bold]2.4 Commit 前 context-for-assemble[/bold]")+ ctx = inspector.get_context_for_assemble(user_id)+ if ctx:+ ctx_msg_count = len(ctx.get("messages", []))+ ctx_archive_count = len(ctx.get("archives", []))+ check(+ "context-for-assemble 返回 messages > 0",+ ctx_msg_count > 0,+ f"messages={ctx_msg_count}",+ )+ check(+ "commit 前 archives == 0",+ ctx_archive_count == 0,+ f"archives={ctx_archive_count}",+ )+ if verbose and ctx.get("stats"):+ console.print(f" [dim]stats: {ctx['stats']}[/dim]")+ else:+ check("context-for-assemble 可调用", False, "返回 None")++ return True+++# ── Phase 3: Commit 验证 ─────────────────────────────────────────────────+++def run_phase_commit(openviking_url: str, user_id: str, verbose: bool) -> bool:+ """Phase 3: Commit 验证 — 触发 commit, 检查归档结构和记忆提取。"""+ console.print()+ console.rule("[bold]Phase 3: Commit 验证 — 触发 session.commit()[/bold]")+ console.print()+ console.print("[dim]验证点:[/dim]")+ console.print("[dim]- commit 返回 status=completed/accepted[/dim]")+ console.print("[dim]- 消息被归档 (archived=true)[/dim]")+ console.print("[dim]- 提取出记忆 (memories_extracted > 0)[/dim]")+ console.print("[dim]- 归档目录含 .overview.md 和 .meta.json[/dim]")+ console.print()++ inspector = OpenVikingInspector(openviking_url)++ # 3.1 执行 commit+ console.print("[bold]3.1 执行 session.commit()[/bold]")+ console.print("[dim]正在等待 commit 完成 (可能需要 1-2 分钟)...[/dim]")++ commit_result = inspector.commit_session(user_id, wait=True)+ check("commit 返回结果", commit_result is not None)++ if not commit_result:+ console.print("[red]Commit 失败,无法继续[/red]")+ return False++ if verbose:+ render_json(commit_result, "Commit 结果")++ status = commit_result.get("status", "unknown")+ check(+ "commit status 为 completed 或 accepted",+ status in ("completed", "accepted"),+ f"status={status}",+ )++ archived = commit_result.get("archived", False)+ check("archived=true (消息已归档)", archived is True, f"archived={archived}")++ memories = commit_result.get("memories_extracted", {})+ total_mem = sum(memories.values()) if memories else 0+ check(+ "memories_extracted > 0 (提取出记忆)",+ total_mem > 0,+ f"total={total_mem}, categories={memories}",+ )++ # 3.2 commit 后 session 状态+ console.print("\n[bold]3.2 Commit 后 session 状态[/bold]")+ post_session = inspector.get_session(user_id)+ if post_session:+ commit_count = post_session.get("commit_count", 0)+ check(+ "commit_count >= 1",+ commit_count >= 1,+ f"commit_count={commit_count}",+ )++ post_pending = post_session.get("pending_tokens", 0)+ # commit 后 pending_tokens 应该很低 (归档后清空了旧消息)+ console.print(f" [dim]commit 后 pending_tokens={post_pending}[/dim]")++ # 3.3 检查归档目录结构+ console.print("\n[bold]3.3 归档目录结构检查[/bold]")+ # 尝试用 context-for-assemble 来间接确认 archives 存在+ ctx_after = inspector.get_context_for_assemble(user_id)+ if ctx_after:+ archive_count = len(ctx_after.get("archives", []))+ check(+ "commit 后 context-for-assemble 返回 archives > 0",+ archive_count > 0,+ f"archives={archive_count}",+ )++ if archive_count > 0:+ first_archive = ctx_after["archives"][0]+ overview = first_archive.get("overview", "")+ check(+ "archive.overview 非空 (摘要已生成)",+ len(overview) > 10,+ f"overview 长度={len(overview)} chars",+ )+ if verbose:+ console.print(f" [dim]overview 前 200 字: {overview[:200]}...[/dim]")+ else:+ check("commit 后 context-for-assemble 可调用", False)++ # 3.4 检查 estimatedTokens 合理性+ if ctx_after:+ stats = ctx_after.get("stats", {})+ archive_tokens = stats.get("archiveTokens", 0)+ check(+ "archiveTokens > 0 (归档 token 计数合理)",+ archive_tokens > 0,+ f"archiveTokens={archive_tokens}",+ )++ return True+++# ── Phase 4: Assemble 验证 ───────────────────────────────────────────────+++def run_phase_assemble(+ gateway_url: str, openviking_url: str, user_id: str, delay: float, verbose: bool+) -> bool:+ """Phase 4: Assemble 验证 — 同用户继续对话,验证上下文从 archives 重组。"""+ console.print()+ console.rule("[bold]Phase 4: Assemble 验证 — 同用户继续对话[/bold]")+ console.print()+ console.print("[dim]验证点:[/dim]")+ console.print("[dim]- 同用户对话触发 assemble(): 从 OV archives + active messages 重组上下文[/dim]")+ console.print("[dim]- 回复应能引用 Phase 1 中已被归档的信息[/dim]")+ console.print("[dim]- context-for-assemble 应返回 archives (证明 assemble 有数据源)[/dim]")+ console.print()++ inspector = OpenVikingInspector(openviking_url)++ # 4.1 确认 assemble 的数据源 (archives) 就绪+ console.print("[bold]4.1 确认 assemble 数据源[/bold]")+ ctx = inspector.get_context_for_assemble(user_id)+ if ctx:+ archive_count = len(ctx.get("archives", []))+ check(+ "context-for-assemble 返回 archives > 0",+ archive_count > 0,+ f"archives={archive_count}, 即 assemble() 有归档可用",+ )+ else:+ check("context-for-assemble 可用", False)+ return False++ # 4.2 assemble budget trimming: 用极小 budget 验证裁剪+ console.print("\n[bold]4.2 Assemble budget trimming[/bold]")+ tiny_ctx = inspector.get_context_for_assemble(user_id, token_budget=1)+ if tiny_ctx:+ stats = tiny_ctx.get("stats", {})+ total_archives = stats.get("totalArchives", 0)+ included = stats.get("includedArchives", 0)+ dropped = stats.get("droppedArchives", 0)+ check(+ "budget=1 时 archives 被裁剪",+ included == 0 or dropped > 0,+ f"total={total_archives}, included={included}, dropped={dropped}",+ )+ active_tokens = stats.get("activeTokens", 0)+ console.print(f" [dim]activeTokens={active_tokens}, archiveTokens={stats.get('archiveTokens', 0)}[/dim]")+ else:+ check("tiny budget context-for-assemble 可调用", False)++ # 4.3 同用户继续对话 — assemble 应重组归档上下文+ console.print("\n[bold]4.3 同用户继续对话 — 验证 assemble 重组归档内容[/bold]")+ console.print(f"[yellow]用户ID:[/yellow] {user_id} (同一用户,继续对话)")+ console.print()++ total = len(ASSEMBLE_FOLLOWUP_MESSAGES)+ for i, item in enumerate(ASSEMBLE_FOLLOWUP_MESSAGES, 1):+ q = item["question"]+ keywords = item["anchor_keywords"]++ console.rule(f"[dim]Assemble 验证 {i}/{total}[/dim]", style="dim")+ console.print(+ Panel(+ f"{q}\n\n[dim]锚点关键词: {', '.join(keywords)}[/dim]\n[dim]Hook: {item['hook']}[/dim]",+ title=f"[bold cyan]Assemble Q{i}[/bold cyan]",+ border_style="cyan",+ )+ )++ try:+ data = send_message(gateway_url, q, user_id)+ reply = extract_reply_text(data)+ render_reply(reply)++ reply_lower = reply.lower()+ hits = [kw for kw in keywords if kw.lower() in reply_lower]+ hit_rate = len(hits) / len(keywords) if keywords else 0+ check(+ f"Assemble Q{i}: 回复包含归档内容 (命中率 >= 50%)",+ hit_rate >= 0.5,+ f"命中={hits}, 未命中={[k for k in keywords if k not in [h for h in hits]]}, rate={hit_rate:.0%}",+ )+ except Exception as e:+ check(f"Assemble Q{i}: 发送成功", False, str(e))++ if i < total:+ time.sleep(delay)++ # 4.4 对话后验证 afterTurn 继续写入 (新消息进入 active messages)+ console.print("\n[bold]4.4 Assemble 后 afterTurn 继续写入[/bold]")+ time.sleep(3)+ post_ctx = inspector.get_context_for_assemble(user_id)+ if post_ctx:+ post_msg_count = len(post_ctx.get("messages", []))+ check(+ "继续对话后 active messages 增加",+ post_msg_count > 0,+ f"active messages={post_msg_count}",+ )++ return True+++# ── Phase 5: SessionId 一致性验证 ────────────────────────────────────────+++def run_phase_session_id(openviking_url: str, user_id: str, verbose: bool) -> bool:+ """Phase 5: SessionId 一致性验证 — 确认整条链路使用统一的 sessionId。"""+ console.print()+ console.rule("[bold]Phase 5: SessionId 一致性验证[/bold]")+ console.print()+ console.print("[dim]验证点:[/dim]")+ console.print("[dim]- 重构后 sessionId 统一为 OpenClaw 传入的 user_id[/dim]")+ console.print("[dim]- OV session_id == user_id (无 sessionKey 前缀/后缀)[/dim]")+ console.print("[dim]- context-for-assemble 用同一 sessionId 可查到数据[/dim]")+ console.print()++ inspector = OpenVikingInspector(openviking_url)++ # 5.1 session_id 就是 user_id+ console.print("[bold]5.1 SessionId == UserId[/bold]")+ session = inspector.get_session(user_id)+ check(+ f"OV session 以 user_id={user_id} 为 ID 可查到",+ session is not None,+ "sessionId 统一: 插件直接用 user_id 作为 OV session_id",+ )++ # 5.2 不存在以 sessionKey 变体为 ID 的 session+ console.print("\n[bold]5.2 无 sessionKey 残留[/bold]")+ # 如果旧代码有 sessionKey 逻辑, 可能会创建带前缀的 session+ stale_variants = [+ f"sk:{user_id}",+ f"sessionKey:{user_id}",+ f"key:{user_id}",+ ]+ for variant in stale_variants:+ stale = inspector.get_session(variant)+ is_absent = stale is None or stale.get("message_count", 0) == 0+ check(+ f"不存在残留 session「{variant}」",+ is_absent,+ "旧 sessionKey 映射应已移除" if is_absent else f"发现残留: {stale}",+ )++ # 5.3 context-for-assemble 用 user_id 能查到归档+ console.print("\n[bold]5.3 同一 sessionId 查询归档[/bold]")+ ctx = inspector.get_context_for_assemble(user_id)+ if ctx:+ has_data = len(ctx.get("archives", [])) > 0 or len(ctx.get("messages", [])) > 0+ check(+ "context-for-assemble(user_id) 返回数据",+ has_data,+ f"archives={len(ctx.get('archives', []))}, messages={len(ctx.get('messages', []))}",+ )+ else:+ check("context-for-assemble(user_id) 可调用", False)++ # 5.4 验证 commit 也是用同一 sessionId (session 有 commit_count > 0)+ console.print("\n[bold]5.4 Commit 使用同一 sessionId[/bold]")+ if session:+ cc = session.get("commit_count", 0)+ check(+ "session(user_id) 有 commit 记录",+ cc > 0,+ f"commit_count={cc}, 说明 commit 也走 user_id 而非 sessionKey",+ )++ return True+++# ── Phase 6: 新用户记忆召回 ──────────────────────────────────────────────+++def run_phase_recall(gateway_url: str, user_id: str, delay: float, verbose: bool) -> list:+ """Phase 6: 新用户记忆召回 — 验证 before_prompt_build auto-recall。"""+ console.print()+ console.rule(+ f"[bold]Phase 6: 新用户记忆召回 ({len(RECALL_QUESTIONS)} 轮) — auto-recall[/bold]"+ )+ console.print()+ console.print("[dim]验证点:[/dim]")+ console.print("[dim]- 新用户 (新 session) 发送问题[/dim]")+ console.print("[dim]- before_prompt_build 通过 memory search 注入相关记忆[/dim]")+ console.print("[dim]- 回复应包含 Phase 1 对话中的关键信息[/dim]")+ console.print()++ verify_user = f"{user_id}-recall-{uuid.uuid4().hex[:4]}"+ console.print(f"[yellow]验证用户:[/yellow] {verify_user} (新 session)")+ console.print()++ results = []+ total = len(RECALL_QUESTIONS)++ for i, item in enumerate(RECALL_QUESTIONS, 1):+ q = item["question"]+ expected = item["expected_keywords"]++ console.rule(f"[dim]Recall {i}/{total}[/dim]", style="dim")+ console.print(+ Panel(+ f"{q}\n\n[dim]期望关键词: {', '.join(expected)}[/dim]",+ title=f"[bold cyan]Recall Q{i}[/bold cyan]",+ border_style="cyan",+ )+ )++ try:+ data = send_message(gateway_url, q, verify_user)+ reply = extract_reply_text(data)+ render_reply(reply)++ reply_lower = reply.lower()+ hits = [kw for kw in expected if kw.lower() in reply_lower]+ hit_rate = len(hits) / len(expected) if expected else 0+ success = hit_rate >= 0.5++ check(+ f"Recall Q{i}: 关键词命中率 >= 50%",+ success,+ f"命中={hits}, rate={hit_rate:.0%}",+ )+ results.append({"question": q, "hits": hits, "hit_rate": hit_rate, "success": success})+ except Exception as e:+ check(f"Recall Q{i}: 发送成功", False, str(e))+ results.append({"question": q, "hits": [], "hit_rate": 0, "success": False})++ if i < total:+ time.sleep(delay)++ return results+++# ── 完整测试 ──────────────────────────────────────────────────────────────+++def run_full_test(+ gateway_url: str, openviking_url: str, user_id: str, delay: float, verbose: bool+):+ console.print()+ console.print(+ Panel.fit(+ f"[bold]OpenClaw 记忆链路完整测试[/bold]\n\n"+ f"Gateway: {gateway_url}\n"+ f"OpenViking: {openviking_url}\n"+ f"User ID: {user_id}\n"+ f"时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",+ title="测试信息",+ )+ )++ # Phase 1: Chat+ chat_ok, chat_fail = run_phase_chat(gateway_url, user_id, delay, verbose)++ # Phase 2: afterTurn+ run_phase_after_turn(openviking_url, user_id, verbose)++ # Phase 3: Commit+ run_phase_commit(openviking_url, user_id, verbose)++ console.print("\n[yellow]等待 10s 让记忆提取完成...[/yellow]")+ time.sleep(10)++ # Phase 4: Assemble (同用户继续)+ run_phase_assemble(gateway_url, openviking_url, user_id, delay, verbose)++ # Phase 5: SessionId 一致性+ run_phase_session_id(openviking_url, user_id, verbose)++ # Phase 6: 新用户召回+ recall_results = run_phase_recall(gateway_url, user_id, delay, verbose)++ # ── 汇总报告 ──────────────────────────────────────────────────────────+ console.print()+ console.rule("[bold]测试报告[/bold]")++ passed = sum(1 for a in assertions if a["ok"])+ failed = sum(1 for a in assertions if not a["ok"])+ total = len(assertions)++ table = Table(title=f"断言结果: {passed}/{total} 通过")+ table.add_column("#", style="bold", width=4)+ table.add_column("状态", width=6)+ table.add_column("断言", max_width=60)+ table.add_column("详情", style="dim", max_width=50)++ for i, a in enumerate(assertions, 1):+ status = "[green]PASS[/green]" if a["ok"] else "[red]FAIL[/red]"+ table.add_row(str(i), status, a["label"][:60], (a.get("detail") or "")[:50])++ console.print(table)++ # 按阶段汇总+ tree = Tree(f"[bold]通过: {passed}/{total}, 失败: {failed}[/bold]")+ tree.add(f"Phase 1: 多轮对话 — {chat_ok} 成功 / {chat_fail} 失败")++ phase_names = {+ "OpenViking": "Phase 2: afterTurn",+ "message_count": "Phase 2: afterTurn",+ "pending_tokens": "Phase 2: afterTurn",+ "消息内容": "Phase 2: afterTurn",+ "commit": "Phase 3: Commit",+ "archived": "Phase 3: Commit",+ "memories_extracted": "Phase 3: Commit",+ "Assemble": "Phase 4: Assemble",+ "budget": "Phase 4: Assemble",+ "SessionId": "Phase 5: SessionId",+ "sessionKey": "Phase 5: SessionId",+ "Recall": "Phase 6: Recall",+ }++ fail_list = [a for a in assertions if not a["ok"]]+ if fail_list:+ fail_branch = tree.add(f"[red]失败断言 ({len(fail_list)})[/red]")+ for a in fail_list:+ fail_branch.add(f"[red]✗[/red] {a['label']}")++ console.print(tree)++ if failed == 0:+ console.print("\n[green bold]全部通过!端到端链路验证成功。[/green bold]")+ else:+ console.print(f"\n[red bold]有 {failed} 个断言失败,请检查上方详情。[/red bold]")+++# ── 入口 ───────────────────────────────────────────────────────────────────+++def main():+ parser = argparse.ArgumentParser(+ description="OpenClaw 记忆链路完整测试",+ formatter_class=argparse.RawDescriptionHelpFormatter,+ epilog="""+示例:+ python test-memory-chain.py+ python test-memory-chain.py --gateway http://127.0.0.1:18790+ python test-memory-chain.py --phase chat+ python test-memory-chain.py --phase afterTurn --user-id test-chain-abc123+ python test-memory-chain.py --phase assemble --user-id test-chain-abc123+ python test-memory-chain.py --verbose+ """,+ )+ parser.add_argument(+ "--gateway",+ default=DEFAULT_GATEWAY,+ help=f"OpenClaw Gateway 地址 (默认: {DEFAULT_GATEWAY})",+ )+ parser.add_argument(+ "--openviking",+ default=DEFAULT_OPENVIKING,+ help=f"OpenViking 服务地址 (默认: {DEFAULT_OPENVIKING})",+ )+ parser.add_argument(+ "--user-id",+ default=USER_ID,+ help="测试用户ID (默认: 随机生成)",+ )+ parser.add_argument(+ "--phase",+ choices=["all", "chat", "afterTurn", "commit", "assemble", "session-id", "recall"],+ default="all",+ help="运行阶段 (默认: all)",+ )+ parser.add_argument(+ "--delay",+ type=float,+ default=2.0,+ help="轮次间等待秒数 (默认: 2)",+ )+ parser.add_argument(+ "--verbose",+ "-v",+ action="store_true",+ help="详细输出",+ )+ args = parser.parse_args()++ gateway_url = args.gateway.rstrip("/")+ openviking_url = args.openviking.rstrip("/")+ user_id = args.user_id++ console.print("[bold]OpenClaw 记忆链路测试[/bold]")+ console.print(f"[yellow]Gateway:[/yellow] {gateway_url}")+ console.print(f"[yellow]OpenViking:[/yellow] {openviking_url}")+ console.print(f"[yellow]User ID:[/yellow] {user_id}")++ if args.phase == "all":+ run_full_test(gateway_url, openviking_url, user_id, args.delay, args.verbose)+ elif args.phase == "chat":+ run_phase_chat(gateway_url, user_id, args.delay, args.verbose)+ elif args.phase == "afterTurn":+ run_phase_after_turn(openviking_url, user_id, args.verbose)+ elif args.phase == "commit":+ run_phase_commit(openviking_url, user_id, args.verbose)+ elif args.phase == "assemble":+ run_phase_assemble(gateway_url, openviking_url, user_id, args.delay, args.verbose)+ elif args.phase == "session-id":+ run_phase_session_id(openviking_url, user_id, args.verbose)+ elif args.phase == "recall":+ run_phase_recall(gateway_url, user_id, args.delay, args.verbose)++ # 打印最终断言统计+ if assertions:+ passed = sum(1 for a in assertions if a["ok"])+ total = len(assertions)+ console.print(f"\n[yellow]断言统计: {passed}/{total} 通过[/yellow]")++ console.print("\n[yellow]测试结束。[/yellow]")+++if __name__ == "__main__":+ main()diff --git a/examples/openclaw-plugin/tool-call-id.ts b/examples/openclaw-plugin/tool-call-id.tsnew file mode 100644index 000000000..0b6204838--- /dev/null+++ b/examples/openclaw-plugin/tool-call-id.ts@@ -0,0 +1,331 @@+/**+ * Tool call ID utilities.+ *+ * Copied from openclaw core (src/agents/tool-call-id.ts).+ * Only change: replaced `import type { AgentMessage } from "@mariozechner/pi-agent-core"`+ * with a local type definition to avoid the external dependency.+ */++import { createHash } from "node:crypto";++// Local AgentMessage type replacing @mariozechner/pi-agent-core import.+// Discriminated union so Extract<AgentMessage, { role: "..." }> works correctly.+export type AgentMessage =+ | { role: "user"; content?: unknown }+ | { role: "assistant"; content?: unknown; stopReason?: string }+ | {+ role: "toolResult";+ content?: unknown;+ toolCallId?: string;+ toolUseId?: string;+ toolName?: string;+ isError?: boolean;+ timestamp?: number;+ };++export type ToolCallIdMode = "strict" | "strict9";++const STRICT9_LEN = 9;+const TOOL_CALL_TYPES = new Set(["toolCall", "toolUse", "functionCall"]);++export type ToolCallLike = {+ id: string;+ name?: string;+};++/**+ * Sanitize a tool call ID to be compatible with various providers.+ *+ * - "strict" mode: only [a-zA-Z0-9]+ * - "strict9" mode: only [a-zA-Z0-9], length 9 (Mistral tool call requirement)+ */+export function sanitizeToolCallId(id: string, mode: ToolCallIdMode = "strict"): string {+ if (!id || typeof id !== "string") {+ if (mode === "strict9") {+ return "defaultid";+ }+ return "defaulttoolid";+ }++ if (mode === "strict9") {+ const alphanumericOnly = id.replace(/[^a-zA-Z0-9]/g, "");+ if (alphanumericOnly.length >= STRICT9_LEN) {+ return alphanumericOnly.slice(0, STRICT9_LEN);+ }+ if (alphanumericOnly.length > 0) {+ return shortHash(alphanumericOnly, STRICT9_LEN);+ }+ return shortHash("sanitized", STRICT9_LEN);+ }++ // Some providers require strictly alphanumeric tool call IDs.+ const alphanumericOnly = id.replace(/[^a-zA-Z0-9]/g, "");+ return alphanumericOnly.length > 0 ? alphanumericOnly : "sanitizedtoolid";+}++export function extractToolCallsFromAssistant(+ msg: Extract<AgentMessage, { role: "assistant" }>,+): ToolCallLike[] {+ const content = msg.content;+ if (!Array.isArray(content)) {+ return [];+ }++ const toolCalls: ToolCallLike[] = [];+ for (const block of content) {+ if (!block || typeof block !== "object") {+ continue;+ }+ const rec = block as { type?: unknown; id?: unknown; name?: unknown };+ if (typeof rec.id !== "string" || !rec.id) {+ continue;+ }+ if (typeof rec.type === "string" && TOOL_CALL_TYPES.has(rec.type)) {+ toolCalls.push({+ id: rec.id,+ name: typeof rec.name === "string" ? rec.name : undefined,+ });+ }+ }+ return toolCalls;+}++export function extractToolResultId(+ msg: Extract<AgentMessage, { role: "toolResult" }>,+): string | null {+ const toolCallId = (msg as { toolCallId?: unknown }).toolCallId;+ if (typeof toolCallId === "string" && toolCallId) {+ return toolCallId;+ }+ const toolUseId = (msg as { toolUseId?: unknown }).toolUseId;+ if (typeof toolUseId === "string" && toolUseId) {+ return toolUseId;+ }+ return null;+}++export function isValidCloudCodeAssistToolId(id: string, mode: ToolCallIdMode = "strict"): boolean {+ if (!id || typeof id !== "string") {+ return false;+ }+ if (mode === "strict9") {+ return /^[a-zA-Z0-9]{9}$/.test(id);+ }+ // Strictly alphanumeric for providers with tighter tool ID constraints+ return /^[a-zA-Z0-9]+$/.test(id);+}++function shortHash(text: string, length = 8): string {+ return createHash("sha256").update(text).digest("hex").slice(0, length);+}++function makeUniqueToolId(params: { id: string; used: Set<string>; mode: ToolCallIdMode }): string {+ if (params.mode === "strict9") {+ const base = sanitizeToolCallId(params.id, params.mode);+ const candidate = base.length >= STRICT9_LEN ? base.slice(0, STRICT9_LEN) : "";+ if (candidate && !params.used.has(candidate)) {+ return candidate;+ }++ for (let i = 0; i < 1000; i += 1) {+ const hashed = shortHash(`${params.id}:${i}`, STRICT9_LEN);+ if (!params.used.has(hashed)) {+ return hashed;+ }+ }++ return shortHash(`${params.id}:${Date.now()}`, STRICT9_LEN);+ }++ const MAX_LEN = 40;++ const base = sanitizeToolCallId(params.id, params.mode).slice(0, MAX_LEN);+ if (!params.used.has(base)) {+ return base;+ }++ const hash = shortHash(params.id);+ // Use separator based on mode: none for strict, underscore for non-strict variants+ const separator = params.mode === "strict" ? "" : "_";+ const maxBaseLen = MAX_LEN - separator.length - hash.length;+ const clippedBase = base.length > maxBaseLen ? base.slice(0, maxBaseLen) : base;+ const candidate = `${clippedBase}${separator}${hash}`;+ if (!params.used.has(candidate)) {+ return candidate;+ }++ for (let i = 2; i < 1000; i += 1) {+ const suffix = params.mode === "strict" ? `x${i}` : `_${i}`;+ const next = `${candidate.slice(0, MAX_LEN - suffix.length)}${suffix}`;+ if (!params.used.has(next)) {+ return next;+ }+ }++ const ts = params.mode === "strict" ? `t${Date.now()}` : `_${Date.now()}`;+ return `${candidate.slice(0, MAX_LEN - ts.length)}${ts}`;+}++function createOccurrenceAwareResolver(mode: ToolCallIdMode): {+ resolveAssistantId: (id: string) => string;+ resolveToolResultId: (id: string) => string;+} {+ const used = new Set<string>();+ const assistantOccurrences = new Map<string, number>();+ const orphanToolResultOccurrences = new Map<string, number>();+ const pendingByRawId = new Map<string, string[]>();++ const allocate = (seed: string): string => {+ const next = makeUniqueToolId({ id: seed, used, mode });+ used.add(next);+ return next;+ };++ const resolveAssistantId = (id: string): string => {+ const occurrence = (assistantOccurrences.get(id) ?? 0) + 1;+ assistantOccurrences.set(id, occurrence);+ const next = allocate(occurrence === 1 ? id : `${id}:${occurrence}`);+ const pending = pendingByRawId.get(id);+ if (pending) {+ pending.push(next);+ } else {+ pendingByRawId.set(id, [next]);+ }+ return next;+ };++ const resolveToolResultId = (id: string): string => {+ const pending = pendingByRawId.get(id);+ if (pending && pending.length > 0) {+ const next = pending.shift()!;+ if (pending.length === 0) {+ pendingByRawId.delete(id);+ }+ return next;+ }++ const occurrence = (orphanToolResultOccurrences.get(id) ?? 0) + 1;+ orphanToolResultOccurrences.set(id, occurrence);+ return allocate(`${id}:tool_result:${occurrence}`);+ };++ return { resolveAssistantId, resolveToolResultId };+}++function rewriteAssistantToolCallIds(params: {+ message: Extract<AgentMessage, { role: "assistant" }>;+ resolveId: (id: string) => string;+}): Extract<AgentMessage, { role: "assistant" }> {+ const content = params.message.content;+ if (!Array.isArray(content)) {+ return params.message;+ }++ let changed = false;+ const next = content.map((block) => {+ if (!block || typeof block !== "object") {+ return block;+ }+ const rec = block as { type?: unknown; id?: unknown };+ const type = rec.type;+ const id = rec.id;+ if (+ (type !== "functionCall" && type !== "toolUse" && type !== "toolCall") ||+ typeof id !== "string" ||+ !id+ ) {+ return block;+ }+ const nextId = params.resolveId(id);+ if (nextId === id) {+ return block;+ }+ changed = true;+ return { ...(block as unknown as Record<string, unknown>), id: nextId };+ });++ if (!changed) {+ return params.message;+ }+ return { ...params.message, content: next as typeof params.message.content };+}++function rewriteToolResultIds(params: {+ message: Extract<AgentMessage, { role: "toolResult" }>;+ resolveId: (id: string) => string;+}): Extract<AgentMessage, { role: "toolResult" }> {+ const toolCallId =+ typeof params.message.toolCallId === "string" && params.message.toolCallId+ ? params.message.toolCallId+ : undefined;+ const toolUseId = (params.message as { toolUseId?: unknown }).toolUseId;+ const toolUseIdStr = typeof toolUseId === "string" && toolUseId ? toolUseId : undefined;+ const sharedRawId =+ toolCallId && toolUseIdStr && toolCallId === toolUseIdStr ? toolCallId : undefined;++ const sharedResolvedId = sharedRawId ? params.resolveId(sharedRawId) : undefined;+ const nextToolCallId =+ sharedResolvedId ?? (toolCallId ? params.resolveId(toolCallId) : undefined);+ const nextToolUseId =+ sharedResolvedId ?? (toolUseIdStr ? params.resolveId(toolUseIdStr) : undefined);++ if (nextToolCallId === toolCallId && nextToolUseId === toolUseIdStr) {+ return params.message;+ }++ return {+ ...params.message,+ ...(nextToolCallId && { toolCallId: nextToolCallId }),+ ...(nextToolUseId && { toolUseId: nextToolUseId }),+ } as Extract<AgentMessage, { role: "toolResult" }>;+}++/**+ * Sanitize tool call IDs for provider compatibility.+ *+ * @param messages - The messages to sanitize+ * @param mode - "strict" (alphanumeric only) or "strict9" (alphanumeric length 9)+ */+export function sanitizeToolCallIdsForCloudCodeAssist(+ messages: AgentMessage[],+ mode: ToolCallIdMode = "strict",+): AgentMessage[] {+ // Strict mode: only [a-zA-Z0-9]+ // Strict9 mode: only [a-zA-Z0-9], length 9 (Mistral tool call requirement)+ // Sanitization can introduce collisions, and some providers also reject raw+ // duplicate tool-call IDs. Track assistant occurrences in-order so repeated+ // raw IDs receive distinct rewritten IDs, while matching tool results consume+ // the same rewritten IDs in encounter order.+ const { resolveAssistantId, resolveToolResultId } = createOccurrenceAwareResolver(mode);++ let changed = false;+ const out = messages.map((msg) => {+ if (!msg || typeof msg !== "object") {+ return msg;+ }+ const role = (msg as { role?: unknown }).role;+ if (role === "assistant") {+ const next = rewriteAssistantToolCallIds({+ message: msg as Extract<AgentMessage, { role: "assistant" }>,+ resolveId: resolveAssistantId,+ });+ if (next !== msg) {+ changed = true;+ }+ return next;+ }+ if (role === "toolResult") {+ const next = rewriteToolResultIds({+ message: msg as Extract<AgentMessage, { role: "toolResult" }>,+ resolveId: resolveToolResultId,+ });+ if (next !== msg) {+ changed = true;+ }+ return next;+ }+ return msg;+ });++ return changed ? out : messages;+}diff --git a/openviking/message/message.py b/openviking/message/message.pyindex c40808527..fdf185e07 100644--- a/openviking/message/message.py+++ b/openviking/message/message.py@@ -31,6 +31,37 @@ class Message:return p.textreturn ""+ @property+ def estimated_tokens(self) -> int:+ """Estimate token count from all parts (ceil(len/4) heuristic).++ Counts fields that actually appear in the assembled prompt:+ - TextPart.text: always emitted+ - ContextPart.abstract: injected as text (uri is not sent to the model)+ - ToolPart: tool_id (appears in toolUse.id / toolResult.toolCallId),+ tool_name, tool_input (JSON), tool_output++ Known limitation: ToolPart estimation undercounts by ~10-20 tokens per+ tool call because tool_id/toolName appear twice in the assembled transcript+ (toolUse + toolResult), and small literals like "(no output)" / "{}" are+ not counted. Under 128k budgets this is negligible; for smaller budgets+ (8k/16k) or tool-dense sessions, consider adding a conservative per-tool+ buffer instead of mirroring the full convertToAgentMessages logic.+ """+ total_chars = 0+ for p in self.parts:+ if isinstance(p, TextPart):+ total_chars += len(p.text)+ elif isinstance(p, ContextPart):+ total_chars += len(p.abstract)+ elif isinstance(p, ToolPart):+ total_chars += len(p.tool_id) + len(p.tool_name)+ if p.tool_input:+ total_chars += len(json.dumps(p.tool_input, ensure_ascii=False))+ if p.tool_output:+ total_chars += len(p.tool_output)+ return -(-total_chars // 4) # ceil division+def to_dict(self) -> dict:"""Serialize to JSONL."""created_at_val = self.created_at or datetime.now(timezone.utc)diff --git a/openviking/server/routers/sessions.py b/openviking/server/routers/sessions.pyindex de94d2bf2..040a2a51e 100644--- a/openviking/server/routers/sessions.py+++ b/openviking/server/routers/sessions.py@@ -138,6 +138,22 @@ async def get_session()result = session.meta.to_dict()result["user"] = session.user.to_dict()+ pending_tokens = sum(len(m.content) // 4 for m in session.messages)+ result["pending_tokens"] = pending_tokens+ return Response(status="ok", result=result)++[email protected]("/{session_id}/context-for-assemble")+async def get_context_for_assemble(+ session_id: str = Path(..., description="Session ID"),+ token_budget: int = Query(128_000, description="Token budget for context assembly"),+ _ctx: RequestContext = Depends(get_request_context),+):+ """Get trimmed context (archive summaries + active messages) for assemble."""+ service = get_service()+ session = service.sessions.session(_ctx, session_id)+ await session.load()+ result = await session.get_context_for_assemble(token_budget)return Response(status="ok", result=result)diff --git a/openviking/session/__init__.py b/openviking/session/__init__.pyindex 13eefae7b..ae0973e21 100644--- a/openviking/session/__init__.py+++ b/openviking/session/__init__.py@@ -4,10 +4,6 @@from typing import Optional-from openviking.storage import VikingDBManager-from openviking_cli.utils import get_logger-from openviking_cli.utils.config import get_openviking_config-from openviking.session.compressor import ExtractionStats, SessionCompressorfrom openviking.session.memory_archiver import (ArchivalCandidate,@@ -28,6 +24,9 @@ from openviking.session.memory_extractor import (ToolSkillCandidateMemory,)from openviking.session.session import Session, SessionCompression, SessionMeta, SessionStats+from openviking.storage import VikingDBManager+from openviking_cli.utils import get_logger+from openviking_cli.utils.config import get_openviking_configlogger = get_logger(__name__)@@ -60,6 +59,7 @@ def create_session_compressor(logger.info("Using v2 memory compressor (templating system)")try:from openviking.session.compressor_v2 import SessionCompressorV2+return SessionCompressorV2(vikingdb=vikingdb)except Exception as e:logger.warning(f"Failed to load v2 compressor, falling back to v1: {e}")diff --git a/openviking/session/session.py b/openviking/session/session.pyindex f62998c99..dcc4c2b8d 100644--- a/openviking/session/session.py+++ b/openviking/session/session.py@@ -301,7 +301,7 @@ class Session:# Update statisticsif role == "user":self._stats.total_turns += 1- self._stats.total_tokens += len(msg.content) // 4+ self._stats.total_tokens += msg.estimated_tokensself._append_to_jsonl(msg)@@ -500,6 +500,16 @@ class Session:content=summary,ctx=self.ctx,)+ await self._viking_fs.write_file(+ uri=f"{archive_uri}/.meta.json",+ content=json.dumps(+ {+ "overview_tokens": -(-len(summary) // 4),+ "abstract_tokens": -(-len(abstract) // 4),+ }+ ),+ ctx=self.ctx,+ )# Memory extractionif self._session_compressor:@@ -655,6 +665,147 @@ class Session:"current_messages": current_messages,}+ async def get_context_for_assemble(self, token_budget: int = 128_000) -> Dict[str, Any]:+ """Get context for assemble: trimmed archive summaries + all active messages.++ Performs token-budget-aware trimming server-side so the caller+ (OpenClaw plugin assemble()) doesn't need to do budget math.++ Archives are read lazily from newest to oldest. When a .meta.json+ exists (written by commit), only its token count is checked before+ deciding whether to read the full overview/abstract — once the+ remaining budget is exhausted, older archives are skipped entirely+ (no I/O). Old archives without .meta.json fall back to reading+ the overview to calculate tokens on the spot.++ Args:+ token_budget: Token budget (default 128k)++ Returns:+ {+ "archives": [{"index": N, "overview": "...", "abstract": "..."}],+ "messages": [Message.to_dict(), ...],+ "estimatedTokens": int,+ "stats": {+ "totalArchives": int,+ "includedArchives": int,+ "droppedArchives": int,+ "activeTokens": int,+ "archiveTokens": int,+ }+ }+ """+ active_messages = list(self._messages) if self._messages else []+ active_tokens = sum(m.estimated_tokens for m in active_messages)++ included: List[Dict[str, Any]] = []+ discovered_archives = 0+ failed_archives = 0+ budget_used = 0+ remaining_budget = max(0, token_budget - active_tokens)++ if self.compression.compression_index > 0:+ try:+ history_items = await self._viking_fs.ls(+ f"{self._session_uri}/history", ctx=self.ctx+ )+ archive_entries: List[tuple] = []+ for item in history_items:+ name = item.get("name") if isinstance(item, dict) else item+ if not (name and name.startswith("archive_")):+ continue+ try:+ archive_num = int(name.split("_")[1])+ except (IndexError, ValueError):+ continue+ archive_entries.append((archive_num, name))++ archive_entries.sort(key=lambda e: e[0], reverse=True)+ discovered_archives = len(archive_entries)++ for archive_num, name in archive_entries:+ base_uri = f"{self._session_uri}/history/{name}"++ overview_tokens = None+ overview = None+ try:+ meta_content = await self._viking_fs.read_file(+ f"{base_uri}/.meta.json", ctx=self.ctx+ )+ overview_tokens = json.loads(meta_content).get("overview_tokens", 0)+ except Exception:+ pass++ if overview_tokens is None:+ try:+ overview = await self._viking_fs.read_file(+ f"{base_uri}/.overview.md", ctx=self.ctx+ )+ overview_tokens = -(-len(overview) // 4)+ except Exception:+ failed_archives += 1+ logger.warning(+ f"Failed to read overview for {name} in session {self.session_id}"+ )+ continue++ if budget_used + overview_tokens > remaining_budget:+ break++ if overview is None:+ try:+ overview = await self._viking_fs.read_file(+ f"{base_uri}/.overview.md", ctx=self.ctx+ )+ except Exception:+ failed_archives += 1+ logger.warning(+ f"Failed to read overview for {name} in session {self.session_id}"+ )+ continue++ if not overview:+ continue++ abstract = ""+ try:+ abstract = await self._viking_fs.read_file(+ f"{base_uri}/.abstract.md", ctx=self.ctx+ )+ except Exception:+ pass++ included.append(+ {+ "index": archive_num,+ "overview": overview,+ "abstract": abstract,+ }+ )+ budget_used += overview_tokens+ except Exception:+ logger.warning(f"Failed to list history directory for session {self.session_id}")+ discovered_archives = -1++ included.reverse()++ return {+ "archives": [+ {"index": a["index"], "overview": a["overview"], "abstract": a["abstract"]}+ for a in included+ ],+ "messages": [m.to_dict() for m in active_messages],+ "estimatedTokens": active_tokens + budget_used,+ "stats": {+ "totalArchives": discovered_archives,+ "includedArchives": len(included),+ "droppedArchives": discovered_archives - len(included) - failed_archives,+ "failedArchives": failed_archives,+ "activeTokens": active_tokens,+ "archiveTokens": budget_used,+ },+ }+# ============= Internal methods =============async def _get_latest_completed_archive_overview(diff --git a/tests/server/test_api_sessions.py b/tests/server/test_api_sessions.pyindex 9fca12532..c96b8de13 100644--- a/tests/server/test_api_sessions.py+++ b/tests/server/test_api_sessions.py@@ -3,10 +3,52 @@"""Tests for session endpoints."""+import json+from unittest.mock import patch+import httpx+import pytestfrom openviking.server.identity import RequestContext, Rolefrom openviking_cli.session.user_id import UserIdentifier+from openviking_cli.utils.config.open_viking_config import OpenVikingConfigSingleton+from tests.utils.mock_agfs import MockLocalAGFS++[email protected](autouse=True)+def _configure_test_env(monkeypatch, tmp_path):+ config_path = tmp_path / "ov.conf"+ config_path.write_text(+ json.dumps(+ {+ "storage": {+ "workspace": str(tmp_path / "workspace"),+ "agfs": {"backend": "local", "mode": "binding-client"},+ "vectordb": {"backend": "local"},+ },+ "embedding": {+ "dense": {+ "provider": "openai",+ "model": "test-embedder",+ "api_base": "http://127.0.0.1:11434/v1",+ "dimension": 1024,+ }+ },+ "encryption": {"enabled": False},+ }+ ),+ encoding="utf-8",+ )++ mock_agfs = MockLocalAGFS(root_path=tmp_path / "mock_agfs_root")++ monkeypatch.setenv("OPENVIKING_CONFIG_FILE", str(config_path))+ OpenVikingConfigSingleton.reset_instance()++ with patch("openviking.utils.agfs_utils.create_agfs_client", return_value=mock_agfs):+ yield++ OpenVikingConfigSingleton.reset_instance()async def test_create_session(client: httpx.AsyncClient):@@ -175,3 +217,50 @@ async def test_extract_session_jsonable_regression(client: httpx.AsyncClient, sebody = resp.json()assert body["status"] == "ok"assert body["result"] == [{"uri": "viking://user/memories/mock.md"}]+++async def test_get_context_for_assemble_endpoint_returns_trimmed_context(client: httpx.AsyncClient):+ create_resp = await client.post("/api/v1/sessions", json={})+ session_id = create_resp.json()["result"]["session_id"]++ await client.post(+ f"/api/v1/sessions/{session_id}/messages",+ json={"role": "user", "content": "archived message"},+ )+ await client.post(f"/api/v1/sessions/{session_id}/commit")++ await client.post(+ f"/api/v1/sessions/{session_id}/messages",+ json={+ "role": "assistant",+ "parts": [+ {"type": "text", "text": "Running tool"},+ {+ "type": "tool",+ "tool_id": "tool_123",+ "tool_name": "demo_tool",+ "tool_uri": f"viking://session/{session_id}/tools/tool_123",+ "tool_input": {"x": 1},+ "tool_status": "running",+ },+ ],+ },+ )++ resp = await client.get(f"/api/v1/sessions/{session_id}/context-for-assemble?token_budget=1")+ assert resp.status_code == 200+ body = resp.json()+ assert body["status"] == "ok"++ result = body["result"]+ assert result["archives"] == []+ assert len(result["messages"]) == 1+ assert result["messages"][0]["role"] == "assistant"+ assert any(+ part["type"] == "tool" and part["tool_id"] == "tool_123"+ for part in result["messages"][0]["parts"]+ )+ assert result["stats"]["totalArchives"] == 1+ assert result["stats"]["includedArchives"] == 0+ assert result["stats"]["droppedArchives"] == 1+ assert result["stats"]["failedArchives"] == 0diff --git a/tests/session/test_session_context.py b/tests/session/test_session_context.pyindex 8eeacd6f9..d92e9f87b 100644--- a/tests/session/test_session_context.py+++ b/tests/session/test_session_context.py@@ -4,11 +4,100 @@"""Context retrieval tests"""import asyncio+import json+from unittest.mock import patch++import pytest_asynciofrom openviking import AsyncOpenVikingfrom openviking.message import TextPart+from openviking.models.embedder.base import DenseEmbedderBase, EmbedResultfrom openviking.service.task_tracker import get_task_trackerfrom openviking.session import Session+from openviking_cli.utils.config.embedding_config import EmbeddingConfig+from openviking_cli.utils.config.open_viking_config import OpenVikingConfigSingleton+from openviking_cli.utils.config.vlm_config import VLMConfig+from tests.utils.mock_agfs import MockLocalAGFS+++def _install_fake_embedder(monkeypatch):+ class FakeEmbedder(DenseEmbedderBase):+ def __init__(self):+ super().__init__(model_name="test-fake-embedder")++ def embed(self, text: str, is_query: bool = False) -> EmbedResult:+ return EmbedResult(dense_vector=[0.1] * 1024)++ def embed_batch(self, texts: list[str], is_query: bool = False) -> list[EmbedResult]:+ return [self.embed(text, is_query=is_query) for text in texts]++ def get_dimension(self) -> int:+ return 1024++ monkeypatch.setattr(EmbeddingConfig, "get_embedder", lambda self: FakeEmbedder())+++def _install_fake_vlm(monkeypatch):+ async def _fake_get_completion(self, prompt, thinking=False, max_retries=0):+ return "# Test Summary\n\nFake summary for testing.\n\n## Details\nTest content."++ async def _fake_get_vision_completion(self, prompt, images, thinking=False):+ return "Fake image description for testing."++ monkeypatch.setattr(VLMConfig, "is_available", lambda self: True)+ monkeypatch.setattr(VLMConfig, "get_completion_async", _fake_get_completion)+ monkeypatch.setattr(VLMConfig, "get_vision_completion_async", _fake_get_vision_completion)+++def _write_test_config(tmp_path):+ config_path = tmp_path / "ov.conf"+ config_path.write_text(+ json.dumps(+ {+ "storage": {+ "workspace": str(tmp_path / "workspace"),+ "agfs": {"backend": "local", "mode": "binding-client"},+ "vectordb": {"backend": "local"},+ },+ "embedding": {+ "dense": {+ "provider": "openai",+ "model": "test-embedder",+ "api_base": "http://127.0.0.1:11434/v1",+ "dimension": 1024,+ }+ },+ "encryption": {"enabled": False},+ }+ ),+ encoding="utf-8",+ )+ return config_path+++@pytest_asyncio.fixture(scope="function")+async def client(test_data_dir, monkeypatch, tmp_path):+ config_path = _write_test_config(tmp_path)+ mock_agfs = MockLocalAGFS(root_path=tmp_path / "mock_agfs_root")++ OpenVikingConfigSingleton.reset_instance()+ await AsyncOpenViking.reset()+ monkeypatch.setenv("OPENVIKING_CONFIG_FILE", str(config_path))+ _install_fake_embedder(monkeypatch)+ _install_fake_vlm(monkeypatch)++ with patch("openviking.utils.agfs_utils.create_agfs_client", return_value=mock_agfs):+ client = AsyncOpenViking(path=str(test_data_dir))+ await client.initialize()+ yield client+ await client.close()++ OpenVikingConfigSingleton.reset_instance()+ await AsyncOpenViking.reset()+++def _estimate_tokens(text: str) -> int:+ return -(-len(text) // 4)async def _wait_for_task(task_id: str, timeout: float = 30.0) -> dict:@@ -100,20 +189,178 @@ class TestGetContextForSearch:"""Test getting context after commit"""session = client.session(session_id="post_commit_context_test")- # Add messagessession.add_message("user", [TextPart("Test message before commit")])session.add_message("assistant", [TextPart("Response before commit")])- # Commitresult = await session.commit_async()await _wait_for_task(result["task_id"])- # Add new messagessession.add_message("user", [TextPart("New message after commit")])- # Getting context should include archive summarycontext = await session.get_context_for_search(query="test")assert isinstance(context, dict)assert context["latest_archive_overview"]assert len(context["current_messages"]) == 1+++class TestGetContextForAssemble:+ """Test get_context_for_assemble"""++ async def test_get_context_for_assemble_trims_archives_from_newest(+ self, client: AsyncOpenViking, monkeypatch+ ):+ session = client.session(session_id="assemble_trim_test")+ summaries = [+ "# Session Summary\n\n" + ("A" * 80),+ "# Session Summary\n\n" + ("B" * 20),+ ]++ async def fake_generate(_messages, latest_archive_overview=""):+ del latest_archive_overview+ return summaries.pop(0)++ monkeypatch.setattr(session, "_generate_archive_summary_async", fake_generate)++ session.add_message("user", [TextPart("first turn")])+ session.add_message("assistant", [TextPart("first reply")])+ result = await session.commit_async()+ await _wait_for_task(result["task_id"])++ session.add_message("user", [TextPart("second turn")])+ session.add_message("assistant", [TextPart("second reply")])+ result = await session.commit_async()+ await _wait_for_task(result["task_id"])++ session.add_message("user", [TextPart("active tail")])++ newest_summary = "# Session Summary\n\n" + ("B" * 20)+ active_tokens = sum(message.estimated_tokens for message in session.messages)+ token_budget = active_tokens + _estimate_tokens(newest_summary)++ context = await session.get_context_for_assemble(token_budget=token_budget)++ assert [archive["index"] for archive in context["archives"]] == [2]+ assert len(context["messages"]) == 1+ assert context["messages"][0]["parts"][0]["text"] == "active tail"+ assert context["estimatedTokens"] == token_budget+ assert context["stats"] == {+ "totalArchives": 2,+ "includedArchives": 1,+ "droppedArchives": 1,+ "failedArchives": 0,+ "activeTokens": active_tokens,+ "archiveTokens": _estimate_tokens(newest_summary),+ }++ async def test_get_context_for_assemble_counts_active_tool_parts(+ self, session_with_tool_call: tuple[Session, str, str]+ ):+ session, _message_id, tool_id = session_with_tool_call++ context = await session.get_context_for_assemble()++ assert len(context["messages"]) == 1+ tool_parts = [part for part in context["messages"][0]["parts"] if part["type"] == "tool"]+ assert tool_parts[0]["tool_id"] == tool_id+ assert context["stats"]["activeTokens"] == session.messages[0].estimated_tokens+ assert context["stats"]["activeTokens"] > _estimate_tokens("Executing tool...")++ async def test_get_context_for_assemble_skips_old_archives_on_budget(+ self, client: AsyncOpenViking, monkeypatch+ ):+ """When budget only fits the newest archive, older archives' overview/abstract+ should never be read (lazy-read optimisation)."""+ session = client.session(session_id="assemble_lazy_read_test")+ summaries = [+ "# Summary\n\n" + ("A" * 80),+ "# Summary\n\n" + ("B" * 80),+ "# Summary\n\n" + ("C" * 80),+ ]++ async def fake_generate(_messages, latest_archive_overview=""):+ del latest_archive_overview+ return summaries.pop(0)++ monkeypatch.setattr(session, "_generate_archive_summary_async", fake_generate)++ for word in ("first", "second", "third"):+ session.add_message("user", [TextPart(f"{word} turn")])+ session.add_message("assistant", [TextPart(f"{word} reply")])+ result = await session.commit_async()+ await _wait_for_task(result["task_id"])++ session.add_message("user", [TextPart("active tail")])++ newest_summary = "# Summary\n\n" + ("C" * 80)+ active_tokens = sum(m.estimated_tokens for m in session.messages)+ token_budget = active_tokens + _estimate_tokens(newest_summary)++ original_read_file = session._viking_fs.read_file+ read_uris: list[str] = []++ async def tracking_read_file(*args, **kwargs):+ uri = args[0] if args else kwargs.get("uri")+ read_uris.append(uri)+ return await original_read_file(*args, **kwargs)++ monkeypatch.setattr(session._viking_fs, "read_file", tracking_read_file)++ context = await session.get_context_for_assemble(token_budget=token_budget)++ assert [a["index"] for a in context["archives"]] == [3]+ assert context["stats"]["includedArchives"] == 1+ assert context["stats"]["droppedArchives"] == 2++ overview_reads = [u for u in read_uris if u.endswith(".overview.md")]+ abstract_reads = [u for u in read_uris if u.endswith(".abstract.md")]+ assert all("archive_003" in u for u in overview_reads), (+ f"Only newest archive overview should be read, got: {overview_reads}"+ )+ assert all("archive_003" in u for u in abstract_reads), (+ f"Only newest archive abstract should be read, got: {abstract_reads}"+ )+ assert not any("archive_001" in u for u in read_uris if not u.endswith(".meta.json")), (+ "Oldest archive should not be read at all (budget exhausted before reaching it)"+ )++ async def test_get_context_for_assemble_tracks_failed_archives(+ self, client: AsyncOpenViking, monkeypatch+ ):+ session = client.session(session_id="assemble_failed_archive_test")+ summaries = [+ "# Session Summary\n\narchive one",+ "# Session Summary\n\narchive two",+ ]++ async def fake_generate(_messages, latest_archive_overview=""):+ del latest_archive_overview+ return summaries.pop(0)++ monkeypatch.setattr(session, "_generate_archive_summary_async", fake_generate)++ session.add_message("user", [TextPart("turn one")])+ result = await session.commit_async()+ await _wait_for_task(result["task_id"])++ session.add_message("user", [TextPart("turn two")])+ result = await session.commit_async()+ await _wait_for_task(result["task_id"])++ original_read_file = session._viking_fs.read_file++ async def flaky_read_file(*args, **kwargs):+ uri = args[0] if args else kwargs.get("uri")+ if isinstance(uri, str) and uri.endswith("archive_002/.overview.md"):+ raise RuntimeError("simulated archive read failure")+ return await original_read_file(*args, **kwargs)++ monkeypatch.setattr(session._viking_fs, "read_file", flaky_read_file)++ context = await session.get_context_for_assemble(token_budget=128_000)++ assert [archive["index"] for archive in context["archives"]] == [1]+ assert context["stats"]["totalArchives"] == 2+ assert context["stats"]["includedArchives"] == 1+ assert context["stats"]["droppedArchives"] == 0+ assert context["stats"]["failedArchives"] == 1