From 894dd21a0688bb662a5df64d249b34ff15f17426 Mon Sep 17 00:00:00 2001 From: finn-atlas Date: Mon, 9 Nov 2026 11:58:44 +0700 Subject: [PATCH] fix(cron): let managers read run receipts Allow admitted channel owners and Control UI administrators to inspect targeted automation run history while preserving ordinary session visibility and rejecting all-history reads. Refs AI-932. --- src/agents/tools/cron-tool.management.test.ts | 13 +++++++++++++ src/agents/tools/cron-tool.ts | 13 +++++-------- src/gateway/cron-creator-authority-grant.ts | 1 + src/gateway/server-methods/cron-runs.ts | 9 +++++++-- src/gateway/server-methods/cron.validation.test.ts | 1 + 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/agents/tools/cron-tool.management.test.ts b/src/agents/tools/cron-tool.management.test.ts index ba71dd8a86c04..c5f3570221b79 100644 --- a/src/agents/tools/cron-tool.management.test.ts +++ b/src/agents/tools/cron-tool.management.test.ts @@ -139,6 +139,19 @@ describe("admin automation management", () => { ); }); + it("reads targeted run history with admitted management authority", async () => { + await withAdminTool("channel-owner", async (tool, calls) => { + await expect( + tool.execute("run-history", { + action: "runs", + jobId, + runId: "run-1", + }), + ).resolves.toMatchObject({ details: { id: jobId } }); + expect(calls).toEqual([["cron.runs", { id: jobId, runId: "run-1" }]]); + }); + }); + it("visibly refuses unsupported next_check", async () => { await withAdminTool("unknown", async (tool, calls) => { await expect(tool.execute("unsupported", { action: "next_check", in: "1m" })).rejects.toThrow( diff --git a/src/agents/tools/cron-tool.ts b/src/agents/tools/cron-tool.ts index 330709d444123..bf144141c170e 100644 --- a/src/agents/tools/cron-tool.ts +++ b/src/agents/tools/cron-tool.ts @@ -205,7 +205,7 @@ function buildCronToolDescription(params: { triggersEnabled: boolean }): string ACTIONS: status | list (summaries; follow nextOffset) | get jobId (full details) | add job | update jobId job (partial; null clears) | remove jobId (operator removal requests active-run cancellation) | run jobId (runMode:"force"=now; waits up to timeoutMs, default 60s; unfinished runs return runId) | runs jobId runId? (history) | next_check in:"30m" (own paced run) | wake text (caller-owned session; mode defaults to next-heartbeat). Check unfinished runs later with runs, never a scheduled verification job. wake has no delay; for a later resume use an at job below. -SCOPE: Authenticated configured channel owners and Control UI administrators can manage any Gateway automation. Other turns see caller-visible jobs and scoped counts only: an empty list or failed list/get/update/remove (including not-found) does not establish global absence, even for IDs from your history. Never recreate or replace a known automation for update/remove/reconciliation based on these results; report the visibility limit and ask an authorized administrator to check through a fresh authenticated owner/administrator turn or the Automations page. New requested automations may still be created. +SCOPE: Authenticated configured channel owners and Control UI administrators can manage any Gateway automation. Other turns see caller-visible jobs and scoped counts only: an empty list or failed list/get/runs/update/remove (including not-found) does not establish global absence, even for IDs from your history. Never recreate or replace a known automation for update/remove/reconciliation based on these results; report the visibility limit and ask an authorized administrator to check through a fresh authenticated owner/administrator turn or the Automations page. New requested automations may still be created. ADD: job requires schedule+payload. @@ -251,7 +251,7 @@ export function createCronTool(opts?: CronToolOptions, deps?: CronToolDeps): Any ? "Inspect or remove only the current automation. Actions: list [includeDisabled], get jobId, remove jobId. Use the current job ID; other jobs and management actions are unavailable." : 'Inspect or remove only the current automation. Actions: status; list [includeDisabled]; get/runs/remove jobId; next_check in:"15m" for this paced run. Use the current job ID. To stop a finished job, remove it; creating/updating/running jobs and waking sessions are unavailable. Return the task result; the scheduler owns delivery.' : managementAuthority?.managementOnly - ? 'Manage any existing automation on this Gateway with the admitted automation management authority. Actions: list [includeDisabled,limit,offset] (compact summaries with timing; follow nextOffset); get jobId (full schedule, payload, and delivery details); update jobId job (partial patch, null clears); run jobId (runMode:"force" runs now; waits up to timeoutMs, default 60s, and returns the finished run, or its runId if still running); remove jobId (operator removal requests cancellation of an active run; the result reports activeRunCancellationRequested:true). Creator attribution and scheduled execution policy stay intact. Use the Automations page for other actions.' + ? 'Manage any existing automation on this Gateway with the admitted automation management authority. Actions: list [includeDisabled,limit,offset] (compact summaries with timing; follow nextOffset); get jobId (full schedule, payload, and delivery details); runs jobId runId? (targeted history); update jobId job (partial patch, null clears); run jobId (runMode:"force" runs now; waits up to timeoutMs, default 60s, and returns the finished run, or its runId if still running); remove jobId (operator removal requests cancellation of an active run; the result reports activeRunCancellationRequested:true). Creator attribution and scheduled execution policy stay intact. Use the Automations page for other actions.' : buildCronToolDescription({ triggersEnabled }), outputSchema: CronToolOutputSchema, parameters: createCronToolSchema({ @@ -300,7 +300,7 @@ export function createCronTool(opts?: CronToolOptions, deps?: CronToolDeps): Any !CRON_MANAGEMENT_METHODS.some((method) => method === `cron.${action}`) ) { throw new Error( - "This turn can only list, get, update, run, or remove automations. Use the Automations page for other actions.", + "This turn can only list, get, read runs, update, run, or remove automations. Use the Automations page for other actions.", ); } assertCronSelfRemoveScope(opts, action, params); @@ -655,14 +655,11 @@ export function createCronTool(opts?: CronToolOptions, deps?: CronToolDeps): Any if (result.enqueued !== true || result.run) { return jsonResult(result); } - const followUp = managementAuthority?.managementOnly - ? "check it later on the Automations page" - : "check it later with runs jobId runId"; return jsonResult({ ...result, note: result.finished - ? `Finished, but its run history is not visible to this turn (a one-shot may have deleted itself after succeeding); ${followUp} if needed.` - : `Not finished yet: it is still running, or it runs in this session and starts after this turn. Its delivery follows the job settings; ${followUp}. Do not schedule a verification job.`, + ? "Finished, but its run history is not visible to this turn (a one-shot may have deleted itself after succeeding); check it later with runs jobId runId if needed." + : "Not finished yet: it is still running, or it runs in this session and starts after this turn. Its delivery follows the job settings; check it later with runs jobId runId. Do not schedule a verification job.", }); } case "next_check": { diff --git a/src/gateway/cron-creator-authority-grant.ts b/src/gateway/cron-creator-authority-grant.ts index 884d23c5de977..94e50e07ca3c4 100644 --- a/src/gateway/cron-creator-authority-grant.ts +++ b/src/gateway/cron-creator-authority-grant.ts @@ -18,6 +18,7 @@ import type { export const CRON_MANAGEMENT_METHODS = [ "cron.list", "cron.get", + "cron.runs", "cron.update", "cron.run", "cron.remove", diff --git a/src/gateway/server-methods/cron-runs.ts b/src/gateway/server-methods/cron-runs.ts index e7f3def1901e6..28c08f07c1a28 100644 --- a/src/gateway/server-methods/cron-runs.ts +++ b/src/gateway/server-methods/cron-runs.ts @@ -53,11 +53,16 @@ export const cronRunsHandler: GatewayRequestHandler = async (options) => { ? await context.cron.list({ includeDisabled: true }) : [await context.cron.readJob(jobId!)].filter((job) => job !== undefined); const storeKey = cronStoreKey(context.cronStorePath); - await visibilityRead.prepare(); + const initialCallerScope = readCronCallerScope(client); + if (initialCallerScope?.manageAll) { + initialCallerScope.manageAll(); + } else { + await visibilityRead.prepare(); + } const resolvePageOptions = (): ReadCronRunHistoryPageOptions | undefined => { - const visibility = visibilityRead.resolve(); const defaultAgentId = context.cron.getDefaultAgentId(); const callerScope = readCronCallerScope(client); + const visibility = callerScope?.manageAll ? undefined : visibilityRead.resolve(); if (scope === "all") { if (callerScope) { throw new Error("Cron history scope changed; retry the request"); diff --git a/src/gateway/server-methods/cron.validation.test.ts b/src/gateway/server-methods/cron.validation.test.ts index 6ab23ef55b407..3144a1719b5cd 100644 --- a/src/gateway/server-methods/cron.validation.test.ts +++ b/src/gateway/server-methods/cron.validation.test.ts @@ -313,6 +313,7 @@ describe("cron method validation", () => { it.each([ ["cron.list", false, "channel-owner", true], ["cron.get", false, "channel-owner", true], + ["cron.runs", false, "channel-owner", true], ["cron.update", false, "channel-owner", false], ["cron.run", false, "channel-owner", true], ["cron.remove", true, "control-ui-admin", true],