Skip to content

Practical patterns, starters & CLI tools for loop engineering with AI coding agents. Design systems that prompt and orchestrate agents (inspired by Addy Osmani and Boris Cherny). Includes loop-audit, loop-init, loop-cost.

fix(loop-metrics): preserve non-date run IDs in timeframe filters (#610)

s4kura0948ac1661de
diff --git a/tools/loop-metrics/dist/metrics.js b/tools/loop-metrics/dist/metrics.js
index aa4797d..e594fd3 100644
--- a/tools/loop-metrics/dist/metrics.js
+++ b/tools/loop-metrics/dist/metrics.js
@@ -32,6 +32,10 @@ export function filterEntries(entries, pattern, days) {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
filtered = filtered.filter(e => {
+ // Match the run-log pruner: Date also accepts IDs such as "run-1"
+ // as old dates, so only ISO-shaped IDs can establish a run's age.
+ if (!/^\d{4}-\d{2}-\d{2}/.test(e.run_id))
+ return true;
const entryDate = new Date(e.run_id);
// An unparseable run_id (e.g. a numeric GitHub run id or a custom
// slug) yields Invalid Date; keep the entry rather than silently
diff --git a/tools/loop-metrics/src/metrics.ts b/tools/loop-metrics/src/metrics.ts
index b4448a5..94e93e1 100644
--- a/tools/loop-metrics/src/metrics.ts
+++ b/tools/loop-metrics/src/metrics.ts
@@ -60,6 +60,9 @@ export function filterEntries(entries: RunEntry[], pattern?: string, days?: numb
cutoffDate.setDate(cutoffDate.getDate() - days);
filtered = filtered.filter(e => {
+ // Match the run-log pruner: Date also accepts IDs such as "run-1"
+ // as old dates, so only ISO-shaped IDs can establish a run's age.
+ if (!/^\d{4}-\d{2}-\d{2}/.test(e.run_id)) return true;
const entryDate = new Date(e.run_id);
// An unparseable run_id (e.g. a numeric GitHub run id or a custom
// slug) yields Invalid Date; keep the entry rather than silently
diff --git a/tools/loop-metrics/test/metrics.test.mjs b/tools/loop-metrics/test/metrics.test.mjs
index a331dba..8f3c5aa 100644
--- a/tools/loop-metrics/test/metrics.test.mjs
+++ b/tools/loop-metrics/test/metrics.test.mjs
@@ -37,13 +37,26 @@ test('successRatePct stays within [0, 100] when a single run logs more than one
assert.ok(metrics.successRatePct >= 0 && metrics.successRatePct <= 100);
});
-test('filterEntries keeps entries with unparseable run_id when a timeframe is set', () => {
+test('filterEntries keeps non-ISO run IDs and filters ISO dates within a timeframe', (t) => {
+ const RealDate = Date;
+ t.mock.method(globalThis, 'Date', class extends RealDate {
+ constructor(...args) {
+ super(...(args.length ? args : ['2026-08-01T12:00:00Z']));
+ }
+ });
+
const entries = [
{ run_id: '2026-07-30T08:50:34Z', pattern: 'daily-triage', duration_s: 5, items_found: 1, actions_taken: 2, escalations: 1, tokens_estimate: 52000, outcome: 'report-only' },
- // Numeric GitHub run id is not a parseable date and must not be dropped.
- { run_id: '29231015995', pattern: 'daily-triage', duration_s: 8, items_found: 1, actions_taken: 1, escalations: 0, tokens_estimate: 52000, outcome: 'report-only' },
+ ...['29231015995', 'run-1', '123', '2026', 'custom-run', '2026-99-99'].map(run_id => ({
+ run_id, pattern: 'daily-triage', duration_s: 8, items_found: 1, actions_taken: 1, escalations: 0, tokens_estimate: 52000, outcome: 'report-only'
+ })),
+ { run_id: '2026-06-01T08:50:34Z', pattern: 'daily-triage', duration_s: 5, items_found: 0, actions_taken: 0, escalations: 0, tokens_estimate: 1000, outcome: 'no-op' },
+ { run_id: 'run-2', pattern: 'ci-sweeper', duration_s: 5, items_found: 0, actions_taken: 0, escalations: 0, tokens_estimate: 1000, outcome: 'no-op' },
];
const filtered = filterEntries(entries, 'daily-triage', 30);
- assert.strictEqual(filtered.length, 2, 'unparseable run_id entries are kept, not dropped');
+ assert.deepStrictEqual(filtered.map(e => e.run_id), [
+ '2026-07-30T08:50:34Z', '29231015995', 'run-1', '123', '2026', 'custom-run', '2026-99-99'
+ ]);
+ assert.strictEqual(filterEntries(entries), entries, 'no filters preserve every entry');
});