@@ -5,6 +5,7 @@ import { describe, expect, it, vi } from "vitest";
55import { formatSqliteSessionFileMarker } from "../config/sessions/legacy-sqlite-marker.js" ;
66import type { SessionEntry } from "../config/sessions/types.js" ;
77import type { OpenClawConfig } from "../config/types.openclaw.js" ;
8+ import { resolveIncognitoOpenClawAgentSqlitePath } from "../state/openclaw-agent-db.js" ;
89import { resolveUserPath } from "../utils.js" ;
910import { createPluginRecord } from "./loader-records.js" ;
1011import { createPluginRegistry } from "./registry.js" ;
@@ -447,6 +448,87 @@ describe("plugin registry runtime config scope", () => {
447448 ) . rejects . toThrow ( "requires exactly one runtime owner" ) ;
448449 } ) ;
449450
451+ it ( "carries incognito scope into embedded agent session identity checks" , async ( ) => {
452+ const sessionKey = "agent:researcher:dashboard:incognito-ownership-check" ;
453+ const entry = {
454+ sessionId : "incognito-session" ,
455+ updatedAt : 1 ,
456+ agentHarnessId : "test-harness" ,
457+ modelSelectionLocked : true as const ,
458+ } ;
459+ const runtime = createPluginRuntime ( ) ;
460+ runtime . agent . session . getSessionEntry = vi . fn ( ( params ) => {
461+ expect ( params ) . toEqual (
462+ params . agentId === undefined
463+ ? { sessionKey, readConsistency : "latest" }
464+ : { agentId : "main" , sessionKey, readConsistency : "latest" } ,
465+ ) ;
466+ return params . sessionKey === sessionKey ? entry : undefined ;
467+ } ) ;
468+ runtime . agent . session . listSessionEntries = vi . fn ( ( params ) => {
469+ expect ( params ) . toEqual ( {
470+ agentId : "researcher" ,
471+ readOnly : true ,
472+ storePath : resolveIncognitoOpenClawAgentSqlitePath ( { agentId : "researcher" } ) ,
473+ } ) ;
474+ return [ { sessionKey, entry } ] ;
475+ } ) ;
476+ const runEmbeddedAgent = vi . fn ( async ( ) => ( {
477+ ok : true ,
478+ } ) ) as unknown as PluginRuntime [ "agent" ] [ "runEmbeddedAgent" ] ;
479+ Object . defineProperty ( runtime . agent , "runEmbeddedAgent" , {
480+ configurable : true ,
481+ value : runEmbeddedAgent ,
482+ } ) ;
483+ const pluginRegistry = createTestRegistry ( runtime ) ;
484+ const ownerRecord = createPluginRecord ( {
485+ id : "harness-owner" ,
486+ source : "/plugins/harness-owner/index.js" ,
487+ origin : "bundled" ,
488+ enabled : true ,
489+ configSchema : false ,
490+ } ) ;
491+ const otherRecord = createPluginRecord ( {
492+ id : "other-plugin" ,
493+ source : "/plugins/other-plugin/index.js" ,
494+ origin : "bundled" ,
495+ enabled : true ,
496+ configSchema : false ,
497+ } ) ;
498+ const ownerApi = pluginRegistry . createApi ( ownerRecord , { config : { } as OpenClawConfig } ) ;
499+ const otherApi = pluginRegistry . createApi ( otherRecord , { config : { } as OpenClawConfig } ) ;
500+ ownerApi . registerAgentHarness ( {
501+ id : "test-harness" ,
502+ label : "Test Harness" ,
503+ supports : ( ) => ( { supported : true } ) ,
504+ runAttempt : async ( ) => {
505+ throw new Error ( "unused" ) ;
506+ } ,
507+ } ) ;
508+ const runParams = {
509+ sessionId : entry . sessionId ,
510+ sessionKey,
511+ workspaceDir : "/tmp" ,
512+ prompt : "continue" ,
513+ timeoutMs : 1 ,
514+ runId : "run-1" ,
515+ } as Parameters < PluginRuntime [ "agent" ] [ "runEmbeddedAgent" ] > [ 0 ] ;
516+
517+ await expect ( ownerApi . runtime . agent . runEmbeddedAgent ( runParams ) ) . resolves . toEqual ( { ok : true } ) ;
518+ expect ( runtime . agent . session . listSessionEntries ) . toHaveBeenCalledOnce ( ) ;
519+ expect ( runEmbeddedAgent ) . toHaveBeenCalledOnce ( ) ;
520+ await expect (
521+ ownerApi . runtime . agent . runEmbeddedAgent ( { ...runParams , agentId : "main" } ) ,
522+ ) . rejects . toThrow ( 'does not match session key agent "researcher"' ) ;
523+ expect ( runtime . agent . session . listSessionEntries ) . toHaveBeenCalledOnce ( ) ;
524+ expect ( runEmbeddedAgent ) . toHaveBeenCalledOnce ( ) ;
525+ await expect ( otherApi . runtime . agent . runEmbeddedAgent ( runParams ) ) . rejects . toThrow (
526+ 'owned by plugin "harness-owner"' ,
527+ ) ;
528+ expect ( runtime . agent . session . listSessionEntries ) . toHaveBeenCalledOnce ( ) ;
529+ expect ( runEmbeddedAgent ) . toHaveBeenCalledOnce ( ) ;
530+ } ) ;
531+
450532 it ( "limits locked harness session mutation and execution to the harness owner" , async ( ) => {
451533 const reservedKey = "agent:main:harness:codex:thread-1" ;
452534 const ordinaryKey = "agent:main:ordinary" ;
0 commit comments