GeezerVison is showing github.com — Scheduled tasks: check the saving thread's MCP profile in the service… · scuttletoby/t3code@1c95c08
File treeExpand file treeCollapse file tree Open diff view settings mcp/toolkits/orchestrator
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .github/fork-seams.json +5-16Lines changed: 5 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 260 | 260 | "if (slot.skipped !== null) return slot.skipped; // Fork: a skipped run is recorded", | 261 | 261 | "yield* slot.end(threadId); // Fork: the slot names the run's thread", | 262 | 262 | "Effect.ensuring(runSlots.ended(task.id)), // Fork: a failed run frees its slot", | 263 | | - "runSlots.directDelivery(deliveryId, request, pendingWebhooks.invalidate), // Fork: a waiting direct delivery is stored" | 264 | | - ], | 265 | | - "announced": { | 266 | | - "since": "2026-11-21", | 267 | | - "added": [ | 268 | | - "yield* runSlots.saving(task, existingTask); // Fork: the saving thread's MCP profile" | 269 | | - ] | 270 | | - } | | 263 | + "runSlots.directDelivery(deliveryId, request, pendingWebhooks.invalidate), // Fork: a waiting direct delivery is stored", | | 264 | + "yield* runSlots.saving(task, existingTask); // Fork: the saving thread's MCP profile" | | 265 | + ] | 271 | 266 | }, | 272 | 267 | "apps/server/src/mcp/toolkits/orchestrator/handlers.ts": { | 273 | 268 | "why": "schedule_task from a thread whose MCP profile is narrower than \"all\" is refused when the task's runs would start a thread with more than the caller's profile, on create and on a change to a task whose runs start new threads (fork/launchMcpProfile.ts, #693). A change from such a thread to the prompt or thread of a task bound to a wider thread is refused there too, before the service uses up a webhook secretRef (#699).", | 274 | 269 | "added": [ | 275 | 270 | "import * as LaunchMcpProfile from \"../../../fork/launchMcpProfile.ts\"; // Fork: MCP profile launch", | 276 | 271 | "yield* LaunchMcpProfile.checkScheduleMcpProfile(input); // Fork: MCP profile launch", | 277 | | - "yield* LaunchMcpProfile.checkScheduleEditMcpProfile(scheduledTaskId, bindToCurrentThread); // Fork: MCP profile launch" | 278 | | - ], | 279 | | - "announced": { | 280 | | - "since": "2026-11-21", | 281 | | - "added": [ | 282 | | - "yield* LaunchMcpProfile.checkEditMcpProfile(scheduledTaskId, bindToCurrentThread, prompt); // Fork: MCP profile launch" | 283 | | - ] | 284 | | - } | | 272 | + "yield* LaunchMcpProfile.checkEditMcpProfile(scheduledTaskId, bindToCurrentThread, prompt); // Fork: MCP profile launch" | | 273 | + ] | 285 | 274 | }, | 286 | 275 | "apps/server/src/mcp/toolkits/thread/handlers.ts": { | 287 | 276 | "why": "t3_thread_transfer's fork from a thread whose MCP profile is narrower than \"all\" is refused when the fork would get more than the caller's profile, and run_scheduled_task_now on a task whose runs would start such a thread (fork/launchMcpProfile.ts, #693); run_scheduled_task_now refused by the agent slot gate answers slots_full (fork/agentSlotRoutes.ts, #706; the prettier-ignore keeps upstream's run_scheduled_task_now lines as they are, since a second pipe step would make the formatter reflow them).", | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/fork/agentSlotRoutes.ts +38-3Lines changed: 38 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,7 @@ import { encodePendingTask } from "../scheduledTasks/webhookDedupe.ts"; | 27 | 27 | import type { WebhookRequest } from "../scheduledTasks/webhookTemplate.ts"; | 28 | 28 | import * as Scheduler from "../scheduling/Scheduler.ts"; | 29 | 29 | import * as SlotGate from "../slots/SlotGate.ts"; | | 30 | +import { makeScheduledTaskMcpProfiles } from "./scheduledTaskMcpProfile.ts"; | 30 | 31 |
| 31 | 32 | type Refused = Extract<SlotGate.SlotAcquire, { readonly type: "refuse" }>; | 32 | 33 | type FullDecision = Extract<SlotDecision, { readonly type: "refuse" }>; | @@ -317,6 +318,9 @@ export const makeScheduledRunSlots = Effect.fnUntraced(function* <W>( | 317 | 318 | // The production wiring gives scheduled runs the gate; a boot test looks for this span. | 318 | 319 | if (Option.isSome(gate)) | 319 | 320 | yield* Effect.void.pipe(Effect.withSpan("AgentSlotRoutes.scheduledRunsGated")); | | 321 | + // Who saved each task, and the run check on it: built here, where the service's one fork | | 322 | + // line already builds the slots, so the service needs no second build line. | | 323 | + const profiles = yield* makeScheduledTaskMcpProfiles(threads); | 320 | 324 |
| 321 | 325 | /** | 322 | 326 | * The slot each running task holds until its dispatch ends (one run per task at a time), so | @@ -472,6 +476,9 @@ export const makeScheduledRunSlots = Effect.fnUntraced(function* <W>( | 472 | 476 | yield* Effect.sleep(WEBHOOK_SLOT_RETRY); | 473 | 477 | const gone = yield* webhookGone(task).pipe(Effect.orElseSucceed(() => null)); | 474 | 478 | if (gone !== null) return yield* Effect.fail(new WebhookSkipped({ reason: gone })); | | 479 | + // Checked again as it would start: the owner may have widened the project meanwhile. | | 480 | + const refused = task.threadId === null ? yield* profiles.runRefusal(task) : null; | | 481 | + if (refused !== null) return yield* Effect.fail(new WebhookSkipped({ reason: refused })); | 475 | 482 | const target = yield* targetOf(task); | 476 | 483 | if (target.queuesOnly) return allowed(task, () => Effect.void); | 477 | 484 | const slot = yield* take(target.instanceId); | @@ -488,14 +495,36 @@ export const makeScheduledRunSlots = Effect.fnUntraced(function* <W>( | 488 | 495 | task: ScheduledTask, | 489 | 496 | trigger: "scheduled" | "manual" | "webhook", | 490 | 497 | now: DateTime.DateTime, | 491 | | - // The missed-run rescheduler: a run that waits is not rescheduled. | 492 | | - _reschedule: ( | | 498 | + // The missed-run rescheduler: a run that waits is not rescheduled; a refused one is. | | 499 | + reschedule: ( | 493 | 500 | task: ScheduledTask, | 494 | 501 | now: DateTime.DateTime, | 495 | 502 | reason: string, | 496 | 503 | runError: string, | 497 | 504 | ) => Effect.Effect<void, E, R>, | 498 | 505 | ) { | | 506 | + // A new thread wider than the profile the task was saved with starts nothing: on its schedule | | 507 | + // a failed run with the reason (moved to its next time), "run now" fails, a webhook skips. | | 508 | + const profileRefusal = task.threadId === null ? yield* profiles.runRefusal(task) : null; | | 509 | + if (profileRefusal !== null) { | | 510 | + if (trigger === "manual") { | | 511 | + return yield* new ScheduledTaskError({ message: profileRefusal, taskId: task.id }); | | 512 | + } | | 513 | + if (trigger === "webhook") | | 514 | + return yield* Effect.fail(new WebhookSkipped({ reason: profileRefusal })); | | 515 | + yield* reschedule(task, now, "MCP profile", profileRefusal).pipe( | | 516 | + Effect.catchCause((cause) => | | 517 | + Effect.logWarning("Could not record a scheduled run refused for its MCP profile", { | | 518 | + taskId: task.id, | | 519 | + cause, | | 520 | + }), | | 521 | + ), | | 522 | + ); | | 523 | + return { | | 524 | + skipped: { task, threadId: null, dispatchFailed: false }, | | 525 | + end: () => Effect.void, | | 526 | + } as ScheduledSlot; | | 527 | + } | 499 | 528 | if (Option.isNone(gate)) return { skipped: null, end: () => Effect.void } as ScheduledSlot; | 500 | 529 | const target = yield* targetOf(task); | 501 | 530 | if (target.queuesOnly) return allowed(task, () => Effect.void); | @@ -556,5 +585,11 @@ export const makeScheduledRunSlots = Effect.fnUntraced(function* <W>( | 556 | 585 | }); | 557 | 586 | yield* scheduler.register("agent-slot-notes", clearStaleNotes); | 558 | 587 |
| 559 | | - return { admit: admitRun, ended, directDelivery }; | | 588 | + return { | | 589 | + admit: admitRun, | | 590 | + ended, | | 591 | + directDelivery, | | 592 | + /** `ScheduledTaskService.upsert`, last before it writes the row (`scheduledTaskMcpProfile.ts`). */ | | 593 | + saving: profiles.saving, | | 594 | + }; | 560 | 595 | }); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/fork/launchMcpProfile.ts +43-9Lines changed: 43 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | /** | 2 | 2 | * Fork: a thread started by a narrower thread (`t3_thread_launch`, `create_threads`, a | 3 | 3 | * `schedule_task` whose runs start new threads, a `t3_thread_transfer` fork) is refused unless its | 4 | | - * profile is within the starter's; `delegate_task` cuts a child to its parent as before. | | 4 | + * profile is within the starter's; `delegate_task` cuts a child to its parent as before. The | | 5 | + * scheduled task service checks a save and a run again (`scheduledTaskMcpProfile.ts`). | 5 | 6 | */ | 6 | 7 | import { | 7 | 8 | OrchestratorMcpFailure, | @@ -22,6 +23,7 @@ import { | 22 | 23 | makeThreadMcpProfiles, | 23 | 24 | } from "./claudeMcpProfile.ts"; | 24 | 25 | import { isEveryMcpTool, isWithinMcpProfile, type McpProfileResolved } from "./mcpProfile.ts"; | | 26 | +import { boundToWiderRefusal } from "./scheduledTaskMcpProfile.ts"; | 25 | 27 |
| 26 | 28 | type Refusal = { readonly reason: string }; | 27 | 29 |
| @@ -165,16 +167,19 @@ export const checkScheduleMcpProfile = Effect.fn("LaunchMcpProfile.schedule")(fu | 165 | 167 | }); | 166 | 168 |
| 167 | 169 | /** | 168 | | - * A task here whose runs, after a change (`bindToCurrentThread` as the call gives it), start a | 169 | | - * thread wider than the caller's profile is refused, whoever made it; `done` ends the refusal. | | 170 | + * A task here whose runs, after a change (`bindToCurrentThread` and `prompt` as the call gives | | 171 | + * them), start a thread wider than the caller's profile is refused, whoever made it, and so is a | | 172 | + * change to the prompt or thread of a task bound to another, wider thread; `done` ends the refusal. | 170 | 173 | */ | 171 | 174 | const checkTaskRuns = Effect.fn("LaunchMcpProfile.taskRuns")(function* ( | 172 | 175 | scheduledTaskId: ScheduledTaskId, | 173 | 176 | bindToCurrentThread: boolean | undefined, | 174 | 177 | done: string, | | 178 | + prompt?: string | undefined, | 175 | 179 | ) { | 176 | | - const { caller } = yield* loadCaller(); | 177 | | - if (caller === undefined || bindToCurrentThread === true) return; | | 180 | + const { caller, threads } = yield* loadCaller(); | | 181 | + if (caller === undefined) return; | | 182 | + const changesBound = prompt !== undefined || bindToCurrentThread !== undefined; | 178 | 183 | // Upstream's orchestrator toolkit does not name this service; the server provides it to every | 179 | 184 | // toolkit, and without it the task cannot be read, so the call is refused. | 180 | 185 | const service = yield* Effect.serviceOption(ScheduledTasks.ScheduledTaskService); | @@ -195,6 +200,33 @@ const checkTaskRuns = Effect.fn("LaunchMcpProfile.taskRuns")(function* ( | 195 | 200 | ); | 196 | 201 | const task = tasks.find((candidate) => candidate.id === scheduledTaskId); | 197 | 202 | if (task === undefined) return; | | 203 | + // Bound to another thread: the service's rule (scheduledTaskMcpProfile.ts), here too so a refused | | 204 | + // change does not use up the webhook secretRef it carries (the service consumes it first). | | 205 | + if (task.threadId !== null && changesBound) { | | 206 | + const profileFor = yield* Effect.mapError(yield* makeThreadMcpProfileReads, launcherUnknown); | | 207 | + const own = yield* Effect.mapError(profileFor(caller), launcherUnknown); | | 208 | + const refusal = yield* boundToWiderRefusal({ | | 209 | + profileFor, | | 210 | + threads, | | 211 | + own, | | 212 | + callerId: caller.id, | | 213 | + existing: task, | | 214 | + next: { | | 215 | + prompt: prompt ?? task.prompt, | | 216 | + projectId: task.projectId, | | 217 | + threadId: | | 218 | + bindToCurrentThread === true | | 219 | + ? caller.id | | 220 | + : bindToCurrentThread === false | | 221 | + ? null | | 222 | + : task.threadId, | | 223 | + }, | | 224 | + }); | | 225 | + if (refusal !== null) { | | 226 | + return yield* new OrchestratorMcpFailure({ code: "capability_denied", message: refusal }); | | 227 | + } | | 228 | + } | | 229 | + if (bindToCurrentThread === true) return; | 198 | 230 | // The service keeps the stored thread unless the call binds or unbinds it. | 199 | 231 | const threadId = bindToCurrentThread === false ? null : task.threadId; | 200 | 232 | if (threadId !== null) return; | @@ -207,13 +239,15 @@ const checkTaskRuns = Effect.fn("LaunchMcpProfile.taskRuns")(function* ( | 207 | 239 | }); | 208 | 240 |
| 209 | 241 | /** | 210 | | - * `schedule_task` changing a task here (unbind, rewrite, re-enable, pause): refused by the rule | 211 | | - * above. Deleting it or binding it to the caller is the way out; a missing task is the service's. | | 242 | + * `schedule_task` changing a scheduled task here (unbind, rewrite, re-enable, pause): refused by | | 243 | + * the rules above. Deleting it or binding it to the caller is the way out; a missing task is the | | 244 | + * service's. | 212 | 245 | */ | 213 | | -export const checkScheduleEditMcpProfile = ( | | 246 | +export const checkEditMcpProfile = ( | 214 | 247 | scheduledTaskId: ScheduledTaskId, | 215 | 248 | bindToCurrentThread: boolean | undefined, | 216 | | -) => checkTaskRuns(scheduledTaskId, bindToCurrentThread, "the task was not changed"); | | 249 | + prompt: string | undefined, | | 250 | +) => checkTaskRuns(scheduledTaskId, bindToCurrentThread, "the task was not changed", prompt); | 217 | 251 |
| 218 | 252 | /** `run_scheduled_task_now` here: the run starts the task's thread now, so the same rule holds. */ | 219 | 253 | export const checkScheduleRunMcpProfile = (scheduledTaskId: ScheduledTaskId) => | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/fork/mcpProfile.ts +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -285,16 +285,17 @@ export const carryMcpProfile = (resolved: McpProfileResolved): McpProfileCarried | 285 | 285 | t3Tools: resolved.profile.t3Tools, | 286 | 286 | }); | 287 | 287 |
| 288 | | -type CarriedLookup = | | 288 | +export type CarriedLookup = | 289 | 289 | | { readonly ok: true; readonly name: string; readonly profile: McpProfileSet } | 290 | 290 | | { readonly ok: false; readonly name: string; readonly issue: string }; | 291 | 291 |
| 292 | 292 | /** | 293 | 293 | * A carried profile, checked as strictly as a settings entry: a field this build does not know | 294 | 294 | * could narrow the profile, so it refuses rather than being dropped. T3 tool groups it does not | 295 | | - * know keep nothing, which only narrows. | | 295 | + * know keep nothing, which only narrows. Also reads a profile a scheduled task was saved with | | 296 | + * (`scheduledTaskMcpProfile.ts`). | 296 | 297 | */ | 297 | | -function readCarried(value: unknown): CarriedLookup { | | 298 | +export function readCarried(value: unknown): CarriedLookup { | 298 | 299 | const name = isRecord(value) && typeof value.name === "string" ? value.name : ""; | 299 | 300 | if (!isRecord(value)) return { ok: false, name, issue: "it is not an object" }; | 300 | 301 | const extra = Object.keys(value).filter((field) => !CARRIED_FIELDS.has(field)); | |
|