dsh-taskbridge 1.7.0 → 1.8.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 (75) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +21 -21
  3. package/README.md +150 -289
  4. package/THIRD_PARTY_NOTICES.txt +23 -23
  5. package/antigravity/agents/dsh-taskbridge.md +31 -0
  6. package/antigravity/skills/dsh-taskbridge/SKILL.md +51 -0
  7. package/codex/dsh-taskbridge.toml +170 -627
  8. package/cordis.patch.yml +6 -6
  9. package/docs/INTRODUCTION.md +119 -295
  10. package/docs/INTRODUCTION.zh.md +134 -279
  11. package/docs/antigravity-integration.md +42 -0
  12. package/docs/auth-contract.md +138 -138
  13. package/docs/claude-code.md +6 -11
  14. package/docs/codex-integration.md +13 -2
  15. package/docs/contracts-stage-b.md +443 -443
  16. package/docs/plugin-market.md +30 -30
  17. package/docs/redaction-contract.md +170 -170
  18. package/docs/releases/1.7.0.zh.md +11 -11
  19. package/docs/releases/1.8.0.zh.md +16 -0
  20. package/docs/runtime-compatibility.md +1 -1
  21. package/docs/taskbridge-upgrade.md +28 -28
  22. package/docs/taskdispatch-upgrade.md +25 -25
  23. package/docs/workspace-contract.md +538 -538
  24. package/docs/workspace-grouping.md +126 -126
  25. package/icon.svg +1 -1
  26. package/lib/client.d.ts +733 -699
  27. package/lib/client.js +7 -7
  28. package/locale/en.json +1 -1
  29. package/locale/zh.json +1 -1
  30. package/package.json +149 -144
  31. package/src/antigravity/installer.js +1046 -0
  32. package/src/antigravity/seam.js +142 -0
  33. package/src/auth/fence.js +361 -361
  34. package/src/auth/grants.js +789 -789
  35. package/src/client/api.js +143 -0
  36. package/src/client/index.jsx +113 -3
  37. package/src/client/sidebar.jsx +862 -862
  38. package/src/client/styles.js +624 -624
  39. package/src/client/task-cleanup.js +304 -304
  40. package/src/client/task-records-table.jsx +217 -217
  41. package/src/client/task-view.js +307 -307
  42. package/src/client/workspace-groups.js +482 -482
  43. package/src/client/workspaces-browser.jsx +770 -770
  44. package/src/codex/agent-ownership.js +270 -270
  45. package/src/codex/installer.js +6 -0
  46. package/src/codex/toml.js +784 -784
  47. package/src/config/redact.js +245 -245
  48. package/src/config/store.js +186 -186
  49. package/src/credentials/seam.js +202 -202
  50. package/src/http/respond.js +6 -5
  51. package/src/http/user-plane.js +72 -0
  52. package/src/index.js +25 -0
  53. package/src/jobs/diagnostics.js +15 -15
  54. package/src/jobs/plan-store.js +1020 -1020
  55. package/src/jobs/review-note.js +63 -63
  56. package/src/jobs/task-descriptions.js +82 -82
  57. package/src/jobs/task-display.js +73 -73
  58. package/src/jobs/task-tree.js +786 -786
  59. package/src/jobs/title.js +134 -134
  60. package/src/mcp/continuation.js +317 -317
  61. package/src/mcp/server.js +12 -5
  62. package/src/model/connectivity.js +371 -371
  63. package/src/model/discovery.js +582 -582
  64. package/src/model/native-catalog.js +68 -68
  65. package/src/model/openai-adapter.js +671 -671
  66. package/src/model/probe.js +264 -264
  67. package/src/model/providers.js +756 -756
  68. package/src/model/routing.js +321 -321
  69. package/src/runtime/permissions.js +1619 -1619
  70. package/src/runtime/question-call.js +162 -162
  71. package/src/runtime/questions.js +909 -909
  72. package/src/runtime/worker-prompt.js +60 -53
  73. package/src/workspace/audit.js +775 -775
  74. package/src/workspace/preflight.js +424 -424
  75. package/src/workspace/worktrees.js +679 -679
