@sabaiway/agent-workflow-kit 15.1.0 → 16.1.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.
@@ -1,75 +0,0 @@
1
- // replace-set.mjs — the ONE replace block an upgrade prints over every file whose write waits for the
2
- // user's yes, and the replace run that yes starts. An owning op reports entries on its outcome: { rel,
3
- // sha256, bytes, channel } for an edited file, { rel, absent: true, channel } for an absent one, either
4
- // marked `hookRun: true` when the commit hook runs it. One digest binds their union, one block prints
5
- // over it, and the yes keeps every edit aside (kept-aside.mjs) before any op replaces a byte. It never
6
- // imports ensure-configs.mjs: the CLI hands it the ops it runs.
7
- //
8
- // Dependency-free, Node >= 22. No side effects on import.
9
-
10
- import { createHash } from 'node:crypto';
11
- import { join } from 'node:path';
12
- import { composeFailure } from '../ensure-ops.mjs';
13
- import { shellQuoteArg } from '../repo-lex.mjs';
14
- import { keepAside, keptAsidePath } from './kept-aside.mjs';
15
-
16
- export { keptAsidePath };
17
-
18
- const INDEX_NOT_RUN = 'the navigator step did not run: a replace run runs it only after every step it re-runs succeeds';
19
- const RACE_LINE = 'the files to replace changed since the replace line was printed (another set or other bytes), so nothing was written — ask the user again over the fresh block below';
20
- const HOOK_RUN = '; the commit hook runs it, so it may stop the next commit';
21
-
22
- // The digest binds the set AND the bytes: `<rel> <sha256>` or `<rel> absent`, sorted by rel in
23
- // code-unit order, LF-joined. Over an all-edited set it is the digest the specs op printed before.
24
- const byRel = (a, b) => (a.rel < b.rel ? -1 : 1);
25
- const digestLine = (entry) => `${entry.rel} ${entry.absent ? 'absent' : entry.sha256}`;
26
- export const replaceSetDigest = (entries) => createHash('sha256').update([...entries].sort(byRel).map(digestLine).join('\n')).digest('hex');
27
-
28
- const entryLine = (entry, digest) => `${entry.absent
29
- ? `${entry.rel}: absent — the user's yes places the bundled body`
30
- : `${entry.rel}: carries an edit — on the user's yes it is kept aside at ${keptAsidePath(digest, entry.rel)}, then the bundled body replaces it; an edit meant to last goes in ${entry.channel}`}${entry.hookRun ? HOOK_RUN : ''}`;
31
-
32
- // One line per entry in rel order, then the ONE command line, its condition first and both paths
33
- // shell-quoted; no line at all for an empty set.
34
- export const replaceBlock = (entries, { kitRoot, cwd }) => {
35
- if (entries.length === 0) return [];
36
- const digest = replaceSetDigest(entries);
37
- return [
38
- ...[...entries].sort(byRel).map((entry) => entryLine(entry, digest)),
39
- `only on the user's yes, keep aside and replace the files named above: node ${shellQuoteArg(join(kitRoot, 'tools', 'ensure-configs.mjs'))} --reconcile --replace-custom ${digest} --cwd ${shellQuoteArg(cwd)}`,
40
- ];
41
- };
42
-
43
- // A refused survey is a failed outcome that carries no entries field: that op takes no part.
44
- const refused = (outcome) => outcome.failed && outcome.entries === undefined;
45
-
46
- // runReplace({ cwd, kitRoot, digest, owning, index, deps }) → { outcomes, block }. `owning` is
47
- // [{ op, run }] in ENSURE_OPS order and `index` the navigator's run; each run(args) returns an
48
- // outcome and never throws (the CLI wraps it). `block` carries the recomputed union only when the
49
- // digest differs — every other outcome of a replace run prints no block.
50
- export const runReplace = ({ cwd, kitRoot, digest, owning, index, deps = {} }) => {
51
- const surveys = owning.map(({ op, run }) => ({ op, run, survey: run({ cwd, kitRoot, dryRun: true, deps }) })).map((s) => ({ ...s, entries: s.survey.entries ?? [] }));
52
- // Outcomes in owning order; index only when none failed, else the last failed one ends with the navigator line.
53
- const finish = (results, block = []) => {
54
- const outcomes = results.filter(Boolean);
55
- const last = outcomes.findLastIndex((o) => o.failed);
56
- if (last === -1) return { outcomes: [...outcomes, index({ cwd, kitRoot, dryRun: false, deps })], block };
57
- return { outcomes: outcomes.map((o, i) => (i === last ? { ...o, lines: [...o.lines, INDEX_NOT_RUN] } : o)), block };
58
- };
59
- const results = surveys.map((s) => (refused(s.survey) ? s.survey : null));
60
- const stopped = surveys.findIndex((s) => s.survey.failed && !refused(s.survey));
61
- if (stopped !== -1) return finish(results.map((o, i) => (i === stopped ? surveys[i].survey : o)));
62
- const taking = surveys.filter((s) => !refused(s.survey));
63
- if (taking.length === 0) return finish(results);
64
- const union = taking.flatMap((s) => s.entries);
65
- const atFirst = (outcome) => results.map((o, i) => (surveys[i] === taking[0] ? outcome : o));
66
- if (union.length > 0 && replaceSetDigest(union) !== digest) return finish(atFirst(composeFailure(taking[0].op, 'race-unresolved', RACE_LINE)), union);
67
- const kept = keepAside({ cwd, digest, entries: union, deps });
68
- if (kept.failed) return finish(atFirst(composeFailure(taking[0].op, kept.cause, ...kept.lines)));
69
- for (const [i, s] of surveys.entries()) {
70
- if (refused(s.survey)) continue;
71
- results[i] = s.run({ cwd, kitRoot, dryRun: false, deps, replace: { digest, entries: s.entries } });
72
- if (results[i].failed) break;
73
- }
74
- return finish(results);
75
- };