Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(codex): capture nested tool activity (#5026)

t0sakida2ff3588c96
diff --git a/examples/codex-memory-plugin/.mcp.json b/examples/codex-memory-plugin/.mcp.json
index fd5668560..a08b1127a 100644
--- a/examples/codex-memory-plugin/.mcp.json
+++ b/examples/codex-memory-plugin/.mcp.json
@@ -6,7 +6,26 @@
"servers/mcp-proxy.mjs"
],
"cwd": ".",
- "startup_timeout_sec": 30
+ "startup_timeout_sec": 30,
+ "env_vars": [
+ "OPENVIKING_CLI_CONFIG_FILE",
+ "OPENVIKING_CONFIG_FILE",
+ "OPENVIKING_HOME",
+ "OPENVIKING_URL",
+ "OPENVIKING_BASE_URL",
+ "OPENVIKING_MCP_URL",
+ "OPENVIKING_API_KEY",
+ "OPENVIKING_BEARER_TOKEN",
+ "OPENVIKING_ACCOUNT",
+ "OPENVIKING_USER",
+ "OPENVIKING_PEER_ID",
+ "OPENVIKING_CREDENTIAL_SOURCE",
+ "OPENVIKING_CREDENTIALS_SOURCE",
+ "OPENVIKING_RECALL_PEER_SCOPE",
+ "OPENVIKING_TIMEOUT_MS",
+ "OPENVIKING_DEBUG",
+ "OPENVIKING_DEBUG_LOG"
+ ]
}
}
}
diff --git a/examples/codex-memory-plugin/scripts/capture-utils.mjs b/examples/codex-memory-plugin/scripts/capture-utils.mjs
index 6fcd5f747..3eff567cb 100644
--- a/examples/codex-memory-plugin/scripts/capture-utils.mjs
+++ b/examples/codex-memory-plugin/scripts/capture-utils.mjs
@@ -5,42 +5,230 @@ import {
export * from "./shared/capture-utils.mjs";
function mcpResultText(result) {
- const ok = result?.Ok;
- if (Array.isArray(ok?.content)) {
- const text = ok.content
+ let value = result;
+ if (Object.prototype.hasOwnProperty.call(result || {}, "Ok")) value = result.Ok;
+ else if (Object.prototype.hasOwnProperty.call(result || {}, "Err")) value = result.Err;
+ if (Array.isArray(value?.content)) {
+ const text = value.content
.filter((part) => part?.type === "text" && typeof part.text === "string")
.map((part) => part.text)
.join("\n");
if (text) return text;
}
- if (typeof ok === "string") return ok;
- if (ok != null) return JSON.stringify(ok);
- const error = result?.Err;
- if (typeof error === "string") return error;
- return error == null ? "" : JSON.stringify(error);
-}
-
-function deduplicateMcpFunctionEvents(rolloutEntries) {
- const mcpCallIds = new Set(
- (rolloutEntries || [])
- .filter((entry) => entry?.payload?.type === "mcp_tool_call_end")
- .map((entry) => entry.payload.call_id)
- .filter(Boolean),
+ if (typeof value === "string") return value;
+ return value == null ? "" : JSON.stringify(value);
+}
+
+const RESPONSE_TOOL_TYPES = new Set([
+ "function_call",
+ "function_call_output",
+ "custom_tool_call",
+ "custom_tool_call_output",
+]);
+const COMPLETED_TURN_TOOL_TYPES = new Set([
+ "CommandExecution",
+ "DynamicToolCall",
+ "FileChange",
+ "McpToolCall",
+]);
+
+const FAILED_TOOL_STATUSES = new Set(["failed", "error", "declined"]);
+const TERMINAL_TOOL_STATUSES = new Set(["completed", ...FAILED_TOOL_STATUSES]);
+
+function inheritedEntry(entry, type, payload) {
+ const turnId = entry?.payload?.turn_id;
+ return {
+ ...entry,
+ type,
+ payload: turnId != null && payload?.turn_id == null
+ ? { ...payload, turn_id: turnId }
+ : payload,
+ };
+}
+
+function expandCodexRolloutEntries(rolloutEntries) {
+ return (rolloutEntries || []).flatMap((entry) => {
+ const payload = entry?.payload;
+ if (entry?.type === "history_mutation" && payload?.operation === "append") {
+ if (!Array.isArray(payload.items)) return [entry];
+ return payload.items.map((item) => inheritedEntry(entry, "response_item", item));
+ }
+ if (
+ entry?.type === "event_msg"
+ && payload?.type === "item_completed"
+ && COMPLETED_TURN_TOOL_TYPES.has(payload.item?.type)
+ ) {
+ return [inheritedEntry(entry, "turn_item", payload.item)];
+ }
+ return [entry];
+ });
+}
+
+function compactObject(value) {
+ return Object.fromEntries(
+ Object.entries(value).filter(([, item]) => item !== undefined && item !== null),
);
- if (mcpCallIds.size === 0) return rolloutEntries || [];
-
- const duplicatedTypes = new Set([
- "function_call",
- "function_call_output",
- "custom_tool_call",
- "custom_tool_call_output",
- ]);
- return (rolloutEntries || []).filter((entry) => {
+}
+
+function joinedOutput(payload) {
+ return [payload?.stdout, payload?.stderr].filter(Boolean).join("\n");
+}
+
+function isFailed(payload) {
+ return payload?.success === false
+ || (typeof payload?.exit_code === "number" && payload.exit_code !== 0)
+ || FAILED_TOOL_STATUSES.has(String(payload?.status || "").toLowerCase())
+ || Boolean(payload?.error);
+}
+
+function completedToolEvent(entry) {
+ const payload = entry?.payload;
+ if (payload?.type === "mcp_tool_call_end") {
+ const hasResult = Object.prototype.hasOwnProperty.call(payload.result || {}, "Ok");
+ const failed = !hasResult || payload.result.Ok?.isError === true;
+ return {
+ kind: "mcp",
+ id: payload.call_id,
+ name: payload.invocation?.tool,
+ input: payload.invocation?.arguments ?? {},
+ output: mcpResultText(payload.result),
+ failed,
+ };
+ }
+ if (payload?.type === "McpToolCall") {
+ return {
+ kind: "mcp",
+ id: payload.id,
+ name: payload.tool,
+ input: payload.arguments ?? {},
+ output: mcpResultText(payload.error ?? payload.result),
+ failed: isFailed(payload) || payload.result?.isError === true,
+ };
+ }
+ if (payload?.type === "exec_command_end" || payload?.type === "CommandExecution") {
+ return {
+ kind: "command",
+ id: payload.call_id || payload.id,
+ name: "exec_command",
+ input: compactObject({
+ command: payload.command,
+ cwd: payload.cwd,
+ source: payload.source,
+ interaction_input: payload.interaction_input,
+ }),
+ output: payload.aggregated_output ?? payload.formatted_output ?? joinedOutput(payload),
+ failed: isFailed(payload),
+ };
+ }
+ if (payload?.type === "patch_apply_end" || payload?.type === "FileChange") {
+ return {
+ kind: "patch",
+ id: payload.call_id || payload.id,
+ name: "apply_patch",
+ input: compactObject({
+ changes: payload.changes,
+ auto_approved: payload.auto_approved,
+ }),
+ output: joinedOutput(payload),
+ failed: isFailed(payload),
+ };
+ }
+ if (payload?.type === "DynamicToolCall") {
+ return {
+ kind: "dynamic",
+ id: payload.id,
+ name: payload.tool,
+ input: payload.arguments ?? {},
+ output: payload.error ?? payload.content_items ?? [],
+ failed: isFailed(payload),
+ };
+ }
+ return null;
+}
+
+function completedToolKey(tool) {
+ return [tool.kind, tool.id, tool.name].join("\0");
+}
+
+function completedToolPriority(entry) {
+ const source = entry?.type === "turn_item" ? 2 : 0;
+ const status = String(entry?.payload?.status || "").toLowerCase();
+ return source + (TERMINAL_TOOL_STATUSES.has(status) ? 1 : 0);
+}
+
+function responseItemKey(entry) {
+ if (entry?.type !== "response_item") return "";
+ const payload = entry.payload;
+ if (!payload?.type) return "";
+ const id = payload.call_id || payload.id;
+ if (!id) return "";
+ const kind = payload.type === "custom_tool_call"
+ ? "function_call"
+ : payload.type === "custom_tool_call_output"
+ ? "function_call_output"
+ : payload.type;
+ return `${kind}\0${id}`;
+}
+
+function deduplicateCodexToolEvents(rolloutEntries) {
+ const entries = [];
+ const responseItems = new Set();
+ for (const entry of rolloutEntries || []) {
+ const key = responseItemKey(entry);
+ if (key && responseItems.has(key)) continue;
+ if (key) responseItems.add(key);
+ entries.push(entry);
+ }
+
+ const completedTools = new Map();
+ const preferredCompleted = new Map();
+ for (const entry of entries) {
+ const tool = completedToolEvent(entry);
+ if (!tool?.id || !tool.name) continue;
+ completedTools.set(entry, tool);
+ const key = completedToolKey(tool);
+ const previous = preferredCompleted.get(key);
+ if (!previous || completedToolPriority(entry) >= completedToolPriority(previous)) {
+ preferredCompleted.set(key, entry);
+ }
+ }
+ const completedNamesById = new Map();
+ for (const entry of preferredCompleted.values()) {
+ const tool = completedTools.get(entry);
+ const names = completedNamesById.get(tool.id) ?? new Set();
+ names.add(tool.name);
+ completedNamesById.set(tool.id, names);
+ }
+ const responseNamesById = new Map();
+ for (const entry of entries) {
+ const payload = entry?.payload;
+ if (
+ entry?.type === "response_item"
+ && RESPONSE_TOOL_TYPES.has(payload?.type)
+ && payload?.name
+ ) {
+ responseNamesById.set(payload.call_id || payload.id, payload.name);
+ }
+ }
+
+ return entries.flatMap((entry) => {
+ const tool = completedTools.get(entry);
+ if (tool?.id && tool.name) {
+ return preferredCompleted.get(completedToolKey(tool)) === entry ? [entry] : [];
+ }
const payload = entry?.payload;
- return !(
- duplicatedTypes.has(payload?.type)
- && mcpCallIds.has(payload.call_id || payload.id)
- );
+ const id = payload?.call_id || payload?.id;
+ const name = payload?.name || responseNamesById.get(id);
+ const completedNames = completedNamesById.get(id);
+ if (!RESPONSE_TOOL_TYPES.has(payload?.type) || !completedNames) return [entry];
+ if (completedNames.has(name)) return [];
+ if (name === "exec") {
+ return [{
+ ...entry,
+ payload: { ...payload, call_id: `codex-container:${id}` },
+ }];
+ }
+ return [];
});
}
@@ -138,43 +326,35 @@ function normalizeCodexNativeToolEvents(rolloutEntries) {
});
}
-function normalizeCodexMcpToolEvents(rolloutEntries) {
+function normalizeCodexCompletedToolEvents(rolloutEntries) {
return (rolloutEntries || []).flatMap((entry) => {
- const payload = entry?.payload;
- if (payload?.type !== "mcp_tool_call_end") return [entry];
-
- const callId = payload.call_id;
- const toolName = payload.invocation?.tool;
- if (!callId || !toolName) return [entry];
-
- const call = {
- payload: {
- type: "function_call",
- id: callId,
- name: toolName,
- arguments: payload.invocation?.arguments || {},
- },
- };
- const output = mcpResultText(payload.result);
- const completed = (
- Object.prototype.hasOwnProperty.call(payload.result || {}, "Ok")
- && payload.result?.Ok?.isError !== true
- );
- const result = {
- payload: {
- type: "function_call_output",
- call_id: callId,
- ...(completed ? { output } : { error: output || "MCP tool call failed" }),
- },
- };
+ const tool = completedToolEvent(entry);
+ if (!tool?.id || !tool.name) return [entry];
+
+ const call = inheritedEntry(entry, "response_item", {
+ type: "function_call",
+ id: tool.id,
+ call_id: tool.id,
+ name: tool.name,
+ arguments: tool.input,
+ });
+ const result = inheritedEntry(entry, "response_item", {
+ type: "function_call_output",
+ call_id: tool.id,
+ status: tool.failed ? "error" : "completed",
+ ...(tool.failed
+ ? { error: tool.output || `${tool.name} failed` }
+ : { output: tool.output }),
+ });
return [call, result];
});
}
export function extractCaptureTurns(rolloutEntries, cfg = {}) {
- const deduplicated = deduplicateMcpFunctionEvents(rolloutEntries);
+ const expanded = expandCodexRolloutEntries(rolloutEntries);
+ const deduplicated = deduplicateCodexToolEvents(expanded);
const normalized = normalizeCodexNativeToolEvents(deduplicated);
- return extractSharedCaptureTurns(normalizeCodexMcpToolEvents(normalized), cfg);
+ return extractSharedCaptureTurns(normalizeCodexCompletedToolEvents(normalized), cfg);
}
/**
diff --git a/examples/codex-memory-plugin/scripts/capture-utils.test.mjs b/examples/codex-memory-plugin/scripts/capture-utils.test.mjs
index 6bfe64a5c..040ee91b9 100644
--- a/examples/codex-memory-plugin/scripts/capture-utils.test.mjs
+++ b/examples/codex-memory-plugin/scripts/capture-utils.test.mjs
@@ -9,6 +9,12 @@ const CAPTURE_CONFIG = {
captureMaxLength: 24000,
};
+function toolParts(turns, toolName) {
+ return turns
+ .flatMap((turn) => turn.parts)
+ .filter((part) => part.type === "tool" && part.tool_name === toolName);
+}
+
test("pairs current Codex function_call records by call_id", () => {
const turns = extractCaptureTurns(
[
@@ -201,6 +207,507 @@ test("deduplicates function records also reported as mcp_tool_call_end", () => {
assert.deepEqual(parts.map((part) => part.tool_id), ["mcp-call-1", "mcp-call-1"]);
});
+test("keeps outer exec and captures its nested MCP tool by the nested call id", () => {
+ const uri = "viking://user/test/memories/experiences/refund.md";
+ const turns = extractCaptureTurns(
+ [
+ {
+ type: "response_item",
+ payload: {
+ type: "custom_tool_call",
+ call_id: "call-outer",
+ name: "exec",
+ input: "await tools.mcp__openviking_memory__read(...)",
+ },
+ },
+ {
+ type: "response_item",
+ payload: {
+ type: "custom_tool_call_output",
+ call_id: "call-outer",
+ output: "nested call completed",
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "mcp_tool_call_end",
+ call_id: "code-mode-nested:7:call-outer:exec-read",
+ invocation: {
+ server: "openviking-memory",
+ tool: "read",
+ arguments: { uris: [uri] },
+ },
+ result: {
+ Ok: {
+ content: [{ type: "text", text: JSON.stringify({ uri, content: "refund policy" }) }],
+ },
+ },
+ },
+ },
+ ],
+ CAPTURE_CONFIG,
+ );
+
+ assert.deepEqual(toolParts(turns, "exec").map((part) => part.tool_status), [
+ "running",
+ "completed",
+ ]);
+ assert.deepEqual(toolParts(turns, "read"), [
+ {
+ type: "tool",
+ tool_id: "code-mode-nested:7:call-outer:exec-read",
+ tool_name: "read",
+ tool_status: "running",
+ tool_input: { uris: [uri] },
+ },
+ {
+ type: "tool",
+ tool_id: "code-mode-nested:7:call-outer:exec-read",
+ tool_name: "read",
+ tool_status: "completed",
+ tool_output: JSON.stringify({ uri, content: "refund policy" }),
+ },
+ ]);
+});
+
+test("keeps outer exec when a nested tool reuses its call id", () => {
+ const turns = extractCaptureTurns(
+ [
+ {
+ type: "response_item",
+ payload: {
+ type: "custom_tool_call",
+ call_id: "shared-call-id",
+ name: "exec",
+ input: "await tools.mcp__openviking_memory__find(...)",
+ },
+ },
+ {
+ type: "response_item",
+ payload: {
+ type: "custom_tool_call_output",
+ call_id: "shared-call-id",
+ output: "nested call completed",
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "mcp_tool_call_end",
+ call_id: "shared-call-id",
+ invocation: {
+ server: "openviking-memory",
+ tool: "find",
+ arguments: { query: "refund" },
+ },
+ result: { Ok: { content: [{ type: "text", text: "found" }] } },
+ },
+ },
+ ],
+ CAPTURE_CONFIG,
+ );
+
+ assert.deepEqual(toolParts(turns, "exec").map((part) => part.tool_status), [
+ "running",
+ "completed",
+ ]);
+ assert.deepEqual(toolParts(turns, "find").map((part) => part.tool_status), [
+ "running",
+ "completed",
+ ]);
+});
+
+test("captures legacy command and patch completion events", () => {
+ const turns = extractCaptureTurns(
+ [
+ {
+ type: "event_msg",
+ payload: {
+ type: "exec_command_end",
+ call_id: "exec-shell",
+ command: ["pwd"],
+ cwd: "/workspace",
+ aggregated_output: "/workspace\n",
+ exit_code: 0,
+ status: "completed",
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "patch_apply_end",
+ call_id: "exec-patch",
+ changes: { "README.md": { type: "update" } },
+ stdout: "Done!",
+ stderr: "",
+ success: true,
+ status: "completed",
+ },
+ },
+ ],
+ CAPTURE_CONFIG,
+ );
+
+ assert.deepEqual(toolParts(turns, "exec_command"), [
+ {
+ type: "tool",
+ tool_id: "exec-shell",
+ tool_name: "exec_command",
+ tool_status: "running",
+ tool_input: { command: ["pwd"], cwd: "/workspace" },
+ },
+ {
+ type: "tool",
+ tool_id: "exec-shell",
+ tool_name: "exec_command",
+ tool_status: "completed",
+ tool_output: "/workspace",
+ },
+ ]);
+ assert.deepEqual(toolParts(turns, "apply_patch"), [
+ {
+ type: "tool",
+ tool_id: "exec-patch",
+ tool_name: "apply_patch",
+ tool_status: "running",
+ tool_input: { changes: { "README.md": { type: "update" } } },
+ },
+ {
+ type: "tool",
+ tool_id: "exec-patch",
+ tool_name: "apply_patch",
+ tool_status: "completed",
+ tool_output: "Done!",
+ },
+ ]);
+});
+
+test("falls back to command stdout and stderr when aggregated output is absent", () => {
+ const turns = extractCaptureTurns(
+ [{
+ type: "event_msg",
+ payload: {
+ type: "exec_command_end",
+ call_id: "exec-shell-streams",
+ command: ["check"],
+ stdout: "partial output",
+ stderr: "failure detail",
+ exit_code: 2,
+ status: "failed",
+ },
+ }],
+ CAPTURE_CONFIG,
+ );
+
+ assert.deepEqual(toolParts(turns, "exec_command").at(-1), {
+ type: "tool",
+ tool_id: "exec-shell-streams",
+ tool_name: "exec_command",
+ tool_status: "error",
+ tool_output: "partial output\nfailure detail",
+ });
+});
+
+test("uses the final legacy command lifecycle event", async (t) => {
+ for (const exitCode of [1, 130]) {
+ await t.test(`records exit ${exitCode} as an error`, () => {
+ const callId = `exec-backgrounded-${exitCode}`;
+ const turns = extractCaptureTurns(
+ [
+ {
+ type: "event_msg",
+ payload: {
+ type: "exec_command_end",
+ call_id: callId,
+ command: ["long-running-command"],
+ aggregated_output: "",
+ exit_code: 0,
+ status: "backgrounded",
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "exec_command_end",
+ call_id: callId,
+ command: ["long-running-command"],
+ aggregated_output: `failed with exit ${exitCode}`,
+ exit_code: exitCode,
+ status: "failed",
+ },
+ },
+ ],
+ CAPTURE_CONFIG,
+ );
+
+ assert.deepEqual(toolParts(turns, "exec_command").map((part) => part.tool_status), [
+ "running",
+ "error",
+ ]);
+ assert.equal(
+ toolParts(turns, "exec_command").at(-1).tool_output,
+ `failed with exit ${exitCode}`,
+ );
+ });
+ }
+});
+
+test("captures completed paginated tools without duplicating legacy events", () => {
+ const experienceUri = "viking://user/test/memories/experiences/refund.md";
+ const turns = extractCaptureTurns(
+ [
+ {
+ type: "event_msg",
+ payload: {
+ type: "item_completed",
+ item: {
+ type: "McpToolCall",
+ id: "nested-find",
+ server: "openviking-memory",
+ tool: "find",
+ arguments: { query: "refund" },
+ status: "completed",
+ result: { content: [{ type: "text", text: "found" }], isError: false },
+ },
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "mcp_tool_call_end",
+ call_id: "nested-find",
+ invocation: {
+ server: "openviking-memory",
+ tool: "find",
+ arguments: { query: "refund" },
+ },
+ result: { Ok: { content: [{ type: "text", text: "found" }] } },
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "item_completed",
+ item: {
+ type: "McpToolCall",
+ id: "nested-read",
+ server: "openviking-memory",
+ tool: "read",
+ arguments: { uris: [experienceUri] },
+ status: "completed",
+ result: {
+ content: [{
+ type: "text",
+ text: JSON.stringify({ uri: experienceUri, content: "refund policy" }),
+ }],
+ isError: false,
+ },
+ },
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "item_completed",
+ item: {
+ type: "CommandExecution",
+ id: "nested-command",
+ command: ["false"],
+ cwd: "/workspace",
+ parsed_cmd: [],
+ source: "unified_exec_startup",
+ status: "failed",
+ aggregated_output: "command failed",
+ exit_code: 1,
+ },
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "exec_command_end",
+ call_id: "nested-command",
+ command: ["false"],
+ cwd: "/workspace",
+ status: "backgrounded",
+ aggregated_output: "backgrounded",
+ exit_code: 0,
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "item_completed",
+ item: {
+ type: "FileChange",
+ id: "nested-patch",
+ changes: { "src/a.js": { type: "add" } },
+ status: "completed",
+ stdout: "Done!",
+ stderr: "",
+ },
+ },
+ },
+ {
+ type: "event_msg",
+ payload: {
+ type: "item_completed",
+ item: {
+ type: "DynamicToolCall",
+ id: "dynamic-1",
+ tool: "lookup",
+ arguments: { id: 7 },
+ status: "completed",
+ success: true,
+ content_items: [{ type: "inputText", text: "record 7" }],
+ },
+ },
+ },
+ ],
+ CAPTURE_CONFIG,
+ );
+
+ assert.deepEqual(toolParts(turns, "find").map((part) => part.tool_status), [
+ "running",
+ "completed",
+ ]);
+ assert.deepEqual(toolParts(turns, "read"), [
+ {
+ type: "tool",
+ tool_id: "nested-read",
+ tool_name: "read",
+ tool_status: "running",
+ tool_input: { uris: [experienceUri] },
+ },
+ {
+ type: "tool",
+ tool_id: "nested-read",
+ tool_name: "read",
+ tool_status: "completed",
+ tool_output: JSON.stringify({ uri: experienceUri, content: "refund policy" }),
+ },
+ ]);
+ assert.deepEqual(toolParts(turns, "exec_command").map((part) => part.tool_status), [
+ "running",
+ "error",
+ ]);
+ assert.equal(toolParts(turns, "exec_command").at(-1).tool_output, "command failed");
+ assert.deepEqual(toolParts(turns, "apply_patch").map((part) => part.tool_status), [
+ "running",
+ "completed",
+ ]);
+ assert.deepEqual(toolParts(turns, "lookup"), [
+ {
+ type: "tool",
+ tool_id: "dynamic-1",
+ tool_name: "lookup",
+ tool_status: "running",
+ tool_input: { id: 7 },
+ },
+ {
+ type: "tool",
+ tool_id: "dynamic-1",
+ tool_name: "lookup",
+ tool_status: "completed",
+ tool_output: JSON.stringify([{ type: "inputText", text: "record 7" }]),
+ },
+ ]);
+});
+
+test("expands history mutation items and deduplicates matching response items", () => {
+ const call = {
+ type: "function_call",
+ id: "fc-history-1",
+ call_id: "history-call-1",
+ name: "exec",
+ arguments: "await Promise.resolve()",
+ };
+ const output = {
+ type: "function_call_output",
+ id: "fco-history-1",
+ call_id: "history-call-1",
+ output: "done",
+ };
+ const turns = extractCaptureTurns(
+ [
+ {
+ type: "history_mutation",
+ payload: {
+ operation: "append",
+ items: [
+ {
+ type: "message",
+ id: "history-message-1",
+ role: "assistant",
+ content: [{ type: "output_text", text: "running a check" }],
+ },
+ call,
+ output,
+ ],
+ },
+ },
+ { type: "response_item", payload: call },
+ { type: "response_item", payload: output },
+ ],
+ CAPTURE_CONFIG,
+ );
+
+ assert.deepEqual(toolParts(turns, "exec").map((part) => part.tool_status), [
+ "running",
+ "completed",
+ ]);
+ assert.equal(turns.some((turn) => turn.text === "running a check"), true);
+});
+
+test("deduplicates custom tool calls repeated as history function calls", () => {
+ const turns = extractCaptureTurns(
+ [
+ {
+ type: "response_item",
+ payload: {
+ type: "custom_tool_call",
+ id: "custom-item-1",
+ call_id: "outer-call-1",
+ name: "exec",
+ input: "await Promise.resolve()",
+ },
+ },
+ {
+ type: "history_mutation",
+ payload: {
+ operation: "append",
+ items: [{
+ type: "function_call",
+ id: "function-item-1",
+ call_id: "outer-call-1",
+ name: "exec",
+ arguments: "await Promise.resolve()",
+ }],
+ },
+ },
+ {
+ type: "history_mutation",
+ payload: {
+ operation: "append",
+ items: [{
+ type: "function_call_output",
+ id: "function-output-1",
+ call_id: "outer-call-1",
+ output: "done",
+ }],
+ },
+ },
+ ],
+ CAPTURE_CONFIG,
+ );
+
+ assert.deepEqual(toolParts(turns, "exec").map((part) => part.tool_status), [
+ "running",
+ "completed",
+ ]);
+});
+
test("captures Codex subagent messages and activity with identity", () => {
const turns = extractCaptureTurns(
[
@@ -360,6 +867,35 @@ test("keeps MCP tool-level errors out of completed generic read tool parts", ()
assert.equal(parts.some((part) => part.tool_status === "completed"), false);
});
+test("marks paginated MCP errors as failed tool parts", () => {
+ const turns = extractCaptureTurns(
+ [
+ {
+ type: "event_msg",
+ payload: {
+ type: "item_completed",
+ item: {
+ type: "McpToolCall",
+ id: "nested-read-error",
+ server: "openviking-memory",
+ tool: "read",
+ arguments: { uris: ["viking://user/test/memories/experiences/a.md"] },
+ status: "failed",
+ error: { message: "OpenViking request failed (HTTP 500)" },
+ },
+ },
+ },
+ ],
+ CAPTURE_CONFIG,
+ );
+
+ assert.deepEqual(toolParts(turns, "read").map((part) => part.tool_status), [
+ "running",
+ "error",
+ ]);
+ assert.match(toolParts(turns, "read")[1].tool_output, /HTTP 500/);
+});
+
test("preserves generic read input when Codex truncates a long MCP result", () => {
const uri = "viking://user/test/memories/experiences/long-experience.md";
const turns = extractCaptureTurns(
diff --git a/examples/trae-cli-memory-hooks/.mcp.json b/examples/trae-cli-memory-hooks/.mcp.json
index fd5668560..30ab7661c 100644
--- a/examples/trae-cli-memory-hooks/.mcp.json
+++ b/examples/trae-cli-memory-hooks/.mcp.json
@@ -6,7 +6,25 @@
"servers/mcp-proxy.mjs"
],
"cwd": ".",
- "startup_timeout_sec": 30
+ "startup_timeout_sec": 30,
+ "env_vars": [
+ "OPENVIKING_CLI_CONFIG_FILE",
+ "OPENVIKING_CONFIG_FILE",
+ "OPENVIKING_URL",
+ "OPENVIKING_BASE_URL",
+ "OPENVIKING_MCP_URL",
+ "OPENVIKING_API_KEY",
+ "OPENVIKING_BEARER_TOKEN",
+ "OPENVIKING_ACCOUNT",
+ "OPENVIKING_USER",
+ "OPENVIKING_PEER_ID",
+ "OPENVIKING_CREDENTIAL_SOURCE",
+ "OPENVIKING_CREDENTIALS_SOURCE",
+ "OPENVIKING_TIMEOUT_MS",
+ "OPENVIKING_INTEGRATION_VERSION",
+ "OPENVIKING_DEBUG",
+ "OPENVIKING_DEBUG_LOG"
+ ]
}
}
}