@otea/cli 0.0.1 → 0.1.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 (243) hide show
  1. package/LICENSE +661 -0
  2. package/README.es.md +151 -0
  3. package/README.md +251 -3
  4. package/THIRD_PARTY_NOTICES.md +114 -0
  5. package/bin/otea.mjs +28 -0
  6. package/dist/app/agent-detail.js +26 -0
  7. package/dist/app/agent-draft.js +429 -0
  8. package/dist/app/agent-prompts.js +52 -0
  9. package/dist/app/agents.js +77 -0
  10. package/dist/app/asking.js +43 -0
  11. package/dist/app/board-archive.js +243 -0
  12. package/dist/app/board-migration.js +272 -0
  13. package/dist/app/boards.js +870 -0
  14. package/dist/app/branch-owner.js +48 -0
  15. package/dist/app/brief.js +53 -0
  16. package/dist/app/choices.js +61 -0
  17. package/dist/app/collections.js +101 -0
  18. package/dist/app/context.js +1 -0
  19. package/dist/app/crop.js +56 -0
  20. package/dist/app/devices.js +62 -0
  21. package/dist/app/draft-request.js +82 -0
  22. package/dist/app/events.js +12 -0
  23. package/dist/app/evidence.js +287 -0
  24. package/dist/app/gates.js +610 -0
  25. package/dist/app/guide.js +37 -0
  26. package/dist/app/home.js +145 -0
  27. package/dist/app/init-boards.js +36 -0
  28. package/dist/app/intake.js +25 -0
  29. package/dist/app/item-search.js +141 -0
  30. package/dist/app/items.js +318 -0
  31. package/dist/app/keep-waiting.js +44 -0
  32. package/dist/app/land-order.js +107 -0
  33. package/dist/app/language.js +52 -0
  34. package/dist/app/launcher.js +56 -0
  35. package/dist/app/lease-take.js +54 -0
  36. package/dist/app/leases.js +86 -0
  37. package/dist/app/machine.js +39 -0
  38. package/dist/app/merged.js +89 -0
  39. package/dist/app/migrate.js +171 -0
  40. package/dist/app/next.js +1637 -0
  41. package/dist/app/notes.js +319 -0
  42. package/dist/app/notify-targets.js +113 -0
  43. package/dist/app/opened.js +25 -0
  44. package/dist/app/owner-only.js +34 -0
  45. package/dist/app/performance.js +28 -0
  46. package/dist/app/phone.js +97 -0
  47. package/dist/app/presence.js +596 -0
  48. package/dist/app/preset-views.js +25 -0
  49. package/dist/app/presets.js +129 -0
  50. package/dist/app/priority.js +156 -0
  51. package/dist/app/profile.js +86 -0
  52. package/dist/app/project.js +247 -0
  53. package/dist/app/proofs.js +179 -0
  54. package/dist/app/questions.js +215 -0
  55. package/dist/app/reader-filter.js +58 -0
  56. package/dist/app/relations.js +164 -0
  57. package/dist/app/relay.js +98 -0
  58. package/dist/app/repos.js +176 -0
  59. package/dist/app/restart.js +92 -0
  60. package/dist/app/set-aside.js +64 -0
  61. package/dist/app/system-locale.js +6 -0
  62. package/dist/app/tailscale-cli.js +77 -0
  63. package/dist/app/tailscale-testing.js +41 -0
  64. package/dist/app/tailscale.js +177 -0
  65. package/dist/app/tips.js +30 -0
  66. package/dist/app/topics-everywhere.js +53 -0
  67. package/dist/app/topics.js +453 -0
  68. package/dist/app/undo.js +52 -0
  69. package/dist/app/validate.js +131 -0
  70. package/dist/app/watch.js +48 -0
  71. package/dist/cli/command.js +44 -0
  72. package/dist/cli/commands.js +3303 -0
  73. package/dist/cli/exit-codes.js +70 -0
  74. package/dist/cli/forge.js +176 -0
  75. package/dist/cli/format.js +340 -0
  76. package/dist/cli/gh.js +17 -0
  77. package/dist/cli/gitrepo.js +489 -0
  78. package/dist/cli/help.js +101 -0
  79. package/dist/cli/hooks.js +232 -0
  80. package/dist/cli/main.js +37 -0
  81. package/dist/cli/merged-outside.js +58 -0
  82. package/dist/cli/names.js +154 -0
  83. package/dist/cli/opener.js +21 -0
  84. package/dist/cli/owner.js +154 -0
  85. package/dist/cli/phone.js +221 -0
  86. package/dist/cli/ports.js +24 -0
  87. package/dist/cli/probe.js +18 -0
  88. package/dist/cli/run.js +246 -0
  89. package/dist/cli/serve-file.js +21 -0
  90. package/dist/cli/stats.js +55 -0
  91. package/dist/cli/version.js +3 -0
  92. package/dist/core/agent-guide.js +31 -0
  93. package/dist/core/agent-prompts.js +139 -0
  94. package/dist/core/authority.js +94 -0
  95. package/dist/core/board/analysis.js +86 -0
  96. package/dist/core/board/choices.js +100 -0
  97. package/dist/core/board/compile.js +362 -0
  98. package/dist/core/board/engine.js +523 -0
  99. package/dist/core/board/hints.js +46 -0
  100. package/dist/core/board/history.js +146 -0
  101. package/dist/core/board/ids.js +87 -0
  102. package/dist/core/board/joins.js +30 -0
  103. package/dist/core/board/mentions.js +68 -0
  104. package/dist/core/board/merged.js +18 -0
  105. package/dist/core/board/model.js +135 -0
  106. package/dist/core/board/next.js +1116 -0
  107. package/dist/core/board/notes.js +298 -0
  108. package/dist/core/board/performance.js +218 -0
  109. package/dist/core/board/plan.js +299 -0
  110. package/dist/core/board/problems.js +232 -0
  111. package/dist/core/board/proofs.js +84 -0
  112. package/dist/core/board/questions.js +114 -0
  113. package/dist/core/board/reading.js +220 -0
  114. package/dist/core/board/readings.js +534 -0
  115. package/dist/core/board/schema.js +335 -0
  116. package/dist/core/board/shape-words.js +179 -0
  117. package/dist/core/board/template-words.js +23 -0
  118. package/dist/core/board/templates-catalogue.js +965 -0
  119. package/dist/core/board/templates-coverage.js +261 -0
  120. package/dist/core/board/templates-flows.js +779 -0
  121. package/dist/core/board/templates.js +586 -0
  122. package/dist/core/brief.js +93 -0
  123. package/dist/core/diagram.js +80 -0
  124. package/dist/core/errors.js +12 -0
  125. package/dist/core/evidence.js +33 -0
  126. package/dist/core/fields.js +129 -0
  127. package/dist/core/hierarchy.js +30 -0
  128. package/dist/core/icons.js +181 -0
  129. package/dist/core/image-crop.js +166 -0
  130. package/dist/core/item.js +49 -0
  131. package/dist/core/lease.js +31 -0
  132. package/dist/core/modules.js +318 -0
  133. package/dist/core/notify/planner.js +117 -0
  134. package/dist/core/notify/signals.js +66 -0
  135. package/dist/core/notify/text.js +60 -0
  136. package/dist/core/path-key.js +22 -0
  137. package/dist/core/placement.js +161 -0
  138. package/dist/core/preset-labels.js +66 -0
  139. package/dist/core/preset-notes.js +109 -0
  140. package/dist/core/preset.js +541 -0
  141. package/dist/core/profile.js +55 -0
  142. package/dist/core/project-id.js +20 -0
  143. package/dist/core/refusal.js +1 -0
  144. package/dist/core/relations.js +208 -0
  145. package/dist/core/repo.js +54 -0
  146. package/dist/core/samples.js +161 -0
  147. package/dist/core/search-words.js +68 -0
  148. package/dist/core/test-report.js +120 -0
  149. package/dist/core/time.js +8 -0
  150. package/dist/core/validation.js +60 -0
  151. package/dist/core/views.js +171 -0
  152. package/dist/core/workflow.js +51 -0
  153. package/dist/mcp/commands.js +101 -0
  154. package/dist/mcp/server.js +239 -0
  155. package/dist/mcp/words.js +95 -0
  156. package/dist/server/boards.js +232 -0
  157. package/dist/server/changes.js +42 -0
  158. package/dist/server/diff-tip.js +83 -0
  159. package/dist/server/drift.js +87 -0
  160. package/dist/server/errors.js +87 -0
  161. package/dist/server/evidence.js +82 -0
  162. package/dist/server/hosts.js +6 -0
  163. package/dist/server/hub.js +98 -0
  164. package/dist/server/lan-access.js +62 -0
  165. package/dist/server/lan.js +164 -0
  166. package/dist/server/listen.js +70 -0
  167. package/dist/server/notifier.js +156 -0
  168. package/dist/server/notify-api.js +151 -0
  169. package/dist/server/pairing.js +124 -0
  170. package/dist/server/projects.js +191 -0
  171. package/dist/server/push.js +199 -0
  172. package/dist/server/server.js +960 -0
  173. package/dist/server/session.js +43 -0
  174. package/dist/server/stream.js +36 -0
  175. package/dist/server/test-push.js +38 -0
  176. package/dist/server/token.js +37 -0
  177. package/dist/server/transports.js +54 -0
  178. package/dist/server/ui.js +44 -0
  179. package/dist/server/writes.js +361 -0
  180. package/dist/server/wrong-address.js +36 -0
  181. package/dist/store/boards.js +111 -0
  182. package/dist/store/database.js +45 -0
  183. package/dist/store/machine.js +77 -0
  184. package/dist/store/migrations.js +493 -0
  185. package/dist/store/queries.js +585 -0
  186. package/dist/store/registry.js +72 -0
  187. package/package.json +72 -4
  188. package/presets/architecture/playbook.md +69 -0
  189. package/presets/architecture/preset.yaml +80 -0
  190. package/presets/architecture/samples.yaml +23 -0
  191. package/presets/bug-triage/playbook.md +129 -0
  192. package/presets/bug-triage/preset.yaml +127 -0
  193. package/presets/bug-triage/samples/wall-slide-after.png +0 -0
  194. package/presets/bug-triage/samples/wall-slide-before.png +0 -0
  195. package/presets/bug-triage/samples.yaml +72 -0
  196. package/presets/change/playbook.md +115 -0
  197. package/presets/change/preset.yaml +141 -0
  198. package/presets/change/samples.yaml +62 -0
  199. package/presets/review/playbook.md +51 -0
  200. package/presets/review/preset.yaml +62 -0
  201. package/presets/review/samples.yaml +27 -0
  202. package/presets/tasks/playbook.md +78 -0
  203. package/presets/tasks/preset.yaml +61 -0
  204. package/presets/tasks/samples.yaml +46 -0
  205. package/schemas/board.schema.json +764 -0
  206. package/ui/dist/THIRD_PARTY_NOTICES.md +114 -0
  207. package/ui/dist/assets/dm-mono-latin-400-normal--0xN8mdc.woff +0 -0
  208. package/ui/dist/assets/dm-mono-latin-400-normal-4GdczIuU.woff2 +0 -0
  209. package/ui/dist/assets/dm-mono-latin-500-normal-CN8Miw6E.woff +0 -0
  210. package/ui/dist/assets/dm-mono-latin-500-normal-DRMDZjhP.woff2 +0 -0
  211. package/ui/dist/assets/dm-mono-latin-ext-400-normal-1aZr6b2b.woff +0 -0
  212. package/ui/dist/assets/dm-mono-latin-ext-400-normal-C2zvOubV.woff2 +0 -0
  213. package/ui/dist/assets/dm-mono-latin-ext-500-normal-BtRyHRi6.woff2 +0 -0
  214. package/ui/dist/assets/dm-mono-latin-ext-500-normal-Dw3M13d8.woff +0 -0
  215. package/ui/dist/assets/index-C3OzJope.js +103 -0
  216. package/ui/dist/assets/index-COdEjckQ.css +1 -0
  217. package/ui/dist/assets/newsreader-latin-ext-opsz-italic-CSPGnFdO.woff2 +0 -0
  218. package/ui/dist/assets/newsreader-latin-ext-opsz-normal-BQn1nviT.woff2 +0 -0
  219. package/ui/dist/assets/newsreader-latin-opsz-italic-kJKFiXvB.woff2 +0 -0
  220. package/ui/dist/assets/newsreader-latin-opsz-normal-s-izfB6B.woff2 +0 -0
  221. package/ui/dist/assets/newsreader-vietnamese-opsz-italic-DTr-ymY4.woff2 +0 -0
  222. package/ui/dist/assets/newsreader-vietnamese-opsz-normal-BjhtXyW2.woff2 +0 -0
  223. package/ui/dist/assets/pdf-YwugTw7Y.js +54 -0
  224. package/ui/dist/assets/pdf-engine-CkBopwck.js +1 -0
  225. package/ui/dist/assets/pdf-rELvHkft.js +56 -0
  226. package/ui/dist/assets/pdf.worker.min-B5Yc7dP4.mjs +27 -0
  227. package/ui/dist/assets/pdf.worker.min-B7U8ptvh.js +1 -0
  228. package/ui/dist/assets/pdf.worker.min-CIqJB0yW.mjs +28 -0
  229. package/ui/dist/assets/pdf.worker.min-UqOn5EDC.js +1 -0
  230. package/ui/dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
  231. package/ui/dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
  232. package/ui/dist/icons/apple-touch-icon.png +0 -0
  233. package/ui/dist/icons/otea-192.png +0 -0
  234. package/ui/dist/icons/otea-512.png +0 -0
  235. package/ui/dist/icons/otea-badge.png +0 -0
  236. package/ui/dist/icons/otea-maskable-512.png +0 -0
  237. package/ui/dist/index.html +25 -0
  238. package/ui/dist/manifest.webmanifest +24 -0
  239. package/ui/dist/otea-idle.svg +5 -0
  240. package/ui/dist/otea-working.svg +5 -0
  241. package/ui/dist/otea-yours-working.svg +5 -0
  242. package/ui/dist/otea-yours.svg +5 -0
  243. package/ui/dist/sw.js +1 -0
