Skip to content

Commit e704c3b

Browse files
fix(server): pin authorized modes on MCP scheduled-task updates
pingdotgg#15219 authorizes an MCP edit against the task's modes before writing it. With the atomic partial update, a mode raise committed in between kept the higher mode while applying the restricted caller's patch, where main's full-row upsert wrote the checked modes back. The update now takes expectedRuntimeMode/expectedInteractionMode preconditions, checked inside the write transaction, and the MCP tool passes the modes it authorized. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent e5ef367 commit e704c3b

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,11 @@ const make = Effect.gen(function* () {
14131413
.update({
14141414
id: existing.id,
14151415
projectId: existing.projectId,
1416+
// loadScheduledTask authorized these modes; a raise committed
1417+
// since must fail the edit rather than run the caller's patch
1418+
// above its limits.
1419+
expectedRuntimeMode: existing.runtimeMode,
1420+
expectedInteractionMode: existing.interactionMode,
14161421
...(input.title === undefined ? {} : { title: input.title }),
14171422
...(input.prompt === undefined ? {} : { prompt: input.prompt }),
14181423
...(input.enabled === undefined ? {} : { enabled: input.enabled }),

‎apps/server/src/scheduledTasks/ScheduledTaskService.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,43 @@ it.effect("update keeps disjoint concurrent edits and untouched fields", () =>
477477
}).pipe(Effect.provide(updateTestLayer)),
478478
);
479479

480+
it.effect("update rejects a patch whose authorized modes were raised since", () =>
481+
Effect.gen(function* () {
482+
const tasks = yield* ScheduledTaskService.ScheduledTaskService;
483+
yield* seedTask;
484+
yield* tasks.update({ id: updateTaskId, projectId: updateProjectId, runtimeMode: "auto" });
485+
// A caller authorized against "auto" loses the race to a raise.
486+
yield* tasks.update({
487+
id: updateTaskId,
488+
projectId: updateProjectId,
489+
runtimeMode: "full-access",
490+
});
491+
const stale = yield* tasks
492+
.update({
493+
id: updateTaskId,
494+
projectId: updateProjectId,
495+
prompt: "Run something else",
496+
expectedRuntimeMode: "auto",
497+
expectedInteractionMode: "default",
498+
})
499+
.pipe(Effect.result);
500+
if (Result.isSuccess(stale)) assert.fail("expected a typed conflict");
501+
assert.equal(stale.failure._tag, "ScheduledTaskError");
502+
const after = yield* findSeeded;
503+
assert.equal(after?.prompt, "prompt original");
504+
assert.equal(after?.runtimeMode, "full-access");
505+
// Matching pins still apply.
506+
const current = yield* tasks.update({
507+
id: updateTaskId,
508+
projectId: updateProjectId,
509+
prompt: "Run something else",
510+
expectedRuntimeMode: "full-access",
511+
expectedInteractionMode: "default",
512+
});
513+
assert.equal(Option.getOrThrow(current).task.prompt, "Run something else");
514+
}).pipe(Effect.provide(updateTestLayer)),
515+
);
516+
480517
it.effect("update patches runtimeMode without disturbing other fields", () =>
481518
Effect.gen(function* () {
482519
const tasks = yield* ScheduledTaskService.ScheduledTaskService;

‎apps/server/src/scheduledTasks/ScheduledTaskService.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,17 @@ export const layer = Layer.effect(
10391039
const row = rows[0];
10401040
if (row === undefined) return null;
10411041
const existing = yield* decodeRow(row);
1042+
if (
1043+
(input.expectedRuntimeMode !== undefined &&
1044+
existing.runtimeMode !== input.expectedRuntimeMode) ||
1045+
(input.expectedInteractionMode !== undefined &&
1046+
existing.interactionMode !== input.expectedInteractionMode)
1047+
) {
1048+
return yield* taskError(
1049+
"Scheduled task changed since it was loaded; retry the operation.",
1050+
{ taskId: input.id },
1051+
);
1052+
}
10421053
// Patches merge with whatever concurrent edits already
10431054
// committed, so the resulting pair must still be dispatchable:
10441055
// a move keeps an existing threadId only when that thread

‎packages/contracts/src/scheduledTask.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ export const ScheduledTaskUpdateInput = Schema.Struct({
178178
runtimeMode: Schema.optional(RuntimeMode),
179179
/** Moves the task to another project; `projectId` stays the lookup scope. */
180180
nextProjectId: Schema.optional(ProjectId),
181+
// Preconditions checked inside the write transaction: when provided, the
182+
// update is rejected if the stored modes no longer match, so a caller
183+
// authorized against these modes cannot patch a task raised since.
184+
expectedRuntimeMode: Schema.optional(RuntimeMode),
185+
expectedInteractionMode: Schema.optional(ProviderInteractionMode),
181186
});
182187
export type ScheduledTaskUpdateInput = typeof ScheduledTaskUpdateInput.Type;
183188

0 commit comments

Comments
 (0)