@drakulavich/cogload 0.10.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.
- package/CHANGELOG.md +303 -0
- package/LICENSE +21 -0
- package/README.md +301 -0
- package/assets/characters.webp +0 -0
- package/assets/fonts/LICENSE-inter.txt +92 -0
- package/assets/fonts/LICENSE-jetbrains-mono.txt +93 -0
- package/assets/fonts/inter-400.woff2 +0 -0
- package/assets/fonts/inter-700.woff2 +0 -0
- package/assets/fonts/inter-800.woff2 +0 -0
- package/assets/fonts/jetbrains-mono-500.woff2 +0 -0
- package/package.json +30 -0
- package/src/cli/args.ts +155 -0
- package/src/cli/commands.io.ts +99 -0
- package/src/cli/index.ts +58 -0
- package/src/cli/timing.ts +18 -0
- package/src/cli/zapara.ts +5 -0
- package/src/lib/card/card.ts +172 -0
- package/src/lib/card/cardhtml.ts +130 -0
- package/src/lib/card/image.io.ts +134 -0
- package/src/lib/card/index.ts +3 -0
- package/src/lib/metrics/analyze.ts +17 -0
- package/src/lib/metrics/derive.ts +221 -0
- package/src/lib/metrics/index.ts +3 -0
- package/src/lib/metrics/score.ts +55 -0
- package/src/lib/report/index.ts +1 -0
- package/src/lib/report/report.io.ts +82 -0
- package/src/lib/status/index.ts +2 -0
- package/src/lib/status/status.ts +52 -0
- package/src/lib/status/statusfile.io.ts +38 -0
- package/src/lib/text/format.ts +16 -0
- package/src/lib/text/index.ts +2 -0
- package/src/lib/text/render.ts +120 -0
- package/src/lib/transcripts/cache.io.ts +256 -0
- package/src/lib/transcripts/index.ts +3 -0
- package/src/lib/transcripts/parse.ts +138 -0
- package/src/lib/transcripts/scan.io.ts +75 -0
- package/src/lib/types.ts +22 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { score } from "./score.ts";
|
|
2
|
+
import type { Day, Event, EventKind, HourBucket, LiveBucket, Metrics, Totals, Window } from "../types.ts";
|
|
3
|
+
|
|
4
|
+
const LOOKBACK_MS = 3 * 60 * 60 * 1000;
|
|
5
|
+
export const GAP_MS = 10 * 60 * 1000;
|
|
6
|
+
const SLOT_MS = 5 * 60 * 1000;
|
|
7
|
+
const LIVE_MS = 60 * 60 * 1000;
|
|
8
|
+
const LATE_HOURS = new Set([23, 0, 1, 2, 3, 4, 5]);
|
|
9
|
+
|
|
10
|
+
// Not localeCompare: a result must not depend on the locale.
|
|
11
|
+
export const compareStrings = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0);
|
|
12
|
+
const pad2 = (n: number) => String(n).padStart(2, "0");
|
|
13
|
+
export const localDate = (d: Date) => `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-${pad2(d.getDate())}`;
|
|
14
|
+
const parseDate = (s: string): Date => {
|
|
15
|
+
const [y, m, d] = s.split("-").map(Number) as [number, number, number];
|
|
16
|
+
return new Date(y, m - 1, d); // local midnight
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// The dates are counted on the calendar, in UTC: local midnight of a day a
|
|
20
|
+
// zone skipped (Samoa, 30 December 2011) is the next day's, which would list
|
|
21
|
+
// that day twice. The skipped date stays as a day with no hours.
|
|
22
|
+
// cutoffMs is the earliest instant read: the look-back, or `streakFrom` when earlier.
|
|
23
|
+
export function windowBounds(w: Window): { startMs: number; endMs: number; cutoffMs: number; dates: string[] } {
|
|
24
|
+
const to = parseDate(w.to);
|
|
25
|
+
const [y, m, d] = w.to.split("-").map(Number) as [number, number, number];
|
|
26
|
+
const dates: string[] = [];
|
|
27
|
+
for (let i = w.days - 1; i >= 0; i--) {
|
|
28
|
+
const u = new Date(Date.UTC(y, m - 1, d - i));
|
|
29
|
+
dates.push(`${u.getUTCFullYear()}-${pad2(u.getUTCMonth() + 1)}-${pad2(u.getUTCDate())}`);
|
|
30
|
+
}
|
|
31
|
+
const startMs = parseDate(dates[0]!).getTime();
|
|
32
|
+
const endMs = new Date(to.getFullYear(), to.getMonth(), to.getDate() + 1).getTime();
|
|
33
|
+
return { startMs, endMs, cutoffMs: Math.min(startMs - LOOKBACK_MS, w.streakFrom?.getTime() ?? Infinity), dates };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const emptyMetrics = (): Metrics => ({
|
|
37
|
+
sessions: 0, prompts: 0, reports: 0, outputTokens: 0, interrupts: 0, rejects: 0, questions: 0, plans: 0, modeSwitches: 0,
|
|
38
|
+
decisions: 0, contextSwitches: 0, activeMin: 0, streakMin: 0, lateNight: false,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Presence is the human's own actions only: agent records and the agent's asks
|
|
42
|
+
// are never presence, because presence answers "is it time to rest?".
|
|
43
|
+
const PRESENCE: ReadonlySet<EventKind> = new Set<EventKind>(["prompt", "interrupt", "reject", "answer"]);
|
|
44
|
+
|
|
45
|
+
type Acc = { m: Metrics; sessions: Set<string>; slots: Set<number>; lastPromptSession: string | null; maxStreakMs: number; lastPresence: { ts: number; streakStart: number } | null };
|
|
46
|
+
const newAcc = (): Acc => ({ m: emptyMetrics(), sessions: new Set(), slots: new Set(), lastPromptSession: null, maxStreakMs: 0, lastPresence: null });
|
|
47
|
+
|
|
48
|
+
// Inside a streak the human sat through the gap, so a pair of actions covers
|
|
49
|
+
// every slot between them; an action that starts a streak covers only its own.
|
|
50
|
+
const coverage = (prevTs: number | null, streakStart: number, e: Event): { from: number; to: number; streakStart: number } => {
|
|
51
|
+
const to = Math.floor(e.ts / SLOT_MS);
|
|
52
|
+
if (prevTs !== null && e.ts - prevTs <= GAP_MS) return { from: Math.floor(prevTs / SLOT_MS), to, streakStart };
|
|
53
|
+
return { from: to, to, streakStart: e.ts };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// The counts an hour bucket and the live bucket share. `streakStart` is the
|
|
57
|
+
// caller's: a streak may have begun before this accumulator's window. The streak
|
|
58
|
+
// kept is the longest seen, not the one the window ended on, or a single prompt
|
|
59
|
+
// after a break would erase the run.
|
|
60
|
+
function accumulate(a: Acc, e: Event, streakStart: number): void {
|
|
61
|
+
if (PRESENCE.has(e.kind)) {
|
|
62
|
+
a.maxStreakMs = Math.max(a.maxStreakMs, e.ts - streakStart);
|
|
63
|
+
a.lastPresence = { ts: e.ts, streakStart }; // for the day's live streak
|
|
64
|
+
}
|
|
65
|
+
switch (e.kind) {
|
|
66
|
+
case "activity":
|
|
67
|
+
a.sessions.add(e.sessionId);
|
|
68
|
+
break;
|
|
69
|
+
case "prompt":
|
|
70
|
+
a.m.prompts++;
|
|
71
|
+
if (a.lastPromptSession !== null && a.lastPromptSession !== e.sessionId) a.m.contextSwitches++;
|
|
72
|
+
a.lastPromptSession = e.sessionId;
|
|
73
|
+
break;
|
|
74
|
+
case "report": a.m.reports++; break;
|
|
75
|
+
case "output": a.m.outputTokens += e.tokens ?? 0; break;
|
|
76
|
+
case "interrupt": a.m.interrupts++; break;
|
|
77
|
+
case "reject": a.m.rejects++; break;
|
|
78
|
+
case "question": a.m.questions++; break;
|
|
79
|
+
case "plan_review": a.m.plans++; break;
|
|
80
|
+
case "mode_change": a.m.modeSwitches++; break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// `lateNight` is the caller's: an hour's own label, or the hour of `now` for the
|
|
85
|
+
// live bucket. A fresh accumulator is an empty hour and scores null.
|
|
86
|
+
function finish(a: Acc, lateNight: boolean): LiveBucket {
|
|
87
|
+
const m = a.m;
|
|
88
|
+
m.sessions = a.sessions.size;
|
|
89
|
+
m.activeMin = a.slots.size * 5;
|
|
90
|
+
m.streakMin = Math.round(a.maxStreakMs / 60000);
|
|
91
|
+
m.decisions = m.interrupts + m.rejects + m.questions + m.plans + m.modeSwitches;
|
|
92
|
+
m.lateNight = lateNight;
|
|
93
|
+
return { ...m, score: score(m) };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// The 24 hour buckets, keyed "date|hour". Events before startMs are look-back:
|
|
97
|
+
// they move the streak but enter no bucket, though a span from one into the
|
|
98
|
+
// window still covers the window's first slots.
|
|
99
|
+
function foldEvents(sorted: Event[], startMs: number): { acc: Map<string, Acc>; carried: { ts: number; streakStart: number } | null } {
|
|
100
|
+
const acc = new Map<string, Acc>(); // key "date|hour"
|
|
101
|
+
const key = (ts: number): string => {
|
|
102
|
+
const d = new Date(ts);
|
|
103
|
+
return `${localDate(d)}|${d.getHours()}`;
|
|
104
|
+
};
|
|
105
|
+
const get = (k: string): Acc => {
|
|
106
|
+
let a = acc.get(k);
|
|
107
|
+
if (!a) { a = newAcc(); acc.set(k, a); }
|
|
108
|
+
return a;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
let prevPresenceTs: number | null = null;
|
|
112
|
+
let streakStart = 0; // always set by the first presence event, which starts a streak
|
|
113
|
+
let carried: { ts: number; streakStart: number } | null = null;
|
|
114
|
+
for (const e of sorted) {
|
|
115
|
+
if (PRESENCE.has(e.kind)) {
|
|
116
|
+
const c = coverage(prevPresenceTs, streakStart, e);
|
|
117
|
+
streakStart = c.streakStart;
|
|
118
|
+
for (let s = c.from; s <= c.to; s++) {
|
|
119
|
+
const slotStart = s * SLOT_MS;
|
|
120
|
+
if (slotStart >= startMs) get(key(slotStart)).slots.add(s); // a slot belongs to the bucket of its start
|
|
121
|
+
}
|
|
122
|
+
prevPresenceTs = e.ts;
|
|
123
|
+
// A person at the keyboard at 23:58 is still in that streak at 00:03,
|
|
124
|
+
// and the day they are looking at holds nothing yet.
|
|
125
|
+
if (e.ts < startMs) carried = { ts: e.ts, streakStart };
|
|
126
|
+
}
|
|
127
|
+
if (e.ts < startMs) continue; // look-back: presence bookkeeping only
|
|
128
|
+
accumulate(get(key(e.ts)), e, streakStart);
|
|
129
|
+
}
|
|
130
|
+
return { acc, carried };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// The sixty minutes ending at `now`, by the rule an hour bucket uses. Presence
|
|
134
|
+
// runs from the first event, so a streak older than the window is measured from
|
|
135
|
+
// where it began. Events before the day's start count here, unlike in a bucket:
|
|
136
|
+
// the window is a clock's hour, not a calendar's.
|
|
137
|
+
function foldLive(sorted: Event[], nowMs: number): Acc {
|
|
138
|
+
const a = newAcc();
|
|
139
|
+
const fromMs = nowMs - LIVE_MS;
|
|
140
|
+
const inWindow = (t: number): boolean => t > fromMs && t <= nowMs;
|
|
141
|
+
let prevPresenceTs: number | null = null;
|
|
142
|
+
let streakStart = 0;
|
|
143
|
+
for (const e of sorted) {
|
|
144
|
+
if (PRESENCE.has(e.kind)) {
|
|
145
|
+
const c = coverage(prevPresenceTs, streakStart, e);
|
|
146
|
+
streakStart = c.streakStart;
|
|
147
|
+
for (let s = c.from; s <= c.to; s++) if (inWindow(s * SLOT_MS)) a.slots.add(s);
|
|
148
|
+
prevPresenceTs = e.ts;
|
|
149
|
+
}
|
|
150
|
+
if (inWindow(e.ts)) accumulate(a, e, streakStart);
|
|
151
|
+
}
|
|
152
|
+
return a;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function buildDay(date: string, acc: Map<string, Acc>): Day {
|
|
156
|
+
const buckets: HourBucket[] = [];
|
|
157
|
+
// Hours run in time order, so the last write is the day's last action.
|
|
158
|
+
let lastPresence: { ts: number; streakStart: number } | null = null;
|
|
159
|
+
for (let hour = 0; hour < 24; hour++) {
|
|
160
|
+
const a = acc.get(`${date}|${hour}`);
|
|
161
|
+
if (a?.lastPresence) lastPresence = a.lastPresence;
|
|
162
|
+
buckets.push({ ...finish(a ?? newAcc(), LATE_HOURS.has(hour)), hour });
|
|
163
|
+
}
|
|
164
|
+
const scored = buckets.filter((b) => b.score !== null);
|
|
165
|
+
const totals: Totals = buckets.reduce((t, b) => ({
|
|
166
|
+
prompts: t.prompts + b.prompts, reports: t.reports + b.reports, outputTokens: t.outputTokens + b.outputTokens,
|
|
167
|
+
interrupts: t.interrupts + b.interrupts, rejects: t.rejects + b.rejects,
|
|
168
|
+
questions: t.questions + b.questions, plans: t.plans + b.plans, modeSwitches: t.modeSwitches + b.modeSwitches,
|
|
169
|
+
decisions: t.decisions + b.decisions, contextSwitches: t.contextSwitches + b.contextSwitches,
|
|
170
|
+
maxSessions: Math.max(t.maxSessions, b.sessions),
|
|
171
|
+
}), { prompts: 0, reports: 0, outputTokens: 0, interrupts: 0, rejects: 0, questions: 0, plans: 0, modeSwitches: 0, decisions: 0, contextSwitches: 0, maxSessions: 0 });
|
|
172
|
+
return {
|
|
173
|
+
date,
|
|
174
|
+
peak: scored.length ? Math.max(...scored.map((b) => b.score!.index)) : null,
|
|
175
|
+
mean: scored.length ? Math.round(scored.reduce((s, b) => s + b.score!.index, 0) / scored.length) : null,
|
|
176
|
+
activeMin: buckets.reduce((s, b) => s + b.activeMin, 0),
|
|
177
|
+
presence: lastPresence && { lastAt: new Date(lastPresence.ts).toISOString(), streakStartAt: new Date(lastPresence.streakStart).toISOString() },
|
|
178
|
+
totals,
|
|
179
|
+
buckets,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Walks a streak's start back through earlier presence, one gap of at most
|
|
184
|
+
// GAP_MS at a time. `earlier` is ascending and all before `start`.
|
|
185
|
+
function traceBack(start: number, earlier: number[]): number {
|
|
186
|
+
for (let i = earlier.length - 1; i >= 0 && start - earlier[i]! <= GAP_MS; i--) start = earlier[i]!;
|
|
187
|
+
return start;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function derive(events: Event[], w: Window): Day[] {
|
|
191
|
+
const { startMs, endMs, cutoffMs, dates } = windowBounds(w);
|
|
192
|
+
const lookbackMs = startMs - LOOKBACK_MS;
|
|
193
|
+
// sort() is stable, so events equal on both keys keep their input order.
|
|
194
|
+
const read = events
|
|
195
|
+
.filter((e) => e.ts >= cutoffMs && e.ts < endMs && (!w.now || e.ts <= w.now.getTime()))
|
|
196
|
+
.sort((a, b) => a.ts - b.ts || compareStrings(a.sessionId, b.sessionId));
|
|
197
|
+
// Events before the look-back, read only for `streakFrom`, move nothing but
|
|
198
|
+
// the start of a streak in `presence`.
|
|
199
|
+
const sorted = read.filter((e) => e.ts >= lookbackMs);
|
|
200
|
+
const earlier = read.filter((e) => e.ts < lookbackMs && PRESENCE.has(e.kind)).map((e) => e.ts);
|
|
201
|
+
const { acc, carried } = foldEvents(sorted, startMs);
|
|
202
|
+
const days = dates.map((date) => buildDay(date, acc));
|
|
203
|
+
// Only a first day with no action of its own borrows the look-back's streak,
|
|
204
|
+
// and only into `presence`: no bucket, total or active minute crosses back.
|
|
205
|
+
const first = days[0];
|
|
206
|
+
if (first && first.presence === null && carried) {
|
|
207
|
+
first.presence = { lastAt: new Date(carried.ts).toISOString(), streakStartAt: new Date(carried.streakStart).toISOString() };
|
|
208
|
+
}
|
|
209
|
+
for (const d of days) {
|
|
210
|
+
if (d.presence) d.presence.streakStartAt = new Date(traceBack(Date.parse(d.presence.streakStartAt), earlier)).toISOString();
|
|
211
|
+
}
|
|
212
|
+
if (w.now) {
|
|
213
|
+
const today = localDate(w.now);
|
|
214
|
+
const open = days.find((d) => d.date === today);
|
|
215
|
+
if (open) {
|
|
216
|
+
open.asOf = w.now.toISOString();
|
|
217
|
+
open.live = finish(foldLive(sorted, w.now.getTime()), LATE_HOURS.has(w.now.getHours()));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return days;
|
|
221
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Level, Metrics, Parts, Score } from "../types.ts";
|
|
2
|
+
|
|
3
|
+
// Calibration lives here and nowhere else. Integer points of 100, so 0.5 sums
|
|
4
|
+
// stay exact; norms are the p90 of two weeks on two machines (see CHANGELOG).
|
|
5
|
+
export const WEIGHTS = { parallel: 25, pace: 15, supervision: 30, reading: 10, streak: 10, late: 10 } as const;
|
|
6
|
+
export const NORMS = { parallelSpan: 4, pacePerHour: 20, supervisionPerHour: 45, decisionWeight: 3, readingTokens: 80_000, streakMin: 40 } as const;
|
|
7
|
+
const LEVELS: readonly { max: number; level: Level }[] = [
|
|
8
|
+
{ max: 29, level: "Calm" },
|
|
9
|
+
{ max: 59, level: "Warming" },
|
|
10
|
+
{ max: 84, level: "Heating" },
|
|
11
|
+
{ max: 100, level: "Fried" },
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
const clamp01 = (x: number): number => Math.min(1, Math.max(0, x));
|
|
15
|
+
|
|
16
|
+
export function levelOf(index: number): Level {
|
|
17
|
+
for (const l of LEVELS) if (index <= l.max) return l.level;
|
|
18
|
+
return "Fried";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function score(m: Metrics): Score | null {
|
|
22
|
+
if (m.sessions === 0) return null;
|
|
23
|
+
const parallel = clamp01((m.sessions - 1) / NORMS.parallelSpan);
|
|
24
|
+
const pace = clamp01(m.prompts / NORMS.pacePerHour);
|
|
25
|
+
// A decision costs `decisionWeight` reports or session hops; the three share a norm.
|
|
26
|
+
const supervision = clamp01(
|
|
27
|
+
(NORMS.decisionWeight * m.decisions + m.reports + m.contextSwitches) / NORMS.supervisionPerHour,
|
|
28
|
+
);
|
|
29
|
+
const reading = clamp01(m.outputTokens / NORMS.readingTokens);
|
|
30
|
+
const streak = clamp01(m.streakMin / NORMS.streakMin);
|
|
31
|
+
const late = m.lateNight ? 1 : 0;
|
|
32
|
+
|
|
33
|
+
const raw = {
|
|
34
|
+
parallel: WEIGHTS.parallel * parallel,
|
|
35
|
+
pace: WEIGHTS.pace * pace,
|
|
36
|
+
supervision: WEIGHTS.supervision * supervision,
|
|
37
|
+
reading: WEIGHTS.reading * reading,
|
|
38
|
+
streak: WEIGHTS.streak * streak,
|
|
39
|
+
late: WEIGHTS.late * late,
|
|
40
|
+
};
|
|
41
|
+
// Rounded for display only.
|
|
42
|
+
const parts: Parts = {
|
|
43
|
+
parallel: Math.round(raw.parallel * 10) / 10,
|
|
44
|
+
pace: Math.round(raw.pace * 10) / 10,
|
|
45
|
+
supervision: Math.round(raw.supervision * 10) / 10,
|
|
46
|
+
reading: Math.round(raw.reading * 10) / 10,
|
|
47
|
+
streak: Math.round(raw.streak * 10) / 10,
|
|
48
|
+
late: Math.round(raw.late * 10) / 10,
|
|
49
|
+
};
|
|
50
|
+
// Rounded once from the raw points, so parts' rounding can never tip it across a boundary.
|
|
51
|
+
const index = Math.round(
|
|
52
|
+
raw.parallel + raw.pace + raw.supervision + raw.reading + raw.streak + raw.late,
|
|
53
|
+
);
|
|
54
|
+
return { index, level: levelOf(index), parts };
|
|
55
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { READERS, report, type Timing } from "./report.io.ts";
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { constants } from "node:buffer";
|
|
2
|
+
import { open } from "node:fs/promises";
|
|
3
|
+
import { analyzeEvents, windowBounds } from "../metrics/index.ts";
|
|
4
|
+
import { tailHash, type Fresh, type TranscriptCache, parseTranscript, scan, type ScanEntry, type ScanStats } from "../transcripts/index.ts";
|
|
5
|
+
import type { Day, Event } from "../types.ts";
|
|
6
|
+
|
|
7
|
+
export type ReportOptions = { projects: string; to: string; days: number; now?: Date; streakFrom?: Date };
|
|
8
|
+
// What --verbose prints about a run: counts and milliseconds, never a path.
|
|
9
|
+
export type Timing = ScanStats & { inWindow: number; read: number; bytes: number; cache: { hits: number; misses: number } | null; startMs?: number; openMs?: number; scanMs: number; lookupMs: number; readMs: number; analyzeMs: number; saveMs: number; render?: { format: string; ms: number }; writeMs?: number; totalMs?: number };
|
|
10
|
+
|
|
11
|
+
// Reading one file at a time left the disk idle between files: 1.6 GB took 5.4 s
|
|
12
|
+
// sequentially and 0.9 s in parallel. The cap keeps open files well under the limit.
|
|
13
|
+
export const READERS = 16;
|
|
14
|
+
|
|
15
|
+
type Read = { bytes: Buffer; stable: ScanEntry | null };
|
|
16
|
+
|
|
17
|
+
// `stable` only when both fstat agree and every byte was read: a file written
|
|
18
|
+
// during the read is used for this run and never cached. UTF-8 decodes to at
|
|
19
|
+
// most one UTF-16 unit per byte, so a file within the engine's longest string
|
|
20
|
+
// always decodes; a longer one is skipped like an unreadable one, before its
|
|
21
|
+
// bytes are read, since decoding it would throw and fail the whole run.
|
|
22
|
+
async function readWhole(entry: ScanEntry): Promise<Read> {
|
|
23
|
+
const fh = await open(entry.path, "r");
|
|
24
|
+
try {
|
|
25
|
+
const before = await fh.stat();
|
|
26
|
+
if (before.size > constants.MAX_STRING_LENGTH) throw new Error("longer than a string can hold");
|
|
27
|
+
const bytes = await fh.readFile();
|
|
28
|
+
const after = await fh.stat();
|
|
29
|
+
const same = before.dev === after.dev && before.ino === after.ino && before.size === after.size && before.mtimeMs === after.mtimeMs && before.size === bytes.length;
|
|
30
|
+
return { bytes, stable: same ? { path: entry.path, dev: after.dev, ino: after.ino, size: after.size, mtimeMs: after.mtimeMs } : null };
|
|
31
|
+
} finally {
|
|
32
|
+
await fh.close();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function report(o: ReportOptions, timing?: Timing, cache?: TranscriptCache | null): Promise<Day[]> {
|
|
37
|
+
const window = { to: o.to, days: o.days, now: o.now, streakFrom: o.streakFrom };
|
|
38
|
+
const { cutoffMs } = windowBounds(window);
|
|
39
|
+
const stats: ScanStats = { files: 0, tailChecks: 0 };
|
|
40
|
+
let t = performance.now();
|
|
41
|
+
const entries = await scan(o.projects, cutoffMs, stats);
|
|
42
|
+
const scanMs = performance.now() - t;
|
|
43
|
+
t = performance.now();
|
|
44
|
+
const found = cache ? await cache.hits(entries, cutoffMs) : null;
|
|
45
|
+
const hits = found ?? new Map<string, Event[]>();
|
|
46
|
+
const lookupMs = performance.now() - t;
|
|
47
|
+
t = performance.now();
|
|
48
|
+
const misses = entries.filter((e) => !hits.has(e.path));
|
|
49
|
+
const reads = new Map<string, Read>();
|
|
50
|
+
let next = 0;
|
|
51
|
+
let bytes = 0;
|
|
52
|
+
const reader = async (): Promise<void> => {
|
|
53
|
+
while (next < misses.length) {
|
|
54
|
+
const entry = misses[next++]!;
|
|
55
|
+
try {
|
|
56
|
+
const r = await readWhole(entry);
|
|
57
|
+
bytes += r.bytes.length;
|
|
58
|
+
reads.set(entry.path, r);
|
|
59
|
+
} catch { /* vanished or unreadable: skip */ }
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
await Promise.all(Array.from({ length: Math.min(READERS, misses.length) }, reader));
|
|
63
|
+
const readMs = performance.now() - t;
|
|
64
|
+
t = performance.now();
|
|
65
|
+
const fresh: Fresh[] = [];
|
|
66
|
+
const events = entries.flatMap((e) => {
|
|
67
|
+
const hit = hits.get(e.path);
|
|
68
|
+
if (hit) return hit;
|
|
69
|
+
const r = reads.get(e.path);
|
|
70
|
+
if (!r) return [];
|
|
71
|
+
const parsed = parseTranscript(r.bytes.toString("utf8"), cutoffMs);
|
|
72
|
+
if (r.stable) fresh.push({ entry: r.stable, tail: tailHash(r.bytes), fromMs: cutoffMs, events: parsed });
|
|
73
|
+
return parsed;
|
|
74
|
+
});
|
|
75
|
+
const days = analyzeEvents(events, window);
|
|
76
|
+
const analyzeMs = performance.now() - t;
|
|
77
|
+
t = performance.now();
|
|
78
|
+
cache?.save(entries.filter((e) => hits.has(e.path)), fresh, Date.now());
|
|
79
|
+
const saveMs = performance.now() - t;
|
|
80
|
+
if (timing) Object.assign(timing, { ...stats, inWindow: entries.length, read: reads.size, bytes, cache: found ? { hits: hits.size, misses: misses.length } : null, scanMs, lookupMs, readMs, analyzeMs, saveMs });
|
|
81
|
+
return days;
|
|
82
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { GAP_MS } from "../metrics/index.ts";
|
|
2
|
+
import type { Day, Level } from "../types.ts";
|
|
3
|
+
|
|
4
|
+
// The status file's nine values. The field order is the file format.
|
|
5
|
+
export type Status = {
|
|
6
|
+
schema: 1;
|
|
7
|
+
asOf: string;
|
|
8
|
+
date: string;
|
|
9
|
+
hour: number;
|
|
10
|
+
index: number | null;
|
|
11
|
+
level: Level | null;
|
|
12
|
+
peak: number | null;
|
|
13
|
+
activeMin: number;
|
|
14
|
+
streakMin: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// The reader contract's ceiling for activeMin and streakMin (status file spec):
|
|
18
|
+
// a reader treats a larger value as no data, so a longer streak is written as this.
|
|
19
|
+
const MAX_MIN = 1500;
|
|
20
|
+
|
|
21
|
+
// How far back `status` reads to find where the live streak began: as far as
|
|
22
|
+
// the file can show it, and one gap more, so that a streak longer than that
|
|
23
|
+
// still reaches past the ceiling and reads as MAX_MIN, not a few minutes under.
|
|
24
|
+
export function streakFrom(now: Date): Date {
|
|
25
|
+
return new Date(now.getTime() - MAX_MIN * 60_000 - GAP_MS);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// index and level are the day's `live` bucket, not the hour's: an hour's bucket
|
|
29
|
+
// is nearly empty just after the hour turns. peak folds the live index in: sixty
|
|
30
|
+
// minutes across two hours can score above both, and index must never exceed peak. streakMin is measured against `now`,
|
|
31
|
+
// not the bucket's: it must not reset on the hour or stop between two actions,
|
|
32
|
+
// and it is over once the last action is more than GAP_MS behind `now`.
|
|
33
|
+
export function statusOf(day: Day, now: Date): Status {
|
|
34
|
+
const hour = now.getHours();
|
|
35
|
+
const index = day.live?.score?.index ?? null;
|
|
36
|
+
const live = day.presence !== null && now.getTime() - Date.parse(day.presence.lastAt) <= GAP_MS;
|
|
37
|
+
return {
|
|
38
|
+
schema: 1,
|
|
39
|
+
asOf: day.asOf ?? now.toISOString(),
|
|
40
|
+
date: day.date,
|
|
41
|
+
hour,
|
|
42
|
+
index,
|
|
43
|
+
level: day.live?.score?.level ?? null,
|
|
44
|
+
peak: index === null ? day.peak : Math.max(index, day.peak ?? 0),
|
|
45
|
+
activeMin: day.activeMin,
|
|
46
|
+
streakMin: live ? Math.min(MAX_MIN, Math.round((now.getTime() - Date.parse(day.presence!.streakStartAt)) / 60000)) : 0,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function renderStatus(s: Status): string {
|
|
51
|
+
return JSON.stringify(s) + "\n";
|
|
52
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// The only module that writes the status file.
|
|
2
|
+
import { chmod, mkdir, open, rename, unlink } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
const FAILED = "cannot write the status file";
|
|
6
|
+
|
|
7
|
+
// Atomic: a temporary file in the same directory, then a rename over the
|
|
8
|
+
// target, so a reader never sees a partial line and a symlink at the target is
|
|
9
|
+
// replaced, not written through. HOME comes from `env`, not os.homedir(), so an
|
|
10
|
+
// empty HOME is an error rather than a silent fallback.
|
|
11
|
+
export async function writeStatus(line: string, env: NodeJS.ProcessEnv): Promise<void> {
|
|
12
|
+
const home = env.HOME;
|
|
13
|
+
if (!home) throw new Error(FAILED);
|
|
14
|
+
// The umask narrows the modes below; set it first so `mkdir` and `open` come
|
|
15
|
+
// out 0700 and 0600 on the first try, with no window where a second run finds
|
|
16
|
+
// a parent it cannot enter. Nothing else is written from here on.
|
|
17
|
+
process.umask(0o077);
|
|
18
|
+
const dir = join(home, ".claude", "cogload");
|
|
19
|
+
// Unique per run: concurrent runs each rename their own complete file. Opened
|
|
20
|
+
// with `wx` so a file or symlink already at the name is an error, and never
|
|
21
|
+
// reused or deleted if not ours: a crashed run's leftover and a slow run's
|
|
22
|
+
// file in flight look the same from outside.
|
|
23
|
+
const tmp = join(dir, `status.json.${process.pid}.${Math.random().toString(36).slice(2, 10)}.tmp`);
|
|
24
|
+
let created = false;
|
|
25
|
+
try {
|
|
26
|
+
// A directory cogload creates is 0700; one that exists keeps the mode the
|
|
27
|
+
// person gave it.
|
|
28
|
+
if ((await mkdir(dir, { recursive: true, mode: 0o700 })) !== undefined) await chmod(dir, 0o700);
|
|
29
|
+
const handle = await open(tmp, "wx", 0o600);
|
|
30
|
+
created = true;
|
|
31
|
+
try { await handle.writeFile(line); } finally { await handle.close(); }
|
|
32
|
+
await chmod(tmp, 0o600);
|
|
33
|
+
await rename(tmp, join(dir, "status.json"));
|
|
34
|
+
} catch {
|
|
35
|
+
if (created) await unlink(tmp).catch(() => {});
|
|
36
|
+
throw new Error(FAILED);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Longest form is five characters, so the layout is sized once. Decimals are
|
|
2
|
+
// truncated, not rounded: 9.96M stays "9.9M".
|
|
3
|
+
function ladder(n: number): string {
|
|
4
|
+
if (n >= 1e12) return "999B+";
|
|
5
|
+
for (const [unit, size] of [["B", 1e9], ["M", 1e6], ["k", 1e3]] as const) {
|
|
6
|
+
if (n < size) continue;
|
|
7
|
+
const v = n / size;
|
|
8
|
+
if (unit !== "k" && v < 10) return `${(Math.floor(v * 10) / 10).toFixed(1)}${unit}`;
|
|
9
|
+
return `${Math.floor(v)}${unit}`;
|
|
10
|
+
}
|
|
11
|
+
return String(n);
|
|
12
|
+
}
|
|
13
|
+
export const formatCount = (n: number): string => (n < 10_000 ? String(n) : ladder(n));
|
|
14
|
+
export const formatTokens = (n: number): string => (n < 1000 ? String(n) : ladder(n));
|
|
15
|
+
|
|
16
|
+
export const plural = (n: number, one: string, many = `${one}s`, count = formatCount): string => `${count(n)} ${n === 1 ? one : many}`;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { formatCount, plural } from "./format.ts";
|
|
2
|
+
import { levelOf } from "../metrics/index.ts";
|
|
3
|
+
import type { Day, HourBucket, Level } from "../types.ts";
|
|
4
|
+
|
|
5
|
+
const GLYPH: Record<Level, string> = { Calm: "░", Warming: "▒", Heating: "▓", Fried: "█" };
|
|
6
|
+
const ANSI: Record<Level, string> = { Calm: "32", Warming: "33", Heating: "35", Fried: "31" };
|
|
7
|
+
const LEVEL_NAME: Record<Level, string> = { Calm: "calm", Warming: "warming", Heating: "heating", Fried: "fried" };
|
|
8
|
+
const LEVELS: Level[] = ["Calm", "Warming", "Heating", "Fried"];
|
|
9
|
+
const WEEKDAY = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
10
|
+
|
|
11
|
+
const paint = (s: string, level: Level, color: boolean) => (color ? `\x1b[${ANSI[level]}m${s}\x1b[0m` : s);
|
|
12
|
+
const dim = (s: string, color: boolean) => (color ? `\x1b[2m${s}\x1b[0m` : s);
|
|
13
|
+
const hm = (min: number) => `${Math.floor(min / 60)}h${String(min % 60).padStart(2, "0")}`;
|
|
14
|
+
const hhmm = (d: Date) => `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
|
|
15
|
+
// Marks the snapshot time on the open day, so two runs minutes apart are
|
|
16
|
+
// explained rather than silently disagreeing.
|
|
17
|
+
const snapshotLine = (d: Day | undefined, color: boolean): string[] =>
|
|
18
|
+
d?.asOf ? [dim(` as of ${hhmm(new Date(d.asOf))}, this hour is still running`, color)] : [];
|
|
19
|
+
// The weekday of the calendar date, in UTC: a date the local zone skipped
|
|
20
|
+
// has no local midnight to ask.
|
|
21
|
+
const label = (date: string) => {
|
|
22
|
+
const [y, m, d] = date.split("-").map(Number) as [number, number, number];
|
|
23
|
+
return `${WEEKDAY[new Date(Date.UTC(y, m - 1, d)).getUTCDay()]} ${String(d).padStart(2, "0")}/${String(m).padStart(2, "0")}`;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function renderWeek(days: Day[], color: boolean): string {
|
|
27
|
+
// 12-char label, 24 cells of 3 chars, peak in 6, active in 8.
|
|
28
|
+
const header = " " + Array.from({ length: 24 }, (_, h) => `${String(h).padStart(2, "0")} `).join("") + " peak active";
|
|
29
|
+
const rows = days.map((d) => {
|
|
30
|
+
const cells = d.buckets.map((b) => ` ${b.score ? paint(GLYPH[b.score.level], b.score.level, color) : "·"} `).join("");
|
|
31
|
+
// Padding stays outside the paint so the escape codes add no width.
|
|
32
|
+
const peak = d.peak === null ? "-".padStart(6) : " ".repeat(6 - String(d.peak).length) + paint(String(d.peak), levelOf(d.peak), color);
|
|
33
|
+
const row = `${label(d.date).padEnd(12)}${cells}${peak}${hm(d.activeMin).padStart(8)}`;
|
|
34
|
+
// A day no hour scored in is 24 dots and a dash: no escape of its own, so
|
|
35
|
+
// one dim wrapper is enough.
|
|
36
|
+
return d.peak === null ? dim(row, color) : row;
|
|
37
|
+
});
|
|
38
|
+
// A painted glyph's own \x1b[0m cancels the line's dim; re-emit it after.
|
|
39
|
+
const dimGlyph = (level: Level) => paint(GLYPH[level], level, color) + (color ? "\x1b[2m" : "");
|
|
40
|
+
const legend = dim(" " + LEVELS.map((l) => `${dimGlyph(l)} ${LEVEL_NAME[l]}`).join(" "), color);
|
|
41
|
+
const active = days.reduce((s, d) => s + d.activeMin, 0);
|
|
42
|
+
const prompts = days.reduce((s, d) => s + d.totals.prompts, 0);
|
|
43
|
+
const reports = days.reduce((s, d) => s + d.totals.reports, 0);
|
|
44
|
+
const decisions = days.reduce((s, d) => s + d.totals.decisions, 0);
|
|
45
|
+
const maxSessions = Math.max(0, ...days.map((d) => d.totals.maxSessions));
|
|
46
|
+
// Compact counts keep a very active window inside the grid's 98 columns.
|
|
47
|
+
const totals = dim(` ${hm(active)} active ${plural(prompts, "prompt")} ${plural(reports, "report")} ${plural(decisions, "decision")} ${plural(maxSessions, "session")} at once`, color);
|
|
48
|
+
// Today need not be the last row: --to may name a later day.
|
|
49
|
+
return [header, ...rows, "", legend, totals, ...snapshotLine(days.find((d) => d.asOf), color)].join("\n");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const fmtTokens = (n: number): string => (n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n));
|
|
53
|
+
|
|
54
|
+
type Col = [string, number, (b: HourBucket) => string];
|
|
55
|
+
|
|
56
|
+
// Hour left-aligned, level left-aligned after two spaces, the rest right-aligned.
|
|
57
|
+
const COLS: Col[] = [
|
|
58
|
+
["hour", 5, (b) => `${String(b.hour).padStart(2, "0")}:00`],
|
|
59
|
+
["index", 7, (b) => String(b.score?.index ?? "")],
|
|
60
|
+
["level", 9, (b) => b.score?.level ?? ""],
|
|
61
|
+
["sess", 6, (b) => String(b.sessions)],
|
|
62
|
+
["prompts", 9, (b) => String(b.prompts)],
|
|
63
|
+
["rep", 5, (b) => String(b.reports)],
|
|
64
|
+
["intr", 6, (b) => String(b.interrupts)],
|
|
65
|
+
["rej", 5, (b) => String(b.rejects)],
|
|
66
|
+
["quest", 7, (b) => String(b.questions)],
|
|
67
|
+
["plan", 6, (b) => String(b.plans)],
|
|
68
|
+
["mode", 6, (b) => String(b.modeSwitches)],
|
|
69
|
+
["ctx-sw", 8, (b) => String(b.contextSwitches)],
|
|
70
|
+
["streak", 8, (b) => `${b.streakMin}m`],
|
|
71
|
+
["out-tok", 9, (b) => fmtTokens(b.outputTokens)],
|
|
72
|
+
];
|
|
73
|
+
const EXPLAIN: Col[] = [
|
|
74
|
+
["par", 5, (b) => String(b.score?.parts.parallel ?? "")],
|
|
75
|
+
["pace", 6, (b) => String(b.score?.parts.pace ?? "")],
|
|
76
|
+
["sup", 6, (b) => String(b.score?.parts.supervision ?? "")],
|
|
77
|
+
["read", 6, (b) => String(b.score?.parts.reading ?? "")],
|
|
78
|
+
["strk", 6, (b) => String(b.score?.parts.streak ?? "")],
|
|
79
|
+
["late", 6, (b) => String(b.score?.parts.late ?? "")],
|
|
80
|
+
];
|
|
81
|
+
// Left out of the table for a day where every active bucket reads zero; the
|
|
82
|
+
// other columns always show, so two days still line up.
|
|
83
|
+
const EVENT_COLS = new Set(["rep", "intr", "rej", "quest", "plan", "mode", "ctx-sw"]);
|
|
84
|
+
const FULL_NAME: Record<string, string> = {
|
|
85
|
+
rep: "reports", intr: "interrupts", rej: "rejects", quest: "questions",
|
|
86
|
+
plan: "plans", mode: "mode switches", "ctx-sw": "context switches",
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export function renderDay(day: Day, opts: { explain: boolean; color: boolean }): string {
|
|
90
|
+
const cols = opts.explain ? [...COLS, ...EXPLAIN] : COLS;
|
|
91
|
+
const line = (columns: Col[], cells: string[]) =>
|
|
92
|
+
cells.map((c, i) => { const w = columns[i]![1]; if (i === 0) return c.padEnd(w); if (columns[i]![0] === "level") return ` ${c.padEnd(w - 2)}`; return c.padStart(w); }).join("").trimEnd();
|
|
93
|
+
|
|
94
|
+
const active = day.buckets.filter((b) => b.score !== null);
|
|
95
|
+
// The snapshot line still shows on an empty open day, or a run at 09:00 and
|
|
96
|
+
// one at 18:00 would print the identical line.
|
|
97
|
+
if (active.length === 0) return [line(cols, cols.map(([name]) => name)), ...snapshotLine(day, opts.color)].join("\n");
|
|
98
|
+
|
|
99
|
+
const visible = cols.filter(([name, , f]) => !EVENT_COLS.has(name) || active.some((b) => f(b) !== "0"));
|
|
100
|
+
const leftOut = cols.filter((col) => EVENT_COLS.has(col[0]) && !visible.includes(col));
|
|
101
|
+
|
|
102
|
+
const header = line(visible, visible.map(([name]) => name));
|
|
103
|
+
const rows = active.map((b) => {
|
|
104
|
+
const cells = visible.map(([, , f]) => f(b));
|
|
105
|
+
const text = line(visible, cells);
|
|
106
|
+
// Safe only while no other column can contain a level word.
|
|
107
|
+
return opts.color && b.score ? text.replace(b.score.level, paint(b.score.level, b.score.level, true)) : text;
|
|
108
|
+
});
|
|
109
|
+
const lines = [header, ...rows];
|
|
110
|
+
if (leftOut.length > 0) {
|
|
111
|
+
const note = ` no ${leftOut.map(([name]) => FULL_NAME[name]).join(", ")} today`;
|
|
112
|
+
lines.push(dim(note, opts.color));
|
|
113
|
+
}
|
|
114
|
+
lines.push(...snapshotLine(day, opts.color));
|
|
115
|
+
return lines.join("\n");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function renderJson(data: Day[] | Day): string {
|
|
119
|
+
return JSON.stringify(data, null, 2);
|
|
120
|
+
}
|