fito-tui 0.2.1 → 0.3.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 (40) hide show
  1. package/README.md +2 -86
  2. package/package.json +4 -4
  3. package/prompts/fito-evaluate.md +10 -5
  4. package/prompts/fito-push-knowledge.md +61 -65
  5. package/prompts/fito-submit.md +7 -2
  6. package/shared/competences/competence-list.js +1 -1
  7. package/shared/competences/competence-list.ts +1 -1
  8. package/shared/fito-api/README.md +1 -1
  9. package/shared/fito-api/client.js +7 -11
  10. package/shared/fito-api/client.ts +8 -12
  11. package/shared/fito-url/fito-url.js +29 -0
  12. package/shared/fito-url/fito-url.ts +32 -0
  13. package/shared/knowledge-components/knowledge-fetch.js +1 -1
  14. package/shared/knowledge-components/knowledge-fetch.ts +1 -1
  15. package/shared/knowledge-components/knowledge-search.js +1 -1
  16. package/shared/knowledge-components/knowledge-search.ts +1 -1
  17. package/shared/knowledge-components/technology-fetch.js +1 -1
  18. package/shared/knowledge-components/technology-fetch.ts +1 -1
  19. package/{skills/fito-case-author → shared/semver}/semver.js +8 -0
  20. package/{skills/fito-case-author → shared/semver}/semver.ts +9 -0
  21. package/skills/fito-auth/SKILL.md +3 -10
  22. package/skills/fito-auth/login.js +3 -2
  23. package/skills/fito-auth/login.ts +3 -2
  24. package/skills/fito-case-author/SKILL.md +1 -3
  25. package/skills/fito-case-author/publish-version.js +4 -4
  26. package/skills/fito-case-author/publish-version.ts +6 -6
  27. package/skills/fito-case-author/publish.js +4 -4
  28. package/skills/fito-case-author/publish.ts +6 -6
  29. package/skills/fito-case-runner/SKILL.md +10 -6
  30. package/skills/fito-case-runner/case-component-names.ts +1 -1
  31. package/skills/fito-case-runner/fito-url.js +8 -0
  32. package/skills/fito-case-runner/fito-url.ts +9 -0
  33. package/skills/fito-case-runner/open.ts +1 -1
  34. package/skills/fito-competence-write/SKILL.md +0 -6
  35. package/skills/fito-knowledge-push/SKILL.md +55 -76
  36. package/skills/fito-knowledge-push/push.js +169 -77
  37. package/skills/fito-knowledge-push/push.ts +210 -84
  38. package/skills/fito-source-ingestion/SKILL.md +0 -6
  39. package/skills/fito-source-ingestion/authoring-check.ts +1 -1
  40. package/skills/fito-technology-write/SKILL.md +0 -6
@@ -5,12 +5,14 @@
5
5
  * (Task 8.7): similar existing components — even below the hard 409 bar — stop the push with
6
6
  * their similarities and the choices printed, before anything is stored; `--proceed` is the
7
7
  * author's decision to store anyway and skips the probe. Then it creates-and-publishes; on a
8
- * near-duplicate 409 it stops and prints the duplicate + the resolution commands for the
9
- * `/fito-push-knowledge` orchestrator to relay — it never resolves a 409 or a probe stop itself.
8
+ * near-duplicate 409 it stops and prints the duplicate and the choices — it never resolves a 409 or
9
+ * a probe stop itself. Each stop prints a message in the author's words above [AGENT_MARKER], which
10
+ * the `/fito-push-knowledge` orchestrator shows word for word, and one command per choice below it.
10
11
  * The author's explicit choice comes back as a re-run with exactly one flag: `--link`
11
- * (alternative definition of the duplicate), `--supersede` (the duplicate's new version;
12
- * `--version` bumps the label), `--override` (store an independent near-duplicate, echoing the
13
- * exact reference the 409 reported), or `--proceed`. Skipping is simply not re-running.
12
+ * (alternative definition of the duplicate), `--supersede` (the duplicate's new revision; requires
13
+ * a `--version` label newer than the duplicate's), `--override` (store an independent
14
+ * near-duplicate, echoing the exact reference the 409 reported), or `--proceed`. Skipping is
15
+ * simply not re-running.
14
16
  *
15
17
  * `runPush` is the testable core (injectable client/home); `main` is the thin CLI wrapper.
16
18
  */
@@ -22,6 +24,7 @@ import { FitoApiError, createFitoApiClient, } from "../../shared/fito-api/client
22
24
  import { resolveFitoSession } from "../../shared/fito-session/session.js";
