@pellux/goodvibes-tui 1.24.2 → 1.26.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 (77) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +1 -1
  3. package/bin/goodvibes-daemon +24 -0
  4. package/docs/foundation-artifacts/operator-contract.json +18234 -12253
  5. package/package.json +8 -7
  6. package/src/cli/help.ts +10 -0
  7. package/src/cli/redaction.ts +68 -3
  8. package/src/config/goodvibes-home.ts +149 -0
  9. package/src/config/secret-config.ts +64 -8
  10. package/src/core/turn-event-wiring.ts +7 -16
  11. package/src/core/workstream-notification.ts +69 -0
  12. package/src/daemon/cli.ts +62 -7
  13. package/src/daemon/handlers/credentials.ts +15 -5
  14. package/src/daemon/handlers/index.ts +7 -6
  15. package/src/daemon/send/channels.ts +200 -0
  16. package/src/daemon/send/command.ts +332 -0
  17. package/src/daemon/send/composition.ts +99 -0
  18. package/src/daemon/send/failure-text.ts +93 -0
  19. package/src/daemon/send/inert-text.ts +225 -0
  20. package/src/daemon/send/stdin.ts +24 -0
  21. package/src/input/commands/calendar-runtime.ts +245 -0
  22. package/src/input/commands/config.ts +68 -2
  23. package/src/input/commands/connection-status.ts +258 -0
  24. package/src/input/commands/local-provider-runtime.ts +9 -1
  25. package/src/input/commands/mail-runtime.ts +267 -0
  26. package/src/input/commands/operator-runtime.ts +6 -1
  27. package/src/input/commands/payment-card-intake.ts +200 -0
  28. package/src/input/commands/profile-render.ts +250 -0
  29. package/src/input/commands/profile-runtime.ts +362 -0
  30. package/src/input/commands/profile-types.ts +381 -0
  31. package/src/input/commands/review-runtime.ts +13 -1
  32. package/src/input/commands.ts +11 -0
  33. package/src/input/composer-editor.ts +4 -0
  34. package/src/input/onboarding/onboarding-wizard-apply.ts +13 -1
  35. package/src/input/payments-config.ts +167 -0
  36. package/src/input/provider-key-intake.ts +10 -2
  37. package/src/input/settings-modal-commit.ts +146 -0
  38. package/src/input/settings-modal-connections.ts +102 -0
  39. package/src/input/settings-modal-data.ts +30 -0
  40. package/src/input/settings-modal-reset.ts +6 -2
  41. package/src/input/settings-modal-secrets.ts +9 -2
  42. package/src/input/settings-modal-types.ts +19 -3
  43. package/src/input/settings-modal.ts +54 -107
  44. package/src/main.ts +7 -2
  45. package/src/panels/modals/marketplace-modal.ts +5 -0
  46. package/src/panels/modals/skills-modal.ts +5 -0
  47. package/src/renderer/settings-modal-connections.ts +63 -0
  48. package/src/renderer/settings-modal-helpers.ts +6 -0
  49. package/src/renderer/settings-modal.ts +21 -1
  50. package/src/runtime/bootstrap-core.ts +1 -1
  51. package/src/runtime/bootstrap.ts +2 -2
  52. package/src/runtime/context.ts +9 -0
  53. package/src/runtime/credential-composition.ts +54 -0
  54. package/src/runtime/daemon-handler-composition.ts +0 -4
  55. package/src/runtime/install-self-check-startup.ts +23 -0
  56. package/src/runtime/mail-composition.ts +65 -0
  57. package/src/runtime/onboarding/types.ts +6 -1
  58. package/src/runtime/path-shadow-startup.ts +243 -0
  59. package/src/runtime/path-shadow.ts +418 -0
  60. package/src/runtime/reachability-notice.ts +100 -0
  61. package/src/runtime/runtime-services-types.ts +243 -0
  62. package/src/runtime/services.ts +30 -224
  63. package/src/shell/ui-openers.ts +7 -0
  64. package/src/verification/verification-ledger.ts +158 -0
  65. package/src/version.ts +1 -1
  66. package/src/daemon/handlers/calendar/caldav-client.ts +0 -662
  67. package/src/daemon/handlers/calendar/ics.ts +0 -556
  68. package/src/daemon/handlers/calendar/index.ts +0 -316
  69. package/src/daemon/handlers/email/config.ts +0 -164
  70. package/src/daemon/handlers/email/imap-connector.ts +0 -441
  71. package/src/daemon/handlers/email/imap-parsing.ts +0 -499
  72. package/src/daemon/handlers/email/index.ts +0 -43
  73. package/src/daemon/handlers/email/read-handlers.ts +0 -80
  74. package/src/daemon/handlers/email/runtime.ts +0 -140
  75. package/src/daemon/handlers/email/smtp-connector.ts +0 -557
  76. package/src/daemon/handlers/email/validation.ts +0 -147
  77. package/src/daemon/handlers/email/write-handlers.ts +0 -133
