Skip to content

Commit cf02a64

Browse files
history: Store compact per-item AIC totals (#101)
Consumers need durable issue and pull-request AIC attribution, not a copy of metadata already available from GitHub. Keep detailed records limited to identity and usage while preserving full activity aggregates and auditable run evidence. Assisted-by: AI Signed-off-by: Colin Walters <walters@verbum.org> Co-authored-by: Colin Walters <walters@verbum.org>
1 parent d05d91c commit cf02a64

3 files changed

Lines changed: 76 additions & 62 deletions

File tree

‎scripts/README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ node scripts/org-history.js --previous-iso-week
1818

1919
Schema v5 retains the v4 period field names and changes detailed `items`: it contains
2020
only items with exact evidence linking them to a relevant workflow run with known AIC.
21-
Each detailed item has sorted, unique `aicRunIds`. Repository aggregates still describe
21+
Each record contains only `repository`, `number`, `type`, `aic`, and sorted, unique
22+
`aicRunIds`; generic GitHub metadata is intentionally omitted. `aic` is the normalized
23+
sum for its linked runs, including zero. A run linked exactly to multiple items contributes
24+
its full AIC to each association, so item AIC values are not additive; use
25+
`coverage.aic.total` for the organization-wide total. Repository aggregates still describe
2226
the full collected activity cohort, not just these AIC-linked detailed items; the linked
2327
run evidence is serialized for audit.
2428

‎scripts/org-history.js‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const path = require('path');
1111

1212
const SCHEMA_VERSION = 5;
1313
const AGENT_LABELS = ['agent/code', 'agent/fixme', 'agent/lgtm'];
14+
const DETAILED_ITEM_FIELDS = ['repository', 'number', 'type', 'aic', 'aicRunIds'];
1415
const AGGREGATE_USAGE_FILE = 'agent_usage.json';
1516
const TOKEN_USAGE_FILE = 'agent/token_usage.jsonl';
1617

@@ -326,7 +327,10 @@ function aicLinkedItems(items, runs, commentAic, org) {
326327
ids.add(runId);
327328
links.set(item, ids);
328329
};
329-
for (const evidence of commentAic) link(evidence.repository, evidence.itemNumber, evidence.runId);
330+
for (const evidence of commentAic) {
331+
const item = itemsByKey.get(`${evidence.repository}/${evidence.itemNumber}`);
332+
if (item && evidence.itemUrl === item.url) link(evidence.repository, evidence.itemNumber, evidence.runId);
333+
}
330334
for (const run of runById.values()) {
331335
for (const pullRequest of normalizedPullRequests(run, org, run.repository)) {
332336
const candidate = itemsByKey.get(`${run.repository}/${pullRequest.number}`);
@@ -335,26 +339,43 @@ function aicLinkedItems(items, runs, commentAic, org) {
335339
}
336340
}
337341
}
338-
return [...links.entries()].map(([item, runIds]) => ({ ...item, aicRunIds: [...runIds].sort((a, b) => a - b) }))
342+
return [...links.entries()].map(([item, runIds]) => {
343+
const aicRunIds = [...runIds].sort((a, b) => a - b);
344+
return {
345+
repository: item.repository,
346+
number: item.number,
347+
type: item.type,
348+
aic: normalizeAic(aicRunIds.reduce((total, runId) => total + runById.get(runId).aic, 0)),
349+
aicRunIds,
350+
};
351+
})
339352
.sort((a, b) => a.repository.localeCompare(b.repository) || a.number - b.number);
340353
}
341354

