@giamme/forge 0.19.0 → 0.21.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 (72) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +7 -0
  3. package/SKILL.md +3 -0
  4. package/VERSION +1 -1
  5. package/docs/README.md +1 -0
  6. package/docs/commands.md +137 -5
  7. package/docs/development.md +1 -1
  8. package/docs/forge-ui.md +70 -19
  9. package/forge +8 -2
  10. package/package.json +1 -1
  11. package/references/decompose.md +20 -8
  12. package/references/memory.md +30 -5
  13. package/scripts/forge-config.py +10 -9
  14. package/scripts/forge-control.py +51 -35
  15. package/scripts/forge-findings.py +675 -0
  16. package/scripts/forge-group.py +2 -1
  17. package/scripts/forge-guard.py +71 -33
  18. package/scripts/forge-handoff.py +14 -17
  19. package/scripts/forge-issues.py +233 -99
  20. package/scripts/forge-memory.sh +471 -19
  21. package/scripts/forge-parallel.sh +24 -14
  22. package/scripts/forge-planner.py +28 -2
  23. package/scripts/forge-pre-review.py +11 -4
  24. package/scripts/forge-ripwire.py +68 -22
  25. package/scripts/forge-route.py +10 -3
  26. package/scripts/forge-runs-compact.py +3 -2
  27. package/scripts/forge-runs-rm.py +14 -14
  28. package/scripts/forge-runtime.py +36 -19
  29. package/scripts/forge-schedule.py +135 -62
  30. package/scripts/forge-shadow.py +16 -9
  31. package/scripts/forge-summarize.py +7 -3
  32. package/scripts/forge-sweep.py +206 -46
  33. package/scripts/forge-ui.py +8 -13
  34. package/scripts/forge-verify.py +29 -23
  35. package/scripts/forge_brief/__init__.py +1 -1
  36. package/scripts/forge_brief/proposals.py +7 -2
  37. package/scripts/forge_decide/budget.py +6 -1
  38. package/scripts/forge_decide/replay.py +6 -1
  39. package/scripts/forge_findings/__init__.py +17 -0
  40. package/scripts/forge_findings/apply.py +442 -0
  41. package/scripts/forge_findings/status.py +43 -0
  42. package/scripts/forge_findings/store.py +595 -0
  43. package/scripts/forge_findings/tags.py +53 -0
  44. package/scripts/forge_findings/triage.py +594 -0
  45. package/scripts/forge_flock.py +87 -0
  46. package/scripts/forge_fractal/__init__.py +7 -3
  47. package/scripts/forge_fractal/cli.py +7 -2
  48. package/scripts/forge_fractal/execution.py +14 -14
  49. package/scripts/forge_fractal/invocation.py +3 -2
  50. package/scripts/forge_fractal/pipeline.py +2 -1
  51. package/scripts/forge_fractal/selection.py +2 -1
  52. package/scripts/forge_identity.py +328 -0
  53. package/scripts/forge_locations.py +14 -4
  54. package/scripts/forge_memory_list.py +7 -3
  55. package/scripts/forge_posix.py +108 -0
  56. package/scripts/forge_proc.py +399 -0
  57. package/scripts/forge_registry.py +15 -6
  58. package/scripts/forge_retro/__init__.py +8 -2
  59. package/scripts/forge_retro/issues.py +2 -1
  60. package/scripts/forge_side/__init__.py +232 -38
  61. package/scripts/forge_storage.py +11 -1
  62. package/ui/docs/api.md +130 -12
  63. package/ui/forge_ui/control.py +693 -17
  64. package/ui/forge_ui/export.py +1 -1
  65. package/ui/forge_ui/projects.py +209 -22
  66. package/ui/forge_ui/reader.py +18 -17
  67. package/ui/forge_ui/server.py +10 -1
  68. package/ui/forge_ui/static/findingsactions.js +201 -0
  69. package/ui/forge_ui/static/index.html +2 -0
  70. package/ui/forge_ui/static/projectsview.js +600 -45
  71. package/ui/forge_ui/static/style.css +59 -0
  72. package/ui/forge_ui/static/triageview.js +423 -0
@@ -1496,6 +1496,65 @@ table.wt-grid { width: max-content; min-width: 100%; }
1496
1496
  .prj-findings td.prj-wide { white-space: normal; min-width: 26em; }
1497
1497
  .prj-summary { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; max-width: 46em; }
1498
1498
  .prj-learning { display: inline-block; max-width: 48em; }
