pi-pier 0.3.0 → 0.3.1

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 (96) hide show
  1. package/README.md +12 -11
  2. package/package.json +2 -2
  3. package/schemas/role-manifest.schema.json +7 -1
  4. package/skills/pi-herdr-workflow/SKILL.md +4 -5
  5. package/src/ansi-text.ts +62 -11
  6. package/src/ask-dialog.ts +308 -0
  7. package/src/ask-user.ts +113 -112
  8. package/src/bootstrap.ts +8 -12
  9. package/src/compact-coordinator.ts +278 -261
  10. package/src/compact-economics-core.ts +77 -145
  11. package/src/config-catalog-core.ts +77 -197
  12. package/src/config-command.ts +271 -64
  13. package/src/dashboard-command.ts +38 -19
  14. package/src/efficiency-config-core.ts +287 -484
  15. package/src/efficiency-store.ts +16 -29
  16. package/src/focus-poller.ts +14 -35
  17. package/src/gc-core.ts +10 -36
  18. package/src/herdr-client.ts +82 -208
  19. package/src/history-store.ts +36 -23
  20. package/src/index-gates.ts +124 -0
  21. package/src/index-locks.ts +2 -7
  22. package/src/index-master.ts +24 -18
  23. package/src/index-notices.ts +28 -15
  24. package/src/index-pipe.ts +17 -12
  25. package/src/index-roles.ts +306 -0
  26. package/src/index-runtime.ts +2 -6
  27. package/src/index-worker.ts +4 -8
  28. package/src/index.ts +305 -564
  29. package/src/jev-client.ts +12 -17
  30. package/src/jev-core.ts +137 -87
  31. package/src/ledger.ts +13 -21
  32. package/src/lock-core.ts +28 -56
  33. package/src/manifest-compose.ts +35 -20
  34. package/src/observation-core.ts +52 -100
  35. package/src/pane-title.ts +19 -38
  36. package/src/pi-surface.ts +34 -22
  37. package/src/pier-options.ts +54 -50
  38. package/src/pipe-channel.ts +27 -59
  39. package/src/platform-paths.ts +19 -52
  40. package/src/{core → plugins}/grid-shape.ts +8 -10
  41. package/src/{core → plugins}/heat-plan.ts +20 -22
  42. package/src/plugins/observation.ts +475 -0
  43. package/src/plugins/subagent.ts +728 -0
  44. package/src/{core → plugins}/terminal.ts +167 -127
  45. package/src/{core → plugins}/todo.ts +61 -100
  46. package/src/progress-core.ts +16 -53
  47. package/src/reconcile-core.ts +24 -33
  48. package/src/reducer-core.ts +24 -60
  49. package/src/reducer-invoker.ts +266 -239
  50. package/src/renderers.ts +76 -87
  51. package/src/role-loader.ts +33 -28
  52. package/src/role-manifest.ts +23 -28
  53. package/src/role-state.ts +119 -0
  54. package/src/routing-telemetry.ts +136 -0
  55. package/src/runtime-policy.ts +25 -40
  56. package/src/session-tail.ts +65 -41
  57. package/src/settle-wake-core.ts +52 -21
  58. package/src/slim-frame.ts +25 -122
  59. package/src/stale-core.ts +17 -24
  60. package/src/storage-layout.ts +14 -43
  61. package/src/subagent-core.ts +253 -118
  62. package/src/subagent-gc.ts +109 -67
  63. package/src/subagent-poller.ts +367 -24
  64. package/src/subagent-session.ts +516 -0
  65. package/src/subagent-spawn.ts +530 -52
  66. package/src/swallow.ts +7 -17
  67. package/src/terminal-core.ts +125 -106
  68. package/src/todo-core.ts +57 -109
  69. package/src/todo-read-hook.ts +23 -39
  70. package/src/todo-reminder-core.ts +26 -51
  71. package/src/todo-window.ts +11 -12
  72. package/src/todos-service.ts +15 -35
  73. package/src/tool-error.ts +6 -13
  74. package/src/tool-gate.ts +38 -21
  75. package/src/vocab.ts +52 -25
  76. package/src/ask-multi.ts +0 -260
  77. package/src/config-guide.ts +0 -361
  78. package/src/core/observation.ts +0 -555
  79. package/src/core/subagent.ts +0 -633
  80. package/src/efficiency-config-io.ts +0 -53
  81. package/src/git-adapter.ts +0 -113
  82. package/src/notice-buffer.ts +0 -23
  83. package/src/subagent-execute.ts +0 -328
  84. package/src/subagent-git-io.ts +0 -80
  85. package/src/subagent-launch.ts +0 -114
  86. package/src/subagent-list-action.ts +0 -69
  87. package/src/subagent-output-action.ts +0 -104
  88. package/src/subagent-output-adapter.ts +0 -27
  89. package/src/subagent-output-core.ts +0 -263
  90. package/src/subagent-poll-loop.ts +0 -305
  91. package/src/subagent-port.ts +0 -27
  92. package/src/subagent-ready.ts +0 -89
  93. package/src/subagent-registry.ts +0 -70
  94. package/src/subagent-resolution.ts +0 -41
  95. package/src/subagent-scope.ts +0 -41
  96. package/src/subagent-session-io.ts +0 -230
