onionskin-cli 0.0.3 → 0.0.5
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 +100 -0
- package/INSTALL.md +5 -1
- package/NOTICE +34 -0
- package/README.md +101 -33
- package/docs/contracts/batch.schema.json +27 -4
- package/docs/contracts/ops.schema.json +117 -5
- package/package.json +4 -2
- package/skills/onionskin/SKILL.md +9 -6
- package/skills/onionskin/reference/batch.md +5 -3
- package/skills/onionskin/reference/formats.md +5 -1
- package/skills/onionskin/reference/ops.md +47 -3
- package/skills/onionskin/reference/troubleshooting.md +31 -0
- package/src/agent-detect.js +28 -2
- package/src/agent-prompt.js +12 -1
- package/src/agent-runner.js +16 -5
- package/src/anchor-work.js +154 -0
- package/src/anchor-worker.js +38 -0
- package/src/anchors.js +61 -11
- package/src/apply.js +119 -47
- package/src/batch.js +32 -8
- package/src/check-worker.js +8 -5
- package/src/cli.js +91 -25
- package/src/constants.js +34 -1
- package/src/diff.js +3 -1
- package/src/docx.js +368 -58
- package/src/edit-hash.js +19 -0
- package/src/fsutil.js +1 -1
- package/src/history-worker.js +8 -8
- package/src/history.js +135 -31
- package/src/html-active.js +384 -0
- package/src/html-envelope.js +51 -0
- package/src/html-parser.js +8 -0
- package/src/html-render-worker.js +25 -0
- package/src/html-render.js +880 -0
- package/src/html-utf8.js +65 -0
- package/src/html-write-worker.js +14 -0
- package/src/html-write.js +380 -0
- package/src/html.js +1257 -0
- package/src/local.js +68 -4
- package/src/merge-package.js +1110 -0
- package/src/merge-pptx.js +98 -38
- package/src/merge-proof.js +1019 -0
- package/src/merge-worker.js +60 -0
- package/src/merge-xlsx.js +84 -29
- package/src/merge.js +286 -83
- package/src/nfc.js +46 -0
- package/src/notes.js +122 -0
- package/src/offload.js +400 -1
- package/src/open-worker.js +27 -0
- package/src/ops/common.js +2 -1
- package/src/ops/docx.js +58 -29
- package/src/ops/html.js +394 -0
- package/src/ops/pptx.js +74 -21
- package/src/ops/run.js +23 -3
- package/src/ops/runs.js +215 -13
- package/src/ops/schema.js +8 -1
- package/src/ops/xlsx.js +12 -7
- package/src/ops-worker.js +17 -0
- package/src/package.js +26 -2
- package/src/person.js +2 -1
- package/src/pptx.js +85 -21
- package/src/quick-plan.js +13 -6
- package/src/redline-worker.js +10 -14
- package/src/reply.js +46 -19
- package/src/retain.js +223 -0
- package/src/review.js +32 -18
- package/src/rewrite-check.js +2 -2
- package/src/sanitize.js +16 -3
- package/src/server.js +80 -36
- package/src/session.js +1138 -208
- package/src/stop-gate.js +34 -0
- package/src/suggest-reply.js +42 -7
- package/src/suggest.js +14 -29
- package/src/text.js +161 -11
- package/src/update.js +288 -0
- package/src/voice-guide.js +4 -3
- package/src/voice-learn.js +4 -3
- package/src/voice-sample-worker.js +7 -6
- package/src/voice-worker.js +4 -6
- package/src/worker-host.js +23 -0
- package/src/xlsx.js +355 -94
- package/src/xml-tree.js +6 -3
- package/src/xml.js +82 -34
- package/src/zip-write.js +2 -1
- package/src/zip.js +9 -5
- package/vendor/parse5/LICENSE.txt +32 -0
- package/vendor/parse5/parse5.mjs +7822 -0
- package/vendor/parse5.lock.json +23 -0
- package/web/dist/assets/index-DkNx0LSH.css +1 -0
- package/web/dist/assets/{index-DS-WST1E.js → index-DytKkMdi.js} +112 -108
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-BCAEKiOG.css +0 -1
package/src/update.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
// `onionskin update [--check] [--yes] [--force]`: brings this copy to the latest release, the way this copy was installed.
|
|
2
|
+
// The network is used only when a person (or their agent) runs this command: `--check` reads one small file, `version.txt`, from the
|
|
3
|
+
// release folder; `update` on an installer copy then runs the release's get.sh exactly as the README one-liner does (get.sh keeps the
|
|
4
|
+
// SHA-256 check). No prompts, no sudo; snapshots and the state folder are never deleted here.
|
|
5
|
+
//
|
|
6
|
+
// Everything this command needs after get.sh starts is imported at the top: install.sh deletes the app folder this process runs from.
|
|
7
|
+
import { spawnSync } from "node:child_process";
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import os from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { PRODUCT } from "./constants.js";
|
|
12
|
+
|
|
13
|
+
const BIN = PRODUCT.bin;
|
|
14
|
+
export const DEFAULT_BASE_URL = "https://github.com/abe238/onionskin/releases/latest/download";
|
|
15
|
+
export const VERSION_FILE = "version.txt";
|
|
16
|
+
export const NPM_COMMANDS = ["npm install -g onionskin-cli", `${BIN} setup --force`]; // README "### Stay up to date", word for word
|
|
17
|
+
const CURL = ["-fsSL", "--connect-timeout", "20", "--max-time", "30", "--proto-redir", "=https"]; // get.sh's own flags, a shorter time limit
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* get.sh's rule for its URLs, the same rule (scripts/get.sh url_ok; test/get-script.test.js checks both on one table): https://, file:///,
|
|
21
|
+
* or http:// to exactly 127.0.0.1 with an optional port, for local tests. No white space anywhere, and no "@" or backslash before the
|
|
22
|
+
* first "/" after the scheme: a user name hides the real host (http://127.0.0.1:80@evil.example/ is evil.example). Read as text, the way
|
|
23
|
+
* the shell rule reads it, not through a URL parser that may read the host differently than curl does.
|
|
24
|
+
*/
|
|
25
|
+
export function baseUrlOk(u) {
|
|
26
|
+
const s = String(u ?? "");
|
|
27
|
+
if (/\s/.test(s)) return false;
|
|
28
|
+
if (s.startsWith("file:///")) return true;
|
|
29
|
+
const m = /^(https?):\/\/([^/]*)/.exec(s);
|
|
30
|
+
if (!m || /[@\\]/.test(m[2])) return false;
|
|
31
|
+
return m[1] === "https" || /^127\.0\.0\.1(?::\d+)?$/.test(m[2]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** curl's --proto for a URL baseUrlOk accepted: plain http only for 127.0.0.1 (redirects stay https only, CURL). */
|
|
35
|
+
export const curlProto = (u) => ["--proto", String(u).startsWith("http://") ? "=https,file,http" : "=https,file"];
|
|
36
|
+
|
|
37
|
+
const VERSION_RE = /^(\d{1,6})\.(\d{1,6})\.(\d{1,6})(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
38
|
+
const MAX_VERSION = 40; // characters of a whole version
|
|
39
|
+
const MAX_DIGITS = 20; // digits of one pre-release number
|
|
40
|
+
|
|
41
|
+
/** `s` as a version's parts, or null: at most MAX_VERSION characters, and no pre-release number longer than MAX_DIGITS digits. */
|
|
42
|
+
function versionParts(s) {
|
|
43
|
+
const v = String(s || "");
|
|
44
|
+
const m = v.length <= MAX_VERSION ? VERSION_RE.exec(v) : null;
|
|
45
|
+
if (!m || (m[4] && m[4].split(".").some((x) => /^\d+$/.test(x) && x.length > MAX_DIGITS))) return null;
|
|
46
|
+
return m;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The first line of version.txt, or null when it is not a version (the file is untrusted: install.sh's character set, 40 characters at most). */
|
|
50
|
+
export function parseVersionText(text) {
|
|
51
|
+
const line = String(text || "").split(/\r?\n/)[0].trim();
|
|
52
|
+
return /^[0-9A-Za-z.+-]+$/.test(line) && versionParts(line) ? line : null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Two strings of digits compared as numbers of any size (no Number: past 2^53 it rounds): -1, 0 or 1. */
|
|
56
|
+
function compareDigits(x, y) {
|
|
57
|
+
x = x.replace(/^0+(?=\d)/, "");
|
|
58
|
+
y = y.replace(/^0+(?=\d)/, "");
|
|
59
|
+
if (x.length !== y.length) return x.length < y.length ? -1 : 1;
|
|
60
|
+
return x === y ? 0 : x < y ? -1 : 1;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** -1, 0 or 1 as `a` is older than, the same as or newer than `b` (numbers compared as numbers: 0.0.10 is newer than 0.0.9); null when either is not a version. */
|
|
64
|
+
export function compareVersions(a, b) {
|
|
65
|
+
const pa = versionParts(a);
|
|
66
|
+
const pb = versionParts(b);
|
|
67
|
+
if (!pa || !pb) return null;
|
|
68
|
+
for (let i = 1; i <= 3; i++) if (Number(pa[i]) !== Number(pb[i])) return Number(pa[i]) < Number(pb[i]) ? -1 : 1;
|
|
69
|
+
if (pa[4] === pb[4]) return 0;
|
|
70
|
+
if (!pa[4]) return 1; // 1.0.0 is newer than 1.0.0-rc.1
|
|
71
|
+
if (!pb[4]) return -1;
|
|
72
|
+
const xa = pa[4].split(".");
|
|
73
|
+
const xb = pb[4].split(".");
|
|
74
|
+
for (let i = 0; i < Math.max(xa.length, xb.length); i++) {
|
|
75
|
+
if (xa[i] === undefined) return -1;
|
|
76
|
+
if (xb[i] === undefined) return 1;
|
|
77
|
+
if (xa[i] === xb[i]) continue;
|
|
78
|
+
const na = /^\d+$/.test(xa[i]);
|
|
79
|
+
const nb = /^\d+$/.test(xb[i]);
|
|
80
|
+
if (na && nb) {
|
|
81
|
+
const c = compareDigits(xa[i], xb[i]);
|
|
82
|
+
if (c) return c;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (na !== nb) return na ? -1 : 1;
|
|
86
|
+
return xa[i] < xb[i] ? -1 : 1;
|
|
87
|
+
}
|
|
88
|
+
return 0;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const real = (p) => {
|
|
92
|
+
try {
|
|
93
|
+
return fs.realpathSync(p);
|
|
94
|
+
} catch {
|
|
95
|
+
return path.resolve(p);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* How the copy at `appRoot` was installed: "source" (a .git file or folder beside package.json; a worktree has a .git file), "installer"
|
|
101
|
+
* (~/.onionskin/app/<version>/, what install.sh makes), "npm" (…/node_modules/onionskin-cli) or "unknown" (an unzipped folder run directly).
|
|
102
|
+
*/
|
|
103
|
+
export function detectRoute({ appRoot, home }) {
|
|
104
|
+
const root = real(appRoot);
|
|
105
|
+
if (fs.existsSync(path.join(root, ".git")) && fs.existsSync(path.join(root, "package.json"))) return { route: "source", root };
|
|
106
|
+
if (path.dirname(root) === path.join(real(home), PRODUCT.stateDirName, "app")) return { route: "installer", root };
|
|
107
|
+
if (path.basename(root) === "onionskin-cli" && path.basename(path.dirname(root)) === "node_modules") return { route: "npm", root };
|
|
108
|
+
return { route: "unknown", root };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Reads <base>/version.txt with curl (one request; curl follows the release redirect, https only). Returns
|
|
113
|
+
* {version} | {missing: true, url} (the release has no version.txt, as releases up to 0.0.3) | {bad: true, url} | {unreachable: detail, url}.
|
|
114
|
+
*/
|
|
115
|
+
export function fetchLatest(base, { run = spawnSync } = {}) {
|
|
116
|
+
const url = `${String(base).replace(/\/+$/, "")}/${VERSION_FILE}`;
|
|
117
|
+
const r = run("curl", [...CURL, ...curlProto(url), url], { encoding: "utf8", timeout: 40_000, stdio: ["ignore", "pipe", "pipe"] });
|
|
118
|
+
if (r.error) return { unreachable: r.error.code === "ENOENT" ? "curl was not found" : r.error.message, url };
|
|
119
|
+
// the server answered 404 (curl says "returned error: 404", exit 22, or 56 over HTTP/2 as GitHub's redirect serves it); 37: no such file:// path
|
|
120
|
+
if (/returned error: 404\b/.test(r.stderr || "")) return { missing: true, url };
|
|
121
|
+
if (r.status === 37) return { missing: true, url };
|
|
122
|
+
if (r.status !== 0) return { unreachable: String(r.stderr || "").trim().split("\n").pop() || `curl exit ${r.status}`, url };
|
|
123
|
+
const version = parseVersionText(r.stdout);
|
|
124
|
+
return version ? { version, url } : { bad: true, url };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** What a running server holds that an update would cut off (from GET /cli/open-work): plain phrases, never file names. */
|
|
128
|
+
export function workAtRisk(w) {
|
|
129
|
+
const n = (k) => (Number.isInteger(w?.[k]) && w[k] > 0 ? w[k] : 0);
|
|
130
|
+
const out = [];
|
|
131
|
+
const plural = (c, one, many) => `${c} ${c === 1 ? one : many}`;
|
|
132
|
+
if (n("open_sessions")) out.push(plural(n("open_sessions"), "file open in a browser tab now or in the last 20 minutes (edits typed there stay in the tab until Send)", "files open in a browser tab now or in the last 20 minutes (edits typed there stay in the tab until Send)"));
|
|
133
|
+
if (n("unsent_edits")) out.push(plural(n("unsent_edits"), "unsent edit", "unsent edits"));
|
|
134
|
+
if (n("draft_comments")) out.push(plural(n("draft_comments"), "draft comment", "draft comments"));
|
|
135
|
+
if (n("batches")) out.push(plural(n("batches"), "batch that was sent and is not finished", "batches that were sent and are not finished"));
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function parseUpdateArgs(args) {
|
|
140
|
+
const a = { check: false, yes: false, force: false };
|
|
141
|
+
for (const x of args) {
|
|
142
|
+
if (x === "--check") a.check = true;
|
|
143
|
+
else if (x === "--yes") a.yes = true;
|
|
144
|
+
else if (x === "--force") a.force = true;
|
|
145
|
+
else if (x.startsWith("-")) throw new Error(`unknown option ${x}`);
|
|
146
|
+
else throw new Error(`unexpected argument ${x}`);
|
|
147
|
+
}
|
|
148
|
+
return a;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @param {string[]} args the arguments after `update`
|
|
153
|
+
* @param {object} d version (running), appRoot, home, env, print(line), and the parts that act:
|
|
154
|
+
* fetchLatest(base), server: {healthy(), openWork() -> {status, body}}, stop(), runGetSh(base) -> exit status,
|
|
155
|
+
* launcherVersion(route, root) -> string|null (the installed command's --version), run(cmd, args) -> exit status (npm --yes)
|
|
156
|
+
* @returns {Promise<number>} the exit code
|
|
157
|
+
*/
|
|
158
|
+
export async function updateCommand(args, d) {
|
|
159
|
+
const a = parseUpdateArgs(args);
|
|
160
|
+
const say = d.print;
|
|
161
|
+
const home = d.home || os.homedir();
|
|
162
|
+
const env = d.env || process.env;
|
|
163
|
+
const shown = (p) => {
|
|
164
|
+
let t = String(p);
|
|
165
|
+
for (const h of [real(home), home]) t = t.split(h).join("~");
|
|
166
|
+
return t;
|
|
167
|
+
};
|
|
168
|
+
const base = env[`${PRODUCT.envPrefix}_BASE_URL`] || DEFAULT_BASE_URL;
|
|
169
|
+
if (!baseUrlOk(base)) {
|
|
170
|
+
say(`Refusing the release URL ${base} (only https:// is accepted). Nothing was changed.`);
|
|
171
|
+
return 1;
|
|
172
|
+
}
|
|
173
|
+
const { route, root } = detectRoute({ appRoot: d.appRoot, home });
|
|
174
|
+
const latest = (d.fetchLatest || fetchLatest)(base);
|
|
175
|
+
const cmp = latest.version ? compareVersions(d.version, latest.version) : null;
|
|
176
|
+
|
|
177
|
+
say(`This copy: ${PRODUCT.name} ${d.version}`);
|
|
178
|
+
if (latest.version) say(`Latest release: ${latest.version}`);
|
|
179
|
+
else if (latest.missing) say(`Latest release: unknown (the release has no ${VERSION_FILE}, as releases up to 0.0.3 do not)`);
|
|
180
|
+
else if (latest.bad) say(`Latest release: unknown (${latest.url} does not hold a version)`);
|
|
181
|
+
else say(`Latest release: unknown (could not reach ${latest.url}: ${latest.unreachable})`);
|
|
182
|
+
|
|
183
|
+
if (a.check) {
|
|
184
|
+
if (latest.unreachable) {
|
|
185
|
+
say("Could not check for an update. Nothing was changed.");
|
|
186
|
+
return 1;
|
|
187
|
+
}
|
|
188
|
+
if (!latest.version) say("Could not tell whether an update exists. Nothing was changed.");
|
|
189
|
+
else if (cmp < 0) say(`An update is available: run ${BIN} update`);
|
|
190
|
+
else if (cmp === 0) say("Up to date.");
|
|
191
|
+
else say("This copy is newer than the latest release. Nothing to update.");
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (route === "source") {
|
|
196
|
+
say(`This is a source checkout (${shown(root)}). Update it with git: git pull, npm install, npm run build:web, then ${BIN} setup --force for the skill folders. Nothing was changed.`);
|
|
197
|
+
return 0;
|
|
198
|
+
}
|
|
199
|
+
if (route === "unknown") {
|
|
200
|
+
say(`This copy runs from ${shown(root)}, which is not the installer's folder (~/.onionskin/app), an npm install or a source checkout, so ${BIN} update cannot tell how to update it. Nothing was changed. To update, install again as the README says under Install.`);
|
|
201
|
+
return 1;
|
|
202
|
+
}
|
|
203
|
+
if (latest.unreachable) {
|
|
204
|
+
say("Not updated: could not reach the release. Nothing was changed.");
|
|
205
|
+
return 1;
|
|
206
|
+
}
|
|
207
|
+
if (cmp === 0) {
|
|
208
|
+
say(`Already up to date: ${PRODUCT.name} ${d.version} is the latest release. Nothing was changed.`);
|
|
209
|
+
return 0;
|
|
210
|
+
}
|
|
211
|
+
if (cmp > 0) {
|
|
212
|
+
say(`This copy (${d.version}) is newer than the latest release (${latest.version}). Nothing was changed.`);
|
|
213
|
+
return 0;
|
|
214
|
+
}
|
|
215
|
+
if (route === "npm" && !a.yes) {
|
|
216
|
+
say("This copy was installed with npm. To update it, run these two commands:");
|
|
217
|
+
for (const c of NPM_COMMANDS) say(` ${c}`);
|
|
218
|
+
say(`Or run ${BIN} update --yes to have them run for you.`);
|
|
219
|
+
return 0;
|
|
220
|
+
}
|
|
221
|
+
// without the release's version this cannot tell an update from a reinstall or a downgrade, so it installs nothing
|
|
222
|
+
if (!latest.version) {
|
|
223
|
+
say(`Not updated: the release does not say its version, so ${BIN} update cannot tell whether it is newer than this copy. Nothing was changed. To install it anyway, run the install command from the README again.`);
|
|
224
|
+
return 1;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// An update restarts the server: a tab open now cannot reconnect (the new server listens on a new port). Refuse while work is open.
|
|
228
|
+
let stopped = false;
|
|
229
|
+
if (await d.server.healthy()) {
|
|
230
|
+
const w = await d.server.openWork();
|
|
231
|
+
const sessions = Number.isInteger(w.body?.sessions) ? w.body.sessions : null;
|
|
232
|
+
// a server that does not count its open tabs (open_sessions) cannot vouch for edits typed in a tab: like one without the route
|
|
233
|
+
const risk = w.status === 200 && Number.isInteger(w.body?.open_sessions) ? workAtRisk(w.body) : null;
|
|
234
|
+
const reason = risk ? (risk.length ? `open work that the update would cut off: ${risk.join(", ")}` : null) : sessions === 0 ? null : "a server from an older version that cannot say whether its open sessions hold unsent work";
|
|
235
|
+
if (reason && !a.force) {
|
|
236
|
+
say(`Not updated: ${PRODUCT.name} is running with ${reason}. The update restarts ${PRODUCT.name}, and a browser tab that is open now cannot reconnect to it, so anything not sent from it would be lost. Send or finish that work, then press Close session in the file's page (or close its tab and wait 20 minutes), and run ${BIN} update again. To update anyway, run ${BIN} update --force. Nothing was changed.`);
|
|
237
|
+
return 1;
|
|
238
|
+
}
|
|
239
|
+
await d.stop();
|
|
240
|
+
stopped = true;
|
|
241
|
+
say(`Stopped the ${PRODUCT.name} server for the update${reason ? ` (--force: ${reason})` : ""}.`);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (route === "npm") {
|
|
245
|
+
for (const c of NPM_COMMANDS) {
|
|
246
|
+
say(`$ ${c}`);
|
|
247
|
+
const [cmd, ...rest] = c.split(" ");
|
|
248
|
+
const status = cmd === BIN ? d.run(process.execPath, [path.join(root, "src", "cli.js"), ...rest]) : d.run(cmd, rest);
|
|
249
|
+
if (status !== 0) {
|
|
250
|
+
say(`Not updated: \`${c}\` failed (exit ${status}). Run the two commands above yourself to see why.`);
|
|
251
|
+
return 1;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const now = d.launcherVersion(route, root);
|
|
255
|
+
if (now !== latest.version) {
|
|
256
|
+
say(`Not updated as expected: npm finished, but this copy now says ${now || "nothing"}, not ${latest.version} (npm may not have that version yet). Run ${BIN} doctor.`);
|
|
257
|
+
return 1;
|
|
258
|
+
}
|
|
259
|
+
say(`UPDATED: ${PRODUCT.name} ${d.version} -> ${now} (npm).`);
|
|
260
|
+
if (stopped) say(`The server was stopped; it starts again on the next ${BIN} command. Open your file again to keep working.`);
|
|
261
|
+
return 0;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// the installer copy: get.sh from the same release folder, piped into sh as the README does
|
|
265
|
+
say(`Running the installer from ${base}/get.sh`);
|
|
266
|
+
const status = d.runGetSh(base);
|
|
267
|
+
const now = d.launcherVersion(route, root);
|
|
268
|
+
if (status !== 0) {
|
|
269
|
+
say(`Not updated: the installer stopped (exit ${status}; its lines above say why). The command now runs ${PRODUCT.name} ${now || "(version unknown)"}.`);
|
|
270
|
+
return 1;
|
|
271
|
+
}
|
|
272
|
+
if (now !== latest.version) {
|
|
273
|
+
say(`Not updated as expected: the installer finished, but ~/.local/bin/${BIN} --version says ${now || "nothing"}, not ${latest.version}. Run ${BIN} doctor.`);
|
|
274
|
+
return 1;
|
|
275
|
+
}
|
|
276
|
+
say("");
|
|
277
|
+
say(`UPDATED: ${PRODUCT.name} ${d.version} -> ${now}. Snapshots and settings in ~/.onionskin were kept.`);
|
|
278
|
+
if (stopped) say(`The server was stopped; it starts again on the next ${BIN} command. Open your file again to keep working.`);
|
|
279
|
+
say(`AGENT: this was an update. Tell the person this one sentence, and not the installer's sentence above: ${PRODUCT.name} is updated to ${now}.`);
|
|
280
|
+
return 0;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** The README one-liner, run for `base` (already checked by baseUrlOk): stdin closed, output to this terminal. pipefail, so a failed download is not a success. */
|
|
284
|
+
export function runGetShDefault(base, env = process.env) {
|
|
285
|
+
const url = String(base).replace(/\/+$/, "");
|
|
286
|
+
const r = spawnSync("/bin/sh", ["-c", 'set -o pipefail; curl -fsSL "$2" "$3" --proto-redir =https "$1/get.sh" | ONIONSKIN_BASE_URL="$1" sh', "sh", url, ...curlProto(url)], { stdio: ["ignore", "inherit", "inherit"], env });
|
|
287
|
+
return r.status ?? 1;
|
|
288
|
+
}
|
package/src/voice-guide.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
// A draft is agent text: untrusted until the person approves it word for word. The check makes sure that what reaches the page is plain
|
|
8
8
|
// text in the fixed layout, with nothing hidden, no link and no markup, so that every word the person approves is a word they could see.
|
|
9
9
|
// Ceiling: it cannot make a persuasive sentence harmless. That is what the person's review is for.
|
|
10
|
+
import { nfc } from "./nfc.js";
|
|
10
11
|
|
|
11
12
|
/** The twelve sections, in order. `title` is the exact `## ` heading; `max` the cap of its body, in characters (code points). */
|
|
12
13
|
export const SECTIONS = [
|
|
@@ -101,7 +102,7 @@ function splitSections(lines, { all }) {
|
|
|
101
102
|
let titleSeen = false;
|
|
102
103
|
for (const line of lines) {
|
|
103
104
|
const t = line.trimEnd();
|
|
104
|
-
if (/^\s*#/u.test(t
|
|
105
|
+
if (/^\s*#/u.test(nfc(t, "NFKC"))) { // a fullwidth number sign looks like # on the page
|
|
105
106
|
if (!cur && !titleSeen && t === DRAFT_TITLE) {
|
|
106
107
|
titleSeen = true;
|
|
107
108
|
continue;
|
|
@@ -138,9 +139,9 @@ export function cleanVoiceDraft(value) {
|
|
|
138
139
|
if (typeof value !== "string") return refuse("bad_structure", "the draft must be text");
|
|
139
140
|
// size first: a huge body is refused before any regex or normalization runs over it
|
|
140
141
|
if (Buffer.byteLength(value, "utf8") > DRAFT_MAX_BYTES) return refuse("too_long", `the draft is over ${DRAFT_MAX_BYTES} bytes`);
|
|
141
|
-
const text = value.replace(/\r\n?/gu, "\n")
|
|
142
|
+
const text = nfc(value.replace(/\r\n?/gu, "\n")).replace(VS, "");
|
|
142
143
|
if (HIDDEN.test(text)) return refuse("hidden_characters", "the draft holds a hidden or control character (tabs included)");
|
|
143
|
-
const nfkc = text
|
|
144
|
+
const nfkc = nfc(text, "NFKC");
|
|
144
145
|
if (MARKUP.some((re) => re.test(nfkc)) || domainPath(nfkc)) return refuse("link_or_markup", "the draft holds a link, an address, @, HTML or Markdown markup, or a code fence");
|
|
145
146
|
const lines = text.split("\n");
|
|
146
147
|
// padding: a run of blank lines could push a planted line far below the part of a box the person reads first
|
package/src/voice-learn.js
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
// What reaches the store from a Send: the five booleans per edit pair and the untrusted examples (from the worker, over three stored
|
|
5
5
|
// versions), and the trait ids that fixed English phrases in the person's own comments point at. Never a comment's text, a file name or a
|
|
6
6
|
// path. Everything here fails quietly: learning never stops a Send, and a step that fails learns nothing.
|
|
7
|
-
import { Worker } from "node:worker_threads";
|
|
8
7
|
import { buildVoiceBatch, printedSize } from "./batch.js";
|
|
9
8
|
import { env } from "./constants.js";
|
|
10
9
|
import { checkRegularOwned, readDoc, sha256 } from "./fsutil.js";
|
|
@@ -17,6 +16,7 @@ import { parseGuide } from "./voice-guide.js";
|
|
|
17
16
|
import { budget, pickRequests, pickRewrites, voiceBatchMax, voiceShares } from "./voice-samples.js";
|
|
18
17
|
import { VoiceError, appendRecords, discardDraft, forget, readDraft, readEvidence, readGuide, setAnswer, setLearning, writeGuide } from "./voice-store.js";
|
|
19
18
|
import { commentHits, suggestions } from "./voice.js";
|
|
19
|
+
import { startWorker } from "./offload.js";
|
|
20
20
|
|
|
21
21
|
const WORKER_TIMEOUT_MS = () => Number(env("VOICE_TIMEOUT_MS", 60_000));
|
|
22
22
|
const TRAIT_IDS = TRAITS.map((t) => t.id);
|
|
@@ -81,7 +81,7 @@ function analyze(versions) {
|
|
|
81
81
|
resolve(r);
|
|
82
82
|
};
|
|
83
83
|
try {
|
|
84
|
-
worker =
|
|
84
|
+
worker = startWorker("voice-worker.js", { workerData: versions });
|
|
85
85
|
} catch {
|
|
86
86
|
return finish(null);
|
|
87
87
|
}
|
|
@@ -236,7 +236,7 @@ function sampleInWorker(job) {
|
|
|
236
236
|
resolve(r);
|
|
237
237
|
};
|
|
238
238
|
try {
|
|
239
|
-
worker =
|
|
239
|
+
worker = startWorker("voice-sample-worker.js", { workerData: job });
|
|
240
240
|
} catch {
|
|
241
241
|
return finish({ error: "unreadable" });
|
|
242
242
|
}
|
|
@@ -270,6 +270,7 @@ export async function buildVoiceRequest(sessions, key, body) {
|
|
|
270
270
|
if (s.closed) throw httpError(409, "session closed", "closed");
|
|
271
271
|
if (s.pending || s.delivered) throw httpError(409, "Wait for the agent to finish, then try again.", "busy");
|
|
272
272
|
if (body.open_file && s.format === "xlsx") throw new VoiceError("Excel files are not used as writing samples.");
|
|
273
|
+
if (body.open_file && s.format === "html") throw new VoiceError("HTML pages are not used as writing samples."); // PLAN-html D9
|
|
273
274
|
const examples = body.edits ? readEvidence().examples : [];
|
|
274
275
|
if (!body.open_file && !files.length && !body.texts.length && !examples.length) throw new VoiceError(NO_SAMPLE);
|
|
275
276
|
const saved = readGuide();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
-
import {
|
|
2
|
+
import { unknownFormat } from "./constants.js";
|
|
3
3
|
import { readDocxParagraphs } from "./docx.js";
|
|
4
4
|
import { validatePackage } from "./package.js";
|
|
5
5
|
import { parseDeck } from "./pptx.js";
|
|
@@ -35,8 +35,9 @@ function office(bytes, format, index) {
|
|
|
35
35
|
if (zip.names.some((n) => MACRO_PART.test(n))) throw new SampleError(index, "macro");
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
const textsOf = (format, bytes) => {
|
|
38
|
+
export const textsOf = (format, bytes) => {
|
|
39
39
|
if (format === "docx") return readDocxParagraphs(bytes).map((p) => p.text);
|
|
40
|
+
if (format !== "pptx") return unknownFormat(format, "textsOf");
|
|
40
41
|
const deck = parseDeck(bytes);
|
|
41
42
|
deck.ensure();
|
|
42
43
|
return deck.slides.flatMap((s) => [s.title, ...(s.shapes || []).map((sh) => sh.text)]);
|
|
@@ -84,12 +85,12 @@ export function sampleSources({ open = null, files = [], texts = [], shares = 0
|
|
|
84
85
|
return { sources };
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
// Run in a worker thread
|
|
88
|
+
// Run in a worker thread by src/worker-host.js, with a timeout in the caller. One message back: {ok: true, sources} or
|
|
88
89
|
// {ok: false, index, code}. Anything else that goes wrong is {ok: false, index: -1, code: "unreadable"}.
|
|
89
|
-
|
|
90
|
+
export function work(data) {
|
|
90
91
|
try {
|
|
91
|
-
|
|
92
|
+
return { ok: true, ...sampleSources(data) };
|
|
92
93
|
} catch (err) {
|
|
93
|
-
|
|
94
|
+
return err instanceof SampleError ? { ok: false, index: err.index, code: err.code } : { ok: false, index: -1, code: "unreadable" };
|
|
94
95
|
}
|
|
95
96
|
}
|
package/src/voice-worker.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
-
import { parentPort, workerData } from "node:worker_threads";
|
|
3
2
|
import { readDocxParagraphs } from "./docx.js";
|
|
4
3
|
import { documentRuns, editPairs, exampleOf, pairSignals } from "./voice.js";
|
|
5
4
|
|
|
@@ -16,13 +15,12 @@ export function analyzeVoice(preBytes, postBytes, mineBytes) {
|
|
|
16
15
|
return { pairs: found.map(pairSignals), examples: found.map((p) => exampleOf(p, runs)).filter(Boolean) };
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
// Run in a worker thread
|
|
18
|
+
// Run in a worker thread by src/worker-host.js: data = {pre, post, mine}, three snapshot blob paths. One message back,
|
|
20
19
|
// {ok: true, pairs, examples} or {ok: false, error}. A version that cannot be read or parsed fails the whole job and learns nothing.
|
|
21
|
-
|
|
20
|
+
export function work({ pre, post, mine }) {
|
|
22
21
|
try {
|
|
23
|
-
|
|
24
|
-
parentPort.postMessage({ ok: true, ...analyzeVoice(fs.readFileSync(pre), fs.readFileSync(post), fs.readFileSync(mine)) });
|
|
22
|
+
return { ok: true, ...analyzeVoice(fs.readFileSync(pre), fs.readFileSync(post), fs.readFileSync(mine)) };
|
|
25
23
|
} catch (err) {
|
|
26
|
-
|
|
24
|
+
return { ok: false, error: String(err?.message || err) };
|
|
27
25
|
}
|
|
28
26
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// The entry of every worker thread the server starts (src/offload.js startWorker). It imports node builtins only, so its own evaluation is
|
|
2
|
+
// synchronous; it then loads the named worker module dynamically, marks `loaded`, runs the module's work(data) and posts what it returns.
|
|
3
|
+
// Why: the worker modules import src/docx.js, which awaits at its top level. Terminating a worker while a module is still being evaluated
|
|
4
|
+
// asynchronously aborts the whole server process on Node 26 ("Check failed: !is_null()", exit 133), and a worker whose job ran inside its
|
|
5
|
+
// module's evaluation was in that state for the whole job. startWorker's terminate() waits for `loaded` (the imports took tens of ms),
|
|
6
|
+
// so a worker is only ever stopped while it runs plain code. It also hands the zlib gate its word (src/stop-gate.js): stopping a worker
|
|
7
|
+
// inside zlib aborts the process too. test/worker-terminate.test.js stops each worker at many points.
|
|
8
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
9
|
+
import { setStopGate } from "./stop-gate.js";
|
|
10
|
+
|
|
11
|
+
const WORKERS = new Set(["check-worker.js", "history-worker.js", "redline-worker.js", "voice-worker.js", "voice-sample-worker.js", "open-worker.js", "merge-worker.js", "ops-worker.js", "anchor-worker.js", "html-write-worker.js", "html-render-worker.js"]);
|
|
12
|
+
const name = process.argv[2];
|
|
13
|
+
if (!WORKERS.has(name)) throw new Error(`unknown worker ${JSON.stringify(String(name)).slice(0, 60)}`);
|
|
14
|
+
const { data, loaded } = workerData;
|
|
15
|
+
setStopGate(new Int32Array(loaded)); // index 0: loaded, index 1: the zlib gate
|
|
16
|
+
// No top-level await here, in it or in src/stop-gate.js (that would make this module's evaluation asynchronous again). A failed import or a throw from work() is an
|
|
17
|
+
// unhandled rejection, which ends the worker with an "error" event its caller already handles; so is a rejected promise from work().
|
|
18
|
+
// A module that exports serve(port) is kept warm instead (src/anchor-worker.js): it answers each message it is sent, itself.
|
|
19
|
+
import(new URL(`./${name}`, import.meta.url).href).then((m) => {
|
|
20
|
+
Atomics.store(new Int32Array(loaded), 0, 1);
|
|
21
|
+
if (typeof m.serve === "function") return m.serve(parentPort, data);
|
|
22
|
+
return Promise.resolve(m.work(data)).then((r) => parentPort.postMessage(r)); // a value, or a promise of one (the merge worker's engine is asynchronous)
|
|
23
|
+
});
|