hacklab 0.10.4 → 0.11.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.
Files changed (42) hide show
  1. package/README.md +54 -12
  2. package/dist/cli.js +1 -0
  3. package/dist/cli.js.map +1 -1
  4. package/dist/commands/config.d.ts.map +1 -1
  5. package/dist/commands/config.js +27 -2
  6. package/dist/commands/config.js.map +1 -1
  7. package/dist/commands/daemon.d.ts +6 -2
  8. package/dist/commands/daemon.d.ts.map +1 -1
  9. package/dist/commands/daemon.js +12 -8
  10. package/dist/commands/daemon.js.map +1 -1
  11. package/dist/commands/sync.d.ts +1 -0
  12. package/dist/commands/sync.d.ts.map +1 -1
  13. package/dist/commands/sync.js +158 -7
  14. package/dist/commands/sync.js.map +1 -1
  15. package/dist/config.d.ts +6 -0
  16. package/dist/config.d.ts.map +1 -1
  17. package/dist/config.js.map +1 -1
  18. package/dist/daily-sync.d.ts +20 -3
  19. package/dist/daily-sync.d.ts.map +1 -1
  20. package/dist/daily-sync.js +154 -44
  21. package/dist/daily-sync.js.map +1 -1
  22. package/dist/prompt-consent.d.ts +61 -0
  23. package/dist/prompt-consent.d.ts.map +1 -0
  24. package/dist/prompt-consent.js +167 -0
  25. package/dist/prompt-consent.js.map +1 -0
  26. package/dist/prompt-stats.d.ts +72 -0
  27. package/dist/prompt-stats.d.ts.map +1 -0
  28. package/dist/prompt-stats.js +244 -0
  29. package/dist/prompt-stats.js.map +1 -0
  30. package/dist/scanners/incremental.d.ts +144 -0
  31. package/dist/scanners/incremental.d.ts.map +1 -0
  32. package/dist/scanners/incremental.js +519 -0
  33. package/dist/scanners/incremental.js.map +1 -0
  34. package/dist/scanners/index.d.ts +44 -0
  35. package/dist/scanners/index.d.ts.map +1 -1
  36. package/dist/scanners/index.js +160 -126
  37. package/dist/scanners/index.js.map +1 -1
  38. package/dist/sync.d.ts +25 -0
  39. package/dist/sync.d.ts.map +1 -1
  40. package/dist/sync.js +52 -5
  41. package/dist/sync.js.map +1 -1
  42. package/package.json +1 -1
