t3code-session-manager 0.0.0-stage → 0.4.0
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/.claude-plugin/marketplace.json +21 -0
- package/.claude-plugin/plugin.json +19 -0
- package/CHANGELOG.md +138 -0
- package/LICENSE +21 -0
- package/README.md +660 -2
- package/README.pt.md +616 -0
- package/bin/entry.d.mts +2 -0
- package/bin/entry.mjs +19 -0
- package/bin/t3sm.mjs +44 -0
- package/commands/import.md +30 -0
- package/commands/organize.md +36 -0
- package/commands/status.md +24 -0
- package/commands/sync.md +26 -0
- package/dist/apply/apply.js +993 -0
- package/dist/apply/claudeWriter.js +168 -0
- package/dist/apply/copyTranscript.js +116 -0
- package/dist/apply/importer.js +697 -0
- package/dist/apply/materialize.js +335 -0
- package/dist/apply/waves.js +108 -0
- package/dist/assistant/core.js +295 -0
- package/dist/assistant/deps.js +256 -0
- package/dist/assistant/i18n.js +38 -0
- package/dist/assistant/messages.en.js +494 -0
- package/dist/assistant/messages.pt.js +495 -0
- package/dist/assistant/runner.js +64 -0
- package/dist/assistant/steps.js +1362 -0
- package/dist/build.json +1 -0
- package/dist/cli/applyScreen.js +158 -0
- package/dist/cli/commands/accounts.js +254 -0
- package/dist/cli/commands/apply.js +199 -0
- package/dist/cli/commands/changelog.js +61 -0
- package/dist/cli/commands/doctor.js +105 -0
- package/dist/cli/commands/help.js +158 -0
- package/dist/cli/commands/merge.js +224 -0
- package/dist/cli/commands/organize.js +872 -0
- package/dist/cli/commands/plan.js +161 -0
- package/dist/cli/commands/projects.js +430 -0
- package/dist/cli/commands/quarantine.js +203 -0
- package/dist/cli/commands/recover.js +230 -0
- package/dist/cli/commands/rollback.js +281 -0
- package/dist/cli/commands/scan.js +117 -0
- package/dist/cli/commands/schedule.js +288 -0
- package/dist/cli/commands/start.js +45 -0
- package/dist/cli/commands/status.js +615 -0
- package/dist/cli/commands/sync.js +336 -0
- package/dist/cli/commands/update.js +398 -0
- package/dist/cli/errors.js +53 -0
- package/dist/cli/failures.js +91 -0
- package/dist/cli/flags.js +53 -0
- package/dist/cli/help.en.js +549 -0
- package/dist/cli/help.pt.js +566 -0
- package/dist/cli/helpText.js +211 -0
- package/dist/cli/helpTopics.js +58 -0
- package/dist/cli/locked.js +12 -0
- package/dist/cli/main.js +155 -0
- package/dist/cli/screen.js +3 -0
- package/dist/cli/ui.js +111 -0
- package/dist/cli/words.js +2 -0
- package/dist/core/activity.js +16 -0
- package/dist/core/changelog.js +47 -0
- package/dist/core/config.js +530 -0
- package/dist/core/fsutil.js +171 -0
- package/dist/core/glob.js +34 -0
- package/dist/core/history.js +32 -0
- package/dist/core/journal.js +274 -0
- package/dist/core/lock.js +337 -0
- package/dist/core/mask.js +31 -0
- package/dist/core/paths.js +68 -0
- package/dist/core/pid.js +101 -0
- package/dist/core/run.js +4 -0
- package/dist/core/semver.js +18 -0
- package/dist/core/sysroot.js +29 -0
- package/dist/core/types.js +229 -0
- package/dist/core/uuid.js +29 -0
- package/dist/core/version.js +72 -0
- package/dist/help/convert.md +20 -0
- package/dist/help/convert.pt.md +21 -0
- package/dist/help/cwd-unreadable.md +22 -0
- package/dist/help/cwd-unreadable.pt.md +22 -0
- package/dist/help/decisions.md +20 -0
- package/dist/help/decisions.pt.md +20 -0
- package/dist/help/errors.md +20 -0
- package/dist/help/errors.pt.md +22 -0
- package/dist/help/held.md +20 -0
- package/dist/help/held.pt.md +20 -0
- package/dist/help/history.md +27 -0
- package/dist/help/history.pt.md +29 -0
- package/dist/help/instance-unavailable.md +18 -0
- package/dist/help/instance-unavailable.pt.md +19 -0
- package/dist/help/not-imported.md +19 -0
- package/dist/help/not-imported.pt.md +19 -0
- package/dist/help/orphaned.md +19 -0
- package/dist/help/orphaned.pt.md +19 -0
- package/dist/help/project-mismatch.md +15 -0
- package/dist/help/project-mismatch.pt.md +15 -0
- package/dist/help/t3-native.md +22 -0
- package/dist/help/t3-native.pt.md +22 -0
- package/dist/help/updates.md +26 -0
- package/dist/help/updates.pt.md +28 -0
- package/dist/help/watcher.md +24 -0
- package/dist/help/watcher.pt.md +25 -0
- package/dist/i18n/cli.js +41 -0
- package/dist/i18n/lang.js +55 -0
- package/dist/i18n/messages.en.js +1140 -0
- package/dist/i18n/messages.pt.js +1149 -0
- package/dist/i18n/words.js +12 -0
- package/dist/ops/accounts.js +170 -0
- package/dist/ops/autoUpdate.js +223 -0
- package/dist/ops/discoverAccounts.js +343 -0
- package/dist/ops/doctor.js +811 -0
- package/dist/ops/elevated.js +47 -0
- package/dist/ops/errorCodes.js +39 -0
- package/dist/ops/follow.js +558 -0
- package/dist/ops/merge.js +830 -0
- package/dist/ops/notify.js +249 -0
- package/dist/ops/plugin.js +92 -0
- package/dist/ops/projects.js +113 -0
- package/dist/ops/quarantine.js +415 -0
- package/dist/ops/rollback.js +868 -0
- package/dist/ops/schedule.js +464 -0
- package/dist/ops/successor.js +51 -0
- package/dist/ops/sync.js +859 -0
- package/dist/ops/syncStatus.js +290 -0
- package/dist/ops/update.js +273 -0
- package/dist/ops/updater.js +641 -0
- package/dist/ops/watch.js +676 -0
- package/dist/ops/watcherControl.js +212 -0
- package/dist/ops/watcherHealth.js +75 -0
- package/dist/organize/apply.js +495 -0
- package/dist/organize/history.js +192 -0
- package/dist/organize/plan.js +283 -0
- package/dist/organize/rename.js +58 -0
- package/dist/organize/sessions.js +100 -0
- package/dist/organize/titles.js +70 -0
- package/dist/organize/types.js +73 -0
- package/dist/plan/categories.js +104 -0
- package/dist/plan/dedupe.js +137 -0
- package/dist/plan/folders.js +82 -0
- package/dist/plan/names.js +93 -0
- package/dist/plan/ownership.js +316 -0
- package/dist/plan/planner.js +669 -0
- package/dist/plan/rehome.js +101 -0
- package/dist/plan/report.js +407 -0
- package/dist/sources/cache.js +89 -0
- package/dist/sources/claude.js +297 -0
- package/dist/sources/codex.js +496 -0
- package/dist/sources/flatten.js +23 -0
- package/dist/sources/jsonl.js +169 -0
- package/dist/sources/native.js +233 -0
- package/dist/sources/opencode.js +483 -0
- package/dist/sources/pi.js +133 -0
- package/dist/sources/registry.js +67 -0
- package/dist/sources/types.js +80 -0
- package/dist/t3/api.js +492 -0
- package/dist/t3/auth.js +248 -0
- package/dist/t3/compat.js +62 -0
- package/dist/t3/decode.js +408 -0
- package/dist/t3/errors.js +185 -0
- package/dist/t3/rpc.js +185 -0
- package/dist/t3/runtime.js +80 -0
- package/dist/ui/caps.js +56 -0
- package/dist/ui/components.js +202 -0
- package/dist/ui/layout.js +205 -0
- package/dist/ui/list.js +423 -0
- package/dist/ui/locale.js +73 -0
- package/dist/ui/organizePreview.js +72 -0
- package/dist/ui/progress.js +237 -0
- package/dist/ui/style.js +32 -0
- package/dist/ui/terminal.js +126 -0
- package/dist/ui/text.js +186 -0
- package/package.json +58 -4
- package/skills/t3code-sessions/SKILL.md +183 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stopping the background watcher (`t3sm sync --watch`) safely. First a stop request over its single-instance
|
|
3
|
+
* endpoint (`stop <token>` -> `ok`; the watcher finishes the current cycle and exits). Only when that does not work
|
|
4
|
+
* the heartbeat pid is killed, and ONLY after its identity is proven: the command line is a t3sm watcher AND the
|
|
5
|
+
* process started when the heartbeat says (pid reuse guard). Never by image name; never pid 0/1/our own.
|
|
6
|
+
*/
|
|
7
|
+
import { access, readFile } from "node:fs/promises";
|
|
8
|
+
import { dirname } from "node:path";
|
|
9
|
+
import { pidAlive, processInfo as realProcessInfo, } from "../core/pid.js";
|
|
10
|
+
import { realRunner } from "./schedule.js";
|
|
11
|
+
import { SocketDirSuffixError, readHeartbeat, legacySingletonAddress, realSingletonNet, singletonAddress, watcherTokenPath, } from "./watch.js";
|
|
12
|
+
import { writeHeartbeatMerged } from "./syncStatus.js";
|
|
13
|
+
|
|
14
|
+
const baseName = (t ) => t.replace(/^["']+|["']+$/g, "").split(/[\\/]/).pop() ?? "";
|
|
15
|
+
|
|
16
|
+
function tokenize(s ) {
|
|
17
|
+
const out = [];
|
|
18
|
+
const re = /"([^"]*)"|'([^']*)'|(\S+)/g;
|
|
19
|
+
for (let m = re.exec(s); m; m = re.exec(s)) out.push(m[1] ?? m[2] ?? m[3] ?? "");
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Rule (tokens; quote and Windows/POSIX path aware): the command line must be `[wrapper] node [node flags] <script>
|
|
25
|
+
* sync ... --watch ...` where: `node` is a token whose basename is node/node.exe; `<script>` has basename t3sm or
|
|
26
|
+
* t3sm.mjs and comes after node with only `-flags` between; the NEXT token is `sync` (a global flag before `sync`,
|
|
27
|
+
* such as `t3sm --quiet sync --watch`, is rejected on purpose: t3sm has none, so such a line is not ours); `--watch`
|
|
28
|
+
* is a later token. The only thing allowed before node is a `conhost(.exe)` wrapper with `-`/`--` flags. An editor or
|
|
29
|
+
* anything else that merely mentions the script, or `t3sm status` / `t3sm sync` without --watch: no.
|
|
30
|
+
* Takes the argv array when the OS gives one (Linux), else the flat command line, tokenized here.
|
|
31
|
+
*/
|
|
32
|
+
export function isT3smWatcher(cmd ) {
|
|
33
|
+
const t = typeof cmd === "string" ? tokenize(cmd) : cmd;
|
|
34
|
+
const n = t.findIndex((x) => /^node(\.exe)?$/i.test(baseName(x)));
|
|
35
|
+
if (n < 0) return false;
|
|
36
|
+
if (n > 0) {
|
|
37
|
+
if (!/^conhost(\.exe)?$/i.test(baseName(t[0] ))) return false;
|
|
38
|
+
if (!t.slice(1, n).every((x) => x.startsWith("-"))) return false;
|
|
39
|
+
}
|
|
40
|
+
let s = n + 1;
|
|
41
|
+
while (s < t.length && t[s] .startsWith("-")) s++;
|
|
42
|
+
if (s >= t.length || !/^t3sm(\.mjs)?$/i.test(baseName(t[s] ))) return false;
|
|
43
|
+
if (t[s + 1] !== "sync") return false;
|
|
44
|
+
return t.slice(s + 2).includes("--watch");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** "closed-without-reply": the 0.2.1 signature (or a refused token); "timed-out": connected, no reply (slow watcher). */
|
|
48
|
+
export async function requestWatcherStop(
|
|
49
|
+
stateDir , platform = process.platform, net = realSingletonNet, addrOpts = {},
|
|
50
|
+
/** Where to send it instead of the derived address (the heartbeat's `socket`). */
|
|
51
|
+
target ,
|
|
52
|
+
) {
|
|
53
|
+
const token = await readFile(watcherTokenPath(stateDir), "utf8").then((s) => s.trim(), () => "");
|
|
54
|
+
const addr = target ?? tryAddress(stateDir, platform, addrOpts);
|
|
55
|
+
if (addr === null) return "no-watcher"; // no address can be derived (see SocketDirSuffixError): nothing to ask
|
|
56
|
+
const r = await net.request(addr, `stop ${token}\n`);
|
|
57
|
+
return r === "ok" ? "stopped" : r === "closed" ? "closed-without-reply" : r;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The derived endpoint, or null when the persisted directory suffix cannot be used (SocketDirSuffixError). */
|
|
61
|
+
function tryAddress(stateDir , platform , o = {}) {
|
|
62
|
+
try { return singletonAddress(stateDir, platform, o); } catch (e) { if (e instanceof SocketDirSuffixError) return null; throw e; }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* win32: the watcher frees its pipe before its process (and the conhost that is the scheduled task's action) has exited.
|
|
91
|
+
* A `schtasks /Run` sent in that gap finds the task still running and is dropped (IgnoreNew, exit 0), so a stop waits
|
|
92
|
+
* up to this long for the pid to go before it returns.
|
|
93
|
+
*/
|
|
94
|
+
export const EXIT_WAIT_MS = 10_000;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The heartbeat of a watcher that is gone (its pid is not alive) without a clean stop: marked stopped now (`stoppedAt`,
|
|
98
|
+
* merged), so `status` and `doctor` stop reporting a crashed watcher once something looked (a stop, `schedule remove`,
|
|
99
|
+
* `schedule install`). A heartbeat that already stopped, or whose pid lives, is left alone. Returns whether it wrote.
|
|
100
|
+
*/
|
|
101
|
+
export async function markGoneWatcher(stateDir , o
|
|
102
|
+
|
|
103
|
+
) {
|
|
104
|
+
const hb = await (o.readHeartbeat ?? readHeartbeat)(stateDir);
|
|
105
|
+
if (hb === null || hb.stoppedAt || (o.pidAlive ?? ((p) => pidAlive(p)))(hb.pid)) return false;
|
|
106
|
+
await (o.writeHeartbeat ?? writeHeartbeatMerged)(stateDir, { ...hb, stoppedAt: new Date(o.now).toISOString(), nextCycleAt: null });
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const POLL_MS = 200;
|
|
111
|
+
const START_SLACK_MS = 2000; // /proc start times are whole seconds
|
|
112
|
+
const START_WINDOW_MS = 120_000;
|
|
113
|
+
const FREE = new Set(["refused", "absent"]);
|
|
114
|
+
const sleep = (c , ms ) => new Promise((ok) => { c.setTimer(ok, ms); });
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* running: from a final probe of the endpoint (the truth, whatever we did). how: "request" the watcher let go after
|
|
118
|
+
* the request; "pid" we killed (or tried to kill) a verified pid; "none" nothing was done to any process.
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
export async function stopWatcher(d ) {
|
|
123
|
+
const read = d.readHeartbeat ?? readHeartbeat;
|
|
124
|
+
const before = d.intent === "restart" ? await read(d.stateDir).catch(() => null) : null;
|
|
125
|
+
const r = await stopOnly(d);
|
|
126
|
+
if (r.running) return r;
|
|
127
|
+
const hb = await read(d.stateDir);
|
|
128
|
+
const alive = (pid ) => Number.isInteger(pid) && pid > 1 && pid !== process.pid && (d.pidAlive ?? ((p) => pidAlive(p)))(pid);
|
|
129
|
+
if (d.platform === "win32" && r.pid !== null) {
|
|
130
|
+
const end = d.clock.now() + (d.exitWaitMs ?? EXIT_WAIT_MS);
|
|
131
|
+
while (alive(r.pid) && d.clock.now() < end) await sleep(d.clock, POLL_MS);
|
|
132
|
+
}
|
|
133
|
+
// What the heartbeat says now: a watcher that is gone without a clean stop is marked stopped; a stop before a restart
|
|
134
|
+
// says so, but only when this call stopped it (or it never stopped cleanly): an old clean stop that this call found
|
|
135
|
+
// as it was is left alone (a restart reason there would read restart-lost forever). Only the heartbeat of the
|
|
136
|
+
// watcher that was there (never a successor's: read again right before the write, same pid and start), best effort.
|
|
137
|
+
const restart = d.intent === "restart" && (r.how !== "none" || (before !== null && !before.stoppedAt));
|
|
138
|
+
try {
|
|
139
|
+
const cur = hb === null ? null : await read(d.stateDir);
|
|
140
|
+
if (hb !== null && cur !== null && cur.pid === hb.pid && cur.startedAt === hb.startedAt
|
|
141
|
+
&& (!cur.stoppedAt || (restart && cur.reason !== "restart")) && (cur.stoppedAt || !alive(cur.pid))) {
|
|
142
|
+
const next = { ...cur, stoppedAt: cur.stoppedAt ?? new Date(d.clock.now()).toISOString(), nextCycleAt: null, ...(restart ? { reason: "restart" } : {}) };
|
|
143
|
+
await (d.writeHeartbeat ?? writeHeartbeatMerged)(d.stateDir, next);
|
|
144
|
+
}
|
|
145
|
+
} catch { /* the heartbeat is only a report */ }
|
|
146
|
+
return r;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function stopOnly(d ) {
|
|
150
|
+
const net = d.net ?? realSingletonNet;
|
|
151
|
+
const derived = tryAddress(d.stateDir, d.platform, d.addr);
|
|
152
|
+
const hb = await (d.readHeartbeat ?? readHeartbeat)(d.stateDir);
|
|
153
|
+
const hbPid = hb && !hb.stoppedAt ? hb.pid : null;
|
|
154
|
+
const hbStart = hb ? Date.parse(hb.startedAt) : Number.NaN;
|
|
155
|
+
const waitMs = d.waitMs ?? 30_000;
|
|
156
|
+
// Where a 0.2.1 watcher may still be listening; a socket there that is not ours (a link, not a socket, another
|
|
157
|
+
// user's) is ignored entirely: it is neither a watcher to stop nor a reason to report "running".
|
|
158
|
+
const legacyAddr = legacySingletonAddress(d.stateDir, d.platform, d.addr);
|
|
159
|
+
const legacy = legacyAddr !== null && (!net.legacySafe || (await net.legacySafe(legacyAddr))) ? legacyAddr : null;
|
|
160
|
+
// The endpoint the running watcher says it bound: a client whose derived address differs still finds it. Only a
|
|
161
|
+
// socket that is ours (a real socket, not a link, our uid) counts, like the legacy address.
|
|
162
|
+
const hbAddr = hb && !hb.stoppedAt && d.platform !== "win32" && typeof hb.socket === "string" && hb.socket !== derived
|
|
163
|
+
&& (!net.legacySafe || (await net.legacySafe(hb.socket))) ? hb.socket : null;
|
|
164
|
+
// An unsafe fallback directory: nothing may be asked of, or believed from, a socket in it. The stop request is
|
|
165
|
+
// skipped; the verified-pid path (command line + start time) still works, and "running" comes from that pid.
|
|
166
|
+
const unsafe = derived === null || (legacyAddr !== null && net.secureDir !== undefined && !(await net.secureDir(dirname(derived))));
|
|
167
|
+
const pidOk = (pid ) => Number.isInteger(pid) && pid > 1 && pid !== process.pid && (d.pidAlive ?? ((p) => pidAlive(p)))(pid);
|
|
168
|
+
/** The heartbeat pid, when it is alive AND provably a t3sm watcher that started when the heartbeat says (else null). */
|
|
169
|
+
const verifiedPid = async () => {
|
|
170
|
+
const pid = hbPid;
|
|
171
|
+
if (pid === null || !pidOk(pid)) return null;
|
|
172
|
+
const info = await (d.processInfo ?? ((p) => realProcessInfo(p, d.platform, realRunner)))(pid);
|
|
173
|
+
if (info === null || !isT3smWatcher(info.argv ?? info.commandLine)) return null;
|
|
174
|
+
// Pid-reuse guard: the process must have started at (or just before) the heartbeat's startedAt.
|
|
175
|
+
if (!Number.isFinite(info.startedAt) || !Number.isFinite(hbStart) || info.startedAt > hbStart + START_SLACK_MS || hbStart - info.startedAt > START_WINDOW_MS) return null;
|
|
176
|
+
return pid;
|
|
177
|
+
};
|
|
178
|
+
const freeAt = async (a ) => a === null || FREE.has(await net.probe(a));
|
|
179
|
+
const isFree = async () => unsafe
|
|
180
|
+
? (await freeAt(legacy)) && (await freeAt(hbAddr)) && (await verifiedPid()) === null
|
|
181
|
+
: (await freeAt(derived)) && (await freeAt(hbAddr)) && (await freeAt(legacy));
|
|
182
|
+
const waitFree = async () => {
|
|
183
|
+
const end = d.clock.now() + waitMs;
|
|
184
|
+
for (;;) {
|
|
185
|
+
if (await isFree()) return true;
|
|
186
|
+
if (d.clock.now() >= end) return false;
|
|
187
|
+
await sleep(d.clock, POLL_MS);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// Requests go to the heartbeat's address when there is one, else to the derived one (never into an unsafe directory).
|
|
192
|
+
let r = hbAddr !== null ? await requestWatcherStop(d.stateDir, d.platform, net, d.addr, hbAddr)
|
|
193
|
+
: unsafe ? "no-watcher" : await requestWatcherStop(d.stateDir, d.platform, net, d.addr, derived ?? undefined);
|
|
194
|
+
// A live legacy address is a 0.2.1 watcher: it never answers, so it takes the verified-pid path.
|
|
195
|
+
if (r === "no-watcher" && legacy !== null && (await net.probe(legacy)) === "alive") r = "closed-without-reply";
|
|
196
|
+
if (r === "no-watcher" && !unsafe) return { how: "none", pid: null, running: !(await isFree()) };
|
|
197
|
+
// A new-protocol watcher whose token write failed refuses every request: that is not the 0.2.1 signature, so it gets the full wait too.
|
|
198
|
+
if (r === "closed-without-reply" && (hb?.protocol ?? 0) >= 1 && !(await access(watcherTokenPath(d.stateDir)).then(() => true, () => false))) r = "timed-out";
|
|
199
|
+
// A slow watcher (answered, or connected without a reply) gets the full wait; a closed socket (0.2.1) skips it.
|
|
200
|
+
if ((r === "stopped" || r === "timed-out") && (await waitFree())) return { how: "request", pid: hbPid, running: false };
|
|
201
|
+
|
|
202
|
+
const none = async () => ({ how: "none", pid: null, running: !(await isFree()) });
|
|
203
|
+
const pid = await verifiedPid();
|
|
204
|
+
if (pid === null) return none();
|
|
205
|
+
try {
|
|
206
|
+
d.kill(pid);
|
|
207
|
+
} catch (e) {
|
|
208
|
+
if ((e ).code !== "ESRCH") return { how: "pid", pid, running: true }; // EPERM etc.: it is still there
|
|
209
|
+
}
|
|
210
|
+
await waitFree();
|
|
211
|
+
return { how: "pid", pid, running: !(await isFree()) };
|
|
212
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// What state the background watcher is in, from its heartbeat alone (pure: no file, no process, no T3 Code). `doctor`
|
|
2
|
+
// (the sync-watcher check) and `status` both classify through this one function, so they never disagree.
|
|
3
|
+
//
|
|
4
|
+
// none no heartbeat: no watcher ever ran (sync runs on the schedule or by hand)
|
|
5
|
+
// stopped stopped cleanly (a stop request, `schedule remove`)
|
|
6
|
+
// restart-lost stopped to restart into a newer install, and no successor wrote a heartbeat within RESTART_LOST_MS
|
|
7
|
+
// gone its pid is gone without a clean stop
|
|
8
|
+
// no-version running, but from a t3sm that does not record its version (0.2.1): it never restarts after an upgrade
|
|
9
|
+
// no-stamp running, recorded its version but could not read its install stamp: it does not restart either
|
|
10
|
+
// stale-version running on another version (or another build) than the install holds, and it had a cycle after
|
|
11
|
+
// the install settled (INSTALL_SETTLE_MS) without restarting into it
|
|
12
|
+
// running running on what the install holds (or a new install it has not had a cycle to notice yet:
|
|
13
|
+
// `pendingVersion`); `fresh`: its cycles are on time (a late one is stale unless T3 Code is closed)
|
|
14
|
+
|
|
15
|
+
import { parseSemver } from "../core/semver.js";
|
|
16
|
+
import { INSTALL_SETTLE_MS, } from "../core/version.js";
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* How long a watcher that stopped to restart into a new install may stay without a successor. Longer than the scheduled
|
|
21
|
+
* task's 15-minute keep-alive trigger, which starts a watcher when none holds the pipe.
|
|
22
|
+
*/
|
|
23
|
+
export const RESTART_LOST_MS = 20 * 60_000;
|
|
24
|
+
/** How late a cycle may be (past nextCycleAt, or two intervals after the last cycle) before the watcher counts as stale. */
|
|
25
|
+
export const CYCLE_SLACK_MS = 10 * 60_000;
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
export function watcherHealth(hb , o ) {
|
|
47
|
+
if (hb === null) return { state: "none" };
|
|
48
|
+
if (hb.stoppedAt) {
|
|
49
|
+
const at = Date.parse(hb.stoppedAt);
|
|
50
|
+
if (hb.reason === "restart" && Number.isFinite(at) && o.now - at > RESTART_LOST_MS) return { state: "restart-lost", pid: hb.pid, stoppedAt: hb.stoppedAt };
|
|
51
|
+
return { state: "stopped", pid: hb.pid, stoppedAt: hb.stoppedAt };
|
|
52
|
+
}
|
|
53
|
+
if (!o.pidAlive(hb.pid)) return { state: "gone", pid: hb.pid, lastCycleAt: hb.lastCycleAt ?? null };
|
|
54
|
+
// Stale version: the watcher checks the install after every cycle and restarts into a new one. One that had a cycle
|
|
55
|
+
// since the install changed and still runs the old one will not (a 0.2.1 watcher, without a version, never does).
|
|
56
|
+
// A version that is not a release version (a hand-edited or hostile file) is unknown: never shown, never compared.
|
|
57
|
+
if (typeof hb.version !== "string" || parseSemver(hb.version) === null) return { state: "no-version", pid: hb.pid };
|
|
58
|
+
if (typeof hb.stamp !== "string") return { state: "no-stamp", pid: hb.pid, version: hb.version };
|
|
59
|
+
let pendingVersion = null;
|
|
60
|
+
const read = o.install(typeof hb.installRoot === "string" ? hb.installRoot : null);
|
|
61
|
+
const inst = read && parseSemver(read.version) !== null ? read : null; // an unreadable version: as an unreadable install
|
|
62
|
+
if (inst && (inst.version !== hb.version || inst.stamp !== hb.stamp)) {
|
|
63
|
+
const since = Date.parse(hb.lastCycleAt ?? hb.startedAt);
|
|
64
|
+
// The watcher deliberately skips a restart while the install changed less than INSTALL_SETTLE_MS before its cycle ended.
|
|
65
|
+
if (inst.changedAtMs === null || !Number.isFinite(since) || inst.changedAtMs + INSTALL_SETTLE_MS <= since) {
|
|
66
|
+
return { state: "stale-version", pid: hb.pid, version: hb.version, installed: inst.version, rebuilt: inst.version === hb.version };
|
|
67
|
+
}
|
|
68
|
+
pendingVersion = inst.version;
|
|
69
|
+
}
|
|
70
|
+
const every = o.everyMinutes * 60_000;
|
|
71
|
+
const next = hb.nextCycleAt ? Date.parse(hb.nextCycleAt) : NaN;
|
|
72
|
+
const last = Date.parse(hb.lastCycleAt ?? hb.startedAt);
|
|
73
|
+
const fresh = Number.isFinite(next) ? o.now <= next + CYCLE_SLACK_MS : Number.isFinite(last) && o.now - last <= 2 * every + CYCLE_SLACK_MS;
|
|
74
|
+
return { state: "running", pid: hb.pid, version: hb.version, fresh, pendingVersion, lastCycleAt: hb.lastCycleAt ?? null, nextCycleAt: hb.nextCycleAt ?? null };
|
|
75
|
+
}
|