@firstdraft.com/cli 0.8.2 → 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.
@@ -0,0 +1,204 @@
1
+ import { isUuidV7 } from "../plan-state.js";
2
+ import { PARENT_SEGMENT, plainWords } from "./report.js";
3
+
4
+ // A note carries only fields of a known shape, as a failure report does: the
5
+ // plain words of free text, and identifiers that fit their patterns. The
6
+ // Service refuses the same shapes.
7
+
8
+ export const NOTE_KINDS = Object.freeze([
9
+ "unstated_request",
10
+ "unclear_guidance",
11
+ "wrong_output",
12
+ ]);
13
+ /**
14
+ * Each kind's fields, in the order a note carries them. A field the kind does
15
+ * not list is no option of its command.
16
+ */
17
+ export const NOTE_FIELDS = Object.freeze({
18
+ unstated_request: Object.freeze(["request", "workaround"]),
19
+ unclear_guidance: Object.freeze(["page", "code", "unclear", "workaround"]),
20
+ wrong_output: Object.freeze([
21
+ "source",
22
+ "code",
23
+ "compilation_id",
24
+ "file",
25
+ "expected",
26
+ "actual",
27
+ "workaround",
28
+ ]),
29
+ });
30
+
31
+ const PLAIN_WORD_FIELDS = new Set([
32
+ "request",
33
+ "unclear",
34
+ "expected",
35
+ "actual",
36
+ "workaround",
37
+ ]);
38
+ const REQUIRED_PLAIN_WORDS = Object.freeze({
39
+ unstated_request: ["request"],
40
+ unclear_guidance: ["unclear"],
41
+ wrong_output: ["expected", "actual"],
42
+ });
43
+ const MAX_PLAIN_WORDS = 300;
44
+ const MAX_PAGE = 200;
45
+ const MAX_CODE = 120;
46
+ const MAX_FILE = 300;
47
+ // SKILL.md, references/plan/policies.md, references/interview.md#scope
48
+ const SKILL_PAGE =
49
+ /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+)*\.md(?:#[A-Za-z0-9_-]+)?$/;
50
+ // foundation_plan.identity.reserved_constant_collision, invalid_output_path
51
+ const CODE = /^[a-z0-9_]+(?:\.[a-z0-9_]+)*$/;
52
+ // app/models/movie.rb, app/models/movie.rb:12
53
+ const APPLICATION_FILE = /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+)*(?::\d{1,7})?$/;
54
+
55
+ export class NoteIncompleteError extends Error {
56
+ /** @param {readonly string[]} missing what noteBody said the note needs */
57
+ constructor(missing) {
58
+ super("A field the note needs did not fit its shape.");
59
+ this.missing = missing;
60
+ }
61
+ }
62
+
63
+ /** @typedef {"unstated_request" | "unclear_guidance" | "wrong_output"} NoteKind */
64
+ /** @typedef {Partial<Record<string, string>>} NoteFields */
65
+
66
+ /**
67
+ * Whether these fields, as given, can make a note of `kind`: each required
68
+ * field is given, `wrong_output` names a source and only the fields that
69
+ * source takes, and a Compilation is a lowercase UUIDv7. A note about
70
+ * generated code may still lack its Compilation, which the command reads
71
+ * from its record.
72
+ *
73
+ * @param {NoteKind} kind
74
+ * @param {NoteFields} fields
75
+ */
76
+ export function noteArgumentsValid(kind, fields) {
77
+ const allowed = /** @type {readonly string[]} */ (NOTE_FIELDS[kind]);
78
+ if (Object.keys(fields).some((field) => !allowed.includes(field))) {
79
+ return false;
80
+ }
81
+ if (REQUIRED_PLAIN_WORDS[kind].some((field) => fields[field] === undefined)) {
82
+ return false;
83
+ }
84
+ if (kind === "unclear_guidance") {
85
+ return fields.page !== undefined || fields.code !== undefined;
86
+ }
87
+ if (kind === "wrong_output") {
88
+ if (fields.source === "analysis") {
89
+ return fields.compilation_id === undefined && fields.file === undefined;
90
+ }
91
+ return (
92
+ fields.source === "generated_code" &&
93
+ fields.code === undefined &&
94
+ (fields.compilation_id === undefined || isUuidV7(fields.compilation_id))
95
+ );
96
+ }
97
+ return true;
98
+ }
99
+
100
+ /**
101
+ * Shapes the note that is printed and sent. Each field keeps only what fits
102
+ * its shape; `omitted` says, for the preview, what was left out, and
103
+ * `missing` says which left-out fields the note needs, so it cannot be sent.
104
+ *
105
+ * @param {NoteKind} kind
106
+ * @param {NoteFields} fields valid for noteArgumentsValid
107
+ */
108
+ export function noteBody(kind, fields) {
109
+ /** @type {Record<string, string>} */
110
+ const note = { kind };
111
+ /** @type {[string, string][]} */
112
+ const leftOut = [];
113
+ for (const field of NOTE_FIELDS[kind]) {
114
+ const value = fields[field];
115
+ if (value === undefined) continue;
116
+
117
+ const shaped = shapeField(field, value);
118
+ if (shaped) {
119
+ note[field] = shaped;
120
+ } else {
121
+ leftOut.push([field, `${field}, which ${SHAPE_REASONS[field]}`]);
122
+ }
123
+ }
124
+
125
+ /** @type {Set<string>} */
126
+ const needed = new Set(REQUIRED_PLAIN_WORDS[kind]);
127
+ // Guidance that was unclear needs a page or a code to say which.
128
+ if (
129
+ kind === "unclear_guidance" &&
130
+ note.page === undefined &&
131
+ note.code === undefined
132
+ ) {
133
+ needed.add("page").add("code");
134
+ }
135
+ return {
136
+ body: { note },
137
+ omitted: leftOut
138
+ .filter(([field]) => !needed.has(field))
139
+ .map(([, reason]) => reason),
140
+ missing: leftOut
141
+ .filter(([field]) => needed.has(field))
142
+ .map(([, reason]) => reason),
143
+ };
144
+ }
145
+
146
+ const NO_PLAIN_WORDS = "has no plain words on its first line";
147
+ /** @type {Readonly<Record<string, string>>} */
148
+ const SHAPE_REASONS = {
149
+ request: NO_PLAIN_WORDS,
150
+ unclear: NO_PLAIN_WORDS,
151
+ expected: NO_PLAIN_WORDS,
152
+ actual: NO_PLAIN_WORDS,
153
+ workaround: NO_PLAIN_WORDS,
154
+ page: "is not SKILL.md or another path ending in .md",
155
+ code: "is not a diagnostic code or CLI error value",
156
+ file: "is not a relative path in the application",
157
+ };
158
+
159
+ /**
160
+ * One field in its shape, or an empty string when nothing of it fits.
161
+ *
162
+ * @param {string} field
163
+ * @param {string} value
164
+ */
165
+ function shapeField(field, value) {
166
+ if (PLAIN_WORD_FIELDS.has(field)) return plainWords(value, MAX_PLAIN_WORDS);
167
+ if (field === "page") return skillPage(value) ?? "";
168
+ if (field === "code") return diagnosticCode(value) ?? "";
169
+ if (field === "file") return applicationFile(value) ?? "";
170
+ // source and compilation_id were checked as arguments.
171
+ return value;
172
+ }
173
+
174
+ /**
175
+ * @param {string} value
176
+ * @returns {string | undefined}
177
+ */
178
+ export function skillPage(value) {
179
+ return value.length <= MAX_PAGE &&
180
+ SKILL_PAGE.test(value) &&
181
+ !PARENT_SEGMENT.test(value.replace(/#.*$/, ""))
182
+ ? value
183
+ : undefined;
184
+ }
185
+
186
+ /**
187
+ * @param {string} value
188
+ * @returns {string | undefined}
189
+ */
190
+ export function diagnosticCode(value) {
191
+ return value.length <= MAX_CODE && CODE.test(value) ? value : undefined;
192
+ }
193
+
194
+ /**
195
+ * @param {string} value
196
+ * @returns {string | undefined}
197
+ */
198
+ export function applicationFile(value) {
199
+ return value.length <= MAX_FILE &&
200
+ APPLICATION_FILE.test(value) &&
201
+ !PARENT_SEGMENT.test(value)
202
+ ? value
203
+ : undefined;
204
+ }
@@ -1,4 +1,3 @@
1
- import { publishPlan } from "./plan-publish.js";
2
1
  import { compileAndDownload } from "./compilation.js";
3
2
  import {
4
3
  prepareCompilationOutputTarget,
@@ -65,63 +64,11 @@ export class PlanCompileAnalysisInvalidError extends Error {
65
64
  * @property {(timeoutMs?: number) => AbortSignal} [createRequestSignal]
66
65
  * @property {(delayMs: number) => Promise<void>} [analysisSleep]
67
66
  * @property {() => number} [analysisNow]
68
- * @property {(delayMs: number) => Promise<void>} [publicationSleep]
69
- * @property {() => number} [publicationNow]
70
67
  * @property {(progress: import("../plan-compile-progress.js").PlanCompileProgress) => void} [onProgress]
71
68
  * @property {typeof pushPlan} [push]
72
69
  * @property {typeof readPlanStatus} [readStatus]
73
- * @property {typeof publishPlan} [publish]
74
70
  */
75
71
 
76
- /**
77
- * Submit and analyze the exact current local Plan before invoking the internal
78
- * GitHub Publication lifecycle. The Publication function performs the final
79
- * local-byte check immediately before its conditional mutation.
80
- *
81
- * @param {CompilePlanOptions} options
82
- */
83
- export async function compilePlanToGitHub({
84
- cwd,
85
- apiUrl,
86
- fetchFunction,
87
- fileSystem,
88
- createTemporaryId,
89
- createRequestSignal,
90
- analysisSleep,
91
- analysisNow,
92
- publicationSleep,
93
- publicationNow,
94
- onProgress = () => {},
95
- push = pushPlan,
96
- readStatus = readPlanStatus,
97
- publish = publishPlan,
98
- }) {
99
- const prepared = await preparePlan({
100
- cwd,
101
- apiUrl,
102
- fetchFunction,
103
- fileSystem,
104
- createTemporaryId,
105
- createRequestSignal,
106
- analysisSleep,
107
- analysisNow,
108
- onProgress,
109
- push,
110
- readStatus,
111
- });
112
-
113
- return publish({
114
- cwd,
115
- fetchFunction,
116
- fileSystem,
117
- createRequestSignal,
118
- sleep: publicationSleep,
119
- now: publicationNow,
120
- expectedEtag: prepared.pushed.etag,
121
- onProgress,
122
- });
123
- }
124
-
125
72
  /**
126
73
  * @typedef {CompilePlanOptions & {
127
74
  * output: string,
@@ -134,8 +81,7 @@ export async function compilePlanToGitHub({
134
81
  /**
135
82
  * Submit and analyze the exact current local Plan, then start one direct
136
83
  * Compilation and materialize its verified artifact into an absent directory
137
- * or the eligible current directory. GitHub Publication is the explicit
138
- * --github mode owned by compilePlanToGitHub.
84
+ * or the eligible current directory.
139
85
  *
140
86
  * @param {CompilePlanToDirectoryOptions} options
141
87
  */
@@ -199,7 +145,7 @@ export async function compilePlanToDirectory({
199
145
  }
200
146
 
201
147
  /**
202
- * @param {Omit<CompilePlanOptions, "publicationSleep" | "publicationNow" | "publish">} options
148
+ * @param {CompilePlanOptions} options
203
149
  */
204
150
  async function preparePlan({
205
151
  cwd,