@@ -1,11 +1,9 @@
1
1
  /**
2
2
  * D78/D81 todo loader entry.
3
- *
4
- * A Cordis plugin keeps the master surface hot-swappable while allowing workers to mount the
5
- * one-shot subset directly. Injected services preserve the D79 registration boundary and keep
6
- * session-owned todo state in index. This module owns the tool, command, widget, read hook,
7
- * and master stop reminder; reconciliation, mirroring, and agent reporting stay with the
8
- * session state layer to avoid a dependency cycle.
3
+ * A Cordis plugin keeps the master surface hot-swappable while workers mount the one-shot subset directly;
4
+ * injected services preserve the D79 registration boundary and keep session-owned todo state in index. This
5
+ * module owns the tool, command, widget, read hook, and master stop reminder — reconciliation, mirroring, and
6
+ * agent reporting stay in the session state layer (cycle avoidance).
9
7
  */
10
8
  import { Context } from '@deepseek-ai/cordis';
11
9
  import { Type } from 'typebox';
@@ -14,31 +12,20 @@ import type { TodosService } from '../todos-service.ts';
14
12
  import { planTodoReadHook } from '../todo-read-hook.ts';
15
13
  import { TODO_REMINDER_CUSTOM_TYPE, planStopTodoReminder, todoReminderGraceMs } from '../todo-reminder-core.ts';
16
14
  import { makeProgressUpdate } from '../subagent-core.ts';
17
- import { TODO_DETAILS_KEY, TODO_TOOL_NAME, formatTodoConfirmation, type TodoItem } from '../vocab.ts';
18
- import { toolError } from '../tool-error.ts'
19
-
20
- /** Raw tool arguments: every field is validated inside the action handlers. */
21
- type ToolParams = Record<string, unknown> | undefined;;
15
+ import { TODO_DETAILS_KEY, TODO_STATUSES, TODO_TOOL_NAME, formatTodoConfirmation, type TodoItem } from '../vocab.ts';
16
+ import { toolError } from '../tool-error.ts';
22
17
  import { formatAge, isArchived } from '../stale-core.ts';
23
- import {
24
- TODO_EDIT_CUSTOM_TYPE,
25
- completionTransitions,
26
- fuzzyFind,
27
- listsEqual,
28
- makeSnapshot,
29
- normalizeStrict,
30
- revertedCompleted,
31
- validateTodos,
32
- } from '../todo-core.ts';
18
+ import { TODO_EDIT_CUSTOM_TYPE, completionTransitions, fuzzyFind, listsEqual, makeSnapshot, normalizeStrict, revertedCompleted, validateTodos, type TodoEditOp } from '../todo-core.ts';
33
19
  import { anchorTodoRange, formatTodoSummary, renderTodoGroups } from '../todo-window.ts';
34
20
  import { swallow } from '../swallow.ts';
35
21
 
22
+ type ToolParams = Record<string, unknown> | undefined;
23
+
36
24
  export interface TodoUiSlot {
37
25
  /** Lets index lifecycle events render through the currently mounted plugin. */
38
26
  renderWidget: (ctx: unknown) => void;
39
27
  /** Re-render from the last seen context without a fresh event (human-gate open/close). */
40
28
  rerenderWidget?: () => void;
41
- /** Cancel any pending unfinished-todo reminder timer. */
42
29
  cancelReminder?: () => void;
43
30
  }
44
31
 
@@ -71,24 +58,28 @@ const TOOL_DESCRIPTION = [
71
58
  'Delegated work belongs on this list too. When you hand an entry to a subagent, append ` <sub>` to its content (the subagent is doing it, not you); when the subagent settles, a matching entry is auto-completed — you will see "Reconciled:" in the settlement note. If no auto-match fired, update the entry yourself.',
72
59
  ].join(' ');
73
60
 
74
- /** Stay within pi interactive mode's ten-line widget cap instead of relying on head truncation. */
75
- export const WIDGET_MAX_LINES = 10;
61
+ /** pi interactive mode renders at most ten widget lines. */
62
+ const WIDGET_MAX_LINES = 10;
63
+
64
+ /** `/todos <op> <query>` verbs; the value is the user-facing past-tense feedback. */
65
+ const OP_VERBS: Record<TodoEditOp, string> = {
66
+ done: 'completed',
67
+ drop: 'abandoned',
68
+ unblock: 'unblocked (back to pending)',
69
+ rm: 'removed',
70
+ };
76
71
 
