@agentxm/workspace-operations 0.28.12 → 0.28.13
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/dist/src/index.d.ts +11 -10
- package/dist/src/index.js +16 -12
- package/dist/src/operations/scan-plan-readiness.d.ts +1 -1
- package/dist/src/operations/scan-plan-readiness.js +1 -1
- package/dist/src/plan/apply-plan.js +8 -8
- package/dist/src/plan/execution-candidate.d.ts +7 -1
- package/dist/src/plan/execution-candidate.js +9 -2
- package/dist/src/plan/interruption-resolution.d.ts +50 -0
- package/dist/src/plan/interruption-resolution.js +158 -0
- package/dist/src/plan/operation-journal.d.ts +9 -9
- package/dist/src/plan/operation-journal.js +5 -20
- package/dist/src/plan/operation-resolution.d.ts +1 -1
- package/dist/src/plan/plan-execution-fixtures.d.ts +2 -2
- package/dist/src/plan/plan-execution-fixtures.js +2 -2
- package/dist/src/plan/plan-execution.d.ts +48 -0
- package/dist/src/plan/plan-execution.js +40 -0
- package/dist/src/plan/plan.d.ts +9 -19
- package/dist/src/plan/resolve-plan-interaction.d.ts +1 -1
- package/dist/src/plan/resolve-plan-interaction.js +1 -1
- package/dist/src/plan/resolve-plan.d.ts +38 -23
- package/dist/src/plan/resolve-plan.js +114 -96
- package/dist/src/plan/step-failure-conversions.d.ts +2 -1
- package/dist/src/testing.d.ts +22 -1
- package/dist/src/testing.js +23 -1
- package/package.json +12 -16
- package/dist/src/live.d.ts +0 -13
- package/dist/src/live.js +0 -12
- package/dist/src/operations/load-workspace.d.ts +0 -44
- package/dist/src/operations/load-workspace.js +0 -80
- package/dist/src/operations/memory-transition-lock.d.ts +0 -20
- package/dist/src/operations/memory-transition-lock.js +0 -57
- package/dist/src/operations/transaction.d.ts +0 -60
- package/dist/src/operations/transaction.js +0 -358
- package/dist/src/operations/transition-lock.d.ts +0 -72
- package/dist/src/operations/transition-lock.js +0 -298
|
@@ -1,358 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Workspace transaction mechanics: the snapshot/restore/validate/rollback
|
|
3
|
-
* runner and the closure settlement operations, implemented against the
|
|
4
|
-
* ambient authority context declared in `../transaction.ts`.
|
|
5
|
-
*
|
|
6
|
-
* @experimental This API is unstable and may change without notice.
|
|
7
|
-
*/
|
|
8
|
-
import { createHash, randomBytes } from "node:crypto";
|
|
9
|
-
import * as Cause from "effect/Cause";
|
|
10
|
-
import * as Effect from "effect/Effect";
|
|
11
|
-
import * as FileSystem from "effect/FileSystem";
|
|
12
|
-
import * as Option from "effect/Option";
|
|
13
|
-
import * as Path from "effect/Path";
|
|
14
|
-
import * as Semaphore from "effect/Semaphore";
|
|
15
|
-
import { recordFootprint } from "@agentxm/workspace-state";
|
|
16
|
-
import { CurrentWorkspaceClosure, CurrentWorkspaceTransaction, protectInContext, TransitionLockUnavailable, WorkspaceDirectoryError, WorkspaceRestorationError, WorkspaceRestorationIncomplete, WorkspaceTransitionCompromised, } from "@agentxm/workspace-state";
|
|
17
|
-
/** Run one semantic closure's mutations under its closure identity. */
|
|
18
|
-
export const withWorkspaceClosure = (closureId) => (effect) => effect.pipe(Effect.provideService(CurrentWorkspaceClosure, closureId));
|
|
19
|
-
const normalizedTargets = (path, targets) => {
|
|
20
|
-
const sorted = Array.from(new Set(targets.map((target) => path.resolve(target)))).sort((left, right) => left.length - right.length || left.localeCompare(right));
|
|
21
|
-
const retained = [];
|
|
22
|
-
for (const target of sorted) {
|
|
23
|
-
if (retained.some((parent) => target === parent || target.startsWith(`${parent}${path.sep}`))) {
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
retained.push(target);
|
|
27
|
-
}
|
|
28
|
-
return retained;
|
|
29
|
-
};
|
|
30
|
-
const workspaceRelative = (path, workspaceDir, target) => {
|
|
31
|
-
const relative = path.relative(path.dirname(workspaceDir), target);
|
|
32
|
-
return relative.startsWith("..") ? target : relative;
|
|
33
|
-
};
|
|
34
|
-
const sha256 = (input) => createHash("sha256").update(input).digest("hex");
|
|
35
|
-
/**
|
|
36
|
-
* Deterministic content hash of a path's current state: file bytes, symlink
|
|
37
|
-
* target, recursive directory listing, or the literal `absent`.
|
|
38
|
-
*/
|
|
39
|
-
const hashPathState = (fs, path, target) => Effect.gen(function* () {
|
|
40
|
-
const link = yield* fs.readLink(target).pipe(Effect.option);
|
|
41
|
-
if (Option.isSome(link))
|
|
42
|
-
return sha256(`symlink:${link.value}`);
|
|
43
|
-
const exists = yield* fs.exists(target);
|
|
44
|
-
if (!exists)
|
|
45
|
-
return "absent";
|
|
46
|
-
const info = yield* fs.stat(target);
|
|
47
|
-
if (info.type === "Directory") {
|
|
48
|
-
const entries = [...(yield* fs.readDirectory(target))].sort();
|
|
49
|
-
const parts = [];
|
|
50
|
-
for (const entry of entries) {
|
|
51
|
-
const child = yield* hashPathState(fs, path, path.join(target, entry));
|
|
52
|
-
parts.push(`${entry}:${child}`);
|
|
53
|
-
}
|
|
54
|
-
return sha256(`dir:${parts.join("\n")}`);
|
|
55
|
-
}
|
|
56
|
-
const bytes = yield* fs.readFile(target);
|
|
57
|
-
return sha256(bytes);
|
|
58
|
-
}).pipe(Effect.catch(() => Effect.succeed("unhashable")));
|
|
59
|
-
const dropClosureSnapshots = (context, closureId) => {
|
|
60
|
-
let index = context.snapshots.length;
|
|
61
|
-
while (index > 0) {
|
|
62
|
-
index -= 1;
|
|
63
|
-
if (context.snapshots[index]?.closure === closureId) {
|
|
64
|
-
context.snapshots.splice(index, 1);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
context.protectedTargets.delete(closureId);
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Settle one closure: its commits stand, so its snapshots leave the
|
|
71
|
-
* restoration set and a later closure touching the same target takes a fresh
|
|
72
|
-
* post-commit preimage. No-op outside a transaction.
|
|
73
|
-
*/
|
|
74
|
-
export const settleWorkspaceClosure = (closureId) => CurrentWorkspaceTransaction.pipe(Effect.flatMap(Option.match({
|
|
75
|
-
onNone: () => Effect.void,
|
|
76
|
-
onSome: (context) => context.snapshotSemaphore.withPermits(1)(Effect.sync(() => {
|
|
77
|
-
dropClosureSnapshots(context, closureId);
|
|
78
|
-
})),
|
|
79
|
-
})));
|
|
80
|
-
/**
|
|
81
|
-
* Roll back one failed closure: restore and verify exactly its snapshots, in
|
|
82
|
-
* reverse order, leaving every other closure's work in place. A restoration
|
|
83
|
-
* that does not complete and verify records a pending typed failure the
|
|
84
|
-
* transaction surfaces at its end — the truth travels in memory, never
|
|
85
|
-
* through a later workspace write. No-op outside a transaction.
|
|
86
|
-
*/
|
|
87
|
-
export const rollbackWorkspaceClosure = (closureId) => CurrentWorkspaceTransaction.pipe(Effect.flatMap(Option.match({
|
|
88
|
-
onNone: () => Effect.void,
|
|
89
|
-
onSome: (context) => context.snapshotSemaphore.withPermits(1)(Effect.gen(function* () {
|
|
90
|
-
const { fs, path } = context;
|
|
91
|
-
const owned = context.snapshots.filter((snapshot) => snapshot.closure === closureId);
|
|
92
|
-
if (owned.length === 0) {
|
|
93
|
-
dropClosureSnapshots(context, closureId);
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
const transitionCompromised = context.isTransitionCompromised;
|
|
97
|
-
yield* restoreAll(fs, path, owned, transitionCompromised).pipe(Effect.andThen(verifySnapshots(fs, path, owned)), Effect.matchEffect({
|
|
98
|
-
onFailure: (restorationCause) => Effect.sync(() => {
|
|
99
|
-
context.pendingRestorationFailures.push({
|
|
100
|
-
closureId,
|
|
101
|
-
restorationCause,
|
|
102
|
-
retained: owned.map((snapshot) => workspaceRelative(path, context.workspaceDir, snapshot.target)),
|
|
103
|
-
});
|
|
104
|
-
}),
|
|
105
|
-
onSuccess: () => Effect.void,
|
|
106
|
-
}));
|
|
107
|
-
dropClosureSnapshots(context, closureId);
|
|
108
|
-
})),
|
|
109
|
-
})));
|
|
110
|
-
/** Whether anything occupies the path: a file, directory, or (broken) symlink. */
|
|
111
|
-
const pathPresent = (fs, target) => fs.readLink(target).pipe(Effect.map(() => true), Effect.catch(() => fs.exists(target)));
|
|
112
|
-
/**
|
|
113
|
-
* Restore one snapshot through validated staging and atomic publication.
|
|
114
|
-
* The restored content is fully staged and validated in an owned
|
|
115
|
-
* `<target>.tmp.<unique>` sibling before a rename publishes it, so abrupt
|
|
116
|
-
* termination — including a forced process exit — can never expose a
|
|
117
|
-
* partially restored target: the authoritative path holds the failure-time
|
|
118
|
-
* content, the restored content, or (for a directory swap only, between two
|
|
119
|
-
* renames) nothing, never a partial tree. The target path itself is never
|
|
120
|
-
* removed; only owned `.tmp.` siblings are.
|
|
121
|
-
*/
|
|
122
|
-
const restoreSnapshot = (fs, path, snapshot) => Effect.gen(function* () {
|
|
123
|
-
if (snapshot.state === "absent") {
|
|
124
|
-
if (!(yield* pathPresent(fs, snapshot.target)))
|
|
125
|
-
return;
|
|
126
|
-
// Publishing absence is one rename: the mutated tree leaves the
|
|
127
|
-
// authoritative path atomically, then the owned trash is removed.
|
|
128
|
-
const trash = `${snapshot.target}.tmp.${randomBytes(6).toString("hex")}`;
|
|
129
|
-
yield* fs.rename(snapshot.target, trash);
|
|
130
|
-
yield* fs.remove(trash, { recursive: true, force: true }).pipe(Effect.ignore);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
yield* fs.makeDirectory(path.dirname(snapshot.target), { recursive: true });
|
|
134
|
-
const staging = `${snapshot.target}.tmp.${randomBytes(6).toString("hex")}`;
|
|
135
|
-
yield* Effect.gen(function* () {
|
|
136
|
-
if (snapshot.state === "symlink") {
|
|
137
|
-
yield* fs.symlink(snapshot.linkTarget, staging);
|
|
138
|
-
const staged = yield* fs.readLink(staging);
|
|
139
|
-
if (staged !== snapshot.linkTarget) {
|
|
140
|
-
return yield* new WorkspaceRestorationError({
|
|
141
|
-
target: snapshot.target,
|
|
142
|
-
step: "stage",
|
|
143
|
-
cause: { staged, expected: snapshot.linkTarget },
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
yield* fs.copy(snapshot.backup, staging, { preserveTimestamps: true });
|
|
149
|
-
const stagedHash = yield* hashPathState(fs, path, staging);
|
|
150
|
-
const backupHash = yield* hashPathState(fs, path, snapshot.backup);
|
|
151
|
-
if (stagedHash !== backupHash || stagedHash === "unhashable") {
|
|
152
|
-
return yield* new WorkspaceRestorationError({
|
|
153
|
-
target: snapshot.target,
|
|
154
|
-
step: "stage",
|
|
155
|
-
cause: { stagedHash, backupHash },
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
const targetLink = yield* fs.readLink(snapshot.target).pipe(Effect.option);
|
|
160
|
-
const targetInfo = Option.isSome(targetLink)
|
|
161
|
-
? Option.none()
|
|
162
|
-
: yield* fs.stat(snapshot.target).pipe(Effect.option);
|
|
163
|
-
const targetPresent = Option.isSome(targetLink) || Option.isSome(targetInfo);
|
|
164
|
-
const targetIsDirectory = Option.exists(targetInfo, (info) => info.type === "Directory");
|
|
165
|
-
const stagedIsDirectory = snapshot.state === "copied" && (yield* fs.stat(staging)).type === "Directory";
|
|
166
|
-
if (!targetPresent || (!targetIsDirectory && !stagedIsDirectory)) {
|
|
167
|
-
// rename atomically replaces a file or symlink target.
|
|
168
|
-
yield* fs.rename(staging, snapshot.target);
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
// A directory is swapped through two renames of owned names; the
|
|
172
|
-
// moved-aside content is intact in the trash sibling until removal.
|
|
173
|
-
const trash = `${snapshot.target}.tmp.${randomBytes(6).toString("hex")}`;
|
|
174
|
-
yield* fs.rename(snapshot.target, trash);
|
|
175
|
-
yield* fs.rename(staging, snapshot.target);
|
|
176
|
-
yield* fs.remove(trash, { recursive: true, force: true }).pipe(Effect.ignore);
|
|
177
|
-
}).pipe(Effect.onError(() => fs.remove(staging, { recursive: true, force: true }).pipe(Effect.ignore)));
|
|
178
|
-
});
|
|
179
|
-
const restoreAll = (fs, path, snapshots, transitionCompromised) => Effect.forEach([...snapshots].reverse(), (snapshot) => Effect.suspend(() =>
|
|
180
|
-
// Restoration is a durable write like any other: once lock ownership
|
|
181
|
-
// is lost it must stop, or it could overwrite a successor's work.
|
|
182
|
-
transitionCompromised()
|
|
183
|
-
? Effect.fail(new WorkspaceRestorationError({
|
|
184
|
-
target: snapshot.target,
|
|
185
|
-
step: "stopped",
|
|
186
|
-
cause: undefined,
|
|
187
|
-
}))
|
|
188
|
-
: restoreSnapshot(fs, path, snapshot).pipe(Effect.andThen(recordFootprint({ path: snapshot.target, change: "restored" })))), {
|
|
189
|
-
discard: true,
|
|
190
|
-
});
|
|
191
|
-
const verifySnapshots = (fs, path, snapshots) => Effect.forEach(snapshots, (snapshot) => Effect.gen(function* () {
|
|
192
|
-
const verified = yield* Effect.gen(function* () {
|
|
193
|
-
if (snapshot.state === "absent") {
|
|
194
|
-
return !(yield* fs.exists(snapshot.target));
|
|
195
|
-
}
|
|
196
|
-
if (snapshot.state === "symlink") {
|
|
197
|
-
const link = yield* fs.readLink(snapshot.target).pipe(Effect.option);
|
|
198
|
-
return Option.exists(link, (value) => value === snapshot.linkTarget);
|
|
199
|
-
}
|
|
200
|
-
const restored = yield* hashPathState(fs, path, snapshot.target);
|
|
201
|
-
const backup = yield* hashPathState(fs, path, snapshot.backup);
|
|
202
|
-
return restored === backup && restored !== "unhashable";
|
|
203
|
-
}).pipe(Effect.catch(() => Effect.succeed(false)));
|
|
204
|
-
if (!verified) {
|
|
205
|
-
return yield* new WorkspaceRestorationError({
|
|
206
|
-
target: snapshot.target,
|
|
207
|
-
step: "verify",
|
|
208
|
-
cause: { state: snapshot.state },
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
}), { discard: true });
|
|
212
|
-
/**
|
|
213
|
-
* Run one coupled workspace mutation under the workspace transition lock.
|
|
214
|
-
*
|
|
215
|
-
* Every authoritative target is snapshotted into a uniquely prefixed
|
|
216
|
-
* OS-temporary directory before the transition begins. A failed transition or
|
|
217
|
-
* postcondition check restores and verifies the exact pre-operation paths and
|
|
218
|
-
* removes the snapshots; a restoration that does not complete and verify
|
|
219
|
-
* fails with the typed {@link WorkspaceRestorationIncomplete}, preserving the
|
|
220
|
-
* snapshot directory for manual inspection. Nothing about a failure persists
|
|
221
|
-
* in the workspace: the next mutation plans from the current workspace state.
|
|
222
|
-
*
|
|
223
|
-
* The invocation-level transition hold is reused when a plan-family apply
|
|
224
|
-
* already acquired it; otherwise this transaction acquires its own for the
|
|
225
|
-
* duration of the mutation.
|
|
226
|
-
*/
|
|
227
|
-
export const runWorkspaceTransaction = (args) => Effect.gen(function* () {
|
|
228
|
-
const current = yield* CurrentWorkspaceTransaction;
|
|
229
|
-
if (Option.isSome(current)) {
|
|
230
|
-
const activeClosure = yield* CurrentWorkspaceClosure;
|
|
231
|
-
yield* Effect.forEach(normalizedTargets(current.value.path, args.targets), (target) => protectInContext(current.value, target, activeClosure), { discard: true });
|
|
232
|
-
const value = yield* args.transition;
|
|
233
|
-
yield* args.validate(value);
|
|
234
|
-
return value;
|
|
235
|
-
}
|
|
236
|
-
const fs = yield* FileSystem.FileSystem;
|
|
237
|
-
const path = yield* Path.Path;
|
|
238
|
-
const workspaceDir = path.resolve(args.workspaceDir);
|
|
239
|
-
const missingWorkspaceAncestors = [];
|
|
240
|
-
let ancestor = workspaceDir;
|
|
241
|
-
while (true) {
|
|
242
|
-
const exists = yield* fs
|
|
243
|
-
.exists(ancestor)
|
|
244
|
-
.pipe(Effect.mapError((cause) => new WorkspaceDirectoryError({ path: ancestor, step: "inspect", cause })));
|
|
245
|
-
if (exists)
|
|
246
|
-
break;
|
|
247
|
-
missingWorkspaceAncestors.push(ancestor);
|
|
248
|
-
const parent = path.dirname(ancestor);
|
|
249
|
-
if (parent === ancestor)
|
|
250
|
-
break;
|
|
251
|
-
ancestor = parent;
|
|
252
|
-
}
|
|
253
|
-
return yield* args.semaphore.withPermits(1)(Effect.gen(function* () {
|
|
254
|
-
yield* fs
|
|
255
|
-
.makeDirectory(workspaceDir, { recursive: true })
|
|
256
|
-
.pipe(Effect.mapError((cause) => new WorkspaceDirectoryError({ path: workspaceDir, step: "create", cause })));
|
|
257
|
-
const scratchDir = path.join(workspaceDir, "tmp");
|
|
258
|
-
const removeEmptyScratch = fs.readDirectory(scratchDir).pipe(Effect.flatMap((entries) => entries.length === 0
|
|
259
|
-
? fs.remove(scratchDir, { recursive: true, force: false })
|
|
260
|
-
: Effect.void), Effect.ignore);
|
|
261
|
-
const removeNewEmptyWorkspace = Effect.forEach(missingWorkspaceAncestors, (directory) => fs.readDirectory(directory).pipe(Effect.flatMap((entries) => entries.length === 0
|
|
262
|
-
? fs.remove(directory, { recursive: true, force: false })
|
|
263
|
-
: Effect.void), Effect.ignore), { concurrency: 1, discard: true });
|
|
264
|
-
return yield* Effect.scoped(Effect.gen(function* () {
|
|
265
|
-
// The invocation-level transition hold already provides
|
|
266
|
-
// cross-process exclusion; acquiring here again would deadlock on
|
|
267
|
-
// our own lock.
|
|
268
|
-
if (args.lock.held(workspaceDir) === undefined) {
|
|
269
|
-
const contention = yield* args.lock.acquire({
|
|
270
|
-
workspaceDir,
|
|
271
|
-
holder: { command: "workspace-transaction", pid: process.pid },
|
|
272
|
-
});
|
|
273
|
-
if (Option.isSome(contention)) {
|
|
274
|
-
return yield* new TransitionLockUnavailable({
|
|
275
|
-
holder: Option.getOrUndefined(contention.value.holder),
|
|
276
|
-
waitedMillis: contention.value.waitedMillis,
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
const held = args.lock.held(workspaceDir);
|
|
281
|
-
const context = {
|
|
282
|
-
isTransitionCompromised: held?.isCompromised ?? (() => false),
|
|
283
|
-
fs,
|
|
284
|
-
path,
|
|
285
|
-
workspaceDir,
|
|
286
|
-
snapshotStore: { dir: undefined },
|
|
287
|
-
protectedTargets: new Map(),
|
|
288
|
-
snapshots: [],
|
|
289
|
-
snapshotSemaphore: Semaphore.makeUnsafe(1),
|
|
290
|
-
pendingRestorationFailures: [],
|
|
291
|
-
snapshotSequence: { value: 0 },
|
|
292
|
-
};
|
|
293
|
-
// The store is removed only when nothing in it is still needed:
|
|
294
|
-
// a closure whose rollback failed leaves its pre-change
|
|
295
|
-
// snapshots preserved for manual recovery, and the typed
|
|
296
|
-
// restoration fact names this directory.
|
|
297
|
-
const removeSnapshotStore = Effect.suspend(() => context.snapshotStore.dir === undefined ||
|
|
298
|
-
context.pendingRestorationFailures.length > 0
|
|
299
|
-
? Effect.void
|
|
300
|
-
: fs
|
|
301
|
-
.remove(context.snapshotStore.dir, { recursive: true, force: true })
|
|
302
|
-
.pipe(Effect.ignore));
|
|
303
|
-
// The compromise signal of the hold serializing this mutation:
|
|
304
|
-
// the invocation-level hold when one exists, else the one just
|
|
305
|
-
// acquired above. Mutation races against it and stops when
|
|
306
|
-
// ownership is lost.
|
|
307
|
-
// Interruptible like the business side: the race runs inside the
|
|
308
|
-
// uninterruptible rollback guard, and its loser must be
|
|
309
|
-
// interruptible for the race to settle.
|
|
310
|
-
const compromiseSignal = (held === undefined ? Effect.never : held.compromised).pipe(Effect.interruptible);
|
|
311
|
-
const transitionCompromised = held === undefined ? () => false : held.isCompromised;
|
|
312
|
-
const business = Effect.gen(function* () {
|
|
313
|
-
// The transaction's own declared targets belong to the
|
|
314
|
-
// operation closure: no semantic closure is active yet.
|
|
315
|
-
yield* Effect.forEach(normalizedTargets(path, args.targets), (target) => protectInContext(context, target, undefined), { discard: true });
|
|
316
|
-
const value = yield* args.transition;
|
|
317
|
-
yield* args.validate(value);
|
|
318
|
-
return value;
|
|
319
|
-
}).pipe(Effect.provideService(CurrentWorkspaceTransaction, Option.some(context)), Effect.interruptible);
|
|
320
|
-
const retainAll = (cause, restorationCause) => Effect.gen(function* () {
|
|
321
|
-
const interruption = Cause.hasInterruptsOnly(cause);
|
|
322
|
-
return yield* new WorkspaceRestorationIncomplete({
|
|
323
|
-
terminationCause: interruption ? "interruption" : "failure",
|
|
324
|
-
transitionCause: cause,
|
|
325
|
-
restorationCause,
|
|
326
|
-
snapshotDir: context.snapshotStore.dir,
|
|
327
|
-
retained: context.snapshots.map((snapshot) => workspaceRelative(path, workspaceDir, snapshot.target)),
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
// The mask/restore shape is load-bearing: the business runs in
|
|
331
|
-
// the restored (interruptible) region so an external termination
|
|
332
|
-
// request reaches it, while the settlement handlers — rollback,
|
|
333
|
-
// verification, and the typed retain path — run uninterruptibly
|
|
334
|
-
// and observe the interruption as a cause. A blanket mask would
|
|
335
|
-
// never deliver the interrupt to the parked business and the
|
|
336
|
-
// invocation could not stop.
|
|
337
|
-
return yield* Effect.uninterruptibleMask((restoreInterruptibility) => restoreInterruptibility(Effect.raceFirst(business, compromiseSignal)).pipe(Effect.matchCauseEffect({
|
|
338
|
-
onFailure: (cause) => {
|
|
339
|
-
const raceError = Option.getOrUndefined(Cause.findErrorOption(cause));
|
|
340
|
-
if (raceError instanceof WorkspaceTransitionCompromised) {
|
|
341
|
-
// Ownership is lost: restoring now could overwrite a
|
|
342
|
-
// successor's work. Retain everything the failure left,
|
|
343
|
-
// keep the snapshots, and fail typed.
|
|
344
|
-
return retainAll(cause, raceError);
|
|
345
|
-
}
|
|
346
|
-
return (args.onRestorationStarted ?? Effect.void)
|
|
347
|
-
.pipe(Effect.andThen(restoreAll(fs, path, context.snapshots, transitionCompromised)), Effect.andThen(verifySnapshots(fs, path, context.snapshots)))
|
|
348
|
-
.pipe(Effect.matchEffect({
|
|
349
|
-
onFailure: (restorationCause) => retainAll(cause, restorationCause),
|
|
350
|
-
onSuccess: () => removeSnapshotStore.pipe(Effect.andThen(Effect.failCause(cause))),
|
|
351
|
-
}));
|
|
352
|
-
},
|
|
353
|
-
onSuccess: (value) => removeSnapshotStore.pipe(Effect.as(value)),
|
|
354
|
-
})));
|
|
355
|
-
})).pipe(Effect.ensuring(removeEmptyScratch), Effect.ensuring(removeNewEmptyWorkspace));
|
|
356
|
-
}));
|
|
357
|
-
});
|
|
358
|
-
//# sourceMappingURL=transaction.js.map
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Workspace transition lock.
|
|
3
|
-
*
|
|
4
|
-
* One cross-process lock serializes mutation-class operations on a workspace.
|
|
5
|
-
* A plan-family apply acquires it after confirmation — planning, network
|
|
6
|
-
* acquisition, preview, and the confirmation decision stay lock-free — then
|
|
7
|
-
* revalidates its candidate and applies while holding it; the workspace
|
|
8
|
-
* transaction reuses the invocation's hold, or acquires its own when no
|
|
9
|
-
* plan-family hold exists. A contending invocation waits with a visible
|
|
10
|
-
* reason up to a bound, then terminates blocked with a machine-readable
|
|
11
|
-
* reference to the holder.
|
|
12
|
-
*
|
|
13
|
-
* Each acquisition records a distinct owner token in the holder metadata.
|
|
14
|
-
* Ownership is proven only by an exact token match — never inferred from a
|
|
15
|
-
* pid or from missing or unreadable metadata — and a holder write that fails
|
|
16
|
-
* releases the acquisition instead of holding anonymously.
|
|
17
|
-
*
|
|
18
|
-
* The lock file lives at `.axm/tmp/workspace-transition.lock`; its path is
|
|
19
|
-
* load-bearing for operational tooling and must not move casually.
|
|
20
|
-
*
|
|
21
|
-
* Acquisition is atomic with respect to interruption: the narrow region from
|
|
22
|
-
* the library granting the hold through finalizer registration is masked, so
|
|
23
|
-
* an interrupt can never strand a granted lock, while contention waits stay
|
|
24
|
-
* interruptible. Only the lock-is-held error class is absorbed by the bounded
|
|
25
|
-
* wait; any other acquisition error surfaces immediately as a typed failure.
|
|
26
|
-
*
|
|
27
|
-
* @experimental This API is unstable and may change without notice.
|
|
28
|
-
*/
|
|
29
|
-
import * as Effect from "effect/Effect";
|
|
30
|
-
import * as FileSystem from "effect/FileSystem";
|
|
31
|
-
import * as Option from "effect/Option";
|
|
32
|
-
import * as Path from "effect/Path";
|
|
33
|
-
import type * as Scope from "effect/Scope";
|
|
34
|
-
import { WorkspaceTransitionCompromised, type TransitionContention, type TransitionLockHolder, type WorkspaceTransitionAcquireFailure } from "@agentxm/workspace-state";
|
|
35
|
-
export declare const TRANSITION_LOCK_FILENAME = "workspace-transition.lock";
|
|
36
|
-
/** How long a contending invocation serializes behind the holder. */
|
|
37
|
-
export declare const TRANSITION_WAIT_BOUND_MILLIS = 60000;
|
|
38
|
-
/** The live view of one acquisition this process currently holds. */
|
|
39
|
-
export interface HeldWorkspaceTransition {
|
|
40
|
-
/** Fails when the hold is compromised; never succeeds and never ends otherwise. */
|
|
41
|
-
readonly compromised: Effect.Effect<never, WorkspaceTransitionCompromised>;
|
|
42
|
-
/** Synchronous probe for boundaries that cannot race, such as restoration. */
|
|
43
|
-
readonly isCompromised: () => boolean;
|
|
44
|
-
}
|
|
45
|
-
export declare const isWorkspaceTransitionHeldByThisInvocation: (workspaceDir: string) => boolean;
|
|
46
|
-
/** The live hold for a workspace this process acquired, when one exists. */
|
|
47
|
-
export declare const heldWorkspaceTransition: (workspaceDir: string) => HeldWorkspaceTransition | undefined;
|
|
48
|
-
export declare const transitionLockPath: (path: Path.Path, workspaceDir: string) => string;
|
|
49
|
-
/**
|
|
50
|
-
* Acquire the workspace transition lock, waiting up to the bound while
|
|
51
|
-
* another invocation holds it. Resolves `None` when acquired (the release is
|
|
52
|
-
* a scope finalizer) and `Some(contention)` when the bound elapsed.
|
|
53
|
-
*/
|
|
54
|
-
export declare const acquireWorkspaceTransitionLock: (args: {
|
|
55
|
-
readonly workspaceDir: string;
|
|
56
|
-
readonly holder: TransitionLockHolder;
|
|
57
|
-
readonly waitBoundMillis?: number;
|
|
58
|
-
/** Called once when the invocation starts waiting on another holder. */
|
|
59
|
-
readonly onWaiting?: (holder: Option.Option<TransitionLockHolder>) => Effect.Effect<void>;
|
|
60
|
-
/** Staleness/refresh override for deterministic compromise tests only. */
|
|
61
|
-
readonly timingMillis?: {
|
|
62
|
-
readonly stale: number;
|
|
63
|
-
readonly update: number;
|
|
64
|
-
};
|
|
65
|
-
}) => Effect.Effect<Option.Option<TransitionContention>, WorkspaceTransitionAcquireFailure, FileSystem.FileSystem | Path.Path | Scope.Scope>;
|
|
66
|
-
/** One invocation's view of transition admission and ownership. */
|
|
67
|
-
export interface WorkspaceTransitionLock {
|
|
68
|
-
readonly acquire: typeof acquireWorkspaceTransitionLock;
|
|
69
|
-
readonly held: typeof heldWorkspaceTransition;
|
|
70
|
-
}
|
|
71
|
-
export declare const liveWorkspaceTransitionLock: WorkspaceTransitionLock;
|
|
72
|
-
//# sourceMappingURL=transition-lock.d.ts.map
|