@@ -0,0 +1,298 @@
1
+ import { timeText } from '../time.js';
2
+ // Notes from the person while an agent does a step (in-flight notes): the person leaves one without
3
+ // moving the item (no new step, round or lease), optionally on a piece of evidence already attached
4
+ // in the round; the agent reads it in the answers it already asks for (heartbeat, show, next,
5
+ // attach) and answers it with otea reply. A handoff waits until every note of the round is
6
+ // answered. The thread is the item's note_left and note_answered events; this module reads it and
7
+ // writes what agents read of it, in English, as the rest of what agents read (core/board/next.ts).
8
+ export const NOTE_LEFT = 'note_left';
9
+ export const NOTE_ANSWERED = 'note_answered';
10
+ // A review comment the person changed or took back while the review was still open (P5-3).
11
+ export const NOTE_EDITED = 'note_edited';
12
+ export const NOTE_WITHDRAWN = 'note_withdrawn';
13
+ function anchorOf(value) {
14
+ if (!value || typeof value !== 'object')
15
+ return undefined;
16
+ const raw = value;
17
+ if (typeof raw.evidence !== 'number' || typeof raw.filename !== 'string')
18
+ return undefined;
19
+ return {
20
+ evidence: raw.evidence,
21
+ filename: raw.filename,
22
+ ...(typeof raw.part === 'string' ? { part: raw.part } : {}),
23
+ ...(typeof raw.partName === 'string' ? { partName: raw.partName } : {}),
24
+ ...(typeof raw.seconds === 'number' ? { seconds: raw.seconds } : {}),
25
+ ...(typeof raw.line === 'number' ? { line: raw.line } : {}),
26
+ ...(typeof raw.quote === 'string' ? { quote: raw.quote } : {}),
27
+ ...(raw.whole === true ? { whole: true } : {}),
28
+ ...(typeof raw.node === 'string' ? { node: raw.node } : {}),
29
+ ...(typeof raw.page === 'number' ? { page: raw.page } : {}),
30
+ ...(typeof raw.x === 'number' ? { x: raw.x } : {}),
31
+ ...(typeof raw.y === 'number' ? { y: raw.y } : {}),
32
+ ...(typeof raw.w === 'number' ? { w: raw.w } : {}),
33
+ ...(typeof raw.h === 'number' ? { h: raw.h } : {}),
34
+ };
35
+ }
36
+ function reviewOf(value) {
37
+ const step = value?.for?.step;
38
+ const round = value?.for?.round;
39
+ return typeof step === 'string' && typeof round === 'number'
40
+ ? { review: { for: { step, round } } }
41
+ : {};
42
+ }
43
+ // The notes of a step's round: those left in it and the review comments sent back into it.
44
+ export function notesOfRound(thread, step, round) {
45
+ return thread.filter((note) => (note.step === step && note.round === round) ||
46
+ (note.review !== undefined &&
47
+ note.review.for.step === step &&
48
+ note.review.for.round === round));
49
+ }
50
+ function deliveredOf(value) {
51
+ const raw = (value ?? {});
52
+ return {
53
+ done: typeof raw.done === 'number' ? raw.done : 0,
54
+ of: typeof raw.of === 'number' ? raw.of : 0,
55
+ };
56
+ }
57
+ // The item's notes, oldest first, each with its answer once there is one.
58
+ export function notesOf(events) {
59
+ const notes = new Map();
60
+ for (const { type, actor, at, data, profile } of events) {
61
+ if (typeof data.note !== 'number')
62
+ continue;
63
+ if (type === NOTE_LEFT) {
64
+ const anchor = anchorOf(data.anchor);
65
+ notes.set(data.note, {
66
+ note: data.note,
67
+ text: String(data.text ?? ''),
68
+ at,
69
+ by: actor.name,
70
+ step: String(data.step ?? ''),
71
+ stepName: String(data.stepName ?? data.step ?? ''),
72
+ round: typeof data.round === 'number' ? data.round : 1,
73
+ delivered: deliveredOf(data.delivered),
74
+ ...(typeof data.to === 'string' ? { to: data.to } : {}),
75
+ ...(anchor ? { anchor } : {}),
76
+ ...reviewOf(data.review),
77
+ });
78
+ }
79
+ else if (type === NOTE_EDITED) {
80
+ const left = notes.get(data.note);
81
+ if (left && !left.answer)
82
+ left.text = String(data.text ?? left.text);
83
+ }
84
+ else if (type === NOTE_WITHDRAWN) {
85
+ notes.delete(data.note);
86
+ }
87
+ else if (type === NOTE_ANSWERED) {
88
+ const left = notes.get(data.note);
89
+ if (!left || left.answer)
90
+ continue;
91
+ left.answer = {
92
+ text: String(data.text ?? ''),
93
+ at,
94
+ by: actor.name,
95
+ ...(profile
96
+ ? { profile: { harness: profile.harness, model: profile.model, effort: profile.effort } }
97
+ : {}),
98
+ };
99
+ }
100
+ }
101
+ return [...notes.values()];
102
+ }
103
+ // The numbers of the notes still unanswered.
104
+ export function unansweredOf(notes) {
105
+ return notes.filter(({ answer }) => !answer).map(({ note }) => note);
106
+ }
107
+ function and(words) {
108
+ if (words.length < 2)
109
+ return words.join('');
110
+ return `${words.slice(0, -1).join(', ')} and ${words.at(-1)}`;
111
+ }
112
+ function numbered(ns) {
113
+ return ns.length === 1 ? `note ${ns[0]}` : `notes ${and(ns.map(String))}`;
114
+ }
115
+ const ANSWER = '"<your short answer>"';
116
+ function replyCommand(id, n) {
117
+ return `otea reply ${id} --note ${n} ${ANSWER}`;
118
+ }
119
+ // The note as quoted: its first line opens the quote, later lines indented under it.
120
+ function quoted(text, indent) {
121
+ const [first = '', ...rest] = text.trim().split(/\r?\n/);
122
+ const lines = [`"${first}`, ...rest.map((line) => `${indent}${line}`)];
123
+ lines[lines.length - 1] += '"';
124
+ return lines;
125
+ }
126
+ // An answered note is said in one line, cut here.
127
+ const BRIEF = 60;
128
+ function brief(text) {
129
+ const first = text.trim().split(/\r?\n/)[0] ?? '';
130
+ return first.length <= BRIEF ? first : `${first.slice(0, BRIEF - 1).trimEnd()}…`;
131
+ }
132
+ // A moment in a recording as m:ss, "1:02", with its tenth of a second when it has one, "1:02.4":
133
+ // in a clip of a few seconds, whole seconds would say little.
134
+ export function moment(seconds) {
135
+ const tenths = Math.max(0, Math.round(seconds * 10));
136
+ const whole = Math.floor(tenths / 10);
137
+ const tenth = tenths % 10 ? `.${tenths % 10}` : '';
138
+ return `${Math.floor(whole / 60)}:${String(whole % 60).padStart(2, '0')}${tenth}`;
139
+ }
140
+ // "on pausa.gif at 0:02", or "on ui/PauseMenu.tsx (Diff)" with the part's name.
141
+ function onText(anchor, withPart, crop) {
142
+ const part = withPart && anchor.partName ? ` (${anchor.partName})` : '';
143
+ const at = anchor.seconds === undefined ? '' : ` at ${moment(anchor.seconds)}`;
144
+ const quote = anchor.quote === undefined ? '' : `"${anchor.quote}"`;
145
+ const line = anchor.line !== undefined
146
+ ? ` at line ${anchor.line}${quote ? `: ${quote}` : ''}`
147
+ : quote
148
+ ? ` at the line ${quote}`
149
+ : '';
150
+ const node = anchor.node === undefined ? '' : ` at node "${anchor.node}"`;
151
+ // A PDF's zone is read on its page; otea crop cuts images only.
152
+ const page = anchor.page === undefined ? '' : ` on page ${anchor.page}`;
153
+ const spot = spotText(anchor, anchor.page === undefined ? crop : undefined);
154
+ return `on ${anchor.filename}${part}${at}${line}${node}${page}${spot}`;
155
+ }
156
+ const pct = (n) => `${Math.round(n * 10) / 10}`;
157
+ // " at the point x 34.2%, y 61.8% (from the top left)", or a zone with the command that cuts it out.
158
+ function spotText(anchor, crop) {
159
+ const { x, y, w, h } = anchor;
160
+ if (x === undefined || y === undefined)
161
+ return '';
162
+ if (w === undefined || h === undefined)
163
+ return ` at the point x ${pct(x)}%, y ${pct(y)}% (from the top left)`;
164
+ const cut = crop ? `; ${crop} cuts it out` : '';
165
+ return ` at the zone x ${pct(x)}–${pct(x + w)}%, y ${pct(y)}–${pct(y + h)}% (from the top left${cut})`;
166
+ }
167
+ // What was in when the note was left: where, when, and how many deliverables.
168
+ function contextText(note) {
169
+ const said = [
170
+ ...(note.anchor ? [onText(note.anchor, true)] : []),
171
+ timeText(note.at),
172
+ ...(note.delivered.of > 0
173
+ ? [`when you had ${note.delivered.done} of ${note.delivered.of} handoff deliverables`]
174
+ : []),
175
+ ];
176
+ return said.join(', ');
177
+ }
178
+ // At most this many answered notes are listed in next and show; the rest are counted.
179
+ const ANSWERED_SHOWN = 3;
180
+ // next's and show's block, when the round has notes: every unanswered note whole, with the command
181
+ // that answers it, and the newest answered ones in a line each; newest first.
182
+ export function notesBlock(id, notes, me) {
183
+ if (notes.length === 0)
184
+ return [];
185
+ const open = unansweredOf(notes).length;
186
+ const head = `Notes from the person: ${notes.length}, ${open === 0 ? 'all answered' : `${open} unanswered`}`;
187
+ const lines = [head];
188
+ const newest = [...notes].reverse();
189
+ let answeredShown = 0;
190
+ for (const note of newest) {
191
+ if (note.answer) {
192
+ answeredShown += 1;
193
+ if (answeredShown > ANSWERED_SHOWN)
194
+ continue;
195
+ const who = note.answer.by === me ? 'you' : note.answer.by;
196
+ const on = note.anchor
197
+ ? `${onText(note.anchor, false, `otea crop ${id} --note ${note.note}`)} · `
198
+ : '';
199
+ lines.push(` ${note.note} · "${brief(note.text)}"`, ` ${on}${who} answered ${timeText(note.answer.at)}`);
200
+ continue;
201
+ }
202
+ const on = note.anchor
203
+ ? `${onText(note.anchor, false, `otea crop ${id} --note ${note.note}`)} · `
204
+ : '';
205
+ const [first = '', ...rest] = quoted(note.text, ' ');
206
+ lines.push(` ${note.note} · ${first}`, ...rest, ` ${on}unanswered: ${replyCommand(id, note.note)}`);
207
+ }
208
+ const hidden = answeredShown - ANSWERED_SHOWN;
209
+ if (hidden > 0)
210
+ lines.push(` and ${hidden} earlier, answered`);
211
+ return lines;
212
+ }
213
+ // What a heartbeat or an attach adds while notes wait for an answer: each with the moment it was
214
+ // left in, and the command that answers it. Empty when none waits.
215
+ export function newNotesText(id, notes) {
216
+ const open = notes.filter(({ answer }) => !answer);
217
+ const keep = 'the step, round and lease stay as they are:';
218
+ if (open.length === 0)
219
+ return '';
220
+ if (open.length === 1) {
221
+ const [note] = open;
222
+ return [
223
+ `New note from the person (note ${note.note}, unanswered):`,
224
+ ...quoted(note.text, ' ').map((line, index) => (index === 0 ? ` ${line}` : line)),
225
+ ` ${contextText(note)}`,
226
+ `Answer it before you hand off; ${keep}`,
227
+ ` ${replyCommand(id, note.note)}`,
228
+ ].join('\n');
229
+ }
230
+ return [
231
+ `New notes from the person (${open.length} unanswered):`,
232
+ ...open.flatMap((note) => [
233
+ ...quoted(note.text, ' ').map((line, index) => index === 0 ? ` Note ${note.note}: ${line}` : line),
234
+ ` ${contextText(note)}`,
235
+ ]),
236
+ `Answer each before you hand off; ${keep}`,
237
+ ...open.map((note) => ` ${replyCommand(id, note.note)}`),
238
+ ].join('\n');
239
+ }
240
+ // The Handoff checklist's line for the notes of the round, when it has any.
241
+ export function notesHandLine(notes) {
242
+ if (notes.length === 0)
243
+ return [];
244
+ const open = unansweredOf(notes);
245
+ const line = ' Notes from the person answered: required';
246
+ if (open.length === 0)
247
+ return [` ✓${line.slice(1)}`];
248
+ const verb = open.length === 1 ? 'is' : 'are';
249
+ return [` ✗${line.slice(1)} (${numbered(open)} ${verb} not)`];
250
+ }
251
+ // The refusal of a handoff while notes wait for an answer: which, and the command for the first.
252
+ export function notesRefusal(id, open) {
253
+ if (open.length === 0)
254
+ return [];
255
+ const [first = 0, ...rest] = open;
256
+ if (rest.length === 0)
257
+ return [
258
+ `Not handed off ${id}: note ${first} from the person is unanswered.`,
259
+ `Answer it first: ${replyCommand(id, first)}`,
260
+ ];
261
+ return [
262
+ `Not handed off ${id}: ${numbered(open)} from the person are unanswered.`,
263
+ `Answer each first: ${replyCommand(id, first)} (then ${rest.map((n) => `--note ${n}`).join(', ')})`,
264
+ ];
265
+ }
266
+ // The refusal of a set-aside for later while notes wait for an answer (P7-R3-8): the person may
267
+ // have asked for the item now; the first note in brief, and the command that answers it.
268
+ export function laterNotesRefusal(id, open) {
269
+ const [first, ...rest] = open;
270
+ if (!first)
271
+ return '';
272
+ const which = rest.length === 0
273
+ ? `note ${first.note} from the person is unanswered`
274
+ : `${numbered(open.map(({ note }) => note))} from the person are unanswered`;
275
+ const each = rest.length === 0 ? 'it' : 'each';
276
+ return `Not set aside ${id}: ${which} ("${brief(first.text)}"). Answer ${each} first: ${replyCommand(id, first.note)}`;
277
+ }
278
+ // The line after otea reply: what is still unanswered, or that the item may be handed off.
279
+ export function repliedText(id, n, open) {
280
+ const head = `Answered note ${n} on ${id}.`;
281
+ if (open.length === 0)
282
+ return `${head} Nothing left unanswered: you may hand it off.`;
283
+ const [first = 0] = open;
284
+ return `${head} Still unanswered: ${numbered(open)} (${replyCommand(id, first)}).`;
285
+ }
286
+ // What otea wait says when a note wakes it: each note in the person's words too, as an agent woken
287
+ // by a number alone acted without reading it (P7-R3-8).
288
+ export function waitNoteText(id, open) {
289
+ const one = open.length === 1;
290
+ const ns = open.map(({ note }) => note);
291
+ return [
292
+ `${id}: new ${one ? 'note' : 'notes'} from the person (${numbered(ns)}). Read ${one ? 'it' : 'them'}: otea show ${id}`,
293
+ ...open.flatMap(({ note, text }) => {
294
+ const [first = '', ...rest] = quoted(text, ' ');
295
+ return [` ${note} · ${first}`, ...rest];
296
+ }),
297
+ ].join('\n');
298
+ }
@@ -0,0 +1,218 @@
1
+ import { isUnknown } from '../profile.js';
2
+ import { isClosed, positionOf, roundsOf } from './engine.js';
3
+ import { withoutUndone } from './history.js';
4
+ import { DONE, DROPPED, stepById, stepIndex } from './model.js';
5
+ // The board's own moves (a publish, a restore, the reading of a preset): not the work.
6
+ const BOARD_MOVES = new Set(['publish', 'restore', 'migrate']);
7
+ const words = (value) => typeof value === 'string' && value.trim() !== '';
8
+ const stepNameOf = (def, step) => stepById(def, step)?.name ?? step;
9
+ // Where a move from `from` to `to` takes the step's work: on (a review of it, a later step, Hecho),
10
+ // back (to this step from its review, or to an earlier one), or neither.
11
+ function isForward(def, from, to) {
12
+ const target = positionOf(to);
13
+ if (isClosed(target))
14
+ return target.closed === DONE;
15
+ if (target.step === from)
16
+ return target.phase !== 'doing';
17
+ return stepIndex(def, target.step) > stepIndex(def, from);
18
+ }
19
+ function factsOf(trail) {
20
+ const { def, item, board } = trail;
21
+ const live = withoutUndone(trail.events);
22
+ const moves = live.filter(({ type, data }) => type === 'transitioned' && words(data.from) && words(data.to));
23
+ const cut = moves.findLastIndex(({ data }) => data.reason === 'migrate');
24
+ const reading = moves[cut];
25
+ const created = live.find(({ type }) => type === 'created');
26
+ const initial = reading
27
+ ? String(reading.data.to)
28
+ : words(created?.data.state)
29
+ ? created.data.state
30
+ : trail.start.initial;
31
+ const createdAt = reading ? reading.at : trail.start.createdAt;
32
+ const after = reading?.id ?? Number.NEGATIVE_INFINITY;
33
+ const claims = live.filter(({ type, id }) => type === 'claimed' && id > after);
34
+ const out = { handoffs: [], finished: [], discards: [], done: [] };
35
+ const transitions = [];
36
+ // The last handoff of each step that may still come back, and the last of each step and of the
37
+ // item (undefined when the person, or an agent with no profile, did it).
38
+ const pending = new Map();
39
+ const lastOf = new Map();
40
+ const leftAt = new Map();
41
+ let last;
42
+ let previousAt = Number.NEGATIVE_INFINITY;
43
+ for (const move of moves.slice(cut + 1)) {
44
+ const from = String(move.data.from);
45
+ const to = String(move.data.to);
46
+ const quiet = BOARD_MOVES.has(String(move.data.reason));
47
+ transitions.push({ from, to, at: move.at, ...(quiet ? { quiet: true } : {}) });
48
+ const source = positionOf(from);
49
+ const target = positionOf(to);
50
+ if (quiet || isClosed(source)) {
51
+ previousAt = move.id;
52
+ continue;
53
+ }
54
+ // A send-back: into a step's doing from its review or a later step (the engine's dips).
55
+ if (!isClosed(target) && target.phase === 'doing') {
56
+ const fromIndex = stepIndex(def, source.step);
57
+ const toIndex = stepIndex(def, target.step);
58
+ const back = source.phase !== 'doing' ? toIndex <= fromIndex : toIndex < fromIndex;
59
+ // A way back an agent takes itself while it does a later step (a conflict once main moved,
60
+ // P3C-R3-6) found no fault in the handoff: only a review's send-back, or the person's, counts.
61
+ const sent = source.phase !== 'doing' || move.actor.kind === 'human';
62
+ const returned = back ? pending.get(target.step) : undefined;
63
+ if (returned) {
64
+ if (sent)
65
+ returned.sentBack = true;
66
+ pending.delete(target.step);
67
+ }
68
+ }
69
+ if (source.phase === 'doing' && isForward(def, source.step, to)) {
70
+ const handoff = handoffOf(move, source.step);
71
+ if (handoff)
72
+ out.handoffs.push(handoff);
73
+ if (handoff)
74
+ pending.set(source.step, handoff);
75
+ else
76
+ pending.delete(source.step);
77
+ lastOf.set(source.step, handoff);
78
+ last = handoff;
79
+ }
80
+ if (isForward(def, source.step, to) && (isClosed(target) || target.step !== source.step)) {
81
+ leftAt.set(source.step, move.at);
82
+ }
83
+ if (isClosed(target)) {
84
+ if (target.closed === DROPPED && last)
85
+ out.discards.push({ by: last, at: move.at });
86
+ if (target.closed === DONE)
87
+ out.done.push({ item, by: [...out.handoffs], at: move.at });
88
+ }
89
+ previousAt = move.id;
90
+ }
91
+ // Steps the item went on past, as it stands now: each one's last handoff, and its rounds.
92
+ const now = transitions.at(-1)?.to ?? initial;
93
+ const position = positionOf(now);
94
+ const { rounds } = roundsOf(def, initial, createdAt, transitions);
95
+ for (const [step, by] of lastOf) {
96
+ if (!by)
97
+ continue;
98
+ const past = isClosed(position)
99
+ ? position.closed === DONE
100
+ : stepIndex(def, position.step) > stepIndex(def, step);
101
+ const at = leftAt.get(step);
102
+ if (past && at)
103
+ out.finished.push({ by, rounds: rounds[step] ?? 1, at });
104
+ }
105
+ return out;
106
+ function handoffOf(move, step) {
107
+ if (move.actor.kind !== 'agent' || !move.profile)
108
+ return undefined;
109
+ const claim = claims.findLast(({ id, actor }) => id < move.id && id > previousAt && actor.name === move.actor.name);
110
+ return {
111
+ item,
112
+ board,
113
+ step,
114
+ stepName: stepNameOf(def, step),
115
+ profile: move.profile,
116
+ at: move.at,
117
+ claimMs: claim ? Date.parse(move.at) - Date.parse(claim.at) : null,
118
+ sentBack: false,
119
+ };
120
+ }
121
+ }
122
+ function median(values) {
123
+ if (values.length === 0)
124
+ return null;
125
+ const sorted = [...values].sort((a, b) => a - b);
126
+ const middle = Math.floor(sorted.length / 2);
127
+ const high = sorted[middle] ?? 0;
128
+ return sorted.length % 2 === 1 ? high : ((sorted[middle - 1] ?? 0) + high) / 2;
129
+ }
130
+ // The numbers of the handoffs, finished steps, discards and finished items that pass `keep`.
131
+ function numbersOf(facts, keep) {
132
+ const handoffs = facts.flatMap((f) => f.handoffs.filter(keep));
133
+ const finished = facts.flatMap((f) => f.finished.filter(({ by }) => keep(by)));
134
+ const items = new Set(facts.flatMap((f) => f.done.filter(({ by }) => by.some(keep)).map(({ item }) => item)));
135
+ const claimed = handoffs.flatMap(({ claimMs }) => (claimMs === null ? [] : [claimMs]));
136
+ return {
137
+ handoffs: handoffs.length,
138
+ sentBack: handoffs.filter(({ sentBack }) => sentBack).length,
139
+ finishedSteps: finished.length,
140
+ rounds: finished.reduce((sum, { rounds }) => sum + rounds, 0),
141
+ itemsFinished: items.size,
142
+ discards: facts.flatMap((f) => f.discards.filter(({ by }) => keep(by))).length,
143
+ medianClaimToHandoffMs: median(claimed),
144
+ };
145
+ }
146
+ const groupOf = ({ family, effort }) => JSON.stringify([family, effort]);
147
+ const byHandoffs = (a, b) => b.handoffs - a.handoffs;
148
+ // How little a row's profile says: a model the agent could not read says least, then an effort.
149
+ // Such rows are their own groups, never a family's, and come after the declared ones.
150
+ const unread = ({ family, effort }) => (isUnknown(family) ? 2 : 0) + (isUnknown(effort) ? 1 : 0);
151
+ const byDeclared = (a, b) => unread(a) - unread(b) || byHandoffs(a, b);
152
+ // Every item's facts in the period (handoffs by when they were made, finished steps, discards and
153
+ // items by when they happened), grouped by family and effort. `since`: an ISO time, or all.
154
+ export function modelPerformance(trails, { since } = {}) {
155
+ const inPeriod = (at) => since === undefined || at >= since;
156
+ const facts = trails.map(factsOf).map((f) => ({
157
+ handoffs: f.handoffs.filter(({ at }) => inPeriod(at)),
158
+ finished: f.finished.filter(({ at }) => inPeriod(at)),
159
+ discards: f.discards.filter(({ at }) => inPeriod(at)),
160
+ done: f.done.filter(({ at }) => inPeriod(at)),
161
+ }));
162
+ const seen = new Map();
163
+ for (const f of facts) {
164
+ for (const h of [
165
+ ...f.handoffs,
166
+ ...f.finished.map(({ by }) => by),
167
+ ...f.discards.map(({ by }) => by),
168
+ ])
169
+ if (!seen.has(groupOf(h.profile)))
170
+ seen.set(groupOf(h.profile), h.profile);
171
+ }
172
+ const rows = [...seen.entries()].map(([key, { family, effort }]) => {
173
+ const inGroup = (h) => groupOf(h.profile) === key;
174
+ const models = new Set();
175
+ const steps = new Map();
176
+ for (const f of facts) {
177
+ for (const h of [...f.handoffs, ...f.finished.map(({ by }) => by)].filter(inGroup)) {
178
+ models.add(h.profile.model);
179
+ steps.set(JSON.stringify([h.board, h.step]), h);
180
+ }
181
+ }
182
+ return {
183
+ family,
184
+ effort,
185
+ ...numbersOf(facts, inGroup),
186
+ models: [...models]
187
+ .map((model) => ({
188
+ model,
189
+ ...numbersOf(facts, (h) => inGroup(h) && h.profile.model === model),
190
+ }))
191
+ .sort(byHandoffs),
192
+ steps: [...steps.values()]
193
+ .map(({ board, step, stepName }) => ({
194
+ board,
195
+ step,
196
+ stepName,
197
+ ...numbersOf(facts, (h) => inGroup(h) && h.board === board && h.step === step),
198
+ }))
199
+ .sort(byHandoffs),
200
+ };
201
+ });
202
+ return { rows: rows.sort(byDeclared), total: numbersOf(facts, () => true) };
203
+ }
204
+ const PERIOD = /^(\d+)([dh])$/;
205
+ const UNIT_MS = { d: 24 * 60 * 60_000, h: 60 * 60_000 };
206
+ export const PERIOD_HINT = 'a period such as 7d, 30d or 12h, or all';
207
+ // A period back from now (`30d`, `7d`, `12h`) as the ISO time it starts at: `{}` for `all` (no
208
+ // limit), null for anything else.
209
+ export function parsePeriod(text, now) {
210
+ if (text === 'all')
211
+ return {};
212
+ const match = PERIOD.exec(text);
213
+ const count = Number(match?.[1]);
214
+ if (!match || !(count > 0))
215
+ return null;
216
+ const unit = match[2];
217
+ return { since: new Date(Date.parse(now) - count * UNIT_MS[unit]).toISOString() };
218
+ }