decklight 0.5.0 → 0.7.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.
Files changed (53) hide show
  1. package/README.md +20 -9
  2. package/SPEC.md +239 -17
  3. package/cli/agents.mjs +97 -2
  4. package/cli/banner.mjs +125 -0
  5. package/cli/bundle.mjs +14 -4
  6. package/cli/{rec.mjs → cast.mjs} +25 -8
  7. package/cli/comments.mjs +389 -0
  8. package/cli/commit-flow.mjs +157 -0
  9. package/cli/commit-message.mjs +475 -0
  10. package/cli/decklight.mjs +95 -25
  11. package/cli/dev.mjs +193 -25
  12. package/cli/edit.mjs +1055 -39
  13. package/cli/git-tree.mjs +87 -0
  14. package/cli/git.mjs +370 -0
  15. package/cli/history.mjs +145 -0
  16. package/cli/init.mjs +199 -7
  17. package/cli/marketplace.mjs +123 -17
  18. package/cli/port-conflict.mjs +228 -22
  19. package/cli/present.mjs +297 -12
  20. package/cli/publish.mjs +15 -40
  21. package/cli/record.mjs +154 -0
  22. package/cli/restore.mjs +120 -4
  23. package/cli/review-remote.mjs +330 -0
  24. package/cli/review-store.mjs +107 -0
  25. package/cli/review-submit.mjs +292 -0
  26. package/cli/review.mjs +361 -0
  27. package/cli/serve.mjs +60 -1
  28. package/cli/skill-content.mjs +12 -1
  29. package/cli/theme.mjs +78 -11
  30. package/cli/units.mjs +2 -2
  31. package/cli/upstream.mjs +256 -0
  32. package/cli/util.mjs +59 -0
  33. package/dist/build-info.json +1 -0
  34. package/dist/decklight.css +534 -3
  35. package/dist/decklight.js +102 -76
  36. package/dist/decklight.js.map +4 -4
  37. package/docs/architecture.svg +7 -0
  38. package/package.json +2 -1
  39. package/tools/args.mjs +20 -0
  40. package/tools/deck-html.mjs +84 -0
  41. package/tools/elevenlabs-tts.mjs +1 -1
  42. package/tools/lipsync-server.mjs +64 -2
  43. package/tools/lipsync.mjs +40 -24
  44. package/tools/local-voice.mjs +170 -16
  45. package/tools/publish-voices.mjs +78 -18
  46. package/tools/recording-impact.mjs +110 -0
  47. package/tools/review-anchor.mjs +180 -0
  48. package/tools/test-impact.mjs +259 -0
  49. package/tools/tts-engines.mjs +211 -12
  50. package/tools/tts-setup.mjs +2 -9
  51. package/tools/video.mjs +56 -12
  52. package/tools/voiceover-server.mjs +262 -11
  53. package/tools/voiceover.mjs +32 -13
@@ -13,7 +13,8 @@
13
13
  // EADDRINUSE, and proactively, by author BEFORE it spawns the edit child —
14
14
  // that child's stdin is closed (piped, not a TTY), so it could never ask.
15
15
 
16
- import { createConnection } from 'node:net';
16
+ import { createConnection, createServer } from 'node:net';
17
+ import { execFileSync } from 'node:child_process';
17
18
 
18
19
  /** Is something listening on `port`? Port 0 ("OS picks one") never conflicts. */
19
20
  export function isPortOpen(port, host = '127.0.0.1', timeout = 400) {
@@ -38,6 +39,55 @@ export async function identifyEditServer(port, host = '127.0.0.1') {
38
39
  }
39
40
  }
40
41
 