1499
+ .prj-actions { margin-top: var(--s-2); }
1500
+ .prj-actions .fnd-picked { min-width: 8em; font-size: var(--t-sm); font-variant-numeric: tabular-nums; }
1501
+ .prj-findings td:first-child input[type=checkbox] { margin: 0; }
1502
+ .fnd-form { display: grid; gap: var(--s-2); }
1503
+ .fnd-form:empty { display: none; }
1504
+ .fnd-form-title { margin: 0; font-size: var(--t-sm); }
1505
+ .fnd-form-body { display: flex; flex-wrap: wrap; align-items: flex-start; gap: var(--s-2); }
1506
+ .fnd-form-body textarea { min-width: 26em; font: inherit; }
1507
+ .fnd-form-msg { margin: 0; font-size: var(--t-sm); }
1508
+ .fnd-form-msg:empty { display: none; }
1509
+ .fnd-confirm:empty, .fnd-result:empty { display: none; }
1510
+ .fnd-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 3px; }
1511
+ .fnd-tag { padding: 0 6px; border: 1px solid var(--rule); border-radius: var(--r-small); background: var(--key); color: var(--ink); font-size: var(--t-xs); }
1512
+ .fnd-tag-derived { color: var(--muted); border-style: dashed; background: transparent; }
1513
+ .fnd-rowbtn { padding: 0 6px; min-height: 22px; font-size: var(--t-xs); }
1514
+ .fnd-rec { margin: var(--s-2) 0 0; font-size: var(--t-xs); overflow-wrap: anywhere; }
1515
+ .fnd-cmds { display: grid; gap: 4px; margin-top: var(--s-2); }
1516
+ .fnd-cmds p { margin: 0; font-size: var(--t-xs); }
1517
+ .fnd-cmd { display: flex; align-items: center; gap: var(--s-2); }
1518
+ .fnd-cmd code { flex: 1 1 auto; min-width: 0; padding: 2px 6px; border: 1px solid var(--rule); border-radius: var(--r-small); font-size: var(--t-xs); white-space: pre-wrap; overflow-wrap: anywhere; }
1519
+ .fnd-copy { min-height: 22px; padding: 0 8px; font-size: var(--t-xs); }
1520
+
1521
+ /* ---------- triage panel on the Findings tab (#161) ---------- */
1522
+ .triage { border: 1px solid var(--rule); border-radius: var(--r-key); background: var(--module-2); }
1523
+ .triage-title { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--s-2); padding: var(--s-2) 10px; cursor: pointer; }
1524
+ .triage-title:focus-visible { outline: 2px solid var(--route); outline-offset: 2px; }
1525
+ .triage-badge { font-size: var(--t-xs); color: var(--muted); }
1526
+ .triage-badge-running { color: var(--warn-ink); font-weight: 600; }
1527
+ .triage-badge-failed { color: var(--lamp-red); font-weight: 600; }
1528
+ .triage-body { display: grid; gap: var(--s-2); padding: 0 10px 10px; }
1529
+ .triage-start { display: grid; gap: var(--s-2); }
1530
+ .triage-form { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2); }
1531
+ .triage-form input[data-field=spec] { min-width: 14rem; }
1532
+ .triage-form input[data-field=max] { width: 6rem; }
1533
+ .triage-check { margin: 0; font-size: var(--t-sm); overflow-wrap: anywhere; }
1534
+ .triage-check:empty, .triage-confirm:empty, .triage-msgs:empty { display: none; }
1535
+ .triage-cmds { display: grid; gap: 4px; }
1536
+ .triage-cmds p { margin: 0; font-size: var(--t-xs); }
1537
+ .triage-view { display: grid; gap: var(--s-2); }
1538
+ .triage-state-line { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin: 0; }
1539
+ .triage-state-name { font-weight: 600; }
1540
+ .triage-reason { margin: 0; overflow-wrap: anywhere; }
1541
+ .triage-group { display: grid; gap: var(--s-2); align-content: start; }
1542
+ .triage-card { display: grid; gap: 4px; padding: var(--s-2) 10px; border: 1px solid var(--rule); border-radius: var(--r-key); background: var(--module); }
1543
+ .triage-card[aria-busy=true] { opacity: .7; }
1544
+ .triage-card-top { display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: baseline; }
1545
+ .triage-state-applied { color: var(--lamp-green, var(--ink)); }
1546
+ .triage-state-rejected { color: var(--muted); }
1547
+ .triage-summary, .triage-why, .triage-title-line, .triage-decided { margin: 0; overflow-wrap: anywhere; }
1548
+ .triage-title-line { font-weight: 600; }
1549
+ .triage-fps, .triage-tasks { margin: 0; padding-left: 1.2em; display: grid; gap: 2px; font-size: var(--t-sm); }
1550
+ .triage-fp-id { font-size: var(--t-xs); }
1551
+ button.triage-fp-id { min-height: 20px; padding: 0 6px; }
1552
+ .triage-fp-text { overflow-wrap: anywhere; }
1553
+ .triage-hash { font-size: var(--t-xs); overflow-wrap: anywhere; }
1554
+ .triage-details summary { cursor: pointer; font-size: var(--t-sm); }
1555
+ .triage-pre { margin: 4px 0 0; max-height: 12rem; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; font-size: var(--t-xs); }
1556
+ .triage-actions { display: flex; flex-wrap: wrap; gap: var(--s-2); align-items: center; }
1557
+ .triage-actions input { flex: 1 1 12rem; min-width: 8rem; min-height: 28px; padding: 4px 8px; }
1499
1558
  .prj-where a, .project-link { white-space: nowrap; }