@@ -1,270 +1,270 @@
1
- // The Codex agent-ownership record: ONE small JSON file per real Codex home, holding the
2
- // LF-normalized SHA-256 of the agent template this plugin last proved it wrote there.
3
- //
4
- // Why it exists
5
- // -------------
6
- // Ownership of `$CODEX_HOME/agents/dsh-taskbridge.toml` is decided by CONTENT, never by the
7
- // file name, a marker or a version line (see `src/codex/installer.js`). A template whose byte
8
- // form was not enumerated in `KNOWN_AGENT_TEMPLATES` therefore looked like "somebody else's
9
- // file" after an upgrade, and the plugin refused to refresh a file it had written itself.
10
- // Enumerating every future release by hand does not scale, so this record answers the OTHER
11
- // half of the question — "is this reachable file the current release's template?" — without
12
- // either a per-release digest table or a heuristic on names, markers or version lines.
13
- //
14
- // What it is
15
- // ----------
16
- // `<plugin data dir>/codex-agent-ownership/<sha256 of the real home>.json`:
17
- //
18
- // { "schemaVersion": 1, "codexHome": "<real home>", "templateLfSha256": "<64 hex>" }
19
- //
20
- // * keyed by the REAL home this plugin pinned, never by a spelling of it: the file name is a
21
- // digest of that path, so two different homes can never collide even on a case-sensitive
22
- // filesystem, and no case equivalence is ever inferred;
23
- // * ONE digest, LF-normalized: CRLF and LF spellings of the same text agree, and nothing else
24
- // is normalized — an edited character, an added line, a tab or a rewritten `version` line all
25
- // change the digest. The template body and the record hold no credentials and no config.
26
- //
27
- // Reading is bounded and non-creating: an absolute `DSH_HOME` or the existing default only, a
28
- // regular file of bounded size, and every ancestor proved to be a REAL directory (a link or a
29
- // reparse point anywhere on the way is "no record" — such a directory is never created here).
30
- // A missing, unreadable, malformed, differently-keyed or over-sized record is NO record: the
31
- // file is then classified exactly as before, which is what keeps an operator's file theirs.
32
-
33
- import { createHash } from 'node:crypto';
34
- import { lstat, readFile } from 'node:fs/promises';
35
- import { homedir } from 'node:os';
36
- import { dirname, isAbsolute, join, resolve } from 'node:path';
37
-
38
- /** Schema version of the record this module writes. */
39
- export const OWNERSHIP_SCHEMA_VERSION = 1;
40
-
41
- /** Directory, inside the plugin data directory, that holds one file per Codex home. */
42
- export const OWNERSHIP_DIR_NAME = 'codex-agent-ownership';
43
-
44
- /** A digest: lowercase hex SHA-256. */
45
- const SHA256 = /^[0-9a-f]{64}$/u;
46
-
47
- /** Bound on a record file this module is willing to parse (~1 KB, never content). */
48
- const MAX_RECORD_BYTES = 4096;
49
-
50
- /**
51
- * The plugin's own data directory: `$DSH_HOME/data/dsh-delegate`.
52
- *
53
- * An ABSOLUTE `DSH_HOME` is the only accepted override — a relative one would resolve against
54
- * the harness working directory and let the record land somewhere nobody named. Without one the
55
- * documented `~/.dsh` default applies, exactly as for the installation record.
56
- * @param {Record<string, string | undefined>} [env] - environment (`DSH_HOME`).
57
- * @returns {string | null} absolute plugin data directory, or null when `DSH_HOME` is unusable.
58
- */
59
- export function pluginDataDirectory(env = process.env) {
60
- const configured = typeof env.DSH_HOME === 'string' ? env.DSH_HOME.trim() : '';
61
- if (configured === '') return join(resolve(homedir()), '.dsh', 'data', 'dsh-delegate');
62
- if (!isAbsolute(configured)) return null;
63
- return join(resolve(configured), 'data', 'dsh-delegate');
64
- }
65
-
66
- /**
67
- * The record path for one Codex home.
68
- *
69
- * The file name is the digest of the REAL home path, so it identifies a directory rather than
70
- * reproducing its spelling — no case folding, no separator guessing, no collision between two
71
- * homes that differ only in case on a case-sensitive filesystem.
72
- * @param {string} codexHome - the REAL (pinned) Codex home.
73
- * @param {Record<string, string | undefined>} [env] - environment (`DSH_HOME`).
74
- * @returns {string | null} absolute record path, or null when the data directory is unusable.
75
- */
76
- export function ownershipRecordPath(codexHome, env = process.env) {
77
- const dataDir = pluginDataDirectory(env);
78
- if (dataDir === null) return null;
79
- if (typeof codexHome !== 'string' || codexHome.trim() === '' || !isAbsolute(codexHome)) return null;
80
- const key = createHash('sha256').update(resolve(codexHome), 'utf8').digest('hex');
81
- return join(dataDir, OWNERSHIP_DIR_NAME, `${key}.json`);
82
- }
83
-
84
- /**
85
- * Check that every existing ancestor of `target` is a REAL directory.
86
- *
87
- * A link, a junction/reparse point that `lstat` reports as a directory, an unreadable level or a
88
- * non-directory refuses the whole path. A missing level is reported as `missing` instead of
89
- * being created, so a READ can never build the data tree it is inspecting — the writer decides
90
- * whether to create it. The components come from the host's own path rules, so a POSIX `/`, a
91
- * Windows drive and a UNC share all keep their root.
92
- * @param {string} target - absolute path to inspect (the file itself is not inspected).
93
- * @param {Function} classify - the installer's own boundary proof, bound to `lstat` and the
94
- * platform attribute read. Injected so this module never grows a second, weaker copy of it.
95
- * @returns {Promise<{ ok: boolean, missing: boolean, reason: string }>} verdict.
96
- */
97
- async function inspectRealChain(target, classify) {
98
- const absolute = resolve(target);
99
- const root = rootOf(absolute);
100
- let current = root;
101
- if ((await classify(root)) !== 'directory') return { ok: false, missing: false, reason: 'ancestor-not-a-directory' };
102
- for (const part of absolute.slice(root.length).split(/[\\/]+/u).filter((part) => part !== '')) {
103
- current = join(current, part);
104
- const kind = await classify(current);
105
- if (kind === 'directory') continue;
106
- if (kind === 'missing') return { ok: false, missing: true, reason: 'ancestor-missing' };
107
- if (kind === 'link') return { ok: false, missing: false, reason: 'ancestor-is-a-link' };
108
- return { ok: false, missing: false, reason: 'ancestor-not-a-directory' };
109
- }
110
- return { ok: true, missing: false, reason: 'ok' };
111
- }
112
-
113
- /**
114
- * The volume root of an absolute path, using the host's own rules.
115
- * @param {string} absolute - resolved absolute path.
116
- * @returns {string} the root (`/`, `C:\` or `\\host\share\`).
117
- */
118
- function rootOf(absolute) {
119
- const drive = /^[A-Za-z]:[\\/]/u.exec(absolute);
120
- // The separator is part of the root: without it `join('C:', 'Users')` yields `C:Users`, a
121
- // RELATIVE path, and the walk would leave the tree it is supposed to prove.
122
- if (drive !== null) return `${drive[0].slice(0, 2)}${drive[0][2] === '/' ? '/' : '\\'}`;
123
- const unc = /^[\\/]{2}[^\\/]+[\\/][^\\/]+[\\/]/u.exec(absolute);
124
- if (unc !== null) return unc[0];
125
- return /^[\\/]/u.test(absolute) ? absolute[0] : '';
126
- }
127
-
128
- /**
129
- * The template digest this plugin last proved it wrote into one Codex home, or null.
130
- *
131
- * Returns null — "no record" — for every case that cannot be proved: an unusable data directory,
132
- * a path that does not exist yet, a link anywhere on the way, a file that is not a regular,
133
- * bounded, exactly-keyed record of this home, or any I/O failure. Null only ever means the file
134
- * on disk is classified by its content alone, never that something was adopted.
135
- * @param {object} options - `codexHome` (real, pinned), `env`, `classify` (see {@link inspectRealChain}).
136
- * @returns {Promise<string | null>} LF-normalized template digest, or null.
137
- */
138
- export async function recallTemplateDigest({ codexHome, env = process.env, classify } = {}) {
139
- const path = ownershipRecordPath(codexHome, env);
140
- if (path === null) return null;
141
- if (typeof classify !== 'function') return null;
142
- const chain = await inspectRealChain(dirname(path), classify);
143
- if (chain.ok !== true) return null;
144
- const info = await lstat(path).catch(() => null);
145
- // A junction is a directory to `lstat`, and a symlink is not a regular file: both are refused.
146
- if (info === null || info.isSymbolicLink() || !info.isFile()) return null;
147
- if (info.size > MAX_RECORD_BYTES) return null;
148
- let parsed;
149
- try {
150
- parsed = JSON.parse(await readFile(path, 'utf8'));
151
- } catch {
152
- return null;
153
- }
154
- if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return null;
155
- if (parsed.schemaVersion !== OWNERSHIP_SCHEMA_VERSION) return null;
156
- // The interior must name THIS home. A record copied from another home (or hand-edited to a
157
- // different path) therefore authorizes nothing, whatever its file name says.
158
- if (typeof parsed.codexHome !== 'string' || resolve(parsed.codexHome) !== resolve(codexHome)) return null;
159
- if (typeof parsed.templateLfSha256 !== 'string' || !SHA256.test(parsed.templateLfSha256)) return null;
160
- return parsed.templateLfSha256;
161
- }
162
-
163
- /**
164
- * Record the digest of the template this plugin has just proved it wrote into one home.
165
- *
166
- * The digest is recomputed from the CURRENT file, which the caller has already proved is exactly
167
- * the template this build renders — never from a value the caller remembered. An unchanged
168
- * record is not rewritten (no write, no mtime change). An unusable or unproved path writes
169
- * nothing.
170
- * @param {object} options - `codexHome` (real, pinned), `env`, `template` (the rendered text
171
- * the file must equal), `readFile` (`(path) => Promise<string>`, injected for the installer's
172
- * own compare-before-write step), `writeFileAtomic`, `classify`.
173
- * @returns {Promise<{ ok: true, written: boolean } | { ok: false, reason: string }>} outcome.
174
- */
175
- export async function rememberTemplateDigest({
176
- codexHome,
177
- env = process.env,
178
- template,
179
- agentPath,
180
- readFile: read = readFile,
181
- writeFileAtomic,
182
- mkdir: makeDirectory,
183
- classify,
184
- } = {}) {
185
- const path = ownershipRecordPath(codexHome, env);
186
- if (path === null) return { ok: false, reason: 'record-path-unusable' };
187
- if (typeof template !== 'string' || template === '') return { ok: false, reason: 'no-template' };
188
- if (typeof classify !== 'function') return { ok: false, reason: 'no-classifier' };
189
- if (typeof writeFileAtomic !== 'function' || typeof makeDirectory !== 'function') {
190
- return { ok: false, reason: 'no-writer' };
191
- }
192
- // Re-read the file NOW. If it is not exactly what this build renders, nothing is recorded:
193
- // the proof must come from the bytes on disk, not from the caller's intention.
194
- let current;
195
- try {
196
- current = await read(agentPath);
197
- } catch {
198
- return { ok: false, reason: 'agent-unreadable' };
199
- }
200
- if (current !== template) return { ok: false, reason: 'agent-not-this-template' };
201
-
202
- const chain = await inspectRealChain(dirname(path), classify);
203
- if (chain.ok !== true) {
204
- if (chain.reason !== 'ancestor-missing') return { ok: false, reason: chain.reason };
205
- // The record directory does not exist yet: create it one level at a time and re-prove.
206
- if (!(await createArchiveDirectory(dirname(path), classify, makeDirectory))) {
207
- return { ok: false, reason: 'record-directory-unusable' };
208
- }
209
- }
210
- const document = `${JSON.stringify({
211
- schemaVersion: OWNERSHIP_SCHEMA_VERSION,
212
- codexHome: resolve(codexHome),
213
- templateLfSha256: digestLf(template),
214
- })}\n`;
215
- const existing = await read(path).catch(() => null);
216
- if (existing === document) return { ok: true, written: false };
217
- try {
218
- await writeFileAtomic(path, document);
219
- } catch {
220
- return { ok: false, reason: 'record-write-failed' };
221
- }
222
- return { ok: true, written: true };
223
- }
224
-
225
- /**
226
- * Create the record directory (and any missing level of it), proving each one after creating it.
227
- *
228
- * Never recursive: a recursive create would walk through a link that appeared in the window.
229
- * @param {string} directory - absolute directory to create.
230
- * @param {Function} classify - boundary proof.
231
- * @param {Function} makeDirectory - `mkdir(path, { mode })`.
232
- * @returns {Promise<boolean>} whether a real directory now exists there.
233
- */
234
- async function createArchiveDirectory(directory, classify, makeDirectory) {
235
- const absolute = resolve(directory);
236
- const root = rootOf(absolute);
237
- let current = root;
238
- // The ROOT itself is a boundary, not a level to create: `C:\` or `/` cannot be a link this
239
- // module may create, but a reparse point at the volume root is refused like any other.
240
- if ((await classify(root)) !== 'directory') return false;
241
- for (const part of absolute.slice(root.length).split(/[\\/]+/u).filter((part) => part !== '')) {
242
- current = join(current, part);
243
- const kind = await classify(current);
244
- if (kind === 'directory') continue;
245
- if (kind !== 'missing') return false;
246
- try {
247
- await makeDirectory(current, { mode: 0o700 });
248
- } catch (error) {
249
- if (error?.code !== 'EEXIST') return false;
250
- }
251
- if ((await classify(current)) !== 'directory') return false;
252
- }
253
- return true;
254
- }
255
-
256
- /**
257
- * The LF-normalized SHA-256 of an agent text.
258
- *
259
- * Folding CRLF to LF is the ONLY tolerated difference, exactly as `isKnownPriorTemplate` treats
260
- * the published identities: the shipped template is stored with CRLF while the managed file is
261
- * written with LF, so the terminator spelling is not a content difference. Nothing else is
262
- * normalized — a version line, a comment, a blank line, a tab, a trailing space or a BOM all
263
- * survive and change the digest. A `\r` left after the fold is a character of the file, not a
264
- * spelling, and is therefore hashed as it is.
265
- * @param {string} text - agent file or template text.
266
- * @returns {string} lowercase hex SHA-256.
267
- */
268
- export function digestLf(text) {
269
- return createHash('sha256').update(String(text).replace(/\r\n/gu, '\n'), 'utf8').digest('hex');
270
- }
1
+ // The Codex agent-ownership record: ONE small JSON file per real Codex home, holding the
2
+ // LF-normalized SHA-256 of the agent template this plugin last proved it wrote there.
3
+ //
4
+ // Why it exists
5
+ // -------------
6
+ // Ownership of `$CODEX_HOME/agents/dsh-taskbridge.toml` is decided by CONTENT, never by the
7
+ // file name, a marker or a version line (see `src/codex/installer.js`). A template whose byte
8
+ // form was not enumerated in `KNOWN_AGENT_TEMPLATES` therefore looked like "somebody else's
9
+ // file" after an upgrade, and the plugin refused to refresh a file it had written itself.
10
+ // Enumerating every future release by hand does not scale, so this record answers the OTHER
11
+ // half of the question — "is this reachable file the current release's template?" — without
12
+ // either a per-release digest table or a heuristic on names, markers or version lines.
13
+ //
14
+ // What it is
15
+ // ----------
16
+ // `<plugin data dir>/codex-agent-ownership/<sha256 of the real home>.json`:
17
+ //
18
+ // { "schemaVersion": 1, "codexHome": "<real home>", "templateLfSha256": "<64 hex>" }
19
+ //
20
+ // * keyed by the REAL home this plugin pinned, never by a spelling of it: the file name is a
21
+ // digest of that path, so two different homes can never collide even on a case-sensitive
22
+ // filesystem, and no case equivalence is ever inferred;
23
+ // * ONE digest, LF-normalized: CRLF and LF spellings of the same text agree, and nothing else
24
+ // is normalized — an edited character, an added line, a tab or a rewritten `version` line all
25
+ // change the digest. The template body and the record hold no credentials and no config.
26
+ //
27
+ // Reading is bounded and non-creating: an absolute `DSH_HOME` or the existing default only, a
28
+ // regular file of bounded size, and every ancestor proved to be a REAL directory (a link or a
29
+ // reparse point anywhere on the way is "no record" — such a directory is never created here).
30
+ // A missing, unreadable, malformed, differently-keyed or over-sized record is NO record: the
31
+ // file is then classified exactly as before, which is what keeps an operator's file theirs.
32
+
33
+ import { createHash } from 'node:crypto';
34
+ import { lstat, readFile } from 'node:fs/promises';
35
+ import { homedir } from 'node:os';
36
+ import { dirname, isAbsolute, join, resolve } from 'node:path';
37
+
38
+ /** Schema version of the record this module writes. */
39
+ export const OWNERSHIP_SCHEMA_VERSION = 1;
40
+
41
+ /** Directory, inside the plugin data directory, that holds one file per Codex home. */
42
+ export const OWNERSHIP_DIR_NAME = 'codex-agent-ownership';
43
+
44
+ /** A digest: lowercase hex SHA-256. */
45
+ const SHA256 = /^[0-9a-f]{64}$/u;
46
+
47
+ /** Bound on a record file this module is willing to parse (~1 KB, never content). */
48
+ const MAX_RECORD_BYTES = 4096;
49
+
50
+ /**
51
+ * The plugin's own data directory: `$DSH_HOME/data/dsh-delegate`.
52
+ *
53
+ * An ABSOLUTE `DSH_HOME` is the only accepted override — a relative one would resolve against
54
+ * the harness working directory and let the record land somewhere nobody named. Without one the
55
+ * documented `~/.dsh` default applies, exactly as for the installation record.
56
+ * @param {Record<string, string | undefined>} [env] - environment (`DSH_HOME`).
57
+ * @returns {string | null} absolute plugin data directory, or null when `DSH_HOME` is unusable.
58
+ */
59
+ export function pluginDataDirectory(env = process.env) {
60
+ const configured = typeof env.DSH_HOME === 'string' ? env.DSH_HOME.trim() : '';
61
+ if (configured === '') return join(resolve(homedir()), '.dsh', 'data', 'dsh-delegate');
62
+ if (!isAbsolute(configured)) return null;
63
+ return join(resolve(configured), 'data', 'dsh-delegate');
64
+ }
65
+
66
+ /**
67
+ * The record path for one Codex home.
68
+ *
69
+ * The file name is the digest of the REAL home path, so it identifies a directory rather than
70
+ * reproducing its spelling — no case folding, no separator guessing, no collision between two
71
+ * homes that differ only in case on a case-sensitive filesystem.
72
+ * @param {string} codexHome - the REAL (pinned) Codex home.
73
+ * @param {Record<string, string | undefined>} [env] - environment (`DSH_HOME`).
74
+ * @returns {string | null} absolute record path, or null when the data directory is unusable.
75
+ */
76
+ export function ownershipRecordPath(codexHome, env = process.env) {
77
+ const dataDir = pluginDataDirectory(env);
78
+ if (dataDir === null) return null;
79
+ if (typeof codexHome !== 'string' || codexHome.trim() === '' || !isAbsolute(codexHome)) return null;
80
+ const key = createHash('sha256').update(resolve(codexHome), 'utf8').digest('hex');
81
+ return join(dataDir, OWNERSHIP_DIR_NAME, `${key}.json`);
82
+ }
83
+
84
+ /**
85
+ * Check that every existing ancestor of `target` is a REAL directory.
86
+ *
87
+ * A link, a junction/reparse point that `lstat` reports as a directory, an unreadable level or a
88
+ * non-directory refuses the whole path. A missing level is reported as `missing` instead of
89
+ * being created, so a READ can never build the data tree it is inspecting — the writer decides
90
+ * whether to create it. The components come from the host's own path rules, so a POSIX `/`, a
91
+ * Windows drive and a UNC share all keep their root.
92
+ * @param {string} target - absolute path to inspect (the file itself is not inspected).
93
+ * @param {Function} classify - the installer's own boundary proof, bound to `lstat` and the
94
+ * platform attribute read. Injected so this module never grows a second, weaker copy of it.
95
+ * @returns {Promise<{ ok: boolean, missing: boolean, reason: string }>} verdict.
96
+ */
97
+ async function inspectRealChain(target, classify) {
98
+ const absolute = resolve(target);
99
+ const root = rootOf(absolute);
100
+ let current = root;
101
+ if ((await classify(root)) !== 'directory') return { ok: false, missing: false, reason: 'ancestor-not-a-directory' };
102
+ for (const part of absolute.slice(root.length).split(/[\\/]+/u).filter((part) => part !== '')) {
103
+ current = join(current, part);
104
+ const kind = await classify(current);
105
+ if (kind === 'directory') continue;
106
+ if (kind === 'missing') return { ok: false, missing: true, reason: 'ancestor-missing' };
107
+ if (kind === 'link') return { ok: false, missing: false, reason: 'ancestor-is-a-link' };
108
+ return { ok: false, missing: false, reason: 'ancestor-not-a-directory' };
109
+ }
110
+ return { ok: true, missing: false, reason: 'ok' };
111
+ }
112
+
113
+ /**
114
+ * The volume root of an absolute path, using the host's own rules.
115
+ * @param {string} absolute - resolved absolute path.
116
+ * @returns {string} the root (`/`, `C:\` or `\\host\share\`).
117
+ */
118
+ function rootOf(absolute) {
119
+ const drive = /^[A-Za-z]:[\\/]/u.exec(absolute);
120
+ // The separator is part of the root: without it `join('C:', 'Users')` yields `C:Users`, a
121
+ // RELATIVE path, and the walk would leave the tree it is supposed to prove.
122
+ if (drive !== null) return `${drive[0].slice(0, 2)}${drive[0][2] === '/' ? '/' : '\\'}`;
123
+ const unc = /^[\\/]{2}[^\\/]+[\\/][^\\/]+[\\/]/u.exec(absolute);
124
+ if (unc !== null) return unc[0];
125
+ return /^[\\/]/u.test(absolute) ? absolute[0] : '';
126
+ }
127
+
128
+ /**
129
+ * The template digest this plugin last proved it wrote into one Codex home, or null.
130
+ *
131
+ * Returns null — "no record" — for every case that cannot be proved: an unusable data directory,
132
+ * a path that does not exist yet, a link anywhere on the way, a file that is not a regular,
133
+ * bounded, exactly-keyed record of this home, or any I/O failure. Null only ever means the file
134
+ * on disk is classified by its content alone, never that something was adopted.
135
+ * @param {object} options - `codexHome` (real, pinned), `env`, `classify` (see {@link inspectRealChain}).
136
+ * @returns {Promise<string | null>} LF-normalized template digest, or null.
137
+ */
138
+ export async function recallTemplateDigest({ codexHome, env = process.env, classify } = {}) {
139
+ const path = ownershipRecordPath(codexHome, env);
140
+ if (path === null) return null;
141
+ if (typeof classify !== 'function') return null;
142
+ const chain = await inspectRealChain(dirname(path), classify);
143
+ if (chain.ok !== true) return null;
144
+ const info = await lstat(path).catch(() => null);
145
+ // A junction is a directory to `lstat`, and a symlink is not a regular file: both are refused.
146
+ if (info === null || info.isSymbolicLink() || !info.isFile()) return null;
147
+ if (info.size > MAX_RECORD_BYTES) return null;
148
+ let parsed;
149
+ try {
150
+ parsed = JSON.parse(await readFile(path, 'utf8'));
151
+ } catch {
152
+ return null;
153
+ }
154
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return null;
155
+ if (parsed.schemaVersion !== OWNERSHIP_SCHEMA_VERSION) return null;
156
+ // The interior must name THIS home. A record copied from another home (or hand-edited to a
157
+ // different path) therefore authorizes nothing, whatever its file name says.
158
+ if (typeof parsed.codexHome !== 'string' || resolve(parsed.codexHome) !== resolve(codexHome)) return null;
159
+ if (typeof parsed.templateLfSha256 !== 'string' || !SHA256.test(parsed.templateLfSha256)) return null;
160
+ return parsed.templateLfSha256;
161
+ }
162
+
163
+ /**
164
+ * Record the digest of the template this plugin has just proved it wrote into one home.
165
+ *
166
+ * The digest is recomputed from the CURRENT file, which the caller has already proved is exactly
167
+ * the template this build renders — never from a value the caller remembered. An unchanged
168
+ * record is not rewritten (no write, no mtime change). An unusable or unproved path writes
169
+ * nothing.
170
+ * @param {object} options - `codexHome` (real, pinned), `env`, `template` (the rendered text
171
+ * the file must equal), `readFile` (`(path) => Promise<string>`, injected for the installer's
172
+ * own compare-before-write step), `writeFileAtomic`, `classify`.
173
+ * @returns {Promise<{ ok: true, written: boolean } | { ok: false, reason: string }>} outcome.
174
+ */
175
+ export async function rememberTemplateDigest({
176
+ codexHome,
177
+ env = process.env,
178
+ template,
179
+ agentPath,
180
+ readFile: read = readFile,
181
+ writeFileAtomic,
182
+ mkdir: makeDirectory,
183
+ classify,
184
+ } = {}) {
185
+ const path = ownershipRecordPath(codexHome, env);
186
+ if (path === null) return { ok: false, reason: 'record-path-unusable' };
187
+ if (typeof template !== 'string' || template === '') return { ok: false, reason: 'no-template' };
188
+ if (typeof classify !== 'function') return { ok: false, reason: 'no-classifier' };
189
+ if (typeof writeFileAtomic !== 'function' || typeof makeDirectory !== 'function') {
190
+ return { ok: false, reason: 'no-writer' };
191
+ }
192
+ // Re-read the file NOW. If it is not exactly what this build renders, nothing is recorded:
193
+ // the proof must come from the bytes on disk, not from the caller's intention.
194
+ let current;
195
+ try {
196
+ current = await read(agentPath);
197
+ } catch {
198
+ return { ok: false, reason: 'agent-unreadable' };
199
+ }
200
+ if (current !== template) return { ok: false, reason: 'agent-not-this-template' };
201
+
202
+ const chain = await inspectRealChain(dirname(path), classify);
203
+ if (chain.ok !== true) {
204
+ if (chain.reason !== 'ancestor-missing') return { ok: false, reason: chain.reason };
205
+ // The record directory does not exist yet: create it one level at a time and re-prove.
206
+ if (!(await createArchiveDirectory(dirname(path), classify, makeDirectory))) {
207
+ return { ok: false, reason: 'record-directory-unusable' };
208
+ }
209
+ }
210
+ const document = `${JSON.stringify({
211
+ schemaVersion: OWNERSHIP_SCHEMA_VERSION,
212
+ codexHome: resolve(codexHome),
213
+ templateLfSha256: digestLf(template),
214
+ })}\n`;
215
+ const existing = await read(path).catch(() => null);
216
+ if (existing === document) return { ok: true, written: false };
217
+ try {
218
+ await writeFileAtomic(path, document);
219
+ } catch {
220
+ return { ok: false, reason: 'record-write-failed' };
221
+ }
222
+ return { ok: true, written: true };
223
+ }
224
+
225
+ /**
226
+ * Create the record directory (and any missing level of it), proving each one after creating it.
227
+ *
228
+ * Never recursive: a recursive create would walk through a link that appeared in the window.
229
+ * @param {string} directory - absolute directory to create.
230
+ * @param {Function} classify - boundary proof.
231
+ * @param {Function} makeDirectory - `mkdir(path, { mode })`.
232
+ * @returns {Promise<boolean>} whether a real directory now exists there.
233
+ */
234
+ async function createArchiveDirectory(directory, classify, makeDirectory) {
235
+ const absolute = resolve(directory);
236
+ const root = rootOf(absolute);
237
+ let current = root;
238
+ // The ROOT itself is a boundary, not a level to create: `C:\` or `/` cannot be a link this
239
+ // module may create, but a reparse point at the volume root is refused like any other.
240
+ if ((await classify(root)) !== 'directory') return false;
241
+ for (const part of absolute.slice(root.length).split(/[\\/]+/u).filter((part) => part !== '')) {
242
+ current = join(current, part);
243
+ const kind = await classify(current);
244
+ if (kind === 'directory') continue;
245
+ if (kind !== 'missing') return false;
246
+ try {
247
+ await makeDirectory(current, { mode: 0o700 });
248
+ } catch (error) {
249
+ if (error?.code !== 'EEXIST') return false;
250
+ }
251
+ if ((await classify(current)) !== 'directory') return false;
252
+ }
253
+ return true;
254
+ }
255
+
256
+ /**
257
+ * The LF-normalized SHA-256 of an agent text.
258
+ *
259
+ * Folding CRLF to LF is the ONLY tolerated difference, exactly as `isKnownPriorTemplate` treats
260
+ * the published identities: the shipped template is stored with CRLF while the managed file is
261
+ * written with LF, so the terminator spelling is not a content difference. Nothing else is
262
+ * normalized — a version line, a comment, a blank line, a tab, a trailing space or a BOM all
263
+ * survive and change the digest. A `\r` left after the fold is a character of the file, not a
264
+ * spelling, and is therefore hashed as it is.
265
+ * @param {string} text - agent file or template text.
266
+ * @returns {string} lowercase hex SHA-256.
267
+ */
268
+ export function digestLf(text) {
269
+ return createHash('sha256').update(String(text).replace(/\r\n/gu, '\n'), 'utf8').digest('hex');
270
+ }
@@ -516,6 +516,12 @@ export const KNOWN_AGENT_TEMPLATES = [
516
516
  // character, an added line, or another body carrying the same version line hashes
517
517
  // differently and stays the operator's file.
518
518
  { version: '1.6.1-test.6', sha256: 'f4650d6250b4133f6693f11c8288e973b433bbfab02e76ebba0eda989594d7b8' },
519
+ // Published 1.7.0 companion before the implementation brief update, in LF and CRLF.
520
+ { version: '1.7.0', sha256: 'b40eedd04139c8c6cd0cbc89e1af008417f3210bc3afb889695a2a87c8fd1e82' },
521
+ { version: '1.7.0', sha256: 'ed3a2efd470e6e7f6e8495538cfa9951dec82878c717ecb84eba41fd52127dc2' },
522
+ // Published 1.7.1 companion before the 1.8.0 black-box simplification, in LF and CRLF.
523
+ { version: '1.7.1', sha256: '5a55b978c3de4f7cec8922d1af5302ef46b0bcbda867bfe53faebac1980eb1b3' },
524
+ { version: '1.7.1', sha256: '026157837e2a99c5a265badb2ca6dc77be1ddcac28871e5949b80e61c949bfa0' },
519
525
  ];
520
526
 
521
527
  /** The `version = "…"` line of an agent file, at column 0. */