onionskin-cli 0.0.0-stage → 0.0.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.
Files changed (117) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/INSTALL.md +17 -0
  3. package/LICENSE +21 -0
  4. package/NOTICE +46 -0
  5. package/README.md +88 -2
  6. package/docs/contracts/batch.schema.json +839 -0
  7. package/docs/contracts/history.schema.json +126 -0
  8. package/docs/contracts/ops.schema.json +541 -0
  9. package/package.json +54 -4
  10. package/scripts/checker/check.py +411 -0
  11. package/scripts/checker/requirements.txt +5 -0
  12. package/scripts/install-check.mjs +9 -0
  13. package/scripts/requirements.txt +5 -0
  14. package/scripts/rewriter/apple/main.swift +135 -0
  15. package/scripts/rewriter/apple/onionskin-fm +0 -0
  16. package/scripts/rewriter/apple/onionskin-fm.json +6 -0
  17. package/skills/onionskin/SKILL.md +117 -0
  18. package/skills/onionskin/reference/batch.md +94 -0
  19. package/skills/onionskin/reference/formats.md +15 -0
  20. package/skills/onionskin/reference/ops.md +253 -0
  21. package/skills/onionskin/reference/troubleshooting.md +73 -0
  22. package/skills/onionskin/reference/voice-guide.md +86 -0
  23. package/skills/onionskin/voice.example.md +51 -0
  24. package/src/agent-detect.js +149 -0
  25. package/src/agent-events.js +159 -0
  26. package/src/agent-prompt.js +27 -0
  27. package/src/agent-runner.js +593 -0
  28. package/src/analyze.js +17 -0
  29. package/src/anchors.js +190 -0
  30. package/src/apply.js +387 -0
  31. package/src/auth.js +130 -0
  32. package/src/batch.js +322 -0
  33. package/src/check-worker.js +8 -0
  34. package/src/checker.js +293 -0
  35. package/src/cli.js +516 -0
  36. package/src/constants.js +20 -0
  37. package/src/diff.js +267 -0
  38. package/src/doctor.js +247 -0
  39. package/src/docx.js +438 -0
  40. package/src/fmt.js +30 -0
  41. package/src/fsutil.js +348 -0
  42. package/src/history-worker.js +16 -0
  43. package/src/history.js +576 -0
  44. package/src/lease.js +79 -0
  45. package/src/local.js +242 -0
  46. package/src/merge-pptx.js +198 -0
  47. package/src/merge-xlsx.js +453 -0
  48. package/src/merge.js +410 -0
  49. package/src/offload.js +34 -0
  50. package/src/ops/common.js +48 -0
  51. package/src/ops/docx-table.js +323 -0
  52. package/src/ops/docx.js +503 -0
  53. package/src/ops/engine.js +41 -0
  54. package/src/ops/pptx.js +336 -0
  55. package/src/ops/run.js +115 -0
  56. package/src/ops/runs.js +127 -0
  57. package/src/ops/schema.js +109 -0
  58. package/src/ops/xlsx.js +277 -0
  59. package/src/package.js +189 -0
  60. package/src/paths.js +64 -0
  61. package/src/person.js +66 -0
  62. package/src/pptx.js +194 -0
  63. package/src/question.js +47 -0
  64. package/src/quick-plan.js +107 -0
  65. package/src/quick.js +164 -0
  66. package/src/redline-check.js +251 -0
  67. package/src/redline-worker.js +29 -0
  68. package/src/redline.js +438 -0
  69. package/src/reply.js +410 -0
  70. package/src/review.js +274 -0
  71. package/src/rewrite-check.js +1014 -0
  72. package/src/rewriter-cleanup.js +105 -0
  73. package/src/rewriter-setup.js +104 -0
  74. package/src/rewriter.js +396 -0
  75. package/src/sanitize.js +42 -0
  76. package/src/sentence-span.js +25 -0
  77. package/src/server-entry.js +13 -0
  78. package/src/server.js +576 -0
  79. package/src/session.js +1910 -0
  80. package/src/settings.js +161 -0
  81. package/src/setup.js +160 -0
  82. package/src/stamp-words.js +10 -0
  83. package/src/stamps.js +59 -0
  84. package/src/style.js +110 -0
  85. package/src/suggest-reply.js +84 -0
  86. package/src/suggest.js +261 -0
  87. package/src/text.js +31 -0
  88. package/src/tracked-words.js +43 -0
  89. package/src/voice-guide.js +182 -0
  90. package/src/voice-learn.js +314 -0
  91. package/src/voice-sample-worker.js +95 -0
  92. package/src/voice-samples.js +329 -0
  93. package/src/voice-store.js +305 -0
  94. package/src/voice-worker.js +28 -0
  95. package/src/voice.js +297 -0
  96. package/src/xlsx.js +454 -0
  97. package/src/xml-tree.js +118 -0
  98. package/src/xml.js +127 -0
  99. package/src/zip-write.js +74 -0
  100. package/src/zip.js +88 -0
  101. package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
  102. package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
  103. package/vendor/engine/collab-BVynXMqc.js +2591 -0
  104. package/vendor/engine/collab.js +2 -0
  105. package/vendor/engine/core.js +4 -0
  106. package/vendor/engine/default-worker-DogNT09A.js +35625 -0
  107. package/vendor/engine/embed.js +107 -0
  108. package/vendor/engine/index.js +5 -0
  109. package/vendor/engine/office-engine.embed.js +184 -0
  110. package/vendor/engine/office-engine.worker.js +36030 -0
  111. package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
  112. package/vendor/engine-node/LICENSE +21 -0
  113. package/vendor/engine-node/engine-node.mjs +46618 -0
  114. package/vendor/engine.lock.json +26 -0
  115. package/web/dist/assets/index-CFISATkn.js +197 -0
  116. package/web/dist/assets/index-spXE4oTz.css +1 -0
  117. package/web/dist/index.html +14 -0
