GeezerVison is showing github.com — feat(server): dispatch tools refuse unfilled briefs and misplaced mod… · mediavee/t3code@4de457f
File treeExpand file treeCollapse file tree Open diff view settings orchestration-v2/testkit/fixtures
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpService.test.ts +84-1Lines changed: 84 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4864,7 +4864,7 @@ describe("OrchestratorMcpService provider resolution", () => { | 4864 | 4864 |
| 4865 | 4865 | it.effect("accepts the inherited options it reports back as a delegate target", () => | 4866 | 4866 | Effect.gen(function* () { | 4867 | | - // t3_thread_launch keeps a model selection as given, so a thread can hold an option its model lacks. | | 4867 | + // A thread's selection can hold an option its model lacks, as from an older launch. | 4868 | 4868 | const inherited = { | 4869 | 4869 | instanceId: codexInstanceId, | 4870 | 4870 | model: "gpt-5.4", | @@ -4953,6 +4953,89 @@ describe("OrchestratorMcpService provider resolution", () => { | 4953 | 4953 | }), | 4954 | 4954 | ); | 4955 | 4955 |
| | 4956 | + it.effect("refuses an unfilled brief before anything starts, unless sent on purpose", () => | | 4957 | + Effect.gen(function* () { | | 4958 | + const commands: Array<unknown> = []; | | 4959 | + const task = { | | 4960 | + id: taskId, | | 4961 | + threadId: parentThreadId, | | 4962 | + origin: "app_owned", | | 4963 | + childThreadId, | | 4964 | + providerInstanceId: codexInstanceId, | | 4965 | + model: "gpt-5.4", | | 4966 | + status: "running", | | 4967 | + result: null, | | 4968 | + }; | | 4969 | + const dependencies = Layer.mergeAll( | | 4970 | + NodeServices.layer, | | 4971 | + NodeCrypto.layer, | | 4972 | + Layer.mock(ThreadManagementService.ThreadManagementService)({ | | 4973 | + hasReceipt: () => Effect.succeed(false), | | 4974 | + assertProviderInstanceEnabled: () => Effect.void, | | 4975 | + getThreadRecords: (threadId) => | | 4976 | + Effect.succeed( | | 4977 | + threadId === parentThreadId | | 4978 | + ? parentProjection(commands.length > 0 ? [task] : []) | | 4979 | + : childProjection, | | 4980 | + ), | | 4981 | + dispatch: (command) => | | 4982 | + Effect.sync(() => { | | 4983 | + commands.push(command); | | 4984 | + return { | | 4985 | + sequence: 1, | | 4986 | + storedEvents: [{ event: { type: "subagent.updated", payload: task } }], | | 4987 | + } as never; | | 4988 | + }), | | 4989 | + }), | | 4990 | + ServerSettings.layerTest(), | | 4991 | + providerRegistryLayer([ | | 4992 | + providerSnapshot({ | | 4993 | + instanceId: codexInstanceId, | | 4994 | + driver: ProviderDriverKind.make("codex"), | | 4995 | + model: "gpt-5.4", | | 4996 | + }), | | 4997 | + ]), | | 4998 | + adapterRegistryLayer([codexInstanceId]), | | 4999 | + Layer.mock(ProjectService.ProjectService)({ getById: () => Effect.succeedNone }), | | 5000 | + Layer.mock(SecretRequests.SecretRequests)({}), | | 5001 | + Layer.mock(RemoteDelegation.RemoteDelegation)({}), | | 5002 | + Layer.mock(ScheduledTaskService.ScheduledTaskService)({}), | | 5003 | + ); | | 5004 | + yield* Effect.gen(function* () { | | 5005 | + const service = yield* OrchestratorMcpService.OrchestratorMcpService; | | 5006 | + for (const [brief, match] of [ | | 5007 | + ["Review this.\n\n<<BRIEF>>", "<<BRIEF>>"], | | 5008 | + ["$(cat brief.md)", "$(cat brief.md)"], | | 5009 | + ["Do {{ task_name }} now.", "{{ task_name }}"], | | 5010 | + ["$(< brief.md)", "$(< brief.md)"], | | 5011 | + ["`cat brief.md`", "`cat brief.md`"], | | 5012 | + ] as const) { | | 5013 | + const error = yield* service | | 5014 | + .delegateTask(scope, { task: brief, mode: "async" }) | | 5015 | + .pipe(Effect.flip); | | 5016 | + assert.equal(error.code, "invalid_request"); | | 5017 | + assert.include(error.message, JSON.stringify(match)); | | 5018 | + assert.include(error.message, "allowLiteralTemplates: true"); | | 5019 | + } | | 5020 | + assert.lengthOf(commands, 0); | | 5021 | + | | 5022 | + // Commands a worker is asked to run are not file substitutions. | | 5023 | + yield* service.delegateTask(scope, { | | 5024 | + task: 'Run `git worktree add "$(mktemp -d)/wt"`, then fill {{ secrets["KEY"].value }}.', | | 5025 | + mode: "async", | | 5026 | + clientRequestId: "delegate-commands", | | 5027 | + }); | | 5028 | + yield* service.delegateTask(scope, { | | 5029 | + task: "Explain the `<<BRIEF>>` placeholder in `$(cat brief.md)`.", | | 5030 | + allowLiteralTemplates: true, | | 5031 | + mode: "async", | | 5032 | + clientRequestId: "delegate-literal", | | 5033 | + }); | | 5034 | + assert.lengthOf(commands, 2); | | 5035 | + }).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(dependencies)))); | | 5036 | + }), | | 5037 | + ); | | 5038 | + | 4956 | 5039 | it.effect("validates options against the model a target names by alias", () => | 4957 | 5040 | Effect.gen(function* () { | 4958 | 5041 | const dependencies = Layer.mergeAll( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpService.ts +27-10Lines changed: 27 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,6 +64,7 @@ import { | 64 | 64 | ThreadId, | 65 | 65 | isOrchestrationV2WorkActive, | 66 | 66 | latestProviderTurnForAttempt, | | 67 | + literalTemplateRefusal, | 67 | 68 | resolveProjectProviderInstanceEnabled, | 68 | 69 | SECRET_REQUEST_UNSAVED_NOTE, | 69 | 70 | } from "@t3tools/contracts"; | @@ -483,7 +484,7 @@ function providerConstraints( | 483 | 484 | return constraints; | 484 | 485 | } | 485 | 486 |
| 486 | | -/** t3_thread_launch keeps a selection as given, so it can hold options its model does not have. */ | | 487 | +/** A thread's selection can hold options its model lacks, as from t3_thread_configure or an older launch. */ | 487 | 488 | function knownOptions( | 488 | 489 | options: ReadonlyArray<ProviderOptionSelection> | undefined, | 489 | 490 | descriptors: ReadonlyArray<ProviderOptionDescriptor> | undefined, | @@ -527,6 +528,25 @@ function invalidOptionSelections( | 527 | 528 | return problems; | 528 | 529 | } | 529 | 530 |
| | 531 | +/** delegate_task and t3_thread_launch accept the same option selections for a model. */ | | 532 | +export const requireAdvertisedOptions = ( | | 533 | + provider: ServerProvider, | | 534 | + model: string, | | 535 | + options: ReadonlyArray<ProviderOptionSelection> | undefined, | | 536 | +) => { | | 537 | + const descriptors = advertisedModel(provider, model)?.capabilities?.optionDescriptors; | | 538 | + const requested = withLegacyUltracodeEffort(descriptors, options); | | 539 | + const invalid = requested === undefined ? [] : invalidOptionSelections(requested, descriptors); | | 540 | + return invalid.length === 0 | | 541 | + ? Effect.void | | 542 | + : Effect.fail( | | 543 | + failure( | | 544 | + "invalid_request", | | 545 | + `Model ${model} on provider ${provider.instanceId} rejected options: ${invalid.join(" ")}`, | | 546 | + ), | | 547 | + ); | | 548 | +}; | | 549 | + | 530 | 550 | function taskStatusForRun( | 531 | 551 | run: Pick<OrchestrationV2Run, "status" | "restartCancelledBackgroundWork"> | undefined, | 532 | 552 | ): OrchestratorMcpDelegateTaskResult["status"] { | @@ -1785,15 +1805,7 @@ const make = Effect.gen(function* () { | 1785 | 1805 | const advertised = advertisedModel(provider, model); | 1786 | 1806 | const descriptors = advertised?.capabilities?.optionDescriptors; | 1787 | 1807 | const requestedOptions = withLegacyUltracodeEffort(descriptors, input.target?.options); | 1788 | | - if (requestedOptions !== undefined) { | 1789 | | - const invalid = invalidOptionSelections(requestedOptions, descriptors); | 1790 | | - if (invalid.length > 0) { | 1791 | | - return yield* failure( | 1792 | | - "invalid_request", | 1793 | | - `Model ${model} on provider ${instanceId} rejected options: ${invalid.join(" ")}`, | 1794 | | - ); | 1795 | | - } | 1796 | | - } | | 1808 | + yield* requireAdvertisedOptions(provider, model, input.target?.options); | 1797 | 1809 |
| 1798 | 1810 | const inheritsOptions = | 1799 | 1811 | instanceId === inheritedSelection.instanceId && | @@ -2898,6 +2910,11 @@ const make = Effect.gen(function* () { | 2898 | 2910 | }; | 2899 | 2911 | }), | 2900 | 2912 | delegateTask: Effect.fn("OrchestratorMcpService.delegateTask")(function* (callerScope, input) { | | 2913 | + const templateRefusal = | | 2914 | + input.allowLiteralTemplates === true | | 2915 | + ? undefined | | 2916 | + : literalTemplateRefusal("task", input.task); | | 2917 | + if (templateRefusal !== undefined) return yield* failure("invalid_request", templateRefusal); | 2901 | 2918 | const environmentId = | 2902 | 2919 | input.target === undefined | 2903 | 2920 | ? undefined | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/advertisedJsonSchema.test.ts +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,8 @@ import * as Schema from "effect/Schema"; | 3 | 3 | import { Tool } from "effect/ai"; | 4 | 4 |
| 5 | 5 | import { advertisedJsonSchema } from "./advertisedJsonSchema.ts"; | | 6 | +import { OrchestratorToolkit } from "./toolkits/orchestrator/tools.ts"; | | 7 | +import { ProjectToolkit } from "./toolkits/project/tools.ts"; | 6 | 8 | import { ThemeToolkit } from "./toolkits/theme/tools.ts"; | 7 | 9 |
| 8 | 10 | it("drops JSON noise, merges tagged unions, and keeps null where it is required", () => { | @@ -59,3 +61,28 @@ it("advertises only string enum values, which Gemini requires", () => { | 59 | 61 |
| 60 | 62 | expect(enumValues.filter((value) => typeof value !== "string")).toEqual([]); | 61 | 63 | }); | | 64 | + | | 65 | +it("advertises the key-checked dispatch objects without allOf or open additionalProperties", () => { | | 66 | + const keys: Array<string> = []; | | 67 | + const open: Array<unknown> = []; | | 68 | + const collect = (node: unknown): void => { | | 69 | + if (Array.isArray(node)) return node.forEach(collect); | | 70 | + if (typeof node !== "object" || node === null) return; | | 71 | + for (const [key, value] of Object.entries(node)) { | | 72 | + keys.push(key); | | 73 | + if (key === "additionalProperties" && (value === true || JSON.stringify(value) === "{}")) | | 74 | + open.push(value); | | 75 | + collect(value); | | 76 | + } | | 77 | + }; | | 78 | + const propertiesOf = (tool: Tool.Any) => | | 79 | + advertisedJsonSchema(tool).properties as Record<string, unknown>; | | 80 | + const { target } = propertiesOf(OrchestratorToolkit.tools.delegate_task); | | 81 | + const { modelSelection } = propertiesOf(ProjectToolkit.tools.t3_thread_launch); | | 82 | + collect([target, modelSelection]); | | 83 | + | | 84 | + expect(keys).not.toContain("allOf"); | | 85 | + expect(open).toEqual([]); | | 86 | + expect(target).toMatchObject({ properties: { model: {}, options: {} } }); | | 87 | + expect(modelSelection).toMatchObject({ required: ["model"] }); | | 88 | +}); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/advertisedJsonSchema.ts +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,8 @@ type JsonObject = { [key: string]: Json }; | 7 | 7 | const isNullSchema = (value: Json) => | 8 | 8 | Predicate.isObject(value) && value.type === "null" && Object.keys(value).length === 1; | 9 | 9 |
| | 10 | +const isEmptyObject = (value: Json) => Predicate.isObject(value) && Object.keys(value).length === 0; | | 11 | + | 10 | 12 | const isBareAnyOf = (value: Json): value is { anyOf: Array<Json> } => | 11 | 13 | Predicate.isObject(value) && Array.isArray(value.anyOf) && Object.keys(value).length === 1; | 12 | 14 |
| @@ -77,7 +79,8 @@ const compact = (schema: Json, optional: boolean): Json => { | 77 | 79 | const required = new Set(Array.isArray(schema.required) ? schema.required : []); | 78 | 80 | let result: JsonObject = {}; | 79 | 81 | for (const [key, value] of Object.entries(schema)) { | 80 | | - if (key === "additionalProperties" && value === true) continue; | | 82 | + // `{}` permits any value, as `true` does; a rest record renders it, and Gemini rejects both. | | 83 | + if (key === "additionalProperties" && (value === true || isEmptyObject(value))) continue; | 81 | 84 | if (SAFE_INTEGER_BOUNDS[key] === value) continue; | 82 | 85 | if (isNonStringEnum(key, value)) continue; | 83 | 86 | result[key] = | @@ -90,6 +93,15 @@ const compact = (schema: Json, optional: boolean): Json => { | 90 | 93 | ) | 91 | 94 | : compact(value, false); | 92 | 95 | } | | 96 | + if (Array.isArray(result.allOf)) { | | 97 | + // A rest record adds `{type:"object"}` once its open `additionalProperties` is gone. | | 98 | + const allOf = result.allOf.filter( | | 99 | + (entry) => | | 100 | + !(Predicate.isObject(entry) && entry.type === "object" && Object.keys(entry).length === 1), | | 101 | + ); | | 102 | + if (allOf.length === 0) delete result.allOf; | | 103 | + else result.allOf = allOf; | | 104 | + } | 93 | 105 | if (Array.isArray(result.anyOf)) { | 94 | 106 | const branches = result.anyOf | 95 | 107 | .flatMap((branch) => (isBareAnyOf(branch) ? branch.anyOf : [branch])) | @@ -103,7 +115,7 @@ const compact = (schema: Json, optional: boolean): Json => { | 103 | 115 |
| 104 | 116 | /** | 105 | 117 | * The input schema T3 advertises for a tool. Drops JSON noise that every thread pays for: | 106 | | - * `additionalProperties: true`, safe-integer bounds, nested `anyOf`, JSON-string fallbacks, and | | 118 | + * open `additionalProperties` and the `allOf` it leaves empty, safe-integer bounds, nested `anyOf`, JSON-string fallbacks, and | 107 | 119 | * the `null` branch of optional properties. A tagged union of objects becomes one object. | 108 | 120 | * Decoding still uses the full schema, so each tag's own required fields still apply. | 109 | 121 | */ | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/project/handlers.test.ts +73-3Lines changed: 73 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,6 +66,8 @@ const clientLaunchHarness = (input: { | 66 | 66 | readonly hostedAppUrl?: string; | 67 | 67 | /** The orchestrator refuses the new thread's mode, as under a managed ceiling. */ | 68 | 68 | readonly refusedByCeiling?: true; | | 69 | + /** The caller is a linked environment. */ | | 70 | + readonly linked?: true; | 69 | 71 | }) => { | 70 | 72 | const projectId = ProjectId.make("project:client-target"); | 71 | 73 | const modelSelection = { | @@ -115,6 +117,7 @@ const clientLaunchHarness = (input: { | 115 | 117 | label: "Claude Code", | 116 | 118 | access: input.runtimeModeCeiling, | 117 | 119 | hostedAppUrl: input.hostedAppUrl, | | 120 | + ...(input.linked === undefined ? {} : { linked: true }), | 118 | 121 | }, | 119 | 122 | issuedAt: 0, | 120 | 123 | capabilities: new Set(["orchestration" as const]), | @@ -401,7 +404,9 @@ it.effect("a launch with a model its provider does not advertise creates no thre | 401 | 404 | name: "Opus", | 402 | 405 | aliases: ["opus"], | 403 | 406 | isCustom: false, | 404 | | - capabilities: null, | | 407 | + capabilities: { | | 408 | + optionDescriptors: [{ id: "fastMode", label: "Fast", type: "boolean" }], | | 409 | + }, | 405 | 410 | } | 406 | 411 | : { slug: "gpt-5.4", name: "GPT", isCustom: false, capabilities: null }, | 407 | 412 | ], | @@ -434,12 +439,12 @@ it.effect("a launch with a model its provider does not advertise creates no thre | 434 | 439 | McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(dependencies)), | 435 | 440 | ), | 436 | 441 | ); | 437 | | - const launchWith = (instanceId: string, model: string) => | | 442 | + const launchWith = (instanceId: string, model: string, extra: object = {}) => | 438 | 443 | toolkit | 439 | 444 | .handle("t3_thread_launch", { | 440 | 445 | title: "Fix", | 441 | 446 | projectId, | 442 | | - modelSelection: { instanceId: ProviderInstanceId.make(instanceId), model }, | | 447 | + modelSelection: { instanceId: ProviderInstanceId.make(instanceId), model, ...extra }, | 443 | 448 | }) | 444 | 449 | .pipe( | 445 | 450 | Stream.unwrap, | @@ -459,6 +464,17 @@ it.effect("a launch with a model its provider does not advertise creates no thre | 459 | 464 | ), | 460 | 465 | }); | 461 | 466 | } | | 467 | + expect( | | 468 | + yield* launchWith("claudeAgent", "claude-opus", { | | 469 | + options: [{ id: "effort", value: "max" }], | | 470 | + }), | | 471 | + ).toMatchObject({ | | 472 | + code: "invalid_request", | | 473 | + message: expect.stringContaining("Unknown option effort; supported options: fastMode."), | | 474 | + }); | | 475 | + expect(String(yield* launchWith("claudeAgent", "claude-opus", { effort: "max" }))).toContain( | | 476 | + 'Unknown key "effort" in modelSelection.', | | 477 | + ); | 462 | 478 | expect(launched).toHaveLength(0); | 463 | 479 |
| 464 | 480 | expect(yield* launchWith("claudeAgent", "Opus")).toMatchObject({ | @@ -471,6 +487,60 @@ it.effect("a launch with a model its provider does not advertise creates no thre | 471 | 487 | }), | 472 | 488 | ); | 473 | 489 |
| | 490 | +it.effect("a launch whose message still holds a placeholder creates no thread", () => | | 491 | + Effect.gen(function* () { | | 492 | + const launchWith = Effect.fn("test.launchWith")(function* ( | | 493 | + params: { readonly message: string; readonly allowLiteralTemplates?: true }, | | 494 | + linked?: true, | | 495 | + ) { | | 496 | + const launched: Array<ThreadLaunch.ThreadLaunchInput> = []; | | 497 | + const { projectId, dependencies } = clientLaunchHarness({ | | 498 | + runtimeModeCeiling: "full-access", | | 499 | + launched, | | 500 | + ...(linked === undefined ? {} : { linked }), | | 501 | + }); | | 502 | + const toolkit = yield* ProjectToolkit.pipe( | | 503 | + Effect.provide( | | 504 | + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe( | | 505 | + Layer.provide(dependencies), | | 506 | + ), | | 507 | + ), | | 508 | + ); | | 509 | + const result = yield* toolkit | | 510 | + .handle("t3_thread_launch", { | | 511 | + title: "Review", | | 512 | + projectId, | | 513 | + ...params, | | 514 | + ...(linked === undefined | | 515 | + ? {} | | 516 | + : { | | 517 | + delegatedFrom: { | | 518 | + environmentId: EnvironmentId.make("environment-origin"), | | 519 | + threadId: ThreadId.make("thread-origin"), | | 520 | + title: "Origin", | | 521 | + }, | | 522 | + }), | | 523 | + }) | | 524 | + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); | | 525 | + return { result: result.at(-1)?.result, launched }; | | 526 | + }); | | 527 | + | | 528 | + for (const message of ["Review.\n\n<<BRIEF>>", "$(cat x)"]) { | | 529 | + const refused = yield* launchWith({ message }); | | 530 | + expect(refused.result).toMatchObject({ | | 531 | + code: "invalid_request", | | 532 | + message: expect.stringContaining("pass allowLiteralTemplates: true"), | | 533 | + }); | | 534 | + expect(refused.launched).toHaveLength(0); | | 535 | + } | | 536 | + const literal = yield* launchWith({ message: "$(cat x)", allowLiteralTemplates: true }); | | 537 | + expect(literal.launched[0]?.initialMessage?.text).toBe("$(cat x)"); | | 538 | + // The origin's delegate_task already checked a linked environment's delegated brief. | | 539 | + const delegated = yield* launchWith({ message: "<<BRIEF>>" }, true); | | 540 | + expect(delegated.launched).toHaveLength(1); | | 541 | + }), | | 542 | +); | | 543 | + | 474 | 544 | it.effect("a launch retried with the same clientRequestId replays the first one", () => | 475 | 545 | Effect.gen(function* () { | 476 | 546 | const launched: Array<ThreadLaunch.ThreadLaunchInput> = []; | |
|