forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcouncil_of_claudes.js
More file actions
533 lines (500 loc) · 26.6 KB
/
Copy pathcouncil_of_claudes.js
File metadata and controls
533 lines (500 loc) · 26.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// Council of Claudes — fan out a PR's diff to persona-based review agents on
// the kagent cluster (agents.tigera.ai) and post each persona's feedback back
// to the PR. Invoked from council_of_claudes.yml via actions/github-script.
//
// Each persona posts a HYBRID review:
// - a sticky summary review (verdict + non-line findings), updated in place;
// - inline review comments anchored to specific diff lines.
//
// Each persona is gated on its own *_AGENT_URL / *_AGENT_TOKEN env vars
// (sourced from repo secrets). Any persona whose vars are unset is skipped.
//
// Agent call contract (verified against tigera/agents-menagerie + live tests):
// POST <url> with Bearer <token>, JSON-RPC 2.0. The public endpoint caps a
// synchronous response at ~30s, so we submit ASYNCHRONOUSLY:
// 1. message/send with params.configuration.blocking=false -> task id.
// 2. poll tasks/get until terminal; on "completed" read
// result.artifacts[0].parts[0].text.
//
// Persona output contract (markdown; see scripts/personas/*.md):
// <summary markdown>
// <!-- coc-finding file="path" line="N" -->
// <finding markdown>
// ...one marker per line-specific finding. Text before the first marker is
// the summary. The diff sent to personas is annotated with [L<n>] new-side
// line numbers so cited lines can be anchored.
// Each persona has a distinct in-comment identity: an emoji + a GitHub alert
// type (NOTE/TIP/CAUTION render as blue/green/red callouts) + a tagline.
const PERSONAS = [
{ key: 'correctness', title: 'Correctness', emoji: '🔎', accent: 'NOTE', tagline: 'bugs · completeness · concurrency · edge cases', urlEnv: 'CORRECTNESS_AGENT_URL', tokenEnv: 'CORRECTNESS_AGENT_TOKEN' },
{ key: 'maintainability', title: 'Maintainability & Tests', emoji: '🧪', accent: 'TIP', tagline: 'simplicity · tests · docs · idioms', urlEnv: 'MAINTAINABILITY_AGENT_URL', tokenEnv: 'MAINTAINABILITY_AGENT_TOKEN' },
{ key: 'security', title: 'Security', emoji: '🛡️', accent: 'CAUTION', tagline: 'validation · secrets · authz · isolation', urlEnv: 'SECURITY_AGENT_URL', tokenEnv: 'SECURITY_AGENT_TOKEN' },
// A "simulated human reviewer" persona distilled from a real reviewer's comment history.
// Uses a custom avatar image (rendered from the public repo) instead of an emoji.
{ key: 'nelljerram', title: 'Nell', emoji: '🧑💻', accent: 'IMPORTANT', image: 'https://raw.githubusercontent.com/tigera/calico-oss-test/master/.github/assets/nell.png', tagline: 'simulated reviewer · simplicity · naming · error handling · keep useful comments', urlEnv: 'NELLJERRAM_AGENT_URL', tokenEnv: 'NELLJERRAM_AGENT_TOKEN' },
{ key: 'caseydavenport', title: 'Casey', emoji: '🧑🔧', accent: 'WARNING', image: 'https://raw.githubusercontent.com/tigera/calico-oss-test/master/.github/assets/casey.png', tagline: 'simulated reviewer · testing discipline · API design · simplicity · robustness', urlEnv: 'CASEYDAVENPORT_AGENT_URL', tokenEnv: 'CASEYDAVENPORT_AGENT_TOKEN' },
];
// Cap the diff we send to keep within model context. Large-PR handling is out
// of scope for the hackathon; oversized diffs are truncated with a note.
const MAX_DIFF_BYTES = 100 * 1024;
// Calico's own context files (conventions / architecture) used to ground the
// review, read from the checked-out workspace. `always` files are injected on
// every PR; the rest only when a changed path matches one of `whenPathStartsWith`.
const CONTEXT_FILES = [
{ file: '.claude/CLAUDE.md', always: true },
{ file: '.github/copilot-instructions.md', always: true },
{ file: 'felix/CLAUDE.md', whenPathStartsWith: ['felix/'] },
{ file: 'goldmane/CLAUDE.md', whenPathStartsWith: ['goldmane/'] },
{ file: '.github/instructions/bpf.instructions.md', whenPathStartsWith: ['felix/bpf'] },
{ file: '.github/instructions/goldmane.instructions.md', whenPathStartsWith: ['goldmane/'] },
{ file: '.github/instructions/helm-charts.instructions.md', whenPathStartsWith: ['charts/'] },
];
const MAX_CONTEXT_BYTES = 80 * 1024; // backstop so injected context can't blow up the message
// Read the relevant Calico context files from the workspace and format them as
// an authoritative preamble, scoped to the paths this PR touches.
function gatherContext(core, touchedPaths) {
const fs = require('fs');
const path = require('path');
const ws = process.env.GITHUB_WORKSPACE || '.';
const paths = [...touchedPaths];
const chosen = CONTEXT_FILES.filter(c =>
c.always || (c.whenPathStartsWith || []).some(pre => paths.some(tp => tp.startsWith(pre))));
let total = 0;
const blocks = [];
const included = [];
for (const c of chosen) {
let content;
try { content = fs.readFileSync(path.join(ws, c.file), 'utf8').trim(); }
catch (e) { core.info(` context: ${c.file} unavailable (${e.message}), skipping`); continue; }
if (total + content.length > MAX_CONTEXT_BYTES) { core.info(` context: budget reached, skipping ${c.file}`); continue; }
total += content.length;
blocks.push(`### ${c.file}\n\n${content}`);
included.push(c.file);
}
if (!blocks.length) return '';
core.info(`Injected ${blocks.length} Calico context file(s) (${total} bytes): ${included.join(', ')}`);
return `## Project context — authoritative Calico conventions; apply these in your review\n\n${blocks.join('\n\n---\n\n')}\n\n---\n\n`;
}
// Async task polling parameters.
const POLL_INTERVAL_MS = 5_000;
const MAX_POLL_MS = 10 * 60 * 1000; // give a slow generation up to 10 minutes
const RPC_TIMEOUT_MS = 30_000; // each individual call is quick
const sleep = ms => new Promise(res => setTimeout(res, ms));
// Sanitize agent-controlled text before logging so it cannot inject
// ::workflow-command:: strings into the Actions log.
const safe = s => String(s ?? '').replace(/::/g, '::');
// Node's fetch (undici) surfaces generic messages like "fetch failed" /
// "terminated" and puts the actual reason (ECONNRESET, UND_ERR_*, etc.) on
// e.cause. Surface both so error logs are diagnosable.
function errDetail(e) {
const c = e?.cause;
const cause = c ? ` | cause: ${c.code || c.message || c}` : '';
return `${e?.message || e}${cause}`;
}
// Strip a single markdown fence that wraps the *entire* response, while
// leaving any inner code/suggestion fences intact.
function stripOuterFence(text) {
const t = (text || '').trim();
const m = t.match(/^```(?:markdown|md)?\s*\n([\s\S]*?)\n```$/);
return (m ? m[1] : t).trim();
}
// Annotate a unified diff with explicit new-file (RIGHT side) line numbers so
// the model can cite exact lines, and build the set of anchorable lines per
// file. Added ('+') and context (' ') lines get a [L<n>] prefix and are
// anchorable on the RIGHT side; deleted ('-') lines are left unmarked.
function annotateDiff(diff) {
const anchors = new Map(); // path -> Set<number>
const out = [];
let path = null;
let newLine = 0;
const addAnchor = n => {
if (!path) return;
if (!anchors.has(path)) anchors.set(path, new Set());
anchors.get(path).add(n);
};
for (const line of (diff || '').split('\n')) {
const plus = line.match(/^\+\+\+ b\/(.*)$/);
if (plus) { path = plus[1]; out.push(line); continue; }
if (line.startsWith('+++ ')) { path = null; out.push(line); continue; } // e.g. /dev/null
const hunk = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
if (hunk) { newLine = parseInt(hunk[1], 10); out.push(line); continue; }
if (line.startsWith('diff --git') || line.startsWith('--- ') || line.startsWith('index ') ||
line.startsWith('new file') || line.startsWith('deleted file') || line.startsWith('rename ') ||
line.startsWith('similarity ') || line.startsWith('\\ ')) {
out.push(line); continue;
}
if (line.startsWith('+')) { addAnchor(newLine); out.push(`[L${newLine}] ${line}`); newLine++; }
else if (line.startsWith('-')) { out.push(`[----] ${line}`); }
else if (line.startsWith(' ')) { addAnchor(newLine); out.push(`[L${newLine}] ${line}`); newLine++; } // context line
else { out.push(line); } // trailing empty split element or unhandled metadata — never anchor/advance
}
return { annotated: out.join('\n'), anchors };
}
// Parse a persona's markdown into a summary plus line-anchored findings. Text
// before the first <!-- coc-finding ... --> marker is the summary; each marker
// plus the markdown after it (until the next marker) is one finding.
function parseFindings(text) {
const re = /<!--\s*coc-finding\s+file="([^"]*)"\s+line="(\d+)"\s*-->/g;
const matches = [];
let m;
while ((m = re.exec(text || '')) !== null) {
matches.push({ start: m.index, end: re.lastIndex, file: m[1], line: parseInt(m[2], 10) });
}
if (matches.length === 0) return { summary: (text || '').trim(), findings: [] };
const summary = text.slice(0, matches[0].start).trim();
const findings = matches.map((mm, i) => ({
file: mm.file,
line: mm.line,
body: text.slice(mm.end, i + 1 < matches.length ? matches[i + 1].start : text.length).trim(),
}));
return { summary, findings };
}
// One JSON-RPC POST. Throws on non-2xx, network error, or a JSON-RPC error
// payload (a 200 response carrying an `error` field).
async function rpc(url, token, body) {
const r = await fetch(url, {
method: 'POST',
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
body: JSON.stringify(body),
signal: AbortSignal.timeout(RPC_TIMEOUT_MS),
});
if (!r.ok) {
// Include statusText and a short body snippet — a 5xx from the gateway often
// carries a reason (e.g. upstream timeout) that the bare status code hides.
let snippet = '';
try { snippet = (await r.text()).replace(/\s+/g, ' ').trim().slice(0, 200); } catch { /* ignore */ }
throw new Error(`http ${r.status} ${r.statusText}${snippet ? ` — ${snippet}` : ''}`);
}
const json = await r.json();
if (json.error) {
throw new Error(`json-rpc error ${json.error.code ?? '?'}: ${json.error.message ?? 'unknown'}`);
}
return json;
}
// Pull the agent's text out of a completed task.
function extractText(task) {
const fromArtifacts = task?.artifacts?.[0]?.parts?.find(p => p.text)?.text;
if (fromArtifacts) return fromArtifacts;
return task?.status?.message?.parts?.find(p => p.text)?.text || '';
}
// Submit the review asynchronously and poll to completion. Returns the
// persona's markdown review, or null on failure. `maxPollMs` bounds the poll
// window (defaults to MAX_POLL_MS; the orchestrator passes a shorter per-attempt
// budget so it can afford a fresh-task retry within the job timeout).
async function reviewWithAgent({ core, title, url, token, messageText, msgId, maxPollMs = MAX_POLL_MS }) {
let taskId = null;
for (let attempt = 1; attempt <= 3; attempt++) {
try {
const sub = await rpc(url, token, {
jsonrpc: '2.0', id: msgId, method: 'message/send',
params: {
message: { messageId: msgId, role: 'user', kind: 'message', parts: [{ kind: 'text', text: messageText }] },
configuration: { blocking: false, acceptedOutputModes: ['application/json', 'text/plain'] },
},
});
taskId = sub.result?.id;
if (taskId) break;
core.info(` ${title}: submit returned no task id (attempt ${attempt})`);
} catch (e) {
core.info(` ${title}: submit attempt ${attempt} failed (${safe(errDetail(e))})`);
}
if (attempt < 3) await sleep(attempt * 5000);
}
if (!taskId) {
core.warning(`${title}: could not submit a task after 3 attempts`);
return null;
}
// Track poll outcomes so a timeout is diagnosable: a stuck task shows a
// non-terminal last state with healthy polls; a network problem shows poll errors.
const start = Date.now();
let polls = 0, pollErrs = 0, lastState = 'none';
while (Date.now() - start < maxPollMs) {
await sleep(POLL_INTERVAL_MS);
let task;
try {
const g = await rpc(url, token, { jsonrpc: '2.0', id: `${msgId}-get`, method: 'tasks/get', params: { id: taskId } });
task = g.result;
polls++;
} catch (e) {
pollErrs++;
core.info(` ${title}: poll error (${safe(errDetail(e))}), will retry`);
continue;
}
const state = task?.status?.state;
if (state) lastState = state;
if (state === 'completed') return stripOuterFence(extractText(task));
if (state === 'failed' || state === 'canceled' || state === 'rejected') {
core.warning(`${title}: task ${state}`);
return null;
}
}
core.warning(`${title}: task did not complete within ${maxPollMs / 1000}s ` +
`(last state: ${lastState}, ${polls} polls ok, ${pollErrs} poll errors)`);
return null;
}
// --- Orchestrator: cross-persona deduplication of inline comments ---
const ORCH_URL_ENV = 'ORCHESTRATOR_AGENT_URL';
const ORCH_TOKEN_ENV = 'ORCHESTRATOR_AGENT_TOKEN';
// A healthy orchestrator dedups in ~2-3 min; a much longer poll means the task
// is stuck server-side, not slow. So bound each attempt and resubmit a *fresh*
// task once — a stuck task usually doesn't recur on a new submission. Two
// attempts at this budget still fit comfortably inside the 30m job timeout
// (alongside the persona phase + posting).
const ORCH_ATTEMPTS = 2;
const ORCH_POLL_MS = 7 * 60 * 1000;
// Robustly extract the orchestrator's { clusters: [...] } JSON from its response
// (tolerates a ```json fence — single- or multi-line — or stray prose). Returns
// the parsed object or null.
function parseClusters(text) {
if (!text) return null;
const tries = [text.trim()];
const fence = text.match(/```[a-zA-Z]*\s*([\s\S]*?)```/); // handles single-line fences too
if (fence) tries.push(fence[1].trim());
const first = text.indexOf('{'), last = text.lastIndexOf('}');
if (first !== -1 && last > first) tries.push(text.slice(first, last + 1));
for (const t of tries) {
try {
const obj = JSON.parse(t);
if (obj && Array.isArray(obj.clusters)) return obj;
} catch { /* try next candidate */ }
}
return null;
}
// Pure decision logic: given the inline comments and the orchestrator's parsed
// clusters, return the SET of ids to drop. No env / network — directly unit
// testable. Defensive against arbitrary LLM output: honor a cluster only if its
// survivor is a known id; drop only known duplicate ids; keep-wins (an id that
// survives anywhere is never dropped); anything not mentioned is implicitly kept.
function applyClusters({ core, allInline, parsed }) {
const known = new Set(allInline.map(c => c.id));
const survivors = new Set();
const candidates = new Set();
for (const cl of parsed.clusters) {
// Every field here is LLM-controlled / possibly garbage — guard each access.
if (!cl || typeof cl.survivor !== 'string' || !known.has(cl.survivor)) continue;
survivors.add(cl.survivor);
const dups = Array.isArray(cl.duplicates) ? cl.duplicates.filter(d => known.has(d)) : [];
dups.forEach(d => candidates.add(d));
if (dups.length) core.info(` orchestrator: keep ${cl.survivor}, drop [${dups.join(', ')}] — ${safe(cl.reason)}`);
}
// The prompt says each id appears at most once; if the model listed an id as
// both a survivor and a duplicate, keep-wins silently saves it — warn so a
// misbehaving orchestrator is visible rather than silently absorbed.
const conflicts = [...candidates].filter(id => survivors.has(id));
if (conflicts.length) core.warning(`Orchestrator: ${conflicts.length} id(s) listed as both survivor and duplicate (kept via keep-wins): ${conflicts.join(', ')}`);
const drop = new Set([...candidates].filter(id => !survivors.has(id)));
core.info(`Orchestrator: dropping ${drop.size} duplicate comment(s) of ${allInline.length} inline`);
return drop;
}
// Ask the orchestrator which inline comments are redundant duplicates and return
// the SET of comment ids to drop. Lossless by construction: returns an empty set
// (drop nothing → post everything) if the orchestrator is unconfigured, errors,
// times out, or returns anything we can't safely apply. The try/catch makes the
// "post everything on failure" guarantee robust even if a callee starts throwing.
async function orchestrateDedup({ core, allInline }) {
const empty = new Set();
const url = process.env[ORCH_URL_ENV];
const token = process.env[ORCH_TOKEN_ENV];
if (!url || !token) {
core.info(`Orchestrator not configured (${ORCH_URL_ENV}/${ORCH_TOKEN_ENV} unset) — posting all comments`);
return empty;
}
if (allInline.length < 2) return empty; // nothing to dedupe (need ≥2 comments)
try {
const payload = JSON.stringify(allInline.map(c =>
({ id: c.id, persona: c.persona, file: c.file, line: c.line, body: c.body })));
const messageText =
`Here are ${allInline.length} inline review comments from one pull request, as a JSON array. ` +
`Identify redundant duplicate groups and return the clusters JSON per your instructions.\n\n${payload}`;
// Up to ORCH_ATTEMPTS, each a *fresh* task bounded by ORCH_POLL_MS — so a
// task that hangs server-side OR returns garbled output gets a second shot.
const runKey = `${process.env.GITHUB_RUN_ID}-${process.env.GITHUB_RUN_ATTEMPT || '1'}`;
for (let attempt = 1; attempt <= ORCH_ATTEMPTS; attempt++) {
// RUN_ATTEMPT + per-attempt suffix → a distinct msgId (hence a fresh
// server-side task) on every submission, including GitHub "Re-run" re-runs.
const msgId = `council-orchestrator-${runKey}-${attempt}`;
const out = await reviewWithAgent({
core, title: `Orchestrator (attempt ${attempt}/${ORCH_ATTEMPTS})`,
url, token, messageText, msgId, maxPollMs: ORCH_POLL_MS,
});
const last = attempt === ORCH_ATTEMPTS;
if (!out) {
if (!last) core.info('Orchestrator: no response — resubmitting a fresh task');
continue;
}
const parsed = parseClusters(out);
if (!parsed) {
if (!last) core.info('Orchestrator: unparseable response — resubmitting a fresh task');
continue;
}
return applyClusters({ core, allInline, parsed });
}
core.warning('Orchestrator: no usable response after retries — posting all comments unfiltered');
return empty;
} catch (e) {
core.warning(`Orchestrator: unexpected error (${safe(errDetail(e))}) — posting all comments unfiltered`);
return empty;
}
}
module.exports = async ({ github, context, core }) => {
const { owner, repo } = context.repo;
const pr = context.payload.pull_request;
if (!pr) {
core.warning('No pull_request in payload, skipping');
return;
}
const pull_number = pr.number;
const commit_id = pr.head.sha;
// 1. Determine which personas are actually configured.
const active = PERSONAS
.map(p => ({ ...p, url: process.env[p.urlEnv], token: process.env[p.tokenEnv] }))
.filter(p => {
if (p.url && p.token) return true;
core.warning(`${p.title}: ${p.urlEnv}/${p.tokenEnv} not set, skipping persona`);
return false;
});
if (active.length === 0) {
core.warning('No persona agents configured, nothing to do');
return;
}
// 2. Fetch the unified diff inline (size-guarded), then annotate with line
// numbers and build the anchorable-line index.
const diffResp = await github.rest.pulls.get({
owner, repo, pull_number, mediaType: { format: 'diff' },
});
let diff = diffResp.data;
let truncated = false;
if (Buffer.byteLength(diff, 'utf8') > MAX_DIFF_BYTES) {
const buf = Buffer.from(diff, 'utf8').subarray(0, MAX_DIFF_BYTES);
diff = new TextDecoder('utf-8').decode(buf, { stream: true });
truncated = true;
core.warning(`diff exceeds ${MAX_DIFF_BYTES} bytes, truncated (large-PR handling out of scope)`);
}
const { annotated, anchors } = annotateDiff(diff);
// Ground the review in Calico's own context files, scoped to the paths this PR
// touches. Parse both new (+++ b/) and old (--- a/) file headers so delete-only
// changes still match path-scoped context; /dev/null headers lack the a//b/
// prefix and are naturally excluded.
const touchedPaths = new Set(
(diff.match(/^(?:\+\+\+ b|--- a)\/.+$/gm) || []).map(l => l.replace(/^(?:\+\+\+ b|--- a)\//, '')),
);
const projectContext = gatherContext(core, touchedPaths);
const messageText = projectContext +
`PR #${pull_number}: ${pr.title}\n\n` +
`Description:\n${pr.body || '(none)'}\n\n` +
(truncated ? '(NOTE: the diff below was truncated for size.)\n\n' : '') +
`The unified diff below is annotated: each new-side line is prefixed with its line number ` +
`as [L<n>]. When a finding is tied to a specific line, cite that number in a coc-finding anchor.\n\n` +
`Annotated unified diff:\n${annotated}`;
// 3. Fan out to all configured personas in parallel; isolate failures.
core.info(`Reviewing PR #${pull_number} with ${active.length} persona(s): ${active.map(p => p.title).join(', ')}`);
const results = await Promise.all(active.map(async p => {
// Include RUN_ATTEMPT so a GitHub "Re-run" gets a fresh server-side task (the
// run id alone is stable across re-runs and could return a stale cached task).
const msgId = `council-${p.key}-${pull_number}-${process.env.GITHUB_RUN_ID}-${process.env.GITHUB_RUN_ATTEMPT || '1'}`;
const review = await reviewWithAgent({ core, title: p.title, url: p.url, token: p.token, messageText, msgId });
if (!review) {
core.warning(`${p.title}: no review produced, skipping`);
return null;
}
core.info(`${p.title}: received ${review.length} chars of feedback`);
return { persona: p, review };
}));
// 4. Fetch existing reviews (for sticky summaries) and existing review
// comments (for inline cleanup) once. A comment "has replies" if some
// other comment is in_reply_to it — we never delete those.
const existingReviews = await github.paginate(github.rest.pulls.listReviews, { owner, repo, pull_number });
const existingComments = await github.paginate(github.rest.pulls.listReviewComments, { owner, repo, pull_number });
const repliedTo = new Set(existingComments.map(c => c.in_reply_to_id).filter(id => id != null));
// 4a. Parse + classify each persona's findings. Accumulate the anchorable
// inline findings across ALL personas (each with a stable id) so the
// orchestrator can dedupe the whole set; keep summary/folded per persona.
const perPersona = [];
const allInline = [];
for (const res of results) {
if (!res) continue;
const p = res.persona;
// Persona badge: a custom avatar image if provided, else the emoji.
const badge = p.image ? `<img src="${p.image}" width="20" align="top" alt="${p.title}">` : p.emoji;
const inlineMarker = `<!-- council-of-claudes:inline:${p.key} -->`;
const { summary, findings } = parseFindings(res.review);
const folded = [];
const anchorable = [];
findings.forEach((f, i) => {
if (anchors.get(f.file)?.has(f.line)) {
// Run-scoped ephemeral id: generated and consumed within this single
// invocation (sent to the orchestrator, matched against its drop set).
// `i` is the index into `findings` (gaps where entries were folded are
// harmless — ids only need per-persona uniqueness); never persisted, so
// it must not be correlated across runs or against existingComments.
const id = `${p.key}-${i}`;
anchorable.push({ id, ...f });
allInline.push({ id, persona: p.title, file: f.file, line: f.line, body: f.body });
} else {
folded.push(f); // no valid line anchor → goes to the summary
}
});
perPersona.push({ p, badge, inlineMarker, summary, folded, anchorable });
}
// 4b. Orchestrator: dedupe the combined inline set → ids to drop. Lossless on
// any failure / unconfigured (returns empty → post everything).
const drop = await orchestrateDedup({ core, allInline });
// 4c. Post per persona: delete prior no-reply inline, post surviving inline
// (skipping orchestrator-dropped duplicates), then the sticky summary.
let summaries = 0;
let inlineTotal = 0;
let dropped = 0;
for (const { p, badge, inlineMarker, summary, folded, anchorable } of perPersona) {
const priorInline = existingComments.filter(c => (c.body || '').includes(inlineMarker));
for (const c of priorInline) {
if (repliedTo.has(c.id)) continue; // keep — has a discussion thread
try {
await github.rest.pulls.deleteReviewComment({ owner, repo, comment_id: c.id });
} catch (e) {
core.info(` ${p.title}: could not delete inline #${c.id} (${safe(errDetail(e))})`);
}
}
let inline = 0;
for (const f of anchorable) {
if (drop.has(f.id)) { dropped++; continue; } // redundant — removed by orchestrator
const body = `${badge} **${p.title}** — ${f.body}\n\n${inlineMarker}`;
try {
await github.rest.pulls.createReviewComment({
owner, repo, pull_number, commit_id, path: f.file, line: f.line, side: 'RIGHT', body,
});
inline++;
} catch (e) {
// Line wasn't commentable after all — fold it into the summary so it isn't lost.
core.info(` ${p.title}: inline anchor failed at ${safe(f.file)}:${f.line} (${safe(errDetail(e))}), folding into summary`);
folded.push(f);
}
}
inlineTotal += inline;
// Summary: sticky review, updated in place. Folded findings (no valid line
// anchor) are listed here so nothing is lost.
const marker = `<!-- council-of-claudes:${p.key} -->`;
let body =
`### ${badge} Council of Claudes — ${p.title}\n\n` +
`> [!${p.accent}]\n> **${p.title} lens** · ${p.tagline}\n\n` +
`${summary || '_No summary provided._'}\n`;
if (folded.length) {
body += `\n**Other notes** (not anchored to a diff line):\n` +
folded.map(f => `- \`${f.file}:${f.line}\` — ${f.body.replace(/\s*\n+\s*/g, ' ')}`).join('\n') + '\n';
}
body += `\n<sub>🤖 Council of Claudes · ${inline} inline comment(s)</sub>\n${marker}`;
const prior = existingReviews.filter(r => (r.body || '').includes(marker)).pop();
try {
if (prior) {
await github.rest.pulls.updateReview({ owner, repo, pull_number, review_id: prior.id, body });
core.info(`${p.title}: summary updated in place (#${prior.id}), ${inline} inline`);
} else {
await github.rest.pulls.createReview({ owner, repo, pull_number, commit_id, event: 'COMMENT', body });
core.info(`${p.title}: summary posted (new), ${inline} inline`);
}
summaries++;
} catch (e) {
core.warning(`${p.title}: failed to post/update summary (${safe(errDetail(e))})`);
}
}
core.info(`Done: ${summaries}/${active.length} summaries, ${inlineTotal} inline posted, ${dropped} duplicate(s) dropped on PR #${pull_number}`);
};