@soffits/dsh-cron-jobs 0.13.1 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -110,7 +110,6 @@ Config jobs live in the config of the `cron-jobs` row. Add an override to your p
110
110
  | `agent.reuseWithinHours` | Send the prompt into the job's previous woken session when that session was opened less than this many hours ago (see below). `0` always opens a new session. The row's `reuseWithinHours` when unset. |
111
111
  | `agent.model.provider`, `agent.model.model` | Model of the new session, with the same provider and model ids the model picker uses. Both must be set; otherwise the preset's or the Host's default model applies. |
112
112
  | `agent.model.reasoningEffort` | Reasoning effort id for that model. The model's default when empty. |
113
- | `agent.answerCheck` | Command that checks the agent's final answer before its turn closes (see [Answer checks](#answer-checks)). No check when empty. |
114
113
 
115
114
  Next to `jobs`, the row also takes:
116
115
 
@@ -174,14 +173,6 @@ A woken turn that a DSH restart cuts off before it finishes is run again once. A
174
173
 
175
174
  The rerun is skipped, with the reason in the run history and a `rerun-skipped` run-log line, when the cut-off run started more than 2 hours ago, a later run of the job has woken the agent since, the job's next run is less than 15 minutes away, or the job is paused, disabled or deleted. A session the job opened for itself that a person has since taken over is left alone: the rerun fails with a note to the delivery target. A rerun that is cut off too is not run again. The run log marks the cut-off with a `cut-off` line, and the rerun's `start` line carries `rerunOf`.
176
175
 
177
- ### Answer checks
178
-
179
- A job can name a command, `agent.answerCheck` (or `answerCheck` in the tools), that looks at the agent's final answer while the turn can still change it. When the turn that the job's prompt started is about to close, the command runs with the answer on stdin, in the session's working directory, with a 60-second limit. Empty stdout passes the answer. Anything it prints goes back into the same turn as a message asking for a rewrite, and the agent's next answer is the one delivered. Each run is steered at most once, so a check that never passes cannot keep a turn going. Empty answers, `[SILENT]` and answers with only `MEDIA:` lines are not checked. A command that fails or times out lets the answer through.
180
-
181
- Use it for checks that are cheap to script and costly to miss, such as the answer's language or a format the channel needs. The run log gets an `answer-check` line per check (`passed`, `steered` or `error`) and, after a rewrite, an `answer-recheck` line saying whether the delivered answer passed (`fixed` or `still-flagged`).
182
-
183
- It works on DSH's `agent/turn-stopping` hook: a listener that steers a message there makes the agent loop run one more step in the same turn.
184
-
185
176
  ### Who sent a prompt
186
177
 
187
178
  Every prompt a job puts into a session has the source kind `schedule`, which the Web GUI shows as a scheduled run, and names the job as `principal`, the field DSH uses for the caller behind a user message:
@@ -190,7 +181,7 @@ Every prompt a job puts into a session has the source kind `schedule`, which the
190
181
  { kind: 'cron-job', channel: 'cron', id, name, trigger, events }