23
25
  import { generatorPayloadSchema, resolveRegisteredTemplate, toCreateInput, } from "../../shared/knowledge-components/push-payload.js";
24
26
  import { parseKnowledgeReference } from "../../shared/knowledge-components/reference.js";
27
+ import { isSemVer, isStrictlyGreaterSemVer } from "../../shared/semver/semver.js";
25
28
  /**
26
29
  * Push one payload: read + validate it, resolve the Fito session and the registered template — by
27
30
  * component type *and* the subject technology's kind, which is what selects the right variant when
@@ -60,6 +63,11 @@ export async function runPush(options) {
60
63
  }
61
64
  case "supersede": {
62
65
  const target = resolveTarget(resolution.target, payload);
66
+ const current = await client.getKnowledgeComponent(target.type, target.slug);
67
+ const problem = revisionLabelProblem(options.version, current.version);
68
+ if (problem) {
69
+ return { kind: "revision-label-refused", problem, label: options.version, target: current };
70
+ }
63
71
  return created(client.createNewVersion(tokens.accessToken, target.type, target.slug, input));
64
72
  }
65
73
  case "override": {
@@ -70,6 +78,21 @@ export async function runPush(options) {
70
78
  }
71
79
  }
72
80
  }
81
+ /**
82
+ * Check a supersede's revision label against the target's current revision. Without a label the
83
+ * backend would store `1.0.0` — the same label as a first revision — so the two revisions could not
84
+ * be told apart. A target whose own revision is not SemVer cannot be compared, so any SemVer label
85
+ * passes there; refusing would block that component's revisions forever.
86
+ */
87
+ function revisionLabelProblem(label, current) {
88
+ if (!label)
89
+ return "missing";
90
+ if (!isSemVer(label))
91
+ return "not-a-version";
92
+ if (isSemVer(current) && !isStrictlyGreaterSemVer(label, current))
93
+ return "not-newer";
94
+ return undefined;
95
+ }
73
96
  /**
74
97
  * Parse a resolution target reference and require its type to match the payload's: a link is an
75
98
  * alternative definition (same type by the domain model) and a supersede is a version of the same
@@ -86,7 +109,7 @@ function resolveTarget(reference, payload) {
86
109
  /** Load one generator payload file, failing with an actionable message on any malformed input. */
