GeezerVison is showing github.com — fix(server): a follow-up to a finished remote task is a new task that… · pingdotgg/t3code@ed57b9b
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/mcp/McpToolAccess.ts +44-2Lines changed: 44 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,7 @@ const refuseOutsideConstruction = (token: symbol) => { | 46 | 46 | let declare: <P, A, E, R>( | 47 | 47 | handle: (params: P) => Effect.Effect<A, E, R>, | 48 | 48 | remote: RemoteCheck<P> | undefined, | | 49 | + forwarded?: Forwarded<P, A, E, R>, | 49 | 50 | ) => Declaration<(params: P) => Effect.Effect<A, E, R>>; | 50 | 51 |
| 51 | 52 | /** | @@ -58,6 +59,16 @@ type RemoteCheck<P> = ( | 58 | 59 | ) => Effect.Effect<unknown, OrchestratorMcpFailure, Effect.Services<typeof writingCaller>>; | 59 | 60 | let remoteCheckOf: <Handler>(declaration: Declaration<Handler>) => RemoteCheck<never> | undefined; | 60 | 61 |
| | 62 | +/** What a call answered in a linked environment does here before it returns. */ | | 63 | +type Forwarded<P, A, E, R> = ( | | 64 | + params: P, | | 65 | + environmentId: EnvironmentId, | | 66 | + result: A, | | 67 | +) => Effect.Effect<A, E, R>; | | 68 | +let forwardedOf: <Handler>( | | 69 | + declaration: Declaration<Handler>, | | 70 | +) => Forwarded<never, unknown, unknown, unknown> | undefined; | | 71 | + | 61 | 72 | type CheckedHandlerOf<D> = D extends Declaration<infer Handler> ? Handler : never; | 62 | 73 |
| 63 | 74 | interface CheckedHandler extends Struct.Lambda { | @@ -74,19 +85,29 @@ let handlersLayer: <Tools extends Record<string, Tool.Any>, EX, RX>( | 74 | 85 | export class Declaration<out Handler> { | 75 | 86 | readonly #handle: Handler; | 76 | 87 | readonly #remote: RemoteCheck<never> | undefined; | | 88 | + readonly #forwarded: Forwarded<never, unknown, unknown, unknown> | undefined; | 77 | 89 | private constructor( | 78 | 90 | token: typeof builtHere, | 79 | 91 | handle: Handler, | 80 | 92 | remote: RemoteCheck<never> | undefined, | | 93 | + forwarded: Forwarded<never, unknown, unknown, unknown> | undefined, | 81 | 94 | ) { | 82 | 95 | refuseOutsideConstruction(token); | 83 | 96 | this.#handle = handle; | 84 | 97 | this.#remote = remote; | | 98 | + this.#forwarded = forwarded; | 85 | 99 | } | 86 | 100 | static { | 87 | | - declare = (handle, remote) => new Declaration(builtHere, handle, remote); | | 101 | + declare = (handle, remote, forwarded) => | | 102 | + new Declaration( | | 103 | + builtHere, | | 104 | + handle, | | 105 | + remote, | | 106 | + forwarded as Forwarded<never, unknown, unknown, unknown> | undefined, | | 107 | + ); | 88 | 108 | checkedHandler = Struct.lambda<CheckedHandler>((declaration) => declaration.#handle); | 89 | 109 | remoteCheckOf = (declaration) => declaration.#remote; | | 110 | + forwardedOf = (declaration) => declaration.#forwarded; | 90 | 111 | } | 91 | 112 | } | 92 | 113 |
| @@ -281,6 +302,20 @@ export const either = <P, A, E, R, A2, E2, R2>( | 281 | 302 | ); | 282 | 303 | }; | 283 | 304 |
| | 305 | +/** | | 306 | + * `declaration`, whose result from a linked environment then goes through | | 307 | + * `forwarded` here, for what this environment records about the call. | | 308 | + */ | | 309 | +export const afterForwarding = <P, A, E, R, E2, R2>( | | 310 | + declaration: Declaration<(params: P) => Effect.Effect<A, E, R>>, | | 311 | + forwarded: Forwarded<P, A, E2, R2>, | | 312 | +) => | | 313 | + declare( | | 314 | + checkedHandler(declaration) as (params: P) => Effect.Effect<A, E | E2, R | R2>, | | 315 | + remoteCheckOf(declaration) as RemoteCheck<P> | undefined, | | 316 | + forwarded, | | 317 | + ); | | 318 | + | 284 | 319 | const fullAccessRequired = | 285 | 320 | "Changing projects or environment settings needs a live full-access/default calling thread or a full-access client."; | 286 | 321 |
| @@ -387,6 +422,9 @@ const routed = Effect.fn("McpToolAccess.routed")(function* <Tools extends Record | 387 | 422 | const tool = toolkit.tools[name]; | 388 | 423 | if (tool === undefined || !takesLinkedEnvironment(tool)) continue; | 389 | 424 | const remote = remoteCheckOf(declaration); | | 425 | + const forwarded = forwardedOf(declaration) as | | 426 | + | Forwarded<object, unknown, Tool.Failure<Tools[keyof Tools]>, never> | | 427 | + | undefined; | 390 | 428 | if (remote === undefined) | 391 | 429 | return yield* Effect.die( | 392 | 430 | `${name} takes a linked environment, but it acts as its calling thread, which is here.`, | @@ -402,7 +440,11 @@ const routed = Effect.fn("McpToolAccess.routed")(function* <Tools extends Record | 402 | 440 | if (scope.linkedEnvironments === false) return yield* PeerForwarding.linkedEnvironmentsOff; | 403 | 441 | yield* (remote as RemoteCheck<typeof params>)(params); | 404 | 442 | if (Option.isNone(forwarding)) return yield* linksUnavailable; | 405 | | - return yield* forwarding.value.call(scope, tool, target, { ...params, environmentId }); | | 443 | + const result = yield* forwarding.value.call(scope, tool, target, { | | 444 | + ...params, | | 445 | + environmentId, | | 446 | + }); | | 447 | + return forwarded === undefined ? result : yield* forwarded(params, target, result); | 406 | 448 | }); | 407 | 449 | } | 408 | 450 | return handlers as Toolkit.HandlersFrom<Tools>; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/orchestrator/handlers.ts +15-5Lines changed: 15 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect"; | 5 | 5 | import * as PeerForwarding from "../../../peer/PeerForwarding.ts"; | 6 | 6 | import * as PeerLinkRequests from "../../../peer/PeerLinkRequests.ts"; | 7 | 7 | import * as PeerLinks from "../../../peer/PeerLinks.ts"; | | 8 | +import * as RemoteDelegation from "../../../peer/RemoteDelegation.ts"; | 8 | 9 | import { readCaller } from "../../threadAccess.ts"; | 9 | 10 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; | 10 | 11 | import * as McpToolAccess from "../../McpToolAccess.ts"; | @@ -168,13 +169,22 @@ const handlers = { | 168 | 169 | return yield* service.update(scope, input); | 169 | 170 | }), | 170 | 171 | ), | 171 | | - t3_thread_send: McpToolAccess.writesThreads( | 172 | | - (input) => [input.threadId], | 173 | | - (input) => | | 172 | + t3_thread_send: McpToolAccess.afterForwarding( | | 173 | + McpToolAccess.writesThreads( | | 174 | + (input) => [input.threadId], | | 175 | + (input) => | | 176 | + Effect.gen(function* () { | | 177 | + const scope = yield* McpInvocationContext.McpInvocationContext; | | 178 | + const service = yield* OrchestratorMcpService.OrchestratorMcpService; | | 179 | + return yield* service.sendToThread(scope, input); | | 180 | + }), | | 181 | + ), | | 182 | + // There the thread's parent is here, so only this side can make a follow-up a task. | | 183 | + (input, environmentId, sent) => | 174 | 184 | Effect.gen(function* () { | 175 | 185 | const scope = yield* McpInvocationContext.McpInvocationContext; | 176 | | - const service = yield* OrchestratorMcpService.OrchestratorMcpService; | 177 | | - return yield* service.sendToThread(scope, input); | | 186 | + const remote = yield* RemoteDelegation.RemoteDelegation; | | 187 | + return yield* remote.followUp(scope, environmentId, input, sent); | 178 | 188 | }), | 179 | 189 | ), | 180 | 190 | t3_thread_wait: McpToolAccess.reads((input) => | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/orchestrator/tools.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,7 @@ import * as ThreadManagementService from "../../../orchestration-v2/ThreadManage | 46 | 46 | import * as PeerForwarding from "../../../peer/PeerForwarding.ts"; | 47 | 47 | import * as PeerLinkRequests from "../../../peer/PeerLinkRequests.ts"; | 48 | 48 | import * as PeerLinks from "../../../peer/PeerLinks.ts"; | | 49 | +import * as RemoteDelegation from "../../../peer/RemoteDelegation.ts"; | 49 | 50 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; | 50 | 51 | import * as OrchestratorMcpService from "../../OrchestratorMcpService.ts"; | 51 | 52 | import * as ThreadMetadataMcpService from "../../ThreadMetadataMcpService.ts"; | @@ -63,6 +64,8 @@ const linkingDependencies = [ | 63 | 64 | PeerLinkRequests.PeerLinkRequests, | 64 | 65 | PeerLinks.PeerLinks, | 65 | 66 | ]; | | 67 | +/** A send there may record a follow-up to a remote task. */ | | 68 | +const sendDependencies = [...dependencies, RemoteDelegation.RemoteDelegation]; | 66 | 69 | const threadMetadataDependencies = [ | 67 | 70 | McpInvocationContext.McpInvocationContext, | 68 | 71 | ThreadManagementService.ThreadManagementService, | @@ -279,7 +282,7 @@ const ThreadSendTool = Tool.make("t3_thread_send", { | 279 | 282 | success: OrchestratorMcpThreadSendResult, | 280 | 283 | failure: OrchestratorMcpFailure, | 281 | 284 | failureMode: "return", | 282 | | - dependencies, | | 285 | + dependencies: sendDependencies, | 283 | 286 | }) | 284 | 287 | .annotate(Tool.Title, "Send to a T3 thread") | 285 | 288 | .annotate(Tool.Destructive, true) | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/CommandPolicy.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,7 @@ import { | 10 | 10 | ProviderInstanceId, | 11 | 11 | ThreadId, | 12 | 12 | } from "@t3tools/contracts"; | | 13 | +import { latestExecutedRun } from "@t3tools/shared/orchestrationV2ThreadError"; | 13 | 14 | import * as Context from "effect/Context"; | 14 | 15 | import * as Effect from "effect/Effect"; | 15 | 16 | import * as Layer from "effect/Layer"; | @@ -125,6 +126,15 @@ export function isActiveRun(run: OrchestrationV2Run): boolean { | 125 | 126 | ); | 126 | 127 | } | 127 | 128 |
| | 129 | +/** The run a follow-up to a finished child binds to: the latest active one, else the last that ran. */ | | 130 | +export function followUpOwnerRun( | | 131 | + runs: ReadonlyArray<OrchestrationV2Run>, | | 132 | +): OrchestrationV2Run | null { | | 133 | + return ( | | 134 | + runs.filter(isActiveRun).toSorted((a, b) => b.ordinal - a.ordinal)[0] ?? latestExecutedRun(runs) | | 135 | + ); | | 136 | +} | | 137 | + | 128 | 138 | function providerTurnCapabilities( | 129 | 139 | projection: OrchestrationV2ThreadProjection, | 130 | 140 | run: OrchestrationV2Run, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +17-5Lines changed: 17 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -107,6 +107,7 @@ import { | 107 | 107 | import { CheckpointServiceV2 } from "./CheckpointService.ts"; | 108 | 108 | import { | 109 | 109 | CommandPolicyV2, | | 110 | + followUpOwnerRun, | 110 | 111 | goalCommandStopsActiveRun, | 111 | 112 | isActiveRun, | 112 | 113 | isNativeMaintenanceCommand, | @@ -8952,9 +8953,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 8952 | 8953 | task.id === (child.forkedFrom?.type === "node" ? child.forkedFrom.nodeId : undefined), | 8953 | 8954 | ); | 8954 | 8955 | if (original === undefined || original.result === null) return; | 8955 | | - const owner = | 8956 | | - parent.runs.filter(isActiveRun).toSorted((a, b) => b.ordinal - a.ordinal)[0] ?? | 8957 | | - latestExecutedRun(parent.runs); | | 8956 | + const owner = followUpOwnerRun(parent.runs); | 8958 | 8957 | if (owner === null || owner.rootNodeId === null) return; | 8959 | 8958 | const now = message.createdAt; | 8960 | 8959 | const adapter = yield* providerAdapters.get(run.providerInstanceId); | @@ -9072,7 +9071,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 9072 | 9071 | events: Ref.Ref<Array<OrchestrationV2DomainEvent>>, | 9073 | 9072 | ) { | 9074 | 9073 | const parentProjection = yield* projectionStore | 9075 | | - .getThreadRecords(command.parentThreadId, ["runs", "providerTurns", "attempts"]) | | 9074 | + .getThreadRecords(command.parentThreadId, ["runs", "providerTurns", "attempts", "subagents"]) | 9076 | 9075 | .pipe( | 9077 | 9076 | Effect.mapError( | 9078 | 9077 | (cause) => new OrchestratorProjectionError({ threadId: command.parentThreadId, cause }), | @@ -9081,7 +9080,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 9081 | 9080 | const parentRun = parentProjection.runs.find( | 9082 | 9081 | (candidate) => candidate.id === command.parentRunId, | 9083 | 9082 | ); | 9084 | | - if (parentRun === undefined || !isActiveRun(parentRun) || parentRun.rootNodeId === null) { | | 9083 | + // A follow-up to a finished task there binds as one to a child here does. | | 9084 | + const followsUp = parentProjection.subagents.some( | | 9085 | + (task) => | | 9086 | + task.result !== null && | | 9087 | + task.remoteChild?.environmentId === command.remoteChild.environmentId && | | 9088 | + task.remoteChild.threadId === command.remoteChild.threadId, | | 9089 | + ); | | 9090 | + if ( | | 9091 | + parentRun === undefined || | | 9092 | + (followsUp | | 9093 | + ? parentRun.id !== followUpOwnerRun(parentProjection.runs)?.id | | 9094 | + : !isActiveRun(parentRun)) || | | 9095 | + parentRun.rootNodeId === null | | 9096 | + ) { | 9085 | 9097 | return yield* new OrchestratorDispatchError({ | 9086 | 9098 | commandId: command.commandId, | 9087 | 9099 | commandType: command.type, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/peer/PeerForwarding.test.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -314,7 +314,10 @@ const makeLaptop = Effect.gen(function* () { | 314 | 314 | Layer.mock(ProjectCloneTracker.ProjectCloneTracker)({}), | 315 | 315 | Layer.mock(SecretRequests.SecretRequests)({}), | 316 | 316 | ServerSettings.layerTest(), | 317 | | - Layer.mock(RemoteDelegation.RemoteDelegation)({}), | | 317 | + // No thread here delegated to the box, so a send there is no follow-up. | | 318 | + Layer.mock(RemoteDelegation.RemoteDelegation)({ | | 319 | + followUp: (_scope, _environmentId, _request, sent) => Effect.succeed(sent), | | 320 | + }), | 318 | 321 | Layer.mock(PeerLinkRequests.PeerLinkRequests)({}), | 319 | 322 | ), | 320 | 323 | ), | |
|