@zinn-dev/cli 0.43.0 → 0.44.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/README.md +1 -1
- package/index.js +3 -3
- package/package.json +7 -4
- package/skills/zinn/SKILL.md +1 -1
- package/src/actor.d.ts +8 -0
- package/src/actor.js +5 -1
- package/src/capabilities-schema.d.ts +1 -1
- package/src/column-hint.js +3 -4
- package/src/errors.d.ts +12 -3
- package/src/errors.js +19 -2
- package/src/failure-log.d.ts +6 -0
- package/src/failure-log.js +167 -0
- package/src/input.d.ts +11 -0
- package/src/input.js +25 -1
- package/src/lib.d.ts +7 -5
- package/src/lib.js +52 -33
- package/src/list-limit.d.ts +2 -2
- package/src/list-limit.js +2 -2
- package/src/overview.d.ts +10 -0
- package/src/overview.js +41 -0
- package/src/presentation/activity-presentation.d.ts +2 -0
- package/src/presentation/activity-presentation.js +96 -35
- package/src/presentation/column-presentation.d.ts +17 -2
- package/src/presentation/column-presentation.js +45 -6
- package/src/presentation/info-presentation.js +36 -6
- package/src/presentation/relation-presentation.d.ts +6 -2
- package/src/presentation/relation-presentation.js +36 -26
- package/src/presentation/task-blame-presentation.d.ts +11 -0
- package/src/presentation/task-blame-presentation.js +56 -0
- package/src/presentation/task-presentation.d.ts +6 -1
- package/src/presentation/task-presentation.js +21 -9
- package/src/result-schemas.d.ts +480 -15
- package/src/result-schemas.js +54 -1
- package/src/routes/activity-router.d.ts +9 -1
- package/src/routes/activity-router.js +15 -2
- package/src/routes/info-router.js +3 -2
- package/src/routes/migrate-router.js +2 -2
- package/src/routes/project-router.d.ts +1 -1
- package/src/routes/project-router.js +3 -12
- package/src/routes/restore-router.js +9 -3
- package/src/routes/router.js +24 -11
- package/src/routes/routes.d.ts +51 -8
- package/src/routes/task-router.d.ts +41 -6
- package/src/routes/task-router.js +81 -24
- package/src/routes/whoami-router.js +1 -1
- package/src/run-facts.d.ts +14 -0
- package/src/run-facts.js +14 -0
- package/src/store-folder.d.ts +5 -0
- package/src/store-folder.js +16 -0
- package/src/telemetry.d.ts +31 -0
- package/src/telemetry.js +329 -0
- package/src/text-input.js +1 -5
- package/src/types.d.ts +1 -1
package/src/telemetry.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { closeSync, openSync, readdirSync, rmSync, statSync, writeSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { isatty } from "node:tty";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import { BYTES_PER_MEGABYTE, TELEMETRY_FOLDER_NAME, getConfiguredStorage, getProcessSettings, isMissingFileError, resolveTelemetry, } from "@zinn-dev/core";
|
|
6
|
+
import packageJson from "../package.json" with { type: "json" };
|
|
7
|
+
import { detectAgent, readExecutionIdentity } from "./actor.js";
|
|
8
|
+
import { isHelpFlag, isRouterFlag, routerOptions, splitAtOptionSeparator } from "./input.js";
|
|
9
|
+
import { currentRuntime, readRunClock } from "./run-facts.js";
|
|
10
|
+
import { ensureFolderInStore } from "./store-folder.js";
|
|
11
|
+
const RECORD_VERSION = 1;
|
|
12
|
+
const RECORD_EXTENSION = ".jsonl";
|
|
13
|
+
const STDIN_DESCRIPTOR = 0;
|
|
14
|
+
const STDOUT_DESCRIPTOR = 1;
|
|
15
|
+
const ISO_DATE_LENGTH = "YYYY-MM-DD".length;
|
|
16
|
+
const RECORD_FILE_NAME = /^(\d{4}-\d{2}-\d{2})-(\d{4})\.jsonl$/;
|
|
17
|
+
const SEQUENCE_DIGITS = 4;
|
|
18
|
+
const FIRST_SEQUENCE = 1;
|
|
19
|
+
/** A file takes records up to this share of the cap, so the cap removes records a slice at a time */
|
|
20
|
+
const FILES_PER_CAP = 10;
|
|
21
|
+
const MILLISECONDS_PER_DAY = 86_400_000;
|
|
22
|
+
/** parseArgs reads any word starting with `--` as an option up to its first `=`, spaces included */
|
|
23
|
+
const OPTION_NAME_SHAPE = /^--?[A-Za-z0-9][\w-]*$/;
|
|
24
|
+
/** A successful run of a command that declares one of these only reads or reports, and leaves no record */
|
|
25
|
+
export const UNRECORDED_STORAGE_EFFECTS = [
|
|
26
|
+
"none",
|
|
27
|
+
"inspect",
|
|
28
|
+
];
|
|
29
|
+
let matchedRun = null;
|
|
30
|
+
let unmatchedRun = null;
|
|
31
|
+
let didParseArgsThrow = false;
|
|
32
|
+
export function noteMatchedRun(run) {
|
|
33
|
+
matchedRun = run;
|
|
34
|
+
unmatchedRun = null;
|
|
35
|
+
didParseArgsThrow = false;
|
|
36
|
+
}
|
|
37
|
+
/** An option-shaped word is left out of `unmatchedWord` because it can carry a value. `options` names it */
|
|
38
|
+
export function noteUnmatchedRun({ namespace, unmatchedWord, help }) {
|
|
39
|
+
matchedRun = null;
|
|
40
|
+
didParseArgsThrow = false;
|
|
41
|
+
unmatchedRun = {
|
|
42
|
+
namespace,
|
|
43
|
+
help,
|
|
44
|
+
...(unmatchedWord === undefined || unmatchedWord.startsWith("-") ? {} : { unmatchedWord }),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function noteParseArgsThrew() {
|
|
48
|
+
didParseArgsThrow = true;
|
|
49
|
+
}
|
|
50
|
+
function readTokens(words, options) {
|
|
51
|
+
return parseArgs({
|
|
52
|
+
args: words,
|
|
53
|
+
options,
|
|
54
|
+
allowPositionals: true,
|
|
55
|
+
strict: false,
|
|
56
|
+
tokens: true,
|
|
57
|
+
}).tokens;
|
|
58
|
+
}
|
|
59
|
+
function readValueForm(token, options) {
|
|
60
|
+
if (token.inlineValue === true) {
|
|
61
|
+
return "joined";
|
|
62
|
+
}
|
|
63
|
+
if (isHelpFlag(token.rawName)) {
|
|
64
|
+
return "none";
|
|
65
|
+
}
|
|
66
|
+
const type = Object.hasOwn(options, token.name) ? options[token.name].type : undefined;
|
|
67
|
+
if (type === "boolean") {
|
|
68
|
+
return "none";
|
|
69
|
+
}
|
|
70
|
+
return type === "string" && token.value !== undefined ? "next" : undefined;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Every option written before `--`, by name, in the order written.
|
|
74
|
+
* A single-dash word such as `-later` counts once, under its first letter,
|
|
75
|
+
* since the letters after it may be a value.
|
|
76
|
+
* A word without an option's shape gets no entry, since it may be free text.
|
|
77
|
+
* The router takes its own flags out first and reads nothing after a help flag,
|
|
78
|
+
* so neither kind of flag is ever a string option's value.
|
|
79
|
+
*/
|
|
80
|
+
function describeOptions(words, known) {
|
|
81
|
+
const { optionWords } = splitAtOptionSeparator(words);
|
|
82
|
+
const options = { ...known, ...routerOptions };
|
|
83
|
+
const describedByWord = new Map();
|
|
84
|
+
const describeGroup = (wordIndexes) => {
|
|
85
|
+
const readWords = new Set();
|
|
86
|
+
const groupWords = wordIndexes.map((index) => optionWords[index]);
|
|
87
|
+
for (const token of readTokens(groupWords, options)) {
|
|
88
|
+
if (token.kind !== "option" || readWords.has(token.index)) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
readWords.add(token.index);
|
|
92
|
+
if (OPTION_NAME_SHAPE.test(token.rawName)) {
|
|
93
|
+
describedByWord.set(wordIndexes[token.index], {
|
|
94
|
+
name: token.rawName,
|
|
95
|
+
valueForm: readValueForm(token, options),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
let group = [];
|
|
101
|
+
for (const [index, word] of optionWords.entries()) {
|
|
102
|
+
if (isRouterFlag(word)) {
|
|
103
|
+
describedByWord.set(index, { name: word, valueForm: "none" });
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (isHelpFlag(word)) {
|
|
107
|
+
describeGroup(group);
|
|
108
|
+
group = [];
|
|
109
|
+
}
|
|
110
|
+
group.push(index);
|
|
111
|
+
}
|
|
112
|
+
describeGroup(group);
|
|
113
|
+
return optionWords.flatMap((_, index) => describedByWord.get(index) ?? []);
|
|
114
|
+
}
|
|
115
|
+
/** The facts the environment gives. Git is never read, so this spawns nothing */
|
|
116
|
+
function describeActor() {
|
|
117
|
+
const agent = detectAgent(process.env);
|
|
118
|
+
const identity = agent ?? readExecutionIdentity(process.env);
|
|
119
|
+
return {
|
|
120
|
+
kind: agent?.kind ?? "human",
|
|
121
|
+
name: agent?.name ?? undefined,
|
|
122
|
+
source: agent?.source,
|
|
123
|
+
sessionId: identity.sessionId ?? undefined,
|
|
124
|
+
threadId: identity.threadId ?? undefined,
|
|
125
|
+
agent: identity.agent ?? undefined,
|
|
126
|
+
model: identity.model ?? undefined,
|
|
127
|
+
effort: identity.effort ?? undefined,
|
|
128
|
+
client: identity.client,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function describeCommand(words) {
|
|
132
|
+
if (matchedRun !== null) {
|
|
133
|
+
const { match, typedRoute, help } = matchedRun;
|
|
134
|
+
return {
|
|
135
|
+
route: match.command,
|
|
136
|
+
alias: typedRoute === match.command ? undefined : typedRoute,
|
|
137
|
+
help,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
// Root help prints without routing, and a failure to load the routes never reaches the router
|
|
141
|
+
return (unmatchedRun ?? {
|
|
142
|
+
namespace: "",
|
|
143
|
+
help: words.some(isHelpFlag),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
/** Left out with no route, for help and when parseArgs refused the words, because records never guess */
|
|
147
|
+
function countPositionals() {
|
|
148
|
+
if (matchedRun === null || matchedRun.help || didParseArgsThrow) {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
const options = matchedRun.match.input.options;
|
|
152
|
+
const tokens = readTokens(matchedRun.commandArgs, options);
|
|
153
|
+
return tokens.filter((token) => token.kind === "positional").length;
|
|
154
|
+
}
|
|
155
|
+
function describeFailure({ failure, logFileName }) {
|
|
156
|
+
if (failure === undefined) {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
const suggestedRoute = failure.suggestedRoute?.join(" ");
|
|
160
|
+
return {
|
|
161
|
+
code: failure.code,
|
|
162
|
+
reason: failure.details?.reason,
|
|
163
|
+
argument: failure.details?.argument,
|
|
164
|
+
suggestedRoute: suggestedRoute === "" ? undefined : suggestedRoute,
|
|
165
|
+
logFileName,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function createRecord(outcome, startedAt, elapsedMs) {
|
|
169
|
+
const words = process.argv.slice(2);
|
|
170
|
+
const known = matchedRun === null ? {} : matchedRun.match.input.options;
|
|
171
|
+
return {
|
|
172
|
+
v: RECORD_VERSION,
|
|
173
|
+
startedAt: startedAt.toISOString(),
|
|
174
|
+
durationMs: elapsedMs,
|
|
175
|
+
exitCode: outcome.failure === undefined ? 0 : 1,
|
|
176
|
+
actor: describeActor(),
|
|
177
|
+
command: describeCommand(words),
|
|
178
|
+
options: describeOptions(words, known),
|
|
179
|
+
positionals: countPositionals(),
|
|
180
|
+
error: describeFailure(outcome),
|
|
181
|
+
version: packageJson.version,
|
|
182
|
+
runtime: currentRuntime(),
|
|
183
|
+
platform: process.platform,
|
|
184
|
+
arch: process.arch,
|
|
185
|
+
stdinIsTerminal: isatty(STDIN_DESCRIPTOR),
|
|
186
|
+
stdoutIsTerminal: isatty(STDOUT_DESCRIPTOR),
|
|
187
|
+
parentPid: process.ppid,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function isUnrecordedRun({ failure }) {
|
|
191
|
+
return (failure === undefined &&
|
|
192
|
+
matchedRun !== null &&
|
|
193
|
+
!matchedRun.help &&
|
|
194
|
+
UNRECORDED_STORAGE_EFFECTS.includes(matchedRun.match.effects.storage));
|
|
195
|
+
}
|
|
196
|
+
/** One write on a descriptor opened for append, so parallel runs cannot split each other's lines */
|
|
197
|
+
function appendLine(path, line) {
|
|
198
|
+
const descriptor = openSync(path, "a");
|
|
199
|
+
try {
|
|
200
|
+
writeSync(descriptor, line);
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
closeSync(descriptor);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/** A file's size in bytes or `undefined` when it does not exist */
|
|
207
|
+
function readFileSize(path) {
|
|
208
|
+
try {
|
|
209
|
+
return statSync(path).size;
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
if (isMissingFileError(error)) {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function firstLineOf(error) {
|
|
219
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
220
|
+
return message.split("\n")[0];
|
|
221
|
+
}
|
|
222
|
+
/** The store's record files, oldest first. Other entries in the folder are left out */
|
|
223
|
+
function listRecordFiles(folder) {
|
|
224
|
+
return readdirSync(folder, { withFileTypes: true })
|
|
225
|
+
.filter((entry) => entry.isFile() && RECORD_FILE_NAME.test(entry.name))
|
|
226
|
+
.map((entry) => entry.name)
|
|
227
|
+
.sort();
|
|
228
|
+
}
|
|
229
|
+
function createRecordFileName(date, sequence) {
|
|
230
|
+
const digits = String(sequence).padStart(SEQUENCE_DIGITS, "0");
|
|
231
|
+
return `${date}-${digits}${RECORD_EXTENSION}`;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Deletes expired record files, then the oldest others until the rest total under the cap.
|
|
235
|
+
* A file another run already removed is not a failure. Any other failure throws and ends the cleanup.
|
|
236
|
+
*/
|
|
237
|
+
function deleteOldRecordFiles(folder, names, startedAt, { capBytes, maxAgeDays }) {
|
|
238
|
+
// A setting too large for a date keeps everything by age
|
|
239
|
+
const oldestKeptMs = Math.max(startedAt.getTime() - maxAgeDays * MILLISECONDS_PER_DAY, 0);
|
|
240
|
+
const oldestKeptDate = new Date(oldestKeptMs).toISOString().slice(0, ISO_DATE_LENGTH);
|
|
241
|
+
const kept = [];
|
|
242
|
+
for (const name of names) {
|
|
243
|
+
const isExpired = name.slice(0, ISO_DATE_LENGTH) < oldestKeptDate;
|
|
244
|
+
if (isExpired) {
|
|
245
|
+
rmSync(join(folder, name), { force: true });
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
kept.push(name);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
const files = kept.map((name) => {
|
|
252
|
+
const path = join(folder, name);
|
|
253
|
+
return { path, size: readFileSize(path) ?? 0 };
|
|
254
|
+
});
|
|
255
|
+
let total = files.reduce((sum, { size }) => sum + size, 0);
|
|
256
|
+
for (const { path, size } of files) {
|
|
257
|
+
if (total < capBytes) {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
rmSync(path, { force: true });
|
|
261
|
+
total -= size;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* The file this run appends to, and what went wrong with the cleanup when it starts a new file.
|
|
266
|
+
* Today's file with the highest sequence takes records until it reaches a tenth of the cap.
|
|
267
|
+
*/
|
|
268
|
+
function chooseRecordFile(folder, startedAt, limits) {
|
|
269
|
+
const date = startedAt.toISOString().slice(0, ISO_DATE_LENGTH);
|
|
270
|
+
let names;
|
|
271
|
+
try {
|
|
272
|
+
names = listRecordFiles(folder);
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
// Without a listing the current file is unknown, so the record goes to the day's first
|
|
276
|
+
return {
|
|
277
|
+
name: createRecordFileName(date, FIRST_SEQUENCE),
|
|
278
|
+
cleanupProblem: `wrote the record but could not list old record files: ${firstLineOf(error)}`,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
const current = names.filter((name) => name.startsWith(`${date}-`)).at(-1);
|
|
282
|
+
const rotationBytes = Math.floor(limits.capBytes / FILES_PER_CAP);
|
|
283
|
+
if (current !== undefined && (readFileSize(join(folder, current)) ?? 0) < rotationBytes) {
|
|
284
|
+
return { name: current };
|
|
285
|
+
}
|
|
286
|
+
const currentSequence = current === undefined ? 0 : Number(current.match(RECORD_FILE_NAME)[2]);
|
|
287
|
+
const name = createRecordFileName(date, currentSequence + 1);
|
|
288
|
+
try {
|
|
289
|
+
deleteOldRecordFiles(folder, names, startedAt, limits);
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
return {
|
|
293
|
+
name,
|
|
294
|
+
cleanupProblem: `wrote the record but could not delete old record files: ${firstLineOf(error)}`,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
return { name };
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Appends this run's record to the current record file in the store's `telemetry` folder.
|
|
301
|
+
* A run that starts a new file first deletes old ones, so the newest records are the ones kept.
|
|
302
|
+
* It never throws, so recording cannot change a run's result.
|
|
303
|
+
* Returns one sentence saying what went wrong, or undefined when nothing did or no record was due.
|
|
304
|
+
*/
|
|
305
|
+
export function writeRunRecord(outcome) {
|
|
306
|
+
try {
|
|
307
|
+
if (isUnrecordedRun(outcome)) {
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
const storage = getConfiguredStorage();
|
|
311
|
+
const { mode, maxMegabytes, maxAgeDays } = resolveTelemetry(getProcessSettings(), process.env);
|
|
312
|
+
if (mode !== "local") {
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
const { startedAt, elapsedMs } = readRunClock();
|
|
316
|
+
const folder = join(storage.directory, TELEMETRY_FOLDER_NAME);
|
|
317
|
+
const line = `${JSON.stringify(createRecord(outcome, startedAt, elapsedMs))}\n`;
|
|
318
|
+
ensureFolderInStore(folder);
|
|
319
|
+
const { name, cleanupProblem } = chooseRecordFile(folder, startedAt, {
|
|
320
|
+
capBytes: maxMegabytes * BYTES_PER_MEGABYTE,
|
|
321
|
+
maxAgeDays,
|
|
322
|
+
});
|
|
323
|
+
appendLine(join(folder, name), line);
|
|
324
|
+
return cleanupProblem;
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
return `could not write a record: ${firstLineOf(error)}`;
|
|
328
|
+
}
|
|
329
|
+
}
|
package/src/text-input.js
CHANGED
|
@@ -155,11 +155,7 @@ export async function resolveTextInput(text, path, textFlag) {
|
|
|
155
155
|
}
|
|
156
156
|
const failure = classifyFailure(error);
|
|
157
157
|
const code = isMissingFileError(error) ? "not_found" : failure.code;
|
|
158
|
-
throw new CliError(`Cannot read ${fileFlag} ${JSON.stringify(path)}.`, code, {
|
|
159
|
-
...failure.details,
|
|
160
|
-
argument: fileFlag,
|
|
161
|
-
path,
|
|
162
|
-
});
|
|
158
|
+
throw new CliError(`Cannot read ${fileFlag} ${JSON.stringify(path)}.`, code, { ...failure.details, argument: fileFlag, path }, undefined, undefined, error);
|
|
163
159
|
}
|
|
164
160
|
let decoded;
|
|
165
161
|
try {
|
package/src/types.d.ts
CHANGED
|
@@ -22,8 +22,8 @@ export declare const commandEffectsSchema: z.ZodObject<{
|
|
|
22
22
|
}, z.core.$strict>;
|
|
23
23
|
export declare const commandConfirmationSchema: z.ZodObject<{
|
|
24
24
|
kind: z.ZodEnum<{
|
|
25
|
-
prompt: "prompt";
|
|
26
25
|
none: "none";
|
|
26
|
+
prompt: "prompt";
|
|
27
27
|
"project-delete": "project-delete";
|
|
28
28
|
}>;
|
|
29
29
|
description: z.ZodOptional<z.ZodString>;
|