342355
function validateDetailedItems(items, workflowRuns, commentAic, org) {
343-
const runById = new Map(workflowRuns.map((run) => [run.id, run]));
356+
const identities = new Set();
344357
for (const item of items) {
345-
if (!Array.isArray(item.aicRunIds) || !item.aicRunIds.length) throw new Error(`Detailed item ${item.repository}#${item.number} has no AIC runs`);
346-
const itemPath = item.type === 'pull_request' ? 'pull' : item.type === 'issue' ? 'issues' : null;
347-
const canonicalItemUrl = itemPath && `https://github.com/${org}/${item.repository}/${itemPath}/${item.number}`;
348-
if (item.url !== canonicalItemUrl) throw new Error(`Detailed item ${item.repository}#${item.number} has a mismatched canonical URL`);
358+
if (!item || typeof item !== 'object' || Array.isArray(item) || Object.keys(item).length !== DETAILED_ITEM_FIELDS.length || Object.keys(item).some((field) => !DETAILED_ITEM_FIELDS.includes(field))) throw new Error('Detailed item has unexpected fields');
359+
if (typeof item.repository !== 'string' || !/^[A-Za-z0-9_.-]+$/.test(item.repository) || !Number.isSafeInteger(item.number) || item.number <= 0 || !['issue', 'pull_request'].includes(item.type)) throw new Error('Detailed item has an invalid identity');
360+
const identity = `${item.repository}/${item.number}`;
361+
if (identities.has(identity)) throw new Error(`Detailed item ${item.repository}#${item.number} is duplicated`);
362+
identities.add(identity);
363+
if (!finiteNonnegative(item.aic)) throw new Error(`Detailed item ${item.repository}#${item.number} has invalid AIC`);
364+
if (!Array.isArray(item.aicRunIds) || !item.aicRunIds.length || item.aicRunIds.some((runId) => !Number.isSafeInteger(runId) || runId <= 0)) throw new Error(`Detailed item ${item.repository}#${item.number} has no valid AIC runs`);
349365
const sortedUnique = [...new Set(item.aicRunIds)].sort((a, b) => a - b);
350366
if (JSON.stringify(item.aicRunIds) !== JSON.stringify(sortedUnique)) throw new Error(`Detailed item ${item.repository}#${item.number} has unsorted or duplicate AIC runs`);
367+
const itemPath = item.type === 'pull_request' ? 'pull' : 'issues';
368+
const canonicalItemUrl = `https://github.com/${org}/${item.repository}/${itemPath}/${item.number}`;
369+
let total = 0;
351370
for (const runId of item.aicRunIds) {
352-
const run = runById.get(runId);
371+
const run = workflowRuns.find((candidate) => candidate && candidate.id === runId && candidate.repository === item.repository);
353372
if (!run || run.repository !== item.repository || !finiteNonnegative(run.aic)) throw new Error(`Detailed item ${item.repository}#${item.number} references unknown-AIC run ${runId}`);
354-
const commentEvidence = (commentAic || []).some((record) => record.repository === item.repository && record.runId === runId && record.itemNumber === item.number && record.itemUrl === item.url);
355-
const pullRequestEvidence = item.type === 'pull_request' && (run.pullRequests || []).some((reference) => reference.number === item.number && reference.url === item.url);
373+
total += run.aic;
374+
const commentEvidence = (commentAic || []).some((record) => record.repository === item.repository && record.runId === runId && record.itemNumber === item.number && record.itemUrl === canonicalItemUrl);
375+
const pullRequestEvidence = item.type === 'pull_request' && (run.pullRequests || []).some((reference) => reference.number === item.number && reference.url === canonicalItemUrl);
356376
if (!commentEvidence && !pullRequestEvidence) throw new Error(`Detailed item ${item.repository}#${item.number} lacks exact evidence for run ${runId}`);
357377
}
378+
if (item.aic !== normalizeAic(total)) throw new Error(`Detailed item ${item.repository}#${item.number} has an incorrect AIC total`);
358379
}
359380
}
360381

‎tests/org-history.test.js‎

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,76 +53,65 @@ test('supports deterministic as-of week selection from the CLI', () => {
5353
assert.throws(() => childProcess.execFileSync(process.execPath, [script, '--previous-iso-week', '--as-of', 'not-a-date'], { encoding: 'utf8' }));
5454
});
5555