42
+ /**
43
+ * The same question for a BRIDGE — the voice and lip-sync servers answer
44
+ * `/ping`, not `/edit/ping`, so an edit-server probe reports them as strangers
45
+ * and the offer to take the port over never appears.
46
+ *
47
+ * Returns the same shape the caller already handles: something with a `name`
48
+ * to put in a sentence.
49
+ */
50
+ export async function identifyBridge(port, host = '127.0.0.1') {
51
+ try {
52
+ const res = await fetch(`http://${host}:${port}/ping`, { signal: AbortSignal.timeout(800) });
53
+ if (!res.ok) return null;
54
+ const d = await res.json();
55
+ if (!d?.ok) return null;
56
+ // tts answers with an engine, lipsync with its own shape; either way the
57
+ // point is that a decklight bridge is there and can be asked to stop.
58
+ return { ...d, bridge: true, name: d.engine ? `the ${d.engine} voice bridge` : 'a decklight bridge' };
59
+ } catch {
60
+ return null;
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Who holds this port, when it is NOT one of ours.
66
+ *
67
+ * This file's whole premise was "ask it over HTTP, never guess at a PID — no
68
+ * lsof, no platform split", and that is still right for anything decklight can
69
+ * stop cleanly. But it left the commonest real case mute: a stranger's process
70
+ * on the port produced a bare "using a different port", or — for the bridges,
71
+ * which never called any of this — a raw EADDRINUSE stack trace.
72
+ *
73
+ * So a stranger gets NAMED, best effort, and only to put in a question. `lsof`
74
+ * is macOS/Linux; Windows and a missing lsof simply return null and the caller
75
+ * falls back to moving ports, which is what it did before.
76
+ */
77
+ export function identifyStranger(port, { exec = null } = {}) {
78
+ if (process.platform === 'win32') return null;
79
+ try {
80
+ const run = exec ?? ((cmd, args) => execFileSync(cmd, args, { encoding: 'utf8', timeout: 2000 }));
81
+ const out = run('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN', '-F', 'pc']);
82
+ // `-F pc` prints `p<pid>` and `c<command>` on their own lines
83
+ const pid = /^p(\d+)$/m.exec(out)?.[1];
84
+ const command = /^c(.+)$/m.exec(out)?.[1];
85
+ return pid ? { pid: Number(pid), command: command ?? 'an unknown process' } : null;
86
+ } catch {
87
+ return null; // no lsof, no permission, nothing listening — all "we cannot say"
88
+ }
89
+ }
90
+
41
91
  /** Ask it to stop — it auto-commits and exits, same as its own Ctrl-C. Resolves once the port is free. */
42
92
  export async function shutdownEditServer(port, host = '127.0.0.1') {
43
93
  try {
@@ -49,45 +99,201 @@ export async function shutdownEditServer(port, host = '127.0.0.1') {
49
99
  return !(await isPortOpen(port, host));
50
100
  }
51
101
 
52
- /** The next port at or after `port` that nothing is listening on. */
53
- export async function nextFreePort(port, host = '127.0.0.1') {
54
- let p = port;
55
- while (await isPortOpen(p, host)) p++;
56
- return p;
102
+ /**
103
+ * Can we actually LISTEN on this port?
104
+ *
105
+ * `isPortOpen` asks a different question — it connects, so it answers "is
106
+ * somebody there". For "may I have this port" that is the wrong question, and
107
+ * on Windows it is wrong in a way that fails: Hyper-V and WinNAT reserve blocks
108
+ * of ephemeral ports (`netsh int ipv4 show excludedportrange`), and a reserved
109
+ * port has NOTHING LISTENING on it. Connect is refused, `isPortOpen` says
110
+ * "free", and the bind that follows gets `EACCES` — a permission error, not
111
+ * `EADDRINUSE`, which is why it read as a crash rather than a busy port (#334).
112
+ *
113
+ * Binding is the only probe that answers the question being asked. It costs one
114
+ * listen/close per candidate, which is cheaper than the connect it replaces.
115
+ */
116
+ export function canBind(port, host = '127.0.0.1') {
117
+ return new Promise((done) => {
118
+ const server = createServer();
119
+ // Every reason a bind can fail is a reason to try the next port: taken
120
+ // (EADDRINUSE), reserved or privileged (EACCES), or an address this host
121
+ // does not have (EADDRNOTAVAIL). None of them become usable by waiting.
122
+ server.once('error', () => done(false));
123
+ server.once('listening', () => server.close(() => done(true)));
124
+ try { server.listen(port, host); } catch { done(false); }
125
+ });
126
+ }
127
+
128
+ /**
129
+ * The next port at or after `port` this process can actually bind.
130
+ *
131
+ * Bounded, because the failure it now catches can repeat: a Hyper-V reservation
132
+ * is a BLOCK of ports, often 16 or more, and an unbounded walk through one is
133
+ * indistinguishable from a hang. Giving up returns the original port so the
134
+ * caller fails with the real bind error rather than on a number this function
135
+ * invented.
136
+ */
137
+ export async function nextFreePort(port, host = '127.0.0.1', tries = 64) {
138
+ for (let p = port; p < port + tries && p <= 65535; p++) {
139
+ if (await canBind(p, host)) return p;
140
+ }
141
+ return port;
57
142
  }
58
143
 
59
144
  /**
60
145
  * Pure: what to do about a taken port — testable without a socket or a
61
146
  * terminal. Only offers to kill an occupant we can actually identify AND
62
- * only when something can be asked; anything else just moves to a free port.
147
+ * only when something can be asked.
148
+ *
149
+ * A BRIDGE MAY NEVER MOVE, and that is the whole reason `kind` exists. The
150
+ * two servers fail differently because only one of them gets to announce
151
+ * where it landed:
152
+ *
153
+ * the EDIT server hands its address to a human. It prints the URL it
154
+ * actually bound and you open that one, so bumping costs nothing.
155
+ *
156
+ * a BRIDGE hands its address to nobody. The deck does not discover the
157
+ * bridge, it ASSUMES it: `src/core/narration.js` hardcodes
158
+ * `http://127.0.0.1:8787/tts` (and derives /ping, /engines, /voices from
159
+ * it), `src/core/character.js` hardcodes `:8789`, and the only override is
160
+ * a `liveUrl`/`bridgeUrl` written into the deck itself. author passes
161
+ * `--tts-port` to the bridge process and never tells the deck — it could
162
+ * not tell it anyway, since the deck is served verbatim and is as often
163
+ * opened over `file://`. The two sides agree only because both default to
164
+ * the same literal.
165
+ *
166
+ * So a bridge that bumps is a bridge nobody is talking to: the deck keeps
167
+ * knocking on 8787, reaches whatever squats there, and reports no live voice
168
+ * with nothing on screen to explain it. That is strictly worse than the
169
+ * EADDRINUSE crash this replaced — the crash at least said something. A
170
+ * bridge therefore either takes ITS port, or stands aside for the bridge
171
+ * already serving it (`reuse`), or declines to start and says why
172
+ * (`refuse`).
63
173
  */
64
- export function planPortConflict({ tty = false, identified = null } = {}) {
65
- return tty && identified ? 'ask' : 'bump';
174
+ export function planPortConflict({ tty = false, identified = null, stranger = null, kind = 'edit' } = {}) {
175
+ // What happens with no terminal — and what a DECLINED offer falls back to.
176
+ const fallback = kind !== 'bridge' ? 'bump' // an edit server announces itself
177
+ : identified ? 'reuse' // ours, already answering: use it
178
+ : 'refuse'; // a bridge off its port is a lie
179
+ if (!tty) return fallback; // never kill anything unattended
180
+ if (identified) return 'ask'; // ours: offer a CLEAN shutdown
181
+ if (stranger) return 'ask-stranger'; // somebody else's: offer, never assume
182
+ return fallback; // nothing we can name
66
183
  }
67
184
 
68
185
  /**
69
186
  * The port is already bound. Work out who's there and either take it over or
70
- * move on. `ask` is `(prompt) => Promise<string>`; its absence means no TTY
71
- * to ask, so the port just bumps. Returns the port actually free to use.
187
+ * step aside. `ask` is `(prompt) => Promise<string>`; its absence means no
188
+ * TTY to ask on, and nothing is ever killed without one.
189
+ *
190
+ * Returns the port to bind, or **null meaning "do not listen at all"** — a
191
+ * bridge whose port is spoken for, which cannot just move (see
192
+ * planPortConflict). `kind: 'edit'` never returns null, so the two existing
193
+ * callers are unaffected.
72
194
  */
73
- export async function resolvePortConflict(port, { host = '127.0.0.1', ask, log = () => {} } = {}) {
74
- const identified = await identifyEditServer(port, host);
75
- const action = planPortConflict({ tty: Boolean(ask), identified });
195
+ export async function resolvePortConflict(port, {
196
+ host = '127.0.0.1', ask, log = () => {}, kind = 'edit', stranger = identifyStranger,
197
+ identify = null,
198
+ } = {}) {
199
+ // Ours, whichever kind: an edit server answers /edit/ping, a bridge answers
200
+ // /ping. Asking only the first reported every bridge as a stranger.
201
+ //
202
+ // Injectable for the same reason `stranger` is: this REALLY CONNECTS to the
203
+ // port, so a test naming a plausible number is at the mercy of whatever the
204
+ // developer happens to be running. That is not hypothetical — the refusal
205
+ // test named 8787, and passed everywhere except on a machine with a live
206
+ // voice bridge on it, which is every machine this feature is for.
207
+ const whoIsThere = identify ?? ((p, h) => (kind === 'bridge'
208
+ ? identifyBridge(p, h)
209
+ : identifyEditServer(p, h).then((e) => e ?? identifyBridge(p, h))));
210
+ const identified = await whoIsThere(port, host);
211
+ const other = identified ? null : stranger(port);
212
+ const action = planPortConflict({ tty: Boolean(ask), identified, stranger: other, kind });
76
213
 
77
- if (action === 'ask') {
78
- log(` port ${port} is already in use — decklight is editing "${identified.name}" there`);
214
+ // Somebody ELSE's process. decklight cannot ask it to stop the way it asks
215
+ // its own — there is no endpoint to POST to — so the only lever is a signal,
216
+ // and a signal is not something to send on decklight's own judgement: that
217
+ // process may be a dev server with unsaved work in it. Named, offered, and
218
+ // DEFAULTED TO THE HARMLESS ANSWER, so the destructive one is only ever
219
+ // reached by typing it.
220
+ let named = false; // the occupant is worth naming once, not once per branch
221
+ if (action === 'ask-stranger') {
222
+ log(` port ${port} is held by ${other.command} (pid ${other.pid}) — not a decklight server`);
223
+ named = true;
224
+ // The alternative to killing is NOT the same for both kinds, so it must not
225
+ // be worded the same: an edit server moves, a bridge does not get to.
226
+ const or = kind === 'bridge' ? '[c]arry on without this bridge? [k/C]' : 'use a [d]ifferent port? [k/D]';
227
+ const answer = (await ask(` [k]ill pid ${other.pid} and take the port, or ${or} `)).trim().toLowerCase();
228
+ if (answer.startsWith('k')) {
229
+ try {
230
+ process.kill(other.pid, 'SIGTERM');
231
+ for (let i = 0; i < 30 && (await isPortOpen(port, host)); i++) await new Promise((r) => setTimeout(r, 100));
232
+ if (!(await isPortOpen(port, host))) { log(` stopped pid ${other.pid}`); return port; }
233
+ log(' it did not stop in time — using a different port instead');
234
+ } catch (e) {
235
+ log(` could not stop pid ${other.pid} (${e.code ?? e.message}) — using a different port instead`);
236
+ }
237
+ }
238
+ } else if (action === 'ask') {
239
+ log(identified.bridge
240
+ ? ` port ${port} is already in use — ${identified.name} is running there`
241
+ : ` port ${port} is already in use — decklight is editing "${identified.name}" there`);
79
242
  const answer = (await ask(' [k]ill that session and take it over, or use a [d]ifferent port? [k/D] ')).trim().toLowerCase();
80
243
  if (answer.startsWith('k')) {
81
- log(` stopping the session editing "${identified.name}"…`);
82
- if (await shutdownEditServer(port, host)) return port;
244
+ log(` stopping ${identified.bridge ? identified.name : `the session editing "${identified.name}"`}…`);
245
+ // An edit server is asked to stop, because it has a final autocommit to
246
+ // make and losing that would be losing work. A BRIDGE has no such
247
+ // endpoint and nothing to lose — it is a stateless synthesiser — so it
248
+ // gets a signal, using the pid the stranger lookup finds. Without a pid
249
+ // (Windows, no lsof) there is nothing to do but move ports, which is
250
+ // exactly what happens.
251
+ if (!identified.bridge) {
252
+ if (await shutdownEditServer(port, host)) return port;
253
+ } else {
254
+ const who = stranger(port);
255
+ if (who) {
256
+ try {
257
+ process.kill(who.pid, 'SIGTERM');
258
+ for (let i = 0; i < 30 && (await isPortOpen(port, host)); i++) await new Promise((r) => setTimeout(r, 100));
259
+ if (!(await isPortOpen(port, host))) return port;
260
+ } catch { /* fall through to the bump */ }
261
+ }
262
+ }
83
263
  log(" it didn't stop in time — using a different port instead");
84
264
  }
85
- } else {
86
- log(identified
87
- ? ` port ${port} is already in use — decklight is editing "${identified.name}" there; using a different port`
88
- : ` port ${port} is already in use; using a different port`);
89
265
  }
90
266
 
267
+ // Whatever was offered was declined, or there was nobody to ask. How that
268
+ // ends is the caller's kind, not this function's mood.
269
+ const ending = planPortConflict({ tty: false, identified, stranger: other, kind });
270
+
271
+ // Ours, and already doing the job. A second bridge would bind a port the
272
+ // deck never calls, so the useful move is to stand aside and say so: null
273
+ // means "do not listen", and the one on `port` serves the deck as before.
274
+ if (ending === 'reuse') {
275
+ log(` port ${port} already has ${identified.name} on it — using that one rather than starting a second`);
276
+ return null;
277
+ }
278
+
279
+ // Somebody else's, and a bridge cannot work anywhere but here. Say who has
280
+ // it and both ways out, then decline: author reports the bridge as gone and
281
+ // the deck degrades honestly, which beats a live voice that fails silently.
282
+ if (ending === 'refuse') {
283
+ if (!named) {
284
+ log(other
285
+ ? ` port ${port} is held by ${other.command} (pid ${other.pid}) — not a decklight bridge`
286
+ : ` port ${port} is already in use`);
287
+ }
288
+ log(` the deck asks for this bridge on ${port} and nowhere else, so it will not start.`);
289
+ log(` free the port (kill ${other ? other.pid : 'the process holding it'}), or move BOTH sides:`);
290
+ log(` run with --port N and set narration.liveUrl in the deck to match.`);
291
+ return null;
292
+ }
293
+
294
+ log(identified
295
+ ? ` port ${port} is already in use — ${identified.bridge ? identified.name : `decklight is editing "${identified.name}"`} there; using a different port`
296
+ : ` port ${port} is already in use; using a different port`);
91
297
  const next = await nextFreePort(port + 1, host);
92
298
  log(` → using port ${next}`);
93
299
  return next;