87
110
  function readPayload(payloadPath) {
88
111
  if (!existsSync(payloadPath)) {
89
- throw new Error(`No payload file at:\n ${payloadPath}`);
112
+ throw new Error(`No file at ${payloadPath}.`);
90
113
  }
91
114
  let raw;
92
115
  try {
@@ -94,104 +117,168 @@ function readPayload(payloadPath) {
94
117
  }
95
118
  catch (error) {
96
119
  const detail = error instanceof Error ? error.message : String(error);
97
- throw new Error(`Could not read the payload file:\n ${payloadPath}\n (${detail})`);
120
+ throw new Error(`Could not read ${payloadPath}: ${detail}`);
98
121
  }
99
122
  let parsed;
100
123
  try {
101
124
  parsed = JSON.parse(raw);
102
125
  }
103
126
  catch {
104
- throw new Error(`The payload file is not valid JSON:\n ${payloadPath}`);
127
+ throw new Error(`${payloadPath} is not valid JSON.`);
105
128
  }
106
129
  const result = generatorPayloadSchema.safeParse(parsed);
107
130
  if (!result.success) {
108
- throw new Error(`The payload file does not match the generator payload shape ({metadata, content}):\n ${payloadPath}\n${result.error.message}`);
131
+ throw new Error(`${payloadPath} is not a generated component file. Generate it again.\n Details: ${result.error.message}`);
109
132
  }
110
133
  return result.data;
111
134
  }
135
+ /** The pushed file, named by its component when it reads cleanly — for the messages only. */
136
+ function pushFileOf(payloadPath) {
137
+ try {
138
+ return { path: payloadPath, componentName: readPayload(payloadPath).metadata.name };
139
+ }
140
+ catch {
141
+ return { path: payloadPath };
142
+ }
143
+ }
144
+ /** How every message names a file: `"Name" (path)`, or the path alone when the name is unknown. */
145
+ function describeFile(file) {
146
+ return file.componentName === undefined ? file.path : `"${file.componentName}" (${file.path})`;
147
+ }
112
148
  /**
113
- * Render a knowledge write 409 into the block the orchestrator relays to the human author, or
114
- * `undefined` when the error is not one of the two conflicts. The near-duplicate block names the
115
- * duplicate and the four choices (link / supersede / skip / override) with their exact re-run
116
- * commands and override's coexistence consequence; the already-superseded block points the
117
- * supersede at the chain head the backend reported.
149
+ * The line that ends every message for the author. The orchestrator shows the author everything
150
+ * above it, word for word, and runs the command below it that matches the author's choice.
118
151
  */
119
- export function renderPushConflict(error, payloadPath) {
152
+ export const AGENT_MARKER = "--- Commands for the agent ---";
153
+ /** The next major revision after [current] — the example a label refusal suggests. */
154
+ function nextMajorRevision(current) {
155
+ return isSemVer(current) ? `${Number(current.split(".")[0]) + 1}.0.0` : "2.0.0";
156
+ }
157
+ /** The longest choice name: every description, and every command for the agent, starts after it. */
158
+ const CHOICE_WIDTH = "Create new component".length;
159
+ /** The question and its choices for the author, then the marker and one command per choice for the agent. */
160
+ function renderChoices(choices) {
161
+ const indent = " ".repeat(2 + CHOICE_WIDTH + 2);
162
+ const forAuthor = choices.flatMap(({ name, description: [first, ...rest] }) => [
163
+ ` ${name.padEnd(CHOICE_WIDTH)} ${first}`,
164
+ ...rest.map((line) => `${indent}${line}`),
165
+ ]);
166
+ const forAgent = choices.flatMap(({ name, command }) => command === undefined ? [] : [`${name.padEnd(CHOICE_WIDTH)} ${command}`]);
167
+ return ["What do you want to do?", ...forAuthor, "", AGENT_MARKER, ...forAgent];
168
+ }
169
+ /** Render a refused supersede label into the message for the author, plus the re-run for the agent. */
170
+ export function renderRevisionLabelRefusal(refusal, payloadPath) {
171
+ const { problem, label, target } = refusal;
172
+ const example = nextMajorRevision(target.version);
173
+ const forAuthor = problem === "missing"
174
+ ? [
175
+ `Supersede needs a revision label for the new revision of "${target.name}".`,
176
+ `Its current revision is ${target.version}. Give a newer label, for example ${example}.`,
177
+ ]
178
+ : problem === "not-a-version"
179
+ ? [`The label "${label}" is not a revision number. Use the form X.Y.Z, for example ${example}.`]
180
+ : [
181
+ `The label ${label} is not newer than the current revision of "${target.name}", ${target.version}.`,
182
+ `Give a newer label, for example ${example}.`,
183
+ ];
184
+ return [
185
+ ...forAuthor,
186
+ "",
187
+ AGENT_MARKER,
188
+ `Supersede node push.js ${payloadPath} --supersede '${target.id}' --version <label>`,
189
+ ].join("\n");
190
+ }
191
+ /**
192
+ * Render a knowledge write 409 into the message for the author, or `undefined` when the error is
193
+ * not one of the two conflicts. The duplicate stop names the duplicate and the four choices, with
194
+ * Create new component's consequence stated in its description; the superseded-target stop offers
195
+ * to supersede the newest revision the backend reported.
196
+ */
197
+ export function renderPushConflict(error, file) {
120
198
  if (error.duplicateId) {
121
199
  const ref = error.duplicateId;
122
200
  return [
123
- `Near-duplicate rejected (409): ${error.message}`,
201
+ `${describeFile(file)} was not stored.`,
202
+ `It is almost the same as ${ref}, which Fito already has.`,
124
203
  "",
125
- ` duplicate: ${ref}`,
126
- "",
127
- "The author must choose the resolution — nothing is resubmitted without an explicit choice:",
128
- ` link node push.js ${payloadPath} --link '${ref}'`,
129
- " store as an alternative definition of the duplicate (preferred)",
130
- ` supersede node push.js ${payloadPath} --supersede '${ref}' [--version <label>]`,
131
- " store as the duplicate's new version (preferred)",
132
- " skip do not push this payload (no command — just move on)",
133
- ` override node push.js ${payloadPath} --override '${ref}'`,
134
- " store an independent near-duplicate component that will coexist with the",
135
- " duplicate in the vocabulary — the false-positive escape; confirm the author",
136
- " accepts that consequence before running it",
204
+ ...renderChoices([
205
+ {
206
+ name: "Link",
207
+ description: [`Store it as an alternative definition of ${ref}.`],
208
+ command: `node push.js ${file.path} --link '${ref}'`,
209
+ },
210
+ {
211
+ name: "Supersede",
212
+ description: [`Store it as the next revision of ${ref}.`],
213
+ command: `node push.js ${file.path} --supersede '${ref}'`,
214
+ },
215
+ { name: "Skip", description: ["Store nothing for this file."] },
216
+ {
217
+ name: "Create new component",
218
+ description: ["Store it as a separate component. Fito will then have two", "almost identical components."],
219
+ command: `node push.js ${file.path} --override '${ref}'`,
220
+ },
221
+ ]),
137
222
  ].join("\n");
138
223
  }
139
224
  if (error.supersededBy) {
140
225
  const head = error.supersededBy;
141
226
  return [
142
- `Target already superseded (409): ${error.message}`,
143
- "",
144
- ` supersededBy: ${head}`,
227
+ `The component you chose to supersede already has a newer revision: ${head}.`,
228
+ "A new revision must follow the newest one. Do you want to supersede that one?",
145
229
  "",
146
- "Version chains are linear — a new version must target the chain head. Offer the author",
147
- "a re-run against the reported successor:",
148
- ` node push.js ${payloadPath} --supersede '${head}' [--version <label>]`,
230
+ AGENT_MARKER,
231
+ `Supersede node push.js ${file.path} --supersede '${head}'`,
149
232
  ].join("\n");
150
233
  }
151
234
  return undefined;
152
235
  }
236
+ /** A backend refusal that is not a conflict: which component was not stored, and the backend's reason. */
237
+ export function renderFailure(error, file) {
238
+ return `✗ ${describeFile(file)} was not stored: ${error.message}`;
239
+ }
153
240
  /**
154
- * Render the advisory probe's near-misses into the block the orchestrator relays to the human
155
- * author. Similarities are shown so the author can judge how close each match really is. Nothing
156
- * has been stored when this block prints.
241
+ * Render the advisory probe's near-misses into the message for the author. Similarities are shown
242
+ * so the author can judge how close each match really is. Nothing has been stored when it prints.
157
243
  *
158
- * Each line names both version axes apart — the content revision (ADR-010) and the major lines the
159
- * match applies to (ADR-011) — plus the server's reading of that scope against the candidate.
160
- * The re-run commands target the closest **same-line** match rather than the closest overall,
161
- * because only a same-line match is one the hard gate would refuse: linking to a cross-line match
162
- * would mint the `alternativeDefinitionOf` edge ADR-011 §7 forbids for divergence across majors,
163
- * and superseding one is worse still — a new version inherits its predecessor's version scope, so
164
- * the component would silently land on the wrong line.
244
+ * The backend reports only matches sharing a major line with the candidate, so every match is one
245
+ * link or supersede can target, and the commands target the closest. Called only with at least one
246
+ * match: an empty probe report stores the component.
165
247
  */
166
- export function renderNearMisses(matches, payloadPath) {
248
+ export function renderNearMisses(matches, file) {
249
+ const nameWidth = Math.max(...matches.map((match) => match.name.length + 2));
167
250
  const listed = matches.map((match) => {
168
- const appliesTo = match.technologyVersions.map((version) => version.slug).join(", ");
169
- const scope = match.sameVersionScope ? "same line" : "different line";
170
- return (` ${match.similarity.toFixed(2)} ${match.id} — "${match.name}" ` +
171
- `(revision ${match.version}, applies to ${appliesTo} — ${scope})`);
251
+ const similarity = `${Math.round(match.similarity * 100)}%`.padStart(4);
252
+ return ` ${similarity} similar ${`"${match.name}"`.padEnd(nameWidth)} ${match.id}`;
172
253
  });
173
- const closestSameLine = matches.find((match) => match.sameVersionScope);
174
- // With no same-line match there is nothing to link or supersede against: the gate would admit
175
- // this candidate, and §7 says divergence across majors is separate components, not links.
176
- const resolutions = closestSameLine === undefined
177
- ? [" every match above is on a different major line, so none is a duplicate of this candidate"]
178
- : [
179
- ` link node push.js ${payloadPath} --link '${closestSameLine.id}'`,
180
- " store as an alternative definition of the closest same-line match (preferred)",
181
- ` supersede node push.js ${payloadPath} --supersede '${closestSameLine.id}' [--version <label>]`,
182
- " store as the closest same-line match's new version (preferred)",
183
- ];
254
+ const closest = matches[0];
184
255
  return [
185
- "Similar existing components found (advisory probe) — nothing has been stored:",
256
+ `${describeFile(file)} was not stored.`,
257
+ "Fito already has components that are similar to it:",
186
258
  "",
187
259
  ...listed,
188
260
  "",
189
- "The author must choose — nothing is stored without an explicit choice:",
190
- ...resolutions,
191
- " skip do not push this payload (no command — just move on)",
192
- ` proceed node push.js ${payloadPath} --proceed`,
193
- " store an independent component that will coexist with the matches; a match",
194
- " at the hard duplicate bar still rejects this with the 409 and its override echo",
261
+ ...renderChoices([
262
+ {
263
+ name: "Link",
264
+ description: [`Store it as an alternative definition of "${closest.name}".`],
265
+ command: `node push.js ${file.path} --link '${closest.id}'`,
266
+ },
267
+ {
268
+ name: "Supersede",
269
+ description: [`Store it as the next revision of "${closest.name}" (now revision ${closest.version}).`],
270
+ command: `node push.js ${file.path} --supersede '${closest.id}'`,
271
+ },
272
+ { name: "Skip", description: ["Store nothing for this file."] },
273
+ {
274
+ name: "Create new component",
275
+ description: [
276
+ "Store it as a separate component, next to the similar ones.",
277
+ "If it is almost the same as one of them, Fito asks you again.",
278
+ ],
279
+ command: `node push.js ${file.path} --proceed`,
280
+ },
281
+ ]),
195
282
  ].join("\n");
196
283
  }
197
284
  const USAGE = "Usage: node push.js <payload-file> [--link <ref> | --supersede <ref> | --override <ref> | --proceed] [--version <label>]";
@@ -230,21 +317,21 @@ function parseArgs(argv) {
230
317
  return options;
231
318
  }
232
319
  /**
233
- * The success line. It names the payload file alongside the created canonical reference so the
234
- * orchestrator's file→reference ledger can be reconstructed from the transcript alone — the
235
- * stateless answer to "which payloads are already pushed?" on an interrupted flow.
320
+ * The success line. It names the file alongside the stored reference id, so the orchestrator's
321
+ * file→reference ledger can be reconstructed from the transcript alone — the stateless answer to
322
+ * "which files are already stored?" on an interrupted flow.
236
323
  */
237
324
  export function describeOutcome(created, payloadPath, resolution) {
238
- const headline = `✓ Created ${created.id} — "${created.name}" (version ${created.version}) ← ${payloadPath}`;
325
+ const headline = `✓ Stored "${created.name}" as ${created.id} (revision ${created.version}), from ${payloadPath}`;
239
326
  if (!resolution)
240
327
  return headline;
241
328
  switch (resolution.kind) {
242
329
  case "link":
243
- return `${headline}\n linked as an alternative definition of ${resolution.target}`;
330
+ return `${headline}\n It is an alternative definition of ${resolution.target}.`;
244
331
  case "supersede":
245
- return `${headline}\n supersedes ${resolution.target}`;
332
+ return `${headline}\n It is the next revision of ${resolution.target}.`;
246
333
  case "override":
247
- return `${headline}\n stored independently (override acknowledged: ${resolution.duplicate})`;
334
+ return `${headline}\n It is a separate component, next to ${resolution.duplicate}.`;
248
335
  }
249
336
  }
250
337
  async function main() {
@@ -257,7 +344,12 @@ async function main() {
257
344
  try {
258
345
  const outcome = await runPush(options);
259
346
  if (outcome.kind === "near-misses") {
260
- console.error(renderNearMisses(outcome.matches, options.payloadPath));
347
+ console.error(renderNearMisses(outcome.matches, pushFileOf(options.payloadPath)));
348
+ process.exitCode = 1;
349
+ return;
350
+ }
351
+ if (outcome.kind === "revision-label-refused") {
352
+ console.error(renderRevisionLabelRefusal(outcome, options.payloadPath));
261
353
  process.exitCode = 1;
262
354
  return;
263
355
  }
@@ -265,8 +357,8 @@ async function main() {
265
357
  }
266
358
  catch (error) {
267
359
  if (error instanceof FitoApiError) {
268
- const conflict = renderPushConflict(error, options.payloadPath);
269
- console.error(conflict ?? `✗ Push failed (${error.status}): ${error.message}`);
360
+ const file = pushFileOf(options.payloadPath);
361
+ console.error(renderPushConflict(error, file) ?? renderFailure(error, file));
270
362
  }
271
363
  else {
272
364
  console.error(`✗ ${error instanceof Error ? error.message : String(error)}`);