@zinn-dev/cli 0.43.0 → 0.45.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 +7 -9
- package/src/actor.d.ts +8 -0
- package/src/actor.js +5 -1
- package/src/capabilities-schema.d.ts +6 -2
- package/src/column-hint.js +2 -5
- package/src/database.d.ts +1 -2
- package/src/database.js +1 -6
- package/src/duration-input.d.ts +7 -0
- package/src/duration-input.js +31 -0
- package/src/errors.d.ts +34 -3
- package/src/errors.js +49 -7
- package/src/expected-values.d.ts +48 -0
- package/src/expected-values.js +60 -0
- package/src/failure-log.d.ts +6 -0
- package/src/failure-log.js +167 -0
- package/src/follow.d.ts +9 -0
- package/src/follow.js +58 -0
- package/src/input.d.ts +10 -7
- package/src/input.js +57 -13
- package/src/lib.d.ts +12 -7
- package/src/lib.js +70 -41
- package/src/list-continuation.js +1 -4
- 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 +4 -0
- package/src/presentation/activity-presentation.js +154 -38
- package/src/presentation/column-presentation.d.ts +22 -2
- package/src/presentation/column-presentation.js +68 -12
- package/src/presentation/doctor-presentation.d.ts +4 -0
- package/src/presentation/doctor-presentation.js +24 -0
- package/src/presentation/info-presentation.js +36 -6
- package/src/presentation/relation-presentation.d.ts +6 -2
- package/src/presentation/relation-presentation.js +37 -26
- package/src/presentation/task-blame-presentation.d.ts +8 -0
- package/src/presentation/task-blame-presentation.js +56 -0
- package/src/presentation/task-presentation.d.ts +8 -1
- package/src/presentation/task-presentation.js +59 -10
- package/src/result-schemas.d.ts +780 -20
- package/src/result-schemas.js +99 -2
- package/src/results.d.ts +6 -2
- package/src/results.js +9 -3
- package/src/routes/activity-router.d.ts +21 -4
- package/src/routes/activity-router.js +107 -60
- package/src/routes/aliases.js +1 -0
- package/src/routes/comment-router.d.ts +24 -6
- package/src/routes/comment-router.js +15 -5
- package/src/routes/doctor-router.d.ts +39 -0
- package/src/routes/doctor-router.js +103 -0
- package/src/routes/info-router.js +3 -2
- package/src/routes/label-router.d.ts +39 -6
- package/src/routes/label-router.js +22 -5
- package/src/routes/migrate-router.js +2 -2
- package/src/routes/project-router.d.ts +61 -12
- package/src/routes/project-router.js +103 -45
- package/src/routes/restore-router.js +11 -5
- package/src/routes/router.js +25 -16
- package/src/routes/routes.d.ts +455 -41
- package/src/routes/routes.js +4 -0
- package/src/routes/skill-router.d.ts +25 -0
- package/src/routes/skill-router.js +96 -0
- package/src/routes/task-router.d.ts +272 -19
- package/src/routes/task-router.js +325 -40
- 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-edit.d.ts +2 -2
- package/src/text-input.d.ts +19 -1
- package/src/text-input.js +27 -10
- package/src/types.d.ts +1 -1
|
@@ -23,7 +23,7 @@ export const whoamiCommand = {
|
|
|
23
23
|
help: `${generateCommandUsage("whoami", noInput)}
|
|
24
24
|
|
|
25
25
|
Show the actor a change would record in the current environment and folder.
|
|
26
|
-
Includes name, email, kind, source, session, thread, agent, model and
|
|
26
|
+
Includes name, email, kind, source, session, thread, agent, model, effort and client.
|
|
27
27
|
Git identity is read from the current folder.
|
|
28
28
|
|
|
29
29
|
Works without reading settings or opening storage.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The runtime's name and version, reported the same way by `zinn info`, failure logs and telemetry */
|
|
2
|
+
export declare function currentRuntime(): {
|
|
3
|
+
name: "Node";
|
|
4
|
+
version: string;
|
|
5
|
+
} | {
|
|
6
|
+
name: "Bun";
|
|
7
|
+
version: string;
|
|
8
|
+
};
|
|
9
|
+
/** When this process started and how long it has run, in milliseconds */
|
|
10
|
+
export declare function readRunClock(): {
|
|
11
|
+
now: Date;
|
|
12
|
+
elapsedMs: number;
|
|
13
|
+
startedAt: Date;
|
|
14
|
+
};
|
package/src/run-facts.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const MS_PER_SECOND = 1000;
|
|
2
|
+
/** The runtime's name and version, reported the same way by `zinn info`, failure logs and telemetry */
|
|
3
|
+
export function currentRuntime() {
|
|
4
|
+
const { bun, node } = process.versions;
|
|
5
|
+
return bun === undefined
|
|
6
|
+
? { name: "Node", version: node }
|
|
7
|
+
: { name: "Bun", version: bun };
|
|
8
|
+
}
|
|
9
|
+
/** When this process started and how long it has run, in milliseconds */
|
|
10
|
+
export function readRunClock() {
|
|
11
|
+
const elapsedMs = Math.round(process.uptime() * MS_PER_SECOND);
|
|
12
|
+
const now = new Date();
|
|
13
|
+
return { now, elapsedMs, startedAt: new Date(now.getTime() - elapsedMs) };
|
|
14
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { mkdirSync } from "node:fs";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a folder if its store exists.
|
|
4
|
+
* ! It never creates the store, which **read** commands avoid
|
|
5
|
+
*/
|
|
6
|
+
export function ensureFolderInStore(folder) {
|
|
7
|
+
try {
|
|
8
|
+
mkdirSync(folder);
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
const alreadyExists = error.code === "EEXIST";
|
|
12
|
+
if (!alreadyExists) {
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ClassifiedFailure } from "./errors.js";
|
|
2
|
+
import type { ParsedRoute } from "./types.js";
|
|
3
|
+
type RunOutcome = {
|
|
4
|
+
failure?: ClassifiedFailure;
|
|
5
|
+
logFileName?: string;
|
|
6
|
+
};
|
|
7
|
+
type UnmatchedRun = {
|
|
8
|
+
namespace: string;
|
|
9
|
+
unmatchedWord?: string;
|
|
10
|
+
help: boolean;
|
|
11
|
+
};
|
|
12
|
+
type MatchedRun = {
|
|
13
|
+
match: ParsedRoute;
|
|
14
|
+
typedRoute: string;
|
|
15
|
+
commandArgs: Array<string>;
|
|
16
|
+
help: boolean;
|
|
17
|
+
};
|
|
18
|
+
/** A successful run of a command that declares one of these only reads or reports, and leaves no record */
|
|
19
|
+
export declare const UNRECORDED_STORAGE_EFFECTS: ReadonlyArray<ParsedRoute["effects"]["storage"]>;
|
|
20
|
+
export declare function noteMatchedRun(run: MatchedRun): void;
|
|
21
|
+
/** An option-shaped word is left out of `unmatchedWord` because it can carry a value. `options` names it */
|
|
22
|
+
export declare function noteUnmatchedRun({ namespace, unmatchedWord, help }: UnmatchedRun): void;
|
|
23
|
+
export declare function noteParseArgsThrew(): void;
|
|
24
|
+
/**
|
|
25
|
+
* Appends this run's record to the current record file in the store's `telemetry` folder.
|
|
26
|
+
* A run that starts a new file first deletes old ones, so the newest records are the ones kept.
|
|
27
|
+
* It never throws, so recording cannot change a run's result.
|
|
28
|
+
* Returns one sentence saying what went wrong, or undefined when nothing did or no record was due.
|
|
29
|
+
*/
|
|
30
|
+
export declare function writeRunRecord(outcome: RunOutcome): string | undefined;
|
|
31
|
+
export {};
|
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-edit.d.ts
CHANGED
|
@@ -22,11 +22,11 @@ export declare function textEditConstraints(textFlag: TextFlag): readonly [{
|
|
|
22
22
|
}, {
|
|
23
23
|
readonly kind: "requiresAny";
|
|
24
24
|
readonly input: "option:append";
|
|
25
|
-
readonly members: readonly ["option:description" | "option:text", "option:description-file" | "option:text-file"];
|
|
25
|
+
readonly members: readonly ["option:description" | "option:text" | "option:expect-description" | "option:expect-text", "option:description-file" | "option:text-file" | "option:expect-description-file" | "option:expect-text-file"];
|
|
26
26
|
}, {
|
|
27
27
|
readonly kind: "requiresAny";
|
|
28
28
|
readonly input: "option:replacing";
|
|
29
|
-
readonly members: readonly ["option:description" | "option:text", "option:description-file" | "option:text-file"];
|
|
29
|
+
readonly members: readonly ["option:description" | "option:text" | "option:expect-description" | "option:expect-text", "option:description-file" | "option:text-file" | "option:expect-description-file" | "option:expect-text-file"];
|
|
30
30
|
}, {
|
|
31
31
|
readonly kind: "requires";
|
|
32
32
|
readonly input: "option:all";
|
package/src/text-input.d.ts
CHANGED
|
@@ -9,6 +9,14 @@ declare const textOptions: {
|
|
|
9
9
|
readonly text: "text";
|
|
10
10
|
readonly file: "text-file";
|
|
11
11
|
};
|
|
12
|
+
readonly "--expect-description": {
|
|
13
|
+
readonly text: "expect-description";
|
|
14
|
+
readonly file: "expect-description-file";
|
|
15
|
+
};
|
|
16
|
+
readonly "--expect-text": {
|
|
17
|
+
readonly text: "expect-text";
|
|
18
|
+
readonly file: "expect-text-file";
|
|
19
|
+
};
|
|
12
20
|
};
|
|
13
21
|
export type TextFlag = keyof typeof textOptions;
|
|
14
22
|
export type TextValues = {
|
|
@@ -20,6 +28,12 @@ export declare function textOptionNames(textFlag: TextFlag): {
|
|
|
20
28
|
} | {
|
|
21
29
|
readonly text: "text";
|
|
22
30
|
readonly file: "text-file";
|
|
31
|
+
} | {
|
|
32
|
+
readonly text: "expect-description";
|
|
33
|
+
readonly file: "expect-description-file";
|
|
34
|
+
} | {
|
|
35
|
+
readonly text: "expect-text";
|
|
36
|
+
readonly file: "expect-text-file";
|
|
23
37
|
};
|
|
24
38
|
/** The flag that reads a text flag's value from a file, or from stdin as "-" */
|
|
25
39
|
export declare const textFileOption: {
|
|
@@ -33,7 +47,7 @@ export declare const textFileOption: {
|
|
|
33
47
|
/** Inline text and its file flag are alternatives for the same field */
|
|
34
48
|
export declare function textSourceConstraint(textFlag: TextFlag): {
|
|
35
49
|
readonly kind: "exclusive";
|
|
36
|
-
readonly members: readonly ["option:description" | "option:text", "option:description-file" | "option:text-file"];
|
|
50
|
+
readonly members: readonly ["option:description" | "option:text" | "option:expect-description" | "option:expect-text", "option:description-file" | "option:text-file" | "option:expect-description-file" | "option:expect-text-file"];
|
|
37
51
|
};
|
|
38
52
|
/**
|
|
39
53
|
* Validates `fields` against their rules in a core input schema, so a command can reject
|
|
@@ -42,6 +56,10 @@ export declare function textSourceConstraint(textFlag: TextFlag): {
|
|
|
42
56
|
*/
|
|
43
57
|
export declare function checkFieldsBeforeText(schema: z.ZodObject, fields: Record<string, unknown>): void;
|
|
44
58
|
export declare function textFileHelp(textFlag: TextFlag): string;
|
|
59
|
+
/** Refuses inline text together with its file flag before anything is read */
|
|
60
|
+
export declare function checkSingleTextSource(text: string | undefined, path: string | undefined, textFlag: TextFlag): void;
|
|
61
|
+
/** Refuses more than one file flag reading stdin, since stdin can be read only once.*/
|
|
62
|
+
export declare function checkSingleStdinReader(values: TextValues, textFlags: ReadonlyArray<TextFlag>): void;
|
|
45
63
|
/** Resolves one inline or file source before the command opens the database. */
|
|
46
64
|
export declare function resolveTextInput(text: string | undefined, path: string | undefined, textFlag: TextFlag): Promise<string | undefined>;
|
|
47
65
|
export {};
|
package/src/text-input.js
CHANGED
|
@@ -7,6 +7,13 @@ import { CliError, classifyFailure } from "./errors.js";
|
|
|
7
7
|
const textOptions = {
|
|
8
8
|
"--description": { text: "description", file: "description-file" },
|
|
9
9
|
"--text": { text: "text", file: "text-file" },
|
|
10
|
+
"--expect-description": { text: "expect-description", file: "expect-description-file" },
|
|
11
|
+
"--expect-text": { text: "expect-text", file: "expect-text-file" },
|
|
12
|
+
};
|
|
13
|
+
/** How to give an empty value for a text flag, shown when its file input is blank */
|
|
14
|
+
const blankInputHints = {
|
|
15
|
+
"--description": ' To clear a description, use --description "".',
|
|
16
|
+
"--expect-description": ' To expect no description, use --expect-description "".',
|
|
10
17
|
};
|
|
11
18
|
export function textOptionNames(textFlag) {
|
|
12
19
|
return textOptions[textFlag];
|
|
@@ -133,15 +140,30 @@ async function readStdin(fileFlag, refuse) {
|
|
|
133
140
|
readAvailable();
|
|
134
141
|
});
|
|
135
142
|
}
|
|
143
|
+
/** Refuses inline text together with its file flag before anything is read */
|
|
144
|
+
export function checkSingleTextSource(text, path, textFlag) {
|
|
145
|
+
const fileFlag = `--${textOptionNames(textFlag).file}`;
|
|
146
|
+
if (text !== undefined && path !== undefined) {
|
|
147
|
+
throw new CliError(`Options "${textFlag}" and "${fileFlag}" cannot be used together.`, "invalid_input", { argument: fileFlag, path });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** Refuses more than one file flag reading stdin, since stdin can be read only once.*/
|
|
151
|
+
export function checkSingleStdinReader(values, textFlags) {
|
|
152
|
+
const stdinFlags = textFlags
|
|
153
|
+
.map((textFlag) => textOptionNames(textFlag).file)
|
|
154
|
+
.filter((file) => values[file] === "-")
|
|
155
|
+
.map((file) => `--${file}`);
|
|
156
|
+
if (stdinFlags.length > 1) {
|
|
157
|
+
throw new CliError(`Only one option can read stdin, but ${stdinFlags.join(" and ")} are both "-".`, "invalid_input", { argument: stdinFlags[1], path: "-" });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
136
160
|
/** Resolves one inline or file source before the command opens the database. */
|
|
137
161
|
export async function resolveTextInput(text, path, textFlag) {
|
|
138
162
|
const fileFlag = `--${textOptionNames(textFlag).file}`;
|
|
139
163
|
const refuse = (message) => {
|
|
140
164
|
throw new CliError(message, "invalid_input", { argument: fileFlag, path });
|
|
141
165
|
};
|
|
142
|
-
|
|
143
|
-
refuse(`Options "${textFlag}" and "${fileFlag}" cannot be used together.`);
|
|
144
|
-
}
|
|
166
|
+
checkSingleTextSource(text, path, textFlag);
|
|
145
167
|
if (path === undefined) {
|
|
146
168
|
return text;
|
|
147
169
|
}
|
|
@@ -155,11 +177,7 @@ export async function resolveTextInput(text, path, textFlag) {
|
|
|
155
177
|
}
|
|
156
178
|
const failure = classifyFailure(error);
|
|
157
179
|
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
|
-
});
|
|
180
|
+
throw new CliError(`Cannot read ${fileFlag} ${JSON.stringify(path)}.`, code, { ...failure.details, argument: fileFlag, path }, undefined, undefined, error);
|
|
163
181
|
}
|
|
164
182
|
let decoded;
|
|
165
183
|
try {
|
|
@@ -180,8 +198,7 @@ export async function resolveTextInput(text, path, textFlag) {
|
|
|
180
198
|
}
|
|
181
199
|
const result = decoded.slice(0, end);
|
|
182
200
|
if (result.trim().length === 0) {
|
|
183
|
-
|
|
184
|
-
refuse(`Input is empty or blank.${clearHint}`);
|
|
201
|
+
refuse(`Input is empty or blank.${blankInputHints[textFlag] ?? ""}`);
|
|
185
202
|
}
|
|
186
203
|
return result;
|
|
187
204
|
}
|
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>;
|