π OpenHands: AI-Driven Development
fix: surface dynamic_context in Agent Tools & Metadata modal (#932) (#973)
burak keles58b03f3b73ac
diff --git a/__tests__/utils/redact-custom-secrets.test.ts b/__tests__/utils/redact-custom-secrets.test.tsnew file mode 100644index 0000000000..1ede6a20eb--- /dev/null+++ b/__tests__/utils/redact-custom-secrets.test.ts@@ -0,0 +1,68 @@+import { describe, it, expect } from "vitest";+import { redactCustomSecrets } from "#/utils/redact-custom-secrets";++describe("redactCustomSecrets", () => {+ it("preserves already-masked secret values", () => {+ const text =+ "<CUSTOM_SECRETS>\nMY_API_KEY=<secret-hidden>\n</CUSTOM_SECRETS>";+ expect(redactCustomSecrets(text)).toBe(text);+ });++ it("redacts an unmasked value inside the block (= separator)", () => {+ const text = "<CUSTOM_SECRETS>\nMY_API_KEY=leaked-value\n</CUSTOM_SECRETS>";+ const result = redactCustomSecrets(text);+ expect(result).not.toContain("leaked-value");+ expect(result).toContain("MY_API_KEY=<secret-hidden>");+ });++ it("redacts an unmasked value inside the block (: separator)", () => {+ const text =+ "<CUSTOM_SECRETS>\nMY_API_KEY: leaked-value\n</CUSTOM_SECRETS>";+ const result = redactCustomSecrets(text);+ expect(result).not.toContain("leaked-value");+ expect(result).toContain("MY_API_KEY: <secret-hidden>");+ });++ it("redacts only inside the block, leaving surrounding text untouched", () => {+ const text =+ "<CURRENT_DATETIME>2026-06-01</CURRENT_DATETIME>\n" ++ "<CUSTOM_SECRETS>\nTOKEN=abc123\n</CUSTOM_SECRETS>\n" ++ "key=value-outside-block";+ const result = redactCustomSecrets(text);+ expect(result).toContain("<CURRENT_DATETIME>2026-06-01</CURRENT_DATETIME>");+ expect(result).toContain("key=value-outside-block");+ expect(result).not.toContain("abc123");+ expect(result).toContain("TOKEN=<secret-hidden>");+ });++ it("returns text unchanged when there is no CUSTOM_SECRETS block", () => {+ const text = "<SKILLS>my-skill</SKILLS>\nkey=value";+ expect(redactCustomSecrets(text)).toBe(text);+ });++ it("redacts multiple secrets within the block", () => {+ const text =+ "<CUSTOM_SECRETS>\nA=one\nB=<secret-hidden>\nC=three\n</CUSTOM_SECRETS>";+ const result = redactCustomSecrets(text);+ expect(result).not.toContain("one");+ expect(result).not.toContain("three");+ expect(result).toContain("A=<secret-hidden>");+ expect(result).toContain("B=<secret-hidden>");+ expect(result).toContain("C=<secret-hidden>");+ });++ it("redacts even when the closing tag is missing (truncated block)", () => {+ const text = "<CUSTOM_SECRETS>\nMY_API_KEY=leaked-value";+ const result = redactCustomSecrets(text);+ expect(result).not.toContain("leaked-value");+ expect(result).toContain("MY_API_KEY=<secret-hidden>");+ });++ it("stays idempotent across repeated calls", () => {+ const text = "<CUSTOM_SECRETS>\nTOKEN=leaked-value\n</CUSTOM_SECRETS>";+ const once = redactCustomSecrets(text);+ const twice = redactCustomSecrets(once);+ expect(twice).toBe(once);+ expect(twice).not.toContain("leaked-value");+ });+});diff --git a/__tests__/utils/system-message-adapter.test.ts b/__tests__/utils/system-message-adapter.test.tsindex 32bd2f9fd2..d0212e85ef 100644--- a/__tests__/utils/system-message-adapter.test.ts+++ b/__tests__/utils/system-message-adapter.test.ts@@ -34,4 +34,44 @@ describe("adaptSystemMessage", () => {it("should return null when no system message is present in events", () => {expect(adaptSystemMessage([])).toBeNull();});++ it("should leave content unchanged when dynamic_context is absent", () => {+ const result = adaptSystemMessage(v1Event);+ expect(result?.content).toBe("v1 prompt");+ });++ it("should append dynamic_context to the system prompt content", () => {+ const events: EventState["events"] = [+ {+ id: "v1-id",+ timestamp: "2025-12-30T12:00:00Z",+ source: "agent",+ system_prompt: { type: "text", text: "v1 prompt" },+ tools: [],+ dynamic_context: { type: "text", text: "<SKILLS>my-skill</SKILLS>" },+ },+ ];+ const result = adaptSystemMessage(events);+ expect(result?.content).toContain("v1 prompt");+ expect(result?.content).toContain("<SKILLS>my-skill</SKILLS>");+ });++ it("should redact unmasked custom secret values in dynamic_context", () => {+ const events: EventState["events"] = [+ {+ id: "v1-id",+ timestamp: "2025-12-30T12:00:00Z",+ source: "agent",+ system_prompt: { type: "text", text: "v1 prompt" },+ tools: [],+ dynamic_context: {+ type: "text",+ text: "<CUSTOM_SECRETS>\nMY_API_KEY=super-secret-value\n</CUSTOM_SECRETS>",+ },+ },+ ];+ const result = adaptSystemMessage(events);+ expect(result?.content).not.toContain("super-secret-value");+ expect(result?.content).toContain("MY_API_KEY=<secret-hidden>");+ });});diff --git a/src/components/features/conversation-panel/system-message-modal/tab-content.tsx b/src/components/features/conversation-panel/system-message-modal/tab-content.tsxindex fba2bb849c..9b32eb9ced 100644--- a/src/components/features/conversation-panel/system-message-modal/tab-content.tsx+++ b/src/components/features/conversation-panel/system-message-modal/tab-content.tsx@@ -1,14 +1,11 @@import { SystemMessageContent } from "./system-message-content";import { ToolsList } from "./tools-list";import { EmptyToolsState } from "./empty-tools-state";-import { ChatCompletionToolParam } from "#/types/agent-server/core";+import { SystemMessageForModal } from "#/utils/system-message-adapter";interface TabContentProps {activeTab: "system" | "tools";- systemMessage: {- content: string;- tools: Array<Record<string, unknown>> | ChatCompletionToolParam[] | null;- };+ systemMessage: SystemMessageForModal;expandedTools: Record<number, boolean>;onToggleTool: (index: number) => void;}diff --git a/src/types/agent-server/core/events/system-event.ts b/src/types/agent-server/core/events/system-event.tsindex a4a7953281..c2d49f822f 100644--- a/src/types/agent-server/core/events/system-event.ts+++ b/src/types/agent-server/core/events/system-event.ts@@ -17,4 +17,10 @@ export interface SystemPromptEvent extends BaseEvent {* List of tools in OpenAI tool format*/tools: ChatCompletionToolParam[];++ /**+ * Runtime-injected context appended to the system message (datetime, skills,+ * runtime services, secrets). Optional for older persisted events.+ */+ dynamic_context?: TextContent;}diff --git a/src/utils/redact-custom-secrets.ts b/src/utils/redact-custom-secrets.tsnew file mode 100644index 0000000000..32720852b7--- /dev/null+++ b/src/utils/redact-custom-secrets.ts@@ -0,0 +1,32 @@+const MASKED_PLACEHOLDER = "<secret-hidden>";++/**+ * Defensive backstop: redact any unmasked value inside a `<CUSTOM_SECRETS>`+ * block before showing dynamic context in the UI, in case backend masking+ * regresses. Text outside the block is untouched.+ */+export function redactCustomSecrets(text: string): string {+ // Regexes are local: the `g` flag makes them stateful, and keeping them+ // scoped to this call avoids cross-call `lastIndex` surprises.++ // Closing tag is optional so a truncated block is still redacted, not leaked.+ const customSecretsBlock =+ /(<CUSTOM_SECRETS>)([\s\S]*?)(<\/CUSTOM_SECRETS>|$)/gi;++ // `KEY: value` / `KEY=value`, capturing key + separator so only the value changes.+ const secretLine = /^(\s*[^=:\n]+?\s*[:=]\s*)(.+?)\s*$/gm;++ return text.replace(+ customSecretsBlock,+ (_match, open: string, body: string, close: string) => {+ const redactedBody = body.replace(+ secretLine,+ (lineMatch, prefix: string, value: string) =>+ value === MASKED_PLACEHOLDER+ ? lineMatch+ : `${prefix}${MASKED_PLACEHOLDER}`,+ );+ return `${open}${redactedBody}${close}`;+ },+ );+}diff --git a/src/utils/system-message-adapter.ts b/src/utils/system-message-adapter.tsindex d7f912524b..5d81f75883 100644--- a/src/utils/system-message-adapter.ts+++ b/src/utils/system-message-adapter.ts@@ -1,6 +1,7 @@import { OHEvent } from "#/stores/use-event-store";import { ChatCompletionToolParam } from "#/types/agent-server/core";import { isSystemPromptEvent } from "#/types/agent-server/type-guards";+import { redactCustomSecrets } from "#/utils/redact-custom-secrets";export interface SystemMessageForModal {content: string;@@ -18,8 +19,15 @@ export function adaptSystemMessage(return null;}+ // dynamic_context is the runtime-injected tail of the same system prompt the+ // model receives, so append it to show the full message as one block.+ const dynamicContextText = systemPromptEvent.dynamic_context?.text;+ const content = dynamicContextText+ ? `${systemPromptEvent.system_prompt.text.trimEnd()}\n\n${redactCustomSecrets(dynamicContextText)}`+ : systemPromptEvent.system_prompt.text;+return {- content: systemPromptEvent.system_prompt.text,+ content,tools: systemPromptEvent.tools ?? null,openhands_version: null,agent_class: null,