dsh-taskbridge 1.5.0 → 1.5.2

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,278 +1,283 @@
1
- // Continuation identity: what a stored path may be READ as, and what may be
2
- // SAVED so it can be read back later.
3
- //
4
- // A `continueFromTaskId` follow-up is admitted only when two durable documents
5
- // agree on where the parent really ran. Those documents are written through
6
- // redaction, which masks a per-profile directory under a temporary root
7
- // (`config/redact.js`). A masked path proves nothing, so it is refused — which is
8
- // correct, but only for a path that was ACTUALLY masked. Redaction used to mask
9
- // ordinary repository names too (`dsh-delegate`, `dsh-taskbridge`), so a normally
10
- // run task could not be continued after a restart.
11
- //
12
- // This module is the single definition of the identity a durable path carries:
13
- //
14
- // * {@link canonicalPath} — the ONE spelling used to compare and to record. It is
15
- // the same rule the session layer keys a Workspace by (`sessionPathKey`), so the
16
- // two can never disagree.
17
- // * {@link pathDigest} — a `sha256:` digest of {@link canonicalPath}. It is the only
18
- // proof of "these two strings are the same directory" that survives redaction,
19
- // because a digest is opaque to every masking rule.
20
- // * {@link readPathIdentity} — what a STORED row proves: its canonical path when the
21
- // value is usable, plus the digest when one is stored. A masked value yields
22
- // neither, so it can only be refused.
23
- // * {@link sameRecordedDirectory} — the ONE comparison rule, including the
24
- // requirement that a row's own path and digest agree with each other.
25
- // * {@link continuationIdentityOf} — what is SAVED beside a task: the canonical
26
- // paths, their digests, and the Workspace/Session ids a follow-up is bound to.
27
-
28
- import { createHash } from 'node:crypto';
29
-
30
- /**
31
- * The placeholder redaction rewrites an account-identifying directory segment to.
32
- *
33
- * One segment of a stored path equal to this is a shortened spelling. It is never
34
- * a pattern and never equals anything, which is why the check is for the WHOLE
35
- * segment: a real directory whose name merely contains this text is still its own
36
- * directory.
37
- */
38
- export const PATH_MASK_SEGMENT = 'masks-user-profile-segment';
39
-
40
- /**
41
- * Whether a value cannot prove which directory it names because redaction rewrote it.
42
- * @param {unknown} value - candidate path.
43
- * @returns {boolean} true when the value was rewritten and can only be refused.
44
- */
45
- export function recordedPathIsMasked(value) {
46
- if (typeof value !== 'string') return false;
47
- return value
48
- .split(/[\\/]+/)
49
- .some((part) => part.trim() === PATH_MASK_SEGMENT);
50
- }
51
-
52
- /**
53
- * The canonical key of a directory, expressed exactly as `sessionPathKey` does.
54
- *
55
- * This module cannot import `runtime/session-placement.js` (that module imports THIS
56
- * one), so the identical rule is restated and a test asserts the two agree for every
57
- * spelling: same directory, both accept; different directory, both reject. Written
58
- * as: trim, strip trailing separators below the root, fold case on Windows.
59
- * @param {unknown} value - candidate directory path.
60
- * @returns {string | null} canonical key, or null.
61
- */
62
- export function sessionKeyOf(value) {
63
- if (typeof value !== 'string') return null;
64
- const trimmed = value.trim();
65
- if (trimmed === '') return null;
66
- const stripped = trimmed.length > 1 ? trimmed.replace(/[\\/]+$/u, '') : trimmed;
67
- return process.platform === 'win32' ? stripped.toLowerCase() : stripped;
68
- }
69
-
70
- /**
71
- * The one canonical spelling of a directory path.
72
- *
73
- * Built on {@link sessionKeyOf} — the same rule the session layer keys a Workspace by —
74
- * so this module does not invent a second spelling, and the two are cross-checked by a
75
- * test. On Windows the fold is over the WHOLE path, not just the drive letter: NTFS is
76
- * case-insensitive at every level, so `E:\repo\Src` and `E:\repo\src` are one
77
- * directory. Elsewhere the path keeps its case, because two POSIX paths differing only
78
- * in case really are two directories.
79
- *
80
- * Deliberately NOT done here: normalising `/` to `\`, or collapsing `..`. The session
81
- * layer does neither, and a normaliser that accepted more spellings than the layer
82
- * that reaches the Session would prove equality for two paths the harness treats as
83
- * different directories. Whatever `sessionPathKey` considers distinct stays distinct.
84
- * @param {unknown} value - candidate path.
85
- * @returns {string | null} canonical path, or null when the value is unusable.
86
- */
87
- export function canonicalPath(value) {
88
- if (typeof value !== 'string') return null;
89
- const trimmed = value.trim();
90
- // A masked path is NOT canonicalised: keying it would produce a plausible spelling
91
- // naming a directory nobody wrote, and a mask is never anything.
92
- if (trimmed === '' || recordedPathIsMasked(trimmed)) return null;
93
- return sessionKeyOf(trimmed);
94
- }
95
-
96
- /**
97
- * Whether two path values name the same directory under {@link canonicalPath}.
98
- * @param {unknown} a - first path.
99
- * @param {unknown} b - second path.
100
- * @returns {boolean} true when both are usable and canonicalise equal.
101
- */
102
- export function sameCanonicalPath(a, b) {
103
- const left = canonicalPath(a);
104
- const right = canonicalPath(b);
105
- return left !== null && right !== null && left === right;
106
- }
107
-
108
- /**
109
- * A stable digest of one canonical path.
110
- *
111
- * The digest is the identity a stored row keeps when the path STRING cannot be
112
- * trusted for comparison (a projection that rewrote it). It is opaque to text
113
- * redaction, so it survives the same projection the path did not.
114
- * @param {unknown} value - candidate path.
115
- * @returns {string | null} `sha256:<hex>` digest, or null when unusable.
116
- */
117
- export function pathDigest(value) {
118
- const canonical = canonicalPath(value);
119
- if (canonical === null) return null;
120
- return `sha256:${createHash('sha256').update(canonical, 'utf8').digest('hex')}`;
121
- }
122
-
123
- /**
124
- * Whether a stored value is a path digest this module would have produced.
125
- * @param {unknown} value - candidate digest.
126
- * @returns {boolean} true for a well-formed digest.
127
- */
128
- export function isPathDigest(value) {
129
- return typeof value === 'string' && /^sha256:[0-9a-f]{64}$/u.test(value);
130
- }
131
-
132
- /**
133
- * What one stored path field PROVES about the directory behind it.
134
- *
135
- * Two independent facts, and either may be enough:
136
- *
137
- * * `canonical` — the value is a readable path and survives redaction, so two
138
- * records carrying the same canonical value are the same directory;
139
- * * `digest` — a stored digest is well formed, so a comparison can be made on the
140
- * digest alone even when the path string was shortened.
141
- *
142
- * Both absent means the field proves nothing, and a caller must refuse. Nothing
143
- * here ever answers "any path", which is what a wildcard would be.
144
- * @param {unknown} value - stored path value.
145
- * @param {unknown} [storedDigest] - stored digest beside it, if the row has one.
146
- * @returns {{ canonical: string | null, digest: string | null, masked: boolean }} facts.
147
- */
148
- export function readPathIdentity(value, storedDigest = null) {
149
- const masked = recordedPathIsMasked(value);
150
- const canonical = masked ? null : canonicalPath(value);
151
- const digest = isPathDigest(storedDigest)
152
- ? storedDigest
153
- : (canonical === null ? null : pathDigest(canonical));
154
- return { canonical, digest, masked };
155
- }
156
-
157
- /**
158
- * Whether two stored path fields prove they name the SAME directory.
159
- *
160
- * The rule is an EQUALITY OF USABLE EVIDENCE, and every kind of evidence a record
161
- * carries must agree before it proves anything:
162
- *
163
- * 1. A record that states both a readable path and a digest must have them name the
164
- * same directory. A digest that contradicts its own path is a record that was
165
- * edited, and NOTHING it says is used — not the path and not the digest. An
166
- * earlier version let the readable path win such a case, which admitted
167
- * `'E:/repo'` recorded with `pathDigest('E:/other')`.
168
- * 2. Then the two records are compared. Two readable paths are compared exactly; a
169
- * masked side contributes only its digest, so a shortened path is never matched
170
- * against a string.
171
- * 3. A side with no readable path AND no digest proves nothing, so the answer is
172
- * `false` — an empty or masked value without a digest is a refusal, never a
173
- * wildcard.
174
- * @param {{ path?: unknown, digest?: unknown }} left - first record.
175
- * @param {{ path?: unknown, digest?: unknown }} right - second record.
176
- * @returns {boolean} true only when both sides prove the same directory.
177
- */
178
- export function sameRecordedDirectory(left, right) {
179
- const a = readPathIdentity(left?.path, left?.digest);
180
- const b = readPathIdentity(right?.path, right?.digest);
181
- // (1) Each side must be internally consistent before it may prove anything.
182
- if (!identityIsConsistent(a) || !identityIsConsistent(b)) return false;
183
- // (2) Both readable: an exact comparison of the canonical spellings.
184
- if (a.canonical !== null && b.canonical !== null) return a.canonical === b.canonical;
185
- // (3) At least one side is masked; both must then offer a digest, and those must match.
186
- if (a.digest !== null && b.digest !== null) return a.digest === b.digest;
187
- return false;
188
- }
189
-
190
- /**
191
- * Whether one record's own two proofs agree.
192
- *
193
- * A digest is derived from the readable path, so a well-formed digest beside a
194
- * readable path has exactly one permissible value. Anything else means the record is
195
- * inconsistent and must not be used as evidence at all.
196
- * @param {{ canonical: string | null, digest: string | null }} reading - one record.
197
- * @returns {boolean} true when the record is consistent (or states only one proof).
198
- */
199
- function identityIsConsistent(reading) {
200
- if (reading.canonical === null || reading.digest === null) return true;
201
- return reading.digest === pathDigest(reading.canonical);
202
- }
203
-
204
- /**
205
- * Whether a stored path field names THIS directory, from either kind of evidence.
206
- * @param {{ path?: unknown, digest?: unknown }} recorded - stored record facts.
207
- * @param {string} directory - the directory a submission resolved.
208
- * @returns {boolean} true only when the record proves it.
209
- */
210
- export function recordedDirectoryIs(recorded, directory) {
211
- return sameRecordedDirectory(recorded, { path: directory, digest: pathDigest(directory) });
212
- }
213
-
214
- /**
215
- * The identity SAVED for one task thread.
216
- *
217
- * Two facts per path, and they serve different purposes:
218
- *
219
- * * `sourcePath` / `executionPath` keep the operator's REAL spelling. This value can
220
- * be handed back to the runtime as a directory to open, so it must never be a
221
- * lower-cased comparison key.
222
- * * `sourcePathDigest` / `executionPathDigest` are digests of the CANONICAL key
223
- * ({@link canonicalPath}), which is what a later comparison is made on.
224
- *
225
- * Storing the canonical key in the path field would look like success and then fail
226
- * at the first real read on a case-sensitive filesystem. Only identity is written: no
227
- * prompt, transcript, output, usage figure or credential has a field.
228
- * @param {object} task - task record with `taskId`, `sourcePath`, `executionPath`.
229
- * @param {object | null} placement - the run's own placement (`workspaceId`, `sessionId`).
230
- * @param {string | null} isolation - the isolation the run used.
231
- * @returns {{ taskId: string, sourcePath: string | null, executionPath: string | null,
232
- * sourcePathDigest: string | null, executionPathDigest: string | null,
233
- * workspaceId: string, sessionId: string, isolation: string | null } | null} identity, or null.
234
- */
235
- export function continuationIdentityOf(task, placement, isolation) {
236
- const taskId = typeof task?.taskId === 'string' ? task.taskId.trim() : '';
237
- if (taskId === '') return null;
238
- const sourcePath = storedSpelling(task.sourcePath);
239
- const executionPath = storedSpelling(task.executionPath);
240
- return {
241
- taskId,
242
- sourcePath,
243
- executionPath,
244
- sourcePathDigest: sourcePath === null ? null : pathDigest(sourcePath),
245
- executionPathDigest: executionPath === null ? null : pathDigest(executionPath),
246
- workspaceId: typeof placement?.workspaceId === 'string' ? placement.workspaceId.trim() : '',
247
- sessionId: typeof placement?.sessionId === 'string' ? placement.sessionId.trim() : '',
248
- isolation: typeof isolation === 'string' && isolation !== '' ? isolation : null,
249
- };
250
- }
251
-
252
- /**
253
- * The spelling to WRITE for a path: the operator's own, trimmed, never a key.
254
- *
255
- * Returns null for a value that is not a usable path (empty, non-string, or already
256
- * masked), so a row cannot carry a mask as though it were a directory.
257
- * @param {unknown} value - candidate path.
258
- * @returns {string | null} the stored spelling, or null.
259
- */
260
- function storedSpelling(value) {
261
- if (typeof value !== 'string') return null;
262
- const trimmed = value.trim();
263
- if (trimmed === '' || recordedPathIsMasked(trimmed)) return null;
264
- return trimmed;
265
- }
266
-
267
- /**
268
- * The identity fields a durable history row carries.
269
- *
270
- * Kept as one list (rather than written out at the save site) so the history
271
- * projection, the engine and the tests name the same fields.
272
- */
273
- export const CONTINUATION_IDENTITY_FIELDS = Object.freeze([
274
- 'sourcePathDigest',
275
- 'executionPathDigest',
276
- 'workspaceId',
277
- 'sessionId',
278
- ]);
1
+ // Continuation identity: what a stored path may be READ as, and what may be
2
+ // SAVED so it can be read back later.
3
+ //
4
+ // A `continueFromTaskId` follow-up is admitted only when two durable documents
5
+ // agree on where the parent really ran. Those documents are written through
6
+ // redaction, which masks a per-profile directory under a temporary root
7
+ // (`config/redact.js`). A masked path proves nothing, so it is refused — which is
8
+ // correct, but only for a path that was ACTUALLY masked. Redaction used to mask
9
+ // ordinary repository names too (`dsh-delegate`, `dsh-taskbridge`), so a normally
10
+ // run task could not be continued after a restart.
11
+ //
12
+ // This module is the single definition of the identity a durable path carries:
13
+ //
14
+ // * {@link canonicalPath} — the ONE spelling used to compare and to record. It is
15
+ // the same rule the session layer keys a Workspace by (`sessionPathKey`), so the
16
+ // two can never disagree.
17
+ // * {@link pathDigest} — a `sha256:` digest of {@link canonicalPath}. It is the only
18
+ // proof of "these two strings are the same directory" that survives redaction,
19
+ // because a digest is opaque to every masking rule.
20
+ // * {@link readPathIdentity} — what a STORED row proves: its canonical path when the
21
+ // value is usable, plus the digest when one is stored. A masked value yields
22
+ // neither, so it can only be refused.
23
+ // * {@link sameRecordedDirectory} — the ONE comparison rule, including the
24
+ // requirement that a row's own path and digest agree with each other.
25
+ // * {@link continuationIdentityOf} — what is SAVED beside a task: the canonical
26
+ // paths, their digests, and the Workspace/Session ids a follow-up is bound to.
27
+
28
+ import { createHash } from 'node:crypto';
29
+
30
+ /**
31
+ * The placeholder redaction rewrites an account-identifying directory segment to.
32
+ *
33
+ * One segment of a stored path equal to this is a shortened spelling. It is never
34
+ * a pattern and never equals anything, which is why the check is for the WHOLE
35
+ * segment: a real directory whose name merely contains this text is still its own
36
+ * directory.
37
+ */
38
+ export const PATH_MASK_SEGMENT = 'masks-user-profile-segment';
39
+
40
+ /**
41
+ * Whether a value cannot prove which directory it names because redaction rewrote it.
42
+ * @param {unknown} value - candidate path.
43
+ * @returns {boolean} true when the value was rewritten and can only be refused.
44
+ */
45
+ export function recordedPathIsMasked(value) {
46
+ if (typeof value !== 'string') return false;
47
+ return value
48
+ .split(/[\\/]+/)
49
+ .some((part) => part.trim() === PATH_MASK_SEGMENT);
50
+ }
51
+
52
+ /**
53
+ * The canonical key of a directory, expressed exactly as `sessionPathKey` does.
54
+ *
55
+ * This module cannot import `runtime/session-placement.js` (that module imports THIS
56
+ * one), so the identical rule is restated and a test asserts the two agree for every
57
+ * spelling: same directory, both accept; different directory, both reject. Written
58
+ * as: trim, strip trailing separators below the root, fold case on Windows.
59
+ * @param {unknown} value - candidate directory path.
60
+ * @returns {string | null} canonical key, or null.
61
+ */
62
+ export function sessionKeyOf(value) {
63
+ if (typeof value !== 'string') return null;
64
+ const trimmed = value.trim();
65
+ if (trimmed === '') return null;
66
+ const stripped = trimmed.length > 1 ? trimmed.replace(/[\\/]+$/u, '') : trimmed;
67
+ return process.platform === 'win32' ? stripped.toLowerCase() : stripped;
68
+ }
69
+
70
+ /**
71
+ * The one canonical spelling of a directory path.
72
+ *
73
+ * Built on {@link sessionKeyOf} — the same rule the session layer keys a Workspace by —
74
+ * so this module does not invent a second spelling, and the two are cross-checked by a
75
+ * test. On Windows the fold is over the WHOLE path, not just the drive letter: NTFS is
76
+ * case-insensitive at every level, so `E:\repo\Src` and `E:\repo\src` are one
77
+ * directory. Elsewhere the path keeps its case, because two POSIX paths differing only
78
+ * in case really are two directories.
79
+ *
80
+ * Deliberately NOT done here: normalising `/` to `\`, or collapsing `..`. The session
81
+ * layer does neither, and a normaliser that accepted more spellings than the layer
82
+ * that reaches the Session would prove equality for two paths the harness treats as
83
+ * different directories. Whatever `sessionPathKey` considers distinct stays distinct.
84
+ * @param {unknown} value - candidate path.
85
+ * @returns {string | null} canonical path, or null when the value is unusable.
86
+ */
87
+ export function canonicalPath(value) {
88
+ if (typeof value !== 'string') return null;
89
+ const trimmed = value.trim();
90
+ // A masked path is NOT canonicalised: keying it would produce a plausible spelling
91
+ // naming a directory nobody wrote, and a mask is never anything.
92
+ if (trimmed === '' || recordedPathIsMasked(trimmed)) return null;
93
+ return sessionKeyOf(trimmed);
94
+ }
95
+
96
+ /**
97
+ * Whether two path values name the same directory under {@link canonicalPath}.
98
+ * @param {unknown} a - first path.
99
+ * @param {unknown} b - second path.
100
+ * @returns {boolean} true when both are usable and canonicalise equal.
101
+ */
102
+ export function sameCanonicalPath(a, b) {
103
+ const left = canonicalPath(a);
104
+ const right = canonicalPath(b);
105
+ return left !== null && right !== null && left === right;
106
+ }
107
+
108
+ /**
109
+ * A stable digest of one canonical path.
110
+ *
111
+ * The digest is the identity a stored row keeps when the path STRING cannot be
112
+ * trusted for comparison (a projection that rewrote it). It is opaque to text
113
+ * redaction, so it survives the same projection the path did not.
114
+ * @param {unknown} value - candidate path.
115
+ * @returns {string | null} `sha256:<hex>` digest, or null when unusable.
116
+ */
117
+ export function pathDigest(value) {
118
+ const canonical = canonicalPath(value);
119
+ if (canonical === null) return null;
120
+ return `sha256:${createHash('sha256').update(canonical, 'utf8').digest('hex')}`;
121
+ }
122
+
123
+ /**
124
+ * Whether a stored value is a path digest this module would have produced.
125
+ * @param {unknown} value - candidate digest.
126
+ * @returns {boolean} true for a well-formed digest.
127
+ */
128
+ export function isPathDigest(value) {
129
+ return typeof value === 'string' && /^sha256:[0-9a-f]{64}$/u.test(value);
130
+ }
131
+
132
+ /**
133
+ * What one stored path field PROVES about the directory behind it.
134
+ *
135
+ * Two independent facts, and either may be enough:
136
+ *
137
+ * * `canonical` — the value is a readable path and survives redaction, so two
138
+ * records carrying the same canonical value are the same directory;
139
+ * * `digest` — a stored digest is well formed, so a comparison can be made on the
140
+ * digest alone even when the path string was shortened.
141
+ *
142
+ * Both absent means the field proves nothing, and a caller must refuse. Nothing
143
+ * here ever answers "any path", which is what a wildcard would be.
144
+ * @param {unknown} value - stored path value.
145
+ * @param {unknown} [storedDigest] - stored digest beside it, if the row has one.
146
+ * @returns {{ canonical: string | null, digest: string | null, masked: boolean }} facts.
147
+ */
148
+ export function readPathIdentity(value, storedDigest = null) {
149
+ const masked = recordedPathIsMasked(value);
150
+ const canonical = masked ? null : canonicalPath(value);
151
+ const digest = isPathDigest(storedDigest)
152
+ ? storedDigest
153
+ : (canonical === null ? null : pathDigest(canonical));
154
+ return { canonical, digest, masked };
155
+ }
156
+
157
+ /**
158
+ * Whether two stored path fields prove they name the SAME directory.
159
+ *
160
+ * The rule is an EQUALITY OF USABLE EVIDENCE, and every kind of evidence a record
161
+ * carries must agree before it proves anything:
162
+ *
163
+ * 1. A record that states both a readable path and a digest must have them name the
164
+ * same directory. A digest that contradicts its own path is a record that was
165
+ * edited, and NOTHING it says is used — not the path and not the digest. An
166
+ * earlier version let the readable path win such a case, which admitted
167
+ * `'E:/repo'` recorded with `pathDigest('E:/other')`.
168
+ * 2. Then the two records are compared. Two readable paths are compared exactly; a
169
+ * masked side contributes only its digest, so a shortened path is never matched
170
+ * against a string.
171
+ * 3. A side with no readable path AND no digest proves nothing, so the answer is
172
+ * `false` — an empty or masked value without a digest is a refusal, never a
173
+ * wildcard.
174
+ * @param {{ path?: unknown, digest?: unknown }} left - first record.
175
+ * @param {{ path?: unknown, digest?: unknown }} right - second record.
176
+ * @returns {boolean} true only when both sides prove the same directory.
177
+ */
178
+ export function sameRecordedDirectory(left, right) {
179
+ const a = readPathIdentity(left?.path, left?.digest);
180
+ const b = readPathIdentity(right?.path, right?.digest);
181
+ // (1) Each side must be internally consistent before it may prove anything.
182
+ if (!identityIsConsistent(a) || !identityIsConsistent(b)) return false;
183
+ // (2) Both readable: an exact comparison of the canonical spellings.
184
+ if (a.canonical !== null && b.canonical !== null) return a.canonical === b.canonical;
185
+ // (3) At least one side is masked; both must then offer a digest, and those must match.
186
+ if (a.digest !== null && b.digest !== null) return a.digest === b.digest;
187
+ return false;
188
+ }
189
+
190
+ /**
191
+ * Whether one record's own two proofs agree.
192
+ *
193
+ * A digest is derived from the readable path, so a well-formed digest beside a
194
+ * readable path has exactly one permissible value. Anything else means the record is
195
+ * inconsistent and must not be used as evidence at all.
196
+ * @param {{ canonical: string | null, digest: string | null }} reading - one record.
197
+ * @returns {boolean} true when the record is consistent (or states only one proof).
198
+ */
199
+ function identityIsConsistent(reading) {
200
+ if (reading.canonical === null || reading.digest === null) return true;
201
+ return reading.digest === pathDigest(reading.canonical);
202
+ }
203
+
204
+ /**
205
+ * Whether a stored path field names THIS directory, from either kind of evidence.
206
+ * @param {{ path?: unknown, digest?: unknown }} recorded - stored record facts.
207
+ * @param {string} directory - the directory a submission resolved.
208
+ * @returns {boolean} true only when the record proves it.
209
+ */
210
+ export function recordedDirectoryIs(recorded, directory) {
211
+ return sameRecordedDirectory(recorded, { path: directory, digest: pathDigest(directory) });
212
+ }
213
+
214
+ /**
215
+ * The identity SAVED for one task thread.
216
+ *
217
+ * Two facts per path, and they serve different purposes:
218
+ *
219
+ * * `sourcePath` / `executionPath` keep the operator's REAL spelling. This value can
220
+ * be handed back to the runtime as a directory to open, so it must never be a
221
+ * lower-cased comparison key.
222
+ * * `sourcePathDigest` / `executionPathDigest` are digests of the CANONICAL key
223
+ * ({@link canonicalPath}), which is what a later comparison is made on.
224
+ *
225
+ * Storing the canonical key in the path field would look like success and then fail
226
+ * at the first real read on a case-sensitive filesystem. Only identity is written: no
227
+ * prompt, transcript, output, usage figure or credential has a field.
228
+ * @param {object} task - task record with `taskId`, `sourcePath`, `executionPath`.
229
+ * @param {object | null} placement - the run's own placement (`workspaceId`, `sessionId`).
230
+ * @param {string | null} isolation - the isolation the run used.
231
+ * @returns {{ taskId: string, sourcePath: string | null, executionPath: string | null,
232
+ * sourcePathDigest: string | null, executionPathDigest: string | null,
233
+ * workspaceId: string, sessionId: string, isolation: string | null,
234
+ * agentTeams: boolean | null } | null} identity, or null.
235
+ */
236
+ export function continuationIdentityOf(task, placement, isolation) {
237
+ const taskId = typeof task?.taskId === 'string' ? task.taskId.trim() : '';
238
+ if (taskId === '') return null;
239
+ const sourcePath = storedSpelling(task.sourcePath);
240
+ const executionPath = storedSpelling(task.executionPath);
241
+ return {
242
+ taskId,
243
+ sourcePath,
244
+ executionPath,
245
+ sourcePathDigest: sourcePath === null ? null : pathDigest(sourcePath),
246
+ executionPathDigest: executionPath === null ? null : pathDigest(executionPath),
247
+ workspaceId: typeof placement?.workspaceId === 'string' ? placement.workspaceId.trim() : '',
248
+ sessionId: typeof placement?.sessionId === 'string' ? placement.sessionId.trim() : '',
249
+ isolation: typeof isolation === 'string' && isolation !== '' ? isolation : null,
250
+ // The chain's Agent Teams decision, so a follow-up inherits the mode its parent ran
251
+ // with unless the dispatcher states a new one explicitly. `null` = no decision was
252
+ // recorded for the parent (the Session keeps its own mode).
253
+ agentTeams: typeof task?.agentTeams === 'boolean' ? task.agentTeams : null,
254
+ };
255
+ }
256
+
257
+ /**
258
+ * The spelling to WRITE for a path: the operator's own, trimmed, never a key.
259
+ *
260
+ * Returns null for a value that is not a usable path (empty, non-string, or already
261
+ * masked), so a row cannot carry a mask as though it were a directory.
262
+ * @param {unknown} value - candidate path.
263
+ * @returns {string | null} the stored spelling, or null.
264
+ */
265
+ function storedSpelling(value) {
266
+ if (typeof value !== 'string') return null;
267
+ const trimmed = value.trim();
268
+ if (trimmed === '' || recordedPathIsMasked(trimmed)) return null;
269
+ return trimmed;
270
+ }
271
+
272
+ /**
273
+ * The identity fields a durable history row carries.
274
+ *
275
+ * Kept as one list (rather than written out at the save site) so the history
276
+ * projection, the engine and the tests name the same fields.
277
+ */
278
+ export const CONTINUATION_IDENTITY_FIELDS = Object.freeze([
279
+ 'sourcePathDigest',
280
+ 'executionPathDigest',
281
+ 'workspaceId',
282
+ 'sessionId',
283
+ ]);