mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-03 02:46:22 +00:00
* refactor: extract agent core package Introduce packages/agent-core as the OpenClaw-owned home for reusable agent loop, harness, session, prompt, and runtime dependency contracts. * refactor: extract shared llm runtime Move provider model registries, stream wrappers, OAuth helpers, and LLM utilities into src/llm with plugin-sdk barrels instead of depending on the old embedded runtime layout. * refactor: remove pi runtime internals Rename remaining Pi-shaped agent surfaces to OpenClaw agent runtime names, delete obsolete Pi docs and package graph checks, and add the third-party notice for incorporated code. * refactor: tighten agent session runtime Make agent-core/runtime dependencies explicit, consolidate compaction and session transcript helpers, and move model/session helpers behind OpenClaw-owned contracts. * refactor: remove static model and pi auth paths Drop static model catalogs and Pi auth bridges, move model/provider facts to manifest-owned runtime contracts, and harden internal embedded-agent utilities. * refactor: remove legacy provider compat paths * docs: remove agent parity notes * fix: skip provider wildcard metadata parsing * refactor: share session extension sdk loading * refactor: inline acpx proxy error formatter * refactor: fold edit recovery into edit tool * fix: accept extension batch separator * test: align startup provider plugin expectations * fix: restore provider-scoped release discovery * test: align static asset packaging expectations * fix: run static provider catalogs during scoped discovery * fix: add provider entry catalogs for scoped live discovery * fix: load lightweight provider catalog entries * fix: refresh provider-scoped plugin metadata * fix: keep provider catalog entries on release live path * fix: keep static manifest models in release live checks * fix: harden release model discovery * fix: reduce OpenAI live cache probe reasoning * fix: disable OpenAI cache probe reasoning * ci: extend OpenAI gateway live timeout * fix: extend live gateway model budget * fix: stabilize release validation regressions * fix: honor provider aliases in model rows * fix: stabilize release validation lanes * fix: stabilize release memory qa * ci: stabilize release validation lanes * ci: prefer ipv4 for live docker node calls * fix: restore shared tool-call stream wrapper * ci: remove legacy pi test shard alias * fix: clean up embedded agent test drift * fix: stabilize runtime alias status * fix: clean up embedded agent ci drift * fix: restore release ci invariants * fix: clean up post-rebase runtime drift * fix: restore release ci checks * fix: restore release ci after rebase * fix: remove stale pi runtime path * test: align compaction runtime expectations * test: update plugin prerelease expectations * fix: handle claude live tool approvals * fix: stabilize release validation gates * fix: finish agent runtime import * test: finish post-rebase agent runtime mocks * fix: keep codex compaction native * fix: stabilize codex app-server hook tests * test: isolate codex diagnostic active run * test: remove codex diagnostic completion race # Conflicts: # extensions/codex/src/app-server/run-attempt.test.ts * ci: fix full release manifest performance run id * refactor: narrow llm plugin sdk boundary * chore: drop generated google boundary stamps * fix: repair rebase fallout * fix: clean up rebased runtime references * fix: decode codex jwt payloads as base64url * fix: preserve shipped pi runtime alias * fix: add scoped sdk virtual modules * fix: decode llm codex oauth jwt as base64url * fix: avoid stale vertex adc negative cache * fix: harden tool arg decoding and codeql path * fix: keep vertex adc negative checks live * refactor: consolidate codex jwt and edit helpers * fix: await codex oauth node runtime imports * fix: preserve sdk tool and notice contracts * fix: preserve shipped compat config boundaries * fix: align codex oauth callback host * fix: terminate agent-core loop streams on failure * fix: keep codex oauth callback alive during fallback * ci: include session tools in critical codeql scans * fix: keep Cloudflare Anthropic provider auth header * docs: redirect legacy pi runtime pages * fix: honor bundled web provider compat discovery * fix: protect session output spill files * fix: keep legacy agent dir env blocked * fix: contain auto-discovered skill symlinks * fix: harden agent core sdk proxy surfaces * fix: restore approval reaction sdk compat * fix: keep live docker runs bounded * fix: keep codex oauth redirect host aligned * fix: resolve post-rebase agent runtime drift * fix: redact anthropic oauth parse failures * fix: preserve responses strict tool shaping * fix: repair agent runtime rebase cleanup * docs: redirect retired parity pages * fix: bound auto-discovered resources to roots * fix: repair post-rebase agent test drift * fix: preserve bundled provider allowlist migration * fix: preserve manifest-owned provider aliases * fix: declare photon image dependency * fix: keep provider headers out of proxy body * fix: preserve shipped env aliases * fix: refresh control ui i18n generated state * fix: quote read fallback paths * fix: preview edits through configured backend * test: satisfy core test typecheck * fix: preserve ZAI usage auth fallback * test: repair codex diagnostic test * fix: repair agent runtime rebase drift * test: finish embedded runner import rename * fix: repair agent runtime rebase integrations * test: align compaction oauth fallback expectations * fix: allow sdk-auth session models * fix: update doctor tool schema import * fix: preserve bedrock plugin region * fix: stream harmony-like prose immediately * ci: include session runtime in codeql shards * fix: repair latest rebase integrations * fix: honor explicit codex websocket transport * fix: keep openai-compatible credentials provider-scoped * fix: refresh sdk api baseline after rebase * fix: route cli runtime aliases through openclaw harness * test: rename stale harness mock expectation * test: rename embedded agent overflow calls * test: clean embedded auth test wording * test: use openclaw stream types in deepinfra cache test * fix: refresh sdk api baseline on latest main * fix: honor bundled discovery compat allowlists * fix: refresh sdk api baseline after latest rebase * fix: remove stale rebase imports * test: rename stale model catalog mock * test: mock renamed doctor runtime modules * fix: map canonical kimi env auth * fix: use internal model registry in bench script * fix: migrate deepinfra provider catalog entry * fix: enforce builtin tool suppression * fix: route compaction auth and proxy payloads safely * refactor: prune unused llm registry leftovers * test: update codex hooks session import * test: fix model picker ci coverage * test: align model picker auth mock types
408 lines
15 KiB
TypeScript
408 lines
15 KiB
TypeScript
import type { AgentMessage } from "openclaw/plugin-sdk/agent-core";
|
|
import { describe, expect, it } from "vitest";
|
|
import type { OpenClawConfig } from "../config/types.openclaw.js";
|
|
import { redactTranscriptMessage } from "./transcript-redact.js";
|
|
|
|
/** Typed accessor for `content` on AgentMessage.
|
|
* AgentMessage is a union that includes custom message types (e.g. BashExecutionMessage)
|
|
* which have no `content` field. Direct `.content` access fails tsgo's strict union check.
|
|
*/
|
|
function msgContent(msg: AgentMessage): unknown {
|
|
return (msg as unknown as { content: unknown }).content;
|
|
}
|
|
|
|
function textMessage(text: string): AgentMessage {
|
|
return {
|
|
role: "assistant",
|
|
content: [{ type: "text", text }],
|
|
} as unknown as AgentMessage;
|
|
}
|
|
|
|
function cfg(mode: "tools" | "off", patterns?: string[]): OpenClawConfig {
|
|
return {
|
|
logging: {
|
|
redactSensitive: mode,
|
|
...(patterns ? { redactPatterns: patterns } : {}),
|
|
},
|
|
} satisfies OpenClawConfig;
|
|
}
|
|
|
|
const EMAIL_PATTERN = String.raw`([\w]|[-.])+@([\w]|[-.])+\.\w+`;
|
|
|
|
describe("redactTranscriptMessage", () => {
|
|
it("redacts text block matching default patterns (sk- token)", () => {
|
|
const msg = textMessage("key is sk-abcdef1234567890xyz end");
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
const text = (msgContent(result) as Array<{ text: string }>)[0].text;
|
|
expect(text).not.toContain("sk-abcdef1234567890xyz");
|
|
expect(text).toContain("end");
|
|
});
|
|
|
|
it("redacts thinking block", () => {
|
|
const msg = {
|
|
role: "assistant",
|
|
content: [
|
|
{ type: "thinking", thinking: "secret sk-abcdef1234567890xyz", thinkingSignature: "sig" },
|
|
],
|
|
} as unknown as AgentMessage;
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
const block = (msgContent(result) as Array<{ thinking: string }>)[0];
|
|
expect(block.thinking).not.toContain("sk-abcdef1234567890xyz");
|
|
});
|
|
|
|
it("redacts partialJson block", () => {
|
|
const msg = {
|
|
role: "assistant",
|
|
content: [{ type: "toolCallDelta", partialJson: '{"key":"sk-abcdef1234567890xyz"}' }],
|
|
} as unknown as AgentMessage;
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
const block = (msgContent(result) as Array<{ partialJson: string }>)[0];
|
|
expect(block.partialJson).not.toContain("sk-abcdef1234567890xyz");
|
|
});
|
|
|
|
it("redacts nested strings in assistant tool-call arguments", () => {
|
|
const msg = {
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "toolCall",
|
|
id: "call_1",
|
|
name: "shell",
|
|
arguments: {
|
|
command: "OPENAI_API_KEY=sk-abcdef1234567890xyz openclaw health",
|
|
env: { nested: ["token sk-abcdef1234567890xyz"] },
|
|
count: 1,
|
|
},
|
|
},
|
|
],
|
|
} as unknown as AgentMessage;
|
|
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
const block = (msgContent(result) as Array<{ arguments: unknown }>)[0];
|
|
const argumentsValue = block.arguments as {
|
|
command: string;
|
|
env: { nested: string[] };
|
|
count: number;
|
|
};
|
|
const serializedArguments = JSON.stringify(block.arguments);
|
|
expect(serializedArguments).not.toContain("sk-abcdef1234567890xyz");
|
|
expect(argumentsValue.command).toBe("OPENAI_API_KEY=sk-abc…0xyz openclaw health");
|
|
expect(argumentsValue.env.nested[0]).toBe("token sk-abc…0xyz");
|
|
expect(argumentsValue.count).toBe(1);
|
|
expect(serializedArguments).toContain("openclaw health");
|
|
expect(block.arguments).not.toBe(
|
|
(msgContent(msg) as Array<{ arguments: unknown }>)[0].arguments,
|
|
);
|
|
});
|
|
|
|
it("redacts structured secret fields in assistant tool-call arguments", () => {
|
|
const msg = {
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "toolCall",
|
|
id: "call_1",
|
|
name: "send_request",
|
|
arguments: {
|
|
apiKey: "plainsecretvalue123",
|
|
password: "hunter2",
|
|
nested: { accessToken: ["nestedplainsecret123"] },
|
|
safe: "visible",
|
|
},
|
|
},
|
|
],
|
|
} as unknown as AgentMessage;
|
|
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
const block = (msgContent(result) as Array<{ arguments: unknown }>)[0];
|
|
const argumentsValue = block.arguments as {
|
|
apiKey: string;
|
|
password: string;
|
|
nested: { accessToken: string[] };
|
|
safe: string;
|
|
};
|
|
const serializedArguments = JSON.stringify(block.arguments);
|
|
expect(serializedArguments).not.toContain("plainsecretvalue123");
|
|
expect(serializedArguments).not.toContain("hunter2");
|
|
expect(serializedArguments).not.toContain("nestedplainsecret123");
|
|
expect(argumentsValue.apiKey).toBe("plains…e123");
|
|
expect(argumentsValue.password).toBe("***");
|
|
expect(argumentsValue.nested.accessToken[0]).toBe("nested…t123");
|
|
expect(serializedArguments).toContain("visible");
|
|
});
|
|
|
|
it("redacts structured tool-use input payloads", () => {
|
|
const msg = {
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "toolUse",
|
|
id: "call_1",
|
|
name: "send_request",
|
|
input: {
|
|
apiKey: "plainsecretvalue123",
|
|
nested: { accessToken: ["nestedplainsecret123"] },
|
|
command: "OPENAI_API_KEY=sk-abcdef1234567890xyz openclaw health",
|
|
safe: "visible",
|
|
},
|
|
},
|
|
],
|
|
} as unknown as AgentMessage;
|
|
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
const block = (msgContent(result) as Array<{ input: unknown }>)[0];
|
|
const inputValue = block.input as {
|
|
apiKey: string;
|
|
nested: { accessToken: string[] };
|
|
command: string;
|
|
safe: string;
|
|
};
|
|
const serializedInput = JSON.stringify(block.input);
|
|
expect(serializedInput).not.toContain("plainsecretvalue123");
|
|
expect(serializedInput).not.toContain("nestedplainsecret123");
|
|
expect(serializedInput).not.toContain("sk-abcdef1234567890xyz");
|
|
expect(inputValue.apiKey).toBe("plains…e123");
|
|
expect(inputValue.nested.accessToken[0]).toBe("nested…t123");
|
|
expect(inputValue.command).toBe("OPENAI_API_KEY=sk-abc…0xyz openclaw health");
|
|
expect(serializedInput).toContain("visible");
|
|
});
|
|
|
|
it("redacts defensive function-call input payloads", () => {
|
|
const msg = {
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "functionCall",
|
|
id: "call_1",
|
|
name: "send_request",
|
|
input: {
|
|
password: "hunter2",
|
|
nested: { accessToken: ["nestedplainsecret123"] },
|
|
},
|
|
},
|
|
],
|
|
} as unknown as AgentMessage;
|
|
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
const block = (msgContent(result) as Array<{ input: unknown }>)[0];
|
|
const inputValue = block.input as {
|
|
password: string;
|
|
nested: { accessToken: string[] };
|
|
};
|
|
const serializedInput = JSON.stringify(block.input);
|
|
expect(serializedInput).not.toContain("hunter2");
|
|
expect(serializedInput).not.toContain("nestedplainsecret123");
|
|
expect(inputValue.password).toBe("***");
|
|
expect(inputValue.nested.accessToken[0]).toBe("nested…t123");
|
|
});
|
|
|
|
it("redacts arbitrary gateway/custom content-block fields recursively", () => {
|
|
const msg = {
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "gatewayCustom",
|
|
source: {
|
|
url: "https://example.com/callback?token=sk-abcdef1234567890xyz",
|
|
},
|
|
data: {
|
|
apiKey: "plainsecretvalue123",
|
|
nested: {
|
|
accessToken: "nestedplainsecret123",
|
|
},
|
|
},
|
|
safe: "visible",
|
|
},
|
|
],
|
|
} as unknown as AgentMessage;
|
|
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
const block = (msgContent(result) as Array<Record<string, unknown>>)[0];
|
|
const serializedBlock = JSON.stringify(block);
|
|
expect(serializedBlock).not.toContain("sk-abcdef1234567890xyz");
|
|
expect(serializedBlock).not.toContain("plainsecretvalue123");
|
|
expect(serializedBlock).not.toContain("nestedplainsecret123");
|
|
expect(serializedBlock).toContain("visible");
|
|
});
|
|
|
|
it("redacts circular structured payloads without throwing", () => {
|
|
const details: Record<string, unknown> = {
|
|
apiKey: "plainsecretvalue123",
|
|
};
|
|
details.self = details;
|
|
const msg = {
|
|
role: "toolResult",
|
|
toolCallId: "call_1",
|
|
toolName: "send_request",
|
|
content: [{ type: "text", text: "result" }],
|
|
details,
|
|
isError: false,
|
|
timestamp: Date.now(),
|
|
} as unknown as AgentMessage;
|
|
|
|
const result = redactTranscriptMessage(msg, cfg("tools")) as unknown as {
|
|
details: Record<string, unknown>;
|
|
};
|
|
expect(result.details.apiKey).toBe("plains…e123");
|
|
expect(result.details.self).toBe("[Circular]");
|
|
});
|
|
|
|
it("redacts structured secret fields in tool-result details", () => {
|
|
const msg = {
|
|
role: "toolResult",
|
|
toolCallId: "call_1",
|
|
toolName: "send_request",
|
|
content: [{ type: "text", text: "result sk-abcdef1234567890xyz" }],
|
|
details: {
|
|
apiKey: "plainsecretvalue123",
|
|
password: "hunter2",
|
|
nested: { accessToken: ["nestedplainsecret123"] },
|
|
safe: "visible",
|
|
},
|
|
isError: false,
|
|
timestamp: Date.now(),
|
|
} as unknown as AgentMessage;
|
|
|
|
const result = redactTranscriptMessage(msg, cfg("tools")) as unknown as {
|
|
content: Array<{ text: string }>;
|
|
details: unknown;
|
|
};
|
|
const serializedDetails = JSON.stringify(result.details);
|
|
const details = result.details as {
|
|
apiKey: string;
|
|
password: string;
|
|
nested: { accessToken: string[] };
|
|
safe: string;
|
|
};
|
|
expect(result.content[0].text).not.toContain("sk-abcdef1234567890xyz");
|
|
expect(serializedDetails).not.toContain("plainsecretvalue123");
|
|
expect(serializedDetails).not.toContain("hunter2");
|
|
expect(serializedDetails).not.toContain("nestedplainsecret123");
|
|
expect(details.apiKey).toBe("plains…e123");
|
|
expect(details.password).toBe("***");
|
|
expect(details.nested.accessToken[0]).toBe("nested…t123");
|
|
expect(serializedDetails).toContain("visible");
|
|
});
|
|
|
|
it("redacts string-form content", () => {
|
|
const msg = {
|
|
role: "user",
|
|
content: "my key is sk-abcdef1234567890xyz",
|
|
} as unknown as AgentMessage;
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
expect(msgContent(result) as string).not.toContain("sk-abcdef1234567890xyz");
|
|
});
|
|
|
|
it("redacts documented transcript text fields on content-less message types", () => {
|
|
const msg = {
|
|
role: "bashExecution",
|
|
command: "OPENAI_API_KEY=sk-abcdef1234567890xyz openclaw health",
|
|
output: "failed with sk-abcdef1234567890xyz",
|
|
exitCode: 1,
|
|
cancelled: false,
|
|
truncated: false,
|
|
timestamp: Date.now(),
|
|
} as unknown as AgentMessage;
|
|
|
|
const result = redactTranscriptMessage(msg, cfg("tools")) as unknown as {
|
|
command: string;
|
|
output: string;
|
|
};
|
|
expect(result.command).not.toContain("sk-abcdef1234567890xyz");
|
|
expect(result.output).not.toContain("sk-abcdef1234567890xyz");
|
|
});
|
|
|
|
it("redacts assistant error and summary transcript fields", () => {
|
|
const assistant = {
|
|
role: "assistant",
|
|
content: [{ type: "text", text: "safe" }],
|
|
errorMessage: "provider rejected sk-abcdef1234567890xyz",
|
|
} as unknown as AgentMessage;
|
|
const summary = {
|
|
role: "compactionSummary",
|
|
summary: "summary mentions sk-abcdef1234567890xyz",
|
|
tokensBefore: 10,
|
|
timestamp: Date.now(),
|
|
} as unknown as AgentMessage;
|
|
|
|
const assistantResult = redactTranscriptMessage(assistant, cfg("tools")) as unknown as {
|
|
errorMessage: string;
|
|
};
|
|
const summaryResult = redactTranscriptMessage(summary, cfg("tools")) as unknown as {
|
|
summary: string;
|
|
};
|
|
expect(assistantResult.errorMessage).not.toContain("sk-abcdef1234567890xyz");
|
|
expect(summaryResult.summary).not.toContain("sk-abcdef1234567890xyz");
|
|
});
|
|
|
|
it("redacts using custom pattern without dropping default patterns", () => {
|
|
const msg = textMessage("email peter@dc.io and key sk-abcdef1234567890xyz ok");
|
|
const result = redactTranscriptMessage(msg, cfg("tools", [EMAIL_PATTERN]));
|
|
const text = (msgContent(result) as Array<{ text: string }>)[0].text;
|
|
expect(text).not.toContain("peter@dc.io");
|
|
expect(text).not.toContain("sk-abcdef1234567890xyz");
|
|
expect(text).toContain("ok");
|
|
});
|
|
|
|
it("passes through unchanged when redactSensitive is off", () => {
|
|
const msg = textMessage("key is sk-abcdef1234567890xyz");
|
|
const result = redactTranscriptMessage(msg, cfg("off"));
|
|
expect(result).toBe(msg); // same reference; nothing changed
|
|
});
|
|
|
|
it("leaves structured tool-call secrets unchanged when redactSensitive is off", () => {
|
|
const msg = {
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "toolCall",
|
|
id: "call_1",
|
|
name: "send_request",
|
|
arguments: { apiKey: "plainsecretvalue123", password: "hunter2" },
|
|
},
|
|
],
|
|
} as unknown as AgentMessage;
|
|
const result = redactTranscriptMessage(msg, cfg("off"));
|
|
expect(result).toBe(msg);
|
|
expect(JSON.stringify(msgContent(result))).toContain("plainsecretvalue123");
|
|
expect(JSON.stringify(msgContent(result))).toContain("hunter2");
|
|
});
|
|
|
|
it("leaves structured tool-result details unchanged when redactSensitive is off", () => {
|
|
const msg = {
|
|
role: "toolResult",
|
|
toolCallId: "call_1",
|
|
toolName: "send_request",
|
|
content: [{ type: "text", text: "result" }],
|
|
details: { apiKey: "plainsecretvalue123", password: "hunter2" },
|
|
isError: false,
|
|
timestamp: Date.now(),
|
|
} as unknown as AgentMessage;
|
|
const result = redactTranscriptMessage(msg, cfg("off")) as unknown as { details: unknown };
|
|
expect(result).toBe(msg);
|
|
expect(JSON.stringify(result.details)).toContain("plainsecretvalue123");
|
|
expect(JSON.stringify(result.details)).toContain("hunter2");
|
|
});
|
|
|
|
it("returns same object reference when nothing matches", () => {
|
|
const msg = textMessage("nothing sensitive here");
|
|
const result = redactTranscriptMessage(msg, cfg("tools"));
|
|
expect(result).toBe(msg);
|
|
});
|
|
|
|
it("redacts with cfg=undefined (falls back to default patterns)", () => {
|
|
const msg = textMessage("key is sk-abcdef1234567890xyz");
|
|
const result = redactTranscriptMessage(msg, undefined);
|
|
const text = (msgContent(result) as Array<{ text: string }>)[0].text;
|
|
expect(text).not.toContain("sk-abcdef1234567890xyz");
|
|
});
|
|
|
|
it("passes through non-object and null blocks without throwing", () => {
|
|
const msg = {
|
|
role: "assistant",
|
|
content: [null, 42, "raw string"],
|
|
} as unknown as AgentMessage;
|
|
expect(() => redactTranscriptMessage(msg, cfg("tools"))).not.toThrow();
|
|
});
|
|
});
|