package/src/fsutil.js ADDED
@@ -0,0 +1,348 @@
1
+ import crypto from "node:crypto";
2
+ import { execFileSync } from "node:child_process";
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import { MAX_DOC_BYTES } from "./constants.js";
6
+ import { sub, writeFully, writeStateFile } from "./paths.js";
7
+
8
+ export const sha256 = (buf) => crypto.createHash("sha256").update(buf).digest("hex");
9
+
10
+ /** The file must be a regular file (not a symlink) owned by us. Returns lstat. */
11
+ export function checkRegularOwned(file) {
12
+ const st = fs.lstatSync(file);
13
+ if (st.isSymbolicLink()) throw Object.assign(new Error(`${file} is a symbolic link`), { code: "SYMLINK" });
14
+ if (!st.isFile()) throw new Error(`${file} is not a regular file`);
15
+ if (st.uid !== process.getuid()) throw new Error(`${file} is not owned by the current user`);
16
+ return st;
17
+ }
18
+
19
+ /** Reads a document, refusing by size first (an oversized file is never pulled into memory). */
20
+ export function readDoc(file, max = MAX_DOC_BYTES) {
21
+ const st = fs.statSync(file);
22
+ if (st.size > max) throw Object.assign(new Error(`${path.basename(file)} is larger than ${Math.round(max / 1024 / 1024)} MB`), { code: "TOO_LARGE" });
23
+ return fs.readFileSync(file);
24
+ }
25
+
26
+ function copyXattrs(from, to) {
27
+ if (process.platform !== "darwin") return "not applicable";
28
+ try {
29
+ const names = execFileSync("/usr/bin/xattr", [from], { encoding: "utf8" }).split("\n").filter(Boolean);
30
+ for (const n of names) {
31
+ const hex = execFileSync("/usr/bin/xattr", ["-px", n, from], { encoding: "utf8" }).replace(/\s+/g, "");
32
+ execFileSync("/usr/bin/xattr", ["-wx", n, hex, to]);
33
+ }
34
+ return `copied ${names.length}`;
35
+ } catch (err) {
36
+ return `failed: ${err.message.split("\n")[0]}`;
37
+ }
38
+ }
39
+
40
+ /** Test seams (never set in production): inject a short-writing write, a hook between the last check and the rename, a fake ACL reader. */
41
+ export const hooks = { write: null, beforeRename: null, aclOf: null };
42
+
43
+ /** The write was refused because the file is not what the caller compared against (before the write, or in the last instant before the rename). */
44
+ export class WriteConflict extends Error {
45
+ constructor(diskSha, baseSha, raced) {
46
+ super(raced ? "the file changed while it was being replaced; the newer version was kept" : "the file changed on disk since it was loaded");
47
+ this.code = "CONFLICT";
48
+ this.diskSha = diskSha;
49
+ this.baseSha = baseSha;
50
+ this.raced = !!raced;
51
+ }
52
+ }
53
+ export class LockBusy extends Error {
54
+ constructor(file) {
55
+ super(`another writer holds the lock for ${path.basename(file)}`);
56
+ this.code = "LOCK_BUSY";
57
+ }
58
+ }
59
+
60
+ const sleepSync = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
61
+ export const lockPathFor = (file) => path.join(path.dirname(file), `.${path.basename(file)}.onionskin.lock`);
62
+
63
+ /**
64
+ * Advisory lock next to the document: created exclusively, holds our pid. Every Onionskin writer (Send, agent write, commit, revert,
65
+ * standalone apply) holds it from the hash comparison through the rename. A lock whose pid is gone is stale and taken over.
66
+ * Returns the release function. Ceiling: advisory only (an editor that does not look for the lock can still write; see the re-hash before
67
+ * the rename in replaceGuarded), and the stale takeover itself is not atomic (two waiters finding the same dead lock).
68
+ */
69
+ export function acquireLock(file, { waitMs = 5000 } = {}) {
70
+ const lock = lockPathFor(file);
71
+ const deadline = Date.now() + waitMs;
72
+ for (;;) {
73
+ try {
74
+ const fd = fs.openSync(lock, "wx", 0o600);
75
+ fs.writeSync(fd, String(process.pid));
76
+ fs.closeSync(fd);
77
+ return () => {
78
+ try {
79
+ if (fs.readFileSync(lock, "utf8") === String(process.pid)) fs.unlinkSync(lock);
80
+ } catch {}
81
+ };
82
+ } catch (err) {
83
+ if (err.code !== "EEXIST") throw err;
84
+ }
85
+ let stale = false;
86
+ try {
87
+ const pid = Number(fs.readFileSync(lock, "utf8"));
88
+ if (Number.isInteger(pid) && pid > 0) {
89
+ try {
90
+ process.kill(pid, 0);
91
+ } catch (e) {
92
+ stale = e.code === "ESRCH";
93
+ }
94
+ } else stale = Date.now() - fs.statSync(lock).mtimeMs > 2000; // being created, or garbage
95
+ } catch {
96
+ continue; // released between our attempts
97
+ }
98
+ if (stale) {
99
+ try {
100
+ fs.unlinkSync(lock);
101
+ } catch {}
102
+ continue;
103
+ }
104
+ if (Date.now() > deadline) throw new LockBusy(file);
105
+ sleepSync(20);
106
+ }
107
+ }
108
+
109
+ /** The ACL entries of a file (macOS `ls -le`), "" when there are none or the platform has no such tool. */
110
+ export function aclOf(file) {
111
+ if (hooks.aclOf) return hooks.aclOf(file);
112
+ if (process.platform !== "darwin") return "";
113
+ try {
114
+ return execFileSync("/bin/ls", ["-led", file], { encoding: "utf8" })
115
+ .split("\n")
116
+ .filter((l) => /^\s*\d+: /.test(l))
117
+ .join("\n");
118
+ } catch {
119
+ return "";
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Stages the new content in a temp file next to `file`. On macOS the temp file is made with `cp -p` (mode, owner, flags, extended
125
+ * attributes and ACLs travel with the inode) and its content is then replaced; a file that HAS an ACL is only replaced when the ACL is
126
+ * verified to be on the temp file too, otherwise the replace is refused and nothing was touched. Elsewhere: O_EXCL temp file, fchmod,
127
+ * xattrs best effort (there is no ACL support on those paths; reported as "not applicable").
128
+ */
129
+ function stage(file, bytes, before) {
130
+ const dir = path.dirname(file);
131
+ const tmp = path.join(dir, `.${path.basename(file)}.${process.pid}.${crypto.randomBytes(6).toString("hex")}.tmp`);
132
+ const origAcl = aclOf(file);
133
+ let cloned = false;
134
+ if (process.platform === "darwin") {
135
+ try {
136
+ execFileSync("/bin/cp", ["-pn", file, tmp], { stdio: "ignore" });
137
+ cloned = true;
138
+ } catch {
139
+ fs.rmSync(tmp, { force: true });
140
+ if (origAcl) throw Object.assign(new Error("this file has an access control list that could not be preserved, so it was not replaced"), { code: "ACL_NOT_PRESERVED" });
141
+ }
142
+ }
143
+ let fd;
144
+ try {
145
+ if (cloned) {
146
+ fs.chmodSync(tmp, 0o600); // keeps the ACL; lets us open a read-only original for writing
147
+ fd = fs.openSync(tmp, "r+");
148
+ fs.ftruncateSync(fd, 0);
149
+ } else fd = fs.openSync(tmp, "wx", 0o600);
150
+ writeFully(fd, bytes, hooks.write || fs.writeSync);
151
+ fs.fchmodSync(fd, before.mode & 0o7777);
152
+ fs.fsyncSync(fd);
153
+ fs.closeSync(fd);
154
+ fd = undefined;
155
+ let xattrs = "preserved by cp -p";
156
+ if (!cloned) xattrs = copyXattrs(file, tmp);
157
+ let acl = process.platform === "darwin" ? "none" : "not applicable";
158
+ if (origAcl) {
159
+ if (aclOf(tmp) !== origAcl) throw Object.assign(new Error("this file has an access control list that could not be preserved, so it was not replaced"), { code: "ACL_NOT_PRESERVED" });
160
+ acl = "preserved";
161
+ }
162
+ return { tmp, info: { mode: before.mode & 0o7777, xattrs, acl } };
163
+ } catch (err) {
164
+ if (fd !== undefined) {
165
+ try {
166
+ fs.closeSync(fd);
167
+ } catch {}
168
+ }
169
+ fs.rmSync(tmp, { force: true });
170
+ throw err;
171
+ }
172
+ }
173
+
174
+ /**
175
+ * The one way a document gets replaced. Holds the advisory lock from the hash comparison through the rename:
176
+ * 1. compare the disk hash with `expectSha` (when given) -> WriteConflict; `snapshotBefore(diskBytes)` stores what is about to be replaced;
177
+ * 2. stage the new bytes (full write, fsync, mode, xattrs, ACL);
178
+ * 3. re-hash the target right before the rename. If it changed, DO NOT overwrite: `snapshotNewer(newBytes)` keeps the newer version and
179
+ * WriteConflict (raced) is thrown;
180
+ * 4. rename, fsync the directory.
181
+ * Ceiling: a writer that ignores the lock can still land between step 3 and the rename (microseconds, one syscall); that version is replaced
182
+ * without a snapshot. Upgrade path: none without kernel support (renameat2 RENAME_EXCHANGE is Linux only).
183
+ */
184
+ export function replaceGuarded(file, bytes, { expectSha = null, snapshotBefore = null, snapshotNewer = null, waitMs } = {}) {
185
+ const release = acquireLock(file, { waitMs });
186
+ try {
187
+ const before = checkRegularOwned(file);
188
+ const disk = readDoc(file);
189
+ const diskSha = sha256(disk);
190
+ if (expectSha != null && diskSha !== expectSha) throw new WriteConflict(diskSha, expectSha, false);
191
+ snapshotBefore?.(disk);
192
+ const { tmp, info } = stage(file, bytes, before);
193
+ try {
194
+ hooks.beforeRename?.({ file, tmp });
195
+ checkRegularOwned(file);
196
+ const now = readDoc(file);
197
+ const nowSha = sha256(now);
198
+ if (nowSha !== diskSha) {
199
+ try {
200
+ snapshotNewer?.(now);
201
+ } catch {} // the newer bytes stay on disk untouched either way
202
+ throw new WriteConflict(nowSha, expectSha ?? diskSha, true);
203
+ }
204
+ fs.renameSync(tmp, file);
205
+ } catch (err) {
206
+ fs.rmSync(tmp, { force: true });
207
+ throw err;
208
+ }
209
+ const dfd = fs.openSync(path.dirname(file), "r");
210
+ try {
211
+ fs.fsyncSync(dfd);
212
+ } finally {
213
+ fs.closeSync(dfd);
214
+ }
215
+ return { ...info, previousSha: diskSha };
216
+ } finally {
217
+ release();
218
+ }
219
+ }
220
+
221
+ const two = (n) => String(n).padStart(2, "0");
222
+ /**
223
+ * Item 12: the version Onionskin holds, saved NEXT TO the original when the original was changed by someone else. The name is built from
224
+ * the session's own (already validated) file path, never from document content: `<name> (Onionskin copy YYYY-MM-DD HHMM).<ext>` in local time,
225
+ * then ` 2`, ` 3` ... when that name exists. The copy gets the original's mode, extended attributes and ACL (staged like a replace) and is
226
+ * linked into place with link(2), which fails if the name exists: an existing file is never overwritten. Throws (EACCES, EROFS ...) when the
227
+ * folder is not writable; the caller then offers the browser download instead. `label` names the kind of copy ("tracked changes" for the
228
+ * tracked-changes copy: `<name> (tracked changes YYYY-MM-DD HHMM).docx`); it is a constant of the caller, never document text.
229
+ * Returns {name, path}.
230
+ */
231
+ export function writeSiblingCopy(file, bytes, { now = new Date(), label = "Onionskin copy" } = {}) {
232
+ const dir = path.dirname(file);
233
+ const ext = path.extname(file);
234
+ const stem = path.basename(file, ext);
235
+ const stamp = `${now.getFullYear()}-${two(now.getMonth() + 1)}-${two(now.getDate())} ${two(now.getHours())}${two(now.getMinutes())}`;
236
+ let tmp;
237
+ try {
238
+ tmp = stage(file, bytes, checkRegularOwned(file)).tmp; // same permissions as the original
239
+ } catch (err) {
240
+ if (err.code === "ACL_NOT_PRESERVED") throw err;
241
+ if (err.code === "ENOENT") {
242
+ // the original was removed: a plain private copy
243
+ tmp = path.join(dir, `.${path.basename(file)}.${process.pid}.${crypto.randomBytes(6).toString("hex")}.tmp`);
244
+ const fd = fs.openSync(tmp, "wx", 0o600);
245
+ try {
246
+ writeFully(fd, bytes, hooks.write || fs.writeSync);
247
+ fs.fsyncSync(fd);
248
+ } finally {
249
+ fs.closeSync(fd);
250
+ }
251
+ } else throw err;
252
+ }
253
+ try {
254
+ for (let n = 1; n <= 1000; n++) {
255
+ const name = `${stem} (${label} ${stamp})${n > 1 ? ` ${n}` : ""}${ext}`;
256
+ const dest = path.join(dir, name);
257
+ try {
258
+ fs.linkSync(tmp, dest);
259
+ return { name, path: dest };
260
+ } catch (err) {
261
+ if (err.code !== "EEXIST") throw err;
262
+ }
263
+ }
264
+ throw Object.assign(new Error("could not find a free name for the copy"), { code: "EEXIST" });
265
+ } finally {
266
+ fs.rmSync(tmp, { force: true });
267
+ }
268
+ }
269
+
270
+ /** Atomic replace without a compare (callers that have no base hash). Same lock, re-hash and staging as replaceGuarded. */
271
+ export const atomicReplace = (file, bytes, opts = {}) => replaceGuarded(file, bytes, opts);
272
+
273
+ // ---- snapshots ----------------------------------------------------------------------------------------------
274
+
275
+ const SNAPSHOT_KEEP = 20;
276
+ const SNAPSHOT_MAX_BYTES = 2 * 1024 ** 3;
277
+
278
+ export class Snapshots {
279
+ constructor(key, { keep = SNAPSHOT_KEEP, maxBytes = SNAPSHOT_MAX_BYTES } = {}) {
280
+ this.dir = sub("snapshots", key);
281
+ this.keep = keep;
282
+ this.maxBytes = maxBytes;
283
+ this.indexFile = path.join(this.dir, "index.json");
284
+ }
285
+ index() {
286
+ try {
287
+ return JSON.parse(fs.readFileSync(this.indexFile, "utf8"));
288
+ } catch {
289
+ return [];
290
+ }
291
+ }
292
+ blob(sha) {
293
+ return path.join(this.dir, `${sha}.bin`);
294
+ }
295
+ has(sha) {
296
+ return fs.existsSync(this.blob(sha));
297
+ }
298
+ read(sha) {
299
+ return fs.readFileSync(this.blob(sha));
300
+ }
301
+ /** Content-addressed. `original` marks the first-ever version, which is never pruned. */
302
+ add(bytes, label, { original = false } = {}) {
303
+ const sha = sha256(bytes);
304
+ const idx = this.index();
305
+ const free = fs.statfsSync(this.dir);
306
+ if (!this.has(sha) && free.bavail * free.bsize < bytes.length * 2 + 100 * 1024 * 1024) throw Object.assign(new Error("not enough free disk space for a snapshot"), { code: "ENOSPC_SNAP" });
307
+ if (!this.has(sha)) writeStateFile(this.blob(sha), bytes);
308
+ const existing = idx.find((e) => e.sha === sha);
309
+ if (existing) {
310
+ if (!existing.original) existing.label = label;
311
+ existing.at = Date.now();
312
+ if (original) existing.original = true;
313
+ } else idx.push({ sha, at: Date.now(), label, size: bytes.length, original: !!original });
314
+ this.save(this.prune(idx));
315
+ return sha;
316
+ }
317
+ /** marks a stored version as the newest so pruning drops others first (the pre-agent version must outlive agent writes) */
318
+ touch(sha) {
319
+ const idx = this.index();
320
+ const e = idx.find((x) => x.sha === sha);
321
+ if (!e) return false;
322
+ e.at = Date.now();
323
+ this.save(idx);
324
+ return true;
325
+ }
326
+ prune(idx) {
327
+ const keepable = idx.filter((e) => !e.original).sort((a, b) => a.at - b.at);
328
+ const drop = new Set();
329
+ while (keepable.length - drop.size > this.keep) drop.add(keepable[drop.size].sha);
330
+ let total = idx.reduce((n, e) => n + (drop.has(e.sha) ? 0 : e.size), 0);
331
+ for (const e of keepable) {
332
+ if (total <= this.maxBytes) break;
333
+ if (!drop.has(e.sha)) {
334
+ drop.add(e.sha);
335
+ total -= e.size;
336
+ }
337
+ }
338
+ for (const sha of drop) {
339
+ try {
340
+ fs.unlinkSync(this.blob(sha));
341
+ } catch {}
342
+ }
343
+ return idx.filter((e) => !drop.has(e.sha));
344
+ }
345
+ save(idx) {
346
+ writeStateFile(this.indexFile, JSON.stringify(idx, null, 2));
347
+ }
348
+ }
@@ -0,0 +1,16 @@
1
+ import fs from "node:fs";
2
+ import { parentPort, workerData } from "node:worker_threads";
3
+ import { compareBytes } from "./history.js";
4
+
5
+ // One message back: a result per job, in order. A version that cannot be read or parsed fails its own job only.
6
+ const bytes = new Map(); // blob path -> Buffer, so a version shared by two pairs is read (and parsed) once
7
+ const read = (f) => bytes.get(f) || (bytes.set(f, fs.readFileSync(f)), bytes.get(f));
8
+ parentPort.postMessage(
9
+ workerData.jobs.map((j) => {
10
+ try {
11
+ return { ok: true, result: compareBytes(j.format, read(j.before), read(j.after), { places: j.places }) };
12
+ } catch (err) {
13
+ return { ok: false, error: String(err?.message || err) };
14
+ }
15
+ })
16
+ );