harness-craft 0.1.1 → 0.1.2

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.
@@ -2,13 +2,22 @@ import { readdir } from 'node:fs/promises';
2
2
  import { join } from 'node:path';
3
3
  import { describeError } from '../diagnostics.js';
4
4
  import { compareStrings, isMissing, isRecord } from '../fsutil.js';
5
- import { dateRange, forEachLine, incr, listFilesRecursive, runPool } from './lines.js';
5
+ import { dateRange, forEachLine, incr, listFilesRecursive, modelKey, runPool } from './lines.js';
6
6
  const TOOL_USE_MARK = Buffer.from('"tool_use"');
7
7
  const COMMAND_MARK = Buffer.from('<command-name>');
8
8
  const TIMESTAMP_MARK = Buffer.from('"timestamp":"');
9
9
  const DATE_LENGTH = 10;
10
10
  const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
11
11
  const COMMAND_NAME_PATTERN = /<command-name>\/?([^<\s]+)<\/command-name>/g;
12
+ /** Assistant records open their message with the model; on 102k real lines `"message":{` sat within the first 206 bytes. */
13
+ const MESSAGE_HEAD_BYTES = 1024;
14
+ const MESSAGE_MARK = Buffer.from('"message":{');
15
+ const MODEL_MARK = Buffer.from('"model":"');
16
+ const ID_MARK = Buffer.from('"id":"');
17
+ const CONTENT_MARK = Buffer.from('"content":');
18
+ const QUOTE = 0x22;
19
+ /** Placeholder model on locally generated messages (errors, interruptions); not a model the user ran. */
20
+ const SYNTHETIC_MODEL = '<synthetic>';
12
21
  const FILE_CONCURRENCY = 8;
13
22
  export const PROGRESS_EVERY_FILES = 100;
14
23
  /** `mcp__<server>__<tool>` -> server (assumes no `__` inside the server name). */
@@ -56,6 +65,49 @@ function countRecord(acc, record) {
56
65
  incr(acc.slashCalls, m[1]);
57
66
  }
58
67
  }
