pi-daddy 0.49.4 → 0.50.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.
- package/CHANGELOG.md +56 -1
- package/README.md +218 -28
- package/dist/governance/initiative-coordinator.d.ts +58 -0
- package/dist/governance/initiative-coordinator.d.ts.map +1 -0
- package/dist/governance/initiative-coordinator.js +348 -0
- package/dist/governance/initiative-coordinator.js.map +1 -0
- package/dist/governance/initiative-effects.d.ts +20 -0
- package/dist/governance/initiative-effects.d.ts.map +1 -0
- package/dist/governance/initiative-effects.js +108 -0
- package/dist/governance/initiative-effects.js.map +1 -0
- package/dist/governance/initiative-state.d.ts +13 -0
- package/dist/governance/initiative-state.d.ts.map +1 -0
- package/dist/governance/initiative-state.js +122 -0
- package/dist/governance/initiative-state.js.map +1 -0
- package/dist/governance/operation-artifact-io.d.ts +8 -0
- package/dist/governance/operation-artifact-io.d.ts.map +1 -0
- package/dist/governance/operation-artifact-io.js +95 -0
- package/dist/governance/operation-artifact-io.js.map +1 -0
- package/dist/governance/operation-artifact-scan.d.ts +13 -0
- package/dist/governance/operation-artifact-scan.d.ts.map +1 -0
- package/dist/governance/operation-artifact-scan.js +71 -0
- package/dist/governance/operation-artifact-scan.js.map +1 -0
- package/dist/governance/operation-artifacts.d.ts +53 -0
- package/dist/governance/operation-artifacts.d.ts.map +1 -0
- package/dist/governance/operation-artifacts.js +309 -0
- package/dist/governance/operation-artifacts.js.map +1 -0
- package/dist/governance/runtime-settlement.d.ts +2 -0
- package/dist/governance/runtime-settlement.d.ts.map +1 -1
- package/dist/governance/runtime-settlement.js +26 -0
- package/dist/governance/runtime-settlement.js.map +1 -1
- package/dist/governance/workspace-acceptance.d.ts +5 -0
- package/dist/governance/workspace-acceptance.d.ts.map +1 -1
- package/dist/governance/workspace-acceptance.js +16 -0
- package/dist/governance/workspace-acceptance.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/kernel/dashboard-observation.d.ts +76 -0
- package/dist/kernel/dashboard-observation.d.ts.map +1 -0
- package/dist/kernel/dashboard-observation.js +49 -0
- package/dist/kernel/dashboard-observation.js.map +1 -0
- package/dist/kernel/dispatch-operation.d.ts +2 -0
- package/dist/kernel/dispatch-operation.d.ts.map +1 -1
- package/dist/kernel/dispatch-operation.js.map +1 -1
- package/dist/kernel/initiative.d.ts +94 -0
- package/dist/kernel/initiative.d.ts.map +1 -0
- package/dist/kernel/initiative.js +38 -0
- package/dist/kernel/initiative.js.map +1 -0
- package/dist/kernel/operation-artifact.d.ts +49 -0
- package/dist/kernel/operation-artifact.d.ts.map +1 -0
- package/dist/kernel/operation-artifact.js +4 -0
- package/dist/kernel/operation-artifact.js.map +1 -0
- package/dist/kernel/principal-binding.d.ts +4 -0
- package/dist/kernel/principal-binding.d.ts.map +1 -0
- package/dist/kernel/principal-binding.js +94 -0
- package/dist/kernel/principal-binding.js.map +1 -0
- package/dist/kernel/project-paths.d.ts +6 -0
- package/dist/kernel/project-paths.d.ts.map +1 -1
- package/dist/kernel/project-paths.js +12 -0
- package/dist/kernel/project-paths.js.map +1 -1
- package/dist/kernel/selected-definitions.d.ts.map +1 -1
- package/dist/kernel/selected-definitions.js +5 -91
- package/dist/kernel/selected-definitions.js.map +1 -1
- package/dist/kernel/workspace-diagnostics.d.ts +24 -0
- package/dist/kernel/workspace-diagnostics.d.ts.map +1 -0
- package/dist/kernel/workspace-diagnostics.js +81 -0
- package/dist/kernel/workspace-diagnostics.js.map +1 -0
- package/dist/products/activity-storage.d.ts +2 -0
- package/dist/products/activity-storage.d.ts.map +1 -0
- package/dist/products/activity-storage.js +119 -0
- package/dist/products/activity-storage.js.map +1 -0
- package/dist/products/activity-timeline.d.ts +4 -0
- package/dist/products/activity-timeline.d.ts.map +1 -1
- package/dist/products/activity-timeline.js +12 -0
- package/dist/products/activity-timeline.js.map +1 -1
- package/dist/products/dashboard-cli.d.ts +2 -0
- package/dist/products/dashboard-cli.d.ts.map +1 -1
- package/dist/products/dashboard-cli.js +41 -9
- package/dist/products/dashboard-cli.js.map +1 -1
- package/dist/products/dashboard-display-controls.d.ts.map +1 -1
- package/dist/products/dashboard-display-controls.js +30 -0
- package/dist/products/dashboard-display-controls.js.map +1 -1
- package/dist/products/dashboard-file-cache.d.ts +10 -0
- package/dist/products/dashboard-file-cache.d.ts.map +1 -0
- package/dist/products/dashboard-file-cache.js +38 -0
- package/dist/products/dashboard-file-cache.js.map +1 -0
- package/dist/products/dashboard-panels.d.ts +6 -0
- package/dist/products/dashboard-panels.d.ts.map +1 -0
- package/dist/products/dashboard-panels.js +179 -0
- package/dist/products/dashboard-panels.js.map +1 -0
- package/dist/products/dashboard-screen.d.ts +8 -0
- package/dist/products/dashboard-screen.d.ts.map +1 -1
- package/dist/products/dashboard-screen.js +86 -15
- package/dist/products/dashboard-screen.js.map +1 -1
- package/dist/products/dashboard-session-client.d.ts +16 -2
- package/dist/products/dashboard-session-client.d.ts.map +1 -1
- package/dist/products/dashboard-session-client.js +64 -11
- package/dist/products/dashboard-session-client.js.map +1 -1
- package/dist/products/dashboard-settings.d.ts.map +1 -1
- package/dist/products/dashboard-settings.js +4 -6
- package/dist/products/dashboard-settings.js.map +1 -1
- package/dist/products/ecosystem-versions.d.ts +1 -1
- package/dist/products/ecosystem-versions.d.ts.map +1 -1
- package/extensions/activity-timeline.ts +3 -1
- package/extensions/artifact-bridge.ts +130 -0
- package/extensions/dashboard-live.ts +63 -0
- package/extensions/dashboard-observation.ts +39 -0
- package/extensions/dashboard-session-server.ts +45 -3
- package/extensions/dashboard-sources.ts +124 -0
- package/extensions/delegation-artifacts.ts +125 -0
- package/extensions/delegation-operations.ts +100 -42
- package/extensions/ecosystem-versions.ts +12 -5
- package/extensions/grants-command.ts +34 -6
- package/extensions/grants.ts +9 -20
- package/extensions/initiative-dashboard.ts +143 -0
- package/extensions/initiative-runtime.ts +406 -0
- package/extensions/runtime-snapshot.ts +121 -2
- package/extensions/session-environment.ts +13 -1
- package/extensions/session.ts +28 -10
- package/extensions/workspace-diagnostics.ts +90 -0
- package/package.json +1 -1
- package/src/governance/initiative-coordinator.ts +400 -0
- package/src/governance/initiative-effects.ts +146 -0
- package/src/governance/initiative-state.ts +144 -0
- package/src/governance/operation-artifact-io.ts +103 -0
- package/src/governance/operation-artifact-scan.ts +77 -0
- package/src/governance/operation-artifacts.ts +363 -0
- package/src/governance/runtime-settlement.ts +28 -0
- package/src/governance/workspace-acceptance.ts +18 -0
- package/src/index.ts +13 -0
- package/src/kernel/dashboard-observation.ts +109 -0
- package/src/kernel/dispatch-operation.ts +2 -0
- package/src/kernel/initiative.ts +126 -0
- package/src/kernel/operation-artifact.ts +46 -0
- package/src/kernel/principal-binding.ts +104 -0
- package/src/kernel/project-paths.ts +16 -0
- package/src/kernel/selected-definitions.ts +5 -100
- package/src/kernel/workspace-diagnostics.ts +110 -0
- package/src/products/activity-storage.ts +128 -0
- package/src/products/activity-timeline.ts +11 -0
- package/src/products/dashboard-cli.ts +44 -9
- package/src/products/dashboard-display-controls.ts +27 -0
- package/src/products/dashboard-file-cache.ts +38 -0
- package/src/products/dashboard-panels.ts +217 -0
- package/src/products/dashboard-screen.ts +107 -17
- package/src/products/dashboard-session-client.ts +78 -14
- package/src/products/dashboard-settings.ts +4 -6
- package/src/products/ecosystem-versions.ts +2 -1
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
/** One coordinator per local initiative. Reuses OS-held ownership; ordinary dispatch still owns grants and writer leases. */
|
|
2
|
+
import { realpathSync, existsSync } from "node:fs";
|
|
3
|
+
import { basename } from "node:path";
|
|
4
|
+
import type { OperationArtifactOwner } from "../kernel/operation-artifact.ts";
|
|
5
|
+
import type { OperationRequest } from "../kernel/dispatch-operation.ts";
|
|
6
|
+
import { validOperationId } from "../kernel/dispatch-operation.ts";
|
|
7
|
+
import {
|
|
8
|
+
validInitiativeTask,
|
|
9
|
+
sameEffectTarget,
|
|
10
|
+
type InitiativeTask,
|
|
11
|
+
type InitiativeTaskIdentity,
|
|
12
|
+
type InitiativeRecord,
|
|
13
|
+
type InitiativeObservation,
|
|
14
|
+
type InitiativeEffectReconciliation,
|
|
15
|
+
} from "../kernel/initiative.ts";
|
|
16
|
+
import {
|
|
17
|
+
effectCurrent,
|
|
18
|
+
assertTaskPriorSettled,
|
|
19
|
+
currentEffectReceipt,
|
|
20
|
+
receiptResolves,
|
|
21
|
+
effectObservation,
|
|
22
|
+
effectIdentity,
|
|
23
|
+
exactEffect,
|
|
24
|
+
settledInitiative,
|
|
25
|
+
type InitiativeResolution,
|
|
26
|
+
} from "./initiative-effects.ts";
|
|
27
|
+
import { lockRuntimeSettlement } from "./runtime-settlement-lock.ts";
|
|
28
|
+
import { publishArtifact } from "./operation-artifact-io.ts";
|
|
29
|
+
import { initiativeLocation, initiativeRevision, readInitiativeState, saveInitiative } from "./initiative-state.ts";
|
|
30
|
+
export interface InitiativeCoordinatorOptions extends InitiativeResolution {
|
|
31
|
+
assertAuthority(): void | Promise<void>;
|
|
32
|
+
/** Optional read permission for exact owner facts; never substitutes for mutation or task-readiness authority. */
|
|
33
|
+
assertObservationAuthority?(): void | Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export interface AcquireInitiative {
|
|
36
|
+
initiativeId: string;
|
|
37
|
+
owner: OperationArtifactOwner;
|
|
38
|
+
expectedRevision: string | null;
|
|
39
|
+
recoveryOnly?: boolean;
|
|
40
|
+
}
|
|
41
|
+
export async function acquireInitiativeCoordinator(
|
|
42
|
+
directory: string,
|
|
43
|
+
request: AcquireInitiative,
|
|
44
|
+
options: InitiativeCoordinatorOptions,
|
|
45
|
+
) {
|
|
46
|
+
directory = realpathSync(directory);
|
|
47
|
+
request = {
|
|
48
|
+
initiativeId: request.initiativeId,
|
|
49
|
+
expectedRevision: request.expectedRevision,
|
|
50
|
+
recoveryOnly: request.recoveryOnly === true,
|
|
51
|
+
owner: { sessionId: request.owner.sessionId, cwd: request.owner.cwd },
|
|
52
|
+
};
|
|
53
|
+
if (
|
|
54
|
+
!request.owner.sessionId ||
|
|
55
|
+
request.owner.sessionId.length > 512 ||
|
|
56
|
+
request.owner.cwd !== realpathSync(request.owner.cwd)
|
|
57
|
+
)
|
|
58
|
+
throw Error("initiative owner identity unavailable");
|
|
59
|
+
await options.assertAuthority();
|
|
60
|
+
const { root, path } = initiativeLocation(directory, request.initiativeId, !request.recoveryOnly);
|
|
61
|
+
const lock = await lockRuntimeSettlement(path + ".lock");
|
|
62
|
+
let closed = false,
|
|
63
|
+
failed = false;
|
|
64
|
+
try {
|
|
65
|
+
await options.assertAuthority();
|
|
66
|
+
const old = readInitiativeState(directory, request.initiativeId);
|
|
67
|
+
if ((old?.revision ?? null) !== request.expectedRevision) throw Error("initiative revision conflict");
|
|
68
|
+
const marker = basename(path) + ".initialized";
|
|
69
|
+
if (!old && existsSync(path + ".initialized")) throw Error("initiative state missing; restore trusted state");
|
|
70
|
+
const settled = (record: InitiativeRecord) => settledInitiative(record, options);
|
|
71
|
+
if (request.recoveryOnly && !old) throw Error("no retained initiative to recover");
|
|
72
|
+
if (!request.recoveryOnly && old && !(await settled(old.record)))
|
|
73
|
+
throw Error("initiative reconciliation required; prior attempt or effects unresolved");
|
|
74
|
+
await options.assertAuthority();
|
|
75
|
+
if (!lock.healthy()) throw Error("initiative ownership lock lost");
|
|
76
|
+
if ((readInitiativeState(directory, request.initiativeId)?.revision ?? null) !== request.expectedRevision)
|
|
77
|
+
throw Error("initiative changed before takeover");
|
|
78
|
+
if (!old)
|
|
79
|
+
publishArtifact(root, marker, JSON.stringify({ version: 1, initiativeId: request.initiativeId }) + "\n", false);
|
|
80
|
+
const record: InitiativeRecord = request.recoveryOnly
|
|
81
|
+
? structuredClone(old!.record)
|
|
82
|
+
: {
|
|
83
|
+
version: 1,
|
|
84
|
+
initiativeId: request.initiativeId,
|
|
85
|
+
generation: (old?.record.generation ?? 0) + 1,
|
|
86
|
+
owner: { ...request.owner },
|
|
87
|
+
state: "owned",
|
|
88
|
+
tasks: structuredClone(old?.record.tasks ?? []),
|
|
89
|
+
};
|
|
90
|
+
if (!Number.isSafeInteger(record.generation)) throw Error("initiative generation exhausted");
|
|
91
|
+
let revision = initiativeRevision(record);
|
|
92
|
+
if (!request.recoveryOnly) saveInitiative(path, root, record);
|
|
93
|
+
let tail = Promise.resolve();
|
|
94
|
+
const currentGuard = (authority: () => void | Promise<void>) => async () => {
|
|
95
|
+
if (closed || failed || !lock.healthy()) throw Error("initiative coordinator is no longer current");
|
|
96
|
+
await authority();
|
|
97
|
+
if (
|
|
98
|
+
closed ||
|
|
99
|
+
failed ||
|
|
100
|
+
!lock.healthy() ||
|
|
101
|
+
readInitiativeState(directory, request.initiativeId)?.revision !== revision
|
|
102
|
+
)
|
|
103
|
+
throw Error("initiative coordinator generation or state changed");
|
|
104
|
+
};
|
|
105
|
+
const assertCurrent = currentGuard(() => options.assertAuthority());
|
|
106
|
+
const assertObservationCurrent = currentGuard(() =>
|
|
107
|
+
options.assertObservationAuthority ? options.assertObservationAuthority() : options.assertAuthority(),
|
|
108
|
+
);
|
|
109
|
+
const change = <T>(work: () => Promise<T>): Promise<T> => {
|
|
110
|
+
const next = tail.then(work);
|
|
111
|
+
tail = next.then(
|
|
112
|
+
() => undefined,
|
|
113
|
+
() => undefined,
|
|
114
|
+
);
|
|
115
|
+
return next;
|
|
116
|
+
};
|
|
117
|
+
const save = () => {
|
|
118
|
+
try {
|
|
119
|
+
saveInitiative(path, root, record);
|
|
120
|
+
revision = initiativeRevision(record);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
failed = true;
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const observeAttempts = async (task: InitiativeRecord["tasks"][number] | undefined) => {
|
|
127
|
+
const states: Awaited<ReturnType<InitiativeResolution["resolveAttempt"]>>[] = [];
|
|
128
|
+
const effects = [],
|
|
129
|
+
receipts = [];
|
|
130
|
+
// Retained attempts are bounded; never fan all historical filesystem checks out at once.
|
|
131
|
+
for (const attempt of task?.attempts ?? []) {
|
|
132
|
+
states.push(await options.resolveAttempt(structuredClone(attempt)));
|
|
133
|
+
const receipt = await currentEffectReceipt(record.initiativeId, task!, attempt, options);
|
|
134
|
+
receipts.push(receipt);
|
|
135
|
+
effects.push(receiptResolves(task!, receipt));
|
|
136
|
+
}
|
|
137
|
+
return { states, effects, receipts };
|
|
138
|
+
};
|
|
139
|
+
const taskAt = (task: InitiativeTask) => {
|
|
140
|
+
if (!validInitiativeTask(task)) throw Error("invalid initiative task");
|
|
141
|
+
const found = record.tasks.find((t) => t.taskId === task.taskId && t.taskRevision === task.taskRevision);
|
|
142
|
+
if (found && (found.effectScope !== task.effectScope || !sameEffectTarget(found.effectTarget, task.effectTarget)))
|
|
143
|
+
throw Error("task effect scope or target changed without revision");
|
|
144
|
+
return found;
|
|
145
|
+
};
|
|
146
|
+
return {
|
|
147
|
+
generation: record.generation,
|
|
148
|
+
recoveryOnly: request.recoveryOnly === true,
|
|
149
|
+
revision: () => revision,
|
|
150
|
+
metadata: () => ({
|
|
151
|
+
initiativeId: record.initiativeId,
|
|
152
|
+
generation: record.generation,
|
|
153
|
+
knownSessions: new Set([
|
|
154
|
+
record.owner.sessionId,
|
|
155
|
+
...record.tasks.flatMap((t) => t.attempts.map((a) => a.owner.sessionId)),
|
|
156
|
+
]).size,
|
|
157
|
+
recordedTasks: record.tasks.length,
|
|
158
|
+
recordedAttempts: record.tasks.reduce((n, t) => n + t.attempts.length, 0),
|
|
159
|
+
}),
|
|
160
|
+
assertCurrent,
|
|
161
|
+
assertObservationCurrent,
|
|
162
|
+
claim(task: InitiativeTask) {
|
|
163
|
+
task = structuredClone(task);
|
|
164
|
+
return change(async () => {
|
|
165
|
+
await assertCurrent();
|
|
166
|
+
if (request.recoveryOnly) throw Error("recovery-only owner cannot claim or dispatch");
|
|
167
|
+
if (taskAt(task)?.generation === record.generation) return;
|
|
168
|
+
const prior = record.tasks.filter((t) => t.taskId === task.taskId);
|
|
169
|
+
if (!(await settled({ ...record, tasks: prior }))) throw Error("task prior attempts need reconciliation");
|
|
170
|
+
await assertCurrent();
|
|
171
|
+
const found = taskAt(task);
|
|
172
|
+
if (found) found.generation = record.generation;
|
|
173
|
+
else record.tasks.push({ ...structuredClone(task), generation: record.generation, attempts: [] });
|
|
174
|
+
save();
|
|
175
|
+
});
|
|
176
|
+
},
|
|
177
|
+
begin(task: InitiativeTask, operation: OperationRequest) {
|
|
178
|
+
task = structuredClone(task);
|
|
179
|
+
return change(async () => {
|
|
180
|
+
await assertCurrent();
|
|
181
|
+
if (request.recoveryOnly) throw Error("recovery-only owner cannot claim or dispatch");
|
|
182
|
+
const found = taskAt(task);
|
|
183
|
+
if (!found || found.generation !== record.generation || !validOperationId(operation.operationId))
|
|
184
|
+
throw Error("initiative task is not claimed by current generation");
|
|
185
|
+
await assertTaskPriorSettled(record, task, options);
|
|
186
|
+
await assertCurrent();
|
|
187
|
+
const all = record.tasks.flatMap((t) => t.attempts);
|
|
188
|
+
if (
|
|
189
|
+
all.some(
|
|
190
|
+
(a) =>
|
|
191
|
+
a.owner.sessionId === request.owner.sessionId &&
|
|
192
|
+
a.owner.cwd === request.owner.cwd &&
|
|
193
|
+
a.operationId === operation.operationId,
|
|
194
|
+
)
|
|
195
|
+
)
|
|
196
|
+
throw Error("initiative operation already associated");
|
|
197
|
+
found.attempts.push({
|
|
198
|
+
owner: { ...request.owner },
|
|
199
|
+
operationId: operation.operationId,
|
|
200
|
+
executionId: operation.executionId,
|
|
201
|
+
requestDigest: operation.requestDigest,
|
|
202
|
+
state: "pending",
|
|
203
|
+
});
|
|
204
|
+
save();
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
async assertPriorAttemptsSettled(task: InitiativeTask, operation: OperationRequest) {
|
|
208
|
+
task = structuredClone(task);
|
|
209
|
+
operation = { ...operation };
|
|
210
|
+
const captured = revision;
|
|
211
|
+
await assertCurrent();
|
|
212
|
+
const found = taskAt(task);
|
|
213
|
+
const attempt = found?.attempts.find(
|
|
214
|
+
(a) =>
|
|
215
|
+
a.owner.sessionId === request.owner.sessionId &&
|
|
216
|
+
a.owner.cwd === request.owner.cwd &&
|
|
217
|
+
a.operationId === operation.operationId &&
|
|
218
|
+
a.executionId === operation.executionId &&
|
|
219
|
+
a.requestDigest === operation.requestDigest,
|
|
220
|
+
);
|
|
221
|
+
if (
|
|
222
|
+
request.recoveryOnly ||
|
|
223
|
+
!found ||
|
|
224
|
+
found.generation !== record.generation ||
|
|
225
|
+
!attempt ||
|
|
226
|
+
attempt.state === "not-admitted"
|
|
227
|
+
)
|
|
228
|
+
throw Error("exact admitted initiative attempt required before start");
|
|
229
|
+
await assertTaskPriorSettled(record, task, options, attempt);
|
|
230
|
+
await assertCurrent();
|
|
231
|
+
if (revision !== captured) throw Error("initiative changed before start");
|
|
232
|
+
},
|
|
233
|
+
notAdmitted(operation: OperationRequest) {
|
|
234
|
+
return change(async () => {
|
|
235
|
+
await assertCurrent();
|
|
236
|
+
const a = record.tasks
|
|
237
|
+
.flatMap((t) => t.attempts)
|
|
238
|
+
.find(
|
|
239
|
+
(a) =>
|
|
240
|
+
a.owner.sessionId === request.owner.sessionId &&
|
|
241
|
+
a.owner.cwd === request.owner.cwd &&
|
|
242
|
+
a.operationId === operation.operationId &&
|
|
243
|
+
a.executionId === operation.executionId,
|
|
244
|
+
);
|
|
245
|
+
if (!a) throw Error("initiative attempt missing");
|
|
246
|
+
a.state = "not-admitted";
|
|
247
|
+
save();
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
async observe(
|
|
251
|
+
input: InitiativeTaskIdentity,
|
|
252
|
+
selectedTask?: InitiativeTask,
|
|
253
|
+
): Promise<InitiativeObservation | { status: "unavailable" }> {
|
|
254
|
+
input = structuredClone(input);
|
|
255
|
+
selectedTask = selectedTask ? structuredClone(selectedTask) : undefined;
|
|
256
|
+
if (input.initiativeId !== record.initiativeId) return { status: "unavailable" };
|
|
257
|
+
const captured = revision;
|
|
258
|
+
await assertObservationCurrent();
|
|
259
|
+
const task = record.tasks.find((t) => t.taskId === input.taskId && t.taskRevision === input.taskRevision);
|
|
260
|
+
if (!task && !selectedTask && record.tasks.some((t) => t.taskId === input.taskId))
|
|
261
|
+
return { status: "unavailable" };
|
|
262
|
+
if (
|
|
263
|
+
selectedTask &&
|
|
264
|
+
(!validInitiativeTask(selectedTask) ||
|
|
265
|
+
selectedTask.taskId !== input.taskId ||
|
|
266
|
+
selectedTask.taskRevision !== input.taskRevision ||
|
|
267
|
+
(task &&
|
|
268
|
+
(task.effectScope !== selectedTask.effectScope ||
|
|
269
|
+
!sameEffectTarget(task.effectTarget, selectedTask.effectTarget))))
|
|
270
|
+
)
|
|
271
|
+
return { status: "unavailable" };
|
|
272
|
+
const { states, effects, receipts } = await observeAttempts(task);
|
|
273
|
+
const prior = record.tasks.filter((t) => t.taskId === input.taskId && t !== task);
|
|
274
|
+
const priorCurrent = await settled({ ...record, tasks: prior });
|
|
275
|
+
const target = task?.effectTarget ?? selectedTask?.effectTarget;
|
|
276
|
+
const observedEffect = effectObservation(target, task?.attempts.at(-1), states.at(-1), receipts.at(-1));
|
|
277
|
+
await assertObservationCurrent();
|
|
278
|
+
if (captured !== revision) return { status: "unavailable" };
|
|
279
|
+
const unresolved =
|
|
280
|
+
!priorCurrent ||
|
|
281
|
+
states.some((status, i) => status === "unknown" || (status !== "not-started" && !effects[i]));
|
|
282
|
+
return {
|
|
283
|
+
status: "observed",
|
|
284
|
+
...input,
|
|
285
|
+
ownerGeneration: record.generation,
|
|
286
|
+
claim: unresolved
|
|
287
|
+
? "uncertain"
|
|
288
|
+
: !task || (!request.recoveryOnly && task.generation !== record.generation && !states.includes("running"))
|
|
289
|
+
? "unclaimed"
|
|
290
|
+
: !request.recoveryOnly && task.generation === record.generation
|
|
291
|
+
? "owned"
|
|
292
|
+
: "other-owner",
|
|
293
|
+
execution: states.includes("unknown")
|
|
294
|
+
? "unknown"
|
|
295
|
+
: states.includes("running")
|
|
296
|
+
? "running"
|
|
297
|
+
: states.length
|
|
298
|
+
? "settled"
|
|
299
|
+
: "none",
|
|
300
|
+
reconciliationRequired: unresolved,
|
|
301
|
+
...(observedEffect ? { effectObservation: observedEffect } : {}),
|
|
302
|
+
async assertCurrent() {
|
|
303
|
+
await assertObservationCurrent();
|
|
304
|
+
const {
|
|
305
|
+
states: currentStates,
|
|
306
|
+
effects: currentEffects,
|
|
307
|
+
receipts: currentReceipts,
|
|
308
|
+
} = await observeAttempts(task);
|
|
309
|
+
const currentEffect = effectObservation(
|
|
310
|
+
target,
|
|
311
|
+
task?.attempts.at(-1),
|
|
312
|
+
currentStates.at(-1),
|
|
313
|
+
currentReceipts.at(-1),
|
|
314
|
+
);
|
|
315
|
+
const priorStillCurrent = await settled({ ...record, tasks: prior });
|
|
316
|
+
await assertObservationCurrent();
|
|
317
|
+
if (
|
|
318
|
+
revision !== captured ||
|
|
319
|
+
priorStillCurrent !== priorCurrent ||
|
|
320
|
+
JSON.stringify(currentStates) !== JSON.stringify(states) ||
|
|
321
|
+
JSON.stringify(currentEffects) !== JSON.stringify(effects) ||
|
|
322
|
+
JSON.stringify(currentEffect) !== JSON.stringify(observedEffect)
|
|
323
|
+
)
|
|
324
|
+
throw Error("initiative observation changed");
|
|
325
|
+
},
|
|
326
|
+
};
|
|
327
|
+
},
|
|
328
|
+
async pendingEffects() {
|
|
329
|
+
const captured = revision;
|
|
330
|
+
await assertCurrent();
|
|
331
|
+
const pending = [];
|
|
332
|
+
for (const task of record.tasks)
|
|
333
|
+
for (const attempt of task.attempts) {
|
|
334
|
+
if (task.effectScope === "local") continue;
|
|
335
|
+
const execution = await options.resolveAttempt(structuredClone(attempt));
|
|
336
|
+
if (execution !== "not-started" && !(await effectCurrent(record.initiativeId, task, attempt, options)))
|
|
337
|
+
pending.push({
|
|
338
|
+
...effectIdentity(record.initiativeId, task, attempt),
|
|
339
|
+
execution,
|
|
340
|
+
...(task.effectTarget ? { effectTarget: structuredClone(task.effectTarget) } : {}),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
await assertCurrent();
|
|
344
|
+
if (captured !== revision) throw Error("initiative observation changed");
|
|
345
|
+
return pending;
|
|
346
|
+
},
|
|
347
|
+
reconcileEffect(input: InitiativeEffectReconciliation) {
|
|
348
|
+
input = structuredClone(input);
|
|
349
|
+
return change(async () => {
|
|
350
|
+
await assertCurrent();
|
|
351
|
+
const { task, attempt } = exactEffect(record, input);
|
|
352
|
+
if (!options.assertEffectReconciled) throw Error("trusted receipt verifier unavailable");
|
|
353
|
+
if ((await options.resolveAttempt(structuredClone(attempt))) !== "settled")
|
|
354
|
+
throw Error("native attempt settlement required");
|
|
355
|
+
await options.assertEffectReconciled({
|
|
356
|
+
...effectIdentity(record.initiativeId, task, attempt),
|
|
357
|
+
receipt: structuredClone(input.receipt),
|
|
358
|
+
});
|
|
359
|
+
await assertCurrent();
|
|
360
|
+
if ((await options.resolveAttempt(structuredClone(attempt))) !== "settled")
|
|
361
|
+
throw Error("native attempt settlement changed");
|
|
362
|
+
await options.assertEffectReconciled({
|
|
363
|
+
...effectIdentity(record.initiativeId, task, attempt),
|
|
364
|
+
receipt: structuredClone(input.receipt),
|
|
365
|
+
});
|
|
366
|
+
await assertCurrent();
|
|
367
|
+
attempt.effectReceipt = structuredClone(input.receipt);
|
|
368
|
+
save();
|
|
369
|
+
const observed = effectObservation(task.effectTarget, attempt, "settled", input.receipt);
|
|
370
|
+
return {
|
|
371
|
+
reconciled: receiptResolves(task, input.receipt),
|
|
372
|
+
taskAccepted: false,
|
|
373
|
+
executionAuthorized: false,
|
|
374
|
+
...(observed ? { effectObservation: observed } : {}),
|
|
375
|
+
};
|
|
376
|
+
});
|
|
377
|
+
},
|
|
378
|
+
release() {
|
|
379
|
+
return change(async () => {
|
|
380
|
+
await assertCurrent();
|
|
381
|
+
if (!(await settled(record))) throw Error("initiative reconciliation required before release");
|
|
382
|
+
await assertCurrent();
|
|
383
|
+
record.state = "released";
|
|
384
|
+
save();
|
|
385
|
+
closed = true;
|
|
386
|
+
await lock.release();
|
|
387
|
+
});
|
|
388
|
+
},
|
|
389
|
+
/** Abandon only cooperative ownership; unresolved attempts remain durable and block takeover. */
|
|
390
|
+
async close() {
|
|
391
|
+
closed = true;
|
|
392
|
+
await lock.release();
|
|
393
|
+
},
|
|
394
|
+
};
|
|
395
|
+
} catch (error) {
|
|
396
|
+
await lock.release();
|
|
397
|
+
throw error;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
export type InitiativeCoordinator = Awaited<ReturnType<typeof acquireInitiativeCoordinator>>;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/** Bounded exact reconciliation helpers; receipt authority is supplied by the selected owner, not a model. */
|
|
2
|
+
import {
|
|
3
|
+
validEffectReceipt,
|
|
4
|
+
sameEffectTarget,
|
|
5
|
+
type InitiativeEffectReceipt,
|
|
6
|
+
type InitiativeEffectTarget,
|
|
7
|
+
type InitiativeEffectObservation,
|
|
8
|
+
type InitiativeTask,
|
|
9
|
+
type InitiativeTaskRecord,
|
|
10
|
+
type InitiativeAttempt,
|
|
11
|
+
type InitiativeRecord,
|
|
12
|
+
type InitiativeEffectIdentity,
|
|
13
|
+
type InitiativeEffectReconciliation,
|
|
14
|
+
} from "../kernel/initiative.ts";
|
|
15
|
+
export interface InitiativeResolution {
|
|
16
|
+
resolveAttempt(attempt: InitiativeAttempt): Promise<"settled" | "not-started" | "running" | "unknown">;
|
|
17
|
+
assertEffectReconciled?(input: InitiativeEffectReconciliation): void | Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
export function effectIdentity(
|
|
20
|
+
initiativeId: string,
|
|
21
|
+
task: InitiativeTaskRecord,
|
|
22
|
+
attempt: InitiativeAttempt,
|
|
23
|
+
): InitiativeEffectIdentity {
|
|
24
|
+
return {
|
|
25
|
+
initiativeId,
|
|
26
|
+
taskId: task.taskId,
|
|
27
|
+
taskRevision: task.taskRevision,
|
|
28
|
+
effectScope: task.effectScope,
|
|
29
|
+
owner: { ...attempt.owner },
|
|
30
|
+
operationId: attempt.operationId,
|
|
31
|
+
executionId: attempt.executionId,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export async function currentEffectReceipt(
|
|
35
|
+
initiativeId: string,
|
|
36
|
+
task: InitiativeTaskRecord,
|
|
37
|
+
attempt: InitiativeAttempt,
|
|
38
|
+
options: InitiativeResolution,
|
|
39
|
+
): Promise<InitiativeEffectReceipt | undefined> {
|
|
40
|
+
if (!attempt.effectReceipt || !options.assertEffectReconciled) return undefined;
|
|
41
|
+
if (
|
|
42
|
+
attempt.effectReceipt.observation &&
|
|
43
|
+
!sameEffectTarget(task.effectTarget, attempt.effectReceipt.observation.target)
|
|
44
|
+
)
|
|
45
|
+
return undefined;
|
|
46
|
+
try {
|
|
47
|
+
await options.assertEffectReconciled({
|
|
48
|
+
...effectIdentity(initiativeId, task, attempt),
|
|
49
|
+
receipt: structuredClone(attempt.effectReceipt),
|
|
50
|
+
});
|
|
51
|
+
return structuredClone(attempt.effectReceipt);
|
|
52
|
+
} catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function receiptResolves(task: InitiativeTaskRecord, receipt?: InitiativeEffectReceipt) {
|
|
57
|
+
if (receipt?.observation?.outcome === "unknown") return false;
|
|
58
|
+
return task.effectScope === "local" || (!!receipt && (!task.effectTarget || !!receipt.observation));
|
|
59
|
+
}
|
|
60
|
+
export async function effectCurrent(
|
|
61
|
+
initiativeId: string,
|
|
62
|
+
task: InitiativeTaskRecord,
|
|
63
|
+
attempt: InitiativeAttempt,
|
|
64
|
+
options: InitiativeResolution,
|
|
65
|
+
) {
|
|
66
|
+
return receiptResolves(task, await currentEffectReceipt(initiativeId, task, attempt, options));
|
|
67
|
+
}
|
|
68
|
+
/** Last exact attempt for the selected target; unknown evidence is never promoted to an applied effect. */
|
|
69
|
+
export function effectObservation(
|
|
70
|
+
target: InitiativeEffectTarget | undefined,
|
|
71
|
+
attempt: InitiativeAttempt | undefined,
|
|
72
|
+
execution: "settled" | "not-started" | "running" | "unknown" | undefined,
|
|
73
|
+
receipt?: InitiativeEffectReceipt,
|
|
74
|
+
): InitiativeEffectObservation | undefined {
|
|
75
|
+
if (!target) return undefined;
|
|
76
|
+
const identity = attempt
|
|
77
|
+
? { owner: { ...attempt.owner }, operationId: attempt.operationId, executionId: attempt.executionId }
|
|
78
|
+
: undefined;
|
|
79
|
+
if (!attempt || execution === "not-started")
|
|
80
|
+
return {
|
|
81
|
+
target: structuredClone(target),
|
|
82
|
+
outcome: "pending",
|
|
83
|
+
provenance: "not-observed",
|
|
84
|
+
...(identity ? { attempt: identity } : {}),
|
|
85
|
+
};
|
|
86
|
+
if (execution === "settled" && receipt?.observation && sameEffectTarget(target, receipt.observation.target))
|
|
87
|
+
return {
|
|
88
|
+
...structuredClone(receipt.observation),
|
|
89
|
+
receipt: { reference: receipt.reference, sha256: receipt.sha256 },
|
|
90
|
+
attempt: identity,
|
|
91
|
+
};
|
|
92
|
+
return { target: structuredClone(target), outcome: "unknown", provenance: "not-observed", attempt: identity };
|
|
93
|
+
}
|
|
94
|
+
export async function settledInitiative(record: InitiativeRecord, options: InitiativeResolution) {
|
|
95
|
+
for (const task of record.tasks)
|
|
96
|
+
for (const attempt of task.attempts) {
|
|
97
|
+
const status = await options.resolveAttempt(structuredClone(attempt));
|
|
98
|
+
if (status === "not-started") continue;
|
|
99
|
+
if (status !== "settled" || !(await effectCurrent(record.initiativeId, task, attempt, options))) return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
/** A changed task revision cannot silently repeat an already-applied exact target. */
|
|
104
|
+
export async function assertTaskPriorSettled(
|
|
105
|
+
record: InitiativeRecord,
|
|
106
|
+
task: InitiativeTask,
|
|
107
|
+
options: InitiativeResolution,
|
|
108
|
+
exclude?: InitiativeAttempt,
|
|
109
|
+
) {
|
|
110
|
+
for (const previous of record.tasks.filter((t) => t.taskId === task.taskId))
|
|
111
|
+
for (const attempt of previous.attempts) {
|
|
112
|
+
if (attempt === exclude) continue;
|
|
113
|
+
const status = await options.resolveAttempt(structuredClone(attempt));
|
|
114
|
+
if (status === "not-started") continue;
|
|
115
|
+
if (status !== "settled") throw Error("task already has unresolved attempts");
|
|
116
|
+
const receipt = await currentEffectReceipt(record.initiativeId, previous, attempt, options);
|
|
117
|
+
if (!receiptResolves(previous, receipt)) throw Error("task prior attempts need reconciliation before start");
|
|
118
|
+
if (
|
|
119
|
+
task.effectTarget &&
|
|
120
|
+
sameEffectTarget(task.effectTarget, previous.effectTarget) &&
|
|
121
|
+
receipt?.observation?.outcome === "applied"
|
|
122
|
+
)
|
|
123
|
+
throw Error("exact task target is already applied; an amended task revision does not authorize repeating it");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export function exactEffect(record: InitiativeRecord, input: InitiativeEffectReconciliation) {
|
|
127
|
+
if (input.initiativeId !== record.initiativeId || !validEffectReceipt(input.receipt))
|
|
128
|
+
throw Error("invalid effect receipt identity");
|
|
129
|
+
const task = record.tasks.find(
|
|
130
|
+
(t) => t.taskId === input.taskId && t.taskRevision === input.taskRevision && t.effectScope === input.effectScope,
|
|
131
|
+
);
|
|
132
|
+
const attempt = task?.attempts.find(
|
|
133
|
+
(a) =>
|
|
134
|
+
a.owner.sessionId === input.owner?.sessionId &&
|
|
135
|
+
a.owner.cwd === input.owner?.cwd &&
|
|
136
|
+
a.operationId === input.operationId &&
|
|
137
|
+
a.executionId === input.executionId,
|
|
138
|
+
);
|
|
139
|
+
if (!task || !attempt || task.effectScope === "local")
|
|
140
|
+
throw Error("effect attempt is outside the exact retained task");
|
|
141
|
+
if (input.receipt.observation && !sameEffectTarget(task.effectTarget, input.receipt.observation.target))
|
|
142
|
+
throw Error("effect observation target differs from selected task");
|
|
143
|
+
if (task.effectTarget && !input.receipt.observation)
|
|
144
|
+
throw Error("selected effect target requires an explicit outcome observation");
|
|
145
|
+
return { task, attempt };
|
|
146
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/** Bounded private snapshot under the existing Daddy agent state; callers hold the existing OS lock. */
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import {
|
|
4
|
+
constants,
|
|
5
|
+
openSync,
|
|
6
|
+
closeSync,
|
|
7
|
+
fsyncSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
renameSync,
|
|
10
|
+
unlinkSync,
|
|
11
|
+
realpathSync,
|
|
12
|
+
existsSync,
|
|
13
|
+
} from "node:fs";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { readBoundedTextSync } from "../kernel/bounded-read-sync.ts";
|
|
16
|
+
import { initiativeStateDir } from "../kernel/project-paths.ts";
|
|
17
|
+
import { validOperationId } from "../kernel/dispatch-operation.ts";
|
|
18
|
+
import {
|
|
19
|
+
validInitiativeId,
|
|
20
|
+
validEffectReceipt,
|
|
21
|
+
sameEffectTarget,
|
|
22
|
+
validInitiativeTask,
|
|
23
|
+
type InitiativeRecord,
|
|
24
|
+
} from "../kernel/initiative.ts";
|
|
25
|
+
import { artifactHash, privateArtifactDirectory } from "./operation-artifact-io.ts";
|
|
26
|
+
const MAX_BYTES = 512 * 1024;
|
|
27
|
+
export const initiativeRevision = (record: InitiativeRecord): string => artifactHash(JSON.stringify(record));
|
|
28
|
+
export function initiativeLocation(directory: string, id: string, create = false) {
|
|
29
|
+
if (!validInitiativeId(id)) throw Error("invalid initiative identity");
|
|
30
|
+
const root = initiativeStateDir(realpathSync(directory));
|
|
31
|
+
privateArtifactDirectory(root, create);
|
|
32
|
+
return { root, path: join(root, artifactHash(id) + ".json") };
|
|
33
|
+
}
|
|
34
|
+
export function validateInitiative(v: unknown, id: string): InitiativeRecord {
|
|
35
|
+
const r = v as InitiativeRecord;
|
|
36
|
+
if (
|
|
37
|
+
!r ||
|
|
38
|
+
r.version !== 1 ||
|
|
39
|
+
r.initiativeId !== id ||
|
|
40
|
+
!Number.isSafeInteger(r.generation) ||
|
|
41
|
+
r.generation < 1 ||
|
|
42
|
+
!r.owner ||
|
|
43
|
+
typeof r.owner.sessionId !== "string" ||
|
|
44
|
+
!r.owner.sessionId ||
|
|
45
|
+
r.owner.sessionId.length > 512 ||
|
|
46
|
+
typeof r.owner.cwd !== "string" ||
|
|
47
|
+
!r.owner.cwd.startsWith("/") ||
|
|
48
|
+
r.owner.cwd.length > 4096 ||
|
|
49
|
+
!["owned", "released"].includes(r.state) ||
|
|
50
|
+
!Array.isArray(r.tasks) ||
|
|
51
|
+
r.tasks.length > 256
|
|
52
|
+
)
|
|
53
|
+
throw Error("invalid initiative record");
|
|
54
|
+
const keys = new Set<string>(),
|
|
55
|
+
operations = new Set<string>();
|
|
56
|
+
let attempts = 0;
|
|
57
|
+
for (const t of r.tasks) {
|
|
58
|
+
const key = JSON.stringify([t.taskId, t.taskRevision]);
|
|
59
|
+
if (
|
|
60
|
+
!validInitiativeTask(t) ||
|
|
61
|
+
keys.has(key) ||
|
|
62
|
+
!Number.isSafeInteger(t.generation) ||
|
|
63
|
+
t.generation < 1 ||
|
|
64
|
+
t.generation > r.generation ||
|
|
65
|
+
!Array.isArray(t.attempts)
|
|
66
|
+
)
|
|
67
|
+
throw Error("invalid initiative task record");
|
|
68
|
+
keys.add(key);
|
|
69
|
+
for (const a of t.attempts) {
|
|
70
|
+
const key = JSON.stringify([a.owner?.sessionId, a.owner?.cwd, a.operationId]);
|
|
71
|
+
if (
|
|
72
|
+
++attempts > 1024 ||
|
|
73
|
+
operations.has(key) ||
|
|
74
|
+
!validOperationId(a.operationId) ||
|
|
75
|
+
typeof a.executionId !== "string" ||
|
|
76
|
+
!a.executionId.startsWith("exec:") ||
|
|
77
|
+
a.executionId.length > 128 ||
|
|
78
|
+
typeof a.requestDigest !== "string" ||
|
|
79
|
+
!/^[a-f0-9]{64}$/.test(a.requestDigest) ||
|
|
80
|
+
!a.owner?.sessionId ||
|
|
81
|
+
a.owner.sessionId.length > 512 ||
|
|
82
|
+
typeof a.owner.cwd !== "string" ||
|
|
83
|
+
!a.owner.cwd.startsWith("/") ||
|
|
84
|
+
a.owner.cwd.length > 4096 ||
|
|
85
|
+
!["pending", "not-admitted"].includes(a.state) ||
|
|
86
|
+
(a.effectReceipt !== undefined && !validEffectReceipt(a.effectReceipt)) ||
|
|
87
|
+
(a.effectReceipt?.observation !== undefined &&
|
|
88
|
+
!sameEffectTarget(t.effectTarget, a.effectReceipt.observation.target))
|
|
89
|
+
)
|
|
90
|
+
throw Error("invalid initiative attempt record");
|
|
91
|
+
operations.add(key);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return r;
|
|
95
|
+
}
|
|
96
|
+
export function readInitiativeState(directory: string, id: string) {
|
|
97
|
+
try {
|
|
98
|
+
const { path, root } = initiativeLocation(directory, id);
|
|
99
|
+
const record = validateInitiative(
|
|
100
|
+
JSON.parse(readBoundedTextSync(path, { maxBytes: MAX_BYTES, confinedRoot: root }).text),
|
|
101
|
+
id,
|
|
102
|
+
);
|
|
103
|
+
return { record, revision: initiativeRevision(record) };
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
106
|
+
const root = initiativeStateDir(realpathSync(directory));
|
|
107
|
+
if (existsSync(join(root, artifactHash(id) + ".json.initialized")))
|
|
108
|
+
throw Error("initiative state missing; restore trusted state");
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export function saveInitiative(path: string, root: string, record: InitiativeRecord) {
|
|
115
|
+
validateInitiative(record, record.initiativeId);
|
|
116
|
+
const text = JSON.stringify(record) + "\n";
|
|
117
|
+
if (Buffer.byteLength(text) > MAX_BYTES) throw Error("initiative record exceeds bound");
|
|
118
|
+
privateArtifactDirectory(root);
|
|
119
|
+
const temporary = path + "." + randomUUID();
|
|
120
|
+
const fd = openSync(
|
|
121
|
+
temporary,
|
|
122
|
+
constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW,
|
|
123
|
+
0o600,
|
|
124
|
+
);
|
|
125
|
+
try {
|
|
126
|
+
writeFileSync(fd, text);
|
|
127
|
+
fsyncSync(fd);
|
|
128
|
+
} finally {
|
|
129
|
+
closeSync(fd);
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
renameSync(temporary, path);
|
|
133
|
+
const parent = openSync(root, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
|
134
|
+
try {
|
|
135
|
+
fsyncSync(parent);
|
|
136
|
+
} finally {
|
|
137
|
+
closeSync(parent);
|
|
138
|
+
}
|
|
139
|
+
} finally {
|
|
140
|
+
try {
|
|
141
|
+
unlinkSync(temporary);
|
|
142
|
+
} catch {}
|
|
143
|
+
}
|
|
144
|
+
}
|