@@ -10,6 +10,7 @@ import {
1010 mintSecretSentinel ,
1111 resolveSecretSentinel ,
1212} from "../secrets/sentinel.js" ;
13+ import type { AgentHarnessHostCapabilities } from "./harness/host-capability-types.js" ;
1314import type { AgentHarness } from "./harness/types.js" ;
1415import type { AgentRuntimeAuthPlan } from "./runtime-plan/types.js" ;
1516
@@ -43,7 +44,20 @@ const executePreparedCliRunMock = vi.fn();
4344const diagDebugMock = vi . fn ( ) ;
4445const ensureSelectedAgentHarnessPluginMock = vi . fn ( ) ;
4546const createAgentHarnessHostCapabilitiesMock = vi . fn ( ) ;
47+ const closeAgentHarnessHostCapabilitiesMock = vi . fn ( ) ;
48+ const agentHarnessHostCapabilitiesMock : AgentHarnessHostCapabilities = Object . freeze ( {
49+ kind : "agent-harness-host-capability" ,
50+ version : 1 ,
51+ assertActive : vi . fn ( ) ,
52+ bindToolSurface : vi . fn ( ( tools ) => tools ) ,
53+ runBeforeToolCall : vi . fn ( ) ,
54+ requestApproval : vi . fn ( ) ,
55+ waitForApproval : vi . fn ( ) ,
56+ } ) ;
57+ const listSessionEntriesCoreMock = vi . fn ( ) ;
58+ const loadSessionEntryMock = vi . fn ( ) ;
4659const loadTranscriptEventsMock = vi . fn ( ) ;
60+ const builtInOpenClawHarnesses = new WeakSet < object > ( ) ;
4761const shouldPreferExplicitConfigApiKeyAuthMock = vi . fn ( ( ..._args : unknown [ ] ) => false ) ;
4862const hasUsableCustomProviderApiKeyMock = vi . fn ( ( ..._args : unknown [ ] ) => false ) ;
4963const resolveProviderEntryApiKeyProfileReferenceMock = vi . fn ( ( _params ?: unknown ) : unknown => ( {
@@ -204,17 +218,30 @@ vi.mock("./harness/runtime-plugin.js", () => ({
204218 ensureSelectedAgentHarnessPluginMock ( ...args ) ,
205219} ) ) ;
206220
207- vi . mock ( "./harness/host-capability.js" , async ( ) => {
208- const actual = await vi . importActual < typeof import ( "./harness/host-capability.js" ) > (
209- "./harness/host-capability.js" ,
210- ) ;
221+ // Selection and host-capability owner suites execute the embedded runner and capability surface.
222+ // BTW only needs their identities while it verifies side-question orchestration.
223+ vi . mock ( "./harness/builtin-openclaw.js" , ( ) => ( {
224+ createOpenClawAgentHarness : ( ) : AgentHarness => {
225+ const harness : AgentHarness = {
226+ id : "openclaw" ,
227+ label : "OpenClaw embedded agent" ,
228+ supports : ( ) => ( { supported : true , priority : 0 } ) ,
229+ runAttempt : vi . fn ( ) ,
230+ } ;
231+ builtInOpenClawHarnesses . add ( harness ) ;
232+ return harness ;
233+ } ,
234+ isBuiltInOpenClawAgentHarness : ( harness : AgentHarness ) => builtInOpenClawHarnesses . has ( harness ) ,
235+ } ) ) ;
236+
237+ vi . mock ( "./harness/host-capability.js" , ( ) => {
211238 return {
212- ...actual ,
213- createAgentHarnessHostCapabilities : (
214- params : Parameters < typeof actual . createAgentHarnessHostCapabilities > [ 0 ] ,
215- ) => {
239+ createAgentHarnessHostCapabilities : ( params : unknown ) => {
216240 createAgentHarnessHostCapabilitiesMock ( params ) ;
217- return actual . createAgentHarnessHostCapabilities ( params ) ;
241+ return {
242+ capabilities : agentHarnessHostCapabilitiesMock ,
243+ close : closeAgentHarnessHostCapabilitiesMock ,
244+ } ;
218245 } ,
219246 } ;
220247} ) ;
@@ -303,15 +330,11 @@ vi.mock("../logging/diagnostic.js", () => ({
303330 } ,
304331} ) ) ;
305332
306- vi . mock ( "../config/sessions/session-accessor.js" , async ( ) => {
307- const actual = await vi . importActual < typeof import ( "../config/sessions/session-accessor.js" ) > (
308- "../config/sessions/session-accessor.js" ,
309- ) ;
310- return {
311- ...actual ,
312- loadTranscriptEvents : ( ...args : unknown [ ] ) => loadTranscriptEventsMock ( ...args ) ,
313- } ;
314- } ) ;
333+ vi . mock ( "../config/sessions/session-accessor.js" , ( ) => ( {
334+ listSessionEntriesCore : ( ...args : unknown [ ] ) => listSessionEntriesCoreMock ( ...args ) ,
335+ loadSessionEntry : ( ...args : unknown [ ] ) => loadSessionEntryMock ( ...args ) ,
336+ loadTranscriptEvents : ( ...args : unknown [ ] ) => loadTranscriptEventsMock ( ...args ) ,
337+ } ) ) ;
315338
316339const { runBtwSideQuestion } = await import ( "./btw.js" ) ;
317340const { clearAgentHarnesses, registerAgentHarness } = await import ( "./harness/registry.js" ) ;
@@ -673,6 +696,11 @@ describe("runBtwSideQuestion", () => {
673696 diagDebugMock . mockReset ( ) ;
674697 ensureSelectedAgentHarnessPluginMock . mockReset ( ) ;
675698 createAgentHarnessHostCapabilitiesMock . mockReset ( ) ;
699+ closeAgentHarnessHostCapabilitiesMock . mockReset ( ) ;
700+ listSessionEntriesCoreMock . mockReset ( ) ;
701+ listSessionEntriesCoreMock . mockReturnValue ( [ ] ) ;
702+ loadSessionEntryMock . mockReset ( ) ;
703+ loadSessionEntryMock . mockReturnValue ( undefined ) ;
676704 loadTranscriptEventsMock . mockReset ( ) ;
677705 shouldPreferExplicitConfigApiKeyAuthMock . mockReset ( ) ;
678706 shouldPreferExplicitConfigApiKeyAuthMock . mockReturnValue ( false ) ;
@@ -938,11 +966,21 @@ describe("runBtwSideQuestion", () => {
938966 } ) ,
939967 } ) ,
940968 ) ;
969+ expect ( mockArg ( codexSideQuestionMock , 0 , 0 ) ) . toHaveProperty (
970+ "hostCapabilities" ,
971+ agentHarnessHostCapabilitiesMock ,
972+ ) ;
941973 expect ( createAgentHarnessHostCapabilitiesMock ) . toHaveBeenCalledWith (
942974 expect . objectContaining ( {
943- attempt : expect . objectContaining ( { runId : "btw-side-authority" } ) ,
975+ attempt : expect . objectContaining ( {
976+ admittedRunContext : expect . objectContaining ( {
977+ operationalRunInstance : expect . objectContaining ( { runId : "btw-side-authority" } ) ,
978+ } ) ,
979+ runId : "btw-side-authority" ,
980+ } ) ,
944981 } ) ,
945982 ) ;
983+ expect ( closeAgentHarnessHostCapabilitiesMock ) . toHaveBeenCalledOnce ( ) ;
946984 expect ( resolveModelAsyncMock ) . toHaveBeenCalledWith (
947985 "openai" ,
948986 "gpt-5.5" ,
@@ -1246,24 +1284,25 @@ describe("runBtwSideQuestion", () => {
12461284 ) ;
12471285 } ) ;
12481286
1249- it ( "uses registry ownership rather than declared harness metadata for BTW approvals " , async ( ) => {
1287+ it ( "uses registry ownership and closes host capabilities when a BTW hook rejects " , async ( ) => {
12501288 registerAgentHarness (
12511289 {
12521290 id : "spoofed" ,
12531291 label : "Spoofed BTW harness" ,
12541292 pluginId : "codex" ,
12551293 supports : ( ) => ( { supported : true , priority : 100 } ) ,
12561294 runAttempt : vi . fn ( ) ,
1257- runSideQuestion : vi . fn ( ) . mockResolvedValue ( { text : "Registry-owned answer." } ) ,
1295+ runSideQuestion : vi . fn ( ) . mockRejectedValue ( new Error ( "side question failed" ) ) ,
12581296 } ,
12591297 { ownerPluginId : "actual-owner" } ,
12601298 ) ;
12611299
1262- await expect ( runSideQuestion ( ) ) . resolves . toEqual ( { text : "Registry-owned answer." } ) ;
1300+ await expect ( runSideQuestion ( ) ) . rejects . toThrow ( "side question failed" ) ;
12631301
12641302 expect ( createAgentHarnessHostCapabilitiesMock ) . toHaveBeenCalledWith (
12651303 expect . objectContaining ( { pluginId : "actual-owner" } ) ,
12661304 ) ;
1305+ expect ( closeAgentHarnessHostCapabilitiesMock ) . toHaveBeenCalledOnce ( ) ;
12671306 } ) ;
12681307
12691308 it ( "reselects the Codex hook after resolving legacy openai-codex route state" , async ( ) => {
@@ -1407,34 +1446,6 @@ describe("runBtwSideQuestion", () => {
14071446 expect ( mockArg ( codexSideQuestionMock , 0 , 0 ) ) . toMatchObject ( { toolsAllow : [ ] } ) ;
14081447 } ) ;
14091448
1410- it ( "prepares a narrow global policy before calling a plugin side-question hook" , async ( ) => {
1411- const codexSideQuestionMock = registerCodexSideQuestionHarness ( ) ;
1412- mockOpenAIPlatformProfile ( ) ;
1413- resolveModelWithRegistryMock . mockReturnValue ( {
1414- provider : "openai" ,
1415- id : "gpt-5.5" ,
1416- api : "openai-responses" ,
1417- } ) ;
1418- resolveModelAsyncMock . mockResolvedValue ( {
1419- model : {
1420- provider : "openai" ,
1421- id : "gpt-5.5" ,
1422- api : "openai-responses" ,
1423- baseUrl : "https://api.openai.com/v1" ,
1424- } ,
1425- } ) ;
1426-
1427- await runSideQuestion ( {
1428- cfg : { tools : { allow : [ "message" ] } } as never ,
1429- provider : "openai" ,
1430- model : "gpt-5.5" ,
1431- sessionKey : DEFAULT_SESSION_KEY ,
1432- } ) ;
1433-
1434- expect ( codexSideQuestionMock ) . toHaveBeenCalledOnce ( ) ;
1435- expect ( mockArg ( codexSideQuestionMock , 0 , 0 ) ) . toMatchObject ( { toolsAllow : [ ] } ) ;
1436- } ) ;
1437-
14381449 it ( "does not fall back to the direct provider call when Codex lacks BTW support" , async ( ) => {
14391450 registerAgentHarness ( {
14401451 id : "codex" ,
@@ -2518,6 +2529,37 @@ describe("runBtwSideQuestion", () => {
25182529 expect ( buildSessionContextMock ) . toHaveBeenCalledWith ( [ userEntry , assistantEntry ] ) ;
25192530 } ) ;
25202531
2532+ it ( "rejects a supplied session key that disagrees with an incomplete SQLite marker target" , async ( ) => {
2533+ const markerStorePath = "/tmp/marker-sessions.sqlite" ;
2534+ listSessionEntriesCoreMock . mockReturnValue ( [
2535+ {
2536+ sessionKey : "agent:main:matching" ,
2537+ entry : createSessionEntry ( ) ,
2538+ } ,
2539+ ] ) ;
2540+ loadSessionEntryMock . mockReturnValue ( createSessionEntry ( { sessionId : "different-session" } ) ) ;
2541+
2542+ await expect (
2543+ runMathSideQuestion ( {
2544+ sessionEntry : createSessionEntry ( {
2545+ sessionFile : `sqlite:main:session-1:${ markerStorePath } ` ,
2546+ } ) ,
2547+ storePath : undefined ,
2548+ } ) ,
2549+ ) . rejects . toThrow ( "No active session context." ) ;
2550+
2551+ expect ( listSessionEntriesCoreMock ) . toHaveBeenCalledWith ( {
2552+ agentId : "main" ,
2553+ storePath : markerStorePath ,
2554+ } ) ;
2555+ expect ( loadSessionEntryMock ) . toHaveBeenCalledWith ( {
2556+ agentId : "main" ,
2557+ sessionKey : DEFAULT_SESSION_KEY ,
2558+ storePath : markerStorePath ,
2559+ } ) ;
2560+ expect ( loadTranscriptEventsMock ) . not . toHaveBeenCalled ( ) ;
2561+ } ) ;
2562+
25212563 it ( "falls back when the active run snapshot leaf no longer exists" , async ( ) => {
25222564 const userEntry = createTranscriptEntry ( {
25232565 id : "user-seed" ,
0 commit comments