@hop-top/kit 0.5.0-alpha.10 → 0.5.0-alpha.12

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 (68) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +4 -4
  3. package/dist/alias.js +3 -3
  4. package/dist/{chunk-T46YPHKN.js → chunk-4FMALWYB.js} +45 -45
  5. package/dist/{chunk-JZ22ZUFA.js → chunk-65VPWMPV.js} +4 -1
  6. package/dist/{chunk-YGYQUFGY.js → chunk-BXJ2J27B.js} +10 -10
  7. package/dist/chunk-CXBZTONY.js +65 -0
  8. package/dist/{chunk-K7VCKJ27.js → chunk-EE4M2UOF.js} +8 -4
  9. package/dist/{chunk-72XZWQX7.js → chunk-GFRAYYRD.js} +475 -162
  10. package/dist/{chunk-2JV3A7OP.js → chunk-IVQHLI56.js} +6 -10
  11. package/dist/{chunk-NLMS7HGP.js → chunk-IZ6Q4SE3.js} +3 -3
  12. package/dist/chunk-MFVUENZR.js +1549 -0
  13. package/dist/{chunk-BLS2OBIQ.js → chunk-ML7D5DWY.js} +2 -2
  14. package/dist/chunk-P3Y46PYF.js +138 -0
  15. package/dist/{chunk-GGWJRWWA.js → chunk-RYKLVOVK.js} +7 -7
  16. package/dist/{chunk-QPUCXQXT.js → chunk-T6OZMPPV.js} +2 -1
  17. package/dist/{chunk-MUIQVU6E.js → chunk-VGOA5Y7S.js} +11 -11
  18. package/dist/{chunk-ABQSE7AI.js → chunk-VWGP4NVJ.js} +38 -38
  19. package/dist/chunk-X4E5QGEB.js +171 -0
  20. package/dist/cli-RSRk1M36.d.ts +707 -0
  21. package/dist/cli.d.ts +1 -1
  22. package/dist/cli.js +60 -5
  23. package/dist/completion.js +2 -2
  24. package/dist/id/index.d.ts +1 -1
  25. package/dist/id/index.js +4 -2
  26. package/dist/{index-DCe1boBz.d.ts → index-CGE5yLBj.d.ts} +1 -1
  27. package/dist/{index-VLDcI5NF.d.ts → index-D9zpUrpE.d.ts} +18 -6
  28. package/dist/index.d.ts +4 -4
  29. package/dist/index.js +23 -23
  30. package/dist/llm-anthropic.js +5 -5
  31. package/dist/llm-openai.js +5 -5
  32. package/dist/llm.js +3 -3
  33. package/dist/log.js +3 -3
  34. package/dist/mcp/index.js +3 -2
  35. package/dist/output.js +2 -4
  36. package/dist/routellm.js +6 -6
  37. package/dist/toolspec.js +4 -4
  38. package/dist/tui/index.d.ts +2 -2
  39. package/dist/tui/index.js +3 -3
  40. package/dist/{upgrade-p_zh3pvV.d.ts → upgrade-BJ2z6Oqf.d.ts} +58 -4
  41. package/dist/upgrade.d.ts +1 -1
  42. package/dist/upgrade.js +4 -2
  43. package/package.json +9 -9
  44. package/src/cli.root.conformance.test.ts +166 -0
  45. package/src/cli.ts +39 -0
  46. package/src/completion.conformance.test.ts +3 -2
  47. package/src/completion.test.ts +12 -0
  48. package/src/completion.ts +8 -3
  49. package/src/delegation.test.ts +299 -0
  50. package/src/delegation.ts +807 -0
  51. package/src/id/README.md +2 -2
  52. package/src/id/contract.test.ts +11 -8
  53. package/src/id/differential.test.ts +54 -0
  54. package/src/id/index.ts +123 -17
  55. package/src/mcp/bridge.test.ts +28 -0
  56. package/src/mcp/bridge.ts +5 -10
  57. package/src/plan.ts +89 -0
  58. package/src/policy.ts +472 -0
  59. package/src/sideeffect.ts +105 -0
  60. package/src/toolspec-manifest.ts +7 -2
  61. package/src/upgrade.test.ts +393 -1
  62. package/src/upgrade.ts +183 -25
  63. package/dist/chunk-GHPJJV5L.js +0 -613
  64. package/dist/chunk-IXVQ62NZ.js +0 -334
  65. package/dist/chunk-L23O2FFE.js +0 -51
  66. package/dist/chunk-WWUKGJB5.js +0 -90
  67. package/dist/cli-C5B5tnpA.d.ts +0 -400
  68. package/dist/{chunk-PSFYJGDG.js → chunk-K7E34Q3A.js} +2 -2
package/src/upgrade.ts CHANGED
@@ -17,14 +17,25 @@
17
17
  * ### Error handling philosophy
