From a41a2b53c96be75c07af135e5ec076c66039ca2e Mon Sep 17 00:00:00 2001 From: oliver Date: Fri, 11 Sep 2026 20:54:40 +0800 Subject: [PATCH] Make cron_retrigger fire-and-forget so callers do not wait for the run. Detached settle still finishes history and delivery in the background. Co-authored-by: Cursor --- lib/host.js | 62 +++++++++++++++++++++++++++++++++++++++++++---- lib/tools.js | 11 ++++++--- test/host.test.js | 38 +++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 9 deletions(-) diff --git a/lib/host.js b/lib/host.js index 1d0e631..8cc7f7e 100644 --- a/lib/host.js +++ b/lib/host.js @@ -716,6 +716,7 @@ export function createHostService(options = {}) { * - recurring (cron): after_minutes must be 0/omitted → enable + immediate run-now (keeps cron next) * - one-shot (at): consumed only; 0 → run-now; >0 → reschedule next * Auto-enables paused jobs. Rejects when a run is already pending/running. + * Immediate run-now is fire-and-forget: returns once the session starts; settle/delivery continue in background. */ async function retriggerJob(jobId, opts = {}, identity = null) { const afterRaw = opts.afterMinutes ?? opts.after_minutes @@ -811,10 +812,12 @@ export function createHostService(options = {}) { } } - const result = await dispatchRun(jobId, 'run-now') + const result = await dispatchRun(jobId, 'run-now', { wait: false }) + const detached = result.detached === true + || (result.run && ACTIVE_RUN_STATUSES.has(result.run.status)) return { ok: true, - mode, + mode: detached ? 'started' : 'immediate', job: result.job, run: result.run, decision: result.decision, @@ -945,6 +948,7 @@ export function createHostService(options = {}) { async function dispatchRun(jobId, trigger, opts = {}) { let claimed const t = Number.isFinite(opts.at) ? opts.at : now() + const waitForCompletion = opts.wait !== false await withState((current) => { claimed = claimOccurrence(current, jobId, t, trigger, current.settings) if (claimed.decision.action === 'wait') return current @@ -964,6 +968,19 @@ export function createHostService(options = {}) { }) let run = (executed.runs || []).find((row) => row.id === claimed.run.id) if (run?.status === 'running' && typeof sessionPort?.waitForTurn === 'function') { + if (!waitForCompletion) { + const runId = run.id + const sessionId = run.sessionId + const decision = claimed.decision + void trackDetachedSettle(jobId, runId, sessionId, decision) + const state = await snapshot() + return { + job: jobView(getJob(state, jobId), state.runs), + run: runView(run), + decision, + detached: true, + } + } let terminal try { terminal = await sessionPort.waitForTurn(run.sessionId) @@ -978,6 +995,38 @@ export function createHostService(options = {}) { return settleAndNotify(jobId, claimed.run.id, claimed.decision) } + async function settleDetachedRun(jobId, runId, sessionId, claimedDecision) { + let terminal + try { + terminal = await sessionPort.waitForTurn(sessionId) + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + terminal = { status: 'failed', error: message, summary: message } + } + try { + await store.mutate((current) => settleRun(current, runId, terminal, now())) + await settleAndNotify(jobId, runId, claimedDecision) + } catch (error) { + logger.warn?.(`[dsh-ops-cron] detached settle failed for ${runId}: ${error instanceof Error ? error.message : error}`) + } + } + + const detachedSettles = new Set() + + function trackDetachedSettle(jobId, runId, sessionId, claimedDecision) { + const task = settleDetachedRun(jobId, runId, sessionId, claimedDecision) + .catch(() => {}) + .finally(() => { detachedSettles.delete(task) }) + detachedSettles.add(task) + return task + } + + async function flushDetachedRuns() { + while (detachedSettles.size) { + await Promise.allSettled([...detachedSettles]) + } + } + async function tick() { if (ticking) return [] ticking = true @@ -1023,9 +1072,11 @@ export function createHostService(options = {}) { } function stopTimer() { - if (!timer) return - clearInterval(timer) - timer = null + if (timer) { + clearInterval(timer) + timer = null + } + return flushDetachedRuns() } async function concealKnownSessions() { @@ -1613,6 +1664,7 @@ export function createHostService(options = {}) { recover, startTimer, stopTimer, + flushDetachedRuns, handleRequest, snapshot, getUdsAuth, diff --git a/lib/tools.js b/lib/tools.js index b0aa3fa..7a70b32 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -746,14 +746,14 @@ export function cronToolDefinitions(service, deps = {}) { }, { name: 'cron_retrigger', - description: 'Start an extra run now: for recurring (cron) jobs fires immediately without changing the schedule; for consumed one-shots (next=n/a) re-arms/fires a new run. Auto-enables if paused. after_minutes>0 only for one-shots. Rejects if already pending/running OR if a one-shot still has a pending next (use cron_reschedule). Listeners: when retriggerable=true and progress done_flag is false, call this instead of creating a duplicate job.', + description: 'Start an extra run now (fire-and-forget): returns as soon as the run session starts — does NOT wait for the job to finish. Poll cron_runs / cron_progress for status. Recurring (cron): one extra run without changing the schedule. Consumed one-shots (next=n/a): re-arms/fires. Auto-enables if paused. after_minutes>0 only for one-shots (schedules next, no run yet). Rejects if already pending/running OR if a one-shot still has a pending next (use cron_reschedule). Listeners: when retriggerable=true and progress done_flag is false, call this instead of creating a duplicate job.', parameters: { type: 'object', additionalProperties: false, properties: { task_id: { type: 'string', description: 'Job id (alias: id).' }, id: { type: 'string', description: 'Alias of task_id.' }, - after_minutes: { type: 'number', description: 'One-shot only: delay before fire. Omit/0 = immediate. Not allowed for recurring jobs.' }, + after_minutes: { type: 'number', description: 'One-shot only: delay before fire. Omit/0 = start now (returns without waiting). Not allowed for recurring jobs.' }, }, }, output: { @@ -774,6 +774,9 @@ export function cronToolDefinitions(service, deps = {}) { const when = value.nextRunAt ? new Date(value.nextRunAt).toISOString() : 'pending' return text(`Retrigger scheduled "${name}" → next ${when}.`) } + if (value.mode === 'started') { + return text(`Started "${name}"${value.run?.id ? ` (run ${value.run.id})` : ''} — running in background.`) + } return text(`Retriggered "${name}"${value.run?.id ? ` (run ${value.run.id})` : ''}.`) }, }, @@ -903,7 +906,7 @@ export function cronGuidanceText(nowMs = Date.now(), timeZone = 'Asia/Shanghai') 'Session mirror: by default the run summary is NOT injected into the origin WhatsApp/Web chat. Pass mirror_to_session=true only when the user wants follow-up context in that chat. Full tool traces always stay in run history; IM delivery (when configured) is independent.', 'Agent preset: omit agent_preset to inherit (WhatsApp chat/group preset → creating session → Host default). Pass agent_preset to pin a preset for every scheduled run.', 'Labels/monitor: pass labels={"role":"worker","task":"theory"} on workers; listeners pass watch={"taskId":"..."} or watch={"labels":{...},"match":"all"}. Use cron_query / cron_progress / cron_runs to poll state and progress.', - 'One-shot wake: cron_resume only unpauses. For a finished one-shot (retriggerable=true, next=n/a), call cron_retrigger. For a still-pending one-shot (has next), call cron_reschedule(after_minutes=N) instead of delete+create. For a recurring job, cron_retrigger starts one extra run immediately without changing the cron schedule.', + 'One-shot wake: cron_resume only unpauses. For a finished one-shot (retriggerable=true, next=n/a), call cron_retrigger (fire-and-forget — do not wait for it to finish; poll cron_runs). For a still-pending one-shot (has next), call cron_reschedule(after_minutes=N) instead of delete+create. For a recurring job, cron_retrigger starts one extra run immediately without changing the cron schedule.', 'When the user asks to look at, create, pause, resume, retrigger, reschedule, or delete 定时任务 / scheduled tasks / cron jobs:', '1. If cron_list / cron_create / cron_pause / cron_resume / cron_retrigger / cron_reschedule / cron_delete / cron_query / cron_runs / cron_progress are in your tool list, call them.', '2. If they are not listed, load skill "scheduled-tasks" for usage guidance, then look again. skill_load does NOT inject tools — cron_* are registered by the dsh-ops-cron Host plugin at boot.', @@ -929,7 +932,7 @@ Tools: - cron_runs — run history for a task_id - cron_progress — read progress.channel.file snapshots - cron_create — "一分钟后" → after_minutes=1. Clock time → hour+minute only. Do not send at/expr at the same time. Pass cwd as the current workspace path when creating from a workspace chat. Pass provider+model or inherit the current session model. Delivery and agent preset auto from session, or pass delivery=im / agent_preset explicitly. Session mirror is off by default; pass mirror_to_session=true only if the user wants the summary injected into the origin chat. Optional labels/watch/progress/report/persist_history for monitor workers and listeners. -- cron_pause / cron_resume / cron_retrigger / cron_reschedule / cron_delete — by id from cron_list. Use cron_retrigger to re-fire a consumed one-shot or to immediately start one extra run of a recurring job. Use cron_reschedule to move a still-pending one-shot (do not delete+create). +- cron_pause / cron_resume / cron_retrigger / cron_reschedule / cron_delete — by id from cron_list. Use cron_retrigger to re-fire a consumed one-shot or to immediately start one extra run of a recurring job (returns when started; does not wait for completion). Use cron_reschedule to move a still-pending one-shot (do not delete+create). `, } } diff --git a/test/host.test.js b/test/host.test.js index df1879f..023befa 100644 --- a/test/host.test.js +++ b/test/host.test.js @@ -409,6 +409,44 @@ test('retriggerJob fires recurring immediately and re-arms consumed oneshot', as assert.ok(httpHit.body.run?.id) }) +test('retriggerJob fire-and-forget returns while live run is still running', async (t) => { + const dir = await mkdtemp(join(tmpdir(), 'dsh-ops-cron-')) + t.after(() => rm(dir, { recursive: true, force: true })) + const archived = [] + const sessionPort = makeLiveSessionPort(fakeLiveCtx(archived)) + const service = createTestHost({ + filePath: join(dir, 'store.json'), + now: () => Date.parse('2026-08-24T01:00:00.000Z'), + sessionPort, + }) + + const job = await service.createJob({ + name: 'detach-me', + prompt: 'slow work', + enabled: false, + schedule: { kind: 'at', at: new Date(Date.parse('2026-08-24T01:00:00.000Z') + 60_000).toISOString(), timezone: 'UTC' }, + }) + await service.store.mutate((state) => ({ + ...state, + jobs: state.jobs.map((row) => (row.id === job.id + ? { ...row, nextRunAt: null, lastStatus: 'succeeded', enabled: false } + : row)), + })) + + const started = await service.retriggerJob(job.id) + assert.equal(started.ok, true) + assert.equal(started.mode, 'started') + assert.equal(started.run.status, 'running') + assert.ok(started.run.sessionId) + + await service.flushDetachedRuns() + const history = await service.listHistory(job.id) + const terminal = history.find((row) => row.id === started.run.id) + assert.ok(terminal) + assert.equal(terminal.status, 'succeeded') + assert.equal(terminal.summary, '测试成功') +}) + test('rescheduleJob moves pending one-shot next without dispatch', async (t) => { const dir = await mkdtemp(join(tmpdir(), 'dsh-ops-cron-')) t.after(() => rm(dir, { recursive: true, force: true }))