T3 messages with role reasoning and an id starting "reasoning:" are now skipped before any other check and counted as reasoningMessages. Any other unknown role, including reasoning with another id prefix, still refuses. Issues updated since the start date are read in pages of 50 until a short page, at most 10 pages. A full tenth page, a duplicate issue number across pages, a page over 50 rows or any failed page refuses with exit 2, so partial totals are never reported. The report prints how many pages it read. The README describes the skip, the page loop and its refusals. Candidate manifest 4eed1d6405a505e88cde5062c61f768639d899b8f301019b1403d68fb15d9d36 (Darkwing round 1, packetd6ea56831), BASEcc48b124f. Approved by Filbert (comment 28407). Sage gate rerun at644ef5310plus the candidate passed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
255 lines
13 KiB
JavaScript
255 lines
13 KiB
JavaScript
import { execFileSync } from 'node:child_process';
|
|
import { createReadStream } from 'node:fs';
|
|
import { readdir, lstat } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
|
|
const DAY = 86400000;
|
|
export const UNKNOWN = 'unknown';
|
|
export class SourceError extends Error {
|
|
constructor(message, exitCode = 1) { super(message); this.exitCode = exitCode; }
|
|
}
|
|
|
|
export function dateRange(since, until = new Date().toISOString().slice(0, 10)) {
|
|
const parse = value => {
|
|
const ms = Date.parse(`${value}T00:00:00Z`);
|
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(value ?? '') || !Number.isFinite(ms) ||
|
|
new Date(ms).toISOString().slice(0, 10) !== value) {
|
|
throw new SourceError('Dates must be valid YYYY-MM-DD values');
|
|
}
|
|
return ms;
|
|
};
|
|
const start = parse(since), end = parse(until) + DAY;
|
|
if (end <= start) throw new SourceError('--until must not precede --since');
|
|
return { since, until, start, end };
|
|
}
|
|
export const inRange = (value, range) => {
|
|
const ms = typeof value === 'number' ? value : Date.parse(value);
|
|
return Number.isFinite(ms) && ms >= range.start && ms < range.end;
|
|
};
|
|
export const issueNumbers = text => [...new Set(
|
|
[...text.matchAll(/(?:^|[^\w])#([1-9]\d*)\b/g)].map(m => Number(m[1]))
|
|
)].filter(Number.isSafeInteger);
|
|
|
|
export function readCommits(root, range) {
|
|
let output;
|
|
try {
|
|
// Filter ourselves: git --since can prune history at an out-of-order date.
|
|
output = execFileSync('git', ['log', 'refactor', '--format=%H%x00%ct%x00%s'],
|
|
{ cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
} catch { throw new SourceError('Cannot read local refactor git log'); }
|
|
return output.split('\n').filter(Boolean).flatMap(line => {
|
|
const [hash, seconds, subject] = line.split('\0');
|
|
const issues = issueNumbers(subject ?? '');
|
|
return inRange(Number(seconds) * 1000, range) && issues.length ? [{ hash, issues }] : [];
|
|
});
|
|
}
|
|
|
|
// Gitea returns at most 50 issues a page. A page with fewer ends the read.
|
|
const PAGE_SIZE = 50, PAGE_CEILING = 10;
|
|
function readIssuePage(root, range, page, tool) {
|
|
let output;
|
|
try {
|
|
// Updated-since includes closes in range. No until filter: later updates must
|
|
// not hide a close in range.
|
|
output = execFileSync(tool, ['GET', `repos/mosaicstack/stack/issues?state=all&type=issues&since=${encodeURIComponent(new Date(range.start).toISOString())}&limit=${PAGE_SIZE}&page=${page}`],
|
|
{ cwd: root, encoding: 'utf8', timeout: 60000, maxBuffer: 16 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
} catch (error) {
|
|
// Never echo an arbitrary API response or stderr that might contain secrets.
|
|
const reason = error.status === 3 ? 'credentials missing, unreadable, or invalid' :
|
|
error.code === 'ENOENT' ? 'gitea-api.sh unavailable' : 'credential or Gitea request failure';
|
|
throw new SourceError(`Issues unavailable: ${reason} on page ${page}; use --no-issues for unknown issue metrics`, 2);
|
|
}
|
|
let issues;
|
|
try { issues = JSON.parse(output); } catch { throw new SourceError(`Issues unavailable: invalid Gitea JSON on page ${page}`, 2); }
|
|
if (!Array.isArray(issues) || issues.length > PAGE_SIZE || issues.some(i => !i || !Number.isSafeInteger(i.number) || i.number < 1 ||
|
|
typeof i.title !== 'string' || !Number.isFinite(Date.parse(i.created_at)) ||
|
|
!(i.closed_at === null || Number.isFinite(Date.parse(i.closed_at))))) {
|
|
throw new SourceError(`Issues unavailable: invalid Gitea issue records on page ${page}`, 2);
|
|
}
|
|
return issues;
|
|
}
|
|
// Reads pages 1, 2, 3 and on until one has fewer than 50 issues, at most 10.
|
|
// A full page 10 refuses: the totals would be partial. So does a number seen
|
|
// twice, since an update during the read can move an issue to the next page.
|
|
export function readIssues(root, range, tool = 'gitea-api.sh') {
|
|
const issues = [], seen = new Set();
|
|
for (let page = 1; page <= PAGE_CEILING; page++) {
|
|
const rows = readIssuePage(root, range, page, tool);
|
|
for (const issue of rows) {
|
|
if (seen.has(issue.number)) throw new SourceError(`Issues unavailable: duplicate issue number on page ${page}; rerun the report`, 2);
|
|
seen.add(issue.number);
|
|
issues.push(issue);
|
|
}
|
|
if (rows.length < PAGE_SIZE) return { issues: issues.filter(i => !i.pull_request), pages: page };
|
|
}
|
|
throw new SourceError(`Issues unavailable: page ${PAGE_CEILING} is full, so the ${PAGE_CEILING}-page ceiling was reached and totals may be incomplete. Narrow --since or use --no-issues`, 2);
|
|
}
|
|
|
|
export function messageText(content) {
|
|
if (typeof content === 'string') return content;
|
|
if (Array.isArray(content)) return content.filter(c => c?.type === 'text' && typeof c.text === 'string').map(c => c.text).join('\n');
|
|
return '';
|
|
}
|
|
// Classes are matched in either case: seats send DECISION and REVIEW-REQUEST.
|
|
// tmux preamble from agent-send.sh: [host:session -> host:session class=x]
|
|
const TMUX = /^\[([^\s:\[\]]+):([^\s\[\]]+) -> ([^\s:\[\]]+):([^\s\[\]]+)(?: class=[A-Za-z-]+)?\](?:\s|$)/;
|
|
// T3 header (docs/guides/T3-AGENT-COMMS.md): [from: role (id) -> to: role (id) class=x]
|
|
const T3 = /^\[from: ([^\s()\[\]]+) \(([^()\[\]]+)\) -> to: ([^\s()\[\]]+) \(([^()\[\]]+)\)(?: class=[A-Za-z-]+)?\](?:\s|$)/;
|
|
const firstLine = text => text.split(/\r?\n/, 1)[0];
|
|
export function t3Header(text) {
|
|
const m = firstLine(text).match(T3);
|
|
return m ? { from: m[1], fromId: m[2], to: m[3], toId: m[4] } : null;
|
|
}
|
|
export function messageKind(text) {
|
|
const tmux = firstLine(text).match(TMUX), t3 = t3Header(text);
|
|
const sender = tmux ? tmux[2] : t3 ? t3.from : null;
|
|
return sender === null ? 'human' : sender === 'control-board' ? 'board' : 'agent';
|
|
}
|
|
// JSONL lines end at \n only. readline also ends a line at U+2028, which JSON
|
|
// allows raw inside a string, so it split valid records (Node 26.8.1).
|
|
async function* jsonLines(input) {
|
|
let rest = '';
|
|
for await (const chunk of input) {
|
|
const parts = (rest + chunk).split('\n');
|
|
rest = parts.pop();
|
|
yield* parts;
|
|
}
|
|
if (rest) yield rest;
|
|
}
|
|
async function directories(dir, optional = false) {
|
|
try {
|
|
if (!(await lstat(dir)).isDirectory()) throw new SourceError('Session source must be a real directory');
|
|
return await readdir(dir, { withFileTypes: true });
|
|
} catch (error) {
|
|
if (optional && error.code === 'ENOENT') return [];
|
|
throw new SourceError(`Cannot read ledger directory: ${dir}`);
|
|
}
|
|
}
|
|
// Seats are the real directories in agents/, sorted.
|
|
export async function readSeats(root) {
|
|
return (await directories(path.join(root, 'agents'))).filter(e => e.isDirectory()).map(e => e.name).sort((a, b) => a.localeCompare(b));
|
|
}
|
|
export async function readSessions(root, range) {
|
|
const rows = [];
|
|
const mentions = new Map();
|
|
// No symlink traversal, no fleet paths, no transcript content in the report.
|
|
const agents = (await readSeats(root)).map(name => ({ name }));
|
|
const state = path.join(root, '.pi', 'state');
|
|
// Check every source ancestor, not only the leaf directory.
|
|
if (!(await directories(path.join(root, '.pi'), true)).length) return { rows, mentions };
|
|
await directories(state, true);
|
|
for (const agent of agents) {
|
|
const seatRoot = path.join(state, agent.name);
|
|
await directories(seatRoot, true);
|
|
const dir = path.join(seatRoot, 'sessions');
|
|
const files = (await directories(dir, true)).filter(e => e.isFile() && e.name.endsWith('.jsonl'));
|
|
const row = { seat: agent.name, board: 0, agent: 0, human: 0 };
|
|
for (const file of files) {
|
|
const input = createReadStream(path.join(dir, file.name), { encoding: 'utf8' });
|
|
let lineNumber = 0;
|
|
try {
|
|
for await (const line of jsonLines(input)) {
|
|
lineNumber++;
|
|
if (!line.trim()) continue;
|
|
let entry;
|
|
try { entry = JSON.parse(line); }
|
|
catch { throw new SourceError(`Malformed session JSON: ${agent.name}/${file.name}:${lineNumber}`); }
|
|
if (entry?.type !== 'message' || entry.message?.role !== 'user') continue;
|
|
const stamp = entry.timestamp ?? entry.message.timestamp;
|
|
if (!Number.isFinite(typeof stamp === 'number' ? stamp : Date.parse(stamp))) {
|
|
throw new SourceError(`Invalid user-message timestamp: ${agent.name}/${file.name}:${lineNumber}`);
|
|
}
|
|
if (!inRange(stamp, range)) continue;
|
|
const text = messageText(entry.message.content);
|
|
row[messageKind(text)]++;
|
|
for (const number of issueNumbers(text)) {
|
|
if (!mentions.has(number)) mentions.set(number, new Set());
|
|
mentions.get(number).add(agent.name);
|
|
}
|
|
}
|
|
} finally { input.destroy(); }
|
|
}
|
|
if (row.board + row.agent + row.human) rows.push(row);
|
|
}
|
|
return { rows, mentions };
|
|
}
|
|
// Adds T3 counts to the Pi rows per seat. Unmapped T3 threads get one row,
|
|
// last. The report keeps the Pi rows and the T3 section so the split shows.
|
|
export function mergeSources(pi, t3, unmapped = 't3:unmapped') {
|
|
if (!t3) return { rows: pi.rows, mentions: pi.mentions, pi: pi.rows, t3: { read: false } };
|
|
const bySeat = new Map(pi.rows.map(r => [r.seat, { ...r }]));
|
|
for (const [seat, counts] of t3.rows) {
|
|
if (seat === unmapped || !(counts.board + counts.agent + counts.human)) continue;
|
|
const row = bySeat.get(seat) ?? { seat, board: 0, agent: 0, human: 0 };
|
|
for (const kind of ['board', 'agent', 'human']) row[kind] += counts[kind];
|
|
bySeat.set(seat, row);
|
|
}
|
|
const rows = [...bySeat.values()].sort((a, b) => a.seat.localeCompare(b.seat));
|
|
const extra = t3.rows.get(unmapped);
|
|
if (extra.board + extra.agent + extra.human) rows.push({ seat: unmapped, ...extra });
|
|
const mentions = new Map([...pi.mentions].map(([n, seats]) => [n, new Set(seats)]));
|
|
for (const [n, seats] of t3.mentions) {
|
|
if (!mentions.has(n)) mentions.set(n, new Set());
|
|
for (const seat of seats) mentions.get(n).add(seat);
|
|
}
|
|
return { rows, mentions, pi: pi.rows, t3: t3.section };
|
|
}
|
|
const round = value => Math.round(value * 10) / 10;
|
|
function duration(issue) {
|
|
if (!issue) return UNKNOWN;
|
|
if (!issue.closed_at) return 'open';
|
|
const hours = (Date.parse(issue.closed_at) - Date.parse(issue.created_at)) / 3600000;
|
|
return hours >= 0 ? hours : UNKNOWN;
|
|
}
|
|
export function summarize(range, commits, issues, sessions) {
|
|
const byNumber = new Map((issues ?? []).map(i => [i.number, i]));
|
|
const counts = new Map();
|
|
for (const commit of commits) for (const n of commit.issues) counts.set(n, (counts.get(n) ?? 0) + 1);
|
|
const closed = (issues ?? []).filter(i => i.closed_at && inRange(i.closed_at, range));
|
|
const touched = new Set([...counts.keys(), ...closed.map(i => i.number)]);
|
|
const rows = [...touched].sort((a, b) => a - b).map(number => {
|
|
const issue = byNumber.get(number), count = counts.get(number) ?? 0;
|
|
const hours = duration(issue);
|
|
return { issue: number, title: issue?.title ?? UNKNOWN, opened: issue?.created_at ?? UNKNOWN,
|
|
hoursOpen: typeof hours === 'number' ? round(hours) : hours, commits: count,
|
|
followUps: Math.max(0, count - 1), seats: [...(sessions.mentions.get(number) ?? [])].sort() };
|
|
});
|
|
const hours = closed.map(duration).sort((a, b) => a - b);
|
|
const middle = Math.floor(hours.length / 2);
|
|
const median = hours.includes(UNKNOWN) ? UNKNOWN : hours.length ?
|
|
round(hours.length % 2 ? hours[middle] : (hours[middle - 1] + hours[middle]) / 2) : 0;
|
|
const human = sessions.rows.reduce((sum, r) => sum + r.human, 0);
|
|
const sources = sessions.t3 ? { pi: sessions.pi, t3: sessions.t3 } : {};
|
|
return { since: range.since, until: range.until, timezone: 'UTC', issues: rows, seats: sessions.rows, ...sources,
|
|
totals: { issuesClosed: issues === null ? UNKNOWN : closed.length,
|
|
medianHoursOpen: issues === null ? UNKNOWN : median, commits: commits.length,
|
|
followUpsPerIssue: rows.length ? round(rows.reduce((sum, r) => sum + r.followUps, 0) / rows.length) : 0,
|
|
humanMessagesPerClosedIssue: issues === null ? UNKNOWN : closed.length ? round(human / closed.length) : human ? UNKNOWN : 0 } };
|
|
}
|
|
export const clean = value => String(value).replace(/[\x00-\x1f\x7f-\x9f]/g, ' ');
|
|
const decimal = value => typeof value === 'number' ? value.toFixed(1) : value;
|
|
export function totalsLine(t) {
|
|
return `Totals: issues closed ${t.issuesClosed} | median hours open ${decimal(t.medianHoursOpen)} | commits ${t.commits} | follow-ups per issue ${decimal(t.followUpsPerIssue)} | human messages per closed issue ${decimal(t.humanMessagesPerClosedIssue)}`;
|
|
}
|
|
export function formatTable(report) {
|
|
return [`Ledger ${report.since} through ${report.until} UTC`,
|
|
'Issue | Title | Opened | Hours open | Commits | Follow-ups | Seats',
|
|
...report.issues.map(r => [`#${r.issue}`, clean(r.title).slice(0, 48), r.opened,
|
|
decimal(r.hoursOpen), r.commits, r.followUps, r.seats.join(', ')].join(' | ')),
|
|
'', 'Seat | Board | Agent | Human',
|
|
...report.seats.map(r => [clean(r.seat), r.board, r.agent, r.human].join(' | ')),
|
|
...t3Line(report.t3), ...issueLine(report.issueSource),
|
|
'', totalsLine(report.totals)].join('\n');
|
|
}
|
|
// How many issue pages the report read, so a short read can't pass unseen.
|
|
function issueLine(source) {
|
|
if (!source) return [];
|
|
return [source.read ? `Issues: ${source.pages} ${source.pages === 1 ? 'page' : 'pages'} read from Gitea` : 'Issues: not read (--no-issues)'];
|
|
}
|
|
// One line when T3 was skipped or read from somewhere other than the default.
|
|
function t3Line(t3) {
|
|
if (!t3) return [];
|
|
if (!t3.read) return ['T3: not read (--no-t3)'];
|
|
return t3.database.default ? [] : [`T3: read from ${clean(t3.database.path)}, not the default`];
|
|
}
|