sideby 0.39.0 → 0.40.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.
Files changed (36) hide show
  1. package/dist/build.json +1 -1
  2. package/dist/src/cli/commands.js +34 -3
  3. package/dist/src/cli/json-schemas.js +9 -2
  4. package/dist/src/cli/main.js +10 -2
  5. package/dist/src/core/cli-commands.js +12 -0
  6. package/dist/src/core/config.d.ts +1 -0
  7. package/dist/src/core/config.js +3 -0
  8. package/dist/src/core/progress-state.d.ts +21 -7
  9. package/dist/src/core/progress-state.js +32 -8
  10. package/dist/src/core/search-catalog.js +18 -0
  11. package/dist/src/core/unattended-pending.d.ts +14 -2
  12. package/dist/src/core/unattended-pending.js +27 -3
  13. package/dist/src/core/unattended-state.d.ts +19 -1
  14. package/dist/src/core/unattended-state.js +31 -5
  15. package/dist/src/core/unattended-wait.d.ts +86 -0
  16. package/dist/src/core/unattended-wait.js +125 -0
  17. package/dist/src/core/unattended.d.ts +19 -0
  18. package/dist/src/core/unattended.js +37 -4
  19. package/dist/src/panel/handler.js +16 -1
  20. package/dist/src/panel/i18n.d.ts +9 -0
  21. package/dist/src/panel/i18n.js +26 -8
  22. package/dist/src/panel/page-script.js +51 -2
  23. package/dist/src/panel/page-styles.js +7 -0
  24. package/dist/src/progress/hook.d.ts +7 -0
  25. package/dist/src/progress/hook.js +18 -11
  26. package/dist/src/types.d.ts +5 -0
  27. package/dist/src/unattended/hook.d.ts +21 -6
  28. package/dist/src/unattended/hook.js +261 -19
  29. package/package.json +1 -1
  30. package/schemas/config.json +14 -0
  31. package/schemas/unattended-defer.json +5 -0
  32. package/schemas/unattended-pending.json +5 -0
  33. package/schemas/unattended.json +16 -2
  34. package/skills/sideby/SKILL.md +3 -2
  35. package/skills/sideby/references/progress.md +3 -3
  36. package/skills/sideby/references/unattended.md +16 -2
package/dist/build.json CHANGED
@@ -1 +1 @@
1
- {"id":"20261116T070722-38c598"}
1
+ {"id":"20261116T100604-6fbbc9"}
@@ -4700,6 +4700,12 @@ function deferFamily(env) {
4700
4700
  return 'codex';
4701
4701
  return 'unknown';
4702
4702
  }
4703
+ /** What happens to a question nobody answers (Answer Wait, ADR-0077), and how to change it. */
4704
+ function answerWaitLine(minutes) {
4705
+ return minutes === null
4706
+ ? `If nobody answers: questions shown in a Claude session wait for you, however long. ${c.dim('Change it with `sideby unattended wait <minutes>`.')}`
4707
+ : `If nobody answers: after ${minutes} minute${minutes === 1 ? '' : 's'} with Unattended Mode on, the session goes on (the recommended choice; a step needing your OK is skipped). ${c.dim('Change it with `sideby unattended wait <minutes>|off`.')}`;
4708
+ }
4703
4709
  /** One line for where Unattended Mode stands now. */
