@plebeiantech/lat.md 0.12.3-fork.1 → 0.12.3-fork.10
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 +39 -3
- package/dist/src/cli/check-coverage.d.ts +2 -0
- package/dist/src/cli/check-coverage.js +71 -0
- package/dist/src/cli/check-mode.d.ts +16 -1
- package/dist/src/cli/check-mode.js +65 -4
- package/dist/src/cli/check.js +5 -1
- package/dist/src/cli/comment-guard.d.ts +14 -0
- package/dist/src/cli/comment-guard.js +10 -33
- package/dist/src/cli/comment-reminder.d.ts +65 -6
- package/dist/src/cli/comment-reminder.js +164 -28
- package/dist/src/cli/fork-instructions.d.ts +19 -0
- package/dist/src/cli/fork-instructions.js +183 -0
- package/dist/src/cli/fork-scaffold.d.ts +106 -0
- package/dist/src/cli/fork-scaffold.js +490 -0
- package/dist/src/cli/index.js +29 -4
- package/dist/src/cli/init.js +5 -0
- package/dist/src/cli/search.js +6 -4
- package/dist/src/cli/ui-build.d.ts +4 -0
- package/dist/src/cli/ui-build.js +13 -0
- package/dist/src/cli/ui.js +18 -2
- package/dist/src/code-refs.js +25 -4
- package/dist/src/fork/upstream-drift-cli.d.ts +1 -0
- package/dist/src/fork/upstream-drift-cli.js +2 -0
- package/dist/src/fork/upstream-drift.d.ts +26 -0
- package/dist/src/fork/upstream-drift.js +185 -0
- package/dist/src/fork/upstream-guard-cli.d.ts +1 -0
- package/dist/src/fork/upstream-guard-cli.js +2 -0
- package/dist/src/fork/upstream-guard.d.ts +72 -0
- package/dist/src/fork/upstream-guard.js +445 -0
- package/dist/src/graph-export.d.ts +2 -0
- package/dist/src/graph-export.js +31 -12
- package/dist/src/init-version.d.ts +1 -1
- package/dist/src/init-version.js +1 -1
- package/dist/src/knowledge/claude-memory.d.ts +1 -0
- package/dist/src/knowledge/claude-memory.js +33 -4
- package/dist/src/lattice.d.ts +2 -1
- package/dist/src/lattice.js +1 -0
- package/dist/src/search/index.js +23 -6
- package/dist/src/search/search.d.ts +1 -0
- package/dist/src/search/search.js +15 -9
- package/dist/src/untrusted.d.ts +0 -6
- package/dist/src/untrusted.js +7 -11
- package/dist/src/view/client/assets/index-C3iaZyYT.css +1 -0
- package/dist/src/view/client/assets/index-KWi67IMz.js +334 -0
- package/dist/src/view/client/assets/logo-CNF8zs47.svg +182 -0
- package/dist/src/view/client/index.html +2 -2
- package/dist/src/view/git-diff.js +17 -0
- package/dist/src/view/graph.d.ts +6 -0
- package/dist/src/view/graph.js +176 -0
- package/dist/src/view/markdown.d.ts +5 -1
- package/dist/src/view/markdown.js +112 -25
- package/dist/src/view/protocol.d.ts +43 -0
- package/dist/src/view/protocol.js +1 -1
- package/dist/src/view/references.d.ts +2 -0
- package/dist/src/view/references.js +30 -8
- package/dist/src/view/repository.d.ts +5 -2
- package/dist/src/view/repository.js +13 -12
- package/dist/src/view/search.js +1 -0
- package/dist/src/view/server.d.ts +3 -0
- package/dist/src/view/server.js +42 -9
- package/dist/src/view/source-target.d.ts +8 -0
- package/dist/src/view/source-target.js +22 -0
- package/dist/src/view/static-build.d.ts +16 -0
- package/dist/src/view/static-build.js +408 -0
- package/dist/src/view/static-protocol.d.ts +23 -0
- package/dist/src/view/static-protocol.js +10 -0
- package/dist/src/view/store.d.ts +9 -1
- package/dist/src/view/store.js +82 -15
- package/dist/src/view/table-of-contents.d.ts +9 -0
- package/dist/src/view/table-of-contents.js +103 -0
- package/package.json +12 -10
- package/templates/AGENTS.md +2 -13
- package/templates/fork/conventions.md +119 -0
- package/templates/skill/SKILL.md +3 -14
- package/dist/src/view/client/assets/index-B0n0u03y.css +0 -1
- package/dist/src/view/client/assets/index-xm2_px4q.js +0 -23
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { execSync } from 'node:child_process';
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
|
-
import { existsSync, mkdirSync, writeFileSync, statSync, lstatSync, } from 'node:fs';
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, statSync, lstatSync, } from 'node:fs';
|
|
4
4
|
import { tmpdir } from 'node:os';
|
|
5
5
|
import { basename, dirname, join } from 'node:path';
|
|
6
6
|
const FAMILIES = [
|
|
@@ -41,35 +41,174 @@ export function matchFamily(filePath) {
|
|
|
41
41
|
}
|
|
42
42
|
return null;
|
|
43
43
|
}
|
|
44
|
-
/**
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
const
|
|
44
|
+
/** Line multiset of `text`, used as the baseline a written fragment is new
|
|
45
|
+
* against. */
|
|
46
|
+
function lineCounts(text) {
|
|
47
|
+
const counts = new Map();
|
|
48
|
+
for (const line of text.split('\n')) {
|
|
49
|
+
counts.set(line, (counts.get(line) ?? 0) + 1);
|
|
50
|
+
}
|
|
51
|
+
return counts;
|
|
52
|
+
}
|
|
53
|
+
/** Baseline taken from the file itself, or `null` when there is no readable
|
|
54
|
+
* file — a brand-new path, a directory, a permission error, a payload naming
|
|
55
|
+
* no file. `null` means every line counts as new, which is what makes an
|
|
56
|
+
* unreadable file fail closed: the gate denies rather than waving the write
|
|
57
|
+
* through on evidence it could not gather. */
|
|
58
|
+
function fileBaseline(filePath) {
|
|
59
|
+
if (!filePath)
|
|
60
|
+
return null;
|
|
61
|
+
try {
|
|
62
|
+
return lineCounts(readFileSync(filePath, 'utf-8'));
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The fragments one tool call writes, each against the narrowest baseline
|
|
70
|
+
* available for it.
|
|
71
|
+
*
|
|
72
|
+
* `old_string` is preferred over the file wherever the payload carries one,
|
|
73
|
+
* because the file is the wrong scope: matching a written line against
|
|
74
|
+
* *anywhere* in the file forgives a genuinely new block whenever one of its
|
|
75
|
+
* lines happens to duplicate an unrelated line elsewhere. `old_string` is the
|
|
76
|
+
* exact text being replaced, so the difference is the real delta.
|
|
77
|
+
*
|
|
78
|
+
* Baselines are per fragment rather than shared across a `MultiEdit`: two
|
|
79
|
+
* hunks that each re-emit the same surrounding comment are both re-emitting
|
|
80
|
+
* it, and charging the second one for it would deny exactly the edit this is
|
|
81
|
+
* here to allow.
|
|
82
|
+
*/
|
|
83
|
+
function writtenFragments(toolInput, phase) {
|
|
84
|
+
let diskRead = false;
|
|
85
|
+
let disk = null;
|
|
86
|
+
const fromFile = () => {
|
|
87
|
+
if (phase !== 'before-write')
|
|
88
|
+
return null;
|
|
89
|
+
if (!diskRead) {
|
|
90
|
+
disk = fileBaseline(toolInput.file_path);
|
|
91
|
+
diskRead = true;
|
|
92
|
+
}
|
|
93
|
+
return disk;
|
|
94
|
+
};
|
|
95
|
+
const baselineFor = (oldString) => typeof oldString === 'string' ? lineCounts(oldString) : fromFile();
|
|
96
|
+
const fragments = [];
|
|
48
97
|
if (typeof toolInput.content === 'string')
|
|
49
|
-
|
|
98
|
+
fragments.push({ text: toolInput.content, baseline: fromFile() });
|
|
50
99
|
if (typeof toolInput.new_string === 'string')
|
|
51
|
-
|
|
100
|
+
fragments.push({
|
|
101
|
+
text: toolInput.new_string,
|
|
102
|
+
baseline: baselineFor(toolInput.old_string),
|
|
103
|
+
});
|
|
52
104
|
if (Array.isArray(toolInput.edits)) {
|
|
53
105
|
for (const edit of toolInput.edits) {
|
|
54
106
|
if (typeof edit?.new_string === 'string')
|
|
55
|
-
|
|
107
|
+
fragments.push({
|
|
108
|
+
text: edit.new_string,
|
|
109
|
+
baseline: baselineFor(edit.old_string),
|
|
110
|
+
});
|
|
56
111
|
}
|
|
57
112
|
}
|
|
58
|
-
return
|
|
113
|
+
return fragments;
|
|
59
114
|
}
|
|
60
|
-
/**
|
|
115
|
+
/** Explicit, reviewable per-line opt-out, spelled the same as the token
|
|
116
|
+
* [[src/code-refs.ts]] already honours so a project has one escape hatch to
|
|
117
|
+
* learn rather than two. It lives here, not in the guard, because the
|
|
118
|
+
* advisory and blocking halves have to agree on it: an exemption that the
|
|
119
|
+
* gate honours but the reminder does not just relocates the nag.
|
|
120
|
+
* It suppresses only the line it appears on, keeping a deliberate exception
|
|
121
|
+
* visible in the diff instead of silently disarming the convention for a
|
|
122
|
+
* whole file. */
|
|
123
|
+
export const LAT_IGNORE_RE = /(?<![\w])lat:ignore(?![-\w])/;
|
|
124
|
+
/** Whether one line is a comment worth counting. Not counted:
|
|
61
125
|
* - a line that already carries a `@lat:` pointer (the compliant state)
|
|
126
|
+
* - a line carrying the explicit opt-out token
|
|
62
127
|
* - machine directives: shebangs, magic comments, linter/type pragmas
|
|
63
128
|
* - decoration with no alphanumeric character (`# ----`, `//////`, a bare
|
|
64
|
-
* `*/` closing a block)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
.
|
|
71
|
-
|
|
72
|
-
|
|
129
|
+
* `*/` closing a block)
|
|
130
|
+
*
|
|
131
|
+
* Both halves of the convention call this and nothing else, so the gate and
|
|
132
|
+
* the reminder cannot drift apart on what counts as prose. */
|
|
133
|
+
export function isCandidateCommentLine(line, family) {
|
|
134
|
+
return (family.commentRe.test(line) &&
|
|
135
|
+
!line.includes('@lat:') &&
|
|
136
|
+
!LAT_IGNORE_RE.test(line) &&
|
|
137
|
+
!/^\s*#!/.test(line) &&
|
|
138
|
+
!family.pragmaRe.test(line) &&
|
|
139
|
+
/[a-zA-Z0-9]/.test(line));
|
|
140
|
+
}
|
|
141
|
+
/** Candidate comment lines in one write before either half reacts. A single
|
|
142
|
+
* line ("// bytes, not chars") is the bare-fact case the convention allows;
|
|
143
|
+
* two is a block of prose. */
|
|
144
|
+
const PROSE_THRESHOLD = 2;
|
|
145
|
+
/**
|
|
146
|
+
* What one Edit/Write/MultiEdit call did to the comments in a file.
|
|
147
|
+
*
|
|
148
|
+
* Two rules, because neither alone survives contact with an agent:
|
|
149
|
+
*
|
|
150
|
+
* A run of `PROSE_THRESHOLD` or more *adjacent* comment lines is flagged as
|
|
151
|
+
* soon as any one of them is new. Counting only the new lines lets a block be
|
|
152
|
+
* grown one line per `Edit` and never reach the threshold — and the denial
|
|
153
|
+
* text itself recommends `Edit` over `Write`, so the technique is advertised.
|
|
154
|
+
* What matters is the block the edit leaves behind, not the increment.
|
|
155
|
+
*
|
|
156
|
+
* Isolated new comment lines are flagged once `PROSE_THRESHOLD` of them
|
|
157
|
+
* accumulate anywhere in the call. Two one-line comments in different places
|
|
158
|
+
* are still two lines of prose; without this a scattered write walks past the
|
|
159
|
+
* adjacency rule.
|
|
160
|
+
*
|
|
161
|
+
* Neither rule counts a line that was already there, so re-emitting a
|
|
162
|
+
* comment — the whole of a `Write`'s payload, or the lines bracketing an
|
|
163
|
+
* `Edit` — stays free.
|
|
164
|
+
*/
|
|
165
|
+
export function judgeWrittenComments(toolInput, family, phase) {
|
|
166
|
+
const fragments = writtenFragments(toolInput, phase);
|
|
167
|
+
if (!fragments.some((fragment) => fragment.text !== ''))
|
|
168
|
+
return { count: 0, flagged: false };
|
|
169
|
+
let inFlaggedRuns = 0;
|
|
170
|
+
let isolatedNew = 0;
|
|
171
|
+
for (const fragment of fragments) {
|
|
172
|
+
const lines = fragment.text.split('\n');
|
|
173
|
+
const unclaimed = fragment.baseline ? new Map(fragment.baseline) : null;
|
|
174
|
+
const isNew = [];
|
|
175
|
+
const isComment = [];
|
|
176
|
+
for (const line of lines) {
|
|
177
|
+
let fresh = true;
|
|
178
|
+
if (unclaimed) {
|
|
179
|
+
const left = unclaimed.get(line) ?? 0;
|
|
180
|
+
if (left > 0) {
|
|
181
|
+
unclaimed.set(line, left - 1);
|
|
182
|
+
fresh = false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
isNew.push(fresh);
|
|
186
|
+
isComment.push(isCandidateCommentLine(line, family));
|
|
187
|
+
}
|
|
188
|
+
let i = 0;
|
|
189
|
+
while (i < lines.length) {
|
|
190
|
+
if (!isComment[i]) {
|
|
191
|
+
i += 1;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
let end = i;
|
|
195
|
+
while (end < lines.length && isComment[end])
|
|
196
|
+
end += 1;
|
|
197
|
+
let fresh = 0;
|
|
198
|
+
for (let k = i; k < end; k += 1)
|
|
199
|
+
if (isNew[k])
|
|
200
|
+
fresh += 1;
|
|
201
|
+
if (end - i >= PROSE_THRESHOLD && fresh > 0)
|
|
202
|
+
inFlaggedRuns += end - i;
|
|
203
|
+
else
|
|
204
|
+
isolatedNew += fresh;
|
|
205
|
+
i = end;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
count: inFlaggedRuns + isolatedNew,
|
|
210
|
+
flagged: inFlaggedRuns > 0 || isolatedNew >= PROSE_THRESHOLD,
|
|
211
|
+
};
|
|
73
212
|
}
|
|
74
213
|
/** Resolvable project root, required so a scratch file in /tmp is not treated
|
|
75
214
|
* as a project. `cwd` is preferred (matches CLAUDE_PROJECT_DIR / payload cwd
|
|
@@ -176,16 +315,13 @@ export function computeCommentReminder(input) {
|
|
|
176
315
|
const family = matchFamily(filePath);
|
|
177
316
|
if (!family)
|
|
178
317
|
return null;
|
|
179
|
-
const written = extractWrittenText(toolInput);
|
|
180
|
-
if (!written)
|
|
181
|
-
return null;
|
|
182
318
|
// A single-line comment ("// increment the counter") almost always states
|
|
183
319
|
// a bare fact, not a rationale — the acceptance case for this hook. Only a
|
|
184
|
-
//
|
|
185
|
-
//
|
|
186
|
-
//
|
|
187
|
-
const
|
|
188
|
-
if (
|
|
320
|
+
// block is treated as prose worth a reminder; the model still makes the
|
|
321
|
+
// real judgment call, this just filters the overwhelmingly common one-liner
|
|
322
|
+
// before it ever reaches that question.
|
|
323
|
+
const verdict = judgeWrittenComments(toolInput, family, 'after-write');
|
|
324
|
+
if (!verdict.flagged)
|
|
189
325
|
return null;
|
|
190
326
|
const sessionId = input.session_id;
|
|
191
327
|
let dedup = null;
|
|
@@ -201,7 +337,7 @@ export function computeCommentReminder(input) {
|
|
|
201
337
|
const base = basename(filePath);
|
|
202
338
|
const message = buildMessage({
|
|
203
339
|
base,
|
|
204
|
-
count:
|
|
340
|
+
count: verdict.count,
|
|
205
341
|
marker: family.marker,
|
|
206
342
|
hasTree,
|
|
207
343
|
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare function readForkConventions(): string;
|
|
2
|
+
/**
|
|
3
|
+
* Replace the fork block in `content`, or append one when absent.
|
|
4
|
+
*
|
|
5
|
+
* Returns null when the block is already byte-identical, so the caller can
|
|
6
|
+
* stay quiet rather than announce a write that did not happen.
|
|
7
|
+
*/
|
|
8
|
+
export declare function spliceForkBlock(content: string, body: string): string | null;
|
|
9
|
+
/** Read the body currently fenced by the fork markers, or null if absent. */
|
|
10
|
+
export declare function extractForkBlock(content: string): string | null;
|
|
11
|
+
/**
|
|
12
|
+
* Write the fork's conventions into every instruction file and skill directory
|
|
13
|
+
* the selected agents produced.
|
|
14
|
+
*
|
|
15
|
+
* `fileHashes` is upstream's record, threaded through so `writeInitMeta`
|
|
16
|
+
* persists these entries alongside its own; a stored hash is what lets a later
|
|
17
|
+
* run tell "the user edited our block" from "the template moved on".
|
|
18
|
+
*/
|
|
19
|
+
export declare function writeForkInstructions(root: string, latDir: string, fileHashes: Record<string, string>, ask: (message: string) => Promise<boolean>): Promise<void>;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { styleText } from 'node:util';
|
|
4
|
+
import { contentHash, readFileHash } from '../init-version.js';
|
|
5
|
+
import { findTemplatesDir } from './templates.js';
|
|
6
|
+
/**
|
|
7
|
+
* The fork's own instruction channel into a consumer project.
|
|
8
|
+
*
|
|
9
|
+
* Upstream teaches a consumer's agent through `templates/AGENTS.md` and
|
|
10
|
+
* `templates/skill/SKILL.md`. Both are upstream files, and both are the ones
|
|
11
|
+
* upstream is most likely to rewrite, so every sentence this fork adds to them
|
|
12
|
+
* is a merge conflict bought on credit. The fork's Diátaxis block cost 13
|
|
13
|
+
* lines in one and 14 in the other before this module existed.
|
|
14
|
+
*
|
|
15
|
+
* Everything here writes into fork-owned paths instead. The entire upstream
|
|
16
|
+
* footprint is one import and one call in `initCmd`.
|
|
17
|
+
*
|
|
18
|
+
* Two shapes, because the destinations behave differently:
|
|
19
|
+
*
|
|
20
|
+
* - Always-loaded instruction files (`CLAUDE.md`, `AGENTS.md`,
|
|
21
|
+
* `.github/copilot-instructions.md`) already carry upstream's
|
|
22
|
+
* `%% lat:begin %%` block, so this appends a *second* block under its own
|
|
23
|
+
* `%% lat-fork:begin %%` markers. Upstream's `appendTemplateSection` slices
|
|
24
|
+
* between its own markers and leaves anything after them intact, so the two
|
|
25
|
+
* blocks do not fight on re-run.
|
|
26
|
+
* - Skill directories get a separate `lat-md-conventions/SKILL.md`. Upstream
|
|
27
|
+
* writes its `lat-md` skill with a whole-file hash comparison, so appending
|
|
28
|
+
* to that file would make every later `lat init` report it as
|
|
29
|
+
* user-modified and prompt.
|
|
30
|
+
*/
|
|
31
|
+
const FORK_BEGIN = '%% lat-fork:begin %%';
|
|
32
|
+
const FORK_END = '%% lat-fork:end %%';
|
|
33
|
+
/**
|
|
34
|
+
* Hash-map key for a fork block inside a file upstream also owns. Suffixed so
|
|
35
|
+
* it can never collide with upstream's entry for the same path — both live in
|
|
36
|
+
* one flat `file_hashes` record.
|
|
37
|
+
*/
|
|
38
|
+
function forkHashKey(relPath) {
|
|
39
|
+
return `${relPath}#lat-fork`;
|
|
40
|
+
}
|
|
41
|
+
/** Instruction files that use upstream's marker-append shape. */
|
|
42
|
+
const BLOCK_TARGETS = [
|
|
43
|
+
'CLAUDE.md',
|
|
44
|
+
'AGENTS.md',
|
|
45
|
+
'.github/copilot-instructions.md',
|
|
46
|
+
];
|
|
47
|
+
/**
|
|
48
|
+
* Skill roots every agent integration installs into. Written to only when the
|
|
49
|
+
* directory already exists, which is what keeps this list from creating skill
|
|
50
|
+
* directories for agents the user did not select.
|
|
51
|
+
*/
|
|
52
|
+
const SKILL_ROOTS = [
|
|
53
|
+
'.claude/skills',
|
|
54
|
+
'.pi/skills',
|
|
55
|
+
'.agents/skills',
|
|
56
|
+
'.codex/skills',
|
|
57
|
+
];
|
|
58
|
+
const SKILL_NAME = 'lat-md-conventions';
|
|
59
|
+
const SKILL_FRONTMATTER = `---
|
|
60
|
+
name: ${SKILL_NAME}
|
|
61
|
+
description: >-
|
|
62
|
+
Rules lat check enforces in this project: Diátaxis mode placement for every
|
|
63
|
+
lat.md/ document, mandatory @lat: code refs starting at the application
|
|
64
|
+
entrypoint, and test specs wired to their tests. Load alongside lat-md
|
|
65
|
+
whenever creating or editing files under lat.md/.
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
`;
|
|
69
|
+
export function readForkConventions() {
|
|
70
|
+
return readFileSync(join(findTemplatesDir(), 'fork', 'conventions.md'), 'utf-8');
|
|
71
|
+
}
|
|
72
|
+
function wrapBlock(body) {
|
|
73
|
+
return `${FORK_BEGIN}\n${body}${body.endsWith('\n') ? '' : '\n'}${FORK_END}\n`;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Replace the fork block in `content`, or append one when absent.
|
|
77
|
+
*
|
|
78
|
+
* Returns null when the block is already byte-identical, so the caller can
|
|
79
|
+
* stay quiet rather than announce a write that did not happen.
|
|
80
|
+
*/
|
|
81
|
+
export function spliceForkBlock(content, body) {
|
|
82
|
+
const wrapped = wrapBlock(body);
|
|
83
|
+
const begin = content.indexOf(FORK_BEGIN);
|
|
84
|
+
const end = content.indexOf(FORK_END);
|
|
85
|
+
if (begin === -1 || end === -1 || end <= begin) {
|
|
86
|
+
let base = content;
|
|
87
|
+
if (base.length > 0 && !base.endsWith('\n'))
|
|
88
|
+
base += '\n';
|
|
89
|
+
return base + (base.length > 0 ? '\n' : '') + wrapped;
|
|
90
|
+
}
|
|
91
|
+
const endWithMarker = end + FORK_END.length;
|
|
92
|
+
const endWithNl = content[endWithMarker] === '\n' ? endWithMarker + 1 : endWithMarker;
|
|
93
|
+
const existing = content.slice(begin, endWithNl);
|
|
94
|
+
if (existing === wrapped)
|
|
95
|
+
return null;
|
|
96
|
+
return content.slice(0, begin) + wrapped + content.slice(endWithNl);
|
|
97
|
+
}
|
|
98
|
+
/** Read the body currently fenced by the fork markers, or null if absent. */
|
|
99
|
+
export function extractForkBlock(content) {
|
|
100
|
+
const begin = content.indexOf(FORK_BEGIN);
|
|
101
|
+
const end = content.indexOf(FORK_END);
|
|
102
|
+
if (begin === -1 || end === -1 || end <= begin)
|
|
103
|
+
return null;
|
|
104
|
+
return content.slice(begin + FORK_BEGIN.length + 1, end);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Write the fork's conventions into every instruction file and skill directory
|
|
108
|
+
* the selected agents produced.
|
|
109
|
+
*
|
|
110
|
+
* `fileHashes` is upstream's record, threaded through so `writeInitMeta`
|
|
111
|
+
* persists these entries alongside its own; a stored hash is what lets a later
|
|
112
|
+
* run tell "the user edited our block" from "the template moved on".
|
|
113
|
+
*/
|
|
114
|
+
export async function writeForkInstructions(root, latDir, fileHashes, ask) {
|
|
115
|
+
const body = readForkConventions();
|
|
116
|
+
const bodyHash = contentHash(body);
|
|
117
|
+
const written = [];
|
|
118
|
+
for (const relPath of BLOCK_TARGETS) {
|
|
119
|
+
const absPath = join(root, relPath);
|
|
120
|
+
if (!existsSync(absPath))
|
|
121
|
+
continue;
|
|
122
|
+
const current = readFileSync(absPath, 'utf-8');
|
|
123
|
+
const existingBlock = extractForkBlock(current);
|
|
124
|
+
if (existingBlock !== null) {
|
|
125
|
+
const existingHash = contentHash(existingBlock);
|
|
126
|
+
if (existingHash === bodyHash) {
|
|
127
|
+
fileHashes[forkHashKey(relPath)] = bodyHash;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const storedHash = readFileHash(latDir, forkHashKey(relPath));
|
|
131
|
+
if (storedHash && existingHash !== storedHash) {
|
|
132
|
+
console.log(styleText('yellow', ` ${relPath}`) +
|
|
133
|
+
' lat-fork section has been modified.');
|
|
134
|
+
if (!(await ask(' Replace lat-fork section with latest template?'))) {
|
|
135
|
+
console.log(styleText('dim', ' Kept existing section.'));
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const updated = spliceForkBlock(current, body);
|
|
141
|
+
if (updated === null) {
|
|
142
|
+
fileHashes[forkHashKey(relPath)] = bodyHash;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
writeFileSync(absPath, updated);
|
|
146
|
+
fileHashes[forkHashKey(relPath)] = bodyHash;
|
|
147
|
+
written.push(relPath);
|
|
148
|
+
}
|
|
149
|
+
const skillBody = SKILL_FRONTMATTER + body;
|
|
150
|
+
const skillHash = contentHash(skillBody);
|
|
151
|
+
for (const skillRoot of SKILL_ROOTS) {
|
|
152
|
+
const rootAbs = join(root, skillRoot);
|
|
153
|
+
if (!existsSync(rootAbs))
|
|
154
|
+
continue;
|
|
155
|
+
const relPath = `${skillRoot}/${SKILL_NAME}/SKILL.md`;
|
|
156
|
+
const absPath = join(root, relPath);
|
|
157
|
+
if (existsSync(absPath)) {
|
|
158
|
+
const current = readFileSync(absPath, 'utf-8');
|
|
159
|
+
if (contentHash(current) === skillHash) {
|
|
160
|
+
fileHashes[relPath] = skillHash;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const storedHash = readFileHash(latDir, relPath);
|
|
164
|
+
if (storedHash && contentHash(current) !== storedHash) {
|
|
165
|
+
console.log(styleText('yellow', ` ${relPath}`) + ' has been modified.');
|
|
166
|
+
if (!(await ask(' Overwrite with latest lat template?'))) {
|
|
167
|
+
console.log(styleText('dim', ' Kept existing file.'));
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
mkdirSync(join(absPath, '..'), { recursive: true });
|
|
173
|
+
writeFileSync(absPath, skillBody);
|
|
174
|
+
fileHashes[relPath] = skillHash;
|
|
175
|
+
written.push(relPath);
|
|
176
|
+
}
|
|
177
|
+
if (written.length > 0) {
|
|
178
|
+
console.log('');
|
|
179
|
+
console.log(styleText('green', ' lat.md conventions') +
|
|
180
|
+
' written to ' +
|
|
181
|
+
written.join(', '));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/** What the root index says about the gate today. */
|
|
2
|
+
export type RequireModeState = 'on' | 'off' | 'unset' | 'invalid';
|
|
3
|
+
/**
|
|
4
|
+
* Read the gate straight from a root index's frontmatter.
|
|
5
|
+
*
|
|
6
|
+
* Four states rather than a boolean, because `checkMode` enforces on `=== true`
|
|
7
|
+
* and anything looser here disagrees with it. A root index reading
|
|
8
|
+
* `require-mode: yes` is a string to this parser: treating that as "set" left
|
|
9
|
+
* the user believing the gate was on, `checkMode` enforcing nothing, and the
|
|
10
|
+
* one prompt that would have said so permanently silenced by the same value.
|
|
11
|
+
* `invalid` is reported rather than assumed either way.
|
|
12
|
+
*/
|
|
13
|
+
export declare function requireModeState(content: string): RequireModeState;
|
|
14
|
+
/** What `planRequireMode` decided, before anything is written or asked. */
|
|
15
|
+
export type StampPlan =
|
|
16
|
+
/** Already `true`; nothing to do. */
|
|
17
|
+
{
|
|
18
|
+
kind: 'on';
|
|
19
|
+
}
|
|
20
|
+
/** Explicitly `false`. A deliberate opt-out is not re-offered. */
|
|
21
|
+
| {
|
|
22
|
+
kind: 'off';
|
|
23
|
+
}
|
|
24
|
+
/** The edited document, ready to write. */
|
|
25
|
+
| {
|
|
26
|
+
kind: 'stamped';
|
|
27
|
+
content: string;
|
|
28
|
+
movedRootKey: boolean;
|
|
29
|
+
}
|
|
30
|
+
/** Present but neither `true` nor `false`. */
|
|
31
|
+
| {
|
|
32
|
+
kind: 'invalid';
|
|
33
|
+
detail: string;
|
|
34
|
+
}
|
|
35
|
+
/** The frontmatter does not parse, so no field on it is read at all. */
|
|
36
|
+
| {
|
|
37
|
+
kind: 'unparseable';
|
|
38
|
+
detail: string;
|
|
39
|
+
}
|
|
40
|
+
/** `lat:` holds something that cannot carry a mapping key. */
|
|
41
|
+
| {
|
|
42
|
+
kind: 'unsupported';
|
|
43
|
+
detail: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Decide what turning the gate on in this document would do, without doing it.
|
|
47
|
+
*
|
|
48
|
+
* Parsed and re-emitted through the YAML document API rather than edited line
|
|
49
|
+
* by line. Line surgery looked cheaper — it preserves the file byte for byte
|
|
50
|
+
* except where it inserts — but it has to answer, from raw text, every question
|
|
51
|
+
* the parser already answers: which line opens the block, which lines are the
|
|
52
|
+
* `lat:` mapping's own children rather than a later key's, how far they are
|
|
53
|
+
* indented, and whether the value on the `lat:` line is a flow mapping, an
|
|
54
|
+
* anchor or a block scalar. Each wrong answer was its own defect: a flow
|
|
55
|
+
* mapping and an anchor were refused although both merge cleanly, an empty
|
|
56
|
+
* `lat:` beside a sibling list was refused because the scan walked past the end
|
|
57
|
+
* of the block, and one shape produced frontmatter that no longer parsed at
|
|
58
|
+
* all. `parseDocument` answers all of it, and round-trips comments and blank
|
|
59
|
+
* lines while doing so.
|
|
60
|
+
*
|
|
61
|
+
* The one thing the document API does not give for free is that the fields
|
|
62
|
+
* already there survive, so that is asserted below rather than assumed.
|
|
63
|
+
*/
|
|
64
|
+
export declare function planRequireMode(content: string): StampPlan;
|
|
65
|
+
/** The edited document when the gate can be turned on, the input when not. */
|
|
66
|
+
export declare function stampRequireMode(content: string): string;
|
|
67
|
+
/**
|
|
68
|
+
* Append the four mode directories to the root index.
|
|
69
|
+
*
|
|
70
|
+
* Without this a fresh `lat init` leaves `lat check index` reporting four
|
|
71
|
+
* missing entries, which is a poor first impression of a tool whose whole
|
|
72
|
+
* pitch is that the check passes. Each directory is tested for separately: an
|
|
73
|
+
* index that already links `reference/` used to suppress the other three, so
|
|
74
|
+
* the three unlinked directories this had just created turned a passing
|
|
75
|
+
* `lat check index` into a failing one.
|
|
76
|
+
*/
|
|
77
|
+
export declare function listModeDirs(content: string): string;
|
|
78
|
+
/**
|
|
79
|
+
* Scaffold the four mode directories and turn the gate on.
|
|
80
|
+
*
|
|
81
|
+
* Nothing at all happens when the gate cannot land in the root index. A tree
|
|
82
|
+
* left with four new directories and the gate still off is worse than one left
|
|
83
|
+
* alone: `checkMode` passes on it, so nothing ever says the adoption stopped
|
|
84
|
+
* half way — and if the index was not rewritten to list them, `lat check index`
|
|
85
|
+
* fails instead, on four directories the user never asked for.
|
|
86
|
+
*/
|
|
87
|
+
export declare function writeForkScaffold(latDir: string): void;
|
|
88
|
+
/**
|
|
89
|
+
* Offer the gate to a tree `lat init` did not create.
|
|
90
|
+
*
|
|
91
|
+
* `writeForkScaffold` runs only on the branch that creates `lat.md/`, which
|
|
92
|
+
* left the flag unreachable for every project that already had one — that is,
|
|
93
|
+
* for every project that needs it. Restructuring someone's tree without asking
|
|
94
|
+
* is still wrong, so this asks.
|
|
95
|
+
*
|
|
96
|
+
* Whether the flag *can* be written is decided before anything is asked and
|
|
97
|
+
* before anything is written. Asking first cost a tree four directories and a
|
|
98
|
+
* rewritten index in exchange for a gate that never landed, and the user was
|
|
99
|
+
* told in the same breath that nothing could be edited.
|
|
100
|
+
*
|
|
101
|
+
* Silent when the flag is already set either way, when a previous run was told
|
|
102
|
+
* no, and when there is no TTY to ask — the non-interactive path prints the
|
|
103
|
+
* count and the manual edit instead, and records nothing, so a later
|
|
104
|
+
* interactive run still offers.
|
|
105
|
+
*/
|
|
106
|
+
export declare function offerRequireMode(latDir: string, interactive: boolean, ask: (message: string) => Promise<boolean>): Promise<void>;
|