longtable 0.1.32 → 0.1.34
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/bin/longtable.mjs +3 -0
- package/package.json +1 -1
- package/src/autostart.mjs +29 -4
- package/src/run.mjs +14 -4
package/bin/longtable.mjs
CHANGED
|
@@ -118,6 +118,9 @@ async function connect() {
|
|
|
118
118
|
if (!(await codexSignIn(codex))) await leave("The ChatGPT sign-in didn't finish. Press Connect again.");
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
// A connection already running holds the old settings in memory: it is ended first, so the one the start-up task
|
|
122
|
+
// starts now (which never replaces a running one) is this new one.
|
|
123
|
+
try { const old = runningConnection(); if (old && old !== process.pid) endConnection(old); } catch {}
|
|
121
124
|
const auto = has("no-autostart") ? { started: false } : enableAutostart(process.execPath, binPath);
|
|
122
125
|
if (!has("no-run") && !auto.started) {
|
|
123
126
|
// A stand-in until the start-up task runs it: it never ends a connection the task started.
|
package/package.json
CHANGED
package/src/autostart.mjs
CHANGED
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
// that ran `connect` may itself live somewhere only the app can see.
|
|
9
9
|
|
|
10
10
|
import { spawnSync } from "node:child_process";
|
|
11
|
-
import { cpSync, existsSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
12
12
|
import { homedir } from "node:os";
|
|
13
13
|
import { dirname, join, normalize } from "node:path";
|
|
14
|
-
import { home } from "./store.mjs";
|
|
14
|
+
import { defaultHome, home } from "./store.mjs";
|
|
15
15
|
|
|
16
16
|
export const TASK = "Longtable connection";
|
|
17
17
|
const legacyStartup = () => join(process.env.APPDATA || "", "Microsoft", "Windows", "Start Menu", "Programs", "Startup", "Longtable.vbs");
|
|
@@ -61,7 +61,9 @@ export function writeStarter(nodePath, binPath) {
|
|
|
61
61
|
// Written as UTF-16 with its mark: Windows Script Host reads any other file in the system's old code page, so a
|
|
62
62
|
// profile folder with letters outside it (an Arabic or accented name) made a path that did not exist, and the
|
|
63
63
|
// connection never started.
|
|
64
|
-
|
|
64
|
+
// "--backup": started by the task (at sign-in, or by the watchdog every few minutes) it only starts a connection
|
|
65
|
+
// when none is running, and never replaces one.
|
|
66
|
+
writeFileSync(file, `\ufeffCreateObject("Wscript.Shell").Run """${q(nodePath)}"" ""${q(binPath)}"" run --backup", 0, False\r\n`, "utf16le");
|
|
65
67
|
return file;
|
|
66
68
|
}
|
|
67
69
|
|
|
@@ -69,9 +71,12 @@ export function registerTask(name, starter) {
|
|
|
69
71
|
powershell(
|
|
70
72
|
"$a = New-ScheduledTaskAction -Execute 'wscript.exe' -Argument ('\"' + $env:LT_STARTER + '\"'); " +
|
|
71
73
|
"$t = New-ScheduledTaskTrigger -AtLogOn -User ([Security.Principal.WindowsIdentity]::GetCurrent().Name); " +
|
|
74
|
+
// The watchdog: every five minutes, for as long as the computer is on. A connection that stopped for any
|
|
75
|
+
// reason is running again within five minutes, without waiting for the next sign-in.
|
|
76
|
+
"$w = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes 5); " +
|
|
72
77
|
"$p = New-ScheduledTaskPrincipal -UserId ([Security.Principal.WindowsIdentity]::GetCurrent().Name) -LogonType Interactive -RunLevel Limited; " +
|
|
73
78
|
"$s = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit ([TimeSpan]::Zero) -StartWhenAvailable; " +
|
|
74
|
-
"Register-ScheduledTask -TaskName $env:LT_TASK -Action $a -Trigger $t -Principal $p -Settings $s -Force | Out-Null",
|
|
79
|
+
"Register-ScheduledTask -TaskName $env:LT_TASK -Action $a -Trigger @($t, $w) -Principal $p -Settings $s -Force | Out-Null",
|
|
75
80
|
{ LT_TASK: name, LT_STARTER: starter },
|
|
76
81
|
);
|
|
77
82
|
}
|
|
@@ -96,6 +101,7 @@ export function taskExists(name) {
|
|
|
96
101
|
|
|
97
102
|
// Returns how the connection will start: on Windows the task also starts it now, outside whatever ran `connect`.
|
|
98
103
|
export function enableAutostart(nodePath, binPath) {
|
|
104
|
+
if (!ownsStartup()) return { started: false, where: "not this computer's start-up entry (a connection in a folder of its own)" };
|
|
99
105
|
if (process.platform === "win32") {
|
|
100
106
|
const copy = installCopy(binPath);
|
|
101
107
|
registerTask(TASK, writeStarter(nodePath, copy));
|
|
@@ -136,7 +142,26 @@ export function enableAutostart(nodePath, binPath) {
|
|
|
136
142
|
return { started: on.status === 0, where: unitFile() };
|
|
137
143
|
}
|
|
138
144
|
|
|
145
|
+
// The computer's start-up entry is one per person, named the same whatever folder a connection keeps its settings in.
|
|
146
|
+
// Only the connection in the real ~/.longtable may change it: a test's connection in a folder of its own once removed
|
|
147
|
+
// the person's real start-up task every time it disconnected.
|
|
148
|
+
const ownsStartup = () => normalize(home) === normalize(defaultHome);
|
|
149
|
+
|
|
150
|
+
// Every connection keeps its computer's start-up entry as the current version writes it (the watchdog came later):
|
|
151
|
+
// rewritten once per version, quietly, by the connection in the real ~/.longtable.
|
|
152
|
+
export function keepStartupCurrent(nodePath, binPath, version) {
|
|
153
|
+
if (!ownsStartup() || process.platform !== "win32" || !version) return;
|
|
154
|
+
const mark = join(home, "startup-written");
|
|
155
|
+
try { if (readFileSync(mark, "utf8").trim() === version) return; } catch {}
|
|
156
|
+
try {
|
|
157
|
+
if (!taskExists(TASK)) return;
|
|
158
|
+
registerTask(TASK, writeStarter(nodePath, binPath));
|
|
159
|
+
writeFileSync(mark, version);
|
|
160
|
+
} catch (e) { console.error(`keeping the start-up task current failed: ${e.message}`); }
|
|
161
|
+
}
|
|
162
|
+
|
|
139
163
|
export function disableAutostart() {
|
|
164
|
+
if (!ownsStartup()) return;
|
|
140
165
|
if (process.platform === "win32") {
|
|
141
166
|
removeTask(TASK);
|
|
142
167
|
rmSync(join(home, "start.vbs"), { force: true });
|
package/src/run.mjs
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
import { layPointed, pointedPaths, pointedSystem } from "./pointed.mjs";
|
|
7
7
|
import { PAGE, PAGE_KINDS, PAGE_SYSTEM, pageOut, pagesPrompt } from "./pages.mjs";
|
|
8
|
-
import { newer, ownVersion, restartOnNew, updateTo } from "./update.mjs";
|
|
8
|
+
import { newer, ownDir, ownVersion, restartOnNew, updateTo } from "./update.mjs";
|
|
9
|
+
import { keepStartupCurrent } from "./autostart.mjs";
|
|
9
10
|
import { attachedPrompt, commandPrompt, fetchAttachments, loadCommand, materialsPrompt, scanCommands, shareable } from "./extras.mjs";
|
|
10
11
|
import { spawnSync } from "node:child_process";
|
|
11
12
|
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
@@ -1079,6 +1080,12 @@ export async function runLoop({ pollMs = 2000, backup = false } = {}) {
|
|
|
1079
1080
|
writeFileSync(lock, `${process.pid} ${Date.now()}`);
|
|
1080
1081
|
const unlock = () => { try { if (runningConnection() === process.pid) rmSync(lock, { force: true }); } catch {} };
|
|
1081
1082
|
process.on("exit", () => { stopAll(); unlock(); });
|
|
1083
|
+
// Something nobody foresaw (a timer, a stream, a promise nobody waited on) is written down and the connection keeps
|
|
1084
|
+
// running: a connection that quietly stops is worse than one that logged a fault and carried on.
|
|
1085
|
+
process.on("uncaughtException", (e) => log(`tripwire: uncaught ${e?.stack?.split(String.fromCharCode(10)).slice(0, 3).join(" | ") || e}`));
|
|
1086
|
+
process.on("unhandledRejection", (e) => log(`tripwire: unhandled ${e?.stack?.split(String.fromCharCode(10)).slice(0, 3).join(" | ") || e}`));
|
|
1087
|
+
// The computer's start-up entry as this version writes it (with its watchdog), once per version.
|
|
1088
|
+
try { keepStartupCurrent(process.execPath, join(ownDir(), "bin", "longtable.mjs"), ownVersion()); } catch (e) { log(`start-up entry not refreshed: ${e.message}`); }
|
|
1082
1089
|
for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"]) process.on(sig, () => { stopAll(); unlock(); process.exit(0); });
|
|
1083
1090
|
// Starting with no network yet (right after sign-in) is normal: keep trying. A table that took this connection
|
|
1084
1091
|
// off is skipped, and the others carry on.
|
|
@@ -1126,9 +1133,10 @@ export async function runLoop({ pollMs = 2000, backup = false } = {}) {
|
|
|
1126
1133
|
setInterval(checkRelease, 10 * 60 * 1000).unref?.();
|
|
1127
1134
|
// Two lanes on one connection: long work in one, everything quick in the other, so an hour of work never keeps
|
|
1128
1135
|
// the table from hearing people or answering a question.
|
|
1136
|
+
// Declared before the lanes start: they read it the moment a full-mode job arrives.
|
|
1137
|
+
let aside = 0;
|
|
1129
1138
|
await Promise.all([lane("light"), lane("heavy")]);
|
|
1130
1139
|
|
|
1131
|
-
let aside = 0;
|
|
1132
1140
|
async function lane(name) {
|
|
1133
1141
|
for (;;) {
|
|
1134
1142
|
let worked = false;
|
|
@@ -1149,12 +1157,14 @@ export async function runLoop({ pollMs = 2000, backup = false } = {}) {
|
|
|
1149
1157
|
busy++;
|
|
1150
1158
|
// A quick job run as their full Claude Code can wait minutes on its person's answer. It works in a tree of its
|
|
1151
1159
|
// own, so it runs alongside, and the lane goes on hearing the table (listening, clients, views) meanwhile.
|
|
1152
|
-
if (name === "light" && job.full === true && aside < 2) {
|
|
1160
|
+
if (name === "light" && job.full === true && !["listen", "client"].includes(job.kind) && aside < 2) {
|
|
1153
1161
|
aside++;
|
|
1154
1162
|
work(rpc, s, engines, job).catch((e) => log(`job ${job.job_id} failed: ${e.message}`)).finally(() => { busy--; aside--; });
|
|
1155
1163
|
continue;
|
|
1156
1164
|
}
|
|
1157
|
-
|
|
1165
|
+
// One job going wrong in a way nobody foresaw never takes the whole connection down: it is logged as a
|
|
1166
|
+
// tripwire and the lane goes on to the next.
|
|
1167
|
+
try { await work(rpc, s, engines, job); } catch (e) { log(`tripwire: job ${job.job_id} broke the lane: ${e.message}`); } finally { busy--; }
|
|
1158
1168
|
}
|
|
1159
1169
|
await new Promise((r) => setTimeout(r, worked ? 100 : name === "heavy" ? pollMs * 2 : pollMs));
|
|
1160
1170
|
}
|