11import { describe , expect , test } from "bun:test"
22import { Workflow } from "@/workflow/workflow"
3+ import { Session } from "@/session/session"
4+ import { Agent } from "@/agent/agent"
5+ import { SessionID } from "@/session/schema"
36import type { SessionPrompt } from "@/session/prompt"
47import type { SessionV1 } from "@opencode-ai/core/v1/session"
58import { Database } from "@opencode-ai/core/database/database"
@@ -17,7 +20,19 @@ import path from "path"
1720
1821// Database.defaultLayer is merged so the orphan-sweep tests can seed a row
1922// directly through the same in-memory SQLite connection the engine uses.
20- const it = testEffect ( Layer . mergeAll ( Workflow . defaultLayer , Database . defaultLayer , CrossSpawnSpawner . defaultLayer ) )
23+ // Session/Agent.defaultLayer are merged so the subagent-permission-inheritance
24+ // tests can create a caller session (with a deny ruleset) and read back the
25+ // child session the engine spawns — through the SAME memoised services the
26+ // engine resolves (Effect dedupes shared layer builds, exactly as for Database).
27+ const it = testEffect (
28+ Layer . mergeAll (
29+ Workflow . defaultLayer ,
30+ Database . defaultLayer ,
31+ Session . defaultLayer ,
32+ Agent . defaultLayer ,
33+ CrossSpawnSpawner . defaultLayer ,
34+ ) ,
35+ )
2136
2237const HELLO_FIXTURE = "hello"
2338
@@ -171,6 +186,28 @@ export async function run(args, ctx) {
171186}
172187`
173188
189+ // Subagent-permission-inheritance fixture (#26514 regression / Fund N9): a
190+ // single agent step that completes. The engine must spawn its child session
191+ // with a derived `permission` ruleset when a `caller` context is supplied.
192+ const SINGLE_AGENT_FIXTURE = "single-agent"
193+ const SINGLE_AGENT_WORKFLOW = `export const meta = { name: "${ SINGLE_AGENT_FIXTURE } ", phases: ["run"] }
194+ export async function run(args, ctx) {
195+ ctx.setPhase("run")
196+ await ctx.agent({ prompt: "do the thing" })
197+ return { ok: true }
198+ }
199+ `
200+
201+ // Prompt-ops that resolve every agent prompt immediately (no hang), so the run
202+ // reaches `completed` and the child session is fully created/projected.
203+ function immediatePromptOps ( ) {
204+ const ops : { prompt : SessionPrompt . Interface [ "prompt" ] ; cancel : SessionPrompt . Interface [ "cancel" ] } = {
205+ prompt : ( ) => Effect . succeed ( assistantReply ( ) ) ,
206+ cancel : ( ) => Effect . void ,
207+ }
208+ return ops
209+ }
210+
174211// N11-Fixture: Der Body startet einen Agenten OHNE ihn zu awaiten (fire-and-
175212// forget) — der hängende ctx.agent-Promise settelt nie vor Body-Ende — und
176213// returnt sofort. Die Pause gibt dem dispatchten Agent-Fiber Zeit, seine
@@ -2456,6 +2493,91 @@ export async function run() { return { ok: true } }
24562493 } ) ,
24572494 )
24582495
2496+ // #26514 regression / Fund N9 (security): a workflow subagent MUST inherit the
2497+ // caller's deny/external_directory rules (and the caller agent's edit-class
2498+ // denies, i.e. Plan Mode) — the same ruleset the task tool derives. Before the
2499+ // fix the engine spawned the child session with NO `permission`, so a parent
2500+ // `edit: deny` (Plan Mode) or `external_directory` confinement silently leaked.
2501+ it . instance ( "workflow subagent inherits the caller session's deny/external_directory rules" , ( ) =>
2502+ Effect . gen ( function * ( ) {
2503+ const test = yield * TestInstance
2504+ yield * Effect . promise ( ( ) => writeWorkflow ( test . directory , SINGLE_AGENT_FIXTURE , SINGLE_AGENT_WORKFLOW ) )
2505+ const workflow = yield * Workflow . Service
2506+ const sessions = yield * Session . Service
2507+
2508+ // The caller session carries the exact rules a Plan-Mode / confined parent
2509+ // would: an edit deny and an external_directory rule. The fix must forward
2510+ // both onto every subagent session the run spawns.
2511+ const caller = yield * sessions . create ( {
2512+ title : "Caller" ,
2513+ permission : [
2514+ { permission : "edit" , pattern : "**" , action : "deny" } ,
2515+ { permission : "external_directory" , pattern : "/outside/**" , action : "allow" } ,
2516+ ] ,
2517+ } )
2518+
2519+ const run = yield * workflow . start ( {
2520+ name : SINGLE_AGENT_FIXTURE ,
2521+ args : { } ,
2522+ prompt : immediatePromptOps ( ) ,
2523+ caller : { sessionID : caller . id , agent : "build" } ,
2524+ } )
2525+ const done = ( yield * workflow . wait ( { id : run . id } ) ) . run ?? ( yield * Effect . fail ( new Error ( "did not finish" ) ) )
2526+ expect ( done . status ) . toBe ( "completed" )
2527+
2528+ const childSessionID = done . agents [ 0 ] ?. session_id
2529+ expect ( childSessionID ) . toBeDefined ( )
2530+ // The projector persists the session row off the Created event, so poll the
2531+ // read until the row (and its derived permission) is visible. A not-yet-
2532+ // projected row fails get() with NotFound → map to undefined to keep polling.
2533+ const child = yield * pollWithTimeout (
2534+ sessions
2535+ . get ( SessionID . make ( childSessionID ! ) )
2536+ . pipe (
2537+ Effect . map ( ( s ) => ( s . permission ? s : undefined ) ) ,
2538+ Effect . catchCause ( ( ) => Effect . succeed ( undefined ) ) ,
2539+ ) ,
2540+ "child session permission never populated" ,
2541+ )
2542+ const rules = child . permission ?? [ ]
2543+ // Core security assertion: the caller's deny + external_directory rules are
2544+ // present on the child (regression of #26514 would leave these absent).
2545+ expect ( rules ) . toContainEqual ( { permission : "edit" , pattern : "**" , action : "deny" } )
2546+ expect ( rules ) . toContainEqual ( { permission : "external_directory" , pattern : "/outside/**" , action : "allow" } )
2547+ } ) ,
2548+ )
2549+
2550+ // Fallback (documented behavior): a programmatic start with NO caller context
2551+ // keeps the prior behavior — the child session carries no derived `permission`.
2552+ it . instance ( "workflow subagent has no inherited ruleset when no caller context is supplied" , ( ) =>
2553+ Effect . gen ( function * ( ) {
2554+ const test = yield * TestInstance
2555+ yield * Effect . promise ( ( ) => writeWorkflow ( test . directory , SINGLE_AGENT_FIXTURE , SINGLE_AGENT_WORKFLOW ) )
2556+ const workflow = yield * Workflow . Service
2557+ const sessions = yield * Session . Service
2558+
2559+ const run = yield * workflow . start ( {
2560+ name : SINGLE_AGENT_FIXTURE ,
2561+ args : { } ,
2562+ prompt : immediatePromptOps ( ) ,
2563+ } )
2564+ const done = ( yield * workflow . wait ( { id : run . id } ) ) . run ?? ( yield * Effect . fail ( new Error ( "did not finish" ) ) )
2565+ expect ( done . status ) . toBe ( "completed" )
2566+
2567+ const childSessionID = done . agents [ 0 ] ?. session_id
2568+ expect ( childSessionID ) . toBeDefined ( )
2569+ // No caller ⇒ no derived ruleset. The session row exists (the run created
2570+ // it), but its `permission` column stays unset (fromRow → undefined). Poll
2571+ // until the row is visible (NotFound → undefined keeps polling), then assert
2572+ // no permission was stored.
2573+ const child = yield * pollWithTimeout (
2574+ sessions . get ( SessionID . make ( childSessionID ! ) ) . pipe ( Effect . catchCause ( ( ) => Effect . succeed ( undefined ) ) ) ,
2575+ "child session never created" ,
2576+ )
2577+ expect ( child . permission ) . toBeUndefined ( )
2578+ } ) ,
2579+ )
2580+
24592581 // Fund 18 (medium): die drei Workflow-Zeitfelder (LogEntry.time,
24602582 // AgentRun.started_at/completed_at) sind Epoch-Millis und damit IMMER endlich.
24612583 // Als `Schema.Number` erzeugte der SDK-Generator für sie eine NaN/Infinity-
0 commit comments