longtable 0.1.33 → 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 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "longtable",
3
- "version": "0.1.33",
3
+ "version": "0.1.34",
4
4
  "description": "Brings your own Claude Code or Codex to your team's table on longtable.chat, quietly, on your own plan.",
5
5
  "homepage": "https://longtable.chat",
6
6
  "repository": {
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
- writeFileSync(file, `\ufeffCreateObject("Wscript.Shell").Run """${q(nodePath)}"" ""${q(binPath)}"" run", 0, False\r\n`, "utf16le");
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.