4704
4710
  function unattendedLine(s, now) {
4705
4711
  const at = (iso) => clock2(iso, now);
@@ -4740,6 +4746,7 @@ function printUnattended(io, s, now, opts = {}) {
4740
4746
  }
4741
4747
  else if (s.on && s.configured)
4742
4748
  io.out(c.dim('Type in a session and it asks you as usual for 30 minutes; the others stay unattended.'));
4749
+ io.out(answerWaitLine(s.answerWait));
4743
4750
  if (!s.configured)
4744
4751
  io.out(c.cyan('Not set up yet: `sideby unattended on` or `sideby unattended schedule preset weeknights` sets it up, and Claude and Codex sessions sideby starts after that are covered.'));
4745
4752
  else
@@ -4763,9 +4770,9 @@ export async function cmdProgress(a, io) {
4763
4770
  const rt = await createRuntime();
4764
4771
  const notes = (every) => [
4765
4772
  every === undefined
4766
- ? `Off. While a Claude session sideby started waits on background work (subagents, long commands), it can tell you what it waits for every few minutes: \`sideby progress on\` (every ${PROGRESS_DEFAULT} minutes; --every <minutes> for another interval).`
4773
+ ? `Off. While a Claude session sideby started waits on background work (subagents, long commands), it can tell you what it waits for: \`sideby progress on\` (every ${PROGRESS_DEFAULT} minutes; --every <minutes> for another interval).`
4767
4774
  : `On: while a Claude session sideby started waits on background work (subagents, long commands), it tells you every ${every} minutes what it waits for, how long, and whether there is new output.`,
4768
- 'Each reminder is one model turn, which reads the whole conversation: it uses quota.',
4775
+ 'Each reminder is one model turn, which reads the whole conversation: it uses quota, and a longer interval uses less.',
4769
4776
  'Turning it on reaches Claude sessions sideby starts from now on; turning it off or changing the interval also reaches running ones. No reminders while a session is unattended.',
4770
4777
  'Codex is not covered: it has no way for sideby to wake a waiting session.',
4771
4778
  ];
@@ -4963,6 +4970,29 @@ export async function cmdUnattended(a, io) {
4963
4970
  }
4964
4971
  throw new UsageError('usage: sideby unattended schedule [add --days … --from … --to … [--name …] | remove <number> | preset weeknights]');
4965
4972
  }
4973
+ if (sub === 'wait') {
4974
+ allow();
4975
+ const usage = 'usage: sideby unattended wait [<minutes>|off]: minutes from 1 to 120 (for example 10), or off to wait for you however long';
4976
+ if (rest.length > 1)
4977
+ throw new UsageError(usage);
4978
+ const raw = rest[0];
4979
+ if (raw === undefined) {
4980
+ const s = await rt.unattendedStatus();
4981
+ if (asJson)
4982
+ json(io, s);
4983
+ else
4984
+ io.out(answerWaitLine(s.answerWait));
4985
+ return 0;
4986
+ }
4987
+ const minutes = raw === 'off' || raw === 'never' ? null : /^\d{1,3}$/.test(raw) ? Number(raw) : Number.NaN;
4988
+ if (minutes !== null && !(minutes >= 1 && minutes <= 120))
4989
+ throw new UsageError(usage);
4990
+ const change = await u.setAnswerWait({ paths, families, minutes, now: now() });
4991
+ const said = minutes === null
4992
+ ? 'Questions shown in Claude sessions now wait for you, however long.'
4993
+ : `Questions shown in Claude sessions now wait ${minutes} minute${minutes === 1 ? '' : 's'}; then, with Unattended Mode on, the session goes on.`;
4994
+ return show(change, change.changed ? said : `It was already set. ${said}`);
4995
+ }
4966
4996
  if (sub === 'pending') {
4967
4997
  const { listPendingDecisions, markPendingDone } = await import('../core/unattended-pending.js');
4968
4998
  const [action, ...args] = rest;
@@ -5001,6 +5031,7 @@ export async function cmdUnattended(a, io) {
5001
5031
  const where = [
5002
5032
  d.account ?? d.family,
5003
5033
  d.kind === 'question' ? 'question' : d.kind === 'permission' ? 'needed your OK' : 'left undone',
5034
+ d.waited ? `no answer for ${d.waited} min` : '',
5004
5035
  d.agentType ? `subagent ${d.agentType}` : '',
5005
5036
  d.doneAt ? 'done' : '',
5006
5037
  ]
@@ -5037,7 +5068,7 @@ export async function cmdUnattended(a, io) {
5037
5068
  io.out(`${c.green('✓')} kept for the user (${decision.id}); they see it with \`sideby unattended pending\`. Go on with the rest of the task.`);
5038
5069
  return 0;
5039
5070
  }
5040
- throw new UsageError('usage: sideby unattended [status|on|off|schedule|pending|defer]; run `sideby unattended --help`');
5071
+ throw new UsageError('usage: sideby unattended [status|on|off|schedule|wait|pending|defer]; run `sideby unattended --help`');
5041
5072
  }
5042
5073
  /**
5043
5074
  * `sideby commands <words>` (spec §3.36, ADR-0054): the Search Catalog the Panel's Command Palette searches, read-only.
@@ -24,6 +24,10 @@ const PendingDecision = Type.Object({
24
24
  text: Type.String({
25
25
  description: 'The question and its options, the tool and what it was to do, or the deferred text; at most 4000 characters.',
26
26
  }),
27
+ waited: Type.Optional(Type.Integer({
28
+ minimum: 1,
29
+ description: 'Minutes the question or permission prompt was on screen with nobody answering before the session went on without an answer (Answer Wait, ADR-0077). Absent when it was never shown.',
30
+ })),
27
31
  doneAt: Type.Optional(Type.String({ description: 'When it was marked done; ISO.' })),
28
32
  });
29
33
  /** The menu bar shortcut (`sideby menubar hotkey`, spec §3.31). */
@@ -1464,6 +1468,9 @@ export const OUTPUT_SCHEMAS = {
1464
1468
  schedule: Type.Array(UnattendedSlotSchema, {
1465
1469
  description: 'Config `unattended.schedule`, numbered from 1.',
1466
1470
  }),
1471
+ answerWait: Type.Union([Type.Integer({ minimum: 1, maximum: 120 }), Type.Null()], {
1472
+ description: 'Answer Wait (ADR-0077): minutes a question or permission prompt shown in a Claude session sideby started waits for a person before, with Unattended Mode on, the session goes on without an answer. null: never. 10 when config `unattended.answerWait` is not set. Change it with `sideby unattended wait <minutes>|off`.',
1473
+ }),
1467
1474
  pending: Type.Integer({ description: 'Open Pending Decisions (`sideby unattended pending`).' }),
1468
1475
  attended: Type.Array(Type.Object({
1469
1476
  sessionId: Str,
@@ -1497,9 +1504,9 @@ export const OUTPUT_SCHEMAS = {
1497
1504
  problem: Type.Optional(Type.String({
1498
1505
  description: 'Why sideby cannot keep its state: `<state>/unattended` is a link, someone else’s, or writable by others. Says what to do.',
1499
1506
  })),
1500
- changed: Type.Optional(Type.Boolean({ description: 'Set by on, off and schedule changes: whether anything changed.' })),
1507
+ changed: Type.Optional(Type.Boolean({ description: 'Set by on, off, schedule and wait changes: whether anything changed.' })),
1501
1508
  undo: Type.Optional(Type.String({ description: 'The command that puts back what the change replaced.' })),
1502
- }, { title: 'sideby unattended [on|off|schedule …] --json' }),
1509
+ }, { title: 'sideby unattended [on|off|schedule …|wait …] --json' }),
1503
1510
  'unattended-pending': Type.Object({
1504
1511
  schemaVersion: Version,
1505
1512
  pending: Type.Array(PendingDecision, {
@@ -345,6 +345,7 @@ no country allowed; 2 malformed command line.`,
345
345
  sideby unattended off [--json]
346
346
  sideby unattended schedule [add --days <days> --from HH:MM --to HH:MM [--name <name>] [--at <n>]] [--json]
347
347
  sideby unattended schedule remove <n> | preset weeknights [--json]
348
+ sideby unattended wait [<minutes>|off] [--json]
348
349
  sideby unattended pending [--all] [--json]
349
350
  sideby unattended pending done <id> | --all [--json]
350
351
  sideby unattended defer "<text>" [--json]
@@ -359,6 +360,12 @@ that failed for a passing reason (an overloaded server, a dropped connection, a
359
360
  Quota) is started again after 1, 2, then 4 minutes; after three it is kept as a Pending Decision. Codex's own
360
361
  approval prompts are not covered.
361
362
 
363
+ If nobody answers (\`wait\`): a question or permission prompt that a Claude session shows while you are there (you
364
+ typed in it within 30 minutes, or Unattended Mode is off) waits for you. Once it has waited 10 minutes (\`wait
365
+ <minutes>\`, 1 to 120) and Unattended Mode is on, the session goes on as above: the recommended choice where it can
366
+ be undone, the prompt answered "no", never "yes". A turn that ended on a question in plain text goes on the same way.
367
+ Each is kept as a Pending Decision with how long it waited. \`wait off\`: questions wait for you however long.
368
+
362
369
  When it is on: during a Slot of the Schedule (local days and times; an end before the start is the next day), from
363
370
  \`on\` until the time you choose (at most 72 hours; without an option, until the current or next Slot ends, else 12
364
371
  hours), or for one session started with \`--unattended\` (\`sideby run <account> --unattended\`, also next, resume,
@@ -375,6 +382,7 @@ Examples:
375
382
  sideby unattended on --until 07:30
376
383
  sideby unattended schedule preset weeknights
377
384
  sideby unattended schedule add --days sat,sun --from 01:00 --to 09:00 --name "Weekend nights"
385
+ sideby unattended wait 30
378
386
  sideby unattended pending
379
387
  sideby unattended defer "deleting the old branch is left for the user"
380
388
  Exit: 0 shown or done; 1 no such Slot or Pending Decision; 2 a time or option it does not take (the message says
@@ -384,11 +392,11 @@ which to use).`,
384
392
  sideby progress off [--json]
385
393
 
386
394
  Progress reminders: while a Claude session sideby started waits on background work (subagents, long commands),
387
- sideby wakes it every few minutes so it tells you in one or two sentences what it waits for, how long, and whether
395
+ sideby wakes it now and then so it tells you in one or two sentences what it waits for, how long, and whether
388
396
  there is new output. Off by default: each reminder is one model turn, which reads the whole conversation, so it uses
389
397
  quota. No reminders while the session is unattended, or while a turn is running.
390
398
 
391
- \`on\` turns them on (every 10 minutes, or --every <minutes>, 1 to 240); \`off\` turns them off. Turning them on reaches
399
+ \`on\` turns them on (every 30 minutes, or --every <minutes>, 1 to 240); \`off\` turns them off. Turning them on reaches
392
400
  Claude sessions sideby starts from now on; turning them off or changing the interval also reaches running ones. Only
393
401
  sideby's config is written, so there is no preview; each change prints its undo. Codex is not covered: it has no
394
402
  way for sideby to wake a waiting session. Grok, pi and sessions started without sideby are not covered either.
@@ -579,6 +579,18 @@ export const CLI_COMMANDS = [
579
579
  example: 'sideby unattended on --until 07:30',
580
580
  feature: 'page.unattended',
581
581
  },
582
+ {
583
+ id: 'unattended.wait',
584
+ group: 'health',
585
+ usage: 'sideby unattended wait [<minutes>|off]',
586
+ help: 'how long a question shown in a Claude session waits for you before, with Unattended Mode on, the session goes on without an answer (1 to 120 minutes, 10 by default; off: wait however long)',
587
+ summary: {
588
+ en: 'Choose how long a question waits for you before the session goes on',
589
+ zh: '设置提问等你多久,之后会话自己继续',
590
+ },
591
+ example: 'sideby unattended wait 30',
592
+ feature: 'action.unattended-wait',
593
+ },
582
594
  // ---------- Panel and app ----------
583
595
  {
584
596
  id: 'ui',
@@ -131,6 +131,7 @@ export declare const ConfigSchema: Type.TObject<{
131
131
  start: Type.TString;
132
132
  end: Type.TString;
133
133
  }>>>;
134
+ answerWait: Type.TOptional<Type.TUnion<[Type.TInteger, Type.TLiteral<false>]>>;
134
135
  }>>;
135
136
  progress: Type.TOptional<Type.TObject<{
136
137
  every: Type.TOptional<Type.TInteger>;
@@ -255,6 +255,9 @@ export const ConfigSchema = Type.Object({
255
255
  schedule: Type.Optional(Type.Array(UnattendedSlotSchema, {
256
256
  description: "Slots that turn Unattended Mode on and off by themselves, in this computer's time zone. Empty by default.",
257
257
  })),
258
+ answerWait: Type.Optional(Type.Union([Type.Integer({ minimum: 1, maximum: 120 }), Type.Literal(false)], {
259
+ description: 'Answer Wait (ADR-0077): how many minutes a question or a permission prompt shown in a Claude session sideby started waits for a person. Once it has waited this long with nobody answering and Unattended Mode is on, the session goes on as Unattended Mode would: the question with the recommended choice where it can be undone, the permission prompt refused (never allowed); each is kept as a Pending Decision. A turn that ended on a question written as text goes on the same way. `false`: never, questions wait for you. Not set: 10 minutes.',
260
+ })),
258
261
  }, {
259
262
  additionalProperties: false,
260
263
  description: 'Unattended Mode (spec §3.37, ADR-0058): while it is on, sessions sideby starts never stop to wait for a person. Present once the feature is used; from then on sideby adds its hook to every Claude and Codex session it starts, so turning it on later covers sessions already running. `sideby config unset unattended` stops adding it.',
@@ -4,7 +4,7 @@ export declare const PROGRESS_ENV = "SIDEBY_PROGRESS_EVERY";
4
4
  export declare const PROGRESS_MIN = 1;
5
5
  export declare const PROGRESS_MAX = 240;
6
6
  /** What turning it on picks when no interval is given. */
7
- export declare const PROGRESS_DEFAULT = 10;
7
+ export declare const PROGRESS_DEFAULT = 30;
8
8
  /** The intervals the Panel offers. */
9
9
  export declare const PROGRESS_CHOICES: readonly number[];
10
10
  /** The `timeout` of the `Stop` entry, in seconds (the same as Auto Handoff's IdleWatch). */
@@ -26,7 +26,7 @@ export interface WatchRecord {
26
26
  id: string;
27
27
  /** When the session began waiting: kept across reminder turns. */
28
28
  since: string;
29
- /** When the `Stop` that started this watch came. */
29
+ /** When the hook of the `Stop` that started this watch began, before reading its input: the order of the Stops. */
30
30
  at: string;
31
31
  /** When this watch woke the session. */
32
32
  wokeAt?: string;
@@ -49,17 +49,31 @@ export interface LockWait {
49
49
  export declare function readWatch(stateDir: string, sessionId: string): Promise<WatchRecord | undefined>;
50
50
  /**
51
51
  * Replaces the session's watch (file 600, atomically) under the session's lock, so it never lands between another
52
- * watch's last check and its `wokeAt`. False when the lock stays held: then nothing is written and the caller must not
53
- * act as the session's watch. Throws when the folder is not safe to use.
52
+ * watch's last check and its `wokeAt`, unless the record holds the watch of a later `Stop` (its `at` is later): a
53
+ * `Stop` whose hook got its input or the lock late never ends the watch of the turn after it. False when the lock
54
+ * stays held or a later watch is kept: then nothing is written and the caller must not act as the session's watch.
55
+ * Throws when the folder is not safe to use.
54
56
  */
55
57
  export declare function writeWatch(stateDir: string, sessionId: string, record: WatchRecord, opts?: LockWait): Promise<boolean>;
56
58
  /**
57
- * Marks the watch `id` woken at `at`, only while the session's record is still that watch: the check and the write
58
- * hold the session's lock, so a later `Stop` replaces the record before or after, never in between. False when the
59
- * record is another watch's, is missing, or the lock stays held (`beforeWrite` runs inside the lock, for tests).
59
+ * Whether the session's latest prompt is not the one the watching `Stop` ended, so a turn began after it: compared by
60
+ * the prompt's id (`own`, hashed), never by time, because the next prompt's record can be written before the watch
61
+ * starts. Only a `Stop` without an id falls back to whether the record came after it (`stopAt`).
62
+ */
63
+ export declare function newerPrompt(p: PromptRecord | undefined, own: string | undefined, stopAt: number): boolean;
64
+ /**
65
+ * Marks the watch `id` woken at `at`, only while the session's record is still that watch and, with `turn`, no
66
+ * prompt came after the turn the watch's `Stop` ended (`newerPrompt`): a reminder sent then would only queue behind
67
+ * that turn and cost a model call of its own. The checks and the write hold the session's lock, so a later `Stop`
68
+ * replaces the record, or a prompt is noted, before or after, never in between. False when the record is another
69
+ * watch's, is missing, a newer prompt is noted, or the lock stays held (`beforeWrite` runs inside the lock, for tests).
60
70
  */
61
71
  export declare function markWatchWoken(stateDir: string, sessionId: string, id: string, at: Date, deps?: {
62
72
  beforeWrite?: () => Promise<void>;
73
+ turn?: {
74
+ prompt?: string;
75
+ stopAt: number;
76
+ };
63
77
  }): Promise<boolean>;
64
78
  export declare function readPromptRecord(stateDir: string, sessionId: string): Promise<PromptRecord | undefined>;
65
79
  /**
@@ -14,7 +14,7 @@ export const PROGRESS_ENV = 'SIDEBY_PROGRESS_EVERY';
14
14
  export const PROGRESS_MIN = 1;
15
15
  export const PROGRESS_MAX = 240;
16
16
  /** What turning it on picks when no interval is given. */
17
- export const PROGRESS_DEFAULT = 10;
17
+ export const PROGRESS_DEFAULT = 30;
18
18
  /** The intervals the Panel offers. */
19
19
  export const PROGRESS_CHOICES = [5, 10, 15, 30, 60];
20
20
  /** The `timeout` of the `Stop` entry, in seconds (the same as Auto Handoff's IdleWatch). */
@@ -122,18 +122,37 @@ export async function readWatch(stateDir, sessionId) {
122
122
  }
123
123
  /**
124
124
  * Replaces the session's watch (file 600, atomically) under the session's lock, so it never lands between another
125
- * watch's last check and its `wokeAt`. False when the lock stays held: then nothing is written and the caller must not
126
- * act as the session's watch. Throws when the folder is not safe to use.
125
+ * watch's last check and its `wokeAt`, unless the record holds the watch of a later `Stop` (its `at` is later): a
126
+ * `Stop` whose hook got its input or the lock late never ends the watch of the turn after it. False when the lock
127
+ * stays held or a later watch is kept: then nothing is written and the caller must not act as the session's watch.
128
+ * Throws when the folder is not safe to use.
127
129
  */
128
130
  export async function writeWatch(stateDir, sessionId, record, opts = {}) {
129
131
  const dir = (await folder(stateDir, true));
130
- const done = await underLock(dir, hashOf(sessionId), opts.lockWait ?? LOCK_WAIT, () => writeFileAtomic(join(dir, watchName(sessionId)), `${JSON.stringify(record)}\n`, 0o600));
131
- return done !== undefined;
132
+ const done = await underLock(dir, hashOf(sessionId), opts.lockWait ?? LOCK_WAIT, async () => {
133
+ const file = join(dir, watchName(sessionId));
134
+ const old = parseWatch(await readSmallFile(file, RECORD_MAX));
135
+ if (old && Date.parse(old.at) > Date.parse(record.at))
136
+ return false;
137
+ await writeFileAtomic(file, `${JSON.stringify(record)}\n`, 0o600);
138
+ return true;
139
+ });
140
+ return done?.value === true;
141
+ }
142
+ /**
143
+ * Whether the session's latest prompt is not the one the watching `Stop` ended, so a turn began after it: compared by
144
+ * the prompt's id (`own`, hashed), never by time, because the next prompt's record can be written before the watch
145
+ * starts. Only a `Stop` without an id falls back to whether the record came after it (`stopAt`).
146
+ */
147
+ export function newerPrompt(p, own, stopAt) {
148
+ return Boolean(p && (own ? p.prompt !== own : Date.parse(p.at) > stopAt));
132
149
  }
133
150
  /**
134
- * Marks the watch `id` woken at `at`, only while the session's record is still that watch: the check and the write
135
- * hold the session's lock, so a later `Stop` replaces the record before or after, never in between. False when the
136
- * record is another watch's, is missing, or the lock stays held (`beforeWrite` runs inside the lock, for tests).
151
+ * Marks the watch `id` woken at `at`, only while the session's record is still that watch and, with `turn`, no
152
+ * prompt came after the turn the watch's `Stop` ended (`newerPrompt`): a reminder sent then would only queue behind
153
+ * that turn and cost a model call of its own. The checks and the write hold the session's lock, so a later `Stop`
154
+ * replaces the record, or a prompt is noted, before or after, never in between. False when the record is another
155
+ * watch's, is missing, a newer prompt is noted, or the lock stays held (`beforeWrite` runs inside the lock, for tests).
137
156
  */
138
157
  export async function markWatchWoken(stateDir, sessionId, id, at, deps = {}) {
139
158
  const dir = await folder(stateDir, false);
@@ -144,6 +163,11 @@ export async function markWatchWoken(stateDir, sessionId, id, at, deps = {}) {
144
163
  const fresh = parseWatch(await readSmallFile(file, RECORD_MAX));
145
164
  if (fresh?.id !== id)
146
165
  return false;
166
+ if (deps.turn) {
167
+ const p = parsePrompt(await readSmallFile(join(dir, promptName(sessionId)), RECORD_MAX));
168
+ if (newerPrompt(p, deps.turn.prompt, deps.turn.stopAt))
169
+ return false;
170
+ }
147
171
  await deps.beforeWrite?.();
148
172
  await writeFileAtomic(file, `${JSON.stringify({ ...fresh, wokeAt: at.toISOString() })}\n`, 0o600);
149
173
  return true;
@@ -348,6 +348,24 @@ const ENTRIES = [
348
348
  target: { action: 'unattended-add-slot', write: true },
349
349
  cliCommand: 'sideby unattended schedule add',
350
350
  },
351
+ {
352
+ id: 'action.unattended-wait',
353
+ kind: 'action',
354
+ title: { en: 'Choose how long a question waits for you', zh: '设置提问等你多久' },
355
+ synonyms: [
356
+ 'answer wait',
357
+ 'question timeout',
358
+ 'nobody answers',
359
+ 'go on without me',
360
+ '没人回答',
361
+ '问题没人回',
362
+ '自动按推荐继续',
363
+ '等多久',
364
+ ],
365
+ panel: at({ en: 'Unattended page', zh: '无人值守页' }, { en: 'If nobody answers', zh: '没人回答时' }),
366
+ target: { action: 'unattended-wait', write: true },
367
+ cliCommand: 'sideby unattended wait <minutes>|off',
368
+ },
351
369
  {
352
370
  id: 'action.network-guard',
353
371
  kind: 'action',
@@ -17,6 +17,11 @@ export interface PendingDecision {
17
17
  agentType?: string;
18
18
  /** The question and its options, the tool and what it was to do, or the deferred text; at most 4000 characters. */
19
19
  text: string;
20
+ /**
21
+ * Minutes the question or confirmation was on screen with nobody answering before the session went on without an
22
+ * answer (Answer Wait, ADR-0077). Absent when it was never shown.
23
+ */
24
+ waited?: number;
20
25
  /** When the user marked it done (ISO). */
21
26
  doneAt?: string;
22
27
  }
@@ -28,12 +33,19 @@ export declare const pendingDir: (stateDir: string) => string;
28
33
  /** At most `PENDING_TEXT_MAX` characters (whole characters, never half of one), ending in `…` when cut. */
29
34
  export declare function clipPendingText(text: string): string;
30
35
  /**
31
- * Keeps one Pending Decision and returns it. With `dedupe`, an open one of the same kind, session and text is
32
- * returned instead of a second copy (a model that asks the same question twice leaves one).
36
+ * Keeps one Pending Decision and returns it. With `dedupe`, an open one of the same kind, session and text (and both
37
+ * having waited on screen, or neither) is returned instead of a second copy (a model that asks the same question twice
38
+ * leaves one).
33
39
  */
34
40
  export declare function addPendingDecision(stateDir: string, input: PendingInput, at?: Date, opts?: {
35
41
  dedupe?: boolean;
36
42
  }): Promise<PendingDecision>;
43
+ /**
44
+ * Takes back a Pending Decision the caller has just kept, when what it was about ended while it was being kept (Answer
45
+ * Wait: a person answered meanwhile). Only that exact one: the id it got back, still open and kept at `at`; an older
46
+ * one the dedupe returned is left alone. Best effort.
47
+ */
48
+ export declare function withdrawPendingDecision(stateDir: string, kept: PendingDecision, at: Date): Promise<void>;
37
49
  /**
38
50
  * The Pending Decisions, oldest first: open ones, or with `all` the done ones too. Deletes the files of those done
39
51
  * more than 30 days ago.
@@ -45,6 +45,7 @@ function parse(value) {
45
45
  return undefined;
46
46
  const opt = (k) => typeof v[k] === 'string' && v[k] ? { [k]: v[k].slice(0, 4096) } : {};
47
47
  const done = typeof v.doneAt === 'string' && Number.isFinite(Date.parse(v.doneAt)) ? { doneAt: v.doneAt } : {};
48
+ const waited = validWaited(v.waited) ? { waited: v.waited } : {};
48
49
  return {
49
50
  id: v.id,
50
51
  at: v.at,
@@ -56,6 +57,7 @@ function parse(value) {
56
57
  ...opt('agentId'),
57
58
  ...opt('agentType'),
58
59
  text: clipPendingText(v.text),
60
+ ...waited,
59
61
  ...done,
60
62
  };
61
63
  }
@@ -87,15 +89,23 @@ async function readAll(stateDir) {
87
89
  .filter((d) => d !== undefined)
88
90
  .sort((a, b) => Date.parse(a.at) - Date.parse(b.at) || a.id.localeCompare(b.id));
89
91
  }
92
+ /** Whole minutes, at most a week. */
93
+ const validWaited = (v) => typeof v === 'number' && Number.isInteger(v) && v >= 1 && v <= 7 * 24 * 60;
90
94
  const fileOf = (stateDir, id) => join(pendingDir(stateDir), `${id}.json`);
91
95
  /**
92
- * Keeps one Pending Decision and returns it. With `dedupe`, an open one of the same kind, session and text is
93
- * returned instead of a second copy (a model that asks the same question twice leaves one).
96
+ * Keeps one Pending Decision and returns it. With `dedupe`, an open one of the same kind, session and text (and both
97
+ * having waited on screen, or neither) is returned instead of a second copy (a model that asks the same question twice
98
+ * leaves one).
94
99
  */
95
100
  export async function addPendingDecision(stateDir, input, at = new Date(), opts = {}) {
96
101
  const text = clipPendingText(input.text);
97
102
  if (opts.dedupe) {
98
- const same = (await readAll(stateDir)).find((d) => !d.doneAt && d.kind === input.kind && d.sessionId === input.sessionId && d.text === text);
103
+ const same = (await readAll(stateDir)).find((d) => !d.doneAt &&
104
+ d.kind === input.kind &&
105
+ d.sessionId === input.sessionId &&
106
+ d.text === text &&
107
+ // One refused at once and one that went on after a wait are two things the user should see.
108
+ Boolean(d.waited) === Boolean(input.waited));
99
109
  if (same)
100
110
  return same;
101
111
  }
@@ -111,10 +121,24 @@ export async function addPendingDecision(stateDir, input, at = new Date(), opts
111
121
  ...(input.agentId ? { agentId: input.agentId } : {}),
112
122
  ...(input.agentType ? { agentType: input.agentType } : {}),
113
123
  text,
124
+ ...(validWaited(input.waited) ? { waited: input.waited } : {}),
114
125
  };
115
126
  await writeFileAtomic(join(dir, `${decision.id}.json`), `${JSON.stringify(decision)}\n`, 0o600);
116
127
  return decision;
117
128
  }
129
+ /**
130
+ * Takes back a Pending Decision the caller has just kept, when what it was about ended while it was being kept (Answer
131
+ * Wait: a person answered meanwhile). Only that exact one: the id it got back, still open and kept at `at`; an older
132
+ * one the dedupe returned is left alone. Best effort.
133
+ */
134
+ export async function withdrawPendingDecision(stateDir, kept, at) {
135
+ if (kept.at !== at.toISOString() || kept.doneAt)
136
+ return;
137
+ const dir = await existingDir(stateDir);
138
+ if (!dir || !ID.test(kept.id))
139
+ return;
140
+ await rm(join(dir, `${kept.id}.json`), { force: true }).catch(() => { });
141
+ }
118
142
  /**
119
143
  * The Pending Decisions, oldest first: open ones, or with `all` the done ones too. Deletes the files of those done
120
144
  * more than 30 days ago.
@@ -105,10 +105,28 @@ export declare function unattendedState(input: {
105
105
  }): UnattendedState;
106
106
  /** Reads a small JSON file that must be a plain file (opened without following a link); undefined otherwise. */
107
107
  export declare function readSmallJson(file: string, max?: number): Promise<unknown>;
108
- /** What the config file says about Unattended Mode, read by hand (no schema): whether it has the section, and its Schedule. */
108
+ /**
109
+ * Answer Wait (spec §3.37 item 7, ADR-0077): how many minutes a question or a confirmation shown in a Claude session
110
+ * waits for a person before, with Unattended Mode on, the session goes on without an answer. Config
111
+ * `unattended.answerWait`: whole minutes, or `false` for never.
112
+ */
113
+ export declare const ANSWER_WAIT_DEFAULT = 10;
114
+ export declare const ANSWER_WAIT_MIN = 1;
115
+ export declare const ANSWER_WAIT_MAX = 120;
116
+ /** The waits the Panel offers; any other one the CLI set is shown beside them. */
117
+ export declare const ANSWER_WAIT_CHOICES: readonly number[];
118
+ /** Whether `v` is a wait the config takes: whole minutes from 1 to 120. */
119
+ export declare const validAnswerWait: (v: unknown) => v is number;
120
+ /** The wait the config's `unattended.answerWait` value means: minutes, or null for never. Anything else is the default. */
121
+ export declare function parseAnswerWait(value: unknown): number | null;
122
+ /**
123
+ * What the config file says about Unattended Mode, read by hand (no schema): whether it has the section, its Schedule
124
+ * and its Answer Wait (minutes, or null for never).
125
+ */
109
126
  export declare function readUnattendedConfig(configFile: string): Promise<{
110
127
  configured: boolean;
111
128
  schedule: UnattendedSlot[];
129
+ answerWait: number | null;
112
130
  }>;
113
131
  /**
114
132
  * The manual state as the file holds it, expired or not; undefined when there is none, it is not one, or
@@ -307,21 +307,47 @@ export async function readSmallJson(file, max = 1024 * 1024) {
307
307
  return undefined;
308
308
  }
309
309
  }
310
- /** What the config file says about Unattended Mode, read by hand (no schema): whether it has the section, and its Schedule. */
310
+ /**
311
+ * Answer Wait (spec §3.37 item 7, ADR-0077): how many minutes a question or a confirmation shown in a Claude session
312
+ * waits for a person before, with Unattended Mode on, the session goes on without an answer. Config
313
+ * `unattended.answerWait`: whole minutes, or `false` for never.
314
+ */
315
+ export const ANSWER_WAIT_DEFAULT = 10;
316
+ export const ANSWER_WAIT_MIN = 1;
317
+ export const ANSWER_WAIT_MAX = 120;
318
+ /** The waits the Panel offers; any other one the CLI set is shown beside them. */
319
+ export const ANSWER_WAIT_CHOICES = [5, 10, 15, 30, 60];
320
+ /** Whether `v` is a wait the config takes: whole minutes from 1 to 120. */
321
+ export const validAnswerWait = (v) => typeof v === 'number' && Number.isInteger(v) && v >= ANSWER_WAIT_MIN && v <= ANSWER_WAIT_MAX;
322
+ /** The wait the config's `unattended.answerWait` value means: minutes, or null for never. Anything else is the default. */
323
+ export function parseAnswerWait(value) {
324
+ if (value === false)
325
+ return null;
326
+ return validAnswerWait(value) ? value : ANSWER_WAIT_DEFAULT;
327
+ }
328
+ /**
329
+ * What the config file says about Unattended Mode, read by hand (no schema): whether it has the section, its Schedule
330
+ * and its Answer Wait (minutes, or null for never).
331
+ */
311
332
  export async function readUnattendedConfig(configFile) {
333
+ const none = { configured: false, schedule: [], answerWait: ANSWER_WAIT_DEFAULT };
312
334
  // The config may be a link a dotfiles manager keeps; reading through it is fine.
313
335
  try {
314
336
  const text = await readFile(configFile, 'utf8');
315
337
  if (text.length > 4 * 1024 * 1024)
316
- return { configured: false, schedule: [] };
338
+ return none;
317
339
  const config = JSON.parse(text);
318
340
  const section = config && typeof config === 'object' ? config.unattended : undefined;
319
341
  if (!section || typeof section !== 'object')
320
- return { configured: false, schedule: [] };
321
- return { configured: true, schedule: parseSchedule(section.schedule) };
342
+ return none;
343
+ return {
344
+ configured: true,
345
+ schedule: parseSchedule(section.schedule),
346
+ answerWait: parseAnswerWait(section.answerWait),
347
+ };
322
348
  }
323
349
  catch {
324
- return { configured: false, schedule: [] };
350
+ return none;
325
351
  }
326
352
  }
327
353
  /**
@@ -0,0 +1,86 @@
1
+ /**
2
+ * The `timeout` of the hook entries that wait. A question shown at 18:00 with Unattended Mode off still goes on when a
3
+ * Slot begins at 23:00; Claude Code ends the hook after this and the question then simply stays on screen.
4
+ */
5
+ export declare const ANSWER_HOOK_TIMEOUT_S: number;
6
+ /** How often a waiting hook looks again: an answered question frees its hook within this. */
7
+ export declare const WATCH_POLL_MS = 3000;
8
+ /** What the session record says is on screen now. */
9
+ export interface ScreenMark {
10
+ status: string;
11
+ /** `statusUpdatedAt`: changes whenever the dialog or the wait ends or another one takes its place. */
12
+ at: number;
13
+ }
14
+ /** Claude Code's config folder for the session: `CLAUDE_CONFIG_DIR`, else `~/.claude`. */
15
+ export declare function claudeConfigDir(env: {
16
+ HOME?: string | undefined;
17
+ CLAUDE_CONFIG_DIR?: string | undefined;
18
+ }): string;
19
+ /**
20
+ * The status the record of process `pid` holds for session `sessionId`; undefined when it cannot be read, is a link,
21
+ * names another session, or lacks `status` or `statusUpdatedAt`.
22
+ */
23
+ export declare function readScreen(env: {
24
+ HOME?: string | undefined;
25
+ CLAUDE_CONFIG_DIR?: string | undefined;
26
+ }, pid: number, sessionId: string): Promise<ScreenMark | undefined>;
27
+ /** What a waiting hook reads afresh at every look: the wait (minutes, or null for never) and whether the mode is on. */
28
+ export interface WaitSettings {
29
+ wait: number | null;
30
+ on: boolean;
31
+ }
32
+ /**
33
+ * Waits while the session shows `expect` (`waiting`: a question or a permission prompt; `idle`: a turn ended and the
34
+ * session waits for a prompt). Returns the record's mark once the wait has passed since `startedAt` and Unattended
35
+ * Mode is on, so the caller can look once more just before it acts (`lastLook`), and undefined as soon as anything
36
+ * says a person (or anything else) moved on or it cannot tell: there is no record for the session, it does not show
37
+ * `expect` within 2 seconds, it shows a dialog put up after `shownBy` (the dialog this hook was started for was on
38
+ * screen before the hook began, so a later one is another dialog: the person answered this one meanwhile), it changes
39
+ * (another status or another `statusUpdatedAt`), it cannot be read, `moved` says so (a person typed after the turn
40
+ * the hook watches), the Host is gone, or the wait was set to never. While the mode is off past the wait it keeps
41
+ * waiting; Claude Code's `timeout` ends it in the end. With `from` it goes on watching that mark, without looking for it
42
+ * first.
43
+ */
44
+ export declare function watchScreen(opts: {
45
+ env: {
46
+ HOME?: string | undefined;
47
+ CLAUDE_CONFIG_DIR?: string | undefined;
48
+ };
49
+ pid: number;
50
+ sessionId: string;
51
+ expect: 'waiting' | 'idle';
52
+ startedAt: number;
53
+ clock: () => number;
54
+ sleep: (ms: number) => Promise<void>;
55
+ parentGone: () => boolean;
56
+ settings: () => Promise<WaitSettings>;
57
+ /** Whether something sideby noted says the session moved on since `startedAt`; checked at every look. */
58
+ moved?: () => Promise<boolean>;
59
+ /** The latest `statusUpdatedAt` the watched mark may have (when the hook began). */
60
+ shownBy?: number;
61
+ /** A mark already found: go on watching it. */
62
+ from?: ScreenMark;
63
+ }): Promise<ScreenMark | undefined>;
64
+ /**
65
+ * The last look before a waiting hook acts (and again after it kept the Pending Decision), with the wait and the mode
66
+ * read afresh: `act` while the Host is still there, the record still shows `mark`, nothing sideby noted says the
67
+ * session moved on, the wait (as set now) has passed and Unattended Mode is on; `wait` when all that holds but the
68
+ * wait was made longer and has not passed, or the mode went off: the hook goes on watching; `stop` for anything else,
69
+ * the wait set to never, or anything it cannot read: the hook then lets it be.
70
+ */
71
+ export declare function lastLook(opts: {
72
+ env: {
73
+ HOME?: string | undefined;
74
+ CLAUDE_CONFIG_DIR?: string | undefined;
75
+ };
76
+ pid: number;
77
+ sessionId: string;
78
+ mark: ScreenMark;
79
+ startedAt: number;
80
+ clock: () => number;
81
+ parentGone: () => boolean;
82
+ settings: () => Promise<WaitSettings>;
83
+ moved?: () => Promise<boolean>;
84
+ }): Promise<'act' | 'wait' | 'stop'>;
85
+ /** Whole minutes from `startedAt` to `now`, at least 1: how long the question waited, as the user reads it. */
86
+ export declare const waitedMinutes: (startedAt: number, now: number) => number;