@@ -0,0 +1,167 @@
1
+ import { createInterface } from 'node:readline';
2
+ import { loadConfig, saveConfig } from './config.js';
3
+ import { bold, dim, info } from './ui.js';
4
+ /**
5
+ * Consent for uploading anything derived from the user's Claude Code
6
+ * conversations.
7
+ *
8
+ * Nothing conversation-derived leaves the machine until someone has answered
9
+ * this explicitly. The tiers are additive:
10
+ *
11
+ * none token counts only — exactly what the CLI did before this existed
12
+ * stats + prompt-length histogram and per-project prompt counts (numbers
13
+ * only; no prompt text ever leaves the machine)
14
+ * full + a sample of the raw prompt text, so the backend can score how
15
+ * technical the prompting is. It scores the sample and discards it.
16
+ *
17
+ * The answer is remembered in ~/.hacklab/config.json so the question is asked
18
+ * once, and can be changed or revoked at any time with
19
+ * `hacklab config prompt-stats <tier>`.
20
+ */
21
+ export const PROMPT_CONSENT_TIERS = ['none', 'stats', 'full'];
22
+ export function isPromptConsentTier(value) {
23
+ return (typeof value === 'string' &&
24
+ PROMPT_CONSENT_TIERS.includes(value));
25
+ }
26
+ /** The stored answer, or null when the user has never been asked. */
27
+ export async function loadPromptConsent() {
28
+ const config = await loadConfig();
29
+ return isPromptConsentTier(config.promptStatsConsent)
30
+ ? config.promptStatsConsent
31
+ : null;
32
+ }
33
+ export async function savePromptConsent(tier) {
34
+ const config = await loadConfig();
35
+ config.promptStatsConsent = tier;
36
+ await saveConfig(config);
37
+ }
38
+ /**
39
+ * Read `--share-prompt-stats[=<tier>]` out of a command's args.
40
+ *
41
+ * This is the agent-friendly path: it answers the question up front so an
42
+ * unattended run never blocks on a prompt it can't see. Bare
43
+ * `--share-prompt-stats` means the numbers-only tier — the safe reading of an
44
+ * unqualified yes, since sending prompt *text* should always be deliberate.
45
+ * `--no-share-prompt-stats` is an explicit refusal.
46
+ *
47
+ * Returns the chosen tier, or null when the flag is absent. `rest` is the args
48
+ * with the flag removed so per-command parsing never sees it.
49
+ */
50
+ export function parsePromptStatsFlag(args) {
51
+ const rest = [];
52
+ let tier = null;
53
+ for (const arg of args) {
54
+ if (arg === '--no-share-prompt-stats') {
55
+ tier = 'none';
56
+ continue;
57
+ }
58
+ if (arg === '--share-prompt-stats') {
59
+ tier = 'stats';
60
+ continue;
61
+ }
62
+ if (arg.startsWith('--share-prompt-stats=')) {
63
+ const value = arg.slice('--share-prompt-stats='.length);
64
+ tier = isPromptConsentTier(value) ? value : 'stats';
65
+ continue;
66
+ }
67
+ rest.push(arg);
68
+ }
69
+ return { tier, rest };
70
+ }
71
+ /**
72
+ * Ask a yes/no question with no default: Enter alone re-asks, and only `y` or
73
+ * `n` settles it. Deliberately not @clack/prompts' confirm, which ships a
74
+ * highlighted default that can be accepted with a stray Enter — consent to
75
+ * upload conversation data should never be something you agree to by
76
+ * reflex.
77
+ *
78
+ * Returns null on a non-TTY stdin or EOF, which callers treat as "unanswered"
79
+ * rather than as a yes.
80
+ */
81
+ export async function askYesNo(question) {
82
+ if (!process.stdin.isTTY)
83
+ return null;
84
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
85
+ try {
86
+ for (;;) {
87
+ const answer = await new Promise((resolve) => {
88
+ rl.question(`${question} ${dim('[y/n]')} `, (value) => resolve(value));
89
+ rl.once('close', () => resolve(null));
90
+ });
91
+ if (answer === null)
92
+ return null;
93
+ const normalized = answer.trim().toLowerCase();
94
+ if (normalized === 'y' || normalized === 'yes')
95
+ return true;
96
+ if (normalized === 'n' || normalized === 'no')
97
+ return false;
98
+ // Anything else (including a bare Enter) is not an answer.
99
+ console.log(dim(' please press y or n.'));
100
+ }
101
+ }
102
+ finally {
103
+ rl.close();
104
+ }
105
+ }
106
+ /** The disclosure. Printed verbatim before the first question. */
107
+ function printDisclosure() {
108
+ console.log('');
109
+ console.log(bold(' share prompt stats?'));
110
+ console.log('');
111
+ info(' hacklab can read your local Claude Code history to chart how you prompt.');
112
+ info(` it is read on this machine, from ${dim('~/.claude/projects')}.`);
113
+ console.log('');
114
+ info(` ${bold('what would be uploaded')}`);
115
+ info(' how many prompts you have sent, and how long they are in words');
116
+ info(' a prompt count per project, matched by its git remote');
117
+ console.log('');
118
+ info(` ${bold('what would not')}`);
119
+ info(' the text of your prompts, unless you separately say yes below');
120
+ info(' anything from a project without a git remote');
121
+ console.log('');
122
+ info(dim(' hacklab stores the numbers on your profile. you can change or revoke'));
123
+ info(dim(' this later with `hacklab config prompt-stats <none|stats|full>`.'));
124
+ console.log('');
125
+ }
126
+ /**
127
+ * Resolve the consent tier for this run.
128
+ *
129
+ * Precedence: an explicit flag wins (and is remembered), then the stored
130
+ * answer, then the interactive questions. An unattended run that has never
131
+ * answered gets `none` — silence is never consent.
132
+ */
133
+ export async function resolvePromptConsent(flagTier, opts = {}) {
134
+ if (flagTier) {
135
+ await savePromptConsent(flagTier);
136
+ return flagTier;
137
+ }
138
+ const stored = await loadPromptConsent();
139
+ if (stored)
140
+ return stored;
141
+ if (!opts.interactive || !process.stdin.isTTY)
142
+ return 'none';
143
+ printDisclosure();
144
+ const shareNumbers = await askYesNo(' share these numbers with hacklab?');
145
+ if (shareNumbers === null)
146
+ return 'none';
147
+ if (!shareNumbers) {
148
+ await savePromptConsent('none');
149
+ console.log('');
150
+ info(dim(' no problem — syncing token counts only, as before.'));
151
+ return 'none';
152
+ }
153
+ console.log('');
154
+ info(' optional: a sample of your prompt text (up to 20k characters) can be');
155
+ info(' sent so an LLM can score how technical your prompting is. hacklab');
156
+ info(` scores it and ${bold('discards it')} — the text is never stored.`);
157
+ console.log('');
158
+ const shareText = await askYesNo(' also send a sample of your prompt text?');
159
+ const tier = shareText === true ? 'full' : 'stats';
160
+ await savePromptConsent(tier);
161
+ console.log('');
162
+ info(dim(tier === 'full'
163
+ ? ' sharing prompt stats and a text sample.'
164
+ : ' sharing prompt stats. numbers only, no text.'));
165
+ return tier;
166
+ }
167
+ //# sourceMappingURL=prompt-consent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompt-consent.js","sourceRoot":"","sources":["../src/prompt-consent.ts"],"names":[],"mappings":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C,CAAA;IACf,IAAI,CACF,GAAG,CACD,IAAI,KAAK,MAAM;QACb,CAAC,CAAC,2CAA2C;QAC7C,CAAC,CAAC,gDAAgD,CACrD,CACF,CAAA;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
@@ -0,0 +1,72 @@
1
+ /** Buckets are `1..bucketMax`, so the axis stays readable at any prompt length. */
2
+ export declare const PROMPT_LENGTH_BUCKET_MIN = 10;
3
+ export declare const PROMPT_LENGTH_BUCKET_MAX = 100;
4
+ /** The percentile that sets `bucketMax`; everything above lands in the overflow. */
5
+ export declare const PROMPT_LENGTH_OVERFLOW_PERCENTILE = 0.9;
6
+ /** Matches the backend's cap — anything longer is truncated before upload. */
7
+ export declare const CONVERSATION_SAMPLE_MAX_CHARS = 20000;
8
+ export type PromptStatsProject = {
9
+ repoUrl: string;
10
+ promptCount: number;
11
+ lastActiveAt: string;
12
+ };
13
+ export type PromptStats = {
14
+ totalPrompts: number;
15
+ bucketMax: number;
16
+ histogram: {
17
+ length: number;
18
+ count: number;
19
+ }[];
20
+ projects: PromptStatsProject[];
21
+ /** Only ever set under the `full` consent tier. */
22
+ conversationSample?: string;
23
+ };
24
+ /**
25
+ * The user's own prompt text from one transcript line, or null when the line
26
+ * isn't one.
27
+ *
28
+ * A transcript's `user` entries cover more than typed prompts: tool results
29
+ * come back as synthetic user turns too. Those carry `tool_result` content
30
+ * blocks, so a line only counts when its content is a plain string or an array
31
+ * of nothing but `text` blocks. Sidechain entries (subagent conversations) are
32
+ * the agent talking to itself, not the person typing, so they're excluded.
33
+ */
34
+ export declare function promptTextFrom(entry: unknown): string | null;
35
+ /** Whitespace-separated word count. Zero-word prompts are dropped by callers. */
36
+ export declare function countWords(text: string): number;
37
+ /**
38
+ * The overflow threshold for this user's own distribution: their p90 prompt
39
+ * length rounded up to a multiple of 10, clamped to [10, 100].
40
+ *
41
+ * Per-user rather than fixed because prompt length varies enormously between
42
+ * people — a fixed axis would either crush a terse user's histogram into the
43
+ * first two buckets or run a verbose one off the end.
44
+ */
45
+ export declare function bucketMaxFor(wordCounts: number[]): number;
46
+ /**
47
+ * Bucket the word counts. Buckets `1..bucketMax-1` are exact word counts; the
48
+ * bucket at `bucketMax` is the overflow, holding every prompt that long or
49
+ * longer. Empty buckets are omitted — the chart fills the gaps.
50
+ */
51
+ export declare function buildHistogram(wordCounts: number[], bucketMax: number): {
52
+ length: number;
53
+ count: number;
54
+ }[];
55
+ /**
56
+ * The `origin` remote of the repo at `cwd`, or null when there isn't one (not
57
+ * a repo, no origin, no git binary, or the directory is gone). The URL is sent
58
+ * as-is; the backend normalizes it before matching against the user's own
59
+ * projects.
60
+ */
61
+ export declare function gitOriginUrl(cwd: string): Promise<string | null>;
62
+ /**
63
+ * Scan the local Claude Code transcripts.
64
+ *
65
+ * `includeSample` gates the raw prompt text: only the `full` consent tier
66
+ * passes true, and the sample never touches disk here — it goes straight into
67
+ * the upload payload the caller builds.
68
+ */
69
+ export declare function scanPromptStats(options?: {
70
+ includeSample?: boolean;
71
+ }): Promise<PromptStats | null>;
72
+ //# sourceMappingURL=prompt-stats.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompt-stats.d.ts","sourceRoot":"","sources":["../src/prompt-stats.ts"],"names":[],"mappings":"AA6BA,mFAAmF;AACnF,eAAO,MAAM,wBAAwB,KAAK,CAAA;AAC1C,eAAO,MAAM,wBAAwB,MAAM,CAAA;AAC3C,oFAAoF;AACpF,eAAO,MAAM,iCAAiC,MAAM,CAAA;AACpD,8EAA8E;AAC9E,eAAO,MAAM,6BAA6B,QAAS,CAAA;AAEnD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE,MAAM,CAAA;CACrB,CAAA;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,YAAY,EAAE,MAAM,CAAA;IACpB,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;IAC9C,QAAQ,EAAE,kBAAkB,EAAE,CAAA;IAC9B,mDAAmD;IACnD,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B,CAAA;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAwB5D;AAED,iFAAiF;AACjF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG/C;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,MAAM,CAazD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,UAAU,EAAE,MAAM,EAAE,EACpB,SAAS,EAAE,MAAM,GAChB;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,CAUrC;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAYtE;AAQD;;;;;;GAMG;AACH,wBAAsB,eAAe,CACnC,OAAO,GAAE;IAAE,aAAa,CAAC,EAAE,OAAO,CAAA;CAAO,GACxC,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAoF7B"}
@@ -0,0 +1,244 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { homedir } from 'node:os';
4
+ import { dirname, join } from 'node:path';
5
+ import { promisify } from 'node:util';
6
+ import { findFiles } from './scanners/util.js';
7
+ /**
8
+ * Prompt statistics, computed entirely on this machine from the local Claude
9
+ * Code transcripts and uploaded only under an explicit consent tier (see
10
+ * prompt-consent.ts).
11
+ *
12
+ * Two numbers come out of a scan:
13
+ *
14
+ * - a histogram of how long the user's own prompts are, in words
15
+ * - a per-project prompt count, keyed by the project's git origin
16
+ *
17
+ * and, under the `full` tier only, a sample of the raw prompt text so the
18
+ * backend can score "how technical is this person's prompting". The backend
19
+ * scores that sample and throws it away; it is never stored.
20
+ *
21
+ * Everything here is deliberately best-effort: an unreadable transcript, a
22
+ * project that isn't a git repo, a missing `git` binary — each drops its own
23
+ * contribution and the rest of the scan still uploads.
24
+ */
25
+ const execFileAsync = promisify(execFile);
26
+ /** Buckets are `1..bucketMax`, so the axis stays readable at any prompt length. */
27
+ export const PROMPT_LENGTH_BUCKET_MIN = 10;
28
+ export const PROMPT_LENGTH_BUCKET_MAX = 100;
29
+ /** The percentile that sets `bucketMax`; everything above lands in the overflow. */
30
+ export const PROMPT_LENGTH_OVERFLOW_PERCENTILE = 0.9;
31
+ /** Matches the backend's cap — anything longer is truncated before upload. */
32
+ export const CONVERSATION_SAMPLE_MAX_CHARS = 20_000;
33
+ /**
34
+ * The user's own prompt text from one transcript line, or null when the line
35
+ * isn't one.
36
+ *
37
+ * A transcript's `user` entries cover more than typed prompts: tool results
38
+ * come back as synthetic user turns too. Those carry `tool_result` content
39
+ * blocks, so a line only counts when its content is a plain string or an array
40
+ * of nothing but `text` blocks. Sidechain entries (subagent conversations) are
41
+ * the agent talking to itself, not the person typing, so they're excluded.
42
+ */
43
+ export function promptTextFrom(entry) {
44
+ if (!entry || typeof entry !== 'object')
45
+ return null;
46
+ const line = entry;
47
+ if (line.type !== 'user')
48
+ return null;
49
+ if (line.isSidechain === true)
50
+ return null;
51
+ const content = line.message?.content;
52
+ if (typeof content === 'string')
53
+ return content;
54
+ if (!Array.isArray(content))
55
+ return null;
56
+ const texts = [];
57
+ for (const block of content) {
58
+ if (!block || typeof block !== 'object')
59
+ return null;
60
+ const { type, text } = block;
61
+ // One non-text block (a tool_result, an image) disqualifies the whole
62
+ // entry: it isn't a prompt the person typed.
63
+ if (type !== 'text' || typeof text !== 'string')
64
+ return null;
65
+ texts.push(text);
66
+ }
67
+ return texts.length > 0 ? texts.join('\n') : null;
68
+ }
69
+ /** Whitespace-separated word count. Zero-word prompts are dropped by callers. */
70
+ export function countWords(text) {
71
+ const matches = text.match(/\S+/g);
72
+ return matches ? matches.length : 0;
73
+ }
74
+ /**
75
+ * The overflow threshold for this user's own distribution: their p90 prompt
76
+ * length rounded up to a multiple of 10, clamped to [10, 100].
77
+ *
78
+ * Per-user rather than fixed because prompt length varies enormously between
79
+ * people — a fixed axis would either crush a terse user's histogram into the
80
+ * first two buckets or run a verbose one off the end.
81
+ */
82
+ export function bucketMaxFor(wordCounts) {
83
+ if (wordCounts.length === 0)
84
+ return PROMPT_LENGTH_BUCKET_MIN;
85
+ const sorted = [...wordCounts].sort((a, b) => a - b);
86
+ const index = Math.min(sorted.length - 1, Math.floor(sorted.length * PROMPT_LENGTH_OVERFLOW_PERCENTILE));
87
+ const p90 = sorted[index] ?? PROMPT_LENGTH_BUCKET_MIN;
88
+ const rounded = Math.ceil(p90 / 10) * 10;
89
+ return Math.min(PROMPT_LENGTH_BUCKET_MAX, Math.max(PROMPT_LENGTH_BUCKET_MIN, rounded));
90
+ }
91
+ /**
92
+ * Bucket the word counts. Buckets `1..bucketMax-1` are exact word counts; the
93
+ * bucket at `bucketMax` is the overflow, holding every prompt that long or
94
+ * longer. Empty buckets are omitted — the chart fills the gaps.
95
+ */
96
+ export function buildHistogram(wordCounts, bucketMax) {
97
+ const counts = new Map();
98
+ for (const words of wordCounts) {
99
+ if (words <= 0)
100
+ continue;
101
+ const bucket = words >= bucketMax ? bucketMax : words;
102
+ counts.set(bucket, (counts.get(bucket) ?? 0) + 1);
103
+ }
104
+ return [...counts.entries()]
105
+ .map(([length, count]) => ({ length, count }))
106
+ .sort((a, b) => a.length - b.length);
107
+ }
108
+ /**
109
+ * The `origin` remote of the repo at `cwd`, or null when there isn't one (not
110
+ * a repo, no origin, no git binary, or the directory is gone). The URL is sent
111
+ * as-is; the backend normalizes it before matching against the user's own
112
+ * projects.
113
+ */
114
+ export async function gitOriginUrl(cwd) {
115
+ try {
116
+ const { stdout } = await execFileAsync('git', ['-C', cwd, 'remote', 'get-url', 'origin'], { timeout: 5_000 });
117
+ const url = stdout.trim();
118
+ return url.length > 0 ? url : null;
119
+ }
120
+ catch {
121
+ return null;
122
+ }
123
+ }
124
+ /**
125
+ * Scan the local Claude Code transcripts.
126
+ *
127
+ * `includeSample` gates the raw prompt text: only the `full` consent tier
128
+ * passes true, and the sample never touches disk here — it goes straight into
129
+ * the upload payload the caller builds.
130
+ */
131
+ export async function scanPromptStats(options = {}) {
132
+ const root = join(homedir(), '.claude', 'projects');
133
+ const files = await findFiles(root, '.jsonl');
134
+ if (files.length === 0)
135
+ return null;
136
+ const wordCounts = [];
137
+ const sampleParts = [];
138
+ let sampleChars = 0;
139
+ // Keyed by the transcript's project directory, which is Claude Code's own
140
+ // grouping. The directory name is a lossy encoding of the path, so the real
141
+ // working directory comes from the `cwd` recorded inside the entries.
142
+ const byProjectDir = new Map();
143
+ for (const filePath of files) {
144
+ const projectDir = dirname(filePath);
145
+ let project = byProjectDir.get(projectDir);
146
+ if (!project) {
147
+ project = { cwd: null, promptCount: 0, lastActiveAt: 0 };
148
+ byProjectDir.set(projectDir, project);
149
+ }
150
+ let content;
151
+ try {
152
+ content = await readFile(filePath, 'utf8');
153
+ }
154
+ catch {
155
+ continue;
156
+ }
157
+ for (const line of content.split('\n')) {
158
+ if (!line.trim())
159
+ continue;
160
+ let parsed;
161
+ try {
162
+ parsed = JSON.parse(line);
163
+ }
164
+ catch {
165
+ continue;
166
+ }
167
+ const entry = parsed;
168
+ if (!project.cwd && typeof entry.cwd === 'string' && entry.cwd) {
169
+ project.cwd = entry.cwd;
170
+ }
171
+ const text = promptTextFrom(parsed);
172
+ if (text === null)
173
+ continue;
174
+ const words = countWords(text);
175
+ if (words <= 0)
176
+ continue;
177
+ wordCounts.push(words);
178
+ project.promptCount += 1;
179
+ if (typeof entry.timestamp === 'string') {
180
+ const at = Date.parse(entry.timestamp);
181
+ if (Number.isFinite(at) && at > project.lastActiveAt) {
182
+ project.lastActiveAt = at;
183
+ }
184
+ }
185
+ if (options.includeSample &&
186
+ sampleChars < CONVERSATION_SAMPLE_MAX_CHARS) {
187
+ sampleParts.push(text);
188
+ sampleChars += text.length + 2;
189
+ }
190
+ }
191
+ }
192
+ if (wordCounts.length === 0)
193
+ return null;
194
+ const bucketMax = bucketMaxFor(wordCounts);
195
+ const stats = {
196
+ totalPrompts: wordCounts.length,
197
+ bucketMax,
198
+ histogram: buildHistogram(wordCounts, bucketMax),
199
+ projects: await resolveProjects(byProjectDir),
200
+ };
201
+ if (options.includeSample && sampleParts.length > 0) {
202
+ stats.conversationSample = sampleParts
203
+ .join('\n\n')
204
+ .slice(0, CONVERSATION_SAMPLE_MAX_CHARS);
205
+ }
206
+ return stats;
207
+ }
208
+ /**
209
+ * Turn the per-directory tallies into repo-keyed entries. Directories with no
210
+ * prompts, no recorded `cwd`, or no git origin drop out — the backend can only
211
+ * match a project by its repo URL, so an entry without one is dead weight.
212
+ *
213
+ * Two transcript directories can resolve to the same repo (the same project
214
+ * opened at different paths), so counts are summed per origin.
215
+ */
216
+ async function resolveProjects(byProjectDir) {
217
+ const byRepo = new Map();
218
+ for (const project of byProjectDir.values()) {
219
+ if (project.promptCount === 0 || !project.cwd)
220
+ continue;
221
+ const repoUrl = await gitOriginUrl(project.cwd);
222
+ if (!repoUrl)
223
+ continue;
224
+ const existing = byRepo.get(repoUrl);
225
+ if (existing) {
226
+ existing.promptCount += project.promptCount;
227
+ existing.lastActiveAt = Math.max(existing.lastActiveAt, project.lastActiveAt);
228
+ }
229
+ else {
230
+ byRepo.set(repoUrl, {
231
+ promptCount: project.promptCount,
232
+ lastActiveAt: project.lastActiveAt,
233
+ });
234
+ }
235
+ }
236
+ return [...byRepo.entries()].map(([repoUrl, entry]) => ({
237
+ repoUrl,
238
+ promptCount: entry.promptCount,
239
+ // A transcript with no usable timestamp still happened; dating it now is
240
+ // more honest than dropping the project or claiming the epoch.
241
+ lastActiveAt: new Date(entry.lastActiveAt > 0 ? entry.lastActiveAt : Date.now()).toISOString(),
242
+ }));
243
+ }
244
+ //# sourceMappingURL=prompt-stats.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompt-stats.js","sourceRoot":"","sources":["../src/prompt-stats.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAE9C;;;;;;;;;;;;;;;;;GAiBG;AAEH,MAAM,aAAa,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAA;AAEzC,mFAAmF;AACnF,MAAM,CAAC,MAAM,wBAAwB,GAAG,EAAE,CAAA;AAC1C,MAAM,CAAC,MAAM,wBAAwB,GAAG,GAAG,CAAA;AAC3C,oFAAoF;AACpF,MAAM,CAAC,MAAM,iCAAiC,GAAG,GAAG,CAAA;AACpD,8EAA8E;AAC9E,MAAM,CAAC,MAAM,6BAA6B,GAAG,MAAM,CAAA;AAiBnD;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAA;IACpD,MAAM,IAAI,GAAG,KAIZ,CAAA;IACD,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM;QAAE,OAAO,IAAI,CAAA;IACrC,IAAI,IAAI,CAAC,WAAW,KAAK,IAAI;QAAE,OAAO,IAAI,CAAA;IAE1C,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,OAAO,CAAA;IACrC,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,OAAO,CAA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YACb,QAAQ,CAAC,WAAW,IAAI,OAAO,CAAC,WAAW,CAAA;YAC3C,QAAQ,CAAC,YAAY,GAAG,IAAI,CAAC,GAAG,CAC9B,QAAQ,CAAC,YAAY,EACrB,OAAO,CAAC,YAAY,CACrB,CAAA;QACH,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE;gBAClB,WAAW,EAAE,OAAO,CAAC,WAAW;gBAChC,YAAY,EAAE,OAAO,CAAC,YAAY;aACnC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;QACtD,OAAO;QACP,WAAW,EAAE,KAAK,CAAC,WAAW;QAC9B,yEAAyE;QACzE,+DAA+D;QAC/D,YAAY,EAAE,IAAI,IAAI,CACpB,KAAK,CAAC,YAAY,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CACzD,CAAC,WAAW,EAAE;KAChB,CAAC,CAAC,CAAA;AACL,CAAC"}
@@ -0,0 +1,144 @@
1
+ import { type AggregateScan, type ScanResult, type TokensMessages, type Tool, type UsageLine } from './index.js';
2
+ export declare const SCAN_STATE_VERSION = 1;
3
+ /** Where a file's tail-follow stands: what we saw, and how far we read. */
4
+ export type FileState = {
5
+ size: number;
6
+ mtimeMs: number;
7
+ offset: number;
8
+ };
9
+ /** Codex only: the running total a session file has reported so far. */
10
+ export type CodexFileState = {
11
+ maxTotal: number;
12
+ model: string;
13
+ date: string;
14
+ };
15
+ export type HarnessState = {
16
+ /** JSONL harnesses: per-file read cursor, keyed by absolute path. */
17
+ files: Record<string, FileState>;
18
+ /** Codex only: per-file running totals (see codexFileTotals). */
19
+ codexTotals?: Record<string, CodexFileState>;
20
+ /** SQLite harnesses: fingerprints of the db and its -wal. */
21
+ dbMtimeMs?: number;
22
+ walMtimeMs?: number;
23
+ /** "date|model" -> tokens/messages */
24
+ daily: Record<string, TokensMessages>;
25
+ /** "date|hour|model" -> tokens/messages */
26
+ hourly: Record<string, TokensMessages>;
27
+ /** model -> cumulative tokens */
28
+ models: Record<string, number>;
29
+ };
30
+ export type ScanState = {
31
+ version: number;
32
+ harnesses: Record<string, HarnessState>;
33
+ /**
34
+ * Dates whose rows changed since the last accepted upload. Dates, not
35
+ * (harness, date) pairs, because the server reconciles a synced date by
36
+ * deleting this machine's rows for that date that the payload didn't
37
+ * mention — so a date we touch has to go out complete, with every harness's
38
+ * rows for it. Cleared only on a 200, so a failed POST retries next tick.
39
+ */
40
+ dirty: string[];
41
+ /** Cumulative totals as of the last accepted upload. */
42
+ uploaded: {
43
+ toolTotals: Record<string, number>;
44
+ modelTotals: Record<string, number>;
45
+ };
46
+ /** 429 backoff: ticks before this epoch-ms do nothing. */
47
+ nextAllowedAt?: number;
48
+ /** The last failure written to sync.log, so a stuck tick can't fill it. */
49
+ lastError?: string;
50
+ };
51
+ /** Lives next to the session file, so it honors HACKLAB_SESSION_PATH. */
52
+ export declare function scanStatePath(): string;
53
+ export declare function emptyHarness(): HarnessState;
54
+ export declare function emptyState(): ScanState;
55
+ /**
56
+ * The saved state, or null when there isn't a usable one — missing, corrupt, or
57
+ * written by an older layout. Null means "re-read everything from scratch";
58
+ * callers must treat that rebuild as a baseline, not as new tokens.
59
+ */
60
+ export declare function loadScanState(): Promise<ScanState | null>;
61
+ /** Persist the state, pruning hourly buckets that fell out of the 90-day window
62
+ * the server keeps (they'd otherwise accumulate forever). Best-effort. */
63
+ export declare function saveScanState(state: ScanState): Promise<void>;
64
+ type Aggregates = {
65
+ daily: Record<string, TokensMessages>;
66
+ hourly: Record<string, TokensMessages>;
67
+ models: Record<string, number>;
68
+ };
69
+ /** A full scanner's result as the maps the state stores. */
70
+ export declare function aggregatesOfResult(result: ScanResult): Aggregates;
71
+ /**
72
+ * Read `[from, to)` and hand back only whole lines. A harness may be mid-append
73
+ * when we look, so the trailing partial line is left for the next tick — that's
74
+ * what the returned offset points at.
75
+ */
76
+ export declare function readCompleteLines(path: string, from: number, to: number): Promise<{
77
+ text: string;
78
+ offset: number;
79
+ }>;
80
+ export type JsonlSource = {
81
+ tool: Tool;
82
+ files: () => Promise<string[]>;
83
+ parse: (line: string) => UsageLine | null;
84
+ };
85
+ export type CodexSource = {
86
+ files: () => Promise<string[]>;
87
+ /** Session date from the path layout, or null to fall back to the mtime day. */
88
+ dateFor: (path: string) => string | null;
89
+ };
90
+ export type SqliteSource = {
91
+ tool: Tool;
92
+ dbPath: () => string;
93
+ scan: () => Promise<ScanResult>;
94
+ };
95
+ export type TickSources = {
96
+ jsonl: JsonlSource[];
97
+ codex: CodexSource;
98
+ sqlite: SqliteSource[];
99
+ };
100
+ /** The real harnesses. Injectable so the tick can be tested against a tmp dir. */
101
+ export declare function defaultSources(): TickSources;
102
+ export type TickOutcome = {
103
+ state: ScanState;
104
+ /** Did anything on disk move? (If not, the state file isn't worth rewriting.) */
105
+ changed: boolean;
106
+ };
107
+ /**
108
+ * One incremental pass over every harness. `prev` is the saved state, or null
109
+ * for a cold start — in which case this reads everything and marks nothing
110
+ * dirty: those tokens are already on the server (the daily job put them there),
111
+ * and re-uploading every date would only invite the reconcile to prune rows
112
+ * this scan can't see (Cursor's, above all).
113
+ */
114
+ export declare function runTick(prev: ScanState | null, sources?: TickSources): Promise<TickOutcome>;
115
+ /** Cumulative per-tool and per-model totals for this machine. */
116
+ export declare function cumulativeTotals(state: ScanState): {
117
+ toolTotals: Record<string, number>;
118
+ modelTotals: Record<string, number>;
119
+ };
120
+ export declare function sameTotals(a: Record<string, number>, b: Record<string, number>): boolean;
121
+ /**
122
+ * What a tick uploads: cumulative tool/model totals (the server diffs those
123
+ * against its own per-machine snapshot) plus the daily and hourly rows for the
124
+ * dirty dates only.
125
+ *
126
+ * Every harness's rows go out for a dirty date, including Cursor's — which the
127
+ * tick never scans, and carries over from the last full scan instead. The
128
+ * server reconciles a date by deleting this machine's rows for it that the
129
+ * payload didn't mention, so a date has to be reported complete or the harnesses
130
+ * left out of it lose that day.
131
+ */
132
+ export declare function tickPayload(state: ScanState): AggregateScan;
133
+ /** Called on a 200: the dirty dates are on the server now. */
134
+ export declare function markUploaded(state: ScanState): void;
135
+ /**
136
+ * Rebuild the whole state from a full scan's results — the self-healing half of
137
+ * the design. Whatever the tick's tail-follow got wrong (a rotated log, a
138
+ * mid-file model switch, a Codex date guessed from an mtime) is corrected here,
139
+ * so incremental drift can never outlive a day. Best-effort: a state we failed
140
+ * to rebuild costs a tick, never the sync that just succeeded.
141
+ */
142
+ export declare function rebuildScanState(results: ScanResult[], sources?: TickSources): Promise<void>;
143
+ export {};
144
+ //# sourceMappingURL=incremental.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"incremental.d.ts","sourceRoot":"","sources":["../../src/scanners/incremental.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,KAAK,aAAa,EAalB,KAAK,UAAU,EAGf,KAAK,cAAc,EACnB,KAAK,IAAI,EAET,KAAK,SAAS,EACf,MAAM,YAAY,CAAA;AAiBnB,eAAO,MAAM,kBAAkB,IAAI,CAAA;AAEnC,2EAA2E;AAC3E,MAAM,MAAM,SAAS,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAA;AAEzE,wEAAwE;AACxE,MAAM,MAAM,cAAc,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAA;AAE9E,MAAM,MAAM,YAAY,GAAG;IACzB,qEAAqE;IACrE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAChC,iEAAiE;IACjE,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,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