56-
test('links detailed items only through exact known-AIC comment or PR evidence', () => {
56+
test('projects compact AIC-linked items from exact known-AIC evidence', () => {
5757
const items = [
58-
{ repository: 'repo', number: 1, type: 'issue', url: 'https://github.com/org/repo/issues/1' },
59-
{ repository: 'repo', number: 2, type: 'pull_request', url: 'https://github.com/org/repo/pull/2' },
58+
{ repository: 'repo', number: 1, type: 'issue', url: 'https://github.com/org/repo/issues/1', updatedAt: 'metadata' },
59+
{ repository: 'repo', number: 2, type: 'pull_request', url: 'https://github.com/org/repo/pull/2', title: 'metadata' },
6060
{ repository: 'repo', number: 3, type: 'issue', url: 'https://github.com/org/repo/issues/3' },
6161
];
6262
const run = (id, aic, pullRequests = []) => ({ id, repository: 'repo', kind: 'drafter', aic, pull_requests: pullRequests });
6363
const pull = (number, repository = 'repo') => ({ url: `https://api.github.com/repos/org/${repository}/pulls/${number}` });
64+
const comment = (runId, itemNumber) => ({ repository: 'repo', runId, itemNumber, itemUrl: items.find((item) => item.number === itemNumber).url });
6465
for (const scenario of [
65-
{ name: 'comment', runs: [run(10, 1)], comments: [{ repository: 'repo', runId: 10, itemNumber: 1 }], expected: [[1, [10]]] },
66-
{ name: 'pull request', runs: [run(11, 2, [pull(2)])], comments: [], expected: [[2, [11]]] },
67-
{ name: 'unrelated evidence', runs: [run(12, 3, [pull(3, 'other')]), run(13, null, [pull(2)])], comments: [{ repository: 'repo', runId: 12, itemNumber: 99 }], expected: [] },
68-
{ name: 'deduplicates links', runs: [run(14, 4, [pull(2), pull(2)])], comments: [{ repository: 'repo', runId: 14, itemNumber: 2 }, { repository: 'repo', runId: 14, itemNumber: 2 }], expected: [[2, [14]]] },
69-
{ name: 'keeps multiple comment targets', runs: [run(16, 5)], comments: [{ repository: 'repo', runId: 16, itemNumber: 1 }, { repository: 'repo', runId: 16, itemNumber: 3 }], expected: [[1, [16]], [3, [16]]] },
70-
{ name: 'retains zero AIC', runs: [run(15, 0)], comments: [{ repository: 'repo', runId: 15, itemNumber: 1 }], expected: [[1, [15]]] },
71-
]) {
72-
const actual = aicLinkedItems(items, scenario.runs, scenario.comments, 'org').map((item) => [item.number, item.aicRunIds]);
73-
assert.deepEqual(actual, scenario.expected, scenario.name);
74-
}
66+
{ name: 'comment', runs: [run(10, 1)], comments: [comment(10, 1)], expected: [{ repository: 'repo', number: 1, type: 'issue', aic: 1, aicRunIds: [10] }] },
67+
{ name: 'pull request', runs: [run(11, 2, [pull(2)])], comments: [], expected: [{ repository: 'repo', number: 2, type: 'pull_request', aic: 2, aicRunIds: [11] }] },
68+
{ name: 'unrelated or cross-repository evidence', runs: [run(12, 3, [pull(3, 'other')]), run(13, null, [pull(2)])], comments: [{ ...comment(12, 1), itemNumber: 99 }], expected: [] },
69+
{ name: 'sums multiple runs', runs: [run(10, 1.25), run(14, 2.5)], comments: [comment(10, 1), comment(14, 1)], expected: [{ repository: 'repo', number: 1, type: 'issue', aic: 3.75, aicRunIds: [10, 14] }] },
70+
{ name: 'deduplicates evidence', runs: [run(14, 4, [pull(2), pull(2)])], comments: [comment(14, 2), comment(14, 2)], expected: [{ repository: 'repo', number: 2, type: 'pull_request', aic: 4, aicRunIds: [14] }] },
71+
{ name: 'attributes one run fully to multiple items', runs: [run(16, 5)], comments: [comment(16, 1), comment(16, 3)], expected: [{ repository: 'repo', number: 1, type: 'issue', aic: 5, aicRunIds: [16] }, { repository: 'repo', number: 3, type: 'issue', aic: 5, aicRunIds: [16] }] },
72+
{ name: 'retains zero AIC', runs: [run(15, 0)], comments: [comment(15, 1)], expected: [{ repository: 'repo', number: 1, type: 'issue', aic: 0, aicRunIds: [15] }] },
73+
]) assert.deepEqual(aicLinkedItems(items, scenario.runs, scenario.comments, 'org'), scenario.expected, scenario.name);
7574
});
7675