77
72
  /**
78
- * Keep the active task visible within WIDGET_MAX_LINES instead of truncating to a fixed head or
79
- * tail. The window anchors on the first in-progress item, then the last open item, then recent
80
- * completions. A +N line points to /todos when the full plan cannot fit. This fixes 01a03c0d,
81
- * where active work scrolled out while only the final entries remained visible.
73
+ * Keep the active task visible within WIDGET_MAX_LINES rather than truncating to a fixed head or tail:
74
+ * the window anchors on the first in-progress item, then the last open item, then recent completions.
82
75
  */
83
76
  export function widgetLines(
84
77
  items: readonly TodoItem[],
85
78
  opts?: { archivedAgeMs?: number | null; blockedDepth?: number | null },
86
79
  ): string[] {
87
80
  if (items.length === 0) return [];
88
- // Human gate open (ask_user_question waiting): the question + editor own the fixed area, so the
89
- // widget collapses to the one-line summary — /todos still reaches the full list. Without this,
90
- // a 10-line widget plus a multi-line question leaves almost no scrollable transcript
91
- // (user-reported: both blocks visible ⇒ history range too small).
81
+ // Human gate open (ask_user_question waiting): the question and editor own the fixed area, so the widget
82
+ // collapses to one summary line — a 10-line widget plus a multi-line question would leave no scrollable transcript.
92
83
  if ((opts?.blockedDepth ?? 0) > 0) {
93
84
  return [`${formatTodoSummary(items)} · /todos 全量`];
94
85
  }
@@ -100,8 +91,8 @@ export function widgetLines(
100
91
  ];
101
92
  }
102
93
 
