GeezerVison is showing github.com — fix(server): a follow-up to a finished remote task is a new task that… · incognitojam/styal@4b8ed6c
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.test.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import * as AnalyticsService from "../telemetry/AnalyticsService.ts"; | 2 | 2 | import * as OrchestratorHandlers from "./toolkits/orchestrator/handlers.ts"; | 3 | 3 | import { OrchestratorToolkit } from "./toolkits/orchestrator/tools.ts"; | | 4 | +import * as RemoteDelegation from "../peer/RemoteDelegation.ts"; | 4 | 5 | import * as OrchestratorMcpService from "./OrchestratorMcpService.ts"; | 5 | 6 | import * as PeerLinkRequests from "../peer/PeerLinkRequests.ts"; | 6 | 7 | import * as PeerLinks from "../peer/PeerLinks.ts"; | @@ -833,6 +834,7 @@ it.effect.each([ | 833 | 834 | Effect.gen(function* () { | 834 | 835 | const dependencies = Layer.mergeAll( | 835 | 836 | NodeCrypto.layer, | | 837 | + Layer.mock(RemoteDelegation.RemoteDelegation)({}), | 836 | 838 | Layer.succeed(McpInvocationContext.McpInvocationContext, scope), | 837 | 839 | Layer.mock(ThreadManagement.ThreadManagementService)({ | 838 | 840 | getThreadShell: (threadId) => Effect.succeed(shells.get(threadId) ?? null), | |
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 |
|---|
@@ -47,6 +47,7 @@ const refuseOutsideConstruction = (token: symbol) => { | 47 | 47 | let declare: <P, A, E, R>( | 48 | 48 | handle: (params: P) => Effect.Effect<A, E, R>, | 49 | 49 | remote: RemoteCheck<P> | undefined, | | 50 | + forwarded?: Forwarded<P, A, E, R>, | 50 | 51 | ) => Declaration<(params: P) => Effect.Effect<A, E, R>>; | 51 | 52 |
| 52 | 53 | /** | @@ -59,6 +60,16 @@ type RemoteCheck<P> = ( | 59 | 60 | ) => Effect.Effect<unknown, OrchestratorMcpFailure, Effect.Services<typeof writingCaller>>; | 60 | 61 | let remoteCheckOf: <Handler>(declaration: Declaration<Handler>) => RemoteCheck<never> | undefined; | 61 | 62 |
| | 63 | +/** What a call answered in a linked environment does here before it returns. */ | | 64 | +type Forwarded<P, A, E, R> = ( | | 65 | + params: P, | | 66 | + environmentId: EnvironmentId, | | 67 | + result: A, | | 68 | +) => Effect.Effect<A, E, R>; | | 69 | +let forwardedOf: <Handler>( | | 70 | + declaration: Declaration<Handler>, | | 71 | +) => Forwarded<never, unknown, unknown, unknown> | undefined; | | 72 | + | 62 | 73 | type CheckedHandlerOf<D> = D extends Declaration<infer Handler> ? Handler : never; | 63 | 74 |
| 64 | 75 | interface CheckedHandler extends Struct.Lambda { | @@ -75,19 +86,29 @@ let handlersLayer: <Tools extends Record<string, Tool.Any>, EX, RX>( | 75 | 86 | export class Declaration<out Handler> { | 76 | 87 | readonly #handle: Handler; | 77 | 88 | readonly #remote: RemoteCheck<never> | undefined; | | 89 | + readonly #forwarded: Forwarded<never, unknown, unknown, unknown> | undefined; | 78 | 90 | private constructor( | 79 | 91 | token: typeof builtHere, | 80 | 92 | handle: Handler, | 81 | 93 | remote: RemoteCheck<never> | undefined, | | 94 | + forwarded: Forwarded<never, unknown, unknown, unknown> | undefined, | 82 | 95 | ) { | 83 | 96 | refuseOutsideConstruction(token); | 84 | 97 | this.#handle = handle; | 85 | 98 | this.#remote = remote; | | 99 | + this.#forwarded = forwarded; | 86 | 100 | } | 87 | 101 | static { | 88 | | - declare = (handle, remote) => new Declaration(builtHere, handle, remote); | | 102 | + declare = (handle, remote, forwarded) => | | 103 | + new Declaration( | | 104 | + builtHere, | | 105 | + handle, | | 106 | + remote, | | 107 | + forwarded as Forwarded<never, unknown, unknown, unknown> | undefined, | | 108 | + ); | 89 | 109 | checkedHandler = Struct.lambda<CheckedHandler>((declaration) => declaration.#handle); | 90 | 110 | remoteCheckOf = (declaration) => declaration.#remote; | | 111 | + forwardedOf = (declaration) => declaration.#forwarded; | 91 | 112 | } | 92 | 113 | } | 93 | 114 |
| @@ -315,6 +336,20 @@ export const either = <P, A, E, R, A2, E2, R2>( | 315 | 336 | ); | 316 | 337 | }; | 317 | 338 |
| | 339 | +/** | | 340 | + * `declaration`, whose result from a linked environment then goes through | | 341 | + * `forwarded` here, for what this environment records about the call. | | 342 | + */ | | 343 | +export const afterForwarding = <P, A, E, R, E2, R2>( | | 344 | + declaration: Declaration<(params: P) => Effect.Effect<A, E, R>>, | | 345 | + forwarded: Forwarded<P, A, E2, R2>, | | 346 | +) => | | 347 | + declare( | | 348 | + checkedHandler(declaration) as (params: P) => Effect.Effect<A, E | E2, R | R2>, | | 349 | + remoteCheckOf(declaration) as RemoteCheck<P> | undefined, | | 350 | + forwarded, | | 351 | + ); | | 352 | + | 318 | 353 | const fullAccessRequired = | 319 | 354 | "This operation needs a live Full access thread in default interaction mode, or a Full access client."; | 320 | 355 |
| @@ -440,6 +475,9 @@ const routed = Effect.fn("McpToolAccess.routed")(function* <Tools extends Record | 440 | 475 | const tool = toolkit.tools[name]; | 441 | 476 | if (tool === undefined || !takesLinkedEnvironment(tool)) continue; | 442 | 477 | const remote = remoteCheckOf(declaration); | | 478 | + const forwarded = forwardedOf(declaration) as | | 479 | + | Forwarded<object, unknown, Tool.Failure<Tools[keyof Tools]>, never> | | 480 | + | undefined; | 443 | 481 | if (remote === undefined) | 444 | 482 | return yield* Effect.die( | 445 | 483 | `${name} takes a linked environment, but it acts as its calling thread, which is here.`, | @@ -455,7 +493,11 @@ const routed = Effect.fn("McpToolAccess.routed")(function* <Tools extends Record | 455 | 493 | if (scope.linkedEnvironments === false) return yield* PeerForwarding.linkedEnvironmentsOff; | 456 | 494 | yield* (remote as RemoteCheck<typeof params>)(params); | 457 | 495 | if (Option.isNone(forwarding)) return yield* linksUnavailable; | 458 | | - return yield* forwarding.value.call(scope, tool, target, { ...params, environmentId }); | | 496 | + const result = yield* forwarding.value.call(scope, tool, target, { | | 497 | + ...params, | | 498 | + environmentId, | | 499 | + }); | | 500 | + return forwarded === undefined ? result : yield* forwarded(params, target, result); | 459 | 501 | }); | 460 | 502 | } | 461 | 503 | return handlers as Toolkit.HandlersFrom<Tools>; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/orchestrator/handlers.ts +23-13Lines changed: 23 additions & 13 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,22 +169,31 @@ export 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 | + // Steering or restarting a turn stays a normal send, under the normal limits. | | 183 | + reportTarget: (input) => | | 184 | + input.freshContext !== true && | | 185 | + (input.mode === undefined || input.mode === "auto" || input.mode === "queue") | | 186 | + ? input.threadId | | 187 | + : undefined, | | 188 | + }, | | 189 | + ), | | 190 | + // There the thread's parent is here, so only this side can make a follow-up a task. | | 191 | + (input, environmentId, sent) => | 174 | 192 | Effect.gen(function* () { | 175 | 193 | const scope = yield* McpInvocationContext.McpInvocationContext; | 176 | | - const service = yield* OrchestratorMcpService.OrchestratorMcpService; | 177 | | - return yield* service.sendToThread(scope, input); | | 194 | + const remote = yield* RemoteDelegation.RemoteDelegation; | | 195 | + return yield* remote.followUp(scope, environmentId, input, sent); | 178 | 196 | }), | 179 | | - { | 180 | | - // Steering or restarting a turn stays a normal send, under the normal limits. | 181 | | - reportTarget: (input) => | 182 | | - input.freshContext !== true && | 183 | | - (input.mode === undefined || input.mode === "auto" || input.mode === "queue") | 184 | | - ? input.threadId | 185 | | - : undefined, | 186 | | - }, | 187 | 197 | ), | 188 | 198 | t3_thread_wait: McpToolAccess.reads((input) => | 189 | 199 | Effect.gen(function* () { | |
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/Orchestrator.ts +25-7Lines changed: 25 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9870,20 +9870,35 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 9870 | 9870 | ) { | 9871 | 9871 | const { followUpOf } = command.remoteChild; | 9872 | 9872 | const parentProjection = yield* projectionStore | 9873 | | - .getThreadRecords( | 9874 | | - command.parentThreadId, | 9875 | | - ["runs", "providerTurns", "attempts", "subagents"], | 9876 | | - { subagentIds: followUpOf === undefined ? [] : [followUpOf] }, | 9877 | | - ) | | 9873 | + .getThreadRecords(command.parentThreadId, ["runs", "providerTurns", "attempts", "subagents"]) | 9878 | 9874 | .pipe( | 9879 | 9875 | Effect.mapError( | 9880 | 9876 | (cause) => new OrchestratorProjectionError({ threadId: command.parentThreadId, cause }), | 9881 | 9877 | ), | 9882 | 9878 | ); | | 9879 | + // Explicit sends and later-run observers can discover the same remote run concurrently. | | 9880 | + if ( | | 9881 | + command.remoteChild.runId !== undefined && | | 9882 | + parentProjection.subagents.some( | | 9883 | + (task) => | | 9884 | + task.remoteChild?.environmentId === command.remoteChild.environmentId && | | 9885 | + task.remoteChild.threadId === command.remoteChild.threadId && | | 9886 | + task.remoteChild.runId === command.remoteChild.runId, | | 9887 | + ) | | 9888 | + ) | | 9889 | + return; | | 9890 | + const followsUp = parentProjection.subagents.some( | | 9891 | + (task) => | | 9892 | + task.result !== null && | | 9893 | + task.remoteChild?.environmentId === command.remoteChild.environmentId && | | 9894 | + task.remoteChild.threadId === command.remoteChild.threadId, | | 9895 | + ); | 9883 | 9896 | const followed = parentProjection.subagents.find( | 9884 | 9897 | (task) => | 9885 | 9898 | task.id === followUpOf && | 9886 | | - task.remoteChild?.followsAfterRunId !== undefined && | | 9899 | + task.result !== null && | | 9900 | + task.remoteChild !== undefined && | | 9901 | + (task.remoteChild.followsAfterRunId !== undefined || command.parentRunId !== undefined) && | 9887 | 9902 | task.remoteChild.environmentId === command.remoteChild.environmentId && | 9888 | 9903 | task.remoteChild.threadId === command.remoteChild.threadId, | 9889 | 9904 | ); | @@ -9920,7 +9935,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 9920 | 9935 | parentRun === undefined || | 9921 | 9936 | parentRun.rootNodeId === null || | 9922 | 9937 | parentNodeId == null || | 9923 | | - (followed === undefined && !isActiveRun(parentRun)) | | 9938 | + (followed === undefined && | | 9939 | + (followsUp | | 9940 | + ? parentRun.id !== followUpOwnerRun(parentProjection.runs)?.id | | 9941 | + : !isActiveRun(parentRun))) | 9924 | 9942 | ) { | 9925 | 9943 | return yield* new OrchestratorDispatchError({ | 9926 | 9944 | commandId: command.commandId, | |
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 |
|---|
@@ -320,7 +320,10 @@ const makeLaptop = Effect.gen(function* () { | 320 | 320 | Layer.mock(ProjectCloneTracker.ProjectCloneTracker)({}), | 321 | 321 | Layer.mock(SecretRequests.SecretRequests)({}), | 322 | 322 | ServerSettings.layerTest(), | 323 | | - Layer.mock(RemoteDelegation.RemoteDelegation)({}), | | 323 | + // No thread here delegated to the box, so a send there is no follow-up. | | 324 | + Layer.mock(RemoteDelegation.RemoteDelegation)({ | | 325 | + followUp: (_scope, _environmentId, _request, sent) => Effect.succeed(sent), | | 326 | + }), | 324 | 327 | Layer.mock(PeerLinkRequests.PeerLinkRequests)({}), | 325 | 328 | ), | 326 | 329 | ), | |
|