@@ -0,0 +1,418 @@
1
+ /**
2
+ * path-shadow.ts — decide whether the copy of a command the updater maintains
3
+ * is actually the copy the user's shell runs.
4
+ *
5
+ * OWNERSHIP: this is a verbatim copy of the SDK's canonical module,
6
+ * `@pellux/goodvibes-sdk/platform/runtime/path-shadow`, which is where the
7
+ * policy lives because more than one surface needs it (this TUI, the agent,
8
+ * and the installer's POSIX sh statement of the same rules). The copy exists
9
+ * only because the SDK release carrying that module has not been published
10
+ * yet and this package pins an exact published version. When it publishes,
11
+ * replace this file's body with a re-export of the SDK subpath — exactly the
12
+ * shape update-check.ts already uses for the update policy — and delete
13
+ * nothing else; the wiring in path-shadow-startup.ts and the tests in
14
+ * src/test/runtime/path-shadow.test.ts keep working unchanged.
15
+ *
16
+ * The platform's update guarantee is "one verified swap mechanism, nothing
17
+ * ever drifts". That guarantee holds only while there is exactly one copy of
18
+ * each command reachable on PATH. When a second, older copy sits in a
19
+ * directory that comes EARLIER on PATH than the install directory, the
20
+ * updater keeps upgrading a file the user never reaches: `goodvibes-agent`
21
+ * reports itself current, an older build answers the typing, and the product
22
+ * looks like it is lying about its own capabilities. That is exactly what
23
+ * happened with a leftover `~/.bun/bin/goodvibes-agent` link (1.18.1) sitting
24
+ * at PATH position 2 while `~/.local/bin/goodvibes-agent` (1.21.0) sat at
25
+ * position 21.
26
+ *
27
+ * This module is the one place that decides those facts. It is pure: every
28
+ * filesystem and subprocess touch (does this file exist and is it runnable,
29
+ * where does this symlink actually point, what does `<path> --version` say)
30
+ * is injected, so the policy is provable under test with fake paths and no
31
+ * real install. The surfaces wire the real I/O:
32
+ * - a client's startup check reports a shadow before anything else renders;
33
+ * - the installer's shell implementation (goodvibes-tui scripts/install.sh)
34
+ * mirrors these same rules in POSIX sh, because an installer cannot
35
+ * import TypeScript.
36
+ *
37
+ * Removal is deliberately conservative. A copy is only ever offered for
38
+ * removal when it is recognisably one of OUR programs — a link into an
39
+ * installed `@pellux/goodvibes-*` package, or a file that answers
40
+ * `--version` with `<command> <semver>` — and only when it lives inside the
41
+ * user's own home directory. Everything else is reported and left alone.
42
+ */
43
+
44
+ // ---------------------------------------------------------------------------
45
+ // Types
46
+ // ---------------------------------------------------------------------------
47
+
48
+ /**
49
+ * How confident we are that a copy found on PATH is one of our own programs,
50
+ * and therefore whether it is safe to offer to remove it.
51
+ *
52
+ * - `install-target`: the copy living in the directory the installer and
53
+ * the auto-updater maintain. Never a removal candidate.
54
+ * - `package-link`: an entry that resolves into an installed
55
+ * `@pellux/goodvibes-*` package (a `bun add -g` / `npm i -g` link, or a
56
+ * project-local dependency link). Removed by uninstalling that package.
57
+ * - `our-binary`: a standalone file that answers `--version` with
58
+ * `<command> <semver>`, i.e. a previous standalone install of the same
59
+ * program. Removed by deleting the file.
60
+ * - `unknown`: anything we cannot positively identify, including anything
61
+ * outside the user's home directory. Reported, never removed.
62
+ */
63
+ export type ShadowOwnership = 'install-target' | 'package-link' | 'our-binary' | 'unknown';
64
+
65
+ /** How a recognised copy is removed, and by what command. */
66
+ export interface ShadowRemoval {
67
+ /** `package` = uninstall the owning package; `file` = delete the PATH entry itself. */
68
+ readonly kind: 'package' | 'file';
69
+ /** The `@pellux/...` package name, when `kind` is `package`. */
70
+ readonly packageName?: string | undefined;
71
+ /** The exact command a user (or the installer) runs to remove this copy. */
72
+ readonly command: string;
73
+ }
74
+
75
+ /** One copy of a command found on PATH. */
76
+ export interface CommandCopy {
77
+ /** The command name, e.g. `goodvibes-agent`. */
78
+ readonly command: string;
79
+ /** The PATH directory the copy was found in. */
80
+ readonly directory: string;
81
+ /** `<directory>/<command>` — the path the shell would execute. */
82
+ readonly path: string;
83
+ /** Zero-based position of `directory` in PATH. Lower wins. */
84
+ readonly pathIndex: number;
85
+ /** The symlink-resolved path, or `path` when it is not a link. */
86
+ readonly resolvedPath: string;
87
+ /** What `<path> --version` reported, when it could be probed. */
88
+ readonly version?: string | undefined;
89
+ readonly ownership: ShadowOwnership;
90
+ /** Present only when `ownership` is `package-link` or `our-binary`. */
91
+ readonly removal?: ShadowRemoval | undefined;
92
+ }
93
+
94
+ /** The reachability verdict for one command. */
95
+ export interface CommandShadowReport {
96
+ readonly command: string;
97
+ /** Every copy found, in PATH order. */
98
+ readonly copies: readonly CommandCopy[];
99
+ /** The copy the shell actually runs — the first on PATH. Absent when the command is not on PATH at all. */
100
+ readonly winner?: CommandCopy | undefined;
101
+ /** The copy in the install directory. Absent when the install directory is not on PATH. */
102
+ readonly installed?: CommandCopy | undefined;
103
+ /**
104
+ * Copies that come earlier on PATH than the install directory's copy. A
105
+ * non-empty list means the maintained install is unreachable by name.
106
+ */
107
+ readonly shadowing: readonly CommandCopy[];
108
+ /** True when the install directory holds this command but is not on PATH at all. */
109
+ readonly installDirNotOnPath: boolean;
110
+ }
111
+
112
+ /** The whole-install verdict across every command the installer maintains. */
113
+ export interface ShadowScanResult {
114
+ readonly reports: readonly CommandShadowReport[];
115
+ /** Commands whose maintained copy loses to an earlier PATH entry. */
116
+ readonly shadowed: readonly CommandShadowReport[];
117
+ /** True when any command is shadowed or the install directory is missing from PATH. */
118
+ readonly hasProblem: boolean;
119
+ }
120
+
121
+ export interface ShadowScanInput {
122
+ /** Command names the installer/updater maintains, e.g. `['goodvibes', 'goodvibes-daemon', 'goodvibes-agent']`. */
123
+ readonly commands: readonly string[];
124
+ /** The directory the installer writes to and the auto-updater swaps in place. */
125
+ readonly installDir: string;
126
+ /** PATH split into directories, in order, exactly as the shell would search them. */
127
+ readonly pathEntries: readonly string[];
128
+ /** The user's home directory. Nothing outside it is ever a removal candidate. */
129
+ readonly homeDir: string;
130
+ /** True when `path` exists and is a runnable file. */
131
+ readonly isExecutableFile: (path: string) => boolean;
132
+ /** Resolves a symlink chain; returns `path` unchanged when it is not a link or cannot be resolved. */
133
+ readonly realPath: (path: string) => string;
134
+ /**
135
+ * Runs `<path> --version` and returns its first line, or undefined when it
136
+ * cannot be run. Optional: without it, `our-binary` can never be
137
+ * established and such copies stay `unknown` (reported, never removed).
138
+ */
139
+ readonly probeVersion?: ((path: string) => string | undefined) | undefined;
140
+ }
141
+
142
+ // ---------------------------------------------------------------------------
143
+ // PATH handling
144
+ // ---------------------------------------------------------------------------
145
+
146
+ /**
147
+ * Splits a raw PATH string into directories in search order.
148
+ *
149
+ * Empty entries (from a leading, trailing, or doubled separator) mean "the
150
+ * current directory" to a POSIX shell. They are dropped rather than resolved:
151
+ * a cwd-relative hit is not a stable install anyone can reason about, and
152
+ * treating it as one would produce a different verdict per directory.
153
+ * Duplicated directories collapse to their first occurrence, which is the
154
+ * only position that can ever win.
155
+ */
156
+ export function splitPathEntries(rawPath: string | undefined, separator = ':'): string[] {
157
+ if (!rawPath) return [];
158
+ const seen = new Set<string>();
159
+ const entries: string[] = [];
160
+ for (const raw of rawPath.split(separator)) {
161
+ const entry = raw.trim();
162
+ if (entry.length === 0) continue;
163
+ const normalized = stripTrailingSeparators(entry);
164
+ if (normalized.length === 0) continue;
165
+ if (seen.has(normalized)) continue;
166
+ seen.add(normalized);
167
+ entries.push(normalized);
168
+ }
169
+ return entries;
170
+ }
171
+
172
+ /** Drops trailing `/` (or `\`) so `~/.local/bin` and `~/.local/bin/` are one directory. */
173
+ function stripTrailingSeparators(value: string): string {
174
+ let end = value.length;
175
+ while (end > 1 && (value[end - 1] === '/' || value[end - 1] === '\\')) end -= 1;
176
+ return value.slice(0, end);
177
+ }
178
+
179
+ function joinPath(directory: string, name: string): string {
180
+ return `${stripTrailingSeparators(directory)}/${name}`;
181
+ }
182
+
183
+ function segmentsOf(path: string): string[] {
184
+ return path.split(/[\\/]/).filter((segment) => segment.length > 0);
185
+ }
186
+
187
+ /** True when `path` is `root` itself or lives underneath it. */
188
+ export function isWithinDirectory(path: string, root: string): boolean {
189
+ const normalizedRoot = stripTrailingSeparators(root);
190
+ if (normalizedRoot.length === 0) return false;
191
+ const normalizedPath = stripTrailingSeparators(path);
192
+ if (normalizedPath === normalizedRoot) return true;
193
+ return normalizedPath.startsWith(`${normalizedRoot}/`);
194
+ }
195
+
196
+ // ---------------------------------------------------------------------------
197
+ // Ownership classification
198
+ // ---------------------------------------------------------------------------
199
+
200
+ /** The npm scope every package this platform publishes lives under. */
201
+ const PACKAGE_SCOPE = '@pellux';
202
+ /** Only `@pellux/goodvibes-*` packages are ours; a same-scope stranger is not assumed. */
203
+ const PACKAGE_PREFIX = 'goodvibes-';
204
+
205
+ /**
206
+ * Extracts the owning `@pellux/goodvibes-*` package name from a resolved
207
+ * path, by finding the LAST `node_modules` segment and reading the scoped
208
+ * package directory that follows it. The last occurrence is the one that
209
+ * owns the file: a nested `node_modules/a/node_modules/b/bin/x` belongs to
210
+ * `b`, not `a`.
211
+ */
212
+ export function owningPackageName(resolvedPath: string): string | undefined {
213
+ const segments = segmentsOf(resolvedPath);
214
+ for (let i = segments.length - 1; i >= 0; i -= 1) {
215
+ if (segments[i] !== 'node_modules') continue;
216
+ const scope = segments[i + 1];
217
+ const name = segments[i + 2];
218
+ if (scope !== PACKAGE_SCOPE || name === undefined) return undefined;
219
+ if (!name.startsWith(PACKAGE_PREFIX)) return undefined;
220
+ return `${scope}/${name}`;
221
+ }
222
+ return undefined;
223
+ }
224
+
225
+ /**
226
+ * Picks the uninstall command for a package link, from where the package
227
+ * actually lives. A bun global install lives under `.bun/install/global`;
228
+ * everything else is treated as npm-managed, which is also the correct
229
+ * advice for a `bun add -g` that used an npm-style prefix.
230
+ */
231
+ function packageRemoval(packageName: string, resolvedPath: string): ShadowRemoval {
232
+ const isBunGlobal = /[\\/]\.bun[\\/]install[\\/]global[\\/]/.test(resolvedPath)
233
+ || segmentsOf(resolvedPath).includes('.bun');
234
+ return {
235
+ kind: 'package',
236
+ packageName,
237
+ command: isBunGlobal ? `bun remove -g ${packageName}` : `npm rm -g ${packageName}`,
238
+ };
239
+ }
240
+
241
+ /**
242
+ * True when a `--version` line is one of ours: `<command> <dotted numbers>`,
243
+ * which is the exact shape every goodvibes command prints. Anything else —
244
+ * an unrelated program that happens to share the name, a wrapper script, a
245
+ * `--version` that errors — fails, and the copy stays `unknown`.
246
+ */
247
+ export function versionLineIdentifiesCommand(line: string | undefined, command: string): string | undefined {
248
+ if (!line) return undefined;
249
+ const match = /^([A-Za-z0-9._-]+)[ \t]+v?(\d+(?:\.\d+)*(?:[-+][0-9A-Za-z.-]+)?)\s*$/.exec(line.trim());
250
+ if (!match) return undefined;
251
+ if (match[1] !== command) return undefined;
252
+ return match[2];
253
+ }
254
+
255
+ // ---------------------------------------------------------------------------
256
+ // The scan
257
+ // ---------------------------------------------------------------------------
258
+
259
+ function classify(input: ShadowScanInput, copy: {
260
+ readonly command: string;
261
+ readonly directory: string;
262
+ readonly path: string;
263
+ readonly pathIndex: number;
264
+ readonly resolvedPath: string;
265
+ }): { ownership: ShadowOwnership; removal?: ShadowRemoval | undefined; version?: string | undefined } {
266
+ if (isWithinDirectory(copy.path, input.installDir)) {
267
+ return { ownership: 'install-target', version: probe(input, copy.path, copy.command) };
268
+ }
269
+
270
+ const version = probe(input, copy.path, copy.command);
271
+
272
+ // Nothing outside the user's own home is ever a removal candidate, however
273
+ // confidently we recognise it: a system-wide copy is somebody else's to
274
+ // manage, and this code must never reach outside the user's directories.
275
+ const insideHome = isWithinDirectory(copy.path, input.homeDir)
276
+ && isWithinDirectory(copy.resolvedPath, input.homeDir);
277
+
278
+ const packageName = owningPackageName(copy.resolvedPath);
279
+ if (packageName && insideHome) {
280
+ return { ownership: 'package-link', removal: packageRemoval(packageName, copy.resolvedPath), version };
281
+ }
282
+ if (packageName) {
283
+ return { ownership: 'unknown', version };
284
+ }
285
+ if (version !== undefined && insideHome) {
286
+ return { ownership: 'our-binary', removal: { kind: 'file', command: `rm ${copy.path}` }, version };
287
+ }
288
+ return { ownership: 'unknown', version };
289
+ }
290
+
291
+ function probe(input: ShadowScanInput, path: string, command: string): string | undefined {
292
+ if (!input.probeVersion) return undefined;
293
+ try {
294
+ return versionLineIdentifiesCommand(input.probeVersion(path), command);
295
+ } catch {
296
+ return undefined;
297
+ }
298
+ }
299
+
300
+ function resolve(input: ShadowScanInput, path: string): string {
301
+ try {
302
+ return input.realPath(path);
303
+ } catch {
304
+ return path;
305
+ }
306
+ }
307
+
308
+ function exists(input: ShadowScanInput, path: string): boolean {
309
+ try {
310
+ return input.isExecutableFile(path);
311
+ } catch {
312
+ return false;
313
+ }
314
+ }
315
+
316
+ /** Scans PATH for every copy of every maintained command and decides which one wins. */
317
+ export function scanCommandShadows(input: ShadowScanInput): ShadowScanResult {
318
+ const entries = input.pathEntries.map(stripTrailingSeparators).filter((entry) => entry.length > 0);
319
+ const installDir = stripTrailingSeparators(input.installDir);
320
+ const installDirOnPath = entries.some((entry) => entry === installDir);
321
+
322
+ const reports: CommandShadowReport[] = [];
323
+ for (const command of input.commands) {
324
+ const copies: CommandCopy[] = [];
325
+ for (let index = 0; index < entries.length; index += 1) {
326
+ const directory = entries[index] as string;
327
+ const path = joinPath(directory, command);
328
+ if (!exists(input, path)) continue;
329
+ const resolvedPath = resolve(input, path);
330
+ const partial = { command, directory, path, pathIndex: index, resolvedPath };
331
+ copies.push({ ...partial, ...classify(input, partial) });
332
+ }
333
+
334
+ const installedOnPath = copies.find((copy) => copy.ownership === 'install-target');
335
+ // The install directory may hold the command while being absent from
336
+ // PATH; that install is just as unreachable as a shadowed one.
337
+ const installedOffPath = !installDirOnPath && exists(input, joinPath(installDir, command));
338
+
339
+ const shadowing = installedOnPath
340
+ ? copies.filter((copy) => copy.pathIndex < installedOnPath.pathIndex)
341
+ : [];
342
+
343
+ reports.push({
344
+ command,
345
+ copies,
346
+ winner: copies[0],
347
+ installed: installedOnPath,
348
+ shadowing,
349
+ installDirNotOnPath: installedOffPath,
350
+ });
351
+ }
352
+
353
+ const shadowed = reports.filter((report) => report.shadowing.length > 0 || report.installDirNotOnPath);
354
+ return { reports, shadowed, hasProblem: shadowed.length > 0 };
355
+ }
356
+
357
+ // ---------------------------------------------------------------------------
358
+ // Plain-words reporting
359
+ // ---------------------------------------------------------------------------
360
+
361
+ function describeVersion(copy: CommandCopy): string {
362
+ return copy.version ? `version ${copy.version}` : 'version unknown';
363
+ }
364
+
365
+ /**
366
+ * Renders one shadowed command as plain lines: which path wins, what version
367
+ * each copy is, and the exact command that fixes it. No jargon, no severity
368
+ * words — the user needs to know which file answers when they type the name.
369
+ */
370
+ export function describeShadowReport(report: CommandShadowReport): string[] {
371
+ const lines: string[] = [];
372
+ if (report.installDirNotOnPath) {
373
+ lines.push(
374
+ `${report.command} is installed but its directory is not on your PATH, so typing "${report.command}" does not reach it.`,
375
+ );
376
+ return lines;
377
+ }
378
+ const winner = report.winner;
379
+ const installed = report.installed;
380
+ if (!winner || !installed) return lines;
381
+
382
+ lines.push(
383
+ `Typing "${report.command}" runs ${winner.path} (${describeVersion(winner)}), `
384
+ + `not the copy this installer maintains at ${installed.path} (${describeVersion(installed)}).`,
385
+ );
386
+ for (const copy of report.shadowing) {
387
+ if (copy.removal) {
388
+ lines.push(` ${copy.path} is a copy of our own program. Remove it with: ${copy.removal.command}`);
389
+ } else {
390
+ lines.push(
391
+ ` ${copy.path} is not something we can identify as ours, so it will not be touched. `
392
+ + `Remove or reorder it yourself, or put ${installed.directory} earlier on your PATH.`,
393
+ );
394
+ }
395
+ }
396
+ return lines;
397
+ }
398
+
399
+ /** Renders every problem in a scan, in command order. Empty when the scan is clean. */
400
+ export function describeShadowScan(result: ShadowScanResult): string[] {
401
+ return result.shadowed.flatMap((report) => describeShadowReport(report));
402
+ }
403
+
404
+ /**
405
+ * The copies a caller may offer to remove: recognised copies of our own
406
+ * program that are shadowing the maintained install. Deduplicated by path,
407
+ * since one leftover package install typically provides several commands.
408
+ */
409
+ export function removableShadows(result: ShadowScanResult): CommandCopy[] {
410
+ const byPath = new Map<string, CommandCopy>();
411
+ for (const report of result.shadowed) {
412
+ for (const copy of report.shadowing) {
413
+ if (!copy.removal) continue;
414
+ if (!byPath.has(copy.path)) byPath.set(copy.path, copy);
415
+ }
416
+ }
417
+ return [...byPath.values()];
418
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * reachability-notice.ts — the two ways a running build can be the wrong one,
3
+ * stated in plain words at startup.
4
+ *
5
+ * The platform's guarantee is that clients auto-update at startup, the daemon
6
+ * checks hourly, and nothing ever drifts. Two things break that guarantee
7
+ * without breaking anything visible:
8
+ *
9
+ * 1. Another copy of this command sits EARLIER on PATH. The updater keeps
10
+ * maintaining the copy it installed; the shell keeps running the other
11
+ * one. Both installs "succeed", the version number reports itself
12
+ * current, and an older build answers every question.
13
+ * 2. This build is simply behind the latest release and, for whatever
14
+ * reason, did not update itself — a package-managed install, a failed
15
+ * swap, a check that could not reach the network.
16
+ *
17
+ * Either way the user finds out by being told a capability does not exist. A
18
+ * build that knows it is not the current one must say so before anything
19
+ * else, so this module turns those two facts into lines and the wiring in
20
+ * path-shadow-startup.ts prints them ahead of the session.
21
+ *
22
+ * Pure by construction: it is handed a completed scan and two version strings
23
+ * and decides only what to say.
24
+ */
25
+
26
+ import { compareVersions, normalizeVersion } from './update-check.ts';
27
+ import { describeShadowReport, type ShadowScanResult } from './path-shadow.ts';
28
+
29
+ export type ReachabilityNoticeKind = 'shadowed' | 'not-on-path' | 'behind';
30
+
31
+ export interface ReachabilityNotice {
32
+ readonly kind: ReachabilityNoticeKind;
33
+ /** Plain lines, already in the order they should be read. */
34
+ readonly lines: readonly string[];
35
+ }
36
+
37
+ export interface ReachabilityNoticeInput {
38
+ /** The completed PATH scan for this command. */
39
+ readonly scan: ShadowScanResult;
40
+ /** The version this process is running. */
41
+ readonly runningVersion: string;
42
+ /** The newest released version, or undefined when it could not be determined. */
43
+ readonly latestVersion?: string | undefined;
44
+ /** The exact command that brings this install up to date. */
45
+ readonly updateCommand: string;
46
+ }
47
+
48
+ /**
49
+ * Builds the notices for this startup. Returns an empty list for the healthy
50
+ * case — one reachable copy, running the latest release — so a normal start
51
+ * says nothing at all.
52
+ */
53
+ export function buildReachabilityNotices(input: ReachabilityNoticeInput): ReachabilityNotice[] {
54
+ const notices: ReachabilityNotice[] = [];
55
+
56
+ for (const report of input.scan.shadowed) {
57
+ const lines = describeShadowReport(report);
58
+ if (lines.length === 0) continue;
59
+ notices.push({
60
+ kind: report.installDirNotOnPath ? 'not-on-path' : 'shadowed',
61
+ lines: [
62
+ ...lines,
63
+ report.installDirNotOnPath
64
+ ? 'Until then, run it by its full path.'
65
+ : 'Until then, updating this install changes nothing you can reach by name.',
66
+ ],
67
+ });
68
+ }
69
+
70
+ const behind = describeVersionGap(input);
71
+ if (behind) notices.push(behind);
72
+
73
+ return notices;
74
+ }
75
+
76
+ /**
77
+ * The staleness line. Deliberately silent when the latest version is unknown:
78
+ * an unreachable network is not evidence of being behind, and saying so would
79
+ * be the same kind of false certainty this module exists to remove.
80
+ */
81
+ function describeVersionGap(input: ReachabilityNoticeInput): ReachabilityNotice | undefined {
82
+ if (!input.latestVersion) return undefined;
83
+ const running = normalizeVersion(input.runningVersion);
84
+ const latest = normalizeVersion(input.latestVersion);
85
+ if (running.length === 0 || latest.length === 0) return undefined;
86
+ if (compareVersions(running, latest) >= 0) return undefined;
87
+ return {
88
+ kind: 'behind',
89
+ lines: [
90
+ `This build is v${running}. The current release is v${latest}, so what you are running is behind.`,
91
+ `Anything added since v${running} is genuinely absent from this build — if it says it cannot do something, that may be why.`,
92
+ `Update with: ${input.updateCommand}`,
93
+ ],
94
+ };
95
+ }
96
+
97
+ /** One flat list of lines, for a surface that prints rather than groups. */
98
+ export function reachabilityNoticeLines(notices: readonly ReachabilityNotice[]): string[] {
99
+ return notices.flatMap((notice) => [...notice.lines]);
100
+ }