1+ import * as DocumentText from "../provider/DocumentText.ts" ;
12import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
23import * as NodeServices from "@effect/platform-node/NodeServices" ;
34import { assert , it } from "@effect/vitest" ;
@@ -14,7 +15,6 @@ import {
1415 ThreadId ,
1516} from "@t3tools/contracts" ;
1617import * as Effect from "effect/Effect" ;
17- import * as Option from "effect/Option" ;
1818import * as Layer from "effect/Layer" ;
1919import { McpSchema , McpServer } from "effect/ai" ;
2020
@@ -113,6 +113,7 @@ const layerTools = Layer.mergeAll(
113113 McpHttpServer . layerThreadToolkit ,
114114 McpHttpServer . layerProjectRegistration ,
115115) . pipe (
116+ Layer . provide ( DocumentText . layer . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ) ,
116117 Layer . provideMerge ( McpServer . McpServer . layer ) ,
117118 Layer . provide ( layerLaunches ) ,
118119 Layer . provide ( NodeCrypto . layer ) ,
@@ -127,7 +128,7 @@ const layerTools = Layer.mergeAll(
127128 Layer . provide ( Layer . mock ( ScheduledTaskService . ScheduledTaskService ) ( { } ) ) ,
128129 Layer . provide (
129130 Layer . mock ( ProjectService . ProjectService ) ( {
130- getById : ( ) => Effect . succeed ( Option . none ( ) ) ,
131+ getById : ( ) => Effect . succeedNone ,
131132 create : ( input ) =>
132133 Effect . succeed ( {
133134 id : input . projectId ,
@@ -139,7 +140,17 @@ const layerTools = Layer.mergeAll(
139140 updatedAt : "2026-10-18T00:00:00.000Z" ,
140141 deletedAt : null ,
141142 } ) ,
142- update : ( ) => Effect . die ( "A linked caller must never reach a project update." ) ,
143+ update : ( input ) =>
144+ Effect . succeed ( {
145+ id : input . projectId ,
146+ title : input . title ?? "Untitled" ,
147+ workspaceRoot : "/p/t3code" ,
148+ defaultModelSelection : null ,
149+ scripts : [ ] ,
150+ createdAt : "2026-10-18T00:00:00.000Z" ,
151+ updatedAt : "2026-10-18T00:00:00.000Z" ,
152+ deletedAt : null ,
153+ } ) ,
143154 } ) ,
144155 ) ,
145156 Layer . provide ( Layer . mock ( SecretRequests . SecretRequests ) ( { } ) ) ,
@@ -183,17 +194,19 @@ const mcpClient = McpSchema.McpServerClient.of({
183194const clientScope = (
184195 sessionId : string ,
185196 linked : boolean ,
197+ access : McpInvocationContext . McpClientCaller [ "access" ] ,
186198) : McpInvocationContext . McpInvocationScope => ( {
187199 environmentId : EnvironmentId . make ( "environment:box" ) ,
188200 requestNamespace : `client:${ sessionId } ` ,
189201 thread : undefined ,
190- client : { sessionId, label : `T3 Code · ${ sessionId } ` , access : "full-access" , linked } ,
202+ client : { sessionId, label : `T3 Code · ${ sessionId } ` , access, linked } ,
191203 capabilities : new Set ( [ "orchestration" , "worktree" , "pull-requests" ] ) ,
192204 issuedAt : 0 ,
193205} ) ;
194- const laptop = clientScope ( "laptop" , true ) ;
195- const desk = clientScope ( "desk" , true ) ;
196- const claudeCode = clientScope ( "claude-code" , false ) ;
206+ const laptop = clientScope ( "laptop" , true , "auto" ) ;
207+ const desk = clientScope ( "desk" , true , "auto" ) ;
208+ const trusted = clientScope ( "trusted" , true , "full-access" ) ;
209+ const claudeCode = clientScope ( "claude-code" , false , "full-access" ) ;
197210
198211const call = (
199212 scope : McpInvocationContext . McpInvocationScope ,
@@ -235,7 +248,8 @@ const usersOwnThread = Effect.gen(function* () {
235248 projectId,
236249 title : "The user's own" ,
237250 modelSelection : { instanceId, model : "gpt-5" } ,
238- runtimeMode : "full-access" ,
251+ // Within an `auto` link's modes, so only the fence decides.
252+ runtimeMode : "auto" ,
239253 interactionMode : "default" ,
240254 branch : null ,
241255 worktreePath : null ,
@@ -251,7 +265,11 @@ it.layer(Layer.provideMerge(layerTools, layerOrchestration))("work a link starts
251265 const projections = yield * ProjectionStore . ProjectionStoreV2 ;
252266 const launched = yield * launch ( laptop , "Started from the laptop" ) ;
253267 const shell = yield * projections . getThreadShell ( launched ) ;
254- assert . deepEqual ( shell ?. linkOrigin , { sessionId : "laptop" , label : "T3 Code · laptop" } ) ;
268+ assert . deepEqual ( shell ?. linkOrigin , {
269+ sessionId : "laptop" ,
270+ label : "T3 Code · laptop" ,
271+ access : "auto" ,
272+ } ) ;
255273
256274 // A subagent of it and a fork of it are built from its thread, origin included.
257275 const thread = ( yield * ThreadManagement . ThreadManagementService . pipe (
@@ -294,64 +312,76 @@ it.layer(Layer.provideMerge(layerTools, layerOrchestration))("work a link starts
294312 } ) ,
295313 ) ;
296314
297- it . effect (
298- "changes only that link's threads, and adds to the environment without changing it" ,
299- ( ) =>
300- Effect . gen ( function * ( ) {
301- const projections = yield * ProjectionStore . ProjectionStoreV2 ;
302- const users = yield * usersOwnThread ;
303- const ownLaunch = yield * launch ( laptop , "The laptop's" ) ;
304- const rename = ( scope : McpInvocationContext . McpInvocationScope , threadId : ThreadId ) =>
305- call ( scope , "t3_thread_update" , {
306- threadId,
307- action : "rename" ,
308- title : `Renamed by ${ scope . client ! . sessionId } ` ,
309- } ) ;
315+ it . effect ( "changes only that link's threads below full access" , ( ) =>
316+ Effect . gen ( function * ( ) {
317+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
318+ const users = yield * usersOwnThread ;
319+ const ownLaunch = yield * launch ( laptop , "The laptop's" ) ;
320+ const rename = ( scope : McpInvocationContext . McpInvocationScope , threadId : ThreadId ) =>
321+ call ( scope , "t3_thread_update" , {
322+ threadId,
323+ action : "rename" ,
324+ title : `Renamed by ${ scope . client ! . sessionId } ` ,
325+ } ) ;
310326
311- // The link may change what it started.
312- assert . equal ( ( yield * rename ( laptop , ownLaunch ) ) . isError , false ) ;
313- assert . equal ( ( yield * projections . getThreadShell ( ownLaunch ) ) ?. title , "Renamed by laptop" ) ;
314- // Not the user's own thread, nor another link's.
315- assert . equal ( failureCode ( yield * rename ( laptop , users ) ) , "capability_denied" ) ;
316- assert . equal ( failureCode ( yield * rename ( desk , ownLaunch ) ) , "capability_denied" ) ;
317- assert . equal ( ( yield * projections . getThreadShell ( users ) ) ?. title , "The user's own" ) ;
318- // An ordinary outside agent is not fenced.
319- assert . equal ( ( yield * rename ( claudeCode , users ) ) . isError , false ) ;
320- // Naming this environment's own id is still this environment's thread.
321- for ( const [ name , args ] of [
322- [ "t3_thread_send" , { message : "From the laptop." } ] ,
323- [ "t3_thread_interrupt" , { } ] ,
324- ] as const ) {
325- const result = yield * call ( laptop , name , {
326- ...args ,
327- environmentId : laptop . environmentId ,
328- threadId : users ,
329- } ) ;
330- assert . equal ( failureCode ( result ) , "capability_denied" , name ) ;
331- }
327+ // The link may change what it started.
328+ assert . equal ( ( yield * rename ( laptop , ownLaunch ) ) . isError , false ) ;
329+ assert . equal ( ( yield * projections . getThreadShell ( ownLaunch ) ) ?. title , "Renamed by laptop" ) ;
330+ // Not the user's own thread, nor another link's.
331+ assert . equal ( failureCode ( yield * rename ( laptop , users ) ) , "capability_denied" ) ;
332+ assert . equal ( failureCode ( yield * rename ( desk , ownLaunch ) ) , "capability_denied" ) ;
333+ assert . equal ( ( yield * projections . getThreadShell ( users ) ) ?. title , "The user's own" ) ;
334+ // An ordinary outside agent is not fenced.
335+ assert . equal ( ( yield * rename ( claudeCode , users ) ) . isError , false ) ;
336+ // Naming this environment's own id is still this environment's thread.
337+ for ( const [ name , args ] of [
338+ [ "t3_thread_send" , { message : "From the laptop." } ] ,
339+ [ "t3_thread_interrupt" , { } ] ,
340+ ] as const ) {
341+ const result = yield * call ( laptop , name , {
342+ ...args ,
343+ environmentId : laptop . environmentId ,
344+ threadId : users ,
345+ } ) ;
346+ assert . equal ( failureCode ( result ) , "capability_denied" , name ) ;
347+ }
332348
333- // Nor what the environment already has: its projects and settings (one
334- // declaration covers both), and scheduled tasks.
335- assert . equal (
336- failureCode ( yield * call ( laptop , "t3_project_update" , { projectId, title : "Mine now" } ) ) ,
337- "capability_denied" ,
338- ) ;
339- // It may bring a repository of its own to work in.
340- for ( const [ name , args ] of [
341- [ "t3_project_clone" , { repository : "pingdotgg/t3code" , destinationPath : "/p/t3code" } ] ,
342- [ "t3_project_create" , { title : "t3code" , workspaceRoot : "/p/t3code" } ] ,
343- ] as const ) {
344- const added = yield * call ( laptop , name , args ) ;
345- assert . equal ( added . isError , false , `${ name } : ${ JSON . stringify ( added . content ) } ` ) ;
346- }
347- assert . equal (
348- failureCode ( yield * call ( laptop , "delete_scheduled_task" , { scheduledTaskId : "any" } ) ) ,
349- "capability_denied" ,
350- ) ;
349+ // Nor scheduled tasks, which belong to no thread.
350+ assert . equal (
351+ failureCode ( yield * call ( laptop , "delete_scheduled_task" , { scheduledTaskId : "any" } ) ) ,
352+ "capability_denied" ,
353+ ) ;
354+
355+ // Reads are not fenced.
356+ const read = yield * call ( laptop , "t3_thread_read" , { threadId : users } ) ;
357+ assert . equal ( read . isError , false ) ;
358+ } ) ,
359+ ) ;
360+
361+ it . effect ( "is not fenced when the link has full access, but still says where it came from" , ( ) =>
362+ Effect . gen ( function * ( ) {
363+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
364+ const users = yield * usersOwnThread ;
365+ const launched = yield * launch ( trusted , "Started from the desk" ) ;
366+ assert . deepEqual ( ( yield * projections . getThreadShell ( launched ) ) ?. linkOrigin , {
367+ sessionId : "trusted" ,
368+ label : "T3 Code · trusted" ,
369+ access : "full-access" ,
370+ } ) ;
351371
352- // Reads are not fenced.
353- const read = yield * call ( laptop , "t3_thread_read" , { threadId : users } ) ;
354- assert . equal ( read . isError , false ) ;
355- } ) ,
372+ const renamed = yield * call ( trusted , "t3_thread_update" , {
373+ threadId : users ,
374+ action : "rename" ,
375+ title : "Renamed from the desk" ,
376+ } ) ;
377+ assert . equal ( renamed . isError , false , JSON . stringify ( renamed . content ) ) ;
378+ for ( const [ name , args ] of [
379+ [ "t3_project_update" , { projectId, title : "The desk's now" } ] ,
380+ [ "t3_project_create" , { title : "t3code" , workspaceRoot : "/p/t3code" } ] ,
381+ ] as const ) {
382+ const changed = yield * call ( trusted , name , args ) ;
383+ assert . equal ( changed . isError , false , `${ name } : ${ JSON . stringify ( changed . content ) } ` ) ;
384+ }
385+ } ) ,
356386 ) ;
357387} ) ;
0 commit comments