68
+ /** One count per API message: a message is split over several lines (one per content block) sharing message.id. */
69
+ function countModel(acc, model, id) {
70
+ if (model === SYNTHETIC_MODEL)
71
+ return;
72
+ if (id !== null) {
73
+ if (acc.messageIds.has(id))
74
+ return;
75
+ acc.messageIds.add(id);
76
+ }
77
+ incr(acc.models, modelKey(model));
78
+ }
79
+ function quotedAfter(line, at, mark) {
80
+ const start = at + mark.length;
81
+ const end = line.indexOf(QUOTE, start);
82
+ return end === -1 ? null : line.toString('utf8', start, end);
83
+ }
84
+ /**
85
+ * Reads message.model / message.id without JSON.parse or a full-line search. Only a message object that opens with
86
+ * `"model":"` within the first MESSAGE_HEAD_BYTES counts (that is how assistant records are written; user and system
87
+ * messages open with "role"), and the id must come before `"content":`, so a `"model"` key inside tool input is never
88
+ * used. Other lines get a model only when they are parsed anyway (tool_use / slash-command lines).
89
+ */
90
+ function fastModel(line) {
91
+ const msg = line.subarray(0, MESSAGE_HEAD_BYTES).indexOf(MESSAGE_MARK);
92
+ if (msg === -1)
93
+ return null;
94
+ const model = msg + MESSAGE_MARK.length;
95
+ if (line.compare(MODEL_MARK, 0, MODEL_MARK.length, model, model + MODEL_MARK.length) !== 0)
96
+ return null;
97
+ const content = line.indexOf(CONTENT_MARK, msg);
98
+ const id = line.indexOf(ID_MARK, msg);
99
+ if (content === -1 || (id !== -1 && id > content))
100
+ return null;
101
+ const name = quotedAfter(line, model, MODEL_MARK);
102
+ return name === null ? null : { model: name, id: id === -1 ? null : quotedAfter(line, id, ID_MARK) };
103
+ }
104
+ function countParsedModel(acc, record) {
105
+ if (!isRecord(record) || record.type !== 'assistant' || !isRecord(record.message))
106
+ return;
107
+ const { model, id } = record.message;
108
+ if (typeof model === 'string')
109
+ countModel(acc, model, typeof id === 'string' ? id : null);
110
+ }
59
111
  function scanLine(acc, line) {
60
112
  const ts = line.indexOf(TIMESTAMP_MARK);
61
113
  if (ts !== -1) {
@@ -64,10 +116,16 @@ function scanLine(acc, line) {
64
116
  if (DATE_PATTERN.test(day))
65
117
  acc.days.add(day);
66
118
  }
119
+ const fast = fastModel(line);
120
+ if (fast)
121
+ countModel(acc, fast.model, fast.id);
67
122
  if (line.indexOf(TOOL_USE_MARK) === -1 && line.indexOf(COMMAND_MARK) === -1)
68
123
  return;
69
124
  try {
70
- countRecord(acc, JSON.parse(line.toString('utf8')));
125
+ const record = JSON.parse(line.toString('utf8'));
126
+ if (fast === null)
127
+ countParsedModel(acc, record);
128
+ countRecord(acc, record);
71
129
  }
72
130
  catch {
73
131
  acc.badLines += 1;
@@ -93,7 +151,7 @@ export async function scanClaudeTranscripts(claudeDir, diag, onProgress) {
93
151
  const files = await listFilesRecursive(projectsDir, '.jsonl');
94
152
  const acc = {
95
153
  toolIds: new Set(), days: new Set(), builtin: new Map(), mcpCalls: new Map(),
96
- skillCalls: new Map(), slashCalls: new Map(), agentCalls: new Map(), badLines: 0,
154
+ skillCalls: new Map(), slashCalls: new Map(), agentCalls: new Map(), messageIds: new Set(), models: new Map(), badLines: 0,
97
155
  };
98
156
  let done = 0;
99
157
  await runPool(files, FILE_CONCURRENCY, async (rel) => {
@@ -117,6 +175,7 @@ export async function scanClaudeTranscripts(claudeDir, diag, onProgress) {
117
175
  projects: await countProjectDirs(projectsDir),
118
176
  toolCalls: acc.toolIds.size,
119
177
  builtinTools: sortedRecord(acc.builtin),
178
+ models: sortedRecord(acc.models),
120
179
  ...dateRange(acc.days),
121
180
  },
122
181
  mcpCalls: acc.mcpCalls,
@@ -1,7 +1,7 @@
1
1
  import { isAbsolute, join, resolve } from 'node:path';
2
2
  import { describeError } from '../diagnostics.js';
3
- import { isRecord } from '../fsutil.js';
4
- import { dateRange, forEachLine, incr, listFilesRecursive, runPool } from './lines.js';
3
+ import { compareStrings, isRecord } from '../fsutil.js';
4
+ import { dateRange, forEachLine, incr, listFilesRecursive, modelKey, runPool } from './lines.js';
5
5
  const ITEM_COMPLETED_MARK = Buffer.from('"item_completed"');
6
6
  const SESSION_META_MARK = Buffer.from('"session_meta"');
7
7
  const TURN_CONTEXT_MARK = Buffer.from('"turn_context"');
@@ -157,6 +157,14 @@ function scanLine(acc, file, line) {
157
157
  acc.badLines += 1;
158
158
  }
159
159
  }
160
+ /** Turns per model id (validated), from interactive turns, or every turn when there are no interactive ones. */
161
+ function modelCounts(acc) {
162
+ const turns = acc.interactiveModels.turns.size > 0 ? acc.interactiveModels.turns : acc.allModels.turns;
163
+ const counts = new Map();
164
+ for (const [model, efforts] of turns)
165
+ incr(counts, modelKey(model), [...efforts.values()].reduce((a, b) => a + b, 0));
166
+ return Object.fromEntries([...counts].sort((a, b) => compareStrings(a[0], b[0])));
167
+ }
160
168
  function pathDay(rel) {
161
169
  const m = PATH_DATE_PATTERN.exec(rel);
162
170
  return m ? `${m[1]}-${m[2]}-${m[3]}` : null;
@@ -171,6 +179,7 @@ export async function scanCodexSessions(codexHome, diag, onProgress) {
171
179
  mcpTotal: 0, subagent: 0, subagentFiles: 0, execFiles: 0, commands: 0, badLines: 0,
172
180
  };
173
181
  const days = new Set(files.map(pathDay).filter((d) => d !== null));
182
+ const interactiveDays = new Set();
174
183
  let done = 0;
175
184
  await runPool(files, FILE_CONCURRENCY, async (rel) => {
176
185
  // A file without session_meta counts as interactive, as for `sessions`.
@@ -181,6 +190,8 @@ export async function scanCodexSessions(codexHome, diag, onProgress) {
181
190
  catch (err) {
182
191
  diag.warn(join(sessionsDir, rel), `읽기 실패 (${describeError(err)})`);
183
192
  }
193
+ if (file.kind === 'interactive' && file.day)
194
+ interactiveDays.add(file.day);
184
195
  done += 1;
185
196
  if (onProgress && (done % PROGRESS_EVERY_FILES === 0 || done === files.length))
186
197
  onProgress(done, files.length);
@@ -195,6 +206,9 @@ export async function scanCodexSessions(codexHome, diag, onProgress) {
195
206
  mcpCalls: acc.mcpTotal,
196
207
  subagentActivities: acc.subagent,
197
208
  commandExecutions: acc.commands,
209
+ models: modelCounts(acc),
210
+ // Days with a session a person opened; the sheet uses it to pick the main tool (exec runs are often automation).
211
+ interactiveDays: interactiveDays.size,
198
212
  ...dateRange(days),
199
213
  },
200
214
  mcpCalls: acc.mcpCalls,
@@ -70,6 +70,12 @@ export function dateRange(days) {
70
70
  activeDays: sorted.length,
71
71
  };
72
72
  }
73
+ /** Public model ids only; anything else (a custom deployment name, a path) is counted under OTHER_MODEL. */
74
+ const MODEL_ID_PATTERN = /^[a-z0-9][a-z0-9.\-_]{0,63}$/i;
75
+ export const OTHER_MODEL = 'other';
76
+ export function modelKey(model) {
77
+ return MODEL_ID_PATTERN.test(model) ? model : OTHER_MODEL;
78
+ }
73
79
  export function incr(map, key, by = 1) {
74
80
  map.set(key, (map.get(key) ?? 0) + by);
75
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "harness-craft",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Turn your Claude Code and Codex harness (MCP, skills, hooks, subagents) into a pixel-RPG character sheet HTML. No network.",
5
5
  "keywords": [
6
6
  "claude-code",