longtable 0.1.17 → 0.1.18
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 +176 -165
- package/package.json +1 -1
- package/src/autostart.mjs +141 -115
- package/src/claude.mjs +13 -0
- package/src/codex.mjs +160 -154
- package/src/extras.mjs +48 -10
- package/src/pack.mjs +8 -4
- package/src/pointed.mjs +68 -14
- package/src/repo.mjs +24 -0
- package/src/run.mjs +57 -22
- package/src/signin.mjs +19 -5
- package/src/store.mjs +24 -2
package/src/repo.mjs
CHANGED
|
@@ -114,6 +114,30 @@ export function neverCommit(tableId, name) {
|
|
|
114
114
|
|
|
115
115
|
// Lay out the files to work on: the chat's own branch when it already exists, so one request builds on the last,
|
|
116
116
|
// else the latest default branch.
|
|
117
|
+
// Whether this computer's git can put two parts of work together on one branch (merge-tree --write-tree, git 2.38
|
|
118
|
+
// and later). An older git still works: a part goes on its own branch beside the other, as when two parts touch the
|
|
119
|
+
// same lines. "version" is what git says of itself, for the person to be told.
|
|
120
|
+
export function gitCanJoin(said) {
|
|
121
|
+
let version = said;
|
|
122
|
+
try { version ??= execFileSync("git", ["--version"], { encoding: "utf8", windowsHide: true }).trim(); } catch { return { ok: false, version: "no git found" }; }
|
|
123
|
+
const m = String(version).match(/(\d+)\.(\d+)/);
|
|
124
|
+
const ok = !!m && (Number(m[1]) > 2 || (Number(m[1]) === 2 && Number(m[2]) >= 38));
|
|
125
|
+
return { ok, version };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Everything the connection keeps of these tables' code on this computer: the clean copies, the work and reading
|
|
129
|
+
// folders, and the kept copies of folders a person pointed at. Done after the connection has stopped; a file still
|
|
130
|
+
// held for a moment is tried again.
|
|
131
|
+
export function forgetCopies(tableIds) {
|
|
132
|
+
const gone = (p) => { try { rmSync(p, { recursive: true, force: true, maxRetries: 10, retryDelay: 300 }); } catch (e) { log(`could not remove ${p}: ${e.message}`); } };
|
|
133
|
+
for (const t of tableIds) {
|
|
134
|
+
let safe;
|
|
135
|
+
try { safe = safeTable(t); } catch { continue; }
|
|
136
|
+
for (const place of ["copies", "work", "look"]) gone(join(home, place, safe));
|
|
137
|
+
}
|
|
138
|
+
gone(join(home, "pointed"));
|
|
139
|
+
}
|
|
140
|
+
|
|
117
141
|
export function openWork(tableId, branch, name = "work", { refresh = false } = {}) {
|
|
118
142
|
safeBranch(branch);
|
|
119
143
|
const p = places(tableId, name);
|
package/src/run.mjs
CHANGED
|
@@ -5,15 +5,16 @@
|
|
|
5
5
|
|
|
6
6
|
import { layPointed, pointedPaths, pointedSystem } from "./pointed.mjs";
|
|
7
7
|
import { attachedPrompt, commandPrompt, fetchAttachments, loadCommand, materialsPrompt, scanCommands, shareable } from "./extras.mjs";
|
|
8
|
+
import { spawnSync } from "node:child_process";
|
|
8
9
|
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
9
10
|
import { homedir, uptime } from "node:os";
|
|
10
11
|
import { isAbsolute, join, normalize, relative, resolve } from "node:path";
|
|
11
12
|
import { client } from "./api.mjs";
|
|
12
|
-
import { ask, findClaude } from "./claude.mjs";
|
|
13
|
+
import { ask, findClaude, stopAll } from "./claude.mjs";
|
|
13
14
|
import { askCodex, codexLimits, findCodex } from "./codex.mjs";
|
|
14
15
|
import { packCode } from "./pack.mjs";
|
|
15
16
|
import { changedSoFar, freshCopy, landWork, neverCommit, openBranch, openWork, patchOf, safeTable, workBranch } from "./repo.mjs";
|
|
16
|
-
import { hideSecrets, home, loadSecret, log, readConfig } from "./store.mjs";
|
|
17
|
+
import { cut, hideSecrets, home, loadSecret, log, readConfig, writeWhole } from "./store.mjs";
|
|
17
18
|
|
|
18
19
|
const SOURCES = {
|
|
19
20
|
type: "array",
|
|
@@ -140,10 +141,10 @@ const fence = (job) => {
|
|
|
140
141
|
const who = (m) => (m.by_ai ? `${m.name}'s AI` : m.client ? `${m.name} (the client, asking)` : m.name);
|
|
141
142
|
const parts = [];
|
|
142
143
|
if (job.people?.length) parts.push(facts(job));
|
|
143
|
-
if (job.pinned?.length) parts.push("Pinned notes:\n" + job.pinned.map((n) => `(${n.title || n.kind}) ${n.body}`).join("\n"));
|
|
144
|
-
if (job.related?.length) parts.push("Related earlier messages from other chats:\n" + job.related.map((m) => `[${m.chat}] ${who(m)}: ${m.body}`).join("\n"));
|
|
144
|
+
if (job.pinned?.length) parts.push("Pinned notes:\n" + job.pinned.map((n) => `(${n.title || n.kind}) ${n.body ?? ""}`).join("\n"));
|
|
145
|
+
if (job.related?.length) parts.push("Related earlier messages from other chats:\n" + job.related.map((m) => `[${m.chat}] ${who(m)}: ${m.body ?? ""}`).join("\n"));
|
|
145
146
|
const talk = recent(job.history, 120000);
|
|
146
|
-
parts.push(talk.length ? `The conversation in "${job.chat_name}"${talk.length < (job.history || []).length ? " (its start is left out for length)" : ""}:\n` + talk.map((m) => `${who(m)}: ${m.body}`).join("\n") : `No earlier messages in "${job.chat_name}".`);
|
|
147
|
+
parts.push(talk.length ? `The conversation in "${job.chat_name}"${talk.length < (job.history || []).length ? " (its start is left out for length)" : ""}:\n` + talk.map((m) => `${who(m)}: ${m.body ?? ""}`).join("\n") : `No earlier messages in "${job.chat_name}".`);
|
|
147
148
|
if (job.kind === "plan") {
|
|
148
149
|
parts.push("What the other AIs said before the work:" + NL + (job.views?.length ? job.views.map((v) => `${v.name}'s AI: ${v.view}`).join(NL) : "Nothing."));
|
|
149
150
|
}
|
|
@@ -164,7 +165,7 @@ const fenceClient = (job) => {
|
|
|
164
165
|
if (job.told?.length) parts.push("What the team told them, newest first:\n" + job.told.map((u) => `- ${u.words}${u.their_answer ? ` (they answered: ${u.their_answer})` : ""}`).join("\n"));
|
|
165
166
|
if (job.work?.length) parts.push("What the team's AIs have taken on, newest first (titles only; \"finished\" means built and waiting for the team to check, not yet live):\n"
|
|
166
167
|
+ job.work.map((w) => `- "${w.title}": ${w.status === "done" ? "finished" : "under way"}`).join("\n"));
|
|
167
|
-
if (job.history?.length) parts.push("Your chat with them so far:\n" + job.history.map((l) => `${l.who === "client" ? job.me : "You"}: ${l.body}`).join("\n"));
|
|
168
|
+
if (job.history?.length) parts.push("Your chat with them so far:\n" + job.history.map((l) => `${l.who === "client" ? job.me : "You"}: ${l.body ?? ""}`).join("\n"));
|
|
168
169
|
parts.push(`Their latest message:\n${job.question}`);
|
|
169
170
|
return ["<<<TABLE", fenced(inert(parts.join("\n\n"))), "TABLE>>>"].join("\n");
|
|
170
171
|
};
|
|
@@ -223,7 +224,7 @@ export function systemFor(job) {
|
|
|
223
224
|
|
|
224
225
|
// The listener: the cheapest thing at the table. It sees the last few messages and nothing else, and decides
|
|
225
226
|
// whether the AIs are wanted at all. No code, no tools, the smallest model.
|
|
226
|
-
const short = (t, n) => (String(t).length > n ?
|
|
227
|
+
const short = (t, n) => (String(t ?? "").length > n ? cut(t, n) + "…" : String(t ?? ""));
|
|
227
228
|
export function listenPrompt(job) {
|
|
228
229
|
const who = (m) => (m.by_ai ? `${m.name}'s AI` : m.client ? `${m.name} (the client)` : m.name);
|
|
229
230
|
// The conversation as far back as a generous size reaches, never a fixed number of messages.
|
|
@@ -371,11 +372,11 @@ export function mind(tableId) {
|
|
|
371
372
|
try { return readFileSync(mindFile(tableId), "utf8").trim(); } catch { return ""; }
|
|
372
373
|
}
|
|
373
374
|
export function remember(tableId, text) {
|
|
374
|
-
const line = hideSecrets(String(text || "")).replace(/\s+/g, " ").trim()
|
|
375
|
+
const line = cut(hideSecrets(String(text || "")).replace(/\s+/g, " ").trim(), 300);
|
|
375
376
|
if (line.length < 12) return;
|
|
376
377
|
mkdirSync(join(home, "minds"), { recursive: true });
|
|
377
378
|
const kept = mind(tableId).split(NL).filter(Boolean).slice(-39);
|
|
378
|
-
|
|
379
|
+
writeWhole(mindFile(tableId), [...kept, `- ${new Date().toISOString().slice(0, 10)}: ${line}`].join(NL) + NL);
|
|
379
380
|
}
|
|
380
381
|
|
|
381
382
|
// Keep only sources that are real files inside the copy, written relative to it.
|
|
@@ -542,12 +543,15 @@ const NOT_IN_A_SANDBOX = new RegExp([
|
|
|
542
543
|
// what runs on the server, at build time, or decides who may do what
|
|
543
544
|
"(^|/)(scripts?|migrations?|supabase|prisma|drizzle|db|database|server|functions|workers?|lambda|infra|deploy|ops|bin|tools)/",
|
|
544
545
|
"(^|/)(api|trpc|actions?)/", "(^|/)(route|middleware|instrumentation|proxy|actions?|server)[.][cm]?[jt]sx?$",
|
|
545
|
-
"(^|[/._-])(auth|login|signin|sign-in|session|permissions?|polic(y|ies)|rls|guard|
|
|
546
|
+
"(^|[/._-])(auth|login|signin|sign-in|session|permissions?|polic(y|ies)|rls|guard|admin|secrets?|credentials?)([/._-]|$)",
|
|
547
|
+
// Words that are also ordinary parts of a name ("role-picker", "token-list") only count as a whole folder or file.
|
|
548
|
+
"(^|/)(roles?|tokens?|keys?)([/.]|$)",
|
|
546
549
|
// tests, so one cannot be changed to pass
|
|
547
550
|
"(^|/)(tests?|__tests__|e2e|spec|cypress|playwright)/", "[.](test|spec)[.]",
|
|
548
551
|
].join("|"), "i");
|
|
549
552
|
const NOT_A_SANDBOX_WORD = new RegExp([
|
|
550
|
-
"\\bprocess\\s*\\??\\.[A-Za-z_$]", "\\bprocess\\s*\\[", "[=(,:]\\s*process\\s*[;),}\\]]", "\\
|
|
553
|
+
"\\bprocess\\s*\\??\\.[A-Za-z_$]", "\\bprocess\\s*\\[", "[=(,:]\\s*process\\s*[;),}\\]]", "[=(,:]\\s*process\\s*$", "import\\s*[.]\\s*meta", "\\b(Deno|Bun)\\s*[.]",
|
|
554
|
+
"\\benv\\s*\\[", "\\benv\\s*\\??\\.[A-Za-z_$]", "[{,]\\s*env\\s*[,})]", "\\bgetenv\\b", "os[.]environ", "\\bENV\\s*[\\[.]",
|
|
551
555
|
"\\b(readFile|readFileSync|readdir|readdirSync|createReadStream|child_process|execSync|spawn)\\b", "[\"'`](node:)?(fs|fs/promises|vm|net|dns|child_process|worker_threads)[\"'`]",
|
|
552
556
|
// The server's other ways out, named where a module is asked for, so "http" in an ordinary sentence is left alone.
|
|
553
557
|
"\\b(require|import)\\s*\\(\\s*[\"'`](node:)?(https?|http2|os|tls|dgram|module)[\"'`]", "\\bfrom\\s*[\"'`](node:)?(https?|http2|os|tls|dgram|module)[\"'`]",
|
|
@@ -586,7 +590,7 @@ async function workSandbox(rpc, seat, engines, job) {
|
|
|
586
590
|
const control = {};
|
|
587
591
|
const tell = async () => {
|
|
588
592
|
told = Date.now();
|
|
589
|
-
try { if ((await rpc("runner_beat", { token: seat.token, job: job.job_id, note })) === false) control.kill?.(); } catch {}
|
|
593
|
+
try { if ((await rpc("runner_beat", { token: seat.token, job: job.job_id, note })) === false) control.kill?.(); } catch (e) { if (/connection was removed/i.test(e.message)) control.kill?.(); }
|
|
590
594
|
};
|
|
591
595
|
const beat = setInterval(tell, 20000);
|
|
592
596
|
try {
|
|
@@ -659,7 +663,19 @@ async function workListen(rpc, seat, engines, job) {
|
|
|
659
663
|
log(`listen job ${job.job_id}: ${act}`);
|
|
660
664
|
}
|
|
661
665
|
|
|
662
|
-
|
|
666
|
+
// People's and tables' names come from the table like any other words, and go into the AI's own instructions: a
|
|
667
|
+
// name could carry a fence marker, quotes or line breaks that read as instructions of their own. Each is one plain
|
|
668
|
+
// line before any prompt is built.
|
|
669
|
+
export const oneName = (t) => cut(inert(t).replace(/\s+/g, " ").replace(/"/g, "'").trim(), 80) || "someone";
|
|
670
|
+
export function cleanNames(job) {
|
|
671
|
+
const out = { ...job };
|
|
672
|
+
for (const k of ["me", "asker", "client", "table_name"]) if (typeof out[k] === "string") out[k] = oneName(out[k]);
|
|
673
|
+
if (Array.isArray(out.people)) out.people = out.people.map((p) => ({ ...p, name: oneName(p?.name) }));
|
|
674
|
+
return out;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
async function work(rpc, seat, engines, raw) {
|
|
678
|
+
const job = cleanNames(raw);
|
|
663
679
|
log(`${job.kind} job ${job.job_id} for ${job.me}`);
|
|
664
680
|
if (job.kind === "sandbox") {
|
|
665
681
|
try {
|
|
@@ -720,7 +736,7 @@ async function work(rpc, seat, engines, job) {
|
|
|
720
736
|
const control = {};
|
|
721
737
|
const tell = async () => {
|
|
722
738
|
told = Date.now();
|
|
723
|
-
try { if ((await rpc("runner_beat", { token: seat.token, job: job.job_id, note })) === false) control.kill?.(); } catch {}
|
|
739
|
+
try { if ((await rpc("runner_beat", { token: seat.token, job: job.job_id, note })) === false) control.kill?.(); } catch (e) { if (/connection was removed/i.test(e.message)) control.kill?.(); }
|
|
724
740
|
};
|
|
725
741
|
beat = setInterval(tell, 20000);
|
|
726
742
|
const dir = opened ? opened.dir : copy.dir;
|
|
@@ -733,7 +749,7 @@ async function work(rpc, seat, engines, job) {
|
|
|
733
749
|
const at = pointedPaths(job.question);
|
|
734
750
|
if (at.length) {
|
|
735
751
|
if (seat.source) neverCommit(seat.table_id, YOURS);
|
|
736
|
-
laid = layPointed(join(dir, YOURS), at, { reading: !doing });
|
|
752
|
+
laid = layPointed(join(dir, YOURS), at, { reading: !doing, home });
|
|
737
753
|
log(`job ${job.job_id}: opened ${laid.files.length + laid.dirs.length + laid.copied.length} pointed place(s), left ${laid.skipped.length}`);
|
|
738
754
|
// And, while their person leaves it on, the text of them for the other AIs on this same request.
|
|
739
755
|
if (job.share_paths && ["plan", "work", "look", "answer"].includes(job.kind)) {
|
|
@@ -772,13 +788,22 @@ async function work(rpc, seat, engines, job) {
|
|
|
772
788
|
if (e.message !== "ran out of time" || !doing) throw e;
|
|
773
789
|
return { structured_output: { answer: "I ran out of time. What I had changed up to then is on the branch. It is not finished, so look at it before building on it.", sources: [] }, usage: {}, modelUsage: {} };
|
|
774
790
|
});
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
const text = job.kind === "view" || job.kind === "review" ? s.view
|
|
791
|
+
const s = result.is_error ? {} : result.structured_output || {};
|
|
792
|
+
let text = job.kind === "view" || job.kind === "review" ? s.view
|
|
778
793
|
: job.kind === "plan" ? planText(job, s)
|
|
779
794
|
: job.kind === "intro" ? `${s.knows}\n\nI can check: ${s.can_check}`
|
|
780
795
|
: s.answer;
|
|
781
|
-
|
|
796
|
+
const unfinished = result.is_error ? String(result.result || "Claude Code reported an error")
|
|
797
|
+
: !text || !String(text).trim() ? "Claude Code came back empty" : null;
|
|
798
|
+
if (unfinished) {
|
|
799
|
+
// Work that already changed files is not thrown away because its last word went wrong: what it changed lands
|
|
800
|
+
// on the branch, and the answer says plainly that it is not finished. Anything else fails as before.
|
|
801
|
+
let changed = 0;
|
|
802
|
+
try { changed = opened && doing ? changedSoFar(seat.table_id, opened).files.length : 0; } catch {}
|
|
803
|
+
if (!changed) throw new Error(unfinished);
|
|
804
|
+
log(`${job.kind} job ${job.job_id} ended without an answer (${unfinished.slice(0, 120)}); landing the ${changed} file(s) it changed`);
|
|
805
|
+
text = "I could not finish my answer. What I had changed up to then is on the branch. It is not finished, so look at it before building on it.";
|
|
806
|
+
}
|
|
782
807
|
if (s.learned) remember(seat.table_id, s.learned);
|
|
783
808
|
const landed = opened && doing ? landWork(seat.table_id, opened, branch, String(job.question || "Work from the table").split(/\r?\n/)[0].slice(0, 72), job.me) : null;
|
|
784
809
|
const said = landed && !landed.pushed ? `${text}\n\nI made the change, but git on ${job.me}'s computer could not push the branch ${branch}, so nobody else can see it yet. It is kept there as commit ${landed.commit}.`
|
|
@@ -823,6 +848,16 @@ async function work(rpc, seat, engines, job) {
|
|
|
823
848
|
}
|
|
824
849
|
}
|
|
825
850
|
|
|
851
|
+
// End another running connection and every program it started. On Windows a plain kill ends only the connection
|
|
852
|
+
// itself and its Claude carries on, so the whole tree is ended there; elsewhere the connection's own signal handler
|
|
853
|
+
// ends its Claudes before it goes.
|
|
854
|
+
export function endConnection(pid) {
|
|
855
|
+
try {
|
|
856
|
+
if (process.platform === "win32") spawnSync("taskkill", ["/PID", String(pid), "/T", "/F"], { windowsHide: true, stdio: "ignore" });
|
|
857
|
+
else process.kill(pid, "SIGTERM");
|
|
858
|
+
} catch (e) { log(`could not end connection ${pid}: ${e.message}`); }
|
|
859
|
+
}
|
|
860
|
+
|
|
826
861
|
// The connection that is running on this computer right now, if its note can be trusted: written since this computer
|
|
827
862
|
// last started (a number from before a restart may belong to any program by now), by a process that still exists.
|
|
828
863
|
export function runningConnection() {
|
|
@@ -862,11 +897,11 @@ export async function runLoop({ pollMs = 2000 } = {}) {
|
|
|
862
897
|
// Only one connection runs on a computer. A second one would wipe the first one's work folder under it.
|
|
863
898
|
const lock = join(home, "runner.pid");
|
|
864
899
|
const old = runningConnection();
|
|
865
|
-
if (old && old !== process.pid) {
|
|
900
|
+
if (old && old !== process.pid) { endConnection(old); log(`ended the connection that was already running (${old})`); }
|
|
866
901
|
writeFileSync(lock, `${process.pid} ${Date.now()}`);
|
|
867
902
|
const unlock = () => { try { if (runningConnection() === process.pid) rmSync(lock, { force: true }); } catch {} };
|
|
868
|
-
process.on("exit", unlock);
|
|
869
|
-
for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"]) process.on(sig, () => { unlock(); process.exit(0); });
|
|
903
|
+
process.on("exit", () => { stopAll(); unlock(); });
|
|
904
|
+
for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"]) process.on(sig, () => { stopAll(); unlock(); process.exit(0); });
|
|
870
905
|
// Starting with no network yet (right after sign-in) is normal: keep trying. A table that took this connection
|
|
871
906
|
// off is skipped, and the others carry on.
|
|
872
907
|
for (const s of seats) {
|
package/src/signin.mjs
CHANGED
|
@@ -72,6 +72,9 @@ export function signIn(claude, { title = "Connect your Claude" } = {}) {
|
|
|
72
72
|
let state = { state: "starting", message: "Starting Claude's sign-in…" };
|
|
73
73
|
let finished = false;
|
|
74
74
|
let pressedEnter = false;
|
|
75
|
+
// Tokens already checked. One read while Claude was still printing it is only part of it: that one is set aside
|
|
76
|
+
// and the screen watched for the rest, where before the whole sign-in ended on the first bad read.
|
|
77
|
+
const tried = new Set();
|
|
75
78
|
const screen = new Terminal({ cols: 400, rows: 200, allowProposedApi: true, scrollback: 2000 });
|
|
76
79
|
const screenText = () => {
|
|
77
80
|
const b = screen.buffer.active;
|
|
@@ -83,9 +86,13 @@ export function signIn(claude, { title = "Connect your Claude" } = {}) {
|
|
|
83
86
|
name: "xterm-256color", cols: 400, rows: 200, cwd: process.env.USERPROFILE || process.env.HOME || process.cwd(), env: childEnv(null),
|
|
84
87
|
});
|
|
85
88
|
|
|
89
|
+
// The ten-minute limit below. Cleared when the sign-in ends, or `connect` waited out all ten minutes before
|
|
90
|
+
// exiting even after a sign-in that worked first time.
|
|
91
|
+
let deadline = null;
|
|
86
92
|
function finish(ok, message) {
|
|
87
93
|
if (finished) return;
|
|
88
94
|
finished = true;
|
|
95
|
+
clearTimeout(deadline);
|
|
89
96
|
state = { state: ok ? "connected" : "failed", message };
|
|
90
97
|
try { term.kill(); } catch {}
|
|
91
98
|
setTimeout(() => { server.close(); resolve(ok); }, 3000);
|
|
@@ -94,17 +101,24 @@ export function signIn(claude, { title = "Connect your Claude" } = {}) {
|
|
|
94
101
|
function look() {
|
|
95
102
|
if (finished) return;
|
|
96
103
|
const text = screenText();
|
|
97
|
-
|
|
98
|
-
|
|
104
|
+
// A token counts once something follows it on the screen: until then Claude may still be printing it.
|
|
105
|
+
const token = text.match(/sk-ant-oat\d{2}-[A-Za-z0-9_-]{20,}(?=\s)/);
|
|
106
|
+
if (token && !tried.has(token[0])) {
|
|
107
|
+
tried.add(token[0]);
|
|
108
|
+
const was = state;
|
|
99
109
|
state = { state: "working", url: state.url, message: "Approved. Checking the sign-in works…" };
|
|
100
|
-
try { term.kill(); } catch {}
|
|
101
110
|
const good = pickWorkingToken(claude, token[0]);
|
|
102
111
|
if (good) {
|
|
112
|
+
try { term.kill(); } catch {}
|
|
103
113
|
saveSecret("claude-token", good);
|
|
104
114
|
log(`saved a working Claude sign-in of ${good.length} characters`);
|
|
105
115
|
finish(true, "Connected. Your Claude can now answer at the table, quietly, on your own plan.");
|
|
106
|
-
} else {
|
|
116
|
+
} else if (tried.size >= 3) {
|
|
117
|
+
try { term.kill(); } catch {}
|
|
107
118
|
finish(false, "Claude approved, but the sign-in didn't work. Close this page and press Connect again.");
|
|
119
|
+
} else {
|
|
120
|
+
log(`a sign-in read from the screen did not work (${tried.size} of 3); watching for the rest of it`);
|
|
121
|
+
state = was;
|
|
108
122
|
}
|
|
109
123
|
return;
|
|
110
124
|
}
|
|
@@ -147,6 +161,6 @@ export function signIn(claude, { title = "Connect your Claude" } = {}) {
|
|
|
147
161
|
res.end();
|
|
148
162
|
});
|
|
149
163
|
server.listen(0, "127.0.0.1", () => openInBrowser(`http://127.0.0.1:${server.address().port}/${secret}`));
|
|
150
|
-
setTimeout(() => finish(false, "This took too long, so it stopped. Close this page and try again."), 10 * 60 * 1000);
|
|
164
|
+
deadline = setTimeout(() => finish(false, "This took too long, so it stopped. Close this page and try again."), 10 * 60 * 1000);
|
|
151
165
|
});
|
|
152
166
|
}
|
package/src/store.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Secrets are encrypted for this Windows user with DPAPI; elsewhere they are files only this user can read.
|
|
3
3
|
|
|
4
4
|
import { spawnSync } from "node:child_process";
|
|
5
|
-
import { appendFileSync, chmodSync, cpSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { appendFileSync, chmodSync, cpSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
6
6
|
import { homedir } from "node:os";
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
|
|
@@ -35,7 +35,29 @@ export function hideSecrets(text) {
|
|
|
35
35
|
.replace(/\b[0-9a-f]{64}\b/gi, "[device token]");
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
// The log keeps to a size. Offline, a connection writes a line every few seconds, so it once grew without end; past
|
|
39
|
+
// 5 MB it starts again and keeps only the file before it (runner.log.1).
|
|
40
|
+
const LOG_MAX = 5 * 1024 * 1024;
|
|
41
|
+
let logWrites = 0;
|
|
42
|
+
// Text cut to n, never through the middle of a character that takes two (most emoji).
|
|
43
|
+
export const cut = (s, n) => {
|
|
44
|
+
const t = String(s ?? "");
|
|
45
|
+
if (t.length <= n) return t;
|
|
46
|
+
const x = t.slice(0, n);
|
|
47
|
+
return /[\uD800-\uDBFF]$/.test(x) ? x.slice(0, -1) : x;
|
|
48
|
+
};
|
|
49
|
+
// A file written whole or not at all: to a file beside it, then put in its place. A stop halfway through a write
|
|
50
|
+
// used to leave settings or notes cut short, and the next start could not read them.
|
|
51
|
+
export function writeWhole(file, text) {
|
|
52
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
53
|
+
writeFileSync(tmp, text);
|
|
54
|
+
renameSync(tmp, file);
|
|
55
|
+
}
|
|
56
|
+
|
|
38
57
|
export function log(line) {
|
|
58
|
+
if (logWrites++ % 200 === 0) {
|
|
59
|
+
try { if (statSync(logFile).size > LOG_MAX) renameSync(logFile, `${logFile}.1`); } catch {}
|
|
60
|
+
}
|
|
39
61
|
appendFileSync(logFile, `${new Date().toISOString()} ${hideSecrets(line)}\n`);
|
|
40
62
|
}
|
|
41
63
|
|
|
@@ -45,7 +67,7 @@ export function readConfig() {
|
|
|
45
67
|
}
|
|
46
68
|
|
|
47
69
|
export function writeConfig(config) {
|
|
48
|
-
|
|
70
|
+
writeWhole(configFile, JSON.stringify(config, null, 2));
|
|
49
71
|
}
|
|
50
72
|
|
|
51
73
|
function powershell(script, input, file) {
|