@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 77 lines 3.08 KB
Raw
1 import {
2 linkOriginIsFenced,
3 OrchestratorMcpFailure,
4 type OrchestrationV2LinkOrigin,
5 type OrchestrationV2ThreadShell,
6 } from "@t3tools/contracts";
7 import * as Effect from "effect/Effect";
8
9 import { assertFullAccess, type Caller } from "./threadAccess.ts";
10
11 /**
12 * Below full access, work a link starts here (all of it carries that session in `linkOrigin`)
13 * may change only work from the same link: not the user's threads, projects or settings, nor
14 * this environment's links. Reads are not fenced. It is a routing rule for T3's tools; agents
15 * still run as this environment's user, so a full-access link is not fenced at all.
16 */
17
18 /** The link a caller acts for: its own session, or the link that started its thread. */
19 export const callerLinkOrigin = (
20 caller: Pick<Caller, "scope" | "caller">,
21 ): OrchestrationV2LinkOrigin | undefined =>
22 caller.caller?.linkOrigin ??
23 (caller.scope.client?.linked === true
24 ? {
25 sessionId: caller.scope.client.sessionId,
26 label: caller.scope.client.label,
27 access: caller.scope.client.access,
28 }
29 : undefined);
30
31 /** The link that fences a caller, if any: one paired below full access. */
32 export const fencingLinkOrigin = (caller: Pick<Caller, "scope" | "caller">) => {
33 const origin = callerLinkOrigin(caller);
34 return origin !== undefined && linkOriginIsFenced(origin) ? origin : undefined;
35 };
36
37 const outsideLinkMessage = (origin: OrchestrationV2LinkOrigin, message: string) =>
38 `This work was started from ${origin.label} through a linked environment, so ${message} A link paired with full access is not limited this way.`;
39
40 const outsideLink = (origin: OrchestrationV2LinkOrigin, message: string) =>
41 new OrchestratorMcpFailure({
42 code: "capability_denied",
43 message: outsideLinkMessage(origin, message),
44 });
45
46 /** A thread a fenced caller may change: one its own link started. */
47 export const assertSameLink = (
48 caller: Pick<Caller, "scope" | "caller">,
49 target: Pick<OrchestrationV2ThreadShell, "id" | "linkOrigin">,
50 ) => {
51 const origin = fencingLinkOrigin(caller);
52 return origin === undefined || target.linkOrigin?.sessionId === origin.sessionId
53 ? Effect.void
54 : Effect.fail(outsideLink(origin, `it may change only threads that link started.`));
55 };
56
57 /** Fenced work never changes what the environment already has: projects, settings, scheduled runs. */
58 export const assertNotLinked = (caller: Pick<Caller, "scope" | "caller">, what: string) => {
59 const origin = fencingLinkOrigin(caller);
60 return origin === undefined
61 ? Effect.void
62 : Effect.fail(outsideLink(origin, `it cannot ${what}.`));
63 };
64
65 /** A fenced caller is held below full access by its link, not its thread, so the refusal names the link. */
66 export const assertFullAccessNamingLink = (caller: Caller, message: string, what: string) => {
67 const origin = fencingLinkOrigin(caller);
68 return assertFullAccess(
69 caller,
70 origin === undefined
71 ? message
72 : outsideLinkMessage(
73 origin,
74 `it has that link's ${origin.access === undefined ? "" : `${origin.access} `}access, which is below the full access needed to ${what}.`,
75 ),
76 );
77 };