18
18
  * Version checking is a best-effort, non-critical operation. Any failure —
19
19
  * network error, bad JSON, HTTP error, filesystem error, invalid semver — is
20
- * silently discarded. The function always resolves without throwing. If
21
- * something goes wrong the user sees neither a notice nor an error.
20
+ * silently discarded by `notifyIfAvailable`, which always resolves without
21
+ * throwing. `check()` is the strict variant: it rejects with an
22
+ * {@link UpgradeCheckError} so callers (e.g. `upgrade --check`) can tell a
23
+ * failed lookup from "up to date".
22
24
  *
23
25
  * ### Notice format
24
26
  * ```
25
27
  * \nA new release of {name} is available: v{current} → v{latest}\nhttps://github.com/{owner}/{repo}/releases/latest\n
26
28
  * ```
27
29
  *
30
+ * ### Component tags and prereleases
31
+ * By default the check reads `releases/latest` and strips a leading `v`.
32
+ * Set `tagPrefix` (e.g. `xray/v`) for a monorepo whose tags carry a
33
+ * component: releases are listed, tags without the prefix are ignored, and
34
+ * the notice links to the matching release. Set `includePrereleases` to
35
+ * consider prereleases; without it, prereleases are never offered. Both
36
+ * options list at most 3 pages of 100 releases, newest first, drafts
37
+ * skipped; the highest semver wins.
38
+ *
28
39
  * ### Usage example
29
40
  * ```ts
30
41
  * import { createChecker } from '@hop-top/kit/upgrade';
@@ -75,6 +86,40 @@ export interface CheckerOptions {
75
86
  * @defaultValue 10_000
76
87
  */
77
88
  timeout?: number;
89
+ /**
90
+ * Tag prefix identifying this tool's releases, stripped before the semver
91
+ * comparison (e.g. `'xray/v'` for tags like `xray/v0.2.0`). Tags without
92
+ * the prefix are not candidates. Setting it lists releases instead of
93
+ * reading `releases/latest`, which may belong to another component.
94
+ * @defaultValue `''` (no prefix; `releases/latest`)
95
+ */
96
+ tagPrefix?: string;
97
+ /**
98
+ * Offer prereleases (e.g. `0.1.0-rc.1`) as upgrades. Setting it lists
99
+ * releases instead of reading `releases/latest`, which skips prereleases.
100
+ * @defaultValue false
101
+ */
102
+ includePrereleases?: boolean;
103
+ }
104
+
105
+ /** Outcome of {@link Checker.check}. */
106
+ export interface UpgradeCheckResult {
107
+ /** Running version, as configured. */
108
+ current: string;
109
+ /** Latest release version (no leading `v` / tag prefix). */
110
+ latest: string;
111
+ /** True when `latest` is strictly greater than `current`. */
112
+ updateAvailable: boolean;
113
+ /** Release page to link to. */
114
+ url: string;
115
+ }
116
+
117
+ /** Rejection of {@link Checker.check}; the underlying error is in `cause`. */
118
+ export class UpgradeCheckError extends Error {
119
+ constructor(message: string, options?: { cause?: unknown }) {
120
+ super(message, options);
121
+ this.name = 'UpgradeCheckError';
122
+ }
78
123
  }
79
124
 
80
125
  /**
@@ -82,6 +127,16 @@ export interface CheckerOptions {
82
127
  * at the end of a CLI command to surface upgrade notices non-intrusively.
83
128
  */
84
129
  export interface Checker {
130
+ /**
131
+ * Looks up the latest release and resolves with the outcome. Unlike
132
+ * {@link Checker.notifyIfAvailable} it never swallows failures: network,
133
+ * HTTP, parse and "no matching release" errors reject with an
134
+ * {@link UpgradeCheckError}, so callers can tell a failed lookup from
135
+ * "up to date". Honours `tagPrefix`, `includePrereleases` and the cache;
136
+ * pass `cacheTTL: 0` to bypass the cache.
137
+ */
138
+ check(): Promise<UpgradeCheckResult>;
139
+
85
140
  /**
86
141
  * Checks for a newer GitHub release and writes a one-line notice to `out`
87
142
  * if one is available. Silently swallows all errors.
@@ -96,8 +151,21 @@ export interface Checker {
96
151
  interface CacheEntry {
97
152
  version: string;
98
153
  checkedAt: number;
154
+ /** Tag of the cached release; written only when releases are listed. */
155
+ tag?: string;
156
+ }
157
+
158
+ interface GitHubRelease {
159
+ tag_name?: unknown;
160
+ draft?: unknown;
161
+ prerelease?: unknown;
99
162
  }
100
163
 
164
+ /** Releases requested per page when listing (GitHub's maximum). */
165
+ const PAGE_SIZE = 100;
166
+ /** Pages fetched at most when listing: bounds the check to 3 requests. */
167
+ const MAX_PAGES = 3;
168
+
101
169
  // ─── Implementation ───────────────────────────────────────────────────────────
102
170
 
103
171
  /**
@@ -118,19 +186,45 @@ export function createChecker(opts: CheckerOptions): Checker {
118
186
  cacheTTL = 24 * 60 * 60 * 1000,
119
187
  stateDir = os.tmpdir(),
120
188
  timeout = 10_000,
189
+ tagPrefix = '',
190
+ includePrereleases = false,
121
191
  } = opts;
122
192
 
193
+ // Default mode (no prefix, no prereleases) keeps the original
194
+ // `releases/latest` lookup untouched.
195
+ const listMode = tagPrefix !== '' || includePrereleases;
196
+
123
197
  const cacheFile = path.join(stateDir, `.upgrade-${name}-cache.json`);
124
198
  const releaseURL = `https://api.github.com/repos/${owner}/${repo}/releases/latest`;
