create-principles-disciple 1.147.18 → 1.147.19

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.
@@ -12,6 +12,48 @@ export function errnoCode(value) {
12
12
  * target with exclusive sharing surfaces as these on Windows. Transient by
13
13
  * nature — the host rewrites its settings periodically and releases. */
14
14
  const RETRYABLE_RENAME_ERRNOS = new Set(['EPERM', 'EBUSY', 'EACCES']);
15
+ /**
16
+ * The target changed after the transaction snapshot; guarded replacement was
17
+ * refused. `committed: false` means this writer did NOT replace the target.
18
+ *
19
+ * LIMIT (must not be described as a CAS): POSIX/Windows filesystems expose no
20
+ * portable compare-and-swap rename. The guard re-reads and byte-compares
21
+ * immediately before each rename, which narrows but CANNOT eliminate the
22
+ * cross-process window between that read and the rename itself. A concurrent
23
+ * write landing inside that window is not detectable here; callers must keep
24
+ * the original snapshot as manual-recovery evidence instead of claiming the
25
+ * concurrent edit survived.
26
+ */
27
+ export class AtomicReplaceConflictError extends Error {
28
+ committed = false;
29
+ filePath;
30
+ /** Private temp dir left behind when cleanup also failed (manual review). */
31
+ residueDir;
32
+ constructor(filePath, residueDir, cleanupDetail) {
33
+ const residue = residueDir !== undefined
34
+ ? `; temporary write residue preserved at ${residueDir}` +
35
+ (cleanupDetail !== undefined ? ` (${cleanupDetail})` : '')
36
+ : '';
37
+ super(`Atomic replacement refused because ${filePath} changed after the transaction snapshot${residue}. ` +
38
+ 'This is a best-effort byte guard, NOT an atomic compare-and-swap: a concurrent write landing in the final read-then-rename window cannot be ruled out.');
39
+ this.name = 'AtomicReplaceConflictError';
40
+ this.filePath = filePath;
41
+ this.residueDir = residueDir;
42
+ }
43
+ }
44
+ /** The target rename committed, but cleanup of the private temp directory failed. */
45
+ export class AtomicReplaceCleanupError extends Error {
46
+ committed = true;
47
+ filePath;
48
+ tempDir;
49
+ constructor(filePath, tempDir, cleanupError) {
50
+ const detail = cleanupError instanceof Error ? cleanupError.message : String(cleanupError);
51
+ super(`Atomic replacement committed at ${filePath}, but temporary directory cleanup failed at ${tempDir}: ${detail}`);
52
+ this.name = 'AtomicReplaceCleanupError';
53
+ this.filePath = filePath;
54
+ this.tempDir = tempDir;
55
+ }
56
+ }
15
57
  function sleepSync(milliseconds) {
16
58
  if (milliseconds <= 0)
17
59
  return;
@@ -22,7 +64,7 @@ function sleepSync(milliseconds) {
22
64
  * WorkBuddy host holds settings.json while running and a single-attempt
23
65
  * rename turned minutes of installer work into a full rollback. */
24
66
  // eslint-disable-next-line @typescript-eslint/max-params -- fs-ops seam + retry options; merging them would break every existing ops caller
25
- export function atomicReplaceTextFile(filePath, content, ops = FILE_OPS, retry = {}) {
67
+ export function atomicReplaceTextFile(filePath, content, ops = FILE_OPS, retry = {}, expectedCurrentBytes) {
26
68
  const maxAttempts = Math.max(1, retry.maxAttempts ?? 5);
27
69
  const wait = retry.sleep ?? sleepSync;
28
70
  const tempDir = ops.mkdtempSync(path.join(path.dirname(filePath), '.pd-config-write-'));
@@ -32,12 +74,35 @@ export function atomicReplaceTextFile(filePath, content, ops = FILE_OPS, retry =
32
74
  let operationError;
33
75
  try {
34
76
  fd = ops.openSync(tempPath, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL, 0o600);
35
- ops.writeFileSync(fd, content, 'utf8');
77
+ if (typeof content === 'string')
78
+ ops.writeFileSync(fd, content, 'utf8');
79
+ else
80
+ ops.writeFileSync(fd, content);
36
81
  ops.fsyncSync(fd);
37
82
  ops.closeSync(fd);
38
83
  fd = undefined;
39
84
  for (let attempt = 0;; attempt += 1) {
40
85
  try {
86
+ // A rollback caller supplies the exact bytes PD previously wrote.
87
+ // Revalidate at the commit boundary on every retry so an edit observed
88
+ // before the rename is not replaced by a stale snapshot. This is a
89
+ // best-effort guard, NOT an atomic CAS: the read and the rename are two
90
+ // separate syscalls and a non-cooperating process can write between
91
+ // them. See AtomicReplaceConflictError for the documented limit.
92
+ if (expectedCurrentBytes !== undefined) {
93
+ let currentBytes;
94
+ try {
95
+ currentBytes = Buffer.from(ops.readFileSync(filePath));
96
+ }
97
+ catch (error) {
98
+ if (errnoCode(error) === 'ENOENT')
99
+ throw new AtomicReplaceConflictError(filePath);
100
+ throw error;
101
+ }
102
+ if (!currentBytes.equals(Buffer.from(expectedCurrentBytes))) {
103
+ throw new AtomicReplaceConflictError(filePath);
104
+ }
105
+ }
41
106
  ops.renameSync(tempPath, filePath);
42
107
  operationError = undefined;
43
108
  break;
@@ -81,12 +146,22 @@ export function atomicReplaceTextFile(filePath, content, ops = FILE_OPS, retry =
81
146
  }
82
147
  }
83
148
  if (operationError !== undefined && cleanupError !== undefined) {
149
+ // A guarded conflict is the authoritative verdict (rename never committed).
150
+ // Surface it with the residue path instead of an opaque AggregateError, so
151
+ // the rollback can report the real state and preserve recovery evidence.
152
+ if (operationError instanceof AtomicReplaceConflictError) {
153
+ const detail = cleanupError instanceof Error ? cleanupError.message : String(cleanupError);
154
+ throw new AtomicReplaceConflictError(operationError.filePath, tempDir, detail);
155
+ }
84
156
  throw new AggregateError([operationError, cleanupError], 'Atomic config replacement and cleanup both failed');
85
157
  }
86
158
  if (operationError !== undefined)
87
159
  throw operationError instanceof Error ? operationError : new Error(String(operationError));
88
- if (cleanupError !== undefined)
160
+ if (cleanupError !== undefined) {
161
+ if (replaced)
162
+ throw new AtomicReplaceCleanupError(filePath, tempDir, cleanupError);
89
163
  throw cleanupError instanceof Error ? cleanupError : new Error(String(cleanupError));
164
+ }
90
165
  }
91
166
  function readLockPid(lockPath, ops) {
92
167
  try {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-principles-disciple",
3
- "version": "1.147.18",
3
+ "version": "1.147.19",
4
4
  "description": "Interactive CLI installer for Principles Disciple OpenClaw plugin",
5
5
  "type": "module",
6
6
  "bin": {