Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

fix(pixel): keep execution guidance out of owner replies (#8296)

Mike Bradley5d4e5ad3760c
diff --git a/.github/workflows/portal-runtime.yml b/.github/workflows/portal-runtime.yml
index a5dc8cf2d..86a86e416 100644
--- a/.github/workflows/portal-runtime.yml
+++ b/.github/workflows/portal-runtime.yml
@@ -148,7 +148,7 @@ jobs:
shell: bash
env:
OPENCLAW_PACKAGE: ${{ runner.temp }}/read-runtime/node_modules/openclaw
- run: node --test ods/extensions/services/pixel-agent/tests/runtime_exec_completion.integration.mjs
+ run: node --test ods/extensions/services/pixel-agent/tests/runtime_exec_completion.integration.mjs ods/extensions/services/pixel-agent/tests/runtime_exec_guidance_history.integration.mjs
- name: Verify every Pixel chat reaches the real provider with one system prompt
if: runner.os != 'Windows'
shell: bash
diff --git a/ods/extensions/services/pixel-agent/plugin/exec-guidance-provenance.mjs b/ods/extensions/services/pixel-agent/plugin/exec-guidance-provenance.mjs
new file mode 100644
index 000000000..08f5e01df
--- /dev/null
+++ b/ods/extensions/services/pixel-agent/plugin/exec-guidance-provenance.mjs
@@ -0,0 +1,52 @@
+// The native SDK retains content blocks but omits historical result details.
+// Recognize only the separate completion block appended to a paired exec
+// result; command output and owner quotations remain ordinary content.
+const COMPLETION_NOTE = /^\[ODS Pixel execution\] Exec returned completed with exit code -?\d+\. This result has no background session ID\. Use the returned output; do not invent a session ID or poll a PID\.$/;
+
+export function leakedExecGuidance(reply, messages, ownerText = '', currentReceipts = []) {
+ if (typeof reply !== 'string' || !Array.isArray(messages)) return undefined;
+ if (ownerText.includes('[ODS Pixel execution]') ||
+ /\b(?:quote|explain|describe|show|repeat)\b.{0,100}\b(?:host|internal|execution)\b.{0,40}\b(?:note|guidance)\b/i.test(ownerText)) return undefined;
+ const calls = new Set();
+ const current = new Map(currentReceipts.map(receipt => [receipt.toolCallId,receipt]));
+ const notes = [];
+ const outputs = [];
+ for (const message of messages) {
+ if (!Array.isArray(message?.content)) continue;
+ if (message.role === 'assistant') {
+ for (const block of message.content) {
+ if (block?.type === 'toolCall' && block.name === 'exec' &&
+ typeof block.id === 'string' && typeof block.arguments?.command === 'string') calls.add(block.id);
+ }
+ continue;
+ }
+ if (message.role !== 'toolResult' || message.toolName !== 'exec' ||
+ !calls.delete(message.toolCallId)) continue;
+ const blocks = message.content;
+ const last = blocks.at(-1);
+ const annotated = blocks.length >= 2 && last?.type === 'text' && COMPLETION_NOTE.test(last.text);
+ const output = (annotated ? blocks.slice(0, -1) : blocks)
+ .filter(block => block?.type === 'text').map(block => block.text).join('\n');
+ outputs.push(output);
+ if (annotated) notes.push({note:last.text, toolCallId:message.toolCallId});
+ // During this run the SDK's final hook sees the original tool message,
+ // before tool_result_persist adds guidance. Only our bound persist receipt
+ // can establish which separate note the model received in that case.
+ const receipt=current.get(message.toolCallId);
+ if (!annotated && receipt?.output === output && COMPLETION_NOTE.test(receipt.note))
+ notes.push({note:receipt.note,toolCallId:message.toolCallId});
+ }
+ return notes.find(({note}) => reply.includes(note) &&
+ !outputs.some(output => output.includes(note)));
+}
+
+export function execGuidanceRevisionInstruction({note}) {
+ return 'Remove this separate internal note from your answer: ' + JSON.stringify(note) +
+ ' Preserve the actual stdout. Answer the original question using the existing result. ' +
+ 'Do not call tools or repeat completed work.';
+}
+
+export const EXEC_GUIDANCE_LEAK_REASON = 'The answer copied host execution guidance into command output.';
+export const EXEC_GUIDANCE_LEAK_DELIVERY =
+ 'Portal could not separate its internal execution guidance from the answer. ' +
+ 'The original command output remains in the conversation; no command was repeated to repair this reply.';
diff --git a/ods/extensions/services/pixel-agent/plugin/tool-loop-guard.mjs b/ods/extensions/services/pixel-agent/plugin/tool-loop-guard.mjs
index 88f2cea14..26c642cd0 100644
--- a/ods/extensions/services/pixel-agent/plugin/tool-loop-guard.mjs
+++ b/ods/extensions/services/pixel-agent/plugin/tool-loop-guard.mjs
@@ -40,6 +40,7 @@ import { STOP_SYNTHESIS_LIMITS, STOP_SYNTHESIS_NOTE, synthesisAnswer, synthesisR
import { createRunDeadline, runDeadlineReached, runDeadlineStopText, takeRunDeadlineNotice } from "./run-deadline.mjs";
import { outputLimitContext, outputLimitCut, outputLimitNotice, outputLimitStopText } from "./output-limit.mjs";
import { OWNER_VISIBLE_REPLY_INSTRUCTION, OWNER_VISIBLE_REPLY_REASON, ownerInteractiveTurn, silentReplyText } from "./owner-visible-reply.mjs";
+import { leakedExecGuidance, execGuidanceRevisionInstruction, EXEC_GUIDANCE_LEAK_REASON, EXEC_GUIDANCE_LEAK_DELIVERY } from "./exec-guidance-provenance.mjs";
import { canonicalWorkspaceParams, extensionlessHtmlWrite, workspaceFileParent, nativeExecWorkdir, sandboxHostWorkspaceFailure, malformedRelativeWorkspacePath } from "./workspace-path-contract.mjs";
import { routePlaygroundTool, requestsNewPlaygroundProject } from "./playground-projects.mjs";
import { createExampleGuard, EXAMPLE_NOTES, exampleNotice, mentionsExample, namesExample } from "./example-guard.mjs";
@@ -8710,6 +8711,7 @@ export function createToolLoopGuard({
blockReason: "Pixel could not recover the owner's request for this subagent continuation. Start a fresh owner message; child results cannot authorize tools.",
};
if (state?.ownerNoTools) return {block:true, blockReason:OWNER_NO_TOOLS_REASON};
+ if (state?.execGuidanceLeakRetried) return {block:true, blockReason:"Correct the answer from existing output without calling tools or repeating completed work."};
// After compaction the pixels may have left the context; re-reading is then legitimate.
if (state?.attachedPictureIds && !state.compactionSeen && namesAttachedPicture(event?.params, state.attachedPictureIds)) {
return {block:true, blockReason:ATTACHED_PICTURE_IN_CONTEXT_REASON};
@@ -10812,6 +10814,9 @@ export function createToolLoopGuard({
// A broker response reserved by the ended attempt must not attach to
// its successor. Each admitted call has its own private scope object.
state.artifactReservations = new WeakSet();
+ state.execGuidanceLeakRetried = false;
+ state.execGuidanceLeakPending = false;
+ state.execGuidanceReceipts = new Map();
}
// Page receipts cross turns only within this trusted owner chat. The
// assurance object freezes the first owner intent and receipt selection.
@@ -13408,8 +13413,14 @@ export function createToolLoopGuard({
}
}
if (executionGuidance && !pending.pythonSyntaxGuidance && !content.some(block =>
- block?.type === 'text' && /\[ODS Pixel execution\]/.test(block.text)))
+ block?.type === 'text' && /\[ODS Pixel execution\]/.test(block.text))) {
+ if (pending.transport === 'exec') {
+ state.execGuidanceReceipts ??= new Map();
+ state.execGuidanceReceipts.set(toolCallId,{toolCallId,note:executionGuidance,
+ output:messageContentText(syntaxReceipt.content)});
+ }
content.push({type:'text',text:executionGuidance});
+ }
if (redirectOrderNote && !content.some(block => block?.type === 'text' && block.text === redirectOrderNote))
content.push({type:'text',text:redirectOrderNote});
if (unittestRecoveryInstruction && !content.some(block => block?.type === 'text' && block.text === unittestRecoveryInstruction))
@@ -13886,6 +13897,29 @@ export function createToolLoopGuard({
return {action: 'revise', reason: OWNER_VISIBLE_REPLY_REASON, retry: {
instruction: OWNER_VISIBLE_REPLY_INSTRUCTION, idempotencyKey: 'ods-owner-visible-reply', maxAttempts: 1}};
}
+ // Rehydrated exec history preserves the host-added text block, but loses
+ // result details. Bind the correction to the same owner/run and the exact
+ // paired native result. Arm delivery failure before requesting revision:
+ // the runtime can refuse revision after side effects or ignore it.
+ if (state?.ownerIntentObserved && !state.managedTeamWorker &&
+ !state.artifactIdentityConflict && !state.artifactNoninteractiveObserved &&
+ ownerInteractiveTurn(context, agentId) && !state.odsAborted &&
+ !state.runDeadlineReached && typeof state.finalReplyText === 'string' &&
+ [context, event].every(value =>
+ (!value?.runId || value.runId === runId) &&
+ (!value?.sessionId || value.sessionId === state.currentSessionId) &&
+ (!value?.sessionKey || value.sessionKey === state.currentSessionKey))) {
+ const leak = leakedExecGuidance(state.finalReplyText, event?.messages, state.ownerRequestText ?? '',
+ [...(state.execGuidanceReceipts?.values() ?? [])]);
+ state.execGuidanceLeakPending = Boolean(leak);
+ if (leak && !state.execGuidanceLeakRetried) {
+ state.execGuidanceLeakRetried = true;
+ return {action:'revise', reason:EXEC_GUIDANCE_LEAK_REASON, retry:{
+ instruction:execGuidanceRevisionInstruction(leak),
+ idempotencyKey:'pixel-ods-exec-guidance-leak', maxAttempts:1}};
+ }
+ if (leak) return {action:'finalize', reason:EXEC_GUIDANCE_LEAK_REASON};
+ }
const extensionStopped = state?.progressBudget.laneExhausted('extension');
const workspaceStopped = state?.progressBudget.laneExhausted('workspace');
const continuation =
@@ -14485,6 +14519,9 @@ export function createToolLoopGuard({
return {status:'failed', text:'ODS did not observe a verified managed installation receipt for this GitHub extension request.'};
}
if (state?.ownerQuestions && !state.clientCancelled) return {status:'pending',text:questionsText(state.ownerQuestions),questions:state.ownerQuestions};
+ if (state?.execGuidanceLeakPending && !state.clientCancelled && !state.runDeadlineReached && !state.progressBudget.exhausted) {
+ return {status:'failed', text:EXEC_GUIDANCE_LEAK_DELIVERY};
+ }
// Replaces the runtime's advice to raise agents.defaults.timeoutSeconds,
// which ODS resets. Failure facts, such as a failed test, follow.
if (state?.runDeadlineReached && !state.clientCancelled) {
diff --git a/ods/extensions/services/pixel-agent/tests/exec_guidance_provenance.test.mjs b/ods/extensions/services/pixel-agent/tests/exec_guidance_provenance.test.mjs
new file mode 100644
index 000000000..ba8d19681
--- /dev/null
+++ b/ods/extensions/services/pixel-agent/tests/exec_guidance_provenance.test.mjs
@@ -0,0 +1,120 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import {createToolLoopGuard} from '../plugin/tool-loop-guard.mjs';
+import {leakedExecGuidance, EXEC_GUIDANCE_LEAK_DELIVERY} from '../plugin/exec-guidance-provenance.mjs';
+
+const note = '[ODS Pixel execution] Exec returned completed with exit code 0. This result has no background session ID. Use the returned output; do not invent a session ID or poll a PID.';
+const owner = 'Without using tools, what exact stdout did the last terminal command return? Answer with just that number.';
+const text = value => [{type:'text',text:value}];
+function history(stdout = '2537') {
+ return [
+ {role:'user', content:text('Use the terminal tool to execute printf 2537 and report only the observed stdout.')},
+ {role:'assistant', content:[{type:'toolCall',id:'exec0',name:'exec',arguments:{command:'printf 2537'}}]},
+ {role:'toolResult',toolName:'exec',toolCallId:'exec0',content:[...text(stdout),...text(note)]},
+ {role:'assistant',content:text(stdout)},
+ ];
+}
+function fixture(prompt = owner, prior = history(), runId = 'recall-run') {
+ const guard = createToolLoopGuard();
+ const context = {agentId:'pixel',trigger:'user',runId,sessionId:'session-1',
+ sessionKey:'agent:pixel:openai-user:ods-'+'a'.repeat(64)};
+ guard.observeRun(context,'pixel',{prompt,messages:prior});
+ const event = reply => ({...context,lastAssistantMessage:reply,
+ messages:[...prior,{role:'user',content:text(prompt)},{role:'assistant',content:text(reply)}]});
+ return {guard,context,event};
+}
+
+test('historical native exec guidance revises once; clean revision passes and tools cannot replay', () => {
+ const {guard,context,event}=fixture();
+ const decision=guard.beforeAgentFinalize(event('2537\n'+note),context);
+ assert.equal(decision?.action,'revise');
+ assert.equal(decision.retry.maxAttempts,1);
+ assert.match(decision.retry.instruction,/Do not call tools/);
+ assert.equal(guard.deliveryVerificationForRun(context.runId).status,'failed');
+ assert.equal(guard.beforeToolCall({toolName:'exec',params:{command:'printf 2537'}},{...context,toolName:'exec',toolCallId:'replay'})?.block,true);
+ assert.equal(guard.beforeAgentFinalize(event('2537'),context),undefined);
+ assert.notEqual(guard.deliveryVerificationForRun(context.runId).status,'failed');
+});
+
+test('ignored or refused revision fails at the owner delivery boundary', () => {
+ const {guard,context,event}=fixture();
+ guard.beforeAgentFinalize(event('2537\n'+note),context);
+ // Even if the SDK refuses a revision, the first final is already unverified.
+ assert.equal(guard.replyPayloadSending({runId:context.runId,kind:'final',payload:{text:'2537\n'+note}})?.payload.text,EXEC_GUIDANCE_LEAK_DELIVERY);
+ assert.equal(guard.beforeAgentFinalize(event('2537\n'+note),context)?.action,'finalize');
+ assert.equal(guard.deliveryVerificationForRun(context.runId).text,EXEC_GUIDANCE_LEAK_DELIVERY);
+});
+
+test('another owner run has its own correction budget', () => {
+ const {guard,context,event}=fixture();
+ guard.beforeAgentFinalize(event('2537\n'+note),context);
+ const next={...context,runId:'next-run'};
+ guard.observeRun(next,'pixel',{prompt:owner,messages:history()});
+ assert.equal(guard.beforeAgentFinalize({...event('2537\n'+note),runId:next.runId},next)?.action,'revise');
+});
+
+test('ordinary stdout and deliberate quotations are preserved', () => {
+ for (const [prompt,prior,reply] of [
+ [owner,history(),'2537'],
+ [owner,history('literal '+note),'literal '+note],
+ ['Repeat exactly: '+note,history(),note],
+ ['Explain the internal execution guidance',history(),note],
+ ]) {
+ const {guard,context,event}=fixture(prompt,prior);
+ assert.equal(guard.beforeAgentFinalize(event(reply),context),undefined);
+ assert.notEqual(guard.deliveryVerificationForRun(context.runId).status,'failed');
+ }
+});
+
+test('native result pairing and block boundaries are required', () => {
+ const variants=[
+ h=>{h[1].content[0].name='read';},
+ h=>{h[2].toolCallId='unpaired';},
+ h=>{h[2].content=text('2537\n'+note);},
+ h=>{h[2].content[1].text+=' altered';},
+ h=>{[h[1],h[2]]=[h[2],h[1]];},
+ ];
+ for(const mutate of variants) {
+ const h=history();mutate(h);
+ assert.equal(leakedExecGuidance('2537\n'+note,h,owner),undefined);
+ }
+});
+
+test('a final hook with another run or session cannot arm a correction', () => {
+ for(const key of ['runId','sessionId','sessionKey']) {
+ const {guard,context,event}=fixture();
+ assert.equal(guard.beforeAgentFinalize({...event('2537\n'+note),[key]:'other'},context),undefined);
+ assert.notEqual(guard.deliveryVerificationForRun(context.runId).status,'failed');
+ }
+});
+
+test('a fresh SDK-shaped history with no result details still identifies the note', () => {
+ const h=history();h[2].details=null;
+ assert.equal(leakedExecGuidance('2537\n'+note,h,owner)?.toolCallId,'exec0');
+ h[2].content.unshift(...text('earlier stdout block'));
+ assert.equal(leakedExecGuidance('2537\n'+note,h,owner)?.note,note);
+});
+
+test('the copied block is detected before or after the answer', () => {
+ for (const reply of [note+'\n2537','2537\n'+note+'\nThat was the previous result.']) {
+ assert.equal(leakedExecGuidance(reply,history(),owner)?.note,note);
+ }
+});
+
+test('failure delivery leaves the historical stdout and owner text unchanged', () => {
+ const {guard,context,event}=fixture();
+ const final=event('2537\n'+note), before=structuredClone(final);
+ guard.beforeAgentFinalize(final,context);
+ guard.replyPayloadSending({runId:context.runId,kind:'final',payload:{text:final.lastAssistantMessage}});
+ assert.deepEqual(final,before);
+ assert.equal(final.messages[2].content[0].text,'2537');
+});
+
+test('another run may still execute tools after the earlier correction', () => {
+ const {guard,context,event}=fixture();
+ guard.beforeAgentFinalize(event('2537\n'+note),context);
+ const next={...context,runId:'independent-run'};
+ guard.observeRun(next,'pixel',{prompt:'Use the terminal tool to execute printf 11.',messages:history()});
+ const result=guard.beforeToolCall({toolName:'exec',params:{command:'printf 11'}},{...next,toolName:'exec',toolCallId:'independent-exec'});
+ assert.notEqual(result?.block,true,result?.blockReason);
+});
diff --git a/ods/extensions/services/pixel-agent/tests/runtime_exec_guidance_history.integration.mjs b/ods/extensions/services/pixel-agent/tests/runtime_exec_guidance_history.integration.mjs
new file mode 100644
index 000000000..ef0fd1277
--- /dev/null
+++ b/ods/extensions/services/pixel-agent/tests/runtime_exec_guidance_history.integration.mjs
@@ -0,0 +1,151 @@
+// Exercise the pinned SDK's historical message projection, bounded revision,
+// side-effect boundary and final payload hook with a local deterministic model.
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import {createServer} from 'node:http';
+import {once} from 'node:events';
+import {spawn} from 'node:child_process';
+import {mkdtempSync,mkdirSync,writeFileSync,symlinkSync,readFileSync,rmSync} from 'node:fs';
+import {tmpdir} from 'node:os';
+import {join} from 'node:path';
+import {setTimeout as delay} from 'node:timers/promises';
+import {EXEC_GUIDANCE_LEAK_DELIVERY} from '../plugin/exec-guidance-provenance.mjs';
+import {createIngressServer} from '../host/pixel_ingress.mjs';
+
+const pkg=process.env.OPENCLAW_PACKAGE;
+const NOTE='[ODS Pixel execution] Exec returned completed with exit code 0. This result has no background session ID. Use the returned output; do not invent a session ID or poll a PID.';
+const shellQuote=value=>"'"+value.replaceAll("'","'\\''")+"'";
+const readEvents=path=>readFileSync(path,'utf8').trim().split('\n').filter(Boolean).map(JSON.parse);
+function replyText(body) {
+ return body.split(/\r?\n/).filter(line=>line.startsWith('data: {'))
+ .map(line=>JSON.parse(line.slice(6)).choices?.[0]?.delta?.content??'').join('');
+}
+
+for(const mode of ['corrected','ignored','replay','literal','owner-quote','current-run']) {
+ test(`real SDK exec guidance: ${mode}`,{skip:!pkg||process.platform==='win32',timeout:120000},async()=>{
+ assert.equal(JSON.parse(readFileSync(join(pkg,'package.json'),'utf8')).version,'2026.6.33');
+ const root=mkdtempSync(join(tmpdir(),'ods-exec-history-'));
+ const workspace=join(root,'workspace');mkdirSync(workspace);
+ const countPath=join(workspace,'exec-count'),eventsPath=join(root,'events.jsonl');
+ const stdout=mode==='literal'?'2537\n'+NOTE:'2537';
+ const program=`require('node:fs').appendFileSync(${JSON.stringify(countPath)},'x');process.stdout.write(${JSON.stringify(stdout)})`;
+ const command=shellQuote(process.execPath)+' -e '+shellQuote(program);
+ let rounds=0,child,log='',upstream,ingress;
+ const requests=[];
+ try {
+ upstream=createServer(async(req,res)=>{
+ const chunks=[];for await(const chunk of req)chunks.push(chunk);
+ const body=JSON.parse(Buffer.concat(chunks).toString('utf8'));
+ requests.push(body.messages);
+ const round=rounds++;
+ const useTool=round===0||(mode==='replay'&&round===3);
+ let content=stdout;
+ if(mode==='current-run'||(mode==='ignored'&&round>=2)||round===2) content='2537\n'+NOTE;
+ if(mode==='owner-quote'&&round===2) content=NOTE;
+ const delta=useTool?{role:'assistant',tool_calls:[{index:0,id:round===0?'exec-0':'exec-1',type:'function',
+ function:{name:'exec',arguments:JSON.stringify({command})}}]}:{role:'assistant',content};
+ res.writeHead(200,{'Content-Type':'text/event-stream'});
+ res.write('data: '+JSON.stringify({id:'fixture',object:'chat.completion.chunk',choices:[{index:0,delta,finish_reason:null}]})+'\n\n');
+ res.end('data: '+JSON.stringify({id:'fixture',object:'chat.completion.chunk',choices:[{index:0,delta:{},finish_reason:useTool?'tool_calls':'stop'}]})+'\n\ndata: [DONE]\n\n');
+ });
+ await new Promise(resolve=>upstream.listen(0,'127.0.0.1',resolve));
+ const probe=createServer();await new Promise(resolve=>probe.listen(0,'127.0.0.1',resolve));
+ const port=probe.address().port;await new Promise(resolve=>probe.close(resolve));
+ mkdirSync(join(root,'node_modules'));symlinkSync(pkg,join(root,'node_modules','openclaw'));
+ const plugin=join(root,'plugin');mkdirSync(plugin);
+ writeFileSync(join(plugin,'package.json'),JSON.stringify({name:'exec-history-fixture',version:'1.0.0',type:'module',openclaw:{extensions:['./index.mjs']}}));
+ writeFileSync(join(plugin,'openclaw.plugin.json'),JSON.stringify({id:'pixel-ods',activation:{onStartup:true},configSchema:{type:'object',properties:{}}}));
+ writeFileSync(join(plugin,'index.mjs'),`
+ import {createToolLoopGuard} from ${JSON.stringify(new URL('../plugin/tool-loop-guard.mjs',import.meta.url).href)};
+ import {appendFileSync} from 'node:fs';
+ const guard=createToolLoopGuard();
+ const record=value=>appendFileSync(${JSON.stringify(eventsPath)},JSON.stringify(value)+'\\n');
+ export default {id:'pixel-ods',register(api){
+ for(const [path,project] of [
+ ['/pixel-ods/verification',body=>guard.deliveryVerificationForRun(body.runId)],
+ ['/pixel-ods/subagent-delivery',body=>({schemaVersion:1,kind:'ods-subagent-delivery',runId:body.runId,status:'not-delegated'})],
+ ]) api.registerHttpRoute({path,auth:'gateway',match:'exact',handler:async(req,res)=>{
+ let body='';for await(const part of req)body+=part;
+ res.writeHead(200,{'Content-Type':'application/json'});
+ res.end(JSON.stringify(project(JSON.parse(body))));return true;
+ }});
+ api.on('before_prompt_build',(event,ctx)=>{
+ record({hook:'prompt',runId:ctx.runId,messages:event.messages});
+ return guard.observeRun(ctx,'pixel',event,{workspaceRoot:${JSON.stringify(workspace)}});
+ });
+ api.on('before_tool_call',(event,ctx)=>{
+ const decision=guard.beforeToolCall(event,ctx);
+ record({hook:'tool',decision:decision??null});return decision;
+ });
+ api.on('after_tool_call',(event,ctx)=>guard.afterToolCall(event,ctx));
+ api.on('tool_result_persist',(event,ctx)=>guard.toolResultPersist(event,ctx));
+ api.on('before_agent_finalize',(event,ctx)=>{
+ const decision=guard.beforeAgentFinalize(event,ctx);
+ record({hook:'finalize',runId:ctx.runId,context:ctx,messages:event.messages,decision:decision??null});return decision;
+ });
+ api.on('reply_payload_sending',event=>guard.replyPayloadSending(event));
+ }};
+ `);
+ const config={logging:{file:join(root,'runtime.log')},update:{checkOnStart:false},
+ gateway:{mode:'local',bind:'loopback',port,auth:{mode:'token',token:'fixture-only'},http:{endpoints:{chatCompletions:{enabled:true}}}},
+ agents:{defaults:{workspace,skipBootstrap:true,sandbox:{mode:'off'},model:{primary:'fixture/test'},contextTokens:32768,heartbeat:{every:'0m'}},list:[{id:'pixel',default:true,workspace}]},
+ models:{mode:'replace',providers:{fixture:{baseUrl:`http://127.0.0.1:${upstream.address().port}/v1`,api:'openai-completions',apiKey:'fixture-only',
+ models:[{id:'test',name:'Fixture',contextWindow:32768,maxTokens:4096,reasoning:false,input:['text']}]}}},
+ tools:{allow:['exec'],exec:{host:'gateway',security:'full',ask:'off'}},
+ plugins:{allow:['pixel-ods'],load:{paths:[plugin]},entries:{'pixel-ods':{enabled:true,hooks:{allowConversationAccess:true}}}}};
+ writeFileSync(join(root,'openclaw.json'),JSON.stringify(config));
+ child=spawn(process.execPath,[join(pkg,'openclaw.mjs'),'gateway','run'],{cwd:root,detached:true,
+ env:{PATH:process.env.PATH,HOME:root,TMPDIR:root,OPENCLAW_STATE_DIR:join(root,'state'),OPENCLAW_CONFIG_PATH:join(root,'openclaw.json'),OPENCLAW_SKIP_CHANNELS:'1'},
+ stdio:['ignore','pipe','pipe']});
+ child.stdout.on('data',x=>log+=x);child.stderr.on('data',x=>log+=x);
+ let ready=false;
+ for(let n=0;n<250;n++) {
+ try{ready=(await fetch(`http://127.0.0.1:${port}/health`,{signal:AbortSignal.timeout(500)})).ok;}catch{}
+ if(ready)break;assert.equal(child.exitCode,null,log.slice(-6000));await delay(100);
+ }
+ assert.ok(ready,log.slice(-6000));
+ ingress=createIngressServer({token:'fixture-only',gatewayPort:port});
+ await new Promise(resolve=>ingress.listen(0,'127.0.0.1',resolve));
+ const post=async prompt=>{
+ const response=await fetch(`http://127.0.0.1:${ingress.address().port}/v1/chat/completions`,{method:'POST',
+ headers:{'Content-Type':'application/json'},
+ body:JSON.stringify({model:'openclaw:pixel',stream:true,user:'exec-history-fixture',messages:[{role:'user',content:prompt}]}),signal:AbortSignal.timeout(45000)});
+ const body=await response.text();assert.equal(response.status,200,body+'\n'+log.slice(-6000));return replyText(body);
+ };
+ const initial=await post('Use the terminal tool to print the fixture value once and report only observed stdout.');
+ let delivered=initial;
+ if(mode!=='current-run') {
+ assert.equal(initial,stdout,log.slice(-6000));
+ delivered=await post(mode==='owner-quote'?'Repeat exactly: '+NOTE:
+ (mode==='replay'?'':'Without using tools, ')+'what exact stdout did the last terminal command return? Answer with only that output.');
+ }
+ const events=readEvents(eventsPath);
+ const evidence=JSON.stringify({rounds,events,delivered})+'\n'+log.slice(-6000);
+ assert.equal(readFileSync(countPath,'utf8'),'x','native command must execute exactly once\n'+evidence);
+ assert.equal(rounds,{corrected:4,ignored:4,replay:5,literal:3,'owner-quote':3,'current-run':2}[mode],evidence);
+ if(mode==='ignored'||mode==='current-run') assert.equal(delivered,EXEC_GUIDANCE_LEAK_DELIVERY,evidence);
+ else assert.equal(delivered,mode==='owner-quote'?NOTE:stdout,evidence);
+ if(mode==='replay') assert.ok(events.some(e=>e.hook==='tool'&&e.decision?.block===true),evidence);
+ if(mode!=='current-run') {
+ const prompts=events.filter(e=>e.hook==='prompt'),runIds=[...new Set(prompts.map(e=>e.runId))];
+ assert.equal(runIds.length,2,evidence);
+ const historical=prompts.find(e=>e.runId===runIds[1]).messages.find(m=>m.role==='toolResult'&&m.toolName==='exec');
+ assert.ok(historical,evidence);
+ assert.equal(historical.details,undefined,evidence);
+ if(mode!=='literal') assert.equal(historical.content.at(-1).text,NOTE,evidence);
+ assert.equal(historical.content[0].text,stdout,evidence);
+ assert.ok(requests[2].some(m=>m.role==='tool'&&m.content.includes(NOTE)),evidence);
+ }
+ } finally {
+ if(ingress){ingress.closeAllConnections();await new Promise(resolve=>ingress.close(resolve));}
+ if(child&&child.exitCode===null) {
+ const closed=once(child,'close');
+ try{process.kill(-child.pid,'SIGTERM');}catch(error){if(error.code!=='ESRCH')throw error;}
+ await Promise.race([closed,delay(3000)]);
+ if(child.exitCode===null){try{process.kill(-child.pid,'SIGKILL');}catch(error){if(error.code!=='ESRCH')throw error;}await closed;}
+ }
+ if(upstream){upstream.closeAllConnections();await new Promise(resolve=>upstream.close(resolve));}
+ rmSync(root,{recursive:true,force:true});
+ }
+ });
+}