7776
test('parses comment AIC with transparent same-repository item identity', () => {
78-
const comment = parseCommentAic({
79-
id: 1,
80-
issue_url: 'https://api.github.com/repos/org/repo/issues/7', html_url: 'https://github.com/org/repo/pull/7#issuecomment-9',
81-
body: '> Generated by [workflow](https://github.com/org/repo/actions/runs/42) · 0 AIC',
82-
}, 'org', 'repo');
83-
assert.deepEqual({ runId: comment.runId, itemNumber: comment.itemNumber, itemUrl: comment.itemUrl, aic: comment.aic }, {
84-
runId: 42, itemNumber: 7, itemUrl: 'https://github.com/org/repo/pull/7', aic: 0,
85-
});
77+
const comment = parseCommentAic({ id: 1, issue_url: 'https://api.github.com/repos/org/repo/issues/7', html_url: 'https://github.com/org/repo/pull/7#issuecomment-9', body: '> Generated by [workflow](https://github.com/org/repo/actions/runs/42) · 0 AIC' }, 'org', 'repo');
78+
assert.deepEqual({ runId: comment.runId, itemNumber: comment.itemNumber, itemUrl: comment.itemUrl, aic: comment.aic }, { runId: 42, itemNumber: 7, itemUrl: 'https://github.com/org/repo/pull/7', aic: 0 });
8679
assert.equal(parseCommentAic({ id: 1, issue_url: 'https://api.github.com/repos/other/repo/issues/7', html_url: 'https://github.com/other/repo/issues/7#issuecomment-9', body: '> Generated by [workflow](https://github.com/org/repo/actions/runs/42) · 1 AIC' }, 'org', 'repo'), null);
8780
assert.equal(parseCommentAic({ id: 1, issue_url: 'https://api.github.com/repos/org/repo/issues/7', html_url: 'https://github.com/org/repo/issues/7#issuecomment-9', body: '> Generated by [workflow](https://github.com/other/repo/actions/runs/42) · 1 AIC' }, 'org', 'repo'), null);
8881
assert.equal(parseCommentAic({ id: 1, issue_url: 'https://api.github.com/repos/org/repo/issues/7', html_url: 'https://github.com/org/repo/issues/7#issuecomment-9', body: '> Generated by [workflow](https://github.com/org/repo/actions/runs/42) · 1 AIC\n<!-- gh-aw-agentic-workflow: id: 42, workflow_id: 1, run: https://github.com/other/repo/actions/runs/42 -->' }, 'org', 'repo'), null);
8982
});
9083

