onionskin-cli 0.0.0-stage → 0.0.2
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 +144 -0
- package/INSTALL.md +25 -0
- package/LICENSE +21 -0
- package/NOTICE +49 -0
- package/README.md +148 -2
- package/docs/contracts/batch.schema.json +839 -0
- package/docs/contracts/history.schema.json +126 -0
- package/docs/contracts/ops.schema.json +541 -0
- package/package.json +67 -4
- package/scripts/checker/check.py +411 -0
- package/scripts/checker/requirements.txt +5 -0
- package/scripts/install-check.mjs +9 -0
- package/scripts/requirements.txt +5 -0
- package/scripts/rewriter/apple/main.swift +135 -0
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +6 -0
- package/skills/onionskin/SKILL.md +117 -0
- package/skills/onionskin/reference/batch.md +94 -0
- package/skills/onionskin/reference/formats.md +15 -0
- package/skills/onionskin/reference/ops.md +253 -0
- package/skills/onionskin/reference/troubleshooting.md +73 -0
- package/skills/onionskin/reference/voice-guide.md +86 -0
- package/skills/onionskin/voice.example.md +51 -0
- package/src/agent-detect.js +149 -0
- package/src/agent-events.js +159 -0
- package/src/agent-prompt.js +27 -0
- package/src/agent-runner.js +593 -0
- package/src/analyze.js +17 -0
- package/src/anchors.js +190 -0
- package/src/apply.js +387 -0
- package/src/auth.js +130 -0
- package/src/batch.js +322 -0
- package/src/check-worker.js +8 -0
- package/src/checker.js +293 -0
- package/src/cli.js +516 -0
- package/src/constants.js +20 -0
- package/src/diff.js +267 -0
- package/src/doctor.js +288 -0
- package/src/docx.js +438 -0
- package/src/fmt.js +30 -0
- package/src/fsutil.js +348 -0
- package/src/history-worker.js +16 -0
- package/src/history.js +576 -0
- package/src/lease.js +79 -0
- package/src/local.js +242 -0
- package/src/merge-pptx.js +198 -0
- package/src/merge-xlsx.js +453 -0
- package/src/merge.js +410 -0
- package/src/offload.js +34 -0
- package/src/ops/common.js +48 -0
- package/src/ops/docx-table.js +323 -0
- package/src/ops/docx.js +503 -0
- package/src/ops/engine.js +41 -0
- package/src/ops/pptx.js +336 -0
- package/src/ops/run.js +115 -0
- package/src/ops/runs.js +127 -0
- package/src/ops/schema.js +109 -0
- package/src/ops/xlsx.js +277 -0
- package/src/package.js +189 -0
- package/src/paths.js +64 -0
- package/src/person.js +66 -0
- package/src/pptx.js +194 -0
- package/src/question.js +47 -0
- package/src/quick-plan.js +107 -0
- package/src/quick.js +164 -0
- package/src/redline-check.js +251 -0
- package/src/redline-worker.js +29 -0
- package/src/redline.js +438 -0
- package/src/reply.js +410 -0
- package/src/review.js +274 -0
- package/src/rewrite-check.js +1014 -0
- package/src/rewriter-cleanup.js +105 -0
- package/src/rewriter-setup.js +104 -0
- package/src/rewriter.js +396 -0
- package/src/sanitize.js +42 -0
- package/src/sentence-span.js +25 -0
- package/src/server-entry.js +13 -0
- package/src/server.js +576 -0
- package/src/session.js +1912 -0
- package/src/settings.js +161 -0
- package/src/setup.js +199 -0
- package/src/stamp-words.js +10 -0
- package/src/stamps.js +59 -0
- package/src/style.js +110 -0
- package/src/suggest-reply.js +84 -0
- package/src/suggest.js +261 -0
- package/src/text.js +31 -0
- package/src/tracked-words.js +43 -0
- package/src/voice-guide.js +182 -0
- package/src/voice-learn.js +314 -0
- package/src/voice-sample-worker.js +95 -0
- package/src/voice-samples.js +329 -0
- package/src/voice-store.js +305 -0
- package/src/voice-worker.js +28 -0
- package/src/voice.js +297 -0
- package/src/xlsx.js +454 -0
- package/src/xml-tree.js +118 -0
- package/src/xml.js +127 -0
- package/src/zip-write.js +74 -0
- package/src/zip.js +88 -0
- package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
- package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
- package/vendor/engine/collab-BVynXMqc.js +2591 -0
- package/vendor/engine/collab.js +2 -0
- package/vendor/engine/core.js +4 -0
- package/vendor/engine/default-worker-DogNT09A.js +35625 -0
- package/vendor/engine/embed.js +107 -0
- package/vendor/engine/index.js +5 -0
- package/vendor/engine/office-engine.embed.js +184 -0
- package/vendor/engine/office-engine.worker.js +36030 -0
- package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
- package/vendor/engine-node/LICENSE +21 -0
- package/vendor/engine-node/engine-node.mjs +46618 -0
- package/vendor/engine.lock.json +26 -0
- package/web/dist/assets/index-BUtofdtr.js +197 -0
- package/web/dist/assets/index-Dfizlg0i.css +1 -0
- package/web/dist/index.html +14 -0
package/src/session.js
ADDED
|
@@ -0,0 +1,1912 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { REFUSED_EXTENSIONS, FORMATS, MAX_DOC_BYTES, PRODUCT, env } from "./constants.js";
|
|
6
|
+
import { buildDurable, readUnits, resolveDurableAny, resolveView, viewDurable } from "./anchors.js";
|
|
7
|
+
import { durableFromEngine, headingBefore, resolveDurable } from "./docx.js";
|
|
8
|
+
import { checkRegularOwned, readDoc, replaceGuarded, sha256, Snapshots, WriteConflict, writeSiblingCopy } from "./fsutil.js";
|
|
9
|
+
import { buildBatch, compactBatch, withOutline } from "./batch.js";
|
|
10
|
+
import { styleChangedOutside, styleNote } from "./style.js";
|
|
11
|
+
import { guideChangedOutside, voiceGuide, writeDraft } from "./voice-store.js";
|
|
12
|
+
import { cleanVoiceDraft } from "./voice-guide.js";
|
|
13
|
+
import { voiceJob } from "./voice-learn.js";
|
|
14
|
+
import { clearTickets, saveTicket, ticketOf } from "./lease.js";
|
|
15
|
+
import { analyze } from "./analyze.js";
|
|
16
|
+
import { deepNotice, validatePackage } from "./package.js";
|
|
17
|
+
import { execFile } from "node:child_process";
|
|
18
|
+
import { sessionKey, stateDir, sub, writeStateFile } from "./paths.js";
|
|
19
|
+
import { cleanFlag, hasControlChars, untrusted, userText } from "./sanitize.js";
|
|
20
|
+
import { addUpdate, historyDetail, historyList, visibleUpdates } from "./history.js";
|
|
21
|
+
import { BAD_QUESTION_NOTE, QUESTIONS_PER_BATCH, cleanQuestion } from "./question.js";
|
|
22
|
+
import { validateOps } from "./ops/schema.js";
|
|
23
|
+
import { Worker } from "node:worker_threads";
|
|
24
|
+
import { AUTHORS, wordDate } from "./redline.js";
|
|
25
|
+
import { deepRedline } from "./checker.js";
|
|
26
|
+
import { TRACKED, formattingText, savedText, unsupportedText } from "./tracked-words.js";
|
|
27
|
+
import { LOCAL_HOLDER } from "./local.js";
|
|
28
|
+
import { bgHolder, isAgentHolder, isBgHolder, termHolder, TERM_PREFIX } from "./agent-detect.js";
|
|
29
|
+
import { OUTLINE_FILE_MAX, REVIEW_ASKS, REVIEW_KINDS, SUGGESTIONS_MAX, UNITS_MAX, cardsFrom, outlineOf, reviewSummary, unitsOf } from "./review.js";
|
|
30
|
+
import { VOICE_BATCH_MAX_CHARS } from "./voice-samples.js";
|
|
31
|
+
import { STAMP_WORDS } from "./stamp-words.js";
|
|
32
|
+
import { USE_SUMMARY, buildUseReply, placeOf } from "./suggest-reply.js";
|
|
33
|
+
|
|
34
|
+
/** The line under a comment whose quick fixes could not be written on this Mac (docs/contracts/routes.md). */
|
|
35
|
+
export const LOCAL_HINT = "This could not be changed on this Mac, so nothing was written. Press Send to ask the agent.";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A batch that may not write the file: "review" (src/review.js) or "voice" ("Build my voice guide"), from the batch itself so it holds
|
|
39
|
+
* after a restart; null for an ordinary batch. While one is delivered, apply and commit are refused and `done` takes its own branch.
|
|
40
|
+
*/
|
|
41
|
+
const readOnlyKind = (batch) => (batch?.instructions?.voice_request ? "voice" : batch?.instructions?.review ? "review" : null);
|
|
42
|
+
const READ_ONLY = {
|
|
43
|
+
review: "this batch is a review: nothing is written. Answer with `reply` and `suggest`.",
|
|
44
|
+
voice: "this batch asks for a draft of the voice guide: nothing is written. Answer with `reply` and `voice_draft`.",
|
|
45
|
+
};
|
|
46
|
+
// the activity summary of a voice run: fixed text, never the agent's own summary
|
|
47
|
+
/** The flag text for a file changed behind the lease by a direct save, in the words of whoever made the change (a run on this Mac has no agent). */
|
|
48
|
+
const directSave = (local) => `The file changed ${local ? "while the change was made on this Mac" : "during the agent's turn"} by a path other than apply or commit (a direct save). Nothing extra was copied: the earlier version is stored; Revert restores it.`;
|
|
49
|
+
const VOICE_KEPT = "The agent wrote a draft of your voice guide.";
|
|
50
|
+
const VOICE_REFUSED = "The agent's draft could not be shown.";
|
|
51
|
+
const VOICE_REPLACED = "Your Send replaced the voice guide request that was waiting for the agent. Build it again when you are ready.";
|
|
52
|
+
|
|
53
|
+
export class HttpError extends Error {
|
|
54
|
+
constructor(status, message, extra = {}) {
|
|
55
|
+
super(message);
|
|
56
|
+
this.status = status;
|
|
57
|
+
this.extra = extra;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const LEASE_MS = () => Number(env("LEASE_MS", 15 * 60 * 1000));
|
|
62
|
+
const MAX_BYTES = MAX_DOC_BYTES;
|
|
63
|
+
/** "Show in folder" exists on macOS (`open -R`); elsewhere the page shows the path. ONIONSKIN_NO_OPEN=1 turns it off, ONIONSKIN_OPEN_STUB=<file> records instead of launching. */
|
|
64
|
+
const canReveal = () => !!env("OPEN_STUB") || (process.platform === "darwin" && !env("NO_OPEN"));
|
|
65
|
+
/** readDoc with the size refusal as an HTTP error */
|
|
66
|
+
function readCapped(file) {
|
|
67
|
+
try {
|
|
68
|
+
return readDoc(file);
|
|
69
|
+
} catch (err) {
|
|
70
|
+
if (err.code === "TOO_LARGE") throw new HttpError(413, `document too large: ${err.message}`, { code: "too_large" });
|
|
71
|
+
throw err;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const num = (v) => (Number.isFinite(Number(v)) ? Math.round(Number(v) * 1000) / 1000 : 0);
|
|
76
|
+
/** A pin: x/y as fractions of the element it sits on (`yPx` is kept for DOCX paragraphs); `on` says what that element is. */
|
|
77
|
+
function cleanPin(p) {
|
|
78
|
+
const out = { xFrac: num(p.xFrac), yPx: num(p.yPx) };
|
|
79
|
+
if (p.yFrac != null) out.yFrac = num(p.yFrac);
|
|
80
|
+
if (["paragraph", "cell", "shape", "slide"].includes(p.on)) out.on = p.on;
|
|
81
|
+
if (p.sxFrac != null) {
|
|
82
|
+
out.sxFrac = num(p.sxFrac);
|
|
83
|
+
out.syFrac = num(p.syFrac);
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const APPLY_STATUS = new Set(["refused", "skipped_protected", "anchor_not_found", "expect_mismatch", "not_applied"]);
|
|
89
|
+
// the three-way merge of each format (loaded on first use: they pull in the engine)
|
|
90
|
+
const MERGERS = { docx: { module: "./merge.js", fn: "mergeDocx" }, xlsx: { module: "./merge-xlsx.js", fn: "mergeXlsx" }, pptx: { module: "./merge-pptx.js", fn: "mergePptx" } };
|
|
91
|
+
const HELD_SHOWN = 5; // items the page's "not in the document" message lists (web/src/Banners.jsx)
|
|
92
|
+
const WRITES_KEPT = 20; // writes per file the merge can trace (src/merge-xlsx.js agentChanges); an older base finds no record and is handed back
|
|
93
|
+
/** What the page says the user did during the run: kinds and places only, used to hold edits back or to name what is not carried, never to write. */
|
|
94
|
+
const editKinds = (edits) => (Array.isArray(edits) ? edits.slice(0, 2000) : []).map((e) => ({ kind: String(e?.kind ?? "").slice(0, 40), sheet: typeof e?.sheet === "string" ? e.sheet.slice(0, 100) : null, slide: Number.isInteger(e?.slide) ? e.slide : null }));
|
|
95
|
+
/** One op `apply` did not apply, as the page shows it: codes and short reasons only (never document text). */
|
|
96
|
+
function cleanProblem(p, run = "") {
|
|
97
|
+
return {
|
|
98
|
+
key: untrusted(p?.key || `${p?.index ?? ""}:${p?.op ?? ""}`, 80),
|
|
99
|
+
run: untrusted(run, 40),
|
|
100
|
+
op: untrusted(p?.op, 40),
|
|
101
|
+
status: APPLY_STATUS.has(p?.status) ? p.status : "refused",
|
|
102
|
+
code: p?.code ? untrusted(p.code, 60) : null,
|
|
103
|
+
reason: untrusted(p?.reason, 200),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function shellQuote(s) {
|
|
108
|
+
return `'${String(s).replace(/'/g, `'\\''`)}'`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A stored durable anchor re-resolved against `units` (the bytes the agent will read next); null when the unit is gone. Without a durable, one is built from the browser's anchor. */
|
|
112
|
+
function reanchor(units, c) {
|
|
113
|
+
const d = c.durable;
|
|
114
|
+
if (!d) return buildDurable(units, c.anchor);
|
|
115
|
+
const r = resolveDurableAny(units, d);
|
|
116
|
+
if (r.method === "none") return null;
|
|
117
|
+
if (units.format === "docx") return { ...d, ...r, format: "docx", quote: d.quote, prefix: d.prefix, suffix: d.suffix, paraText: r.text, engineDelta: r.engineDelta };
|
|
118
|
+
if (units.format === "xlsx") return buildDurable(units, { location: { sheetIndex: r.sheetIndex, sheetName: r.sheetName, range: r.range } });
|
|
119
|
+
return buildDurable(units, { location: { slideIndex: r.slideIndex, slideId: r.slideId, shapeId: r.shapeId ?? undefined, shapeName: r.shapeName, cell: d.cell || undefined }, quote: d.quote, prefix: d.prefix, suffix: d.suffix, hitKind: d.quote ? undefined : "shape" });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* All session state and transitions, with no HTTP in it.
|
|
124
|
+
* States (derived): idle, listening, working, queued, stranded, closed. poll results add timeout and superseded.
|
|
125
|
+
* - Send with a poller waiting: batch delivered, session `working`, document locked.
|
|
126
|
+
* - Send with nobody polling: batch kept as `pending`, session `stranded`, document stays editable.
|
|
127
|
+
* - Send while working: comments only, kept as `queued`; released to the next poller after `done`.
|
|
128
|
+
* - Lease expiry or Take back: `delivered` is withdrawn, the document unlocks.
|
|
129
|
+
*/
|
|
130
|
+
export class Sessions extends EventEmitter {
|
|
131
|
+
// delivered run -> styleChangedOutside() when it went out; memory only, so a run reloaded after a restart has none and is not flagged
|
|
132
|
+
#styleSeen = new WeakMap();
|
|
133
|
+
// delivered run -> guideChangedOutside() when it went out (the same idea for voice-guide.json); taking it also arms the guide's guard
|
|
134
|
+
#guideSeen = new WeakMap();
|
|
135
|
+
constructor({ now = Date.now } = {}) {
|
|
136
|
+
super();
|
|
137
|
+
this.now = now;
|
|
138
|
+
this.map = new Map(); // key -> session
|
|
139
|
+
this.createdCopies = new Set(); // absolute paths of conflict copies THIS server wrote: the only paths `showCopy` will reveal
|
|
140
|
+
this.editors = new Map(); // key -> id of the one browser tab that may edit (in memory only: a tab's id lives as long as its page)
|
|
141
|
+
this.briefed = new Map(); // key -> Set of holders that already received a complete batch in this session (memory only: a restarted server briefs again)
|
|
142
|
+
// key -> how the file got from one version to the next, oldest first: {from, to, route: "apply", ops, partial, anchors} for an agent's
|
|
143
|
+
// apply, {from, to, route: "commit"} for a commit, {from, to, route: "merge", stable} for a pass of the user's merge. The merge reads it
|
|
144
|
+
// to know whether cell addresses still hold. Memory only (ops carry document text): after a restart there is no record, and edits are handed back.
|
|
145
|
+
this.writes = new Map();
|
|
146
|
+
this.exporting = new Set(); // keys with a tracked-changes copy being made: one at a time per file
|
|
147
|
+
this.local = null; // the local runner's {wants(s, batch)} (src/local.js), set by the server; null: every batch goes to an agent
|
|
148
|
+
this.agent = null; // the background agent runner's {viewFor(key)} (src/agent-runner.js), set by the server; null: no background agent
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ---- one editable tab per file -----------------------------------------------------------------------------
|
|
152
|
+
/** A tab asks to be the editor. The first one wins; `takeover` is the user's explicit "Edit here instead". Returns whether this tab is the editor. */
|
|
153
|
+
claimEditor(key, tab, { takeover = false } = {}) {
|
|
154
|
+
const s = this.get(key);
|
|
155
|
+
const cur = this.editors.get(key);
|
|
156
|
+
if (s.closed) return false; // a closed session has no editor; the tab that reopens it claims it
|
|
157
|
+
if (cur && cur !== tab && !takeover) return false;
|
|
158
|
+
if (cur !== tab) {
|
|
159
|
+
this.editors.set(key, tab);
|
|
160
|
+
this.emit("change", s, "state");
|
|
161
|
+
}
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The tab's event stream ended (closed, reloaded): the file has no editor until a tab claims it. */
|
|
166
|
+
releaseEditor(key, tab) {
|
|
167
|
+
if (this.editors.get(key) !== tab) return;
|
|
168
|
+
this.editors.delete(key);
|
|
169
|
+
const s = this.map.get(key);
|
|
170
|
+
if (s) this.emit("change", s, "state");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Browser routes that change the session refuse a tab that is not the editor (when a tab holds the file). */
|
|
174
|
+
requireEditor(key, tab) {
|
|
175
|
+
const cur = this.editors.get(key);
|
|
176
|
+
if (cur && cur !== tab) throw new HttpError(409, "This file is open for editing in another tab. Use Edit here instead to take over.", { code: "not_editor" });
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ---- open ------------------------------------------------------------------------------------------------
|
|
180
|
+
/** `opts.agent === "background"`: the person opened it from a terminal, so the server's background agent works on it (stored on the session). */
|
|
181
|
+
open(input, opts = {}) {
|
|
182
|
+
let real;
|
|
183
|
+
try {
|
|
184
|
+
real = fs.realpathSync(path.resolve(input));
|
|
185
|
+
} catch {
|
|
186
|
+
throw new HttpError(404, `File not found: ${JSON.stringify(untrusted(input, 200))}`);
|
|
187
|
+
}
|
|
188
|
+
// a path with control or bidi characters would sit in the commands printed for the agent: refuse it up front
|
|
189
|
+
if (hasControlChars(real)) throw new HttpError(400, "This file's path contains control or bidirectional-text characters. Rename or move the file and open it again.", { code: "unsafe_path" });
|
|
190
|
+
const ext = path.extname(real).toLowerCase();
|
|
191
|
+
if (REFUSED_EXTENSIONS.has(ext)) throw new HttpError(400, `${ext} (macro-enabled) files are refused`);
|
|
192
|
+
const format = ext.slice(1);
|
|
193
|
+
if (!FORMATS[format]) throw new HttpError(400, "Only .docx, .xlsx and .pptx files are supported");
|
|
194
|
+
let st;
|
|
195
|
+
try {
|
|
196
|
+
st = checkRegularOwned(real);
|
|
197
|
+
} catch (err) {
|
|
198
|
+
throw new HttpError(400, err.message);
|
|
199
|
+
}
|
|
200
|
+
const bytes = readCapped(real);
|
|
201
|
+
try {
|
|
202
|
+
validatePackage(bytes, format);
|
|
203
|
+
} catch (err) {
|
|
204
|
+
throw new HttpError(400, `Not a valid .${format}: ${err.message}`);
|
|
205
|
+
}
|
|
206
|
+
const key = sessionKey(real);
|
|
207
|
+
let s = this.map.get(key);
|
|
208
|
+
const sha = sha256(bytes);
|
|
209
|
+
if (!s) {
|
|
210
|
+
s = this.#load(key) || this.#fresh(key, real, format, sha);
|
|
211
|
+
s.file = real;
|
|
212
|
+
s.format = format;
|
|
213
|
+
this.map.set(key, s);
|
|
214
|
+
}
|
|
215
|
+
s.snapshots = new Snapshots(key);
|
|
216
|
+
if (!s.snapshots.index().some((e) => e.original)) s.snapshots.add(bytes, "original", { original: true });
|
|
217
|
+
if (!s.delivered && s.baseSha !== sha) s.baseSha = sha; // the user re-opened: the file on disk is the base
|
|
218
|
+
if (!s.delivered && s.histTip !== sha) {
|
|
219
|
+
// history starts from the version the user opened (kept as a snapshot so a later outside change has a "before")
|
|
220
|
+
try {
|
|
221
|
+
if (!s.snapshots.has(sha)) s.snapshots.add(bytes, "opened");
|
|
222
|
+
} catch {} // a full disk must not block opening
|
|
223
|
+
s.histTip = sha;
|
|
224
|
+
}
|
|
225
|
+
s.mtimeMs = st.mtimeMs;
|
|
226
|
+
s.closed = false;
|
|
227
|
+
if (opts.agent === "background") s.openedFor = "background";
|
|
228
|
+
s.mode = st.mode & 0o7777;
|
|
229
|
+
this.#save(s);
|
|
230
|
+
this.emit("change", s);
|
|
231
|
+
return s;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
#fresh(key, file, format, sha) {
|
|
235
|
+
return {
|
|
236
|
+
key, file, format, baseSha: sha, comments: [], note: "", queuedNote: "", edits: [], pending: null, delivered: null, queued: false,
|
|
237
|
+
activity: [], view: null, closed: false, pollers: [], seq: 0, createdAt: this.now(), history: [], histTip: null,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
#load(key) {
|
|
242
|
+
try {
|
|
243
|
+
const f = path.join(sub("sessions"), `${key}.json`);
|
|
244
|
+
const j = JSON.parse(fs.readFileSync(f, "utf8"));
|
|
245
|
+
const s = { ...this.#fresh(key, j.file, j.format, j.baseSha), ...j, pollers: [] };
|
|
246
|
+
// a hard kill can leave work in flight that no result will ever settle
|
|
247
|
+
if (s.delivered?.stage === "checking") s.delivered.stage = "writing";
|
|
248
|
+
if (s.delivered?.local) {
|
|
249
|
+
// the local runner died with the server. Not yet writing: the batch waits for an agent, as if handed back. Writing: the lease
|
|
250
|
+
// expires at the first tick, which records what was written (it needs the snapshots, which open() sets after this)
|
|
251
|
+
if (s.delivered.stage === "delivered") this.#unDeliver(s);
|
|
252
|
+
else s.delivered.lease.expiresAt = 0;
|
|
253
|
+
try {
|
|
254
|
+
this.#writeLease(s);
|
|
255
|
+
} catch {} // the lease file is a mirror: a failure must not lose the session
|
|
256
|
+
}
|
|
257
|
+
for (const a of s.activity || []) {
|
|
258
|
+
if (a.checks?.deep?.soffice !== "pending") continue;
|
|
259
|
+
const deep = { ...a.checks.deep };
|
|
260
|
+
delete deep.soffice;
|
|
261
|
+
deep.skipped = [...(deep.skipped || []), { check: "soffice-convert", reason: "the server stopped before the check finished" }];
|
|
262
|
+
a.checks = { ...a.checks, deep, notice: deepNotice(deep.skipped) };
|
|
263
|
+
}
|
|
264
|
+
return s;
|
|
265
|
+
} catch {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
#save(s) {
|
|
271
|
+
const { pollers, snapshots, ...rest } = s;
|
|
272
|
+
writeStateFile(path.join(sub("sessions"), `${s.key}.json`), JSON.stringify(rest));
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
get(key) {
|
|
276
|
+
const s = this.map.get(key);
|
|
277
|
+
if (!s) throw new HttpError(404, "no such session");
|
|
278
|
+
return s;
|
|
279
|
+
}
|
|
280
|
+
byFile(input) {
|
|
281
|
+
let real;
|
|
282
|
+
try {
|
|
283
|
+
real = fs.realpathSync(path.resolve(input));
|
|
284
|
+
} catch {
|
|
285
|
+
throw new HttpError(404, `File not found: ${input}`);
|
|
286
|
+
}
|
|
287
|
+
return this.get(sessionKey(real));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// ---- derived state ---------------------------------------------------------------------------------------
|
|
291
|
+
state(s) {
|
|
292
|
+
if (s.closed) return "closed";
|
|
293
|
+
if (s.delivered) return s.queued ? "queued" : "working";
|
|
294
|
+
if (s.pending) return "stranded";
|
|
295
|
+
return s.pollers.length ? "listening" : "idle";
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
view(s) {
|
|
299
|
+
const state = this.state(s);
|
|
300
|
+
const lease = s.delivered ? s.delivered.lease : null;
|
|
301
|
+
return {
|
|
302
|
+
key: s.key,
|
|
303
|
+
name: path.basename(s.file),
|
|
304
|
+
format: s.format,
|
|
305
|
+
state,
|
|
306
|
+
locked: !!s.delivered,
|
|
307
|
+
local: !!s.delivered?.local, // the batch in flight is a run on this Mac (src/local.js), no agent: the page words the wait for it
|
|
308
|
+
baseSha: s.baseSha,
|
|
309
|
+
comments: s.comments,
|
|
310
|
+
note: s.note,
|
|
311
|
+
edits: s.edits.length,
|
|
312
|
+
activity: s.activity,
|
|
313
|
+
view: s.view,
|
|
314
|
+
pollers: s.pollers.length,
|
|
315
|
+
pendingBatchId: s.pending?.batch_id || null,
|
|
316
|
+
deliveredBatchId: s.delivered?.batch.batch_id || null,
|
|
317
|
+
leaseExpiresAt: lease?.expiresAt || null,
|
|
318
|
+
// where the delivered batch is: "delivered" (an agent has it) | "writing" (apply/commit arrived) | "checking" (done is running its checks) | null (no batch delivered)
|
|
319
|
+
stage: s.delivered?.local && s.delivered.stage === "delivered" ? "local" : (s.delivered?.stage ?? null),
|
|
320
|
+
// "voice" while the batch in flight (waiting or delivered) asks for a draft of the voice guide: the page words the wait for it
|
|
321
|
+
runKind: readOnlyKind(s.delivered?.batch ?? s.pending) === "voice" ? "voice" : null,
|
|
322
|
+
strandedCommand: state === "stranded" ? `${PRODUCT.bin} poll ${shellQuote(s.file)}` : null,
|
|
323
|
+
startCommand: state === "stranded" ? `${PRODUCT.bin} ${shellQuote(s.file)}` : null, // what a person types in a terminal to start an agent on this file
|
|
324
|
+
seq: s.seq,
|
|
325
|
+
editor: this.editors.get(s.key) || null,
|
|
326
|
+
conflict: s.conflict || null,
|
|
327
|
+
canReveal: canReveal(),
|
|
328
|
+
// the agent's review cards (src/review.js); their ops stay on the server
|
|
329
|
+
suggestions: (s.suggestions || []).map(({ op, acceptedBatch, ...c }) => c),
|
|
330
|
+
// the background agent (src/agent-runner.js): {state, cli, show, activity, failure}; never a path, a holder id or agent text
|
|
331
|
+
agent: this.agent ? this.agent.viewFor(s.key) : { state: "none", cli: null, show: false, activity: [], failure: null },
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
#changed(s, event = "state") {
|
|
336
|
+
s.seq++;
|
|
337
|
+
this.#save(s);
|
|
338
|
+
this.emit("change", s, event);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ---- drafts ----------------------------------------------------------------------------------------------
|
|
342
|
+
/** Browser replaces its list of comments/pins and the overall note. Server-owned fields (status, results) are kept. */
|
|
343
|
+
setDraft(key, { comments, note }) {
|
|
344
|
+
const s = this.get(key);
|
|
345
|
+
if (!Array.isArray(comments)) throw new HttpError(400, "comments must be an array");
|
|
346
|
+
const prev = new Map(s.comments.map((c) => [c.id, c]));
|
|
347
|
+
s.comments = comments.slice(0, 500).map((c) => {
|
|
348
|
+
const old = prev.get(String(c.id));
|
|
349
|
+
const base = {
|
|
350
|
+
id: String(c.id).slice(0, 64),
|
|
351
|
+
kind: ["text", "element", "pin"].includes(c.kind) ? c.kind : "element",
|
|
352
|
+
text: userText(c.text),
|
|
353
|
+
anchor: c.anchor && typeof c.anchor === "object" ? c.anchor : null,
|
|
354
|
+
pin: c.pin && typeof c.pin === "object" ? cleanPin(c.pin) : null,
|
|
355
|
+
pinNo: Number.isInteger(c.pinNo) ? c.pinNo : null,
|
|
356
|
+
};
|
|
357
|
+
if (old && old.status !== "draft") return { ...old, text: old.text, kind: old.kind }; // sent/done comments are frozen
|
|
358
|
+
// an answered question: the words, the question and the answer stay as the person left them (the page cannot change them)
|
|
359
|
+
const keep = old?.skipLocal ? { skipLocal: true } : {}; // a reverted local run: the next Send of this comment goes to the agent
|
|
360
|
+
if (old?.localHint && userText(c.text) === old.text) keep.localHint = old.localHint; // the words changed: the hint no longer applies
|
|
361
|
+
if (old?.answer) return { ...base, text: old.text, status: "draft", durable: old.durable || null, question: old.question, answer: old.answer, ...keep };
|
|
362
|
+
return { ...base, status: "draft", durable: old?.durable || null, ...keep };
|
|
363
|
+
});
|
|
364
|
+
s.note = userText(typeof note === "string" ? note : "");
|
|
365
|
+
this.#changed(s, "draft");
|
|
366
|
+
return s;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// ---- send ------------------------------------------------------------------------------------------------
|
|
370
|
+
/**
|
|
371
|
+
* payload: { baseSha, bytes?: Buffer, comments, note, edits: [{kind,before,after,label,location}], viewState }
|
|
372
|
+
* Compare-and-swap on the disk hash. Returns the session.
|
|
373
|
+
*/
|
|
374
|
+
send(key, payload) {
|
|
375
|
+
const s = this.get(key);
|
|
376
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
377
|
+
const hasBytes = !!payload.bytes?.length;
|
|
378
|
+
if (s.delivered && hasBytes) throw new HttpError(423, s.delivered.local ? "the document is locked while a change is made on this Mac" : "the document is locked while the agent works", { code: "locked" }); // before touching any state
|
|
379
|
+
this.setDraft(key, { comments: payload.comments ?? s.comments, note: payload.note ?? s.note });
|
|
380
|
+
if (s.delivered) {
|
|
381
|
+
// queue: comments and note only. A new comment gets its durable anchor from the bytes the page shows (the pre-agent snapshot),
|
|
382
|
+
// never from the disk, which the agent may already have changed; done() then moves it onto the post-agent file.
|
|
383
|
+
const fresh = s.comments.filter((c) => c.status === "draft");
|
|
384
|
+
if (fresh.some((c) => !c.durable)) {
|
|
385
|
+
let units = null;
|
|
386
|
+
try {
|
|
387
|
+
units = readUnits(s.format, s.snapshots.read(s.delivered.preSha));
|
|
388
|
+
} catch {
|
|
389
|
+
units = null; // no snapshot: done() falls back to building from the post-agent file
|
|
390
|
+
}
|
|
391
|
+
if (units) {
|
|
392
|
+
for (const c of fresh) {
|
|
393
|
+
if (c.durable) continue;
|
|
394
|
+
c.durable = buildDurable(units, c.anchor);
|
|
395
|
+
if (units.format === "pptx" && c.kind === "pin" && c.durable?.shapeId != null) c.durable.pinFallback = buildDurable(units, { location: { slideIndex: c.durable.slideIndex, slideId: c.durable.slideId } });
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
for (const c of fresh) c.status = "queued";
|
|
400
|
+
if (s.note) {
|
|
401
|
+
// the note belongs to the queued batch now; the stored note is cleared so the next Send does not repeat it
|
|
402
|
+
s.queuedNote = [s.queuedNote, s.note].filter(Boolean).join("\n\n");
|
|
403
|
+
s.note = "";
|
|
404
|
+
}
|
|
405
|
+
if (fresh.length || s.queuedNote) s.queued = true; // a Send with nothing new queues nothing (no empty batch after this one)
|
|
406
|
+
this.#changed(s);
|
|
407
|
+
this.#voice(s, { sent: fresh });
|
|
408
|
+
return s;
|
|
409
|
+
}
|
|
410
|
+
const { diskSha, written, writtenSha, writeInfo, lastUpdate } = this.#takePage(s, payload);
|
|
411
|
+
|
|
412
|
+
// user edits since the last Send (document-derived text) accumulate until a batch is delivered
|
|
413
|
+
for (const e of Array.isArray(payload.edits) ? payload.edits.slice(0, 2000) : []) s.edits.push({ kind: e.kind, before: e.before, after: e.after, label: e.label, location: e.location });
|
|
414
|
+
|
|
415
|
+
const units = readUnits(s.format, written);
|
|
416
|
+
const resolve = (anchor) => buildDurable(units, anchor);
|
|
417
|
+
const eligible = s.comments.filter((c) => c.status === "draft" || c.status === "sent");
|
|
418
|
+
for (const c of eligible) {
|
|
419
|
+
c.durable = resolve(c.anchor);
|
|
420
|
+
// a PPTX pin dropped on a shape also remembers its slide: if the shape is deleted the pin stays on the slide
|
|
421
|
+
if (units.format === "pptx" && c.kind === "pin" && c.durable?.shapeId != null) c.durable.pinFallback = buildDurable(units, { location: { slideIndex: c.durable.slideIndex, slideId: c.durable.slideId } });
|
|
422
|
+
}
|
|
423
|
+
for (const e of s.edits) e.durable = e.durable || resolve({ location: e.location, quote: e.after });
|
|
424
|
+
s.view = this.#resolveView(s, units, payload.viewState);
|
|
425
|
+
|
|
426
|
+
const batch = buildBatch({ file: s.file, format: s.format, baseSha: writtenSha, note: s.note, comments: eligible, edits: s.edits, style: styleNote(), guide: voiceGuide() });
|
|
427
|
+
const sent = eligible.filter((c) => c.status === "draft");
|
|
428
|
+
for (const c of eligible) {
|
|
429
|
+
c.status = "sent";
|
|
430
|
+
c.batchId = batch.batch_id;
|
|
431
|
+
}
|
|
432
|
+
s.note = ""; // the note moved into the batch: it is not asked for again
|
|
433
|
+
// a voice guide request still waiting for an agent is replaced by this Send (as a waiting review is); its pasted texts go with it, so say so
|
|
434
|
+
if (readOnlyKind(s.pending) === "voice") s.activity.unshift({ id: `r_${crypto.randomBytes(4).toString("hex")}`, kind: "voice", at: this.now(), batchId: s.pending.batch_id, summary: VOICE_REPLACED, results: [], flags: [{ code: "voice_request_replaced", severity: "warn" }], checks: null, changes: null, reverted: false });
|
|
435
|
+
s.pending = batch;
|
|
436
|
+
s.lastWrite = writeInfo;
|
|
437
|
+
// a quick change (src/quick-plan.js): the note and the count the person saw on the button. Held for this Send only; it never enters the batch.
|
|
438
|
+
const qc = payload.quick;
|
|
439
|
+
if (qc && typeof qc === "object" && typeof qc.command === "string" && qc.command.length <= 300 && Number.isInteger(qc.count) && qc.count > 0 && qc.count <= 1e6) s.quickConfirm = { command: qc.command, count: qc.count };
|
|
440
|
+
this.#deliverIfPolled(s);
|
|
441
|
+
delete s.quickConfirm;
|
|
442
|
+
delete s.skipLocalNote; // it held for this Send only (the decision for its batch is made)
|
|
443
|
+
this.#changed(s);
|
|
444
|
+
this.#voice(s, { last: lastUpdate, diskSha, writtenSha, sent });
|
|
445
|
+
if (this.state(s) === "stranded" && !s.pollers.length) this.emit("stranded", s); // nobody took it: the server may start its background agent
|
|
446
|
+
return s;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* The page's bytes become the file, as at Send (and at a quick fix's Use): the disk read, the compare-and-swap against the version
|
|
451
|
+
* the page loaded, the package check, the guarded replace, the "sent" snapshot, the new base and the person's own history update.
|
|
452
|
+
* `check(bytes)` (optional) sees the bytes that are about to be the file, after the package check and before anything is written, and
|
|
453
|
+
* may throw to refuse. Returns {disk, diskSha, written, writtenSha, writeInfo, lastUpdate}.
|
|
454
|
+
*/
|
|
455
|
+
#takePage(s, payload, check = null) {
|
|
456
|
+
const hasBytes = !!payload.bytes?.length;
|
|
457
|
+
let disk;
|
|
458
|
+
try {
|
|
459
|
+
checkRegularOwned(s.file);
|
|
460
|
+
disk = readCapped(s.file);
|
|
461
|
+
} catch (err) {
|
|
462
|
+
throw new HttpError(409, `cannot read the file: ${err.message}`, { code: "gone" });
|
|
463
|
+
}
|
|
464
|
+
const diskSha = sha256(disk);
|
|
465
|
+
if (diskSha !== payload.baseSha) {
|
|
466
|
+
throw this.#outsideChange(s, { side: "user", bytes: payload.bytes, diskSha, baseSha: payload.baseSha });
|
|
467
|
+
}
|
|
468
|
+
let written = disk;
|
|
469
|
+
let writeInfo = null;
|
|
470
|
+
if (hasBytes) {
|
|
471
|
+
if (payload.bytes.length > MAX_BYTES) throw new HttpError(413, "document too large");
|
|
472
|
+
try {
|
|
473
|
+
validatePackage(payload.bytes, s.format);
|
|
474
|
+
} catch (err) {
|
|
475
|
+
throw new HttpError(422, `refusing to write: ${err.message}`, { code: "invalid_package" });
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (check) check(hasBytes ? payload.bytes : disk);
|
|
479
|
+
if (hasBytes) {
|
|
480
|
+
writeInfo = this.#replace(s, payload.bytes, { expectSha: diskSha, label: "before send", side: "user" });
|
|
481
|
+
written = payload.bytes;
|
|
482
|
+
}
|
|
483
|
+
const writtenSha = sha256(written);
|
|
484
|
+
s.snapshots.add(written, "sent");
|
|
485
|
+
s.baseSha = writtenSha;
|
|
486
|
+
const lastUpdate = s.history?.at(-1); // learning pairs an agent run with the person's edits right after it
|
|
487
|
+
if (hasBytes && writtenSha !== diskSha) addUpdate(s, { kind: "user", before: diskSha, after: writtenSha }, this.now());
|
|
488
|
+
s.histTip = writtenSha;
|
|
489
|
+
return { disk, diskSha, written, writtenSha, writeInfo, lastUpdate };
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Hands what this Send can teach (src/voice-learn.js) to the server's `voice` listener. Last in send(): learning never fails a Send. */
|
|
493
|
+
#voice(s, info) {
|
|
494
|
+
try {
|
|
495
|
+
const job = voiceJob(s, info);
|
|
496
|
+
if (job) this.emit("voice", job);
|
|
497
|
+
} catch {}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// ---- merge what the user typed while the agent worked ---------------------------------------------------
|
|
501
|
+
/**
|
|
502
|
+
* The page kept editing while a batch was delivered. `bytes` is the page's document: the version it loaded (`baseSha`) plus the
|
|
503
|
+
* user's edits; `edits` names the kinds of edits the page saw. The disk now holds the agent's version. What only the user changed
|
|
504
|
+
* (DOCX paragraphs, formatting and moves: src/merge.js; XLSX cells: src/merge-xlsx.js; PPTX shape text: src/merge-pptx.js) is carried onto it as ops (same engine, expect, protected and package checks
|
|
505
|
+
* as an agent's apply) and written as the user's own update. Refused while a batch is delivered (423): the page waits and asks
|
|
506
|
+
* again. Returns {merged, unplaced, sha, same, mineSha, copy_name}.
|
|
507
|
+
* Ceiling: the engine runs in the server process (about 0.1 s on a 300-paragraph file); move it to a worker if large files stall polls.
|
|
508
|
+
*/
|
|
509
|
+
async mergeUserEdits(key, { baseSha, bytes, edits = [] }) {
|
|
510
|
+
const s = this.get(key);
|
|
511
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
512
|
+
if (s.delivered) throw new HttpError(423, s.delivered.local ? "a change is being made on this Mac" : "the agent is working", { code: "locked" });
|
|
513
|
+
const merger = MERGERS[s.format];
|
|
514
|
+
if (!/^[0-9a-f]{64}$/.test(baseSha)) throw new HttpError(400, "baseSha must be a sha256"); // it names a snapshot file
|
|
515
|
+
if (!bytes?.length) throw new HttpError(400, "no document bytes");
|
|
516
|
+
if (bytes.length > MAX_BYTES) throw new HttpError(413, "document too large");
|
|
517
|
+
try {
|
|
518
|
+
validatePackage(bytes, s.format);
|
|
519
|
+
} catch (err) {
|
|
520
|
+
throw new HttpError(422, `refusing to merge: ${err.message}`, { code: "invalid_package" });
|
|
521
|
+
}
|
|
522
|
+
let disk;
|
|
523
|
+
try {
|
|
524
|
+
checkRegularOwned(s.file);
|
|
525
|
+
disk = readCapped(s.file);
|
|
526
|
+
} catch (err) {
|
|
527
|
+
throw new HttpError(409, `cannot read the file: ${err.message}`, { code: "gone" });
|
|
528
|
+
}
|
|
529
|
+
const diskSha = sha256(disk);
|
|
530
|
+
if (diskSha === baseSha) return { same: true, merged: 0, unplaced: [], sha: diskSha, mineSha: null }; // the file did not change under the page
|
|
531
|
+
if (diskSha !== s.baseSha) throw this.#outsideChange(s, { side: "user", bytes, diskSha, baseSha });
|
|
532
|
+
let base;
|
|
533
|
+
try {
|
|
534
|
+
base = s.snapshots.read(baseSha);
|
|
535
|
+
} catch {
|
|
536
|
+
throw new HttpError(409, "the version this page started from is no longer stored", { code: "no_base" });
|
|
537
|
+
}
|
|
538
|
+
const [mod, { runOps }] = await Promise.all([import(merger.module), import("./ops/run.js")]);
|
|
539
|
+
const r = await mod[merger.fn]({ base, mine: bytes, theirs: disk, name: path.basename(s.file), runOps, edits: editKinds(edits), agent: s.format === "xlsx" ? this.#writesBetween(s, baseSha, diskSha, disk) : null });
|
|
540
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
541
|
+
if (s.delivered) throw new HttpError(423, s.delivered.local ? "a change is being made on this Mac" : "the agent is working", { code: "locked" }); // a batch was delivered while the engine ran
|
|
542
|
+
// The page shows the first HELD_SHOWN items and drops its own version when it reloads. When the list stands for more than it
|
|
543
|
+
// shows, the user's whole file is saved next to the original first. No copy, no merge: the edits stay in the page.
|
|
544
|
+
let copy = null;
|
|
545
|
+
if (r.copy || r.unplaced.length > HELD_SHOWN) {
|
|
546
|
+
try {
|
|
547
|
+
copy = writeSiblingCopy(s.file, bytes);
|
|
548
|
+
this.createdCopies.add(copy.path);
|
|
549
|
+
} catch (err) {
|
|
550
|
+
throw new HttpError(409, `a copy of your version could not be saved: ${err.code || err.message}`, { code: "copy_failed" });
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
s.snapshots.add(bytes, "mine"); // a second pass (the user typed during this one) starts from this version
|
|
554
|
+
let sha = diskSha;
|
|
555
|
+
if (r.bytes) {
|
|
556
|
+
this.#replace(s, r.bytes, { expectSha: diskSha, label: "before merge", side: "user" });
|
|
557
|
+
sha = sha256(r.bytes);
|
|
558
|
+
s.snapshots.add(r.bytes, "merged");
|
|
559
|
+
s.baseSha = sha;
|
|
560
|
+
addUpdate(s, { kind: "user", before: diskSha, after: sha }, this.now());
|
|
561
|
+
s.histTip = sha;
|
|
562
|
+
this.#changed(s);
|
|
563
|
+
}
|
|
564
|
+
// the second pass compares this version of the page with the result: the sheets kept their addresses where this pass merged by address
|
|
565
|
+
if (Array.isArray(r.stable)) this.#logWrite(s, { from: sha256(bytes), to: sha, route: "merge", stable: r.stable });
|
|
566
|
+
return { same: false, merged: r.merged, unplaced: r.unplaced.slice(0, 50), sha, mineSha: sha256(bytes), copy_name: copy?.name ?? null };
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
#logWrite(s, entry) {
|
|
570
|
+
const list = this.writes.get(s.key) || [];
|
|
571
|
+
list.push(entry);
|
|
572
|
+
if (list.length > WRITES_KEPT) list.shift();
|
|
573
|
+
this.writes.set(s.key, list);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* The recorded writes that lead from version `from` to version `to` (the file on disk, `disk`), oldest first, with the bytes an
|
|
578
|
+
* apply write started from and produced; null when no chain of records connects them (a revert, a direct save, a restart, a pruned
|
|
579
|
+
* snapshot). Hashes name content, so a chain found is what happened to the bytes, whatever the order of the runs.
|
|
580
|
+
*/
|
|
581
|
+
#writesBetween(s, from, to, disk) {
|
|
582
|
+
const list = this.writes.get(s.key) || [];
|
|
583
|
+
const seen = new Set();
|
|
584
|
+
const walk = (sha) => {
|
|
585
|
+
if (sha === from) return [];
|
|
586
|
+
if (seen.has(sha)) return null;
|
|
587
|
+
seen.add(sha);
|
|
588
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
589
|
+
if (list[i].to !== sha) continue;
|
|
590
|
+
const before = walk(list[i].from);
|
|
591
|
+
if (before) return [...before, list[i]];
|
|
592
|
+
}
|
|
593
|
+
return null;
|
|
594
|
+
};
|
|
595
|
+
const chain = walk(to);
|
|
596
|
+
if (!chain) return null;
|
|
597
|
+
try {
|
|
598
|
+
return chain.map((e) => (e.route === "apply" ? { ...e, from: s.snapshots.read(e.from), to: e.to === to ? disk : s.snapshots.read(e.to) } : e));
|
|
599
|
+
} catch {
|
|
600
|
+
return null;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* The one write path of the session (Send, agent write, commit, revert): the advisory lock and the compare-and-swap cover the whole
|
|
606
|
+
* replacement (see replaceGuarded). `expectSha` is the hash the caller compared against; a file that is different now, or becomes
|
|
607
|
+
* different just before the rename, is not overwritten (the newer bytes are snapshotted) and the caller gets the 409 conflict.
|
|
608
|
+
*/
|
|
609
|
+
#replace(s, bytes, { expectSha, label, side = null }) {
|
|
610
|
+
try {
|
|
611
|
+
return replaceGuarded(s.file, bytes, {
|
|
612
|
+
expectSha,
|
|
613
|
+
snapshotBefore: (disk) => s.snapshots.add(disk, label),
|
|
614
|
+
snapshotNewer: (newer) => s.snapshots.add(newer, "newer version found while writing"),
|
|
615
|
+
});
|
|
616
|
+
} catch (err) {
|
|
617
|
+
if (err instanceof WriteConflict) throw this.#outsideChange(s, { side, bytes, diskSha: err.diskSha, baseSha: err.baseSha, message: err.message });
|
|
618
|
+
if (err.code === "ENOSPC_SNAP") throw new HttpError(507, err.message);
|
|
619
|
+
if (err.code === "LOCK_BUSY") throw new HttpError(409, err.message, { code: "write_busy" });
|
|
620
|
+
if (err.code === "ACL_NOT_PRESERVED") throw new HttpError(409, err.message, { code: "acl_not_preserved" });
|
|
621
|
+
if (err.code === "SYMLINK") throw new HttpError(409, `write failed: ${err.message}`, { code: "symlink" });
|
|
622
|
+
throw new HttpError(500, `write failed: ${err.message}`, { code: "write_failed" });
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* The file on disk is not what the writer compared against: someone else changed it. The original is NOT touched. When `side` says whose
|
|
628
|
+
* bytes these are ("user": the page's current bytes; "agent": the agent's candidate), they are saved as a sibling copy so nothing is lost,
|
|
629
|
+
* and the 409 carries the copy's name and path (built by the server from the session's own file path). An agent-side conflict is also
|
|
630
|
+
* recorded for the page: an activity entry and a banner. Returns the HttpError to throw.
|
|
631
|
+
*/
|
|
632
|
+
#outsideChange(s, { side, bytes, diskSha, baseSha, message }) {
|
|
633
|
+
let copy = null;
|
|
634
|
+
let copyError = null;
|
|
635
|
+
if (side && bytes?.length) {
|
|
636
|
+
try {
|
|
637
|
+
copy = writeSiblingCopy(s.file, bytes);
|
|
638
|
+
this.createdCopies.add(copy.path);
|
|
639
|
+
} catch (err) {
|
|
640
|
+
copyError = `the copy could not be saved: ${err.code || err.message}`;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
const extra = { code: "conflict", diskSha, baseSha, side, copy_name: copy?.name ?? null, copy_path: copy?.path ?? null, copy_error: copyError, can_reveal: canReveal() };
|
|
644
|
+
if (side === "agent") {
|
|
645
|
+
const local = !!s.delivered?.local; // a change made on this Mac: no agent was involved
|
|
646
|
+
const what = local ? "The change made on this Mac" : "The agent's write";
|
|
647
|
+
const note = copy ? `Someone changed this file outside Onionskin. ${what} was not applied and your original file was left untouched. A copy with ${local ? "the change" : "the agent's edits"} was saved as ${copy.name} in the same folder.` : `Someone changed this file outside Onionskin. ${what} was not applied and your original file was left untouched.${copyError ? ` ${copyError}.` : ""}`;
|
|
648
|
+
s.conflict = { side: "agent", ...(local ? { local: true } : {}), copyName: copy?.name ?? null, copyPath: copy?.path ?? null, copyError, at: this.now() };
|
|
649
|
+
if (s.delivered) s.delivered.outsideChange = { copyName: copy?.name ?? null };
|
|
650
|
+
s.activity.unshift({ id: `r_${crypto.randomBytes(4).toString("hex")}`, kind: "outside_change", at: this.now(), summary: userText(note, 600), flags: [{ code: "outside_change", severity: "warn" }], results: [] });
|
|
651
|
+
this.#changed(s);
|
|
652
|
+
}
|
|
653
|
+
return new HttpError(409, message || "the file changed on disk since it was loaded", extra);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** Reveal a conflict copy in the Finder. Only a path this server itself wrote is accepted; `open -R` gets it as an argument (no shell). */
|
|
657
|
+
showCopy(p) {
|
|
658
|
+
if (!this.createdCopies.has(String(p)) || !fs.existsSync(p)) throw new HttpError(404, "no such copy");
|
|
659
|
+
const stub = env("OPEN_STUB"); // tests: record the path instead of launching Finder
|
|
660
|
+
if (stub) {
|
|
661
|
+
fs.appendFileSync(stub, `${p}\n`);
|
|
662
|
+
return { ok: true, opened: true };
|
|
663
|
+
}
|
|
664
|
+
if (!canReveal()) return { ok: true, opened: false };
|
|
665
|
+
execFile("/usr/bin/open", ["-R", p], () => {});
|
|
666
|
+
return { ok: true, opened: true };
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
dismissConflict(key) {
|
|
670
|
+
const s = this.get(key);
|
|
671
|
+
s.conflict = null;
|
|
672
|
+
this.#changed(s);
|
|
673
|
+
return s;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** The cheap early check: does the disk still hold the version the page loaded? (Not asked while a batch is delivered: the agent changes the disk then.) */
|
|
677
|
+
staleCheck(key, loadedSha) {
|
|
678
|
+
const s = this.get(key);
|
|
679
|
+
const diskSha = sha256(readCapped(s.file));
|
|
680
|
+
return { stale: !s.delivered && !!loadedSha && diskSha !== loadedSha, diskSha };
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
#resolveView(s, units, vs) {
|
|
684
|
+
if (!vs) return null;
|
|
685
|
+
if (units.format === "docx" && vs.format !== "docx") return { format: s.format, raw: vs };
|
|
686
|
+
if (units.format !== "docx") return viewDurable(units, vs) || { format: s.format, raw: vs };
|
|
687
|
+
const d = durableFromEngine(units.paragraphs, { blockPath: vs.blockPath }, { quote: vs.quote });
|
|
688
|
+
return { format: "docx", durable: d, offsetPx: Number(vs.offsetPx) || 0, zoom: vs.zoom ?? "fit", fraction: vs.fraction };
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// ---- poll / delivery -------------------------------------------------------------------------------------
|
|
692
|
+
/**
|
|
693
|
+
* Registers a poller. Returns a handle {promise, cancel}. Resolves with the batch object, or {status:'superseded'|'closed'}.
|
|
694
|
+
* ack=true acknowledges a finished run and renews the lease of a run in progress. compact=true (only `reply --then-poll` asks) lets a holder that was briefed already get the short form (see compactBatch);
|
|
695
|
+
* every other poll, including a new agent conversation on the same file that shares the saved holder, gets the complete batch. full=true overrides compact.
|
|
696
|
+
*/
|
|
697
|
+
poll(key, { ack = false, holder = "agent", full = false, compact = false } = {}) {
|
|
698
|
+
const s = this.get(key);
|
|
699
|
+
if (holder === LOCAL_HOLDER) throw new HttpError(400, "this holder id is reserved for Onionskin itself; poll with another one", { code: "reserved_holder" });
|
|
700
|
+
// the background agent's holder is bound to its own file, and it never takes a file from an agent that polls it (that agent supersedes it instead)
|
|
701
|
+
if (isBgHolder(holder) && holder !== bgHolder(key)) throw new HttpError(400, "this holder id is reserved for Onionskin itself; poll with another one", { code: "reserved_holder" });
|
|
702
|
+
if (typeof holder === "string" && holder.startsWith(TERM_PREFIX) && holder !== termHolder(key)) throw new HttpError(400, "this holder id is reserved for Onionskin itself; poll with another one", { code: "reserved_holder" });
|
|
703
|
+
if (isBgHolder(holder) && (s.pollers.some((p) => p.holder !== holder) || (s.delivered && !s.delivered.local && s.delivered.lease.holder !== holder))) {
|
|
704
|
+
this.emit("superseded", s, holder);
|
|
705
|
+
return { promise: Promise.resolve({ status: "superseded", next_step: "Another poller took over this file." }), cancel: () => {} };
|
|
706
|
+
}
|
|
707
|
+
// a background poll with no agent process behind it (the agent ended, its poll child lived on): answered closed, so no Send goes to nobody
|
|
708
|
+
if (isBgHolder(holder) && this.agent?.alive && !this.agent.alive(key)) return { promise: Promise.resolve({ status: "closed", reason: "the agent was stopped" }), cancel: () => {} };
|
|
709
|
+
if (ack && s.delivered && s.delivered.lease.holder === holder) this.#renew(s);
|
|
710
|
+
let resolveFn;
|
|
711
|
+
const promise = new Promise((r) => (resolveFn = r));
|
|
712
|
+
for (const old of s.pollers.splice(0)) {
|
|
713
|
+
old.resolve({ status: "superseded", next_step: "Another poller took over this file." });
|
|
714
|
+
if (isBgHolder(old.holder) && old.holder !== holder) this.emit("superseded", s, old.holder);
|
|
715
|
+
}
|
|
716
|
+
const p = { holder, full, compact, resolve: resolveFn };
|
|
717
|
+
s.pollers.push(p);
|
|
718
|
+
if (s.closed) {
|
|
719
|
+
s.pollers = s.pollers.filter((x) => x !== p);
|
|
720
|
+
resolveFn({ status: "closed" });
|
|
721
|
+
} else {
|
|
722
|
+
this.#deliverIfPolled(s);
|
|
723
|
+
}
|
|
724
|
+
this.#changed(s);
|
|
725
|
+
return {
|
|
726
|
+
promise,
|
|
727
|
+
cancel: () => {
|
|
728
|
+
const before = s.pollers.length;
|
|
729
|
+
s.pollers = s.pollers.filter((x) => x !== p);
|
|
730
|
+
if (s.pollers.length !== before) this.#changed(s);
|
|
731
|
+
},
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
#deliverIfPolled(s) {
|
|
736
|
+
if (!s.pending || s.delivered) return;
|
|
737
|
+
if (this.#deliverLocal(s)) return;
|
|
738
|
+
if (!s.pollers.length) return;
|
|
739
|
+
const p = s.pollers.shift();
|
|
740
|
+
const batch = s.pending;
|
|
741
|
+
this.#deliverTo(s, batch, p.holder);
|
|
742
|
+
s.pending = null;
|
|
743
|
+
s.queued = false;
|
|
744
|
+
this.#writeLease(s);
|
|
745
|
+
this.#styleSeen.set(s.delivered, styleChangedOutside());
|
|
746
|
+
this.#guideSeen.set(s.delivered, guideChangedOutside());
|
|
747
|
+
// the poller gets a copy that carries this delivery's lease; the stored batch stays free of it
|
|
748
|
+
// the commands in it carry this delivery's ticket (`--lease`), so an agent that runs them as printed names its own delivery
|
|
749
|
+
const ticket = ticketOf(batch.batch_id, s.delivered.lease.token);
|
|
750
|
+
const lease = { holder: p.holder, token: s.delivered.lease.token };
|
|
751
|
+
const shown = this.#withOutline(s, batch, p.holder);
|
|
752
|
+
const briefed = this.briefed.get(s.key) || this.briefed.set(s.key, new Set()).get(s.key);
|
|
753
|
+
if (briefed.has(p.holder) && p.compact && !p.full) {
|
|
754
|
+
p.resolve(compactBatch(shown, { file: s.file, ticket, lease }));
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
briefed.add(p.holder);
|
|
758
|
+
p.resolve({
|
|
759
|
+
...shown,
|
|
760
|
+
// a voice batch writes nothing, so it has no how_to_apply
|
|
761
|
+
...(batch.how_to_apply ? { how_to_apply: { ...batch.how_to_apply, command: batch.how_to_apply.command.replace(/( --ops \S+)/, `$1 --lease ${ticket}`), rules: batch.how_to_apply.rules.map((r) => r.replaceAll("<ticket>", ticket)) } } : {}),
|
|
762
|
+
next_step: batch.next_step.replace(/( --results \S+)$/, `$1 --lease ${ticket}`).replaceAll("<ticket>", ticket),
|
|
763
|
+
lease,
|
|
764
|
+
lease_ticket: ticket,
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* An agent Onionskin started (its background holder or the Terminal form) cannot open the file, so each edit batch it receives, complete or
|
|
770
|
+
* compact, also carries the outline of the file as it is now (the bytes its ops are checked against), with the target and expect of every
|
|
771
|
+
* unit. As much as fits one shell output is inline; when that is not all of it, the complete outline (up to OUTLINE_FILE_MAX units) is
|
|
772
|
+
* written to <state>/agents/<key>/outline/outline-<batch id>.jsonl (0600, atomic, one unit per line), the only folder that agent may read,
|
|
773
|
+
* and the batch names it. Older outline files of the session go at each such delivery, and with the folder on Close.
|
|
774
|
+
* Any other holder, a review (it has `units`) and a voice request (it quotes only the sources the person ticked) get the batch as stored.
|
|
775
|
+
* A file that cannot be read gets no outline (the agent declines what it cannot find); an outline file that cannot be written leaves the
|
|
776
|
+
* inline part with `outline_more`. Ceiling: read and parsed on the main thread at each such delivery, as a review is; upgrade path as in unitsOf.
|
|
777
|
+
*/
|
|
778
|
+
#withOutline(s, batch, holder) {
|
|
779
|
+
if (!isAgentHolder(holder) || batch.instructions?.review || batch.instructions?.voice_request) return batch;
|
|
780
|
+
let all;
|
|
781
|
+
try {
|
|
782
|
+
checkRegularOwned(s.file);
|
|
783
|
+
all = outlineOf(s.format, readCapped(s.file), OUTLINE_FILE_MAX);
|
|
784
|
+
} catch {
|
|
785
|
+
return batch;
|
|
786
|
+
}
|
|
787
|
+
const inline = withOutline(batch, all, VOICE_BATCH_MAX_CHARS, { inlineMax: UNITS_MAX });
|
|
788
|
+
const name = `outline-${batch.batch_id}.jsonl`;
|
|
789
|
+
let dir = null;
|
|
790
|
+
try {
|
|
791
|
+
dir = fs.realpathSync(sub("agents", s.key, "outline")); // a real path: --restricted compares paths literally
|
|
792
|
+
for (const n of fs.readdirSync(dir)) if (/^outline-.*\.jsonl$/.test(n) && n !== name) fs.rmSync(path.join(dir, n), { force: true });
|
|
793
|
+
} catch {}
|
|
794
|
+
if (!inline.untrusted_document_evidence.outline_more || !dir) return inline;
|
|
795
|
+
const file = path.join(dir, name);
|
|
796
|
+
try {
|
|
797
|
+
writeStateFile(file, all.outline.map((u) => JSON.stringify(u)).join("\n") + "\n");
|
|
798
|
+
} catch {
|
|
799
|
+
return inline;
|
|
800
|
+
}
|
|
801
|
+
return withOutline(batch, all, VOICE_BATCH_MAX_CHARS, { inlineMax: UNITS_MAX, file: { path: file, units: all.outline.length } });
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* The local runner (src/local.js) is asked once per batch id whether it does this whole batch on this Mac. Yes: the batch is delivered to
|
|
806
|
+
* LOCAL_HOLDER with a fresh token, like an agent's delivery, and no poller is taken. The `local` event carries the ticket; the runner uses
|
|
807
|
+
* only that. A batch that comes back (hand-back, lease expiry, restart) is never asked again, so it goes to the agent.
|
|
808
|
+
*/
|
|
809
|
+
#deliverLocal(s) {
|
|
810
|
+
const batch = s.pending;
|
|
811
|
+
if (!this.local || s.decidedBatch === batch.batch_id) return false;
|
|
812
|
+
s.decidedBatch = batch.batch_id;
|
|
813
|
+
let yes = false;
|
|
814
|
+
try {
|
|
815
|
+
yes = this.local.wants(s, batch) === true;
|
|
816
|
+
} catch {
|
|
817
|
+
yes = false; // a predicate that throws means no
|
|
818
|
+
}
|
|
819
|
+
if (!yes) return false;
|
|
820
|
+
this.#deliverTo(s, batch, LOCAL_HOLDER, { local: true, ...(s.quickConfirm ? { quick: s.quickConfirm } : {}) });
|
|
821
|
+
s.pending = null;
|
|
822
|
+
s.queued = false;
|
|
823
|
+
this.#writeLease(s);
|
|
824
|
+
this.emit("local", s, { batchId: batch.batch_id, token: s.delivered.lease.token });
|
|
825
|
+
return true;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
#deliverTo(s, batch, holder, extra = {}) {
|
|
829
|
+
s.delivered = { batch, stage: "delivered", preSha: s.baseSha, knownSha: s.baseSha, deliveredAt: this.now(), edits: s.edits, lease: { holder, token: crypto.randomBytes(8).toString("hex"), expiresAt: this.now() + LEASE_MS() }, ...extra };
|
|
830
|
+
s.edits = [];
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/** A delivery that wrote nothing goes back: the batch is pending again (decided: the next delivery is an agent's), the typed edits are evidence again, queued comments are drafts again (as at lease expiry). */
|
|
834
|
+
#unDeliver(s) {
|
|
835
|
+
const d = s.delivered;
|
|
836
|
+
if (!d.accept) s.pending = d.batch; // an accepted suggestion has nothing to ask an agent
|
|
837
|
+
this.#backToDraft(s, d, true);
|
|
838
|
+
s.decidedBatch = d.batch.batch_id;
|
|
839
|
+
s.edits = [...d.edits, ...s.edits];
|
|
840
|
+
s.delivered = null;
|
|
841
|
+
this.#queuedToDrafts(s);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
#queuedToDrafts(s) {
|
|
845
|
+
if (s.queued) {
|
|
846
|
+
for (const c of s.comments) if (c.status === "queued") c.status = "draft";
|
|
847
|
+
s.note = [s.queuedNote, s.note].filter(Boolean).join("\n\n");
|
|
848
|
+
s.queuedNote = "";
|
|
849
|
+
}
|
|
850
|
+
s.queued = false;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* The local runner could not finish and nothing was written: the batch goes to the agent exactly as it would have (same batch, a new
|
|
855
|
+
* lease when a poller takes it). Strict lease; refused (409 `written`) once the delivery wrote anything, which then finishes like a run.
|
|
856
|
+
*/
|
|
857
|
+
handBack(key, claim) {
|
|
858
|
+
const s = this.get(key);
|
|
859
|
+
if (!s.delivered) throw new HttpError(409, "no batch is in progress for this file", { code: "no_batch" });
|
|
860
|
+
this.#assertLease(s, { ...claim, strict: true });
|
|
861
|
+
if (!s.delivered.local) throw new HttpError(409, "only a run on this Mac is handed back", { code: "not_local" });
|
|
862
|
+
if ((s.delivered.knownSha ?? s.delivered.preSha) !== s.delivered.preSha) throw new HttpError(409, "the file was already written for this batch", { code: "written" });
|
|
863
|
+
this.#unDeliver(s);
|
|
864
|
+
this.#writeLease(s);
|
|
865
|
+
this.#deliverIfPolled(s);
|
|
866
|
+
this.#changed(s);
|
|
867
|
+
return s;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* A batch made by Onionskin itself (idea 21's Accept: no instructions, the stored ops) run on this Mac with an agent's powers. Refused
|
|
872
|
+
* while anything is delivered or waiting, so it never takes a Send's place. `job` stays in memory with the delivery and goes to the runner
|
|
873
|
+
* with the `local` event. Returns the ticket.
|
|
874
|
+
*/
|
|
875
|
+
startLocal(key, batch, job = null) {
|
|
876
|
+
const s = this.get(key);
|
|
877
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
878
|
+
if (s.delivered || s.pending) throw new HttpError(409, "a batch is already waiting or in progress for this file", { code: "busy" });
|
|
879
|
+
if (!batch?.batch_id || batch.base_sha256 !== s.baseSha) throw new HttpError(409, "the batch is not for the current version of the file", { code: "stale_batch" });
|
|
880
|
+
this.#deliverTo(s, batch, LOCAL_HOLDER, { local: true, accept: true, ...(job?.model === true ? { model: true } : {}) }); // model: the words come from the local writing model
|
|
881
|
+
s.decidedBatch = batch.batch_id;
|
|
882
|
+
this.#writeLease(s);
|
|
883
|
+
this.#changed(s);
|
|
884
|
+
const ticket = { batchId: batch.batch_id, token: s.delivered.lease.token, job };
|
|
885
|
+
this.emit("local", s, ticket);
|
|
886
|
+
return ticket;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
/** A startLocal delivery that wrote nothing ends with no trace: nothing goes to an agent (there is nothing to ask it). Strict lease, like handBack. */
|
|
890
|
+
dropLocal(key, claim) {
|
|
891
|
+
const s = this.get(key);
|
|
892
|
+
if (!s.delivered) throw new HttpError(409, "no batch is in progress for this file", { code: "no_batch" });
|
|
893
|
+
this.#assertLease(s, { ...claim, strict: true });
|
|
894
|
+
if (!s.delivered.accept) throw new HttpError(409, "only an accepted suggestion is dropped", { code: "not_local" });
|
|
895
|
+
if ((s.delivered.knownSha ?? s.delivered.preSha) !== s.delivered.preSha) throw new HttpError(409, "the file was already written for this batch", { code: "written" });
|
|
896
|
+
this.#settleCards(s, s.delivered, []); // nothing applied: the text there changed
|
|
897
|
+
this.#backToDraft(s, s.delivered, true);
|
|
898
|
+
s.edits = [...s.delivered.edits, ...s.edits];
|
|
899
|
+
s.delivered = null;
|
|
900
|
+
this.#writeLease(s);
|
|
901
|
+
this.#deliverIfPolled(s);
|
|
902
|
+
this.#changed(s);
|
|
903
|
+
return s;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* An accepted run that ends without `done` (dropLocal, a restart, a lease expiry): the comment of a quick fix it carried is a draft
|
|
908
|
+
* again, so the person can send it to the agent. `hint`: nothing was written, and the comment says so.
|
|
909
|
+
*/
|
|
910
|
+
#backToDraft(s, d, hint) {
|
|
911
|
+
if (!d?.accept) return;
|
|
912
|
+
for (const c of s.comments) {
|
|
913
|
+
if (c.batchId !== d.batch.batch_id || c.status !== "sent") continue;
|
|
914
|
+
c.status = "draft";
|
|
915
|
+
delete c.batchId;
|
|
916
|
+
if (hint) c.localHint = LOCAL_HINT;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
#writeLease(s) {
|
|
921
|
+
const f = path.join(sub("leases"), `${s.key}.json`);
|
|
922
|
+
if (!s.delivered) {
|
|
923
|
+
try {
|
|
924
|
+
fs.unlinkSync(f);
|
|
925
|
+
} catch {}
|
|
926
|
+
clearTickets(s.key);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
// a local delivery's token lives in the server's memory only: no `status --ticket` can hand it to an agent
|
|
930
|
+
if (!s.delivered.local) saveTicket(s.key, { batchId: s.delivered.batch.batch_id, holder: s.delivered.lease.holder, token: s.delivered.lease.token });
|
|
931
|
+
writeStateFile(f, JSON.stringify({ holder: s.delivered.lease.holder, expiresAt: s.delivered.lease.expiresAt, baseSha256: s.delivered.preSha }));
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
#renew(s) {
|
|
935
|
+
s.delivered.lease.expiresAt = this.now() + LEASE_MS();
|
|
936
|
+
this.#writeLease(s);
|
|
937
|
+
this.#changed(s);
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/** Called on a timer: a delivered batch whose lease expired goes back to pending (stranded) and the document unlocks. */
|
|
941
|
+
tick() {
|
|
942
|
+
for (const s of this.map.values()) if (s.delivered && s.delivered.lease.expiresAt <= this.now()) this.#expire(s);
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* The background agent stopped while it held this batch (a crash, a refusal): end the delivery now instead of at lease expiry, with the
|
|
947
|
+
* same steps as an expiry. A run that wrote is recorded with Revert; one that did not is pending again. Only the delivery's own holder.
|
|
948
|
+
*/
|
|
949
|
+
releaseAgent(key, holder) {
|
|
950
|
+
const s = this.get(key);
|
|
951
|
+
if (!s.delivered) throw new HttpError(409, "no batch is in progress for this file", { code: "no_batch" });
|
|
952
|
+
if (!isBgHolder(holder) || s.delivered.local || s.delivered.lease.holder !== holder) throw new HttpError(403, "another agent holds the lease for this batch", { code: "not_lease_holder" });
|
|
953
|
+
this.#expire(s);
|
|
954
|
+
return s;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
#expire(s) {
|
|
958
|
+
const local = !!s.delivered.local; // a run on this Mac: no agent in it
|
|
959
|
+
const wrote = this.#recordMidRun(s, local ? "The change on this Mac stopped after it changed the file." : "The agent stopped responding after changing the file.", "lease_expired_mid_run");
|
|
960
|
+
if (!s.delivered.accept) s.pending = s.delivered.batch; // a local run's batch was decided at its delivery: it goes to the agent now
|
|
961
|
+
this.#backToDraft(s, s.delivered, !wrote);
|
|
962
|
+
s.edits = [...s.delivered.edits, ...s.edits];
|
|
963
|
+
const accepted = !!s.delivered.accept;
|
|
964
|
+
s.delivered = null;
|
|
965
|
+
// comments queued behind the expired run have no batch to ride in: they go back to drafts (with their note) instead of waiting for a done that cannot come
|
|
966
|
+
this.#queuedToDrafts(s);
|
|
967
|
+
s.activity.unshift({ id: `r_${crypto.randomBytes(4).toString("hex")}`, kind: "lease_expired", at: this.now(), summary: local ? (accepted ? (wrote ? "The change on this Mac stopped after it changed the file. Revert restores the earlier version." : "The change on this Mac did not finish. Nothing was written.") : "The change on this Mac did not finish. Your request is waiting for an agent.") : "The agent stopped responding. The batch is waiting for the next poll.", flags: [], results: [], ...(local ? { by: "local" } : {}) });
|
|
968
|
+
this.#writeLease(s);
|
|
969
|
+
this.#changed(s, wrote ? "reload" : "state");
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
// ---- agent write -----------------------------------------------------------------------------------------
|
|
973
|
+
/** Moves the delivered batch to `stage` (see view().stage) and tells the page. No-op without a delivery or when it is already there. */
|
|
974
|
+
setStage(key, stage) {
|
|
975
|
+
const s = this.get(key);
|
|
976
|
+
if (!s.delivered || s.delivered.stage === stage) return;
|
|
977
|
+
s.delivered.stage = stage;
|
|
978
|
+
this.#changed(s);
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
/**
|
|
982
|
+
* Who may act on the delivered batch. A stale agent (its lease expired and the batch went to another poll) carries the old delivery's
|
|
983
|
+
* token, so even the same batch id cannot be completed or written by it. Writers (agentWrite, noteApply) always pass `strict`: holder,
|
|
984
|
+
* batch id and token are all required, a missing one is `missing_lease`. `done` is strict at the server route.
|
|
985
|
+
*/
|
|
986
|
+
#assertLease(s, { holder, token, batchId, strict = false }, { staleOnHolder = false } = {}) {
|
|
987
|
+
const d = s.delivered;
|
|
988
|
+
if (strict && (!holder || !token || !batchId)) throw new HttpError(400, "holder, batch_id and lease_token are required: they come from the output of `poll`", { code: "missing_lease" });
|
|
989
|
+
const stale = () => new HttpError(409, "this is not your delivery: the batch was taken back, finished or handed to another agent. Run poll again.", { code: "stale_batch" });
|
|
990
|
+
if (batchId != null && batchId !== d.batch.batch_id) throw stale();
|
|
991
|
+
if (holder != null && d.lease.holder !== holder) {
|
|
992
|
+
if (staleOnHolder) throw stale();
|
|
993
|
+
throw new HttpError(403, "another agent holds the lease for this batch", { code: "not_lease_holder" });
|
|
994
|
+
}
|
|
995
|
+
if (token != null && token !== d.lease.token) throw stale();
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* `apply` through a live session: the single authority for the write. Accepted only while a batch is delivered (state working or
|
|
1000
|
+
* queued: both hold a live lease) and only for the lease holder; compare-and-swap on the disk hash; renews the lease.
|
|
1001
|
+
*/
|
|
1002
|
+
agentWrite(key, { baseSha, bytes, holder, token, batchId, label = "before agent write", route = "commit", ops = null, partial = false }) {
|
|
1003
|
+
const s = this.get(key);
|
|
1004
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
1005
|
+
const state = this.state(s);
|
|
1006
|
+
if (!s.delivered) throw new HttpError(409, `no batch is delivered to an agent (state: ${state}); run poll and wait for the user's Send`, { code: "not_working", state });
|
|
1007
|
+
this.#assertLease(s, { holder, token, batchId, strict: true });
|
|
1008
|
+
const readOnly = readOnlyKind(s.delivered.batch);
|
|
1009
|
+
if (readOnly) throw new HttpError(409, READ_ONLY[readOnly], { code: `${readOnly}_only` });
|
|
1010
|
+
// `commit` does not know the hash its candidate was edited from: the base is the last hash this server wrote or served for the delivery
|
|
1011
|
+
if (baseSha == null) baseSha = s.delivered.knownSha ?? s.delivered.preSha;
|
|
1012
|
+
if (!bytes?.length) throw new HttpError(400, "no bytes");
|
|
1013
|
+
if (bytes.length > MAX_BYTES) throw new HttpError(413, "document too large");
|
|
1014
|
+
try {
|
|
1015
|
+
validatePackage(bytes, s.format);
|
|
1016
|
+
} catch (err) {
|
|
1017
|
+
throw new HttpError(422, `refusing to write: ${err.message}`, { code: "invalid_package" });
|
|
1018
|
+
}
|
|
1019
|
+
let disk;
|
|
1020
|
+
try {
|
|
1021
|
+
checkRegularOwned(s.file);
|
|
1022
|
+
disk = readCapped(s.file);
|
|
1023
|
+
} catch (err) {
|
|
1024
|
+
throw new HttpError(409, `cannot read the file: ${err.message}`, { code: "gone" });
|
|
1025
|
+
}
|
|
1026
|
+
const diskSha = sha256(disk);
|
|
1027
|
+
if (diskSha !== baseSha) throw this.#outsideChange(s, { side: "agent", bytes, diskSha, baseSha, message: "the file changed on disk since apply read it" });
|
|
1028
|
+
this.setStage(key, "writing"); // only once the checks passed: a refused write does not claim to be writing
|
|
1029
|
+
const info = this.#replace(s, bytes, { expectSha: diskSha, label, side: "agent" });
|
|
1030
|
+
const newSha = sha256(bytes);
|
|
1031
|
+
// what the merge may rely on later: the ops `apply` says it ran (checked against the bytes when a merge needs them), or that it was a commit
|
|
1032
|
+
const checked = route === "apply" && s.format === "xlsx" && Array.isArray(ops) && validateOps({ schema: "onionskin.ops/1", ops }).ok; // only the XLSX merge reads them
|
|
1033
|
+
this.#logWrite(s, checked ? { from: diskSha, to: newSha, route, ops, partial: partial === true, anchors: s.delivered.batch.untrusted_document_evidence?.anchors || [] } : { from: diskSha, to: newSha, route: route === "apply" ? "apply" : "commit", ops: null });
|
|
1034
|
+
s.delivered.knownSha = newSha;
|
|
1035
|
+
// the pre-agent version is what Revert restores: keep it the newest entry so many agent writes cannot prune it
|
|
1036
|
+
s.snapshots.touch(s.delivered.preSha);
|
|
1037
|
+
s.lastWrite = info;
|
|
1038
|
+
this.#renew(s);
|
|
1039
|
+
return { ok: true, sha256: newSha, lease_expires_at: s.delivered.lease.expiresAt };
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/**
|
|
1043
|
+
* What `apply` could not do (refused by the engine, protected, anchor or expectation mismatch), told by the lease holder after each run
|
|
1044
|
+
* (also when nothing was written). Shown to the user with the activity entry at `done`.
|
|
1045
|
+
*/
|
|
1046
|
+
noteApply(key, { holder, token, batchId, run = "", problems = [], applied = [] }) {
|
|
1047
|
+
const s = this.get(key);
|
|
1048
|
+
if (!s.delivered) throw new HttpError(409, "no batch is delivered to an agent", { code: "not_working" });
|
|
1049
|
+
this.#assertLease(s, { holder, token, batchId, strict: true });
|
|
1050
|
+
this.setStage(key, "writing");
|
|
1051
|
+
// problems are keyed by op: a later run replaces what an earlier run said about the same op, and an op that now applied clears it
|
|
1052
|
+
const fresh = problems.slice(0, 40).map((p) => cleanProblem(p, run));
|
|
1053
|
+
const settled = new Set([...fresh.map((p) => p.key), ...applied.slice(0, 200).map((k) => untrusted(k, 80))]);
|
|
1054
|
+
s.delivered.applyProblems = [...(s.delivered.applyProblems || []).filter((p) => !settled.has(p.key)), ...fresh].slice(0, 40);
|
|
1055
|
+
this.#changed(s);
|
|
1056
|
+
return { ok: true };
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
// ---- done ------------------------------------------------------------------------------------------------
|
|
1060
|
+
/** The bytes `done` will analyze (null when the pre-agent version is gone), for the server to analyze off the event loop. Asserts the lease first. */
|
|
1061
|
+
analysisInputs(key, claim) {
|
|
1062
|
+
const s = this.get(key);
|
|
1063
|
+
if (!s.delivered) throw new HttpError(409, "no batch is in progress for this file (taken back or already finished)", { code: "no_batch" });
|
|
1064
|
+
this.#assertLease(s, claim, { staleOnHolder: true });
|
|
1065
|
+
this.setStage(key, "checking");
|
|
1066
|
+
let post;
|
|
1067
|
+
let pre;
|
|
1068
|
+
try {
|
|
1069
|
+
post = readCapped(s.file);
|
|
1070
|
+
pre = s.snapshots.read(s.delivered.preSha);
|
|
1071
|
+
} catch {
|
|
1072
|
+
return null; // done() states what it could not read
|
|
1073
|
+
}
|
|
1074
|
+
const readOnly = readOnlyKind(s.delivered.batch);
|
|
1075
|
+
if (readOnly === "voice") return null; // a voice run writes nothing; a file changed anyway is recorded by done, not analyzed
|
|
1076
|
+
if (readOnly === "review" && sha256(post) === s.delivered.preSha) return null; // a review that wrote nothing: nothing to check
|
|
1077
|
+
// deferSoffice: the LibreOffice conversion runs after `done` returns (startLateSoffice); ONIONSKIN_SOFFICE_SYNC=1 keeps it inline
|
|
1078
|
+
return { pre, post, format: s.format, postSha: sha256(post), batchId: s.delivered.batch.batch_id, deferSoffice: env("SOFFICE_SYNC", "") !== "1" };
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
done(key, results, claim = {}, analysis = null) {
|
|
1082
|
+
const s = this.get(key);
|
|
1083
|
+
if (!s.delivered) throw new HttpError(409, "no batch is in progress for this file (taken back or already finished)", { code: "no_batch" });
|
|
1084
|
+
this.#assertLease(s, claim, { staleOnHolder: true });
|
|
1085
|
+
if (!results || typeof results !== "object") throw new HttpError(400, "results must be an object");
|
|
1086
|
+
const list = Array.isArray(results.results) ? results.results : [];
|
|
1087
|
+
const run = s.delivered;
|
|
1088
|
+
let post;
|
|
1089
|
+
try {
|
|
1090
|
+
post = readCapped(s.file);
|
|
1091
|
+
} catch (err) {
|
|
1092
|
+
throw new HttpError(409, `cannot read the file: ${err.message}`);
|
|
1093
|
+
}
|
|
1094
|
+
const postSha = sha256(post);
|
|
1095
|
+
if (readOnlyKind(run.batch) === "voice") return this.#doneVoice(s, results, run, post, postSha);
|
|
1096
|
+
// a review that wrote nothing ends with cards. One whose file changed anyway (a direct save past review_only) is finished as a run below:
|
|
1097
|
+
// its write is flagged and can be reverted, and no card is kept
|
|
1098
|
+
if (run.batch.instructions?.review && postSha === run.preSha) return this.#doneReview(s, results, run, post);
|
|
1099
|
+
s.snapshots.add(post, "after agent");
|
|
1100
|
+
|
|
1101
|
+
// ---- plan: everything that can throw happens here, on locals; the session is not touched until the commit below ------
|
|
1102
|
+
let pre;
|
|
1103
|
+
try {
|
|
1104
|
+
pre = s.snapshots.read(run.preSha);
|
|
1105
|
+
} catch {
|
|
1106
|
+
pre = null;
|
|
1107
|
+
}
|
|
1108
|
+
// the analysis normally comes from the worker (server route); a call without one, or with one for other bytes, analyzes inline
|
|
1109
|
+
let found;
|
|
1110
|
+
if (!pre) found = { flags: [{ code: "no_pre_snapshot", severity: "warn" }], checks: { node: "skipped", deep: { status: "skipped", reason: "no pre-agent snapshot" }, notice: "Checks skipped: the pre-agent version is no longer stored." }, changes: null };
|
|
1111
|
+
else if (analysis && analysis.postSha === postSha && analysis.batchId === run.batch.batch_id) found = analysis;
|
|
1112
|
+
else found = analyze(pre, post, s.format);
|
|
1113
|
+
const flags = [...found.flags];
|
|
1114
|
+
const { checks, changes } = found;
|
|
1115
|
+
// the file must be what this server last wrote (apply/commit) or delivered; anything else changed it behind the lease (a direct save by
|
|
1116
|
+
// the agent's own library, or another program): the lost-update class the lease cannot see. Error severity, so Revert is recommended.
|
|
1117
|
+
if (postSha !== (run.knownSha ?? run.preSha)) flags.push({ code: "file_changed_outside_onionskin", severity: "error", detail: directSave(run.local) });
|
|
1118
|
+
if (run.outsideChange) flags.push({ code: "outside_change", severity: "warn", detail: run.local ? (run.outsideChange.copyName ? `The file was changed outside Onionskin while the change was made on this Mac; the changed version was saved as a copy: ${run.outsideChange.copyName}` : "The file was changed outside Onionskin while the change was made on this Mac; the change was not applied.") : run.outsideChange.copyName ? `The file was changed outside Onionskin while the agent worked; the agent's version was saved as a copy: ${run.outsideChange.copyName}` : "The file was changed outside Onionskin while the agent worked; the agent's write was not applied." });
|
|
1119
|
+
for (const p of run.applyProblems || []) flags.push({ code: p.status === "skipped_protected" ? "apply_skipped_protected" : "apply_not_applied", severity: p.status === "skipped_protected" ? "info" : "warn", op: p.op, status: p.status, ...(p.code ? { reason_code: p.code } : {}), detail: p.reason });
|
|
1120
|
+
const byId = new Map(s.comments.map((c) => [c.id, c]));
|
|
1121
|
+
const entries = [];
|
|
1122
|
+
const updates = [];
|
|
1123
|
+
const asks = this.#askPlan(s, run);
|
|
1124
|
+
for (const r of list.slice(0, 1000)) {
|
|
1125
|
+
const c = byId.get(String(r.comment_id));
|
|
1126
|
+
if (!c || !["done", "declined", "asked"].includes(r.status) || (r.status === "asked" && !asks.allowed(c))) {
|
|
1127
|
+
flags.push({ code: "unknown_result", severity: "warn", comment_id: String(r.comment_id).slice(0, 64) });
|
|
1128
|
+
continue;
|
|
1129
|
+
}
|
|
1130
|
+
if (r.status === "asked") {
|
|
1131
|
+
const q = asks.take(c, r);
|
|
1132
|
+
if (q.question) {
|
|
1133
|
+
updates.push({ c, status: "asked", result: undefined, question: q.question });
|
|
1134
|
+
entries.push({ comment_id: c.id, status: "asked", note: "" });
|
|
1135
|
+
} else {
|
|
1136
|
+
// the batch is not lost: the comment is declined with a fixed note, never the agent's text
|
|
1137
|
+
flags.push({ code: "bad_question", severity: "warn", comment_id: c.id, reason: q.code });
|
|
1138
|
+
updates.push({ c, status: "declined", result: BAD_QUESTION_NOTE });
|
|
1139
|
+
entries.push({ comment_id: c.id, status: "declined", note: BAD_QUESTION_NOTE });
|
|
1140
|
+
}
|
|
1141
|
+
continue;
|
|
1142
|
+
}
|
|
1143
|
+
const note = userText(r.note ?? "", 1000);
|
|
1144
|
+
updates.push({ c, status: r.status, result: note });
|
|
1145
|
+
entries.push({ comment_id: c.id, status: r.status, note });
|
|
1146
|
+
}
|
|
1147
|
+
for (const c of run.batch.instructions.comments) {
|
|
1148
|
+
if (!entries.some((e) => e.comment_id === c.id)) flags.push({ code: "no_result_for_comment", severity: "warn", comment_id: c.id });
|
|
1149
|
+
}
|
|
1150
|
+
// style.json changed by something else while this batch was out (an agent with a shell, say): already put back by src/style.js
|
|
1151
|
+
this.#guardFlags(run, flags);
|
|
1152
|
+
if (results.voice_draft !== undefined) flags.push({ code: "voice_draft_not_allowed", severity: "warn" }); // only a voice batch takes a draft; nothing is stored
|
|
1153
|
+
const queuedPlan = this.#planQueued(s, post, postSha, flags);
|
|
1154
|
+
const cleanFlags = flags.map(cleanFlag);
|
|
1155
|
+
const { list: ignored, more: ignoredMore } = ignoredInstructions(results.document_instructions_ignored);
|
|
1156
|
+
const entry = {
|
|
1157
|
+
id: `r_${crypto.randomBytes(4).toString("hex")}`, kind: "agent", at: this.now(), batchId: run.batch.batch_id,
|
|
1158
|
+
summary: userText(typeof results.summary === "string" ? results.summary : "", 400), results: entries,
|
|
1159
|
+
// text in the document that tried to instruct the agent: agent-supplied, so sanitized like other untrusted text (5 items of 200 chars)
|
|
1160
|
+
...(ignored.length ? { documentInstructionsIgnored: ignored, ...(ignoredMore ? { documentInstructionsIgnoredMore: true } : {}) } : {}), flags: cleanFlags, checks,
|
|
1161
|
+
revertRecommended: cleanFlags.some((f) => f.severity === "error"),
|
|
1162
|
+
changes,
|
|
1163
|
+
preSha: run.preSha, postSha, reverted: false,
|
|
1164
|
+
// a run on this Mac (src/local.js): the page titles it "Done on this Mac"; its note comes back to the box if the person reverts it
|
|
1165
|
+
...(run.local ? { by: "local", localNote: userText(run.batch.instructions?.overall_note || "") } : {}),
|
|
1166
|
+
};
|
|
1167
|
+
|
|
1168
|
+
// ---- commit: assignments only ------------------------------------------------------------------------------------
|
|
1169
|
+
for (const u of updates) {
|
|
1170
|
+
u.c.status = u.status;
|
|
1171
|
+
u.c.result = u.result;
|
|
1172
|
+
if (u.question) u.c.question = u.question;
|
|
1173
|
+
}
|
|
1174
|
+
if (run.accept) {
|
|
1175
|
+
this.#settleCards(s, run, Array.isArray(results.accepted) ? results.accepted : []);
|
|
1176
|
+
s.edits = [...run.edits, ...s.edits]; // an accepted suggestion carried none of the person's edits: they ride with the next Send
|
|
1177
|
+
}
|
|
1178
|
+
s.activity.unshift(entry);
|
|
1179
|
+
addUpdate(s, { kind: run.local ? "local" : "agent", before: run.preSha, after: postSha, summary: entry.summary, model: run.model === true }, this.now());
|
|
1180
|
+
s.histTip = postSha;
|
|
1181
|
+
s.baseSha = postSha;
|
|
1182
|
+
s.delivered = null;
|
|
1183
|
+
s.conflict = null;
|
|
1184
|
+
if (s.queued) {
|
|
1185
|
+
s.queued = false;
|
|
1186
|
+
if (queuedPlan) {
|
|
1187
|
+
for (const c of queuedPlan.queued) {
|
|
1188
|
+
c.durable = queuedPlan.durables.get(c.id);
|
|
1189
|
+
c.status = "sent";
|
|
1190
|
+
c.batchId = queuedPlan.batch.batch_id;
|
|
1191
|
+
}
|
|
1192
|
+
s.pending = queuedPlan.batch;
|
|
1193
|
+
s.queuedNote = "";
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
this.#writeLease(s);
|
|
1197
|
+
this.#deliverIfPolled(s);
|
|
1198
|
+
this.#changed(s, "reload");
|
|
1199
|
+
return { ok: true, flags: cleanFlags, checks };
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
/**
|
|
1203
|
+
* The LibreOffice conversion result for a version `done` already accepted (activity entry checks.deep.soffice === "pending"). Recorded against
|
|
1204
|
+
* the entry of the version it checked. A failure that found something is an error flag (Revert offered like any error flag from done) only
|
|
1205
|
+
* while that run is still the latest and the file is still its version; after a newer write it is a warning on that run and changes nothing.
|
|
1206
|
+
* Never throws; false when the entry or session is gone or the entry was already settled.
|
|
1207
|
+
*/
|
|
1208
|
+
recordLateCheck(key, { batchId, postSha, result }) {
|
|
1209
|
+
let s;
|
|
1210
|
+
try {
|
|
1211
|
+
s = this.get(key);
|
|
1212
|
+
} catch {
|
|
1213
|
+
return false;
|
|
1214
|
+
}
|
|
1215
|
+
const entry = s.activity.find((a) => a.kind === "agent" && a.batchId === batchId && a.postSha === postSha && a.checks?.deep?.soffice === "pending");
|
|
1216
|
+
if (!entry) return false;
|
|
1217
|
+
// current: still the latest run and the file is still its version. A next batch handed out meanwhile (comments queued during the run) changes nothing until its agent writes.
|
|
1218
|
+
const d = s.delivered;
|
|
1219
|
+
const current = s.baseSha === postSha && s.activity.find((a) => a.kind === "agent" && !a.reverted) === entry && (!d || (d.preSha === postSha && d.knownSha === postSha));
|
|
1220
|
+
const found = (result.flags || []).map((f) => cleanFlag(current || f.severity !== "error" ? f : { ...f, severity: "warn", detail: `${f.detail || ""} (the file has changed since this run)`.trim() }));
|
|
1221
|
+
const deep = { ...entry.checks.deep };
|
|
1222
|
+
delete deep.soffice;
|
|
1223
|
+
const skipped = [...(deep.skipped || [])];
|
|
1224
|
+
if (result.ran) deep.checks = [...(deep.checks || []), "soffice-convert"];
|
|
1225
|
+
else skipped.push({ check: "soffice-convert", reason: untrusted(result.reason || "did not run", 120) });
|
|
1226
|
+
deep.skipped = skipped;
|
|
1227
|
+
entry.checks = { ...entry.checks, deep, notice: deepNotice(skipped) };
|
|
1228
|
+
entry.flags = [...entry.flags, ...found];
|
|
1229
|
+
if (found.some((f) => f.severity === "error")) entry.revertRecommended = true;
|
|
1230
|
+
this.#changed(s);
|
|
1231
|
+
return true;
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
/**
|
|
1235
|
+
* Who `done` may mark `asked`, and how many. Only a comment of the delivered batch (never an unsent draft), never one that already
|
|
1236
|
+
* carries an answer (one question per comment), and at most QUESTIONS_PER_BATCH per `done` (the rule `reply` can check up front).
|
|
1237
|
+
* Ceiling: questions left open from two runs and answered together put more than three entries in one batch's `questions[]`; counting
|
|
1238
|
+
* open questions here would refuse asks that `reply` accepted. Pure: reads the session, changes nothing.
|
|
1239
|
+
*/
|
|
1240
|
+
#askPlan(s, run) {
|
|
1241
|
+
const inBatch = new Map(run.batch.instructions.comments.map((c) => [c.id, c]));
|
|
1242
|
+
let open = 0;
|
|
1243
|
+
return {
|
|
1244
|
+
allowed: (c) => inBatch.has(c.id) && c.batchId === run.batch.batch_id,
|
|
1245
|
+
take: (c, r) => {
|
|
1246
|
+
if (inBatch.get(c.id).answer || c.answer) return { code: "already_asked" };
|
|
1247
|
+
const q = cleanQuestion({ ask: r.ask, options: r.options });
|
|
1248
|
+
if (!q.question) return q;
|
|
1249
|
+
if (open >= QUESTIONS_PER_BATCH) return { code: "too_many_questions" };
|
|
1250
|
+
open++;
|
|
1251
|
+
return q;
|
|
1252
|
+
},
|
|
1253
|
+
};
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
/**
|
|
1257
|
+
* The person tapped an option on a question card. `choice` is the index of the option (checked against the options this server
|
|
1258
|
+
* stored; option text is never taken from the page), or null for "Something else". An index keeps the question and sets the answer;
|
|
1259
|
+
* null drops the question. Either way the comment is a draft again, and the page sends it as usual.
|
|
1260
|
+
*/
|
|
1261
|
+
answer(key, { id, choice }) {
|
|
1262
|
+
const s = this.get(key);
|
|
1263
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
1264
|
+
const c = s.comments.find((x) => x.id === id);
|
|
1265
|
+
if (!c || c.status !== "asked" || !c.question) throw new HttpError(409, "this comment has no open question", { code: "not_asked" });
|
|
1266
|
+
if (choice !== null && !(Number.isInteger(choice) && choice >= 0 && choice < c.question.options.length)) throw new HttpError(400, "choice must be the number of one of the options, or null", { code: "bad_choice" });
|
|
1267
|
+
if (choice === null) {
|
|
1268
|
+
delete c.question;
|
|
1269
|
+
delete c.answer;
|
|
1270
|
+
} else c.answer = { choice };
|
|
1271
|
+
c.status = "draft";
|
|
1272
|
+
delete c.result;
|
|
1273
|
+
delete c.batchId;
|
|
1274
|
+
this.#changed(s, "draft");
|
|
1275
|
+
return s;
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
/** Comments queued while the agent worked become the next pending batch, anchored on the file the agent leaves. Pure planning: touches no session state; re-anchoring problems are pushed onto `flags`. */
|
|
1279
|
+
#planQueued(s, post, postSha, flags) {
|
|
1280
|
+
let plan = null;
|
|
1281
|
+
if (s.queued) {
|
|
1282
|
+
const queued = s.comments.filter((c) => c.status === "queued");
|
|
1283
|
+
if (queued.length || s.queuedNote) {
|
|
1284
|
+
let units = null;
|
|
1285
|
+
try {
|
|
1286
|
+
units = readUnits(s.format, post);
|
|
1287
|
+
} catch (err) {
|
|
1288
|
+
flags.push({ code: "queued_reanchor_failed", severity: "warn", detail: err.message });
|
|
1289
|
+
}
|
|
1290
|
+
const durables = new Map();
|
|
1291
|
+
for (const c of queued) {
|
|
1292
|
+
let d = null;
|
|
1293
|
+
try {
|
|
1294
|
+
if (units) d = reanchor(units, c);
|
|
1295
|
+
} catch (err) {
|
|
1296
|
+
flags.push({ code: "queued_reanchor_failed", severity: "warn", comment_id: c.id, detail: err.message });
|
|
1297
|
+
}
|
|
1298
|
+
durables.set(c.id, d);
|
|
1299
|
+
}
|
|
1300
|
+
const batch = buildBatch({ file: s.file, format: s.format, baseSha: postSha, note: s.queuedNote, comments: queued.map((c) => ({ ...c, durable: durables.get(c.id) })), edits: [], style: styleNote(), guide: voiceGuide() });
|
|
1301
|
+
plan = { queued, durables, batch };
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
return plan;
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
// ---- review first (idea 21, src/review.js) ------------------------------------------------------------------
|
|
1308
|
+
/**
|
|
1309
|
+
* The person tapped a review button. The batch carries the fixed sentence for `kind` and the code-built outline of the file on disk; the
|
|
1310
|
+
* outline's index (where each unit is, its text hash) stays here. Refused while a batch waits or is delivered, and when the disk is not
|
|
1311
|
+
* the version the session knows. It goes to an agent, never to the local runner.
|
|
1312
|
+
*/
|
|
1313
|
+
requestReview(key, kind) {
|
|
1314
|
+
const s = this.get(key);
|
|
1315
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
1316
|
+
if (!REVIEW_KINDS.includes(kind)) throw new HttpError(400, `kind must be one of ${REVIEW_KINDS.join(", ")}`, { code: "bad_kind" });
|
|
1317
|
+
if (s.delivered || s.pending) throw new HttpError(409, s.delivered?.local ? "Wait for the change on this Mac to finish, then try again." : "Wait for the agent to finish, then try again.", { code: "busy" });
|
|
1318
|
+
let disk;
|
|
1319
|
+
try {
|
|
1320
|
+
checkRegularOwned(s.file);
|
|
1321
|
+
disk = readCapped(s.file);
|
|
1322
|
+
} catch (err) {
|
|
1323
|
+
throw new HttpError(409, `cannot read the file: ${err.message}`, { code: "gone" });
|
|
1324
|
+
}
|
|
1325
|
+
if (sha256(disk) !== s.baseSha) throw new HttpError(409, "The file changed outside Onionskin. Reload it, then try again.", { code: "changed_since" });
|
|
1326
|
+
s.pending = this.#reviewBatch(s, kind, disk, s.baseSha);
|
|
1327
|
+
this.#deliverIfPolled(s);
|
|
1328
|
+
this.#changed(s);
|
|
1329
|
+
return s;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
#reviewBatch(s, kind, bytes, baseSha) {
|
|
1333
|
+
const { units, more, index } = unitsOf(s.format, bytes);
|
|
1334
|
+
const batch = buildBatch({ file: s.file, format: s.format, baseSha, note: "", comments: [], edits: [], review: { kind, ask: REVIEW_ASKS[kind] }, units, unitsMore: more });
|
|
1335
|
+
s.review = { batchId: batch.batch_id, kind, units: units.map((u) => ({ id: u.id, protected: u.protected })), index };
|
|
1336
|
+
s.decidedBatch = batch.batch_id; // never the local runner's: only an agent reads the file
|
|
1337
|
+
return batch;
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
/** `done` of a review that wrote nothing: the suggestions become cards (checked against the file), an activity entry of kind "review", no history update. */
|
|
1341
|
+
#doneReview(s, results, run, post) {
|
|
1342
|
+
const flags = [];
|
|
1343
|
+
const ix = s.review?.batchId === run.batch.batch_id ? s.review : null;
|
|
1344
|
+
const { cards, dropped } = ix ? cardsFrom(results.suggestions, { bytes: post, format: s.format, units: ix.units, index: ix.index, reviewId: run.batch.batch_id }) : { cards: [], dropped: (Array.isArray(results.suggestions) ? results.suggestions : []).map((_, i) => ({ index: i, reason: "the review's outline is gone" })) };
|
|
1345
|
+
for (const d of dropped) flags.push({ code: "bad_suggestion", severity: "warn", ...(d.index != null ? { index: d.index } : {}), reason: d.reason });
|
|
1346
|
+
if (results.voice_draft !== undefined) flags.push({ code: "voice_draft_not_allowed", severity: "warn" });
|
|
1347
|
+
const queuedPlan = this.#planQueued(s, post, run.preSha, flags);
|
|
1348
|
+
const cleanFlags = flags.map(cleanFlag);
|
|
1349
|
+
const { list: ignored, more: ignoredMore } = ignoredInstructions(results.document_instructions_ignored); // a review reads the whole file: its report counts most here
|
|
1350
|
+
const entry = {
|
|
1351
|
+
...(ignored.length ? { documentInstructionsIgnored: ignored, ...(ignoredMore ? { documentInstructionsIgnoredMore: true } : {}) } : {}),
|
|
1352
|
+
id: `r_${crypto.randomBytes(4).toString("hex")}`, kind: "review", at: this.now(), batchId: run.batch.batch_id, reviewKind: run.batch.instructions.review.kind,
|
|
1353
|
+
summary: reviewSummary(cards.length), results: [], flags: cleanFlags, checks: null, changes: null, suggestions: cards.length, suggestionsDropped: dropped.length,
|
|
1354
|
+
preSha: run.preSha, postSha: run.preSha, reverted: false,
|
|
1355
|
+
};
|
|
1356
|
+
// ---- commit
|
|
1357
|
+
for (const g of s.suggestions || []) if (g.status === "open") g.status = "replaced";
|
|
1358
|
+
s.suggestions = [...cards, ...(s.suggestions || [])].slice(0, 3 * SUGGESTIONS_MAX);
|
|
1359
|
+
s.activity.unshift(entry);
|
|
1360
|
+
s.edits = [...run.edits, ...s.edits]; // the review carried none of the person's edits: they ride with the next Send
|
|
1361
|
+
s.delivered = null;
|
|
1362
|
+
s.review = null;
|
|
1363
|
+
if (s.queued) {
|
|
1364
|
+
s.queued = false;
|
|
1365
|
+
if (queuedPlan) {
|
|
1366
|
+
for (const c of queuedPlan.queued) {
|
|
1367
|
+
c.durable = queuedPlan.durables.get(c.id);
|
|
1368
|
+
c.status = "sent";
|
|
1369
|
+
c.batchId = queuedPlan.batch.batch_id;
|
|
1370
|
+
}
|
|
1371
|
+
s.pending = queuedPlan.batch;
|
|
1372
|
+
s.queuedNote = "";
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
this.#writeLease(s);
|
|
1376
|
+
this.#deliverIfPolled(s);
|
|
1377
|
+
this.#changed(s); // nothing changed in the file: no reload
|
|
1378
|
+
return { ok: true, flags: cleanFlags, checks: null, suggestions: cards.length, dropped: dropped.length };
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
* The person tapped a card button: `choice` 0 = Accept (edit cards), 1 = Reject or Dismiss. "Show me" never reaches the server. Only ids
|
|
1383
|
+
* and an integer come from the page. Accept runs the stored ops on this Mac (startLocal: the lease, apply with --partial, done, Revert);
|
|
1384
|
+
* the cards stay open until that run settles them (accepted, or stale when the text there changed).
|
|
1385
|
+
*/
|
|
1386
|
+
answerSuggestions(key, { ids, choice }) {
|
|
1387
|
+
const s = this.get(key);
|
|
1388
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
1389
|
+
if (choice !== 0 && choice !== 1) throw new HttpError(400, "choice must be 0 (accept) or 1 (reject)", { code: "bad_choice" });
|
|
1390
|
+
if (!Array.isArray(ids) || !ids.length || ids.length > SUGGESTIONS_MAX || !ids.every((x) => typeof x === "string") || new Set(ids).size !== ids.length) throw new HttpError(400, `ids must list 1 to ${SUGGESTIONS_MAX} different suggestion ids`, { code: "bad_ids" });
|
|
1391
|
+
const cards = ids.map((id) => (s.suggestions || []).find((g) => g.id === id));
|
|
1392
|
+
if (cards.some((g) => !g)) throw new HttpError(400, "no such suggestion", { code: "unknown_suggestion" });
|
|
1393
|
+
if (cards.some((g) => g.status !== "open")) throw new HttpError(409, "this suggestion was already answered", { code: "not_open" });
|
|
1394
|
+
if (choice === 1) {
|
|
1395
|
+
for (const g of cards) g.status = g.kind === "note" ? "dismissed" : "rejected";
|
|
1396
|
+
this.#changed(s);
|
|
1397
|
+
return s;
|
|
1398
|
+
}
|
|
1399
|
+
if (cards.some((g) => g.kind !== "edit" || !g.op)) throw new HttpError(400, "a note cannot be accepted", { code: "not_acceptable" });
|
|
1400
|
+
if (s.delivered) throw new HttpError(423, s.delivered?.local ? "Wait for the change on this Mac to finish, then try again." : "Wait for the agent to finish, then try again.", { code: "locked" });
|
|
1401
|
+
if (s.pending) throw new HttpError(409, s.delivered?.local ? "Wait for the change on this Mac to finish, then try again." : "Wait for the agent to finish, then try again.", { code: "busy" });
|
|
1402
|
+
let disk;
|
|
1403
|
+
try {
|
|
1404
|
+
checkRegularOwned(s.file);
|
|
1405
|
+
disk = readCapped(s.file);
|
|
1406
|
+
} catch (err) {
|
|
1407
|
+
throw new HttpError(409, `cannot read the file: ${err.message}`, { code: "gone" });
|
|
1408
|
+
}
|
|
1409
|
+
if (sha256(disk) !== s.baseSha) throw new HttpError(409, "The file changed outside Onionskin. Reload it, then try again.", { code: "changed_since" });
|
|
1410
|
+
const batch = buildBatch({ file: s.file, format: s.format, baseSha: s.baseSha, note: "", comments: [], edits: [] });
|
|
1411
|
+
const n = cards.length;
|
|
1412
|
+
this.startLocal(key, batch, { ops: cards.map((g) => g.op), summary: `Accepted ${n} suggestion${n === 1 ? "" : "s"} from the agent's review.` });
|
|
1413
|
+
s.delivered.suggestionIds = ids; // settled by done or dropLocal
|
|
1414
|
+
this.#save(s);
|
|
1415
|
+
return s;
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
/**
|
|
1419
|
+
* "Use this" on a quick fix (idea 10 option A, src/suggest.js). `suggestion` is the server's stored record ({task, quote, location,
|
|
1420
|
+
* before, after}, from memory, handed over by src/suggest.js); the page sends only ids, its comments and its bytes. The page's unsent edits
|
|
1421
|
+
* are taken first, exactly as a Send takes them (#takePage); then one comment, whose words must be the server's own stamp words for the
|
|
1422
|
+
* task and whose anchor must be the one the suggestion was made for, becomes a batch of its own. The local runner writes it through
|
|
1423
|
+
* `reply` (one find and replace inside that paragraph or text box, the anchor's text hash as `expect`), apply's checks, the lease, `done`
|
|
1424
|
+
* and Revert, as for any local run. The text written is the stored suggestion, never text from the page. Every refusal happens before
|
|
1425
|
+
* anything is written. Other comments and the overall note stay for the next Send; typed edits ride with it too.
|
|
1426
|
+
*/
|
|
1427
|
+
useSuggestion(key, { suggestion: rec, commentId, comments, note, baseSha, bytes = null, edits = [] }) {
|
|
1428
|
+
const s = this.get(key);
|
|
1429
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
1430
|
+
if (s.delivered || s.pending) throw new HttpError(409, "The agent started working on this file, so nothing was written. Ask the agent instead, or try again when it is done.", { code: "busy" });
|
|
1431
|
+
const words = rec && Object.hasOwn(STAMP_WORDS, rec.task) && rec.task !== "translate" ? STAMP_WORDS[rec.task] : null;
|
|
1432
|
+
if (!words || typeof rec.before !== "string" || typeof rec.after !== "string") throw new HttpError(409, "This suggestion is out of date. Tap the stamp again.", { code: "expired" });
|
|
1433
|
+
const id = String(commentId ?? "").slice(0, 64);
|
|
1434
|
+
const mine = Array.isArray(comments) ? comments.find((c) => c && String(c.id) === id) : null;
|
|
1435
|
+
const old = s.comments.find((c) => c.id === id);
|
|
1436
|
+
const sameAnchor = (a) => a && typeof a === "object" && a.quote === rec.quote && JSON.stringify(a.location ?? null) === JSON.stringify(rec.location ?? null);
|
|
1437
|
+
if (!mine || userText(mine.text) !== words || !sameAnchor(mine.anchor) || (old && old.status !== "draft")) throw new HttpError(409, "This comment is not the suggestion's stamp.", { code: "not_this_comment" });
|
|
1438
|
+
|
|
1439
|
+
this.setDraft(key, { comments, note: note ?? s.note });
|
|
1440
|
+
const c = s.comments.find((x) => x.id === id);
|
|
1441
|
+
const anchor = { location: c.anchor.location, quote: c.anchor.quote, prefix: c.anchor.prefix, suffix: c.anchor.suffix };
|
|
1442
|
+
let plan = null;
|
|
1443
|
+
// the place and the reply are worked out on the very bytes that are about to be the file; any refusal here writes nothing
|
|
1444
|
+
const check = (b) => {
|
|
1445
|
+
const units = readUnits(s.format, b);
|
|
1446
|
+
const durable = buildDurable(units, anchor);
|
|
1447
|
+
const place = placeOf(units, durable, rec.quote);
|
|
1448
|
+
if (!place.ok) throw new HttpError(409, place.code === "protected" ? "This part of the file is protected, so it cannot be changed here." : LOCAL_HINT, { code: place.code });
|
|
1449
|
+
const reply = buildUseReply(rec, id, place.text);
|
|
1450
|
+
const batch = buildBatch({ file: s.file, format: s.format, baseSha: sha256(b), note: "", comments: [{ ...c, durable }], edits: [] });
|
|
1451
|
+
if (!reply || batch.instructions.comments[0]?.reply_with !== "find_replace_or_text") throw new HttpError(409, LOCAL_HINT, { code: "not_here" });
|
|
1452
|
+
plan = { durable, reply, batch };
|
|
1453
|
+
};
|
|
1454
|
+
let taken;
|
|
1455
|
+
try {
|
|
1456
|
+
taken = this.#takePage(s, { baseSha, bytes }, check);
|
|
1457
|
+
} catch (err) {
|
|
1458
|
+
// nothing was written: the comment stays a draft, and says it can go to the agent
|
|
1459
|
+
if (["not_here", "not_eligible", "protected"].includes(err?.extra?.code)) {
|
|
1460
|
+
c.localHint = LOCAL_HINT;
|
|
1461
|
+
this.#changed(s);
|
|
1462
|
+
}
|
|
1463
|
+
throw err;
|
|
1464
|
+
}
|
|
1465
|
+
for (const e of Array.isArray(edits) ? edits.slice(0, 2000) : []) s.edits.push({ kind: e.kind, before: e.before, after: e.after, label: e.label, location: e.location });
|
|
1466
|
+
c.durable = plan.durable;
|
|
1467
|
+
c.status = "sent";
|
|
1468
|
+
c.batchId = plan.batch.batch_id;
|
|
1469
|
+
delete c.localHint;
|
|
1470
|
+
s.lastWrite = taken.writeInfo;
|
|
1471
|
+
try {
|
|
1472
|
+
this.startLocal(key, plan.batch, { reply: plan.reply, summary: USE_SUMMARY, model: true }); // saves, tells the page, starts the runner
|
|
1473
|
+
} catch (err) {
|
|
1474
|
+
// not reachable after the checks above; if it were, the page's bytes are kept (they are the person's) and the comment is a draft again
|
|
1475
|
+
c.status = "draft";
|
|
1476
|
+
delete c.batchId;
|
|
1477
|
+
this.#changed(s);
|
|
1478
|
+
throw err;
|
|
1479
|
+
}
|
|
1480
|
+
return s;
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
/** An accepted run ended: its cards whose op applied are "accepted", the rest "stale". `applied` are op ids the runner read from apply's report. */
|
|
1484
|
+
#settleCards(s, run, applied) {
|
|
1485
|
+
const ids = new Set(run?.suggestionIds || []);
|
|
1486
|
+
const ok = new Set(applied.filter((x) => ids.has(x)));
|
|
1487
|
+
for (const g of s.suggestions || []) {
|
|
1488
|
+
if (!ids.has(g.id) || g.status !== "open") continue;
|
|
1489
|
+
g.status = ok.has(g.id) ? "accepted" : "stale";
|
|
1490
|
+
if (ok.has(g.id)) g.acceptedBatch = run.batch.batch_id;
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
// ---- "Build my voice guide" (stage 2, src/voice-learn.js builds the batch) ------------------------------------------------
|
|
1495
|
+
/**
|
|
1496
|
+
* The person asked the agent for a draft of their voice guide. `batch` is buildVoiceBatch's (src/batch.js): code constants, their
|
|
1497
|
+
* approved guide and preferences, and the samples they ticked as evidence. Refused while a batch waits or is delivered, and when the file
|
|
1498
|
+
* on disk or the session moved on since the samples were taken. It goes to an agent, never to the local runner. While it is delivered
|
|
1499
|
+
* nothing may be written (409 voice_only), and `done` keeps the draft for the person's review (#doneVoice).
|
|
1500
|
+
*/
|
|
1501
|
+
requestVoice(key, batch) {
|
|
1502
|
+
const s = this.get(key);
|
|
1503
|
+
if (s.closed) throw new HttpError(409, "session closed", { code: "closed" });
|
|
1504
|
+
if (readOnlyKind(batch) !== "voice") throw new HttpError(400, "not a voice guide request", { code: "bad_request" });
|
|
1505
|
+
if (s.delivered || s.pending) throw new HttpError(409, s.delivered?.local ? "Wait for the change on this Mac to finish, then try again." : "Wait for the agent to finish, then try again.", { code: "busy" });
|
|
1506
|
+
let disk;
|
|
1507
|
+
try {
|
|
1508
|
+
checkRegularOwned(s.file);
|
|
1509
|
+
disk = readCapped(s.file);
|
|
1510
|
+
} catch (err) {
|
|
1511
|
+
throw new HttpError(409, `cannot read the file: ${err.message}`, { code: "gone" });
|
|
1512
|
+
}
|
|
1513
|
+
// a file changed outside Onionskin would otherwise be blamed on the voice run when it ends; samples taken from an older version are not sent
|
|
1514
|
+
if (sha256(disk) !== s.baseSha || batch.base_sha256 !== s.baseSha) throw new HttpError(409, "The file changed outside Onionskin. Reload it, then try again.", { code: "changed_since" });
|
|
1515
|
+
s.pending = batch;
|
|
1516
|
+
s.decidedBatch = batch.batch_id; // never the local runner's: only an agent drafts a guide
|
|
1517
|
+
this.#deliverIfPolled(s);
|
|
1518
|
+
this.#changed(s);
|
|
1519
|
+
return s;
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
/**
|
|
1523
|
+
* `done` of a voice batch (the trust boundary for the draft). The draft is agent text: it is checked by cleanVoiceDraft and stored as
|
|
1524
|
+
* an untrusted draft (voice-draft.json) for the person to read, edit and approve, never copied into the saved guide: only their Save on
|
|
1525
|
+
* the page does that. Every other field the agent sends (a guide, `on`, `save`, results) changes nothing. An activity entry of kind
|
|
1526
|
+
* "voice" with a fixed summary, no history update and no analysis. A file that changed anyway (a direct save past voice_only) is
|
|
1527
|
+
* recorded as a withdrawn run would be: flagged, kept Revert-able.
|
|
1528
|
+
*/
|
|
1529
|
+
#doneVoice(s, results, run, post, postSha) {
|
|
1530
|
+
const flags = [];
|
|
1531
|
+
for (const r of (Array.isArray(results.results) ? results.results : []).slice(0, 1000)) flags.push({ code: "unknown_result", severity: "warn", comment_id: String(r?.comment_id ?? "").slice(0, 64) });
|
|
1532
|
+
const check = cleanVoiceDraft(results.voice_draft); // size first, then hidden characters, links and markup, the layout, the caps
|
|
1533
|
+
if (check.code) flags.push({ code: "bad_voice_draft", severity: "warn", reason: check.code });
|
|
1534
|
+
this.#guardFlags(run, flags);
|
|
1535
|
+
const queuedPlan = this.#planQueued(s, post, postSha, flags);
|
|
1536
|
+
const { list: ignored, more: ignoredMore } = ignoredInstructions(results.document_instructions_ignored);
|
|
1537
|
+
// ---- commit: the draft file first (a failed write keeps nothing and says so), then the session
|
|
1538
|
+
let kept = false;
|
|
1539
|
+
if (!check.code) {
|
|
1540
|
+
try {
|
|
1541
|
+
writeDraft({ batch_id: run.batch.batch_id, text: results.voice_draft });
|
|
1542
|
+
kept = true;
|
|
1543
|
+
} catch {
|
|
1544
|
+
flags.push({ code: "voice_draft_not_saved", severity: "warn" });
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
const cleanFlags = flags.map(cleanFlag);
|
|
1548
|
+
const wrote = postSha !== run.preSha && this.#recordMidRun(s, "The file changed while the agent drafted your voice guide.", "voice_run_changed_file");
|
|
1549
|
+
s.activity.unshift({
|
|
1550
|
+
...(ignored.length ? { documentInstructionsIgnored: ignored, ...(ignoredMore ? { documentInstructionsIgnoredMore: true } : {}) } : {}),
|
|
1551
|
+
id: `r_${crypto.randomBytes(4).toString("hex")}`, kind: "voice", at: this.now(), batchId: run.batch.batch_id,
|
|
1552
|
+
summary: kept ? VOICE_KEPT : VOICE_REFUSED, results: [], flags: cleanFlags, checks: null, changes: null,
|
|
1553
|
+
preSha: run.preSha, postSha: run.preSha, reverted: false,
|
|
1554
|
+
});
|
|
1555
|
+
s.edits = [...run.edits, ...s.edits]; // the voice batch carried none of the person's edits: they ride with the next Send
|
|
1556
|
+
s.delivered = null;
|
|
1557
|
+
if (s.queued) {
|
|
1558
|
+
s.queued = false;
|
|
1559
|
+
if (queuedPlan) {
|
|
1560
|
+
for (const c of queuedPlan.queued) {
|
|
1561
|
+
c.durable = queuedPlan.durables.get(c.id);
|
|
1562
|
+
c.status = "sent";
|
|
1563
|
+
c.batchId = queuedPlan.batch.batch_id;
|
|
1564
|
+
}
|
|
1565
|
+
s.pending = queuedPlan.batch;
|
|
1566
|
+
s.queuedNote = "";
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
this.#writeLease(s);
|
|
1570
|
+
this.#deliverIfPolled(s);
|
|
1571
|
+
this.#changed(s, wrote ? "reload" : "state"); // nothing changed in the file: no reload (as a review)
|
|
1572
|
+
return { ok: true, flags: cleanFlags, checks: null, voice_draft: kept ? "kept" : "refused" };
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
/** style.json and voice-guide.json changed by something else while this run was out (an agent with a shell, say): already put back by their stores. */
|
|
1576
|
+
#guardFlags(run, flags) {
|
|
1577
|
+
if (this.#styleSeen.has(run) && styleChangedOutside() > this.#styleSeen.get(run)) flags.push({ code: "style_changed_outside_onionskin", severity: "warn", detail: "Your writing preferences were changed outside Onionskin, so they were put back." });
|
|
1578
|
+
if (this.#guideSeen.has(run) && guideChangedOutside() > this.#guideSeen.get(run)) flags.push({ code: "voice_guide_changed_outside_onionskin", severity: "warn", detail: "Your voice guide was changed outside Onionskin, so it was put back." });
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
// ---- take back / revert / close -------------------------------------------------------------------------
|
|
1582
|
+
takeBack(key) {
|
|
1583
|
+
const s = this.get(key);
|
|
1584
|
+
const batch = s.delivered?.batch || s.pending;
|
|
1585
|
+
if (!batch) throw new HttpError(409, "nothing to take back");
|
|
1586
|
+
// the note travels back with the comments: the withdrawn batch's note, then a queued one, then whatever was typed since
|
|
1587
|
+
const notes = [batch.instructions?.overall_note, s.queuedNote, s.note].filter(Boolean);
|
|
1588
|
+
s.note = [...new Set(notes)].join("\n\n");
|
|
1589
|
+
s.queuedNote = "";
|
|
1590
|
+
let event = "state";
|
|
1591
|
+
if (s.delivered) {
|
|
1592
|
+
// the typed edits stay as evidence for the next batch: they were saved into the file before this batch went out
|
|
1593
|
+
s.edits = [...s.delivered.edits, ...s.edits];
|
|
1594
|
+
if (this.#recordMidRun(s, s.delivered.local ? "Taken back mid-run: the change on this Mac had already changed the file." : "Taken back mid-run: the agent had already changed the file.", "taken_back_mid_run")) event = "reload";
|
|
1595
|
+
}
|
|
1596
|
+
for (const c of s.comments) if (["sent", "queued"].includes(c.status)) c.status = "draft";
|
|
1597
|
+
s.delivered = null;
|
|
1598
|
+
s.pending = null;
|
|
1599
|
+
s.conflict = null;
|
|
1600
|
+
s.queued = false;
|
|
1601
|
+
this.#writeLease(s);
|
|
1602
|
+
this.#changed(s, event);
|
|
1603
|
+
return s;
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
/**
|
|
1607
|
+
* The agent may have written before its batch was withdrawn (Take back, lease expiry): the page still shows the pre-agent copy. When the
|
|
1608
|
+
* disk differs from the pre-agent snapshot, keep that run Revert-able (an `agent` entry with the original preSha), make the new bytes the
|
|
1609
|
+
* base and say so; the caller emits `reload`. Returns whether anything changed on disk.
|
|
1610
|
+
* Ceiling: after a lease expiry the redelivered batch starts from these partial bytes, so Revert on ITS run returns to the partial state.
|
|
1611
|
+
*/
|
|
1612
|
+
#recordMidRun(s, summary, code) {
|
|
1613
|
+
let disk = null;
|
|
1614
|
+
try {
|
|
1615
|
+
disk = readCapped(s.file);
|
|
1616
|
+
} catch {
|
|
1617
|
+
return false;
|
|
1618
|
+
}
|
|
1619
|
+
if (sha256(disk) === s.delivered.preSha) return false;
|
|
1620
|
+
try {
|
|
1621
|
+
s.snapshots.add(disk, "after agent (withdrawn)");
|
|
1622
|
+
} catch {} // a full disk must not block withdrawing; Revert will say the version is not stored
|
|
1623
|
+
const postSha = sha256(disk);
|
|
1624
|
+
const flags = [{ code, severity: "warn" }];
|
|
1625
|
+
// the same rule as done: bytes this server never wrote or delivered came from a direct save, not the agent's apply or commit
|
|
1626
|
+
if (postSha !== (s.delivered.knownSha ?? s.delivered.preSha)) flags.push({ code: "file_changed_outside_onionskin", severity: "error", detail: directSave(s.delivered.local) });
|
|
1627
|
+
s.activity.unshift({
|
|
1628
|
+
id: `r_${crypto.randomBytes(4).toString("hex")}`, kind: "agent", at: this.now(), batchId: s.delivered.batch.batch_id,
|
|
1629
|
+
summary, results: [], flags, checks: null,
|
|
1630
|
+
revertRecommended: true, changes: null, preSha: s.delivered.preSha, postSha, reverted: false,
|
|
1631
|
+
...(s.delivered.local ? { by: "local", localNote: userText(s.delivered.batch.instructions?.overall_note || "") } : {}),
|
|
1632
|
+
});
|
|
1633
|
+
addUpdate(s, { kind: s.delivered.local ? "local" : "agent", before: s.delivered.preSha, after: postSha, summary }, this.now());
|
|
1634
|
+
s.histTip = postSha;
|
|
1635
|
+
s.baseSha = postSha;
|
|
1636
|
+
return true;
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
revert(key, activityId) {
|
|
1640
|
+
const s = this.get(key);
|
|
1641
|
+
if (s.delivered) throw new HttpError(423, s.delivered.local ? "a change is being made on this Mac" : "the agent is working", { code: "locked" });
|
|
1642
|
+
const entry = s.activity.find((a) => a.id === activityId && a.kind === "agent");
|
|
1643
|
+
if (!entry || entry.reverted) throw new HttpError(404, "nothing to revert");
|
|
1644
|
+
const mac = entry.by === "local"; // a change made on this Mac: no agent was involved, so the words do not say one was
|
|
1645
|
+
const latest = s.activity.find((a) => a.kind === "agent" && !a.reverted);
|
|
1646
|
+
if (latest !== entry) throw new HttpError(409, mac ? "only the latest change can be reverted" : "only the latest agent run can be reverted", { code: "not_latest" });
|
|
1647
|
+
const disk = readCapped(s.file);
|
|
1648
|
+
if (sha256(disk) !== entry.postSha) throw new HttpError(409, mac ? "the file changed after the change was made; revert refused" : "the file changed after the agent finished; revert refused", { code: "changed_since" });
|
|
1649
|
+
if (!s.snapshots.has(entry.preSha)) throw new HttpError(410, mac ? "the version before this change is no longer stored" : "the pre-agent version is no longer stored");
|
|
1650
|
+
const pre = s.snapshots.read(entry.preSha);
|
|
1651
|
+
this.#replace(s, pre, { expectSha: entry.postSha, label: "before revert" }); // a revert is a new version
|
|
1652
|
+
s.snapshots.add(pre, "reverted");
|
|
1653
|
+
s.baseSha = entry.preSha;
|
|
1654
|
+
addUpdate(s, { kind: "revert", before: entry.postSha, after: entry.preSha, local: mac }, this.now());
|
|
1655
|
+
s.histTip = entry.preSha;
|
|
1656
|
+
entry.reverted = true;
|
|
1657
|
+
// the run's comments were answered against a version that no longer exists: they are drafts again
|
|
1658
|
+
for (const c of s.comments) {
|
|
1659
|
+
if (c.batchId === entry.batchId && ["done", "declined", "asked"].includes(c.status)) {
|
|
1660
|
+
if (c.status === "asked") delete c.question; // asked about a version that no longer exists
|
|
1661
|
+
c.status = "draft";
|
|
1662
|
+
delete c.result;
|
|
1663
|
+
delete c.batchId;
|
|
1664
|
+
if (entry.by === "local") c.skipLocal = true; // the person undid what this Mac did: the next Send asks the agent
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
for (const g of s.suggestions || []) if (g.status === "accepted" && g.acceptedBatch === entry.batchId) g.status = "open"; // the text is back: it can be accepted again
|
|
1668
|
+
if (entry.by === "local" && entry.localNote && !s.note) {
|
|
1669
|
+
s.note = entry.localNote; // the request comes back to the box, and the same words go to the agent next time
|
|
1670
|
+
s.skipLocalNote = entry.localNote;
|
|
1671
|
+
}
|
|
1672
|
+
// a batch built against the reverted version is rebuilt against the restored bytes (a review: its outline is read again; a voice
|
|
1673
|
+
// request keeps its samples, which left out what that run wrote, and points at the restored version)
|
|
1674
|
+
if (readOnlyKind(s.pending) === "voice") s.pending = { ...s.pending, base_sha256: entry.preSha };
|
|
1675
|
+
else if (s.pending?.instructions?.review) s.pending = this.#reviewBatch(s, s.pending.instructions.review.kind, pre, entry.preSha);
|
|
1676
|
+
else if (s.pending) {
|
|
1677
|
+
const ids = new Set(s.pending.instructions.comments.map((c) => c.id));
|
|
1678
|
+
const cs = s.comments.filter((c) => ids.has(c.id));
|
|
1679
|
+
let units = null;
|
|
1680
|
+
try {
|
|
1681
|
+
units = readUnits(s.format, pre);
|
|
1682
|
+
} catch {
|
|
1683
|
+
units = null;
|
|
1684
|
+
}
|
|
1685
|
+
for (const c of cs) {
|
|
1686
|
+
try {
|
|
1687
|
+
if (units) c.durable = reanchor(units, c);
|
|
1688
|
+
} catch {
|
|
1689
|
+
c.durable = null;
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
s.pending = buildBatch({ file: s.file, format: s.format, baseSha: entry.preSha, note: s.pending.instructions.overall_note, comments: cs, edits: [], style: styleNote(), guide: voiceGuide() });
|
|
1693
|
+
for (const c of cs) c.batchId = s.pending.batch_id;
|
|
1694
|
+
}
|
|
1695
|
+
s.activity.unshift({ id: `r_${crypto.randomBytes(4).toString("hex")}`, kind: "revert", at: this.now(), summary: mac ? "Reverted to the version before this change." : "Reverted to the version before the agent's run.", flags: [], results: [], postSha: entry.preSha });
|
|
1696
|
+
this.#changed(s, "reload");
|
|
1697
|
+
return s;
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
close(key) {
|
|
1701
|
+
const s = this.get(key);
|
|
1702
|
+
s.closed = true;
|
|
1703
|
+
this.editors.delete(key); // the closed tab may stay on screen with its event stream open: it must not hold the file against the tab that reopens it
|
|
1704
|
+
for (const p of s.pollers.splice(0)) p.resolve({ status: "closed" });
|
|
1705
|
+
fs.rmSync(path.join(stateDir(), "agents", s.key, "outline"), { recursive: true, force: true }); // the outline files of Onionskin's own agent
|
|
1706
|
+
this.emit("closed", s); // the server ends background checks for this file
|
|
1707
|
+
this.#changed(s);
|
|
1708
|
+
return s;
|
|
1709
|
+
}
|
|
1710
|
+
|
|
1711
|
+
/**
|
|
1712
|
+
* The page loads the file (first load, "Reload the changed file", a reload of the tab). When the disk holds a version history does not
|
|
1713
|
+
* know, someone changed it outside Onionskin and the user is now taking that version: store it and record an "outside" update
|
|
1714
|
+
* (before = the last version history knew, after = these bytes). Never while an agent works (the disk changes then by design).
|
|
1715
|
+
* Returns the same {diskSha, baseSha, bytes} as diskCheck.
|
|
1716
|
+
*/
|
|
1717
|
+
loadForPage(key) {
|
|
1718
|
+
const out = this.diskCheck(key);
|
|
1719
|
+
const s = this.get(key);
|
|
1720
|
+
if (!s.delivered && !s.closed && s.histTip && out.diskSha !== s.histTip) {
|
|
1721
|
+
try {
|
|
1722
|
+
s.snapshots.add(out.bytes, "outside change");
|
|
1723
|
+
addUpdate(s, { kind: "outside", before: s.histTip, after: out.diskSha }, this.now());
|
|
1724
|
+
s.histTip = out.diskSha;
|
|
1725
|
+
this.#save(s);
|
|
1726
|
+
} catch {} // no space for the snapshot: nothing is recorded, the file still loads
|
|
1727
|
+
}
|
|
1728
|
+
return out;
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
/** The history list and one update (browser routes, read-only; see src/history.js). */
|
|
1732
|
+
history(key) {
|
|
1733
|
+
return historyList(this.get(key));
|
|
1734
|
+
}
|
|
1735
|
+
async historyUpdate(key, id) {
|
|
1736
|
+
const d = await historyDetail(this.get(key), id);
|
|
1737
|
+
if (!d) throw new HttpError(404, "no such update", { code: "no_such_update" });
|
|
1738
|
+
return d;
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
// ---- the tracked-changes copy (M4 Idea 16, docs/contracts/tracked-changes.md) ------------------------------------------
|
|
1742
|
+
/**
|
|
1743
|
+
* The versions a tracked-changes copy covers. `id` is one agent run (only while its result is the file on disk) or "all": everything
|
|
1744
|
+
* History holds, from the oldest stored update to the current version, refused when the chain has a gap or holds an outside change.
|
|
1745
|
+
* Returns {before, after, author, date}; throws HttpError with a plain message.
|
|
1746
|
+
*/
|
|
1747
|
+
#exportSpan(s, id) {
|
|
1748
|
+
const vis = visibleUpdates(s); // newest first
|
|
1749
|
+
const run = (u) => u.kind === "agent" || u.kind === "local"; // a run on this Mac is recorded like an agent's (activity kind "agent", by "local")
|
|
1750
|
+
const outsideRun = (u) => run(u) && (s.activity || []).some((a) => a.kind === "agent" && a.preSha === u.before && a.postSha === u.after && (a.flags || []).some((f) => f.code === "file_changed_outside_onionskin"));
|
|
1751
|
+
const refuse = (status, code, msg = TRACKED[code]) => new HttpError(status, msg, { code });
|
|
1752
|
+
let list;
|
|
1753
|
+
if (id === "all") {
|
|
1754
|
+
if (!vis.length) throw refuse(404, "nothing");
|
|
1755
|
+
list = vis.slice().reverse();
|
|
1756
|
+
for (let k = 0; k + 1 < list.length; k++) {
|
|
1757
|
+
if (list[k].after === list[k + 1].before) continue;
|
|
1758
|
+
// a break in the chain: an outside change no longer listed, or an update whose versions were pruned
|
|
1759
|
+
const from = s.history.indexOf(list[k]);
|
|
1760
|
+
const to = s.history.indexOf(list[k + 1]);
|
|
1761
|
+
if (s.history.slice(from + 1, to).some((u) => u.kind === "outside")) throw refuse(409, "outside");
|
|
1762
|
+
throw refuse(409, "unsupported", unsupportedText(["pruned"]));
|
|
1763
|
+
}
|
|
1764
|
+
if (list[list.length - 1].after !== s.histTip) throw refuse(409, "outside"); // the file on disk is a version History never recorded
|
|
1765
|
+
} else {
|
|
1766
|
+
const u = vis.find((x) => x.id === id);
|
|
1767
|
+
if (!u) throw new HttpError(404, "no such update", { code: "no_such_update" });
|
|
1768
|
+
if (!run(u)) throw refuse(409, "not_agent");
|
|
1769
|
+
if (u.after !== s.histTip) throw refuse(409, "not_current");
|
|
1770
|
+
list = [u];
|
|
1771
|
+
}
|
|
1772
|
+
if (list.some((u) => u.kind === "outside" || outsideRun(u))) throw refuse(409, "outside");
|
|
1773
|
+
const newest = list[list.length - 1];
|
|
1774
|
+
return { before: list[0].before, after: newest.after, author: list.every((u) => u.kind === "agent" || u.kind === "revert") ? AUTHORS.agent : AUTHORS.mixed, date: wordDate(newest.at) };
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
/**
|
|
1778
|
+
* "Save a copy with tracked changes": builds and checks the copy in a worker, then writes it NEXT TO the file (never over it, never the file
|
|
1779
|
+
* itself). Nothing is written unless every check passes: the span rules, the disk still holding the span's newest version before and after
|
|
1780
|
+
* the build, the builder's gates, the in-code round trip, and the deeper check when it is set up. Returns {ok, saved, name, path, ...}.
|
|
1781
|
+
*/
|
|
1782
|
+
async exportTracked(key, id) {
|
|
1783
|
+
const s = this.get(key);
|
|
1784
|
+
if (s.format !== "docx") throw new HttpError(409, TRACKED.not_docx, { code: "not_docx" });
|
|
1785
|
+
if (s.delivered) throw new HttpError(423, TRACKED.locked, { code: "locked" });
|
|
1786
|
+
if (this.exporting.has(key)) throw new HttpError(409, TRACKED.busy, { code: "busy" });
|
|
1787
|
+
const span = this.#exportSpan(s, id);
|
|
1788
|
+
this.exporting.add(key);
|
|
1789
|
+
try {
|
|
1790
|
+
const current = () => {
|
|
1791
|
+
if (s.delivered) throw new HttpError(423, TRACKED.locked, { code: "locked" });
|
|
1792
|
+
let disk = null;
|
|
1793
|
+
try {
|
|
1794
|
+
disk = sha256(readCapped(s.file));
|
|
1795
|
+
} catch {} // moved, removed or unreadable: not the version the copy is made from
|
|
1796
|
+
if (s.histTip !== span.after || disk !== span.after) throw new HttpError(409, id === "all" ? TRACKED.outside : TRACKED.not_current, { code: id === "all" ? "outside" : "not_current" });
|
|
1797
|
+
};
|
|
1798
|
+
current();
|
|
1799
|
+
if (!s.snapshots.has(span.before) || !s.snapshots.has(span.after)) throw new HttpError(409, unsupportedText(["pruned"]), { code: "unsupported" });
|
|
1800
|
+
const r = await exportInWorker({ before: s.snapshots.blob(span.before), after: s.snapshots.blob(span.after), author: span.author, date: span.date });
|
|
1801
|
+
if (r.timeout) throw new HttpError(409, TRACKED.timeout, { code: "timeout" });
|
|
1802
|
+
if (r.refused?.includes("has_revisions")) throw new HttpError(409, TRACKED.has_revisions, { code: "has_revisions" });
|
|
1803
|
+
if (r.refused?.includes("no_changes")) throw new HttpError(409, TRACKED.no_changes, { code: "no_changes" });
|
|
1804
|
+
if (r.refused) throw new HttpError(409, unsupportedText(r.refused), { code: "unsupported", reasons: r.refused });
|
|
1805
|
+
if (!r.ok) throw new HttpError(409, TRACKED.check_failed, { code: "check_failed" });
|
|
1806
|
+
const bytes = Buffer.from(r.bytes);
|
|
1807
|
+
const deep = await deepRedline(s.snapshots.read(span.before), s.snapshots.read(span.after), bytes);
|
|
1808
|
+
if (deep.ran && !deep.ok) throw new HttpError(409, TRACKED.check_failed, { code: "check_failed" });
|
|
1809
|
+
current(); // the agent, a Send or another program may have changed the file while the copy was made
|
|
1810
|
+
const notes = [];
|
|
1811
|
+
if (r.counts.formatting) notes.push(formattingText(r.counts.formatting));
|
|
1812
|
+
if (r.counts.moved) notes.push(TRACKED.moves);
|
|
1813
|
+
if (!deep.ran) notes.push(TRACKED.not_set_up);
|
|
1814
|
+
const counts = r.counts;
|
|
1815
|
+
let copy;
|
|
1816
|
+
try {
|
|
1817
|
+
copy = writeSiblingCopy(s.file, bytes, { label: "tracked changes" });
|
|
1818
|
+
} catch (err) {
|
|
1819
|
+
if (!["EACCES", "EPERM", "EROFS", "ENOSPC", "EDQUOT"].includes(err.code)) throw err;
|
|
1820
|
+
const ext = path.extname(s.file);
|
|
1821
|
+
const name = `${path.basename(s.file, ext)} (tracked changes)${ext}`; // from the session's own path, never document text
|
|
1822
|
+
if (bytes.length > 25 * 1024 * 1024) return { ok: true, saved: false, code: "folder_too_large", message: TRACKED.folder_too_large, counts, notes };
|
|
1823
|
+
return { ok: true, saved: false, code: "folder_not_writable", message: TRACKED.folder, download: { name, bytesB64: bytes.toString("base64") }, counts, notes };
|
|
1824
|
+
}
|
|
1825
|
+
this.createdCopies.add(copy.path);
|
|
1826
|
+
return { ok: true, saved: true, name: copy.name, path: copy.path, can_reveal: canReveal(), message: savedText(copy.name), counts, notes };
|
|
1827
|
+
} finally {
|
|
1828
|
+
this.exporting.delete(key);
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
/** Unlocked document, current disk hash vs the base we hold; used by the page after a reconnect. */
|
|
1833
|
+
diskCheck(key) {
|
|
1834
|
+
const s = this.get(key);
|
|
1835
|
+
const disk = readCapped(s.file);
|
|
1836
|
+
return { diskSha: sha256(disk), baseSha: s.baseSha, bytes: disk };
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
/**
|
|
1840
|
+
* Re-resolve durable anchors (comments + view) against the current disk bytes. DOCX results keep the shape docx.js returns; XLSX and
|
|
1841
|
+
* PPTX results are in terms of the current file (sheetIndex/sheetName/range, slideIndex/slideId/shapeId) so the page never reuses a stored location.
|
|
1842
|
+
*/
|
|
1843
|
+
resolveAll(key) {
|
|
1844
|
+
const s = this.get(key);
|
|
1845
|
+
const bytes = readCapped(s.file);
|
|
1846
|
+
const out = { baseSha: sha256(bytes), format: s.format, anchors: {}, view: null };
|
|
1847
|
+
const units = readUnits(s.format, bytes);
|
|
1848
|
+
for (const c of s.comments) {
|
|
1849
|
+
if (!c.durable) continue;
|
|
1850
|
+
let r = resolveDurableAny(units, c.durable);
|
|
1851
|
+
if (r.method === "none" && units.format === "pptx" && c.durable.pinFallback) {
|
|
1852
|
+
const f = resolveDurableAny(units, c.durable.pinFallback);
|
|
1853
|
+
if (f.method !== "none") r = { ...f, tieLost: true };
|
|
1854
|
+
}
|
|
1855
|
+
if (units.format === "docx" && r.method !== "none") r.heading = headingBefore(units.paragraphs, r.ordinal);
|
|
1856
|
+
out.anchors[c.id] = r;
|
|
1857
|
+
}
|
|
1858
|
+
if (s.format === "pptx") out.slides = units.deck.slides.map((x) => ({ index: x.index, id: x.id }));
|
|
1859
|
+
if (s.format === "docx") {
|
|
1860
|
+
if (s.view?.durable) out.view = { ...resolveDurable(units.paragraphs, s.view.durable), offsetPx: s.view.offsetPx, zoom: s.view.zoom, fraction: s.view.fraction };
|
|
1861
|
+
} else out.view = resolveView(units, s.view);
|
|
1862
|
+
out.sheets = s.format === "xlsx" ? units.wb.sheets.map((x) => ({ index: x.index, name: x.name, sheetId: x.sheetId })) : undefined;
|
|
1863
|
+
return out;
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1867
|
+
/**
|
|
1868
|
+
* `document_instructions_ignored` from the agent's results. Only strings count (anything else is dropped), each sanitized as untrusted text,
|
|
1869
|
+
* empty ones dropped and repeats ignoring case counted once. Looks at the first 50 raw items; keeps the first 5 distinct ones and says in `more`
|
|
1870
|
+
* when a sixth distinct one was reported. The count is the agent's own report: it is never proof of how many orders the file held.
|
|
1871
|
+
*/
|
|
1872
|
+
function ignoredInstructions(v) {
|
|
1873
|
+
const list = [];
|
|
1874
|
+
let more = false;
|
|
1875
|
+
if (!Array.isArray(v)) return { list, more };
|
|
1876
|
+
const seen = new Set();
|
|
1877
|
+
for (const x of v.slice(0, 50)) {
|
|
1878
|
+
if (typeof x !== "string") continue;
|
|
1879
|
+
const t = untrusted(x, 200);
|
|
1880
|
+
const k = t.toLowerCase();
|
|
1881
|
+
if (!t || seen.has(k)) continue;
|
|
1882
|
+
seen.add(k);
|
|
1883
|
+
if (list.length < 5) list.push(t);
|
|
1884
|
+
else more = true;
|
|
1885
|
+
}
|
|
1886
|
+
return { list, more };
|
|
1887
|
+
}
|
|
1888
|
+
|
|
1889
|
+
const EXPORT_TIMEOUT_MS = () => Number(env("EXPORT_TIMEOUT_MS", 60_000));
|
|
1890
|
+
/** Builds and checks one tracked-changes copy in a worker thread (src/redline-worker.js), so a large file never blocks the server. */
|
|
1891
|
+
function exportInWorker(job) {
|
|
1892
|
+
return new Promise((resolve) => {
|
|
1893
|
+
let worker;
|
|
1894
|
+
let settled = false;
|
|
1895
|
+
const finish = (r) => {
|
|
1896
|
+
if (settled) return;
|
|
1897
|
+
settled = true;
|
|
1898
|
+
clearTimeout(timer);
|
|
1899
|
+
worker?.terminate();
|
|
1900
|
+
resolve(r);
|
|
1901
|
+
};
|
|
1902
|
+
const timer = setTimeout(() => finish({ timeout: true }), EXPORT_TIMEOUT_MS());
|
|
1903
|
+
try {
|
|
1904
|
+
worker = new Worker(new URL("./redline-worker.js", import.meta.url), { workerData: job });
|
|
1905
|
+
} catch (err) {
|
|
1906
|
+
return finish({ check_failed: String(err?.message || err) });
|
|
1907
|
+
}
|
|
1908
|
+
worker.once("message", finish);
|
|
1909
|
+
worker.once("error", (err) => finish({ check_failed: String(err?.message || err) }));
|
|
1910
|
+
worker.once("exit", (code) => finish({ check_failed: `the worker exited (${code})` }));
|
|
1911
|
+
});
|
|
1912
|
+
}
|