1500
1559
  .prj-tabs a:focus-visible, .prj-links a:focus-visible, .project-link:focus-visible { outline: 2px solid var(--route); outline-offset: 2px; }
1501
1560
 
@@ -0,0 +1,423 @@
1
+ /* forge-ui triage panel (#161): on a project's Findings tab a person starts a read-only triage agent on a model they
2
+ name, watches it, and approves or rejects each proposal it wrote. window.ForgeTriage, and a CommonJS module under
3
+ node:test. The pure helpers (proposalSummary, cliFor, isActive, pollDelay, confirmFor, elapsed) and the panel are
4
+ tested in ui/tests/js/triageview.test.js. Everything the agent wrote (reasons, titles, bodies, plans) is untrusted:
5
+ it only ever enters the page as text.
6
+ An approval sends exactly the hash of the card it was made on, so a proposal that changed since it was read is
7
+ refused by the server. Read-only (no write token, or controls off) the panel shows the `forge findings` command
8
+ of each action to copy, and the state and the proposals as they are.
9
+ render(host, ctx) returns {stop, refresh}. ctx = { id, root, writes, reload(), store?, openCount?(), rows?(), show?(fp) }:
10
+ `store` is a plain object the caller keeps between redraws (open, spec, max, the last answer, the plan dirs made);
11
+ `rows()` are the Findings tab's rows (to name a finding by its summary), `show(fp)` shows one finding in the table.
12
+ Polling: one timer at most, only while the triage runs, gone on stop(), and when the host leaves the page. */
13
+ (function (root, factory) {
14
+ 'use strict';
15
+ const api = factory(root);
16
+ if (typeof module === 'object' && module.exports) module.exports = api;
17
+ else root.ForgeTriage = api;
18
+ })(typeof self !== 'undefined' ? self : this, function (root) {
19
+ 'use strict';
20
+
21
+ const list = v => Array.isArray(v) ? v : [];
22
+ const POLL_MS = 5000, CHECK_MS = 400, MAX_LIMIT = 2000, MAX_REASON = 500;
23
+ const KINDS = ['duplicate', 'fixed', 'tag', 'issue', 'plan'];
24
+ const KIND_TITLE = { duplicate: 'Duplicates', fixed: 'Already fixed', tag: 'Tags', issue: 'Issues to file', plan: 'Plans' };
25
+ const str = v => typeof v === 'string' ? v : v == null ? '' : String(v);
26
+ const noun = n => `${n} finding${n === 1 ? '' : 's'}`;
27
+
28
+ /* ---------- pure ---------- */
29
+ function shellQuote(value) {
30
+ const s = str(value);
31
+ return /^[A-Za-z0-9_@%+=:,./-]+$/.test(s) ? s : `'${s.replace(/'/g, `'\\''`)}'`;
32
+ }
33
+ // The fingerprints a proposal names, in order, each once.
34
+ function fpsOf(p) {
35
+ if (!p || typeof p !== 'object') return [];
36
+ const raw = p.kind === 'duplicate' || p.kind === 'fixed' ? [p.fp] : list(p.fps);
37
+ return Array.from(new Set(raw.filter(fp => typeof fp === 'string' && fp)));
38
+ }
39
+ // One line per proposal.
40
+ function proposalSummary(p) {
41
+ if (!p || typeof p !== 'object') return '—';
42
+ const n = fpsOf(p).length;
43
+ if (p.kind === 'duplicate') return `duplicate: ${str(p.fp)} repeats ${str(p.of)}`;
44
+ if (p.kind === 'fixed') return `fixed: ${str(p.fp)}` + (p.commit ? ` by ${str(p.commit).slice(0, 12)}` : '');
45
+ if (p.kind === 'tag') return `tag ${noun(n)}: ${list(p.tags).join(', ')}`;
46
+ if (p.kind === 'issue') return `issue for ${noun(n)}: ${str(p.title)}`;
47
+ if (p.kind === 'plan') return `plan for ${noun(n)}: ${str(p.title)}` + (list(p.plan && p.plan.tasks).length ? ` (${list(p.plan.tasks).length} tasks)` : '');
48
+ return str(p.kind) || 'proposal';
49
+ }
50
+ // What an approval changes, said before it happens.
51
+ function confirmFor(p) {
52
+ const n = fpsOf(p).length;
53
+ if (!p || typeof p !== 'object') return 'Approve this proposal?';
54
+ if (p.kind === 'duplicate') return `Mark ${noun(1)} ${str(p.fp)} as a duplicate of ${str(p.of)}?`;
55
+ if (p.kind === 'fixed') return `Mark ${noun(1)} ${str(p.fp)} fixed${p.commit ? ` by ${str(p.commit)}` : ''}?`;
56
+ if (p.kind === 'tag') return `Add ${list(p.tags).join(', ')} to ${noun(n)}?`;
57
+ if (p.kind === 'issue') return `Mark ${noun(n)} to file as "${str(p.title)}"? (No GitHub issue is made yet.)`;
58
+ if (p.kind === 'plan') return 'Create the plan dir and print its plan command (nothing runs)?';
59
+ return 'Approve this proposal?';
60
+ }
61
+ // The `forge findings …` command of each action. `p` is the proposal (id, hash) or, for `triage`, {spec}.
62
+ function cliFor(op, repo, n, p) {
63
+ const r = shellQuote(repo), o = p || {};
64
+ if (op === 'triage') return `forge findings triage ${r} --spec ${shellQuote(o.spec || 'SPEC')} --detach`;
65
+ if (op === 'proposals') return `forge findings proposals ${r}`;
66
+ if (op === 'apply') return `forge findings apply ${r} ${shellQuote(n)} ${shellQuote(o.id)} --approve ${shellQuote(o.hash)}`;
67
+ if (op === 'reject') return `forge findings reject ${r} ${shellQuote(n)} ${shellQuote(o.id)}`;
68
+ return '';
69
+ }
70
+ const isActive = state => !!state && typeof state === 'object' && state.state === 'running';
71
+ const pollDelay = state => isActive(state) ? POLL_MS : null;
72
+ // "1m 05s" from an ISO start (and an ISO or ms end) to now; null when the start is not a time.
73
+ function elapsed(started, end) {
74
+ const from = Date.parse(started);
75
+ if (!Number.isFinite(from)) return null;
76
+ const to = end == null ? Date.now() : typeof end === 'number' ? end : Date.parse(end);
77
+ if (!Number.isFinite(to)) return null;
78
+ const s = Math.max(0, Math.round((to - from) / 1000));
79
+ return s < 60 ? `${s}s` : s < 3600 ? `${Math.floor(s / 60)}m ${String(s % 60).padStart(2, '0')}s` : `${Math.floor(s / 3600)}h ${String(Math.floor(s / 60) % 60).padStart(2, '0')}m`;
80
+ }
81
+ // The harness and model a spec resolved to, as one line.
82
+ function resolvedText(resolved) {
83
+ if (typeof resolved === 'string') return resolved;
84
+ const r = resolved && typeof resolved === 'object' ? resolved : {};
85
+ const head = [r.harness, r.model].filter(Boolean).join(' · ');
86
+ const extra = [r.effort ? `effort ${r.effort}` : '', r.alias ? `alias ${r.alias}` : ''].filter(Boolean).join(', ');
87
+ return (head || 'resolved') + (extra ? ` (${extra})` : '');
88
+ }
89
+ // max findings: '' → null (the server's default); 1..2000 → the number; anything else → NaN.
90
+ function parseMax(text) {
91
+ const t = str(text).trim();
92
+ if (!t) return null;
93
+ const n = Number(t);
94
+ return /^\d+$/.test(t) && n >= 1 && n <= MAX_LIMIT ? n : NaN;
95
+ }
96
+
97
+ /* ---------- DOM ---------- */
98
+ const forgeUI = () => root && root.ForgeUI ? root.ForgeUI : globalThis.ForgeUI;
99
+ function el(tag, text, className) { return forgeUI().el(tag, text, className); }
100
+ function timers() { return globalThis; }
101
+
102
+ function render(host, ctx) {
103
+ ctx = ctx || {};
104
+ const UI = forgeUI();
105
+ const store = ctx.store && typeof ctx.store === 'object' ? ctx.store : {};
106
+ if (!store.notes) store.notes = {};
107
+ const enc = encodeURIComponent, base = `/api/projects/${enc(ctx.id)}/triage`;
108
+ let live = true, writes = ctx.writes === true, pollTimer = null, checkTimer = null, checkSeq = 0, loadSeq = 0;
109
+ let check = { ok: false, text: '', tone: 'muted' }, starting = false, loadError = null;
110
+ const rowsByFp = () => new Map(list(ctx.rows && ctx.rows()).filter(r => r && r.record_fp).map(r => [r.record_fp, r]));
111
+
112
+ const box = el('details', undefined, 'triage');
113
+ const title = el('summary', undefined, 'triage-title');
114
+ const badge = el('span', '', 'triage-badge');
115
+ title.append(el('span', 'Triage with an agent', 'plate'), badge);
116
+ const startHost = el('div', undefined, 'triage-start'), view = el('div', undefined, 'triage-view');
117
+ const inner = el('div', undefined, 'triage-body');
118
+ inner.append(startHost, view);
119
+ box.append(title, inner);
120
+ host.replaceChildren(box);
121
+ box.open = !!store.open;
122
+
123
+ function cancelConfirms() {
124
+ for (const strip of [...(startHost.children || [])]) {
125
+ if (!strip.classList || !strip.classList.contains('ui-confirm')) continue;
126
+ for (const b of strip.children || []) if (b.tagName === 'BUTTON' && b.textContent === 'Cancel' && b.click) { b.click(); break; }
127
+ }
128
+ }
129
+ function stop() {
130
+ live = false;
131
+ timers().clearTimeout(pollTimer); pollTimer = null;
132
+ timers().clearTimeout(checkTimer); checkTimer = null;
133
+ cancelConfirms();
134
+ }
135
+ // a panel is drawn into its host before the host is on the page, so only a firing timer judges whether the host left
136
+ const alive = () => live && host.isConnected !== false;
137
+
138
+ /* the state, and the one polling timer */
139
+ const stateNow = () => store.data && store.data.state || null;
140
+ function schedule() {
141
+ timers().clearTimeout(pollTimer); pollTimer = null;
142
+ if (!live) return;
143
+ const delay = pollDelay(stateNow());
144
+ if (delay == null) return;
145
+ pollTimer = timers().setTimeout(async () => { pollTimer = null; if (alive()) await load(); else stop(); }, delay);
146
+ }
147
+ async function load(n) {
148
+ const mine = ++loadSeq;
149
+ const res = await UI.get(base + (n ? `?n=${enc(n)}` : ''));
150
+ if (!live || mine !== loadSeq) return;
151
+ if (res.ok) { store.data = { state: res.data.state || null, proposals: res.data.proposals || null }; loadError = null; }
152
+ else loadError = res.detail || res.error || 'no answer';
153
+ drawView();
154
+ schedule();
155
+ }
156
+
157
+ /* the start form */
158
+ const specInput = el('input'); specInput.type = 'text'; specInput.maxLength = 120; specInput.placeholder = 'e.g. sol:high'; specInput.value = store.spec || '';
159
+ specInput.setAttribute('aria-label', 'Model spec'); specInput.dataset.field = 'spec';
160
+ const maxInput = el('input'); maxInput.type = 'number'; maxInput.min = '1'; maxInput.max = String(MAX_LIMIT); maxInput.placeholder = 'all';
161
+ maxInput.setAttribute('aria-label', 'Most findings to read'); maxInput.dataset.field = 'max'; maxInput.value = store.max || '';
162
+ const checkLine = el('p', '', 'triage-check muted'); checkLine.setAttribute('aria-live', 'polite');
163
+ const startKey = el('button', 'Start triage', 'primary'); startKey.type = 'button'; startKey.dataset.op = 'start';
164
+ const confirmHost = el('div', undefined, 'triage-confirm'), msgs = el('div', undefined, 'triage-msgs');
165
+ const running = () => isActive(stateNow());
166
+ function syncStart() {
167
+ const max = parseMax(maxInput.value);
168
+ startKey.disabled = !check.ok || Number.isNaN(max) || running() || starting;
169
+ startKey.title = running() ? 'a triage is already running' : '';
170
+ checkLine.textContent = Number.isNaN(max) ? `Most findings: a whole number from 1 to ${MAX_LIMIT}.` : check.text;
171
+ checkLine.className = 'triage-check ' + (Number.isNaN(max) ? 'warn' : check.tone);
172
+ }
173
+ async function runCheck() {
174
+ checkTimer = null;
175
+ const spec = specInput.value.trim(), mine = ++checkSeq;
176
+ if (!spec) { check = { ok: false, text: '', tone: 'muted' }; syncStart(); return; }
177
+ const res = await UI.get('/api/specs/check?spec=' + enc(spec));
178
+ if (!live || mine !== checkSeq) return;
179
+ if (res.ok && res.data.ok === true) check = { ok: true, text: 'Runs as ' + resolvedText(res.data.resolved), tone: 'muted' };
180
+ else check = { ok: false, text: res.ok ? str(res.data.error) || 'this spec does not resolve' : res.detail || res.error || 'could not check the spec', tone: 'warn' };
181
+ syncStart();
182
+ }
183
+ function typed() {
184
+ store.spec = specInput.value;
185
+ check = { ok: false, text: specInput.value.trim() ? 'Checking the spec…' : '', tone: 'muted' };
186
+ checkSeq += 1;
187
+ timers().clearTimeout(checkTimer); checkTimer = null;
188
+ syncStart();
189
+ if (specInput.value.trim()) checkTimer = timers().setTimeout(runCheck, CHECK_MS);
190
+ }
191
+ async function start() {
192
+ if (startKey.disabled) return;
193
+ const spec = specInput.value.trim(), max = parseMax(maxInput.value);
194
+ const open = Number.isFinite(ctx.openCount && ctx.openCount()) ? ctx.openCount() : null;
195
+ const count = open == null ? null : max ? Math.min(open, max) : open;
196
+ const what = count == null ? 'the open findings' : `${count} open finding${count === 1 ? '' : 's'}`;
197
+ starting = true; syncStart(); msgs.replaceChildren();
198
+ try {
199
+ const yes = await UI.confirmInline(confirmHost, `Start a read-only triage of ${what} with ${spec}? It uses that model's quota.`, {});
200
+ if (!live || !yes) return;
201
+ const body = { spec };
202
+ if (max) body.max_findings = max;
203
+ const res = await UI.post(base, body);
204
+ if (res && res.ok) store.data = { state: { state: 'running', spec, started: new Date().toISOString(), findings: count, triage: res.data.triage }, proposals: null };
205
+ if (!live) return;
206
+ if (!res || !res.ok) { UI.outcome(msgs, res); return; }
207
+ drawView(); schedule();
208
+ load(res.data.triage);
209
+ } finally {
210
+ starting = false;
211
+ if (live) syncStart();
212
+ }
213
+ }
214
+ function drawStart() {
215
+ startHost.replaceChildren();
216
+ if (!writes) {
217
+ const cmds = el('div', undefined, 'triage-cmds');
218
+ cmds.append(el('p', 'This page is read-only. Start and read a triage from a terminal:', 'muted'));
219
+ for (const op of ['triage', 'proposals']) cmds.append(commandRow(cliFor(op, ctx.root, null, {})));
220
+ startHost.append(cmds);
221
+ return;
222
+ }
223
+ const row = el('div', undefined, 'triage-form');
224
+ const l1 = el('label', undefined, 'fnd-field'), l2 = el('label', undefined, 'fnd-field');
225
+ l1.append(el('span', 'Model', 'plate'), specInput); l2.append(el('span', 'Most findings', 'plate'), maxInput);
226
+ row.append(l1, l2, startKey);
227
+ startHost.append(row, checkLine, confirmHost, msgs);
228
+ syncStart();
229
+ }
230
+ specInput.addEventListener('input', typed);
231
+ maxInput.addEventListener('input', () => { store.max = maxInput.value; syncStart(); });
232
+ startKey.addEventListener('click', () => { start(); });
233
+
234
+ function commandRow(command) {
235
+ const row = el('div', undefined, 'fnd-cmd'), code = el('code', command, 'mono'); code.tabIndex = 0;
236
+ const b = el('button', 'Copy', 'fnd-copy'); b.type = 'button'; b.setAttribute('aria-label', 'Copy: ' + command);
237
+ b.addEventListener('click', async () => {
238
+ try { await navigator.clipboard.writeText(command); b.textContent = 'Copied'; } catch (_) { b.textContent = 'Copy failed: select it'; }
239
+ timers().setTimeout(() => { b.textContent = 'Copy'; }, 2500);
240
+ });
241
+ row.append(code, b);
242
+ return row;
243
+ }
244
+
245
+ /* the proposals */
246
+ function findingRef(fp, rows) {
247
+ const li = el('li', undefined, 'triage-fp'), row = rows.get(fp);
248
+ const label = el(ctx.show && row ? 'button' : 'span', fp, 'mono triage-fp-id');
249
+ if (label.tagName === 'BUTTON') {
250
+ label.type = 'button'; label.title = 'show this finding in the table';
251
+ label.addEventListener('click', () => ctx.show(fp));
252
+ }
253
+ li.append(label);
254
+ if (row && row.summary) {
255
+ const text = str(row.summary).replace(/^[-*\s]*(\[[^\]]*\]\s*)*/, '').slice(0, 200);
256
+ li.append(el('span', ' ' + (text || row.summary), 'muted triage-fp-text'));
257
+ }
258
+ return li;
259
+ }
260
+ async function decide(card, p, n, op, input, parts) {
261
+ if (card.dataset.busy === '1') return;
262
+ let body;
263
+ if (op === 'apply') {
264
+ body = { hash: p.hash };
265
+ const dir = input && input.value.trim();
266
+ if (dir) body.plan_dir = dir;
267
+ card.dataset.busy = '1'; setBusy(parts, true);
268
+ const yes = await UI.confirmInline(parts.confirm, confirmFor(p), {});
269
+ if (!live || !yes) { card.dataset.busy = '0'; if (live) setBusy(parts, false); return; }
270
+ } else {
271
+ body = {};
272
+ const reason = input && input.value.trim();
273
+ if (reason) body.reason = reason.slice(0, MAX_REASON);
274
+ card.dataset.busy = '1'; setBusy(parts, true);
275
+ }
276
+ const res = await UI.post(`${base}/${enc(n)}/${enc(p.id)}/${op}`, body);
277
+ card.dataset.busy = '0';
278
+ if (res && res.ok && op === 'apply' && res.data && (res.data.next || res.data.plan_dir)) {
279
+ store.notes[`${n}:${p.id}`] = { plan_dir: res.data.plan_dir || null, next: res.data.next || null };
280
+ }
281
+ if (!live) { if (res && res.ok && ctx.reload) ctx.reload(); return; }
282
+ if (!res || !res.ok) { setBusy(parts, false); UI.outcome(parts.msg, res); return; }
283
+ await load(n);
284
+ if (ctx.reload) ctx.reload();
285
+ }
286
+ function setBusy(parts, on) {
287
+ if (on) parts.card.setAttribute('aria-busy', 'true'); else parts.card.removeAttribute('aria-busy');
288
+ for (const b of parts.buttons) b.disabled = on || (b.dataset.op === 'approve' && !parts.hash);
289
+ }
290
+ function proposalCard(p, n, rows) {
291
+ const card = el('article', undefined, 'triage-card'); card.dataset.pid = str(p.id); card.dataset.kind = str(p.kind);
292
+ const top = el('div', undefined, 'triage-card-top');
293
+ top.append(el('span', str(p.kind) || '—', 'tag'), el('span', str(p.state) || '—', 'tag triage-state triage-state-' + str(p.state)), el('span', str(p.id), 'mono muted'));
294
+ card.append(top, el('p', proposalSummary(p), 'triage-summary'));
295
+ const fps = fpsOf(p);
296
+ if (fps.length) { const ul = el('ul', undefined, 'triage-fps'); for (const fp of fps) ul.append(findingRef(fp, rows)); card.append(ul); }
297
+ if (p.kind === 'duplicate' && p.why) card.append(el('p', 'Why: ' + str(p.why), 'triage-why'));
298
+ if (p.kind === 'fixed') {
299
+ if (p.evidence) card.append(el('p', 'Evidence: ' + str(p.evidence), 'triage-why'));
300
+ if (p.commit) card.append(el('p', 'Commit: ' + str(p.commit), 'triage-why mono'));
301
+ }
302
+ if (p.kind === 'tag') {
303
+ const chips = el('div', undefined, 'fnd-tags');
304
+ for (const t of list(p.tags)) chips.append(el('span', str(t), 'fnd-tag'));
305
+ card.append(chips);
306
+ }
307
+ if (p.kind === 'issue' || p.kind === 'plan') card.append(el('p', str(p.title), 'triage-title-line'));
308
+ if (p.kind === 'issue') {
309
+ const d = el('details', undefined, 'triage-details'); d.append(el('summary', 'Issue body'), el('pre', str(p.body), 'triage-pre'));
310
+ card.append(d);
311
+ }
312
+ if (p.kind === 'plan') {
313
+ if (p.request) { const d = el('details', undefined, 'triage-details'); d.append(el('summary', 'Request'), el('pre', str(p.request), 'triage-pre')); card.append(d); }
314
+ const tasks = list(p.plan && p.plan.tasks);
315
+ if (tasks.length) {
316
+ const ul = el('ul', undefined, 'triage-tasks');
317
+ for (const t of tasks) {
318
+ const li = el('li');
319
+ li.append(el('span', str(t && t.id), 'mono'), el('span', ' ' + str(t && t.difficulty || '—'), 'tag'), el('span', ' ' + str(t && t.title)));
320
+ ul.append(li);
321
+ }
322
+ card.append(ul);
323
+ }
324
+ }
325
+ if (p.hash) card.append(el('code', str(p.hash), 'mono triage-hash'));
326
+ if (p.decided && typeof p.decided === 'object') {
327
+ card.append(el('p', `${str(p.state)} ${str(p.decided.at)}${p.decided.actor ? ' by ' + str(p.decided.actor) : ''}${p.decided.reason ? ': ' + str(p.decided.reason) : ''}`, 'muted triage-decided'));
328
+ }
329
+ const note = store.notes[`${n}:${p.id}`];
330
+ const planDir = p.plan_dir || (note && note.plan_dir);
331
+ if (planDir) card.append(el('p', 'Plan dir: ' + str(planDir), 'triage-why mono'));
332
+ if (note && note.next) { card.append(el('p', 'Next, to run it (nothing has run):', 'muted'), commandRow(note.next)); }
333
+ const confirm = el('div', undefined, 'triage-confirm'), msg = el('div', undefined, 'triage-msgs');
334
+ const parts = { card, confirm, msg, buttons: [], hash: p.hash };
335
+ if (p.state === 'pending') {
336
+ if (writes) {
337
+ const actions = el('div', undefined, 'triage-actions');
338
+ const approve = el('button', 'Approve', 'primary'); approve.type = 'button'; approve.dataset.op = 'approve'; approve.disabled = !p.hash;
339
+ let dirInput = null;
340
+ if (p.kind === 'plan') {
341
+ dirInput = el('input'); dirInput.type = 'text'; dirInput.placeholder = 'plan dir (optional)'; dirInput.setAttribute('aria-label', 'Plan dir'); dirInput.dataset.field = 'plan-dir';
342
+ }
343
+ approve.addEventListener('click', () => { decide(card, p, n, 'apply', dirInput, parts); });
344
+ const reject = el('button', 'Reject'); reject.type = 'button'; reject.dataset.op = 'reject';
345
+ const reason = el('input'); reason.type = 'text'; reason.maxLength = MAX_REASON; reason.placeholder = 'Reason (optional)'; reason.setAttribute('aria-label', 'Reject reason'); reason.dataset.field = 'reason';
346
+ reject.addEventListener('click', () => { decide(card, p, n, 'reject', reason, parts); });
347
+ parts.buttons.push(approve, reject);
348
+ actions.append(approve);
349
+ if (dirInput) actions.append(dirInput);
350
+ actions.append(reject, reason);
351
+ card.append(actions);
352
+ } else {
353
+ card.append(el('p', 'Change it from a terminal:', 'muted'));
354
+ card.append(commandRow(cliFor('apply', ctx.root, n, p)), commandRow(cliFor('reject', ctx.root, n, p)));
355
+ }
356
+ }
357
+ card.append(confirm, msg);
358
+ return card;
359
+ }
360
+
361
+ function statusBlock(st) {
362
+ const wrap = el('div', undefined, 'triage-status');
363
+ const tone = st.state === 'running' ? 'running' : st.state === 'done' ? 'pass' : 'fail';
364
+ const head = el('p', undefined, 'triage-state-line');
365
+ head.append(el('i', undefined, 'lamp st-' + tone), el('span', ' ' + str(st.state), 'triage-state-name'));
366
+ const bits = [];
367
+ if (st.spec) bits.push(str(st.spec));
368
+ const time = st.state === 'running' ? elapsed(st.started) : elapsed(st.started, st.finished);
369
+ if (time) bits.push(st.state === 'running' ? `running for ${time}` : `took ${time}`);
370
+ if (Number.isFinite(st.findings)) bits.push(noun(st.findings));
371
+ if (st.state === 'done' && Number.isFinite(st.proposals)) bits.push(`${st.proposals} proposal${st.proposals === 1 ? '' : 's'}`);
372
+ head.append(el('span', bits.length ? ' · ' + bits.join(' · ') : '', 'muted'));
373
+ wrap.append(head);
374
+ if (st.state === 'failed') wrap.append(el('p', 'Failed: ' + (str(st.reason) || 'no reason given'), 'warn triage-reason'));
375
+ return wrap;
376
+ }
377
+ function drawView() {
378
+ const st = stateNow();
379
+ badge.textContent = st ? str(st.state) : '';
380
+ badge.className = 'triage-badge' + (st ? ' triage-badge-' + str(st.state) : '');
381
+ syncStart();
382
+ view.replaceChildren();
383
+ if (loadError) view.append(el('p', `Could not read the triage (${loadError}).`, 'warn'));
384
+ if (!store.data) { if (!loadError) view.append(el('p', 'Reading the triage…', 'muted')); return; }
385
+ if (!st) { view.append(el('p', 'No triage has run for this project yet.', 'muted')); return; }
386
+ view.append(statusBlock(st));
387
+ if (!writes) {
388
+ view.append(commandRow(cliFor('proposals', ctx.root, null, {})));
389
+ }
390
+ const proposals = list(store.data.proposals && store.data.proposals.proposals).filter(p => p && typeof p === 'object');
391
+ if (st.state !== 'done' && !proposals.length) return;
392
+ const n = st.triage || (store.data.proposals && store.data.proposals.triage) || null;
393
+ if (!proposals.length) { if (st.state === 'done') view.append(el('p', 'The agent proposed nothing.', 'muted')); return; }
394
+ const rows = rowsByFp();
395
+ for (const kind of KINDS) {
396
+ const items = proposals.filter(p => p.kind === kind);
397
+ if (!items.length) continue;
398
+ const sec = el('section', undefined, 'triage-group'); sec.dataset.kind = kind;
399
+ const head = el('p', undefined, 'brief-list-head');
400
+ head.append(el('span', KIND_TITLE[kind], 'plate'), el('span', String(items.length), 'module-count'));
401
+ sec.append(head);
402
+ for (const p of items) sec.append(proposalCard(p, n, rows));
403
+ view.append(sec);
404
+ }
405
+ }
406
+
407
+ /* mount */
408
+ box.addEventListener('toggle', () => {
409
+ store.open = !!box.open;
410
+ if (box.open && live && !store.data) load();
411
+ });
412
+ drawStart();
413
+ drawView();
414
+ if (writes !== true && typeof ctx.writes !== 'boolean') {
415
+ UI.controlsEnabled().then(on => { if (!live) return; writes = !!on; drawStart(); drawView(); });
416
+ }
417
+ if (store.data) { schedule(); if (box.open) load(); }
418
+ else if (box.open) load();
419
+ return { stop, refresh: () => load(), box };
420
+ }
421
+
422
+ return { render, proposalSummary, confirmFor, cliFor, isActive, pollDelay, elapsed, fpsOf, parseMax, resolvedText, shellQuote, POLL_MS, CHECK_MS, KINDS };
423
+ });