103
- // Reserve summary and overflow lines, then shrink around the anchor because phase headers also consume the budget.
104
- const renderBudget = WIDGET_MAX_LINES - 1 - 1;
94
+ // Reserve summary and overflow lines; phase headers consume the budget too, hence the shrink loop.
95
+ const renderBudget = WIDGET_MAX_LINES - 2;
105
96
  const [start, end] = anchorTodoRange(
106
97
  items,
107
98
  (s, e) => renderTodoGroups(items.slice(s, e)).length <= renderBudget,
@@ -129,21 +120,13 @@ export default function todoPlugin(ctx: Context): void {
129
120
  : null;
130
121
  }
131
122
 
132
- /** Name the guarded event UI once so callers do not repeat unsafe inline assertions. */
133
- function widgetUi(eventCtx: unknown): { setWidget?: (id: string, lines: string[]) => void } | undefined {
134
- if (eventCtx === null || typeof eventCtx !== 'object' || !('ui' in eventCtx)) return undefined;
135
- const ui = (eventCtx as { ui: unknown }).ui; // Safe after the property guard above.
136
- return ui !== null && typeof ui === 'object'
137
- ? (ui as { setWidget?: (id: string, lines: string[]) => void })
138
- : undefined;
139
- }
140
-
141
123
  /** Gate transitions fire outside lifecycle events; remember one ctx so rerenderWidget can target the live ui. */
142
124
  let lastEventCtx: unknown = null;
143
125
  function renderWidget(eventCtx: unknown): void {
144
126
  if (eventCtx !== null && eventCtx !== undefined) lastEventCtx = eventCtx;
127
+ const ui = (eventCtx as { ui?: { setWidget?: (id: string, lines: string[]) => void } } | null | undefined)?.ui;
145
128
  try {
146
- widgetUi(eventCtx)?.setWidget?.('todos', widgetLines(todos.items, {
129
+ ui?.setWidget?.('todos', widgetLines(todos.items, {
147
130
  archivedAgeMs: archivedAgeMs(),
148
131
  blockedDepth: getBlockedDepth?.() ?? 0,
149
132
  }));
@@ -152,7 +135,6 @@ export default function todoPlugin(ctx: Context): void {
152
135
  }
153
136
  }
154
137
 
155
- // Let index lifecycle hooks call the current plugin implementation.
156
138
  state.renderWidget = renderWidget;
157
139
  state.rerenderWidget = () => renderWidget(lastEventCtx);
158
140
 
@@ -164,7 +146,6 @@ export default function todoPlugin(ctx: Context): void {
164
146
  let staleNotices = 0;
165
147
  let lastStaleGuardTurn: number | null = null;
166
148
 
167
- // Any real edit ends the current stale period because the plan is active again.
168
149
  todos.on('todo.updated', () => {
169
150
  lastWriteTurn = todoReadTurn;
170
151
  staleNotices = 0;
@@ -185,21 +166,18 @@ export default function todoPlugin(ctx: Context): void {
185
166
  staleNotices += 1;
186
167
  lastStaleGuardTurn = todoReadTurn;
187
168
  }
188
- // Share cadence across empty, archive, and R2 rewrite notices so adjacent notices do not
189
- // consume the grace window.
169
+ // Empty, archive, and R2 rewrite notices share one cadence so adjacent notices do not each consume the grace window.
190
170
  if (plan.inject && (plan.effect === 'empty-guard' || plan.archived)) {
191
171
  lastEmptyGuardTurn = todoReadTurn;
192
172
  }
193
- // R1 clears archived entries after notice because retaining dead items suppresses the empty
194
- // guard that restored tracking after the 01a03c0d two-hour gap. Persist removals for replay;
195
- // the session log still retains history.
173
+ // R1: persist the clear as one rm-per-item edit so replay folds to an empty list. The session
174
+ // log keeps the history; a dead in-memory list would suppress the empty guard.
196
175
  if (plan.inject && plan.effect === 'archive-notice' && plan.clearArchived && todos.items.length > 0) {
197
176
  const edits = todos.items.map((it) => ({ op: 'rm' as const, content: it.content }));
198
177
  try {
199
178
  appendEntry(TODO_EDIT_CUSTOM_TYPE, { version: 1, edits, ts: Date.now() });
200
179
  } catch (err) {
201
- // Persistence is best-effort because the in-memory clear must still proceed, but a silent
202
- // failure here is exactly how the archive/replay path broke unnoticed before (SA-13).
180
+ // The in-memory clear must proceed even when this persistence fails.
203
181
  swallow('todo.persist-archive', err);
204
182
  }
205
183
  todos.replace([], { source: 'archive' });
@@ -220,7 +198,7 @@ export default function todoPlugin(ctx: Context): void {
220
198
  sendMessage?: (
221
199
  message: { customType: string; content: string; display?: boolean; details?: Record<string, unknown> },
222
200
  opts?: { deliverAs?: string; triggerTurn?: boolean },
223
- ) => Promise<void>;
201
+ ) => void;
224
202
  };
225
203
  let todoReminders = 0;
226
204
  let lastAssistantStopReason: string | null = null;
@@ -233,24 +211,18 @@ export default function todoPlugin(ctx: Context): void {
233
211
  }
234
212
  state.cancelReminder = cancelTodoReminder;
235
213
  scoped.on('turn_end', async (event: unknown) => {
236
- if (event === null || typeof event !== 'object' || !('message' in event)) return;
237
- const msg = (event as { message: unknown }).message;
214
+ const msg = (event as { message?: unknown } | null | undefined)?.message;
238
215
  if (msg === null || typeof msg !== 'object') return;
239
216
  const { role, stopReason } = msg as { role?: unknown; stopReason?: unknown };
240
- if (role === 'assistant' && typeof stopReason === 'string') {
241
- lastAssistantStopReason = stopReason;
242
- }
217
+ if (role === 'assistant' && typeof stopReason === 'string') lastAssistantStopReason = stopReason;
243
218
  });
244
- scoped.on('agent_start', () => cancelTodoReminder());
245
- scoped.on('session_shutdown', () => cancelTodoReminder());
219
+ for (const ev of ['agent_start', 'session_shutdown']) scoped.on(ev, () => cancelTodoReminder());
246
220
  scoped.on('agent_settled', async () => {
247
221
  cancelTodoReminder();
248
- const inFlight = stopReminder.isCompactionInFlight?.() ?? false;
249
- const intentional = stopReminder.isIntentionalAbort?.() ?? false;
250
222
  const plan = planStopTodoReminder({
251
223
  lastStopReason: lastAssistantStopReason,
252
- intentionalAbort: intentional,
253
- compactionInFlight: inFlight,
224
+ intentionalAbort: stopReminder.isIntentionalAbort?.() ?? false,
225
+ compactionInFlight: stopReminder.isCompactionInFlight?.() ?? false,
254
226
  reminders: todoReminders,
255
227
  runningSubs: stopReminder.getRunningSubs(),
256
228
  blockedDepth: stopReminder.getBlockedDepth(),
@@ -261,19 +233,20 @@ export default function todoPlugin(ctx: Context): void {
261
233
  todoReminderTimer = setTimeout(() => {
262
234
  todoReminderTimer = null;
263
235
  if (stopReminder.isCompactionInFlight?.()) return;
264
- void (async () => {
265
- const send = pi.sendMessage;
266
- if (typeof send !== 'function') return;
267
- try {
268
- await send(
269
- { customType: TODO_REMINDER_CUSTOM_TYPE, content, display: true },
270
- { deliverAs: 'followUp', triggerTurn: true },
271
- );
272
- todoReminders += 1;
273
- } catch {
274
- /* Delivery failure is non-fatal. */
275
- }
276
- })();
236
+ const send = pi.sendMessage;
237
+ if (typeof send !== 'function') return;
238
+ try {
239
+ // pi's sendMessage is fire-and-forget (it returns void), so the cap counter must advance
240
+ // synchronously: `send(...).then(...)` threw, got swallowed here, and every settle re-sent
241
+ // "Reminder 1/3" forever.
242
+ send(
243
+ { customType: TODO_REMINDER_CUSTOM_TYPE, content, display: true },
244
+ { deliverAs: 'followUp', triggerTurn: true },
245
+ );
246
+ todoReminders += 1;
247
+ } catch {
248
+ /* A synchronous send failure must not escape the timer callback. */
249
+ }
277
250
  }, todoReminderGraceMs());
278
251
  todoReminderTimer.unref?.();
279
252
  });
@@ -292,13 +265,10 @@ export default function todoPlugin(ctx: Context): void {
292
265
  Type.Object(
293
266
  {
294
267
  content: Type.String({ description: 'What the task is — a short imperative line' }),
295
- status: Type.Union([
296
- Type.Literal('pending'),
297
- Type.Literal('in_progress'),
298
- Type.Literal('completed'),
299
- Type.Literal('blocked'),
300
- Type.Literal('abandoned'),
301
- ], { description: 'pending | in_progress | completed | blocked | abandoned' }),
268
+ status: Type.Union(
269
+ TODO_STATUSES.map((status) => Type.Literal(status)),
270
+ { description: TODO_STATUSES.join(' | ') },
271
+ ),
302
272
  blocker: Type.Optional(Type.String({ description: 'Only when status is "blocked": what it is waiting for. Omit the field entirely (never an empty string) for other statuses' })),
303
273
  phase: Type.Optional(Type.String({ description: 'Optional group name (≤30 chars); omit for a flat list (never an empty string)' })),
304
274
  },
@@ -315,10 +285,9 @@ export default function todoPlugin(ctx: Context): void {
315
285
  // A1: an invalid list is a hard failure — throw so pi flags isError for the model.
316
286
  return toolError(result.error ?? 'invalid todo list');
317
287
  }
318
- const strictMode = todos.config.strict; // D75 phase 2 keeps policy in the service config.
319
288
  let next = result.items!;
320
289
  const strictNotes: string[] = [];
321
- if (strictMode) {
290
+ if (todos.config.strict) {
322
291
  const strict = normalizeStrict(next);
323
292
  const demoted = next
324
293
  .filter((it, i) => it.status === 'in_progress' && strict[i]?.status === 'pending')
@@ -329,11 +298,10 @@ export default function todoPlugin(ctx: Context): void {
329
298
  if (promoted) strictNotes.push(`auto-promoted to in_progress: ${promoted}`);
330
299
  next = strict;
331
300
  }
332
- // D35 avoids persistence and mirroring when the authoritative list is unchanged.
301
+ // D35: skip persistence and mirroring when the authoritative list is unchanged.
333
302
  if (listsEqual(todos.items, next)) {
334
303
  return { content: [{ type: 'text', text: 'No change: todo list already matches.' }], details: {} };
335
304
  }
336
- // D36/D37 surface completion transitions and regressions to the caller.
337
305
  const completed = completionTransitions(todos.items, next);
338
306
  const reverted = revertedCompleted(todos.items, next);
339
307
  todos.replace(next, { source: 'tool' });
@@ -363,12 +331,11 @@ export default function todoPlugin(ctx: Context): void {
363
331
  description: 'Show the todo list, or edit it: /todos done|drop|rm|unblock <fuzzy content match>',
364
332
  handler: async (args: unknown, eventCtx: unknown) => {
365
333
  const ui = (eventCtx as { ui?: { notify?: (text: string, level?: string) => void } }).ui;
366
- const ops = ['done', 'drop', 'rm', 'unblock'] as const;
367
334
  const raw = typeof args === 'string'
368
335
  ? args.split(/\s+/).filter(Boolean)
369
336
  : Array.isArray(args) ? args.map(String) : [];
370
337
  const opArg = raw[0];
371
- if (opArg && (ops as readonly string[]).includes(opArg)) {
338
+ if (opArg && Object.hasOwn(OP_VERBS, opArg)) {
372
339
  const query = raw.slice(1).join(' ').trim();
373
340
  if (!query) {
374
341
  ui?.notify?.('usage: /todos done|drop|rm|unblock <content>', 'warning');
@@ -384,14 +351,14 @@ export default function todoPlugin(ctx: Context): void {
384
351
  return;
385
352
  }
386
353
  const content = candidates[0];
387
- const edit = { op: opArg as 'done' | 'drop' | 'rm' | 'unblock', content };
354
+ const edit = { op: opArg as TodoEditOp, content };
388
355
  const before = todos.items;
389
356
  todos.applyEdits([edit], { source: 'human' });
390
357
  if (listsEqual(before, todos.items)) {
391
358
  ui?.notify?.(`no change: "${content}" is already in that state`, 'info');
392
359
  return;
393
360
  }
394
- // D38 persists human edits so branch replay rebuilds the same authoritative state.
361
+ // D38: persist human edits so branch replay rebuilds the same authoritative state.
395
362
  try {
396
363
  appendEntry(TODO_EDIT_CUSTOM_TYPE, { version: 1, edits: [edit], ts: Date.now() });
397
364
  } catch {
@@ -399,14 +366,9 @@ export default function todoPlugin(ctx: Context): void {
399
366
  }
400
367
  mirrorTodos();
401
368
  renderWidget(eventCtx);
402
- const verb = opArg === 'done' ? 'completed'
403
- : opArg === 'drop' ? 'abandoned'
404
- : opArg === 'unblock' ? 'unblocked (back to pending)'
405
- : 'removed';
406
- ui?.notify?.(`"${content}" ${verb}`, 'info');
369
+ ui?.notify?.(`"${content}" ${OP_VERBS[edit.op]}`, 'info');
407
370
  return;
408
371
  }
409
- // /todos is the unbounded view because the widget intentionally prioritizes active context.
410
372
  const body = renderTodoGroups(todos.items);
411
373
  if (body.length === 0) {
412
374
  ui?.notify?.('todo list is empty', 'info');
@@ -419,4 +381,3 @@ export default function todoPlugin(ctx: Context): void {
419
381
  },
420
382
  });
421
383
  }
422
-
@@ -1,41 +1,27 @@
1
1
  /**
2
- * M16 pure core for progress visibility (development plan §M16; prioritize conservative output, with no new D3 protocol).
3
- *
4
- * Channels in the post-M22 world:
5
- * - Progress badge (N/M plus optional ETA) is embedded in the pane title (state_labels has only five keys, so it cannot fit there);
6
- * - Tool badge (🔧 tool) is sent through report_agent.message;
7
- * - Rate estimates follow kimi estimator semantics: rate window, unfinished cap, and confidence gate.
8
- *
9
- * Conservative principle (risk ⑥ decision): when an estimate is not trustworthy, fall back to plain `N/M` and
10
- * never display a misleading ETA.
2
+ * M16 progress visibility: conservative output only, no new D3 protocol. The progress badge
3
+ * (N/M plus optional ETA) rides in the pane title, the tool badge (🔧 tool) goes through
4
+ * report_agent.message, and an untrustworthy estimate falls back to plain `N/M` — never a
5
+ * misleading ETA.
11
6
  */
12
7
 
13
8
  import { countTodos, type TodoItem } from './vocab.ts';
14
9
 
15
10
  /** Minimum completed points needed for an ETA (<2 cannot establish a rate). */
16
11
  export const ETA_MIN_SAMPLES = 2;
17
- /** Rate sample window (kimi rate-window semantics; use the most recent K completion points). */
18
- export const RATE_WINDOW_MS = 45_000;
19
- /** Cap for displayed unfinished progress (kimi unfinished-cap semantics; prevent a false "almost done" impression). */
20
- export const UNFINISHED_CAP = 0.85;
21
- /** Completion freshness gate: if the most recent completion is older than this, data is stale and no ETA is estimated. */
12
+ /** An older latest completion means the data is stale and no ETA is estimated. */
22
13
  export const PROGRESS_STALE_MS = 5 * 60_000;
23
- /** Number of recent completion points used for rate fitting. */
24
14
  const RATE_SAMPLE_POINTS = 5;
25
15
 
26
16
  export interface EtaEstimate {
27
- /** Remaining steps. */
28
17
  remaining: number;
29
- /** Estimated remaining time (milliseconds). */
30
18
  etaMs: number;
31
19
  confidence: 'ok';
32
20
  }
33
21
 
34
22
  /**
35
- * Estimate ETA from completion timestamps and remaining steps.
36
- * - Fewer than 2 completion points → null (no rate can be inferred);
37
- * - Most recent completion older than PROGRESS_STALE_MS → null (stale after a burst followed by a pause or manual hold);
38
- * - Rate = use the latest K(≤5) points, (k-1) / (t_k - t_1); eta = remaining / rate.
23
+ * Fewer than 2 points, or a stale latest point, → null.
24
+ * Rate = latest K(≤5) points, (k-1) / (t_k - t_1); eta = remaining / rate.
39
25
  */
40
26
  export function estimateEta(opts: {
41
27
  completedAt: readonly number[];
@@ -54,11 +40,10 @@ export function estimateEta(opts: {
54
40
  const span = sample[sample.length - 1] - sample[0];
55
41
  const steps = sample.length - 1;
56
42
  if (steps < 1 || span <= 0) return null;
57
- const ratePerMs = steps / span;
58
- return { remaining, etaMs: Math.round(remaining / ratePerMs), confidence: 'ok' };
43
+ return { remaining, etaMs: Math.round(remaining / (steps / span)), confidence: 'ok' };
59
44
  }
60
45
 
61
- /** Title progress suffix: conservative `3/7`; trusted ETA gives `3/7 ~4m`; total=0 → empty; all complete → `5/5 ✓`. */
46
+ /** Title suffix: `3/7`; trusted ETA gives `3/7 ~4m`; total=0 → empty; all complete → `5/5 ✓`. */
62
47
  export function formatProgressSuffix(opts: {
63
48
  completed: number;
64
49
  total: number;
@@ -68,44 +53,22 @@ export function formatProgressSuffix(opts: {
68
53
  if (total <= 0) return '';
69
54
  const base = completed >= total ? `${completed}/${total} ✓` : `${completed}/${total}`;
70
55
  if (!eta || eta.etaMs <= 0) return base;
71
- const human = eta.etaMs < 60_000 ? '<1m'
72
- : (() => {
73
- const mins = Math.ceil(eta.etaMs / 60_000);
74
- return mins < 60 ? `~${mins}m` : `~${Math.round((mins / 60) * 10) / 10}h`;
75
- })();
56
+ const human = eta.etaMs < 60_000 ? '<1m' : (() => {
57
+ const mins = Math.ceil(eta.etaMs / 60_000);
58
+ return mins < 60 ? `~${mins}m` : `~${Math.round((mins / 60) * 10) / 10}h`;
59
+ })();
76
60
  return `${base} ${human}`;
77
61
  }
78
62
 
79
- /** Tool badge (report_agent.message): one tool name; multiple use the first plus a count; empty → null (do not overwrite). */
63
+ /** Tool badge for report_agent.message: one name, or the first plus a count; empty → null (do not overwrite). */
80
64
  export function planToolBadge(runningToolNames: readonly string[]): string | null {
81
65
  if (runningToolNames.length === 0) return null;
82
66
  const first = runningToolNames[0];
83
67
  return runningToolNames.length === 1 ? `🔧 ${first}` : `🔧 ${first} +${runningToolNames.length - 1}`;
84
68
  }
85
69
 
86
- /** Derive progress input from a list (completed count / total open-work count). */
70
+ /** Completed count over all tracked work (abandoned excluded). */
87
71
  export function progressOf(items: readonly TodoItem[]): { completed: number; total: number } {
88
72
  const c = countTodos(items);
89
- const blockedCount = items.filter((it) => it.status === 'blocked').length;
90
- return { completed: c.completed, total: c.completed + c.pending + c.inProgress + blockedCount };
91
- }
92
-
93
- /** Hide the progress badge after it has been marked for the full lifecycle (avoid persistent completed-state noise; callers compare this). */
94
- export const PROGRESS_HIDE_MS = 60_000;
95
-
96
- /**
97
- * Count completed transitions between two lists (match by content; an item completed in the new list whose old status
98
- * was not completed, including items newly added as completed). M16 ETA estimation uses this as input: every edit path
99
- * (todo_write / human edit / M17 reconciliation) passes through TodosService, so the diff is centralized here.
100
- */
101
- export function countCompletedTransitions(
102
- before: readonly TodoItem[],
103
- after: readonly TodoItem[],
104
- ): number {
105
- const oldStatus = new Map(before.map((it) => [it.content, it.status]));
106
- let n = 0;
107
- for (const it of after) {
108
- if (it.status === 'completed' && oldStatus.get(it.content) !== 'completed') n += 1;
109
- }
110
- return n;
73
+ return { completed: c.completed, total: c.completed + c.pending + c.inProgress + c.blocked };
111
74
  }
@@ -1,35 +1,31 @@
1
1
  /**
2
- * M17: pure planner for automatic todo↔subagent reconciliation (development plan §M17; P2 = do not check off low-confidence matches, only prompt).
2
+ * Pure planner for automatic todo↔subagent reconciliation, run after a settlement push and before
3
+ * the followUp is injected.
3
4
  *
4
- * Timing (adapter): after the controller receives a settlement push (pollLoop / pipe reply), before injecting followUp.
5
- * Semantics (aligned with omp reconcileTodosWithSubagents):
6
- * - Match description ↔ todo.content: normalize (lowercase + collapse whitespace), then exact → bidirectional prefix
7
- * → substring, in the same family as D38 fuzzyFind;
8
- * - Auto-complete only when settled and the unique best tier is in {exact, prefix}; at most one item per settlement;
9
- * - Unblock blocked items whose blocker ↔ description matches at any tier (exact/prefix)
10
- * (multiple allowed; failed/low-confidence matches do not unblock);
11
- * - Low confidence/ambiguity/failure: leave the list unchanged and add a noteLines prompt to the settlement notice (P2 decision);
12
- * - Persistence: edits use the D38 authoritative path (pi-herdr.todo-edit custom entry, effective during branch replay).
5
+ * Match description ↔ todo.content after normalize (lowercase + collapse whitespace) at tiers
6
+ * exact → prefix → substring, the same family as D38 fuzzyFind.
7
+ * - Auto-complete only a settled subagent's unique best candidate at exact/prefix, at most one item;
8
+ * - Unblock blocked items whose blocker matches at any tier (blockers are phrases like "waiting for
9
+ * X to finish", so substring matters and a soft return to pending is safe);
10
+ * - Low confidence / ambiguity / failure leaves the list untouched and adds a prompt line (never
11
+ * check off a guess);
12
+ * - Edits persist through the authoritative pi-herdr.todo-edit path, so branch replay reproduces them.
13
13
  */
14
14
 
15
15
  import { applyTodoEdits, type TodoEdit, type TodoItem } from './todo-core.ts';
16
16
 
17
- export type ReconcileOutcome = 'settled' | 'failed';
18
- export type MatchTier = 'exact' | 'prefix' | 'substring' | null;
19
- export interface ReconcilePlan {
20
- /** List after applying edits (original reference when there are no edits). */
17
+ type ReconcileOutcome = 'settled' | 'failed';
18
+ type MatchTier = 'exact' | 'prefix' | 'substring' | null;
19
+ interface ReconcilePlan {
21
20
  items: TodoItem[];
22
21
  /** Edits persisted through the authoritative path (at most one done plus any unblocks). */
23
22
  edits: TodoEdit[];
24
23
  /** Item automatically checked off (at most one). */
25
24
  completed: TodoItem | null;
26
- /** Best matching tier for auto-completion (null when there are no candidates). */
27
25
  tier: MatchTier;
28
- /** Items unblocked by a blocker match. */
29
26
  unblocked: TodoItem[];
30
- /** Prompt lines injected into the settlement notice (aligned with D36 feedback tone; empty when none). */
31
27
  noteLines: string[];
32
- /** Match-rate metric (P2: collected with the settlement notice in session JSONL). */
28
+ /** Match-rate metric, collected with the settlement notice in session JSONL. */
33
29
  metric: {
34
30
  description: string;
35
31
  outcome: ReconcileOutcome;
@@ -64,9 +60,9 @@ export function reconcileTodos(
64
60
  const edits: TodoEdit[] = [];
65
61
  const noteLines: string[] = [];
66
62
 
67
- // 1) Check-off candidates: pending / in_progress (completed/abandoned never participate).
68
- const matchable = prev.filter((t) => t.status === 'pending' || t.status === 'in_progress');
69
- const scored = matchable
63
+ // Check-off candidates: completed/abandoned never participate.
64
+ const scored = prev
65
+ .filter((t) => t.status === 'pending' || t.status === 'in_progress')
70
66
  .map((t) => ({ item: t, tier: matchTier(description, t.content) }))
71
67
  .filter((c): c is { item: TodoItem; tier: Exclude<MatchTier, null> } => c.tier !== null)
72
68
  .sort((a, b) => TIER_ORDER[a.tier] - TIER_ORDER[b.tier]);
@@ -80,35 +76,30 @@ export function reconcileTodos(
80
76
  edits.push({ op: 'done', content: completed.content });
81
77
  noteLines.push(`Reconciled: completed "${completed.content}" (${bestTier} match with subagent description).`);
82
78
  } else if (best.length > 1) {
83
- // Ambiguous match: list candidates and leave the decision to a person.
84
79
  noteLines.push(
85
80
  `Todo match ambiguous between: ${best.map((c) => `"${c.item.content}"`).join(', ')} — update todo_write yourself.`,
86
81
  );
87
- } else if (outcome === 'failed') {
88
- // High-confidence candidate, but the subagent did not settle successfully: keep it open and prompt.
82
+ } else {
83
+ // High-confidence candidate, but the subagent did not settle successfully: keep it open.
89
84
  for (const c of best) {
90
85
  noteLines.push(`Todo kept open: "${c.item.content}" (subagent did not settle successfully).`);
91
86
  }
92
87
  }
93
88
  } else if (bestTier === 'substring') {
94
- // Low-confidence match (P2): do not check it off; prompt with the candidate.
95
89
  for (const c of best) {
96
90
  noteLines.push(`Todo not auto-completed (low-confidence match): "${c.item.content}" — update todo_write if this work is done.`);
97
91
  }
98
92
  }
99
93
 
100
- // 2) Unblock on any blocker match, including substring—real blockers are phrases such as "waiting for X to finish",
101
- // so the description may be embedded in the middle and prefix would miss it. The low-confidence gate applies only
102
- // to check-offs; returning an unblocked item to pending is a soft operation. Only settled subagents can unblock (possibly multiple).
94
+ // Unblocking is a soft operation, so any tier counts; only a settled subagent can unblock.
103
95
  const unblocked: TodoItem[] = [];
104
96
  if (outcome === 'settled') {
105
97
  for (const t of prev) {
106
98
  if (t.status !== 'blocked' || typeof t.blocker !== 'string') continue;
107
- if (matchTier(description, t.blocker) !== null) {
108
- unblocked.push(t);
109
- edits.push({ op: 'unblock', content: t.content });
110
- noteLines.push(`Unblocked "${t.content}" (was waiting on: ${t.blocker}).`);
111
- }
99
+ if (matchTier(description, t.blocker) === null) continue;
100
+ unblocked.push(t);
101
+ edits.push({ op: 'unblock', content: t.content });
102
+ noteLines.push(`Unblocked "${t.content}" (was waiting on: ${t.blocker}).`);
112
103
  }
113
104
  }
114
105