9184
test('retains consistent comment evidence for every exact item target', () => {
92-
const records = deduplicateCommentAic([
93-
{ runId: 20, commentId: 1, commentUrl: 'one', agentAic: 1, detectionAic: null, aic: 1, repository: 'repo', itemNumber: 1 },
94-
{ runId: 20, commentId: 2, commentUrl: 'two', agentAic: 1, detectionAic: null, aic: 1, repository: 'repo', itemNumber: 2 },
95-
]).records;
96-
const items = [{ repository: 'repo', number: 1, type: 'issue' }, { repository: 'repo', number: 2, type: 'issue' }];
97-
assert.deepEqual(aicLinkedItems(items, [{ id: 20, repository: 'repo', kind: 'drafter', aic: 1 }], records, 'org').map((item) => [item.number, item.aicRunIds]), [[1, [20]], [2, [20]]]);
85+
const records = deduplicateCommentAic([{ runId: 20, commentId: 1, commentUrl: 'one', agentAic: 1, detectionAic: null, aic: 1, repository: 'repo', itemNumber: 1 }, { runId: 20, commentId: 2, commentUrl: 'two', agentAic: 1, detectionAic: null, aic: 1, repository: 'repo', itemNumber: 2 }]).records;
86+
const items = [{ repository: 'repo', number: 1, type: 'issue', url: 'https://github.com/org/repo/issues/1' }, { repository: 'repo', number: 2, type: 'issue', url: 'https://github.com/org/repo/issues/2' }];
87+
for (const record of records) record.itemUrl = `https://github.com/org/repo/issues/${record.itemNumber}`;
88+
assert.deepEqual(aicLinkedItems(items, [{ id: 20, repository: 'repo', kind: 'drafter', aic: 1 }], records, 'org'), [{ repository: 'repo', number: 1, type: 'issue', aic: 1, aicRunIds: [20] }, { repository: 'repo', number: 2, type: 'issue', aic: 1, aicRunIds: [20] }]);
9889
});
9990

10091
test('normalizes only same-repository workflow PR references', () => {
101-
assert.deepEqual(normalizedPullRequests({ pull_requests: [
102-
{ url: 'https://api.github.com/repos/org/repo/pulls/2' },
103-
{ url: 'https://api.github.com/repos/org/repo/pulls/1' },
104-
{ url: 'https://api.github.com/repos/org/repo/pulls/2' },
105-
{ url: 'https://api.github.com/repos/org/other/pulls/3' },
106-
] }, 'org', 'repo'), [
107-
{ number: 1, url: 'https://github.com/org/repo/pull/1' },
108-
{ number: 2, url: 'https://github.com/org/repo/pull/2' },
109-
]);
92+
assert.deepEqual(normalizedPullRequests({ pull_requests: [{ url: 'https://api.github.com/repos/org/repo/pulls/2' }, { url: 'https://api.github.com/repos/org/repo/pulls/1' }, { url: 'https://api.github.com/repos/org/repo/pulls/2' }, { url: 'https://api.github.com/repos/org/other/pulls/3' }] }, 'org', 'repo'), [{ number: 1, url: 'https://github.com/org/repo/pull/1' }, { number: 2, url: 'https://github.com/org/repo/pull/2' }]);
11093
});
11194