199
+ const listURL = `https://api.github.com/repos/${owner}/${repo}/releases`;
125
200
  const releasesPageURL = `https://github.com/${owner}/${repo}/releases/latest`;
126
201
 
202
+ // `releases/latest` can be another component's release or 404 while only
203
+ // prereleases exist, so in list mode link the exact release, else the list.
204
+ function noticeURL(tag: string | undefined): string {
205
+ if (!listMode) return releasesPageURL;
206
+ const base = `https://github.com/${owner}/${repo}/releases`;
207
+ if (tag === undefined) return base;
208
+ return `${base}/tag/${tag.split('/').map(encodeURIComponent).join('/')}`;
209
+ }
210
+
127
211
  // ── cache helpers ──────────────────────────────────────────────────────────
128
212
 
213
+ function tagNamesVersion(tag: unknown, version: string): boolean {
214
+ return typeof tag === 'string' && tag.startsWith(tagPrefix) &&
215
+ semver.valid(tag.slice(tagPrefix.length).replace(/^v/, '')) === version;
216
+ }
217
+
129
218
  function readCache(): CacheEntry | null {
130
219
  try {
131
220
  const raw = fs.readFileSync(cacheFile, 'utf8');
132
221
  const parsed = JSON.parse(raw) as CacheEntry;
133
222
  if (typeof parsed.version === 'string' && typeof parsed.checkedAt === 'number') {
223
+ // The tag becomes part of a link, and the cache file may sit in a
224
+ // shared directory: trust it only if it names this cached version.
225
+ if (listMode && !tagNamesVersion(parsed.tag, parsed.version)) {
226
+ return null;
227
+ }
134
228
  return parsed;
135
229
  }
136
230
  } catch {
@@ -139,9 +233,10 @@ export function createChecker(opts: CheckerOptions): Checker {
139
233
  return null;
140
234
  }
141
235
 
142
- function writeCache(version: string): void {
236
+ function writeCache(version: string, tag?: string): void {
143
237
  try {
144
238
  const entry: CacheEntry = { version, checkedAt: Date.now() };
239
+ if (tag !== undefined) entry.tag = tag;
145
240
  fs.writeFileSync(cacheFile, JSON.stringify(entry), 'utf8');
146
241
  } catch {
147
242
  // Cache write failure is non-fatal.
@@ -150,56 +245,119 @@ export function createChecker(opts: CheckerOptions): Checker {
150
245
 
151
246
  // ── fetch latest version ───────────────────────────────────────────────────
152
247
 
153
- async function fetchLatestVersion(): Promise<string | null> {
248
+ interface Latest {
249
+ version: string;
250
+ tag?: string;
251
+ }
252
+
253
+ async function fetchLatestVersion(): Promise<Latest> {
154
254
  const controller = new AbortController();
155
255
  const timer = setTimeout(() => controller.abort(), timeout);
156
256
  try {
257
+ if (listMode) return await fetchHighestRelease(controller.signal);
157
258
  const res = await fetch(releaseURL, {
158
259
  signal: controller.signal,
159
260
  headers: { Accept: 'application/vnd.github+json' },
160
261
  });
161
- if (!res.ok) return null;
262
+ if (!res.ok) throw new UpgradeCheckError(`release lookup failed: HTTP ${res.status}`);
162
263
  const body = (await res.json()) as { tag_name?: string };
163
264
  const tag = body.tag_name;
164
- if (typeof tag !== 'string') return null;
265
+ if (typeof tag !== 'string') throw new UpgradeCheckError('release lookup failed: no tag_name');
165
266
  // Strip leading 'v' so semver.gt works with or without prefix.
166
- return tag.replace(/^v/, '');
267
+ return { version: tag.replace(/^v/, '') };
167
268
  } finally {
168
269
  clearTimeout(timer);
169
270
  }
170
271
  }
171
272
 
172
- // ── notifyIfAvailable ──────────────────────────────────────────────────────
273
+ /**
274
+ * Lists releases (newest first, bounded) and returns the highest semver
275
+ * among non-draft releases whose tag starts with `tagPrefix`; prereleases
276
+ * only when `includePrereleases` is set.
277
+ */
278
+ async function fetchHighestRelease(signal: AbortSignal): Promise<Latest> {
279
+ let best: Latest | null = null;
280
+ for (let page = 1; page <= MAX_PAGES; page++) {
281
+ const res = await fetch(`${listURL}?per_page=${PAGE_SIZE}&page=${page}`, {
282
+ signal,
283
+ headers: { Accept: 'application/vnd.github+json' },
284
+ });
285
+ if (!res.ok) {
286
+ // Page 1 failing is a failed check; a later page keeps what we have.
287
+ if (page === 1) throw new UpgradeCheckError(`release lookup failed: HTTP ${res.status}`);
288
+ break;
289
+ }
290
+ const body = (await res.json()) as unknown;
291
+ if (!Array.isArray(body)) {
292
+ if (page === 1) throw new UpgradeCheckError('release lookup failed: unexpected response');
293
+ break;
294
+ }
295
+ for (const rel of body as GitHubRelease[]) {
296
+ if (rel === null || typeof rel !== 'object') continue;
297
+ if (rel.draft === true || typeof rel.tag_name !== 'string') continue;
298
+ const tag = rel.tag_name;
299
+ if (!tag.startsWith(tagPrefix)) continue;
300
+ const version = semver.valid(tag.slice(tagPrefix.length).replace(/^v/, ''));
301
+ if (version === null) continue;
302
+ if (!includePrereleases && (rel.prerelease === true || semver.prerelease(version) !== null)) {
303
+ continue;
304
+ }
305
+ if (best === null || semver.gt(version, best.version)) best = { version, tag };
306
+ }
307
+ if (body.length < PAGE_SIZE) break;
308
+ }
309
+ if (best === null) throw new UpgradeCheckError('no matching release found');
310
+ return best;
311
+ }
173
312
 
174
- async function notifyIfAvailable(out: NodeJS.WritableStream): Promise<void> {
313
+ // ── check ──────────────────────────────────────────────────────────────────
314
+
315
+ async function check(): Promise<UpgradeCheckResult> {
175
316
  try {
176
- let latestVersion: string | null = null;
317
+ let latestVersion: string;
318
+ let latestTag: string | undefined;
177
319
 
178
- // Check cache first.
179
320
  const cached = readCache();
180
321
  if (cached !== null && Date.now() - cached.checkedAt < cacheTTL) {
181
322
  latestVersion = cached.version;
323
+ latestTag = cached.tag;
182
324
  } else {
183
- latestVersion = await fetchLatestVersion();
184
- if (latestVersion !== null) {
185
- writeCache(latestVersion);
186
- }
325
+ const latest = await fetchLatestVersion();
326
+ latestVersion = latest.version;
327
+ latestTag = latest.tag;
328
+ writeCache(latest.version, latest.tag);
187
329
  }
188
330
 
189
- if (latestVersion === null) return;
190
-
191
- // Only notify when latest is strictly greater than current.
192
- if (!semver.gt(latestVersion, currentVersion)) return;
331
+ return {
332
+ current: currentVersion,
333
+ latest: latestVersion,
334
+ // Only strictly greater counts as an update.
335
+ updateAvailable: semver.gt(latestVersion, currentVersion),
336
+ url: noticeURL(latestTag),
337
+ };
338
+ } catch (err) {
339
+ if (err instanceof UpgradeCheckError) throw err;
340
+ throw new UpgradeCheckError(
341
+ `upgrade check failed: ${err instanceof Error ? err.message : String(err)}`,
342
+ { cause: err },
343
+ );
344
+ }
345
+ }
193
346
 
194
- const notice =
195
- `\nA new release of ${name} is available: v${currentVersion} → v${latestVersion}\n` +
196
- `${releasesPageURL}\n`;
347
+ // ── notifyIfAvailable ──────────────────────────────────────────────────────
197
348
 
198
- out.write(notice);
349
+ async function notifyIfAvailable(out: NodeJS.WritableStream): Promise<void> {
350
+ try {
351
+ const r = await check();
352
+ if (!r.updateAvailable) return;
353
+ out.write(
354
+ `\nA new release of ${name} is available: v${r.current} → v${r.latest}\n` +
355
+ `${r.url}\n`,
356
+ );
199
357
  } catch {
200
- // Any unexpected error is silently swallowed.
358
+ // Best-effort: any failure is silently swallowed.
201
359
  }
202
360
  }
203
361
 
204
- return { notifyIfAvailable };
362
+ return { check, notifyIfAvailable };
205
363
  }