GeezerVison is showing github.com — feat(server): a task delegated to another environment can run in its … · mediavee/t3code@5a25d91
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/environment/ServerEnvironment.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -302,6 +302,7 @@ export const make = Effect.gen(function* () { | 302 | 302 | linkThreadStream: true, | 303 | 303 | linkClientPairing: true, | 304 | 304 | linkParentDone: true, | | 305 | + linkWorktreeBase: true, | 305 | 306 | linkPairingForLink: true, | 306 | 307 | mcpApprovalCodes: true, | 307 | 308 | peerLinkPairingCodes: true, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpService.test.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3755,7 +3755,7 @@ describe("OrchestratorMcpService provider resolution", () => { | 3755 | 3755 | const parent = yield* orchestrator.getThreadProjection(parentThreadId); | 3756 | 3756 | return { taskId: parent.subagents[0]!.id }; | 3757 | 3757 | }).pipe(Effect.orDie), | 3758 | | - runStatus: () => Effect.succeed(null), | | 3758 | + readThere: () => Effect.succeed(null), | 3759 | 3759 | }); | 3760 | 3760 | }), | 3761 | 3761 | ).pipe(Layer.provide(layerWatchedThreads)); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpService.ts +14-34Lines changed: 14 additions & 34 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2297,9 +2297,13 @@ const make = Effect.gen(function* () { | 2297 | 2297 | waitTimedOut: boolean, | 2298 | 2298 | acknowledgeTerminal: boolean, | 2299 | 2299 | ): Effect.fn.Return<OrchestratorMcpDelegateTaskResult, OrchestratorMcpFailure> { | | 2300 | + // A task with its result needs nothing more from there. | | 2301 | + const there = task.result === null ? yield* remoteDelegation.readThere(scope, task) : null; | | 2302 | + // A task with its result keeps the checkout its result was read from. | | 2303 | + const worktree = there?.worktree ?? task.remoteChild?.worktree; | 2300 | 2304 | const status: OrchestratorMcpDelegateTaskResult["status"] = | 2301 | 2305 | task.result === null | 2302 | | - ? (yield* remoteDelegation.runStatus(scope, task)) === "starting" | | 2306 | + ? there?.status === "starting" | 2303 | 2307 | ? "starting" | 2304 | 2308 | : "running" | 2305 | 2309 | : task.status === "completed" || | @@ -2351,6 +2355,7 @@ const make = Effect.gen(function* () { | 2351 | 2355 | remoteChild: task.remoteChild, | 2352 | 2356 | link: formatThreadLink(task.remoteChild, task.remoteChild.title), | 2353 | 2357 | }), | | 2358 | + ...(worktree == null ? {} : { worktree }), | 2354 | 2359 | childRunId: null, | 2355 | 2360 | childNodeId: task.id, | 2356 | 2361 | status, | @@ -2428,39 +2433,8 @@ const make = Effect.gen(function* () { | 2428 | 2433 | effect.pipe(Effect.timeoutOption(Duration.millis(timeoutMs)), Effect.map(Option.flatten)), | 2429 | 2434 | ); | 2430 | 2435 |
| 2431 | | - const refExists = (cwd: string, revision: string) => | 2432 | | - gitDriver.resolveCommit({ cwd, revision }).pipe( | 2433 | | - Effect.as(true), | 2434 | | - Effect.orElseSucceed(() => false), | 2435 | | - ); | 2436 | | - | 2437 | | - const resolveScheduledTaskBaseRef = Effect.fn( | 2438 | | - "OrchestratorMcpService.resolveScheduledTaskBaseRef", | 2439 | | - )(function* (cwd: string) { | 2440 | | - const remoteName = yield* gitDriver | 2441 | | - .resolvePrimaryRemoteName(cwd) | 2442 | | - .pipe(Effect.orElseSucceed(() => null)); | 2443 | | - const conventional = ["main", "master"]; | 2444 | | - if (remoteName !== null) { | 2445 | | - const remoteDefault = yield* gitDriver | 2446 | | - .resolveDefaultBranchName(cwd, remoteName) | 2447 | | - .pipe(Effect.orElseSucceed(() => null)); | 2448 | | - // Fetch does not update a recorded HEAD, so it can outlive the branch it names. | 2449 | | - for (const branch of remoteDefault === null | 2450 | | - ? conventional | 2451 | | - : [remoteDefault, ...conventional]) { | 2452 | | - // Launch resolves a bare name through a local branch's upstream, or else assumes `origin`. | 2453 | | - if (yield* refExists(cwd, `refs/remotes/${remoteName}/${branch}`)) { | 2454 | | - return `${remoteName}/${branch}`; | 2455 | | - } | 2456 | | - } | 2457 | | - } | 2458 | | - // Local-only repositories, or a remote with neither a usable HEAD nor a conventional branch. | 2459 | | - for (const branch of conventional) { | 2460 | | - if (yield* refExists(cwd, `refs/heads/${branch}`)) return branch; | 2461 | | - } | 2462 | | - return "main"; | 2463 | | - }); | | 2436 | + const resolveScheduledTaskBaseRef = (cwd: string) => | | 2437 | + GitVcsDriver.defaultWorktreeBaseRef(gitDriver, cwd); | 2464 | 2438 |
| 2465 | 2439 | const scheduledTaskWorkspaceStrategy = Effect.fn( | 2466 | 2440 | "OrchestratorMcpService.scheduledTaskWorkspaceStrategy", | @@ -3077,6 +3051,12 @@ const make = Effect.gen(function* () { | 3077 | 3051 | ? result | 3078 | 3052 | : { ...result, runtimeModeSteppedDown }; | 3079 | 3053 | } | | 3054 | + if (input.baseRef !== undefined) { | | 3055 | + return yield* failure( | | 3056 | + "invalid_request", | | 3057 | + 'baseRef is for a task in another environment (target.environmentId). Here, isolation "worktree" starts from this checkout\'s commit.', | | 3058 | + ); | | 3059 | + } | 3080 | 3060 | const { scope, parent } = yield* loadThreadCaller(callerScope, "delegate_task"); | 3081 | 3061 | // Another project here gets its own thread, recorded and followed as a task there would be. | 3082 | 3062 | const targetProject = | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -10968,7 +10968,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 10968 | 10968 | const now = yield* DateTime.now; | 10969 | 10969 | const updatedTask: OrchestrationV2Subagent = { | 10970 | 10970 | ...task, | 10971 | | - remoteChild, | | 10971 | + remoteChild: | | 10972 | + command.worktree === undefined | | 10973 | + ? remoteChild | | 10974 | + : { ...remoteChild!, worktree: command.worktree }, | 10972 | 10975 | status: command.status, | 10973 | 10976 | result: command.result, | 10974 | 10977 | ...(command.executedModel === undefined ? {} : { executedModel: command.executedModel }), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/peer/PeerForwarding.ts +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | OrchestratorMcpThreadWaitInput, | 10 | 10 | OrchestratorMcpThreadWaitResult, | 11 | 11 | type PeerLinkError, | | 12 | + type PeerLinkWorktreeBaseRequest, | | 13 | + type PeerLinkWorktreeBaseResult, | 12 | 14 | RuntimeMode, | 13 | 15 | } from "@t3tools/contracts"; | 14 | 16 | import * as Context from "effect/Context"; | @@ -72,6 +74,18 @@ export class PeerForwarding extends Context.Service< | 72 | 74 | environmentId: EnvironmentId, | 73 | 75 | input: OrchestratorMcpThreadWaitInput, | 74 | 76 | ) => Effect.Effect<OrchestratorMcpThreadWaitResult, OrchestratorMcpFailure>; | | 77 | + /** | | 78 | + * The base a delegated task's new worktree in a project there starts from, resolved and | | 79 | + * checked there. Starts work, so it holds to the same gates as a launch there. | | 80 | + */ | | 81 | + readonly worktreeBase: ( | | 82 | + scope: Scope, | | 83 | + environmentId: EnvironmentId, | | 84 | + request: PeerLinkWorktreeBaseRequest, | | 85 | + ) => Effect.Effect< | | 86 | + PeerLinkWorktreeBaseResult | { readonly outcome: "refused"; readonly message: string }, | | 87 | + OrchestratorMcpFailure | | 88 | + >; | 75 | 89 | /** The environments this one links to, as an agent sees them. */ | 76 | 90 | readonly links: ( | 77 | 91 | scope: Scope, | @@ -336,8 +350,25 @@ const make = Effect.gen(function* () { | 336 | 350 | }, | 337 | 351 | ); | 338 | 352 |
| | 353 | + const worktreeBase: PeerForwarding["Service"]["worktreeBase"] = Effect.fn( | | 354 | + "PeerForwarding.worktreeBase", | | 355 | + )(function* (scope, environmentId, request) { | | 356 | + yield* requireEnabled; | | 357 | + yield* forwardingCaller(scope); | | 358 | + return yield* links.worktreeBase(environmentId, request).pipe( | | 359 | + Effect.mapError( | | 360 | + (error) => | | 361 | + new OrchestratorMcpFailure({ | | 362 | + code: error.reason === "incompatible" ? "invalid_request" : "orchestration_error", | | 363 | + message: error.message, | | 364 | + }), | | 365 | + ), | | 366 | + ); | | 367 | + }); | | 368 | + | 339 | 369 | return PeerForwarding.of({ | 340 | 370 | call, | | 371 | + worktreeBase, | 341 | 372 | readThread, | 342 | 373 | interruptThread, | 343 | 374 | waitForThread, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/peer/PeerLinks.testkit.ts +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,11 +31,15 @@ import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; | 31 | 31 | import * as ServerHttp from "../http.ts"; | 32 | 32 | import * as McpHttpServer from "../mcp/McpHttpServer.ts"; | 33 | 33 | import * as McpSessionRegistry from "../mcp/McpSessionRegistry.ts"; | | 34 | +import * as ThreadLaunch from "../orchestration-v2/ThreadLaunchService.ts"; | 34 | 35 | import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts"; | | 36 | +import * as ProjectService from "../project/ProjectService.ts"; | | 37 | +import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; | 35 | 38 | import * as Sqlite from "../persistence/Sqlite.ts"; | 36 | 39 | import * as PeerHttp from "./http.ts"; | 37 | 40 | import * as PeerClientPairing from "./PeerClientPairing.ts"; | 38 | 41 | import * as PeerLinks from "./PeerLinks.ts"; | | 42 | +import * as PeerWorktreeBase from "./PeerWorktreeBase.ts"; | 39 | 43 | import * as ServerSettings from "../serverSettings.ts"; | 40 | 44 | import * as PeerMcpClient from "./PeerMcpClient.ts"; | 41 | 45 | import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; | @@ -90,6 +94,14 @@ export const servePeer = <E, R, E2 = never, R2 = never>( | 90 | 94 | threads: Layer.Layer<ThreadManagement.ThreadManagementService> = Layer.mock( | 91 | 95 | ThreadManagement.ThreadManagementService, | 92 | 96 | )({}), | | 97 | + /** Its projects, launches and Git, as its worktree-base route sees them. */ | | 98 | + worktrees: Layer.Layer< | | 99 | + ProjectService.ProjectService | ThreadLaunch.ThreadLaunchService | GitVcsDriver.GitVcsDriver | | 100 | + > = Layer.mergeAll( | | 101 | + Layer.mock(ProjectService.ProjectService)({}), | | 102 | + Layer.mock(ThreadLaunch.ThreadLaunchService)({}), | | 103 | + Layer.mock(GitVcsDriver.GitVcsDriver)({}), | | 104 | + ), | 93 | 105 | ) => | 94 | 106 | Effect.gen(function* () { | 95 | 107 | const descriptor = yield* Ref.make(initial); | @@ -143,7 +155,11 @@ export const servePeer = <E, R, E2 = never, R2 = never>( | 143 | 155 | ), | 144 | 156 | ), | 145 | 157 | Layer.provide( | 146 | | - PeerHttp.layer.pipe(Layer.provide(PeerClientPairing.layerIssuer), Layer.provide(threads)), | | 158 | + PeerHttp.layer.pipe( | | 159 | + Layer.provide(PeerClientPairing.layerIssuer), | | 160 | + Layer.provide(threads), | | 161 | + Layer.provide(PeerWorktreeBase.layer.pipe(Layer.provide(worktrees))), | | 162 | + ), | 147 | 163 | ), | 148 | 164 | Layer.provide(ServerHttp.layerServerEnvironmentHttpApi), | 149 | 165 | Layer.provide(AuthHttp.layerAuthenticatedAuth), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/peer/PeerLinks.ts +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,8 @@ import { | 12 | 12 | type PeerLinkName, | 13 | 13 | type PeerLinkParentDoneRequest, | 14 | 14 | type PeerLinkParentDoneResult, | | 15 | + type PeerLinkWorktreeBaseRequest, | | 16 | + type PeerLinkWorktreeBaseResult, | 15 | 17 | type PeerLinkSummary, | 16 | 18 | } from "@t3tools/contracts"; | 17 | 19 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; | @@ -59,6 +61,8 @@ export const probeTimeout = (origin: string) => | 59 | 61 | isLocalLoopbackHost(new URL(origin).hostname) ? Duration.seconds(3) : Duration.seconds(10); | 60 | 62 | /** How long a listing waits on a link's addresses; a real call still gives a waking peer `probeTimeout`. */ | 61 | 63 | const LIST_PROBE_TIMEOUT = Duration.seconds(3); | | 64 | +/** A worktree base there may be fetched from its remote first. */ | | 65 | +const WORKTREE_BASE_TIMEOUT = Duration.seconds(60); | 62 | 66 |
| 63 | 67 | /** Node's TLS verification code in a failed request's cause chain, if any. */ | 64 | 68 | export const certificateErrorCode = (error: unknown) => { | @@ -146,6 +150,17 @@ export class PeerLinks extends Context.Service< | 146 | 150 | environmentId: EnvironmentId, | 147 | 151 | request: PeerLinkParentDoneRequest, | 148 | 152 | ) => Effect.Effect<PeerLinkParentDoneResult["outcome"] | "refused", PeerLinkError>; | | 153 | + /** | | 154 | + * The base a delegated task's worktree in a project there starts from, checked there. | | 155 | + * `refused`: that environment does not let this link start work in a new checkout there. | | 156 | + */ | | 157 | + readonly worktreeBase: ( | | 158 | + environmentId: EnvironmentId, | | 159 | + request: PeerLinkWorktreeBaseRequest, | | 160 | + ) => Effect.Effect< | | 161 | + PeerLinkWorktreeBaseResult | { readonly outcome: "refused"; readonly message: string }, | | 162 | + PeerLinkError | | 163 | + >; | 149 | 164 | } | 150 | 165 | >()("t3/peer/PeerLinks") {} | 151 | 166 |
| @@ -874,6 +889,46 @@ const make = Effect.gen(function* () { | 874 | 889 | ); | 875 | 890 | }); | 876 | 891 |
| | 892 | + const worktreeBase: PeerLinks["Service"]["worktreeBase"] = Effect.fn("PeerLinks.worktreeBase")( | | 893 | + function* (environmentId, request) { | | 894 | + const peer = yield* resolve(environmentId); | | 895 | + const { label } = peer.link; | | 896 | + if (peer.capabilities.linkWorktreeBase !== true) { | | 897 | + return yield* new PeerLinkError({ | | 898 | + reason: "incompatible", | | 899 | + message: `${label} runs a T3 Code version that does not support worktree isolation for a delegated task. Update it, or omit isolation to run in that project's checkout.`, | | 900 | + }); | | 901 | + } | | 902 | + const client = yield* clientFor(peer.url); | | 903 | + return yield* client.peerLink | | 904 | + .worktreeBase({ headers: { authorization: `Bearer ${peer.token}` }, payload: request }) | | 905 | + .pipe( | | 906 | + Effect.timeout(WORKTREE_BASE_TIMEOUT), | | 907 | + Effect.catchTags({ | | 908 | + EnvironmentHttpForbiddenError: (error) => | | 909 | + Effect.succeed({ | | 910 | + outcome: "refused" as const, | | 911 | + message: `${label}: ${error.message}`, | | 912 | + }), | | 913 | + EnvironmentHttpUnauthorizedError: () => | | 914 | + Effect.fail( | | 915 | + new PeerLinkError({ | | 916 | + reason: "expired", | | 917 | + message: `${label} no longer accepts this link's session for this. Link it again with a new pairing code from ${label}.`, | | 918 | + }), | | 919 | + ), | | 920 | + }), | | 921 | + Effect.catchIf( | | 922 | + (error) => !isPeerLinkError(error), | | 923 | + () => | | 924 | + Effect.fail( | | 925 | + new PeerLinkError({ reason: "unreachable", message: `${label} did not answer.` }), | | 926 | + ), | | 927 | + ), | | 928 | + ); | | 929 | + }, | | 930 | + ); | | 931 | + | 877 | 932 | return PeerLinks.of({ | 878 | 933 | enabled, | 879 | 934 | link, | @@ -886,6 +941,7 @@ const make = Effect.gen(function* () { | 886 | 941 | recordOutcome, | 887 | 942 | recordExpiry, | 888 | 943 | tellParentDone, | | 944 | + worktreeBase, | 889 | 945 | }); | 890 | 946 | }); | 891 | 947 |
| |
|