112-
test('validates detailed item links have exact serialized evidence', () => {
113-
const issue = { repository: 'repo', number: 1, type: 'issue', url: 'https://github.com/org/repo/issues/1', aicRunIds: [1] };
114-
const pullRequest = { repository: 'repo', number: 2, type: 'pull_request', url: 'https://github.com/org/repo/pull/2', aicRunIds: [2] };
115-
const commentAic = [{ repository: 'repo', runId: 1, itemNumber: 1, itemUrl: issue.url }];
116-
const workflowRuns = [
117-
{ id: 1, repository: 'repo', aic: 0, pullRequests: [] },
118-
{ id: 2, repository: 'repo', aic: 1, pullRequests: [{ number: 2, url: pullRequest.url }] },
119-
];
95+
test('validates compact detailed item records and exact serialized evidence', () => {
96+
const issue = { repository: 'repo', number: 1, type: 'issue', aic: 0, aicRunIds: [1] };
97+
const pullRequest = { repository: 'repo', number: 2, type: 'pull_request', aic: 1, aicRunIds: [2] };
98+
const issueUrl = 'https://github.com/org/repo/issues/1';
99+
const pullRequestUrl = 'https://github.com/org/repo/pull/2';
100+
const commentAic = [{ repository: 'repo', runId: 1, itemNumber: 1, itemUrl: issueUrl }];
101+
const workflowRuns = [{ id: 1, repository: 'repo', aic: 0, pullRequests: [] }, { id: 2, repository: 'repo', aic: 1, pullRequests: [{ number: 2, url: pullRequestUrl }] }];
120102
assert.doesNotThrow(() => validateDetailedItems([issue, pullRequest], workflowRuns, commentAic, 'org'));
121-
assert.throws(() => validateDetailedItems([issue], [{ id: 1, repository: 'repo', aic: 0, pullRequests: [] }], [], 'org'));
122-
assert.throws(() => validateDetailedItems([pullRequest], [{ id: 2, repository: 'repo', aic: 1, pullRequests: [{ number: 2, url: issue.url }] }], [], 'org'));
123-
assert.throws(() => validateDetailedItems([{ ...issue, type: 'pull_request' }], workflowRuns, commentAic, 'org'));
124-
assert.throws(() => validateDetailedItems([{ ...issue, url: 'https://github.com/other/repo/issues/1' }], workflowRuns, [{ ...commentAic[0], itemUrl: 'https://github.com/other/repo/issues/1' }], 'org'));
125-
assert.throws(() => validateDetailedItems([{ ...issue, aicRunIds: [1, 1] }], workflowRuns, commentAic, 'org'));
126-
assert.throws(() => validateDetailedItems([{ ...issue, aicRunIds: [2] }], [{ id: 2, repository: 'repo', aic: null, pullRequests: [] }], commentAic, 'org'));
127-
assert.throws(() => validateDetailedItems([{ ...issue, aicRunIds: [3] }], [{ id: 3, repository: 'other', aic: 1, pullRequests: [] }], commentAic, 'org'));
103+
for (const [name, items, runs, comments] of [
104+
['missing evidence', [issue], [{ id: 1, repository: 'repo', aic: 0, pullRequests: [] }], []],
105+
['wrong PR evidence', [pullRequest], [{ id: 2, repository: 'repo', aic: 1, pullRequests: [{ number: 2, url: issueUrl }] }], []],
106+
['wrong type', [{ ...issue, type: 'pull_request' }], workflowRuns, commentAic],
107+
['cross-repository comment URL', [issue], workflowRuns, [{ ...commentAic[0], itemUrl: 'https://github.com/other/repo/issues/1' }]],
108+
['duplicate run ID', [{ ...issue, aicRunIds: [1, 1] }], workflowRuns, commentAic],
109+
['unknown AIC', [{ ...issue, aicRunIds: [2] }], [{ id: 2, repository: 'repo', aic: null, pullRequests: [] }], commentAic],
110+
['cross-repository run', [{ ...issue, aicRunIds: [3] }], [{ id: 3, repository: 'other', aic: 1, pullRequests: [] }], commentAic],
111+
['wrong total', [{ ...pullRequest, aic: 2 }], workflowRuns, commentAic],
112+
['duplicate item identity', [issue, { ...issue, type: 'pull_request' }], workflowRuns, commentAic],
113+
['unexpected metadata', [{ ...issue, updatedAt: '2026-09-01T00:00:00Z' }], workflowRuns, commentAic],
114+
['non-finite AIC', [{ ...issue, aic: Number.NaN }], workflowRuns, commentAic],
115+
['malformed identity', [{ ...issue, repository: 'bad/repo' }], workflowRuns, commentAic],
116+
]) assert.throws(() => validateDetailedItems(items, runs, comments, 'org'), name);
128117
});

0 commit comments

Comments
 (0)