dsh-taskbridge 1.4.2 → 1.5.1
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/CHANGELOG.md +34 -2
- package/README.md +10 -8
- package/bin/dsh-delegate-mcp.d.ts +20 -3
- package/bin/dsh-delegate-mcp.js +15 -0
- package/codex/dsh-taskbridge.toml +180 -16
- package/docs/INTRODUCTION.md +2 -2
- package/docs/INTRODUCTION.zh.md +2 -2
- package/docs/codex-integration.md +11 -13
- package/docs/conversation-placement.md +44 -7
- package/docs/plugin-market.md +1 -1
- package/docs/redaction-contract.md +47 -4
- package/docs/releases/1.5.0-token-benchmark.zh.md +73 -0
- package/docs/releases/1.5.0.zh.md +51 -0
- package/docs/releases/1.5.1.zh.md +70 -0
- package/docs/runtime-compatibility.md +4 -4
- package/docs/silent-wait.md +157 -3
- package/docs/taskbridge-upgrade.md +1 -1
- package/docs/worker-permissions.md +154 -17
- package/lib/client.js +298 -103
- package/package.json +9 -3
- package/src/client/api.js +315 -34
- package/src/client/index.jsx +151 -67
- package/src/codex/installer.js +251 -11
- package/src/config/redact.js +72 -11
- package/src/http/execution-plane.js +124 -10
- package/src/index.d.ts +347 -0
- package/src/index.js +88 -5
- package/src/jobs/activity.js +102 -9
- package/src/jobs/continuation-identity.js +283 -0
- package/src/jobs/engine.js +491 -55
- package/src/jobs/history.js +96 -0
- package/src/mcp/continuation.js +317 -0
- package/src/mcp/metrics.js +366 -0
- package/src/mcp/server.js +1203 -53
- package/src/mcp/wait.js +338 -73
- package/src/review/prompts.js +145 -126
- package/src/runtime/agent-runner.js +330 -0
- package/src/runtime/permissions.js +192 -2
- package/src/runtime/question-call.js +162 -0
- package/src/runtime/questions.js +909 -0
- package/src/runtime/sandbox.js +363 -358
- package/src/runtime/session-placement.js +1207 -1121
- package/src/version.js +121 -0
- package/src/workspace/worktrees.js +70 -4
package/src/jobs/activity.js
CHANGED
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
// The clock is injected (`now`) so tests can simulate twenty minutes of real
|
|
42
42
|
// progress deterministically instead of sleeping.
|
|
43
43
|
|
|
44
|
-
import { createHash } from 'node:crypto';
|
|
44
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
45
45
|
import { REPEATED_FAILURE_KIND } from './diagnostics.js';
|
|
46
46
|
|
|
47
47
|
/**
|
|
@@ -518,6 +518,16 @@ export class RepeatedFailureTracker {
|
|
|
518
518
|
const threshold = Number(options.threshold);
|
|
519
519
|
this.threshold = Number.isSafeInteger(threshold) && threshold > 0 ? threshold : 0;
|
|
520
520
|
this.digest = typeof options.digest === 'function' ? options.digest : sha256Hex;
|
|
521
|
+
// A per-monitor random nonce, folded into every eventId this monitor mints.
|
|
522
|
+
// Two tasks whose monitors freeze the same clock, run the same tool, the same
|
|
523
|
+
// operation and the same failure would otherwise hash identical episode
|
|
524
|
+
// identities to the SAME eventId — and then one task's ack could acknowledge
|
|
525
|
+
// (and silence) the other task's reminder. The nonce namespaces the event
|
|
526
|
+
// identity per monitor; it carries no content of its own. SIXTEEN random bytes:
|
|
527
|
+
// the collision space of two monitors in the same process must be far larger
|
|
528
|
+
// than any plausible task count, and a smaller nonce would shrink it for no
|
|
529
|
+
// benefit — the id is 16 hex chars of digest, not of entropy.
|
|
530
|
+
this.nonce = randomHex16();
|
|
521
531
|
/** callId -> { name, operationDigest } for calls whose result has not arrived yet. */
|
|
522
532
|
this.openCalls = new Map();
|
|
523
533
|
/**
|
|
@@ -604,8 +614,15 @@ export class RepeatedFailureTracker {
|
|
|
604
614
|
this.episode.lastSeenAt = at;
|
|
605
615
|
if (this.episode.reported || this.episode.count < this.threshold) return null;
|
|
606
616
|
this.episode.reported = true;
|
|
617
|
+
// The stable event identity of THIS reminder. It is a digest of the episode's
|
|
618
|
+
// own hashed identity and its internal sequence — never the raw arguments,
|
|
619
|
+
// the error prose or the result body, which exist only as hash input. The
|
|
620
|
+
// receiver acknowledges THIS id, so a lost answer can be recovered by
|
|
621
|
+
// replaying exactly this event and an ack can be bound to it precisely.
|
|
622
|
+
const eventId = this.digest(`event\u0000${this.nonce}\u0000${identity}\u0000${this.episode.sequence}\u0000${this.episode.firstSeenAt}`).slice(0, 16);
|
|
607
623
|
const entry = {
|
|
608
624
|
kind: REPEATED_FAILURE_KIND,
|
|
625
|
+
eventId,
|
|
609
626
|
toolName: call.name,
|
|
610
627
|
failureCode: code,
|
|
611
628
|
operationDigest: call.operationDigest,
|
|
@@ -736,11 +753,23 @@ function sha256Hex(value) {
|
|
|
736
753
|
return createHash('sha256').update(value, 'utf8').digest('hex');
|
|
737
754
|
}
|
|
738
755
|
|
|
756
|
+
/**
|
|
757
|
+
* One 32-hex random nonce (16 random bytes), the per-monitor namespace of every
|
|
758
|
+
* eventId it mints.
|
|
759
|
+
* @returns {string} 32 lowercase hex characters.
|
|
760
|
+
*/
|
|
761
|
+
function randomHex16() {
|
|
762
|
+
return randomBytes(16).toString('hex');
|
|
763
|
+
}
|
|
764
|
+
|
|
739
765
|
/** Why one observation wait ended. */
|
|
740
766
|
export const OBSERVE_REASONS = Object.freeze({
|
|
741
767
|
heartbeat: 'heartbeat',
|
|
742
768
|
attention: 'attention',
|
|
743
769
|
permission: 'permission',
|
|
770
|
+
// The worker asked a human an ordinary question. It is its own reason because answering
|
|
771
|
+
// it grants nothing, and it must be reported at once rather than as silence.
|
|
772
|
+
question: 'question',
|
|
744
773
|
ready: 'ready',
|
|
745
774
|
failed: 'failed',
|
|
746
775
|
cancelled: 'cancelled',
|
|
@@ -822,13 +851,24 @@ export class TaskMonitor {
|
|
|
822
851
|
*/
|
|
823
852
|
this.failures = new RepeatedFailureTracker({ threshold: options.repeatFailureThreshold });
|
|
824
853
|
/**
|
|
825
|
-
* The one failure reminder waiting to be
|
|
854
|
+
* The one failure reminder waiting to be DELIVERED AND ACKNOWLEDGED, or null.
|
|
826
855
|
*
|
|
827
856
|
* A single slot, not a queue: the reminder is about the CURRENT episode, and an
|
|
828
|
-
* undelivered older one is superseded rather than replayed.
|
|
857
|
+
* undelivered older one is superseded rather than replayed. Unlike a take-once
|
|
858
|
+
* claim, this reminder STAYS parked until a receiver acknowledges it by its
|
|
859
|
+
* `eventId`: the server's having written one answer out is not proof that the
|
|
860
|
+
* receiver ever read it, and an answer lost on the wire must be recoverable by
|
|
861
|
+
* the client's own retry — the retry observes the SAME event, not a heartbeat.
|
|
829
862
|
* @type {object | null}
|
|
830
863
|
*/
|
|
831
864
|
this.pendingFailure = null;
|
|
865
|
+
/**
|
|
866
|
+
* The eventId of the most recently acknowledged reminder, kept so a repeated
|
|
867
|
+
* (idempotent) ack of a cleared event still answers success, and so an ack
|
|
868
|
+
* can be checked against what this monitor actually delivered.
|
|
869
|
+
* @type {string | null}
|
|
870
|
+
*/
|
|
871
|
+
this.ackedEventId = null;
|
|
832
872
|
/** Episode identity: bumped whenever genuine activity ends the idle episode. */
|
|
833
873
|
this.episode = 0;
|
|
834
874
|
/** The episode the self-check already ran for, or null. */
|
|
@@ -920,15 +960,18 @@ export class TaskMonitor {
|
|
|
920
960
|
/**
|
|
921
961
|
* Take the failure reminder that is waiting to be delivered, if any.
|
|
922
962
|
*
|
|
923
|
-
*
|
|
924
|
-
*
|
|
925
|
-
*
|
|
926
|
-
*
|
|
927
|
-
*
|
|
963
|
+
* The reminder is NOT consumed here: it stays parked until it is acknowledged by
|
|
964
|
+
* its `eventId` through {@link ackFailureReport}. That is what makes a lost HTTP
|
|
965
|
+
* answer recoverable — the client's retry observes the same reminder again,
|
|
966
|
+
* because the server's having generated one answer proves nothing about what the
|
|
967
|
+
* receiver got. A reminder whose episode has ended or changed since it was parked
|
|
968
|
+
* is DROPPED rather than delivered: the run already recovered or moved on, so it
|
|
969
|
+
* is history, not news. Every observation returns the SAME event (same `eventId`),
|
|
970
|
+
* so one repeated failure is one notification identity, never several.
|
|
971
|
+
* @returns {object | null} the published diagnostic, or null.
|
|
928
972
|
*/
|
|
929
973
|
claimFailureReport() {
|
|
930
974
|
const report = this.pendingFailure;
|
|
931
|
-
this.pendingFailure = null;
|
|
932
975
|
if (!this.failures.isCurrent(report)) return null;
|
|
933
976
|
// Published form: the internal episode sequence does not leave this module.
|
|
934
977
|
const { episodeSequence, ...published } = report;
|
|
@@ -936,6 +979,47 @@ export class TaskMonitor {
|
|
|
936
979
|
return published;
|
|
937
980
|
}
|
|
938
981
|
|
|
982
|
+
/**
|
|
983
|
+
* Acknowledge one delivered failure reminder by its exact event identity.
|
|
984
|
+
*
|
|
985
|
+
* The ack is bound to what THIS monitor parked: a matching `eventId` clears the
|
|
986
|
+
* reminder, a second ack of the same (now cleared) event is an idempotent
|
|
987
|
+
* success, and anything else — an unknown id, a stale one, garbage — leaves the
|
|
988
|
+
* pending reminder untouched. A NEW episode keeps parking its own reminder, so
|
|
989
|
+
* clearing one event can never silence the next notification.
|
|
990
|
+
* @param {unknown} eventId - the `eventId` the receiver observed.
|
|
991
|
+
* @returns {{ acknowledged: boolean, eventId: string | null, reason: string | null }} ack result.
|
|
992
|
+
*/
|
|
993
|
+
ackFailureReport(eventId) {
|
|
994
|
+
const requested = typeof eventId === 'string' && eventId !== '' ? eventId : null;
|
|
995
|
+
let result;
|
|
996
|
+
if (requested === null) {
|
|
997
|
+
result = { acknowledged: false, eventId: null, reason: 'event-id-required' };
|
|
998
|
+
} else if (requested === this.ackedEventId) {
|
|
999
|
+
// Idempotent: the receiver repeats an ack that already landed.
|
|
1000
|
+
result = { acknowledged: true, eventId: requested, reason: null };
|
|
1001
|
+
} else {
|
|
1002
|
+
const report = this.pendingFailure;
|
|
1003
|
+
if (report === null || report.eventId !== requested) {
|
|
1004
|
+
// Wrong, unknown or stale id. The pending reminder (if any) is NOT cleared:
|
|
1005
|
+
// a bad ack must not silence a live notification.
|
|
1006
|
+
result = { acknowledged: false, eventId: requested, reason: 'unknown-event' };
|
|
1007
|
+
} else if (!this.failures.isCurrent(report)) {
|
|
1008
|
+
// The episode ended before the ack arrived (recovery, or a different
|
|
1009
|
+
// failure took over). The reminder is dropped here — it was superseded,
|
|
1010
|
+
// and a late ack must not resurrect it — but it is honest that nothing
|
|
1011
|
+
// pending was acknowledged.
|
|
1012
|
+
this.pendingFailure = null;
|
|
1013
|
+
result = { acknowledged: false, eventId: requested, reason: 'superseded' };
|
|
1014
|
+
} else {
|
|
1015
|
+
this.pendingFailure = null;
|
|
1016
|
+
this.ackedEventId = requested;
|
|
1017
|
+
result = { acknowledged: true, eventId: requested, reason: null };
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
return { ...result };
|
|
1021
|
+
}
|
|
1022
|
+
|
|
939
1023
|
/**
|
|
940
1024
|
* The instant the current idle episode began.
|
|
941
1025
|
*
|
|
@@ -1111,6 +1195,7 @@ export class TaskMonitor {
|
|
|
1111
1195
|
// a stored record or a client.
|
|
1112
1196
|
reported: this.failures.reported.map((entry) => ({
|
|
1113
1197
|
kind: entry.kind,
|
|
1198
|
+
eventId: entry.eventId,
|
|
1114
1199
|
toolName: entry.toolName,
|
|
1115
1200
|
failureCode: entry.failureCode,
|
|
1116
1201
|
operationDigest: entry.operationDigest,
|
|
@@ -1211,6 +1296,13 @@ export class TaskMonitor {
|
|
|
1211
1296
|
if (this.task?.permission?.pending !== null && this.task?.permission?.pending !== undefined) {
|
|
1212
1297
|
return this.outcome(OBSERVE_REASONS.permission, activitySinceWait, at - startedAt, selfCheck);
|
|
1213
1298
|
}
|
|
1299
|
+
if (this.task?.question?.pending !== null && this.task?.question?.pending !== undefined) {
|
|
1300
|
+
// A question is checked with the same precedence as a permission (and AFTER it, so
|
|
1301
|
+
// the one wait that can grant is reported first): the worker is paused on a human
|
|
1302
|
+
// and the host must act now. The self-check above still runs, but it can never
|
|
1303
|
+
// turn this into `attention`.
|
|
1304
|
+
return this.outcome(OBSERVE_REASONS.question, activitySinceWait, at - startedAt, selfCheck);
|
|
1305
|
+
}
|
|
1214
1306
|
// (3b) Repeated failures. Same reason as the silence report — it needs the
|
|
1215
1307
|
// commander — but it carries its own typed diagnostic and is NOT gated on
|
|
1216
1308
|
// silence: a busy run that keeps failing the same operation must still reach
|
|
@@ -1280,6 +1372,7 @@ export class TaskMonitor {
|
|
|
1280
1372
|
// trail stays as bounded evidence. The undelivered reminder is dropped with it.
|
|
1281
1373
|
this.failures.dispose();
|
|
1282
1374
|
this.pendingFailure = null;
|
|
1375
|
+
this.ackedEventId = null;
|
|
1283
1376
|
this.releaseWaiters();
|
|
1284
1377
|
}
|
|
1285
1378
|
}
|
|
@@ -0,0 +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,
|
|
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
|
+
]);
|