191
182
  ```
192
183
 
193
- `id` is the job's id (its name for a config job), `name` its name, `trigger` what started the run (`schedule`, `manual`, `event`, `rerun`, or `answer-check` for an answer check's rewrite request), and `events` the channels of the events that started an event run. Scripts that read session logs can tell job prompts apart, and which job sent each, without parsing the text.
184
+ `id` is the job's id (its name for a config job), `name` its name, `trigger` what started the run (`schedule`, `manual`, `event`, or `rerun`), and `events` the channels of the events that started an event run. Scripts that read session logs can tell job prompts apart, and which job sent each, without parsing the text.
194
185
 
195
186
  ### Delivery channels
196
187
 
@@ -319,7 +310,6 @@ Where the prompt goes (`target`):
319
310
 
320
311
  `prompt_replace` and `command_replace` on `cron_job_edit` change a long prompt or command without resending it: a list of literal `{ "old": "...", "new": "...", "replace_all": false }` replacements, applied in order, each to the result of the one before. Every `old` must match exactly once unless that item sets `replace_all`; otherwise the call fails naming the item (`prompt_replace[1] (old "..."): no match` or `matches 3 times`) and changes nothing. They cannot be combined with `prompt` or `command`.
321
312
 
322
- `answerCheck` sets the job's [answer check](#answer-checks) command; on `cron_job_edit`, an empty string removes it.
323
313
 
324
314
  `model` is an object, `{ "provider": "anthropic", "model": "claude-sonnet-4-5", "reasoningEffort": "high" }`, with the same ids the model picker uses; `provider` and `model` are required together and `reasoningEffort` is optional. The sessions the job opens start on that model, like `agent.model` on a config job; left out, the preset's or host default applies. `cron_job_edit` with `model` sets it on an existing job, and an empty object (`{}`) clears it. `cron_job_list` and `cron_job_view` show the model when one is set. A job that posts into an existing session (`this_session`, `sessionId`) does not open a session, so its model has no effect.
325
315
 
package/index.js CHANGED
@@ -17,7 +17,6 @@ import { runCommand } from './src/command.js'
17
17
  import { archiveWhenIdle, deliverToSession, openAgentSession, waitForAnswer } from './src/session.js'
18
18
  import { sendMessage } from './src/deliver.js'
19
19
  import { CronJobs } from './src/controller.js'
20
- import { createAnswerChecks } from './src/answer-check.js'
21
20
  import { createRunLog } from './src/runlog.js'
22
21
  import { openHistory } from './src/history.js'
23
22
  import { createRoutes, createRpcHandler } from './src/rpc.js'
@@ -61,17 +60,13 @@ export function apply(ctx, config) {
61
60
  const controller = new AbortController()
62
61
  let jobs
63
62
  let history
64
- let checks
65
63
  let spool
66
- // A job's answerCheck runs as the turn its prompt started is about to close.
67
- const stopTurnHook = ctx.on?.('agent/turn-stopping', payload => checks?.onTurnStopping(payload))
68
64
  const ready = (async () => {
69
65
  history = await openHistory(ctx.storageDomain, {
70
66
  limit: config.historyRecords, logger, signal: controller.signal,
71
67
  })
72
68
  if (controller.signal.aborted) return
73
69
  const record = createRunLog(config.runLog || undefined, logger)
74
- checks = createAnswerChecks({ run: runCommand, logger, record })
75
70
  jobs = new CronJobs({
76
71
  jobs: config.jobs ?? [],
77
72
  logger,
@@ -83,7 +78,6 @@ export function apply(ctx, config) {
83
78
  // this plugin, reload, or be missing for a while (the delivery waits).
84
79
  send: (target, message, from, options) => sendMessage({ lookup: name => ctx.get(name), target, message, from, signal: controller.signal, ...options }),
85
80
  answer: (sessionId, messageId, options) => waitForAnswer(ctx, sessionId, messageId, options),
86
- checks,
87
81
  defaults: {
88
82
  agentCwd: config.defaultAgentCwd || undefined,
89
83
  autoArchive: config.autoArchive ?? true,
@@ -114,7 +108,6 @@ export function apply(ctx, config) {
114
108
  started = ready
115
109
  return async () => {
116
110
  current = undefined
117
- stopTurnHook?.()
118
111
  // Stop reading events first, so none reaches a stopping scheduler.
119
112
  await spool?.stop()
120
113
  // Stop the timers first so no new run starts with an aborted signal.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soffits/dsh-cron-jobs",
3
- "version": "0.13.1",
3
+ "version": "0.14.0",
4
4
  "description": "Scheduled jobs for DeepSeek Harness: shell commands and agent prompts on cron, RRULE or one-time schedules, a gate that wakes an agent only when there is something to do, result delivery through any plugin that can send messages, tools to manage jobs from a conversation, and a Cron tasks page in the Web GUI.",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/config.js CHANGED
@@ -24,7 +24,6 @@ const Agent = z.object({
24
24
  sessionId: z.string().description('Send the prompt into this existing session instead of opening a new one. Empty: open a new session.'),
25
25
  model: Model,
26
26
  autoArchive: z.boolean().description('Archive the woken session once its turn ends, unless it is pinned or a person has sent it a message (such a session is also never reused). The plugin\'s autoArchive when unset.'),
27
- answerCheck: z.string().description('Command that reads the agent\'s final answer on stdin before the turn closes. Empty output passes it; any output is sent back into the turn as a request to rewrite the answer (once per run). Empty: no check.'),
28
27
  reuseWithinHours: z.number().min(0).description('Send the prompt into this job\'s previous woken session instead of opening a new one, when that session was opened within this many hours. 0 always opens a new session. The plugin\'s reuseWithinHours when unset.'),
29
28
  }).description('Wake an agent: run the command as a gate and open a new session only when its output asks for it.')
30
29
 
package/src/controller.js CHANGED
@@ -85,7 +85,7 @@ export class CronJobs {
85
85
  * @param {object} [options.watcherOptions] - Passed to {@link WatcherSupervisor} (stateRoot, limits, spawn).
86
86
  * @param {{ maxAgeMs?: number, minLeadMs?: number }} [options.rerun] - Limits for rerunning cut-off runs (see continueCut).
87
87
  */
88
- constructor({ jobs, logger, run, wake, reuse, settle, send, answer, checks, defaults = {}, signal, record, history, CronClass = CronTimer, TimerClass = ScheduleTimer, now = () => new Date(), watchdogMs = WATCHDOG_MS, overdueMs = OVERDUE_MS, events = {}, timers, watchers = [], watcherOptions = {}, rerun = {} }) {
88
+ constructor({ jobs, logger, run, wake, reuse, settle, send, answer, defaults = {}, signal, record, history, CronClass = CronTimer, TimerClass = ScheduleTimer, now = () => new Date(), watchdogMs = WATCHDOG_MS, overdueMs = OVERDUE_MS, events = {}, timers, watchers = [], watcherOptions = {}, rerun = {} }) {
89
89
  this.logger = logger
90
90
  this.defaults = defaults
91
91
  this.history = history
@@ -96,7 +96,7 @@ export class CronJobs {
96
96
  this.watchdogMs = watchdogMs
97
97
  this.overdueMs = overdueMs
98
98
  this.record = record ?? (async () => {})
99
- this.deps = { run, wake, reuse, settle, send, answer, checks, defaults, logger, signal, record, history, now }
99
+ this.deps = { run, wake, reuse, settle, send, answer, defaults, logger, signal, record, history, now }
100
100
  this.eventSettings = {
101
101
  coalesceMs: (events.coalesceSeconds ?? DEFAULT_COALESCE_SECONDS) * 1000,
102
102
  maxRunsPerHour: events.maxRunsPerHour ?? DEFAULT_MAX_RUNS_PER_HOUR,
@@ -623,7 +623,6 @@ export class CronJobs {
623
623
  title: title(input.title),
624
624
  ...text('prompt', input.prompt),
625
625
  ...text('command', input.command),
626
- ...text('answerCheck', input.answerCheck),
627
626
  ...(spec ? { schedule: spec } : {}),
628
627
  ...(on.length > 0 ? { on } : {}),
629
628
  ...(input.once === true ? { once: true } : {}),
@@ -663,7 +662,7 @@ export class CronJobs {
663
662
  const current = entry.record
664
663
  const next = { ...current, updatedAt: now.toISOString() }
665
664
  if (patch.title !== undefined) next.title = title(patch.title)
666
- for (const field of ['prompt', 'command', 'answerCheck']) {
665
+ for (const field of ['prompt', 'command']) {
667
666
  if (patch[field] === undefined) continue
668
667
  delete next[field]
669
668
  Object.assign(next, text(field, patch[field]))
@@ -849,7 +848,7 @@ function fromStored(record) {
849
848
  ...(record.cwd ? { cwd: record.cwd } : {}),
850
849
  timeoutSeconds: record.timeoutSeconds ?? DEFAULT_STORED_TIMEOUT_SECONDS,
851
850
  timeZone: scheduleZone(record.schedule),
852
- ...(record.prompt ? { agent: { prompt: record.prompt, ...(record.preset ? { preset: record.preset } : {}), ...(record.cwd ? { cwd: record.cwd } : {}), ...(record.model ? { model: record.model } : {}), ...(record.answerCheck ? { answerCheck: record.answerCheck } : {}) } } : {}),
851
+ ...(record.prompt ? { agent: { prompt: record.prompt, ...(record.preset ? { preset: record.preset } : {}), ...(record.cwd ? { cwd: record.cwd } : {}), ...(record.model ? { model: record.model } : {}) } } : {}),
853
852
  target: record.target,
854
853
  ...(record.deliver !== undefined ? { deliver: record.deliver } : {}),
855
854
  spec: record.schedule,
@@ -940,7 +939,6 @@ function agentView(job, defaults) {
940
939
  // Where the cwd came from: the job, the plugin default, or the job's command cwd.
941
940
  cwdSource: job.agent.cwd ? 'job' : defaults.agentCwd ? 'default' : job.cwd ? 'command' : null,
942
941
  model: agentModel(job) ?? null,
943
- answerCheck: job.agent.answerCheck || null,
944
942
  // An explicit session is never archived or swapped for another one.
945
943
  autoArchive: target === 'session' ? false : settings.autoArchive,
946
944
  reuseWithinHours: target === 'new' ? settings.reuseWithinHours : 0,
package/src/job.js CHANGED
@@ -105,7 +105,6 @@ export class JobRunner {
105
105
  * @param {(sessionId: string, options: { signal?: AbortSignal, isLatest: () => boolean, lock: Function }) => Promise<{ archive: string, note?: string }>} [deps.settle] - Archives a session after its turn (see session.js archiveWhenIdle); without it nothing is archived.
106
106
  * @param {(target: { via: string, to: string }, message: { text: string, files: string[] }, from: { job: string, sessionId?: string }, options: { waitMs?: number, onWait?: Function }) => Promise<{ status: 'sent' | 'failed' | 'unavailable' | 'stopped', attempts: number, error?: string }>} [deps.send] - Hands a result to its delivery service (see deliver.js sendMessage); without it nothing is delivered.
107
107
  * @param {(sessionId: string, messageId: string, options: { signal?: AbortSignal }) => Promise<{ pending: true } | { text: string, reason?: string } | { error: string }>} [deps.answer] - Waits for the final answer to a prompt (see session.js waitForAnswer).
108
- * @param {ReturnType<typeof import('./answer-check.js').createAnswerChecks>} [deps.checks] - Answer checks (see answer-check.js); without it a job's answerCheck is ignored.
109
108
  * @param {{ agentCwd?: string, autoArchive?: boolean, reuseWithinHours?: number, deliver?: object }} [deps.defaults] - Plugin-level agent settings and the default delivery target.
110
109
  * @param {{ info: Function, warn: Function }} deps.logger
111
110
  * @param {AbortSignal} [deps.signal] - Stops a running command when aborted.
@@ -113,11 +112,10 @@ export class JobRunner {
113
112
  * @param {{ begin: Function, finish: Function }} [deps.history] - Saved run history (see history.js).
114
113
  * @param {() => Date} [deps.now]
115
114
  */
116
- constructor(job, { run, wake, reuse, keepSession, settle, send, answer, checks, defaults = {}, logger, signal, record = async () => {}, history = NO_HISTORY, now = () => new Date() }) {
115
+ constructor(job, { run, wake, reuse, keepSession, settle, send, answer, defaults = {}, logger, signal, record = async () => {}, history = NO_HISTORY, now = () => new Date() }) {
117
116
  this.job = job
118
117
  this.send = send
119
118
  this.answer = answer
120
- this.checks = checks
121
119
  this.reuse = reuse
122
120
  this.keepSession = keepSession
123
121
  this.settle = settle
@@ -324,8 +322,6 @@ export class JobRunner {
324
322
  const answer = await this.answer(sessionId, messageId, { signal: this.signal })
325
323
  if ('pending' in answer || this.signal?.aborted) return undefined
326
324
  const cut = 'text' in answer && cutOff(answer)
327
- if ('text' in answer && !cut) await this.checks?.recheck(messageId, answer.text, { sessionId, signal: this.signal })
328
- this.checks?.forget(messageId)
329
325
  const reason = 'error' in answer ? 'error' : answer.reason
330
326
  await this.history.update(jobKey(job), runId, { turn: { status: cut ? 'cut-off' : 'ended', messageId, ...(reason ? { reason } : {}) } })
331
327
  let delivery
@@ -446,7 +442,6 @@ export class JobRunner {
446
442
  if (delivery?.message !== undefined) {
447
443
  this.watchDelivery(run.sessionId, run.id, delivery)
448
444
  } else if (messageId && run.sessionId && this.answer !== undefined) {
449
- this.expectCheck(messageId)
450
445
  this.watchTurn(run.sessionId, run.id, messageId, delivery)
451
446
  }
452
447
  }
@@ -496,10 +491,7 @@ export class JobRunner {
496
491
  const target = job.target ?? { kind: 'new' }
497
492
  // Lets the answer to this prompt be found once its turn ends.
498
493
  const messageId = randomUUID()
499
- // Watched before the prompt is sent, so even a turn that ends at once is checked.
500
- this.expectCheck(messageId)
501
494
  const sent = await this.deliverPrompt({ job, target, text, messageId, settings, firedAt, localTime })
502
- if (sent.status !== 'woke') this.checks?.forget(messageId)
503
495
  return sent.status === 'woke' ? { ...sent, messageId } : sent
504
496
  }
505
497
 
@@ -523,7 +515,6 @@ export class JobRunner {
523
515
  delivery: this.deliveryTarget(),
524
516
  })
525
517
  const messageId = randomUUID()
526
- this.expectCheck(messageId)
527
518
  const archive = target.kind !== 'session' && settings.autoArchive && this.settle !== undefined ? { archive: 'pending' } : {}
528
519
  const fresh = target.kind === 'new'
529
520
  const sent = await this.sendToTarget(original.sessionId, text, {
@@ -533,7 +524,6 @@ export class JobRunner {
533
524
  messageId,
534
525
  fields: fresh ? { reused: true, sessionOpenedAt: original.sessionOpenedAt ?? original.startedAt } : { targeted: true },
535
526
  })
536
- if (sent.status !== 'woke') this.checks?.forget(messageId)
537
527
  return sent.status === 'woke' ? { ...sent, messageId } : sent
538
528
  }
539
529
 
@@ -554,14 +544,6 @@ export class JobRunner {
554
544
  }
555
545
  }
556
546
 
557
- /** Have the answer to this prompt checked before its turn closes, when the job names a check. */
558
- expectCheck(messageId) {
559
- const command = this.job.agent?.answerCheck
560
- if (!command || this.checks === undefined) return
561
- const cwd = agentSettings(this.job, this.defaults).cwd
562
- this.checks.expect(messageId, { job: this.job.name, jobId: jobKey(this.job), command, ...(cwd ? { cwd } : {}) })
563
- }
564
-
565
547
  /** Send the prompt to the target session, a reused one, or a new one. */
566
548
  async deliverPrompt({ job, target, text, messageId, settings, firedAt, localTime }) {
567
549
  const { logger } = this
package/src/store.js CHANGED
@@ -32,8 +32,6 @@ export const storedJobSchema = z.object({
32
32
  title: z.string().min(1),
33
33
  prompt: z.string().optional(),
34
34
  command: z.string().optional(),
35
- // Command that checks the agent's final answer before its turn closes (answer-check.js).
36
- answerCheck: z.string().optional(),
37
35
  // Omitted for a job that runs only on events.
38
36
  schedule: scheduleSchema.optional(),
39
37
  // Channel patterns whose events start a run (events.js).
package/tools.js CHANGED
@@ -43,7 +43,6 @@ export function toolSchemas(prefix) {
43
43
  description: 'Where the prompt goes; defaults to new. this_session: this session, for reminders and follow-ups to this conversation. new_once: one new session, opened by the first run and used by every later run. new: a new session for each run, archived once its turn ends. New sessions use cwd and this session\'s agent preset.',
44
44
  },
45
45
  sessionId: { type: 'string', description: 'Deliver to this other existing session instead of target.' },
46
- answerCheck: { type: 'string', description: 'Shell command run in the session\'s cwd on the agent\'s final answer (on stdin) before its turn closes. Empty stdout passes the answer; any stdout is sent back into the turn as a request to rewrite it, once per run. Skipped for [SILENT].' },
47
46
  cwd: { type: 'string', description: 'Working directory of the command and of new sessions; defaults to this session\'s.' },
48
47
  deliver: { type: 'string', description: 'Where each run\'s result is sent: "<service>:<address>", a DSH service that delivers messages and an address it understands (such as telegramTopics:notify), or "none". The result is the agent\'s final answer (exactly [SILENT] sends nothing; MEDIA:<path> lines become attachments), or for a job without a prompt the command\'s stdout and failures. Defaults to the plugin\'s default target, or none when the prompt goes to an existing session.' },
49
48
  on: {
@@ -107,7 +106,6 @@ export function toolSchemas(prefix) {
107
106
  command: { ...fields.command, description: 'New command; an empty string removes it.' },
108
107
  prompt_replace: replacements('prompt'),
109
108
  command_replace: replacements('command'),
110
- answerCheck: { ...fields.answerCheck, description: 'New answer check command; an empty string removes it.' },
111
109
  cron: { ...schedule.cron, description: `${schedule.cron.description} An empty cron, rrule or at removes the schedule of a job with on.` },
112
110
  rrule: schedule.rrule,
113
111
  at: schedule.at,
@@ -163,7 +161,6 @@ export function createTools({ prefix = 'cron_job', getJobs, ctx }) {
163
161
  title: args.title,
164
162
  prompt: args.prompt,
165
163
  command: args.command,
166
- answerCheck: args.answerCheck,
167
164
  schedule: { cron: args.cron, rrule: args.rrule, at: args.at, timeZone: args.timeZone },
168
165
  ...(args.on !== undefined ? { on: args.on } : {}),
169
166
  ...(args.once !== undefined ? { once: args.once } : {}),
@@ -180,7 +177,7 @@ export function createTools({ prefix = 'cron_job', getJobs, ctx }) {
180
177
  `Update, pause, resume or delete one job created with ${prefix}_create, by id. Jobs from the plugin config are read-only.`,
181
178
  async (args, jobs, agent) => {
182
179
  const { id, action } = args
183
- const changes = ['title', 'prompt', 'command', 'prompt_replace', 'command_replace', 'answerCheck', 'cron', 'rrule', 'at', 'timeZone', 'target', 'sessionId', 'cwd', 'deliver', 'model', 'on', 'once', 'coalesceSeconds'].filter(key => args[key] !== undefined)
180
+ const changes = ['title', 'prompt', 'command', 'prompt_replace', 'command_replace', 'cron', 'rrule', 'at', 'timeZone', 'target', 'sessionId', 'cwd', 'deliver', 'model', 'on', 'once', 'coalesceSeconds'].filter(key => args[key] !== undefined)
184
181
  if (action !== 'update' && changes.length > 0) return failure('invalid_arguments', `${action} takes only id and action.`)
185
182
  if (action === 'delete') return await jobs.remove(id)
186
183
  if (action === 'pause' || action === 'resume') {
@@ -206,7 +203,6 @@ export function createTools({ prefix = 'cron_job', getJobs, ctx }) {
206
203
  title: args.title,
207
204
  prompt: args.prompt,
208
205
  command: args.command,
209
- answerCheck: args.answerCheck,
210
206
  cwd: args.cwd,
211
207
  on: args.on,
212
208
  once: args.once,
@@ -364,7 +360,7 @@ export function modelJob(job, { brief = false } = {}) {
364
360
  ...(job.agent?.model ? { model: job.agent.model } : {}),
365
361
  deliver: job.deliver?.via ? (job.deliver.to ? `${job.deliver.via}:${job.deliver.to}` : job.deliver.via) : 'none',
366
362
  ...(brief ? {} : {
367
- ...(job.agent ? { prompt: job.agent.prompt, preset: job.agent.preset, sessionCwd: job.agent.cwd, ...(job.agent.answerCheck ? { answerCheck: job.agent.answerCheck } : {}) } : {}),
363
+ ...(job.agent ? { prompt: job.agent.prompt, preset: job.agent.preset, sessionCwd: job.agent.cwd } : {}),
368
364
  ...(job.command ? { command: job.command, cwd: job.cwd } : {}),
369
365
  ...(job.createdBy ? { createdBy: job.createdBy } : {}),
370
366
  }),
@@ -1,159 +0,0 @@
1
- /**
2
- * Answer checks: a job may name a command that reads its agent's final answer
3
- * on stdin before the turn closes. Empty stdout passes the answer; anything
4
- * else is steered back into the same turn as a rewrite request, so the agent
5
- * writes the answer again and the job delivers that one. Each prompt is
6
- * steered at most once, so a check that never passes cannot loop a turn.
7
- *
8
- * The hook is DSH's `agent/turn-stopping` (serial): when a turn is about to
9
- * close with an empty next-step inbox, a listener that calls `agent.steer()`
10
- * makes the loop run one more step in the same turn.
11
- */
12
- import { randomUUID } from 'node:crypto'
13
- import { messageFromText } from './deliver.js'
14
- import { userMessage } from './session.js'
15
-
16
- /** Limit for one run of a check command. */
17
- export const ANSWER_CHECK_TIMEOUT_MS = 60_000
18
-
19
- /**
20
- * How long a prompt stays watched. A run whose answer is not delivered (no
21
- * delivery target) is never forgotten by its delivery, so old entries are
22
- * dropped when new ones come.
23
- */
24
- export const ANSWER_CHECK_TTL_MS = 12 * 60 * 60 * 1000
25
-
26
- /**
27
- * The answer a turn has so far: the last text block of the last assistant
28
- * message after the prompt with `messageId`, when that prompt is in the turn
29
- * still open at the end of the log. Undefined when the prompt is not there or
30
- * a turn has ended since.
31
- * @param {Array<{ type: string, data?: any }>} log
32
- * @param {string} messageId
33
- * @returns {string | undefined}
34
- */
35
- export function openTurnAnswer(log, messageId) {
36
- let text
37
- for (let i = log.length - 1; i >= 0; i--) {
38
- const event = log[i]
39
- if (event.type === 'turn/end') return undefined
40
- if (event.type === 'user/message' && event.data?.id === messageId) return text ?? ''
41
- if (text === undefined && event.type === 'assistant/message') {
42
- const parts = event.data?.message?.content ?? []
43
- const texts = parts.filter(part => part?.type === 'text' && typeof part.text === 'string')
44
- .map(part => part.text.trim()).filter(Boolean)
45
- if (texts.length > 0) text = texts.at(-1)
46
- }
47
- }
48
- return undefined
49
- }
50
-
51
- /**
52
- * Whether an answer is worth checking: not empty, not [SILENT], and not only
53
- * MEDIA lines.
54
- * @param {string} text
55
- */
56
- export function checkable(text) {
57
- const message = messageFromText(text)
58
- return message !== undefined && message.text.trim() !== ''
59
- }
60
-
61
- /**
62
- * Pending checks, keyed by the prompt's message id.
63
- * @param {object} deps
64
- * @param {(options: object) => Promise<import('./command.js').CommandResult>} deps.run - Runs a command (command.js runCommand).
65
- * @param {{ info: Function, warn: Function }} deps.logger
66
- * @param {(entry: object) => Promise<void>} [deps.record] - Appends a run-log line.
67
- * @param {() => Date} [deps.now]
68
- */
69
- export function createAnswerChecks({ run, logger, record = async () => {}, now = () => new Date() }) {
70
- /** @type {Map<string, { job: string, jobId?: string, command: string, cwd?: string, at: number, steered: boolean, verdict?: string }>} */
71
- const pending = new Map()
72
-
73
- async function check(entry, text, signal) {
74
- const result = await run({ command: entry.command, cwd: entry.cwd, input: text, timeoutMs: ANSWER_CHECK_TIMEOUT_MS, signal })
75
- if (!result.ok) return { error: result.error ?? (result.timedOut ? 'timed out' : `exit code ${result.exitCode}`) }
76
- return { request: result.stdout.trim() }
77
- }
78
-
79
- return {
80
- /**
81
- * Check the answer to this prompt before its turn closes.
82
- * @param {string} messageId
83
- * @param {{ job: string, jobId?: string, command: string, cwd?: string }} options
84
- */
85
- expect(messageId, { job, jobId, command, cwd }) {
86
- const at = now().getTime()
87
- for (const [id, entry] of pending) if (at - entry.at > ANSWER_CHECK_TTL_MS) pending.delete(id)
88
- pending.set(messageId, { job, ...(jobId ? { jobId } : {}), command, ...(cwd ? { cwd } : {}), at, steered: false })
89
- },
90
-
91
- /** Stop watching a prompt; returns what its check did ('steered', 'passed', 'error' or undefined). */
92
- forget(messageId) {
93
- const entry = pending.get(messageId)
94
- pending.delete(messageId)
95
- return entry?.verdict
96
- },
97
-
98
- /** Number of prompts watched, for tests. */
99
- get size() {
100
- return pending.size
101
- },
102
-
103
- /**
104
- * The `agent/turn-stopping` listener. Finds a watched prompt in the
105
- * closing turn, runs its check on the answer and steers a rewrite
106
- * request when the check prints one.
107
- * @param {{ agent: { session?: { id?: string, log?: any[] }, steer: (message: object) => void }, signal?: AbortSignal }} payload
108
- */
109
- async onTurnStopping({ agent, signal }) {
110
- if (pending.size === 0) return
111
- const log = agent?.session?.log ?? []
112
- for (const [messageId, entry] of pending) {
113
- if (entry.steered) continue
114
- const text = openTurnAnswer(log, messageId)
115
- if (text === undefined) continue
116
- if (!checkable(text)) return
117
- const outcome = await check(entry, text, signal).catch(error => ({ error: error instanceof Error ? error.message : String(error) }))
118
- if (signal?.aborted) return
119
- const sessionId = agent.session?.id
120
- const line = { time: now().toISOString(), job: entry.job, event: 'answer-check', ...(sessionId ? { sessionId } : {}) }
121
- if ('error' in outcome) {
122
- entry.verdict = 'error'
123
- logger.warn('[%s] answer check failed: %s', entry.job, outcome.error)
124
- await record({ ...line, status: 'error', error: outcome.error })
125
- } else if (outcome.request === '') {
126
- entry.verdict = 'passed'
127
- await record({ ...line, status: 'passed' })
128
- } else {
129
- entry.steered = true
130
- entry.verdict = 'steered'
131
- logger.info('[%s] answer check asked for a rewrite', entry.job)
132
- await record({ ...line, status: 'steered', request: outcome.request })
133
- agent.steer(userMessage(outcome.request, randomUUID(), { kind: 'cron-job', channel: 'cron', id: entry.jobId ?? entry.job, name: entry.job, trigger: 'answer-check' }))
134
- }
135
- return
136
- }
137
- },
138
-
139
- /**
140
- * Run a prompt's check once more on its delivered answer, only to log
141
- * whether a steered rewrite fixed it. Never steers.
142
- * @param {string} messageId
143
- * @param {string} text
144
- * @param {{ sessionId?: string, signal?: AbortSignal }} [options]
145
- */
146
- async recheck(messageId, text, { sessionId, signal } = {}) {
147
- const entry = pending.get(messageId)
148
- if (entry?.verdict !== 'steered' || !checkable(text)) return
149
- const outcome = await check(entry, text, signal).catch(error => ({ error: error instanceof Error ? error.message : String(error) }))
150
- if (signal?.aborted) return
151
- const status = 'error' in outcome ? 'error' : outcome.request === '' ? 'fixed' : 'still-flagged'
152
- if (status === 'still-flagged') logger.warn('[%s] the rewritten answer still fails its check', entry.job)
153
- await record({
154
- time: now().toISOString(), job: entry.job, event: 'answer-recheck', ...(sessionId ? { sessionId } : {}), status,
155
- ...('error' in outcome ? { error: outcome.error } : {}),
156
- })
157
- },
158
- }
159
- }