bantamkit-mcp 0.35.1 → 0.35.2
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/dist/mcp/status.js +14 -0
- package/dist/selfupdate.d.ts +21 -12
- package/dist/selfupdate.js +74 -1
- package/dist/updatecheck.d.ts +118 -0
- package/dist/updatecheck.js +278 -0
- package/package.json +1 -1
package/dist/mcp/status.js
CHANGED
|
@@ -34,6 +34,7 @@ import { join } from 'node:path';
|
|
|
34
34
|
import { AssetNotFound, assetsRoot } from '../assets.js';
|
|
35
35
|
import { Memory } from '../memory/component.js';
|
|
36
36
|
import { INDEX_PRESSURE_PERCENT } from '../memory/store.js';
|
|
37
|
+
import { updateLine } from '../updatecheck.js';
|
|
37
38
|
import { Undetermined, currentInstall, originStat } from './identity.js';
|
|
38
39
|
/**
|
|
39
40
|
* The tool AND the prompt answer to this one name. Deliberately the same word: the operator
|
|
@@ -423,6 +424,19 @@ export function statusReport(memory, log, conditions, identity, tools, prompts,
|
|
|
423
424
|
(size === null ? 'unreadable' : String(size)) +
|
|
424
425
|
` of ${budget} bytes`,
|
|
425
426
|
`event log: ${log.enabled ? 'on' : 'off'}`,
|
|
427
|
+
// THE UPDATE LINE IS NOT A CONDITION, and that is the ruling of 2026-09-19 rather than an
|
|
428
|
+
// omission. A newer version existing is not a fault: the server is serving correctly, and
|
|
429
|
+
// this file's own header carries the operator's reason — a footer on every result is
|
|
430
|
+
// noise, noise trains the reader to stop reading. So this never flips line 1, never enters
|
|
431
|
+
// `degradedConditions`, and never rides another tool's reply. It also means the record is
|
|
432
|
+
// opened ONLY here, when someone asked for a report, and not on the per-call path whose
|
|
433
|
+
// cost `docs/status.md` documents.
|
|
434
|
+
//
|
|
435
|
+
// `updatecheck` opens one file and asks nobody anything. It is imported instead of
|
|
436
|
+
// `selfupdate.js` deliberately: `test/selfupdate.test.mjs` pins that `cli.ts` is the ONLY
|
|
437
|
+
// module in `src/` reaching the one `fetch` in this runtime, and this file is on a path a
|
|
438
|
+
// host reaches. The version is the one line 2 already printed.
|
|
439
|
+
updateLine(identity.get('version')),
|
|
426
440
|
];
|
|
427
441
|
if (conditions.length > 0) {
|
|
428
442
|
lines.push(`${plural(conditions.length, 'problem')}:`);
|
package/dist/selfupdate.d.ts
CHANGED
|
@@ -278,19 +278,28 @@ export interface KeptInstall {
|
|
|
278
278
|
*/
|
|
279
279
|
export declare function keptInstall(prefix?: string): KeptInstall | null;
|
|
280
280
|
/**
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
* THE
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
281
|
+
* Put the version this flag just fetched into the record `bantamkit_status` reads.
|
|
282
|
+
*
|
|
283
|
+
* THE PORT OF `selfupdate.record_update`. Its docstring carries the argument and is not
|
|
284
|
+
* restated: no second network call and no new failure mode (`update` already holds `latest`),
|
|
285
|
+
* no directory is ever created, the other runtime's key is left exactly as found, and the
|
|
286
|
+
* write goes through a temp file and a rename so no reader sees half a record.
|
|
287
|
+
*
|
|
288
|
+
* WHY THE WRITER IS HERE AND NOT IN `updatecheck.ts`. That module is the READER, and
|
|
289
|
+
* `test/updatecheck.test.mjs` gates its source against `writeFileSync`, `renameSync`,
|
|
290
|
+
* `mkdirSync` and the rest by name — the same claim `test_updatecheck.py` makes over the
|
|
291
|
+
* reference's AST. The split is structural on both sides, so a future reader cannot grow a
|
|
292
|
+
* write by accident. `updatecheck.js` is imported here for the PATH and the KEY, which is the
|
|
293
|
+
* direction that keeps one spelling of each.
|
|
294
|
+
*
|
|
295
|
+
* `renameSync` is atomic on POSIX and on Windows, and the temp file is made in the SAME
|
|
296
|
+
* directory so the rename is never across a filesystem.
|
|
297
|
+
*
|
|
298
|
+
* RETURNS whether it wrote, and THROWS FOR NOTHING. A failed write must not turn a successful
|
|
299
|
+
* `--update` into a failure: the operator's install was updated either way, and the worst case
|
|
300
|
+
* is a status line that still says `never checked`.
|
|
293
301
|
*/
|
|
302
|
+
export declare function recordUpdate(latest: string, now?: string): boolean;
|
|
294
303
|
export declare function update(installed: string, origin: Origin, options?: UpdateOptions): Promise<string>;
|
|
295
304
|
/**
|
|
296
305
|
* `--update` on the CLI: ask the package index, act on the answer, print what happened, exit.
|
package/dist/selfupdate.js
CHANGED
|
@@ -40,11 +40,12 @@
|
|
|
40
40
|
* `test/selfupdate.test.mjs` — and it calls `runUpdate` from the flag and returns before a
|
|
41
41
|
* memory store or a transport exists, the same shape `--assets-root` and `--install` use.
|
|
42
42
|
*/
|
|
43
|
-
import { readFileSync } from 'node:fs';
|
|
43
|
+
import { readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
44
44
|
import { basename, dirname, join, resolve } from 'node:path';
|
|
45
45
|
import { fileURLToPath } from 'node:url';
|
|
46
46
|
import { currentInstall, Undetermined } from './mcp/identity.js';
|
|
47
47
|
import { compareVersions, keptCli, keptManifest, keptPrefix, PACKAGE, runInstaller, shlexJoin } from './npminstall.js';
|
|
48
|
+
import { KEY as RECORD_KEY, loadRecord, recordPath, SOURCE_RECORD } from './updatecheck.js';
|
|
48
49
|
// `PACKAGE`, `shlexJoin` and `runInstaller` moved to `npminstall.ts` in job51 so `--install` can
|
|
49
50
|
// share the npm spawner without importing this module (see that file's header). Re-exported so
|
|
50
51
|
// this module's surface — and every test importing these from `dist/selfupdate.js` — is unchanged.
|
|
@@ -404,6 +405,71 @@ export function keptInstall(prefix = keptPrefix()) {
|
|
|
404
405
|
* checkout five releases behind is owed that number even though this flag will not be the thing
|
|
405
406
|
* that installs it. The shape decides the ACTION, never whether the question is asked.
|
|
406
407
|
*/
|
|
408
|
+
/**
|
|
409
|
+
* The `checked_at` stamp's shape, spelled rather than defaulted.
|
|
410
|
+
*
|
|
411
|
+
* `toISOString()` renders `.000Z` and CPython's `isoformat()` renders `+00:00`, so a record
|
|
412
|
+
* written by the two runtimes would differ in bytes neither reader cares about. The reference
|
|
413
|
+
* formats `%Y-%m-%dT%H:%M:%SZ`; this strips the milliseconds to reach the same shape.
|
|
414
|
+
*/
|
|
415
|
+
function stamp() {
|
|
416
|
+
return new Date().toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Put the version this flag just fetched into the record `bantamkit_status` reads.
|
|
420
|
+
*
|
|
421
|
+
* THE PORT OF `selfupdate.record_update`. Its docstring carries the argument and is not
|
|
422
|
+
* restated: no second network call and no new failure mode (`update` already holds `latest`),
|
|
423
|
+
* no directory is ever created, the other runtime's key is left exactly as found, and the
|
|
424
|
+
* write goes through a temp file and a rename so no reader sees half a record.
|
|
425
|
+
*
|
|
426
|
+
* WHY THE WRITER IS HERE AND NOT IN `updatecheck.ts`. That module is the READER, and
|
|
427
|
+
* `test/updatecheck.test.mjs` gates its source against `writeFileSync`, `renameSync`,
|
|
428
|
+
* `mkdirSync` and the rest by name — the same claim `test_updatecheck.py` makes over the
|
|
429
|
+
* reference's AST. The split is structural on both sides, so a future reader cannot grow a
|
|
430
|
+
* write by accident. `updatecheck.js` is imported here for the PATH and the KEY, which is the
|
|
431
|
+
* direction that keeps one spelling of each.
|
|
432
|
+
*
|
|
433
|
+
* `renameSync` is atomic on POSIX and on Windows, and the temp file is made in the SAME
|
|
434
|
+
* directory so the rename is never across a filesystem.
|
|
435
|
+
*
|
|
436
|
+
* RETURNS whether it wrote, and THROWS FOR NOTHING. A failed write must not turn a successful
|
|
437
|
+
* `--update` into a failure: the operator's install was updated either way, and the worst case
|
|
438
|
+
* is a status line that still says `never checked`.
|
|
439
|
+
*/
|
|
440
|
+
export function recordUpdate(latest, now) {
|
|
441
|
+
const path = recordPath();
|
|
442
|
+
const directory = dirname(path);
|
|
443
|
+
try {
|
|
444
|
+
if (!statSync(directory).isDirectory())
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
catch {
|
|
448
|
+
// No `<homedir>/.bantamkit` at all: an install makes that directory, and `--update` does
|
|
449
|
+
// not get to decide where this toolbox's home lives. Writing nothing is the answer.
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
const { source, record } = loadRecord(path);
|
|
453
|
+
// An UNREADABLE record is replaced rather than merged: there is nothing in it to preserve.
|
|
454
|
+
const payload = source === SOURCE_RECORD && record !== null ? { ...record } : {};
|
|
455
|
+
payload['checked_at'] = now ?? stamp();
|
|
456
|
+
payload[RECORD_KEY] = { package: PACKAGE, latest };
|
|
457
|
+
const temporary = join(directory, `.update-check-${process.pid}-${Date.now()}.json`);
|
|
458
|
+
try {
|
|
459
|
+
writeFileSync(temporary, `${JSON.stringify(payload, null, 2)}\n`, 'utf8');
|
|
460
|
+
renameSync(temporary, path);
|
|
461
|
+
}
|
|
462
|
+
catch {
|
|
463
|
+
try {
|
|
464
|
+
unlinkSync(temporary);
|
|
465
|
+
}
|
|
466
|
+
catch {
|
|
467
|
+
/* the temp file never existed, or is already gone: either way there is nothing to do */
|
|
468
|
+
}
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
407
473
|
export async function update(installed, origin, options = {}) {
|
|
408
474
|
const ask = options.fetch ?? fetchIndex;
|
|
409
475
|
const installer = options.installer ?? runInstaller;
|
|
@@ -426,6 +492,13 @@ export async function update(installed, origin, options = {}) {
|
|
|
426
492
|
throw e;
|
|
427
493
|
}
|
|
428
494
|
const latest = latestFromIndexPayload(body);
|
|
495
|
+
// THE RECORD IS WRITTEN HERE AND NOT IN ONE OF THE ARMS BELOW, because what it records is
|
|
496
|
+
// "the index said X on this date" — which is true the moment the fetch returned, whatever
|
|
497
|
+
// this flag then decides to do about it. An operator whose shape has no route gets the
|
|
498
|
+
// refusal AND a `bantamkit_status` that now knows the number; one who is already current
|
|
499
|
+
// gets a line that says so with a date. The return value is deliberately dropped: a record
|
|
500
|
+
// that could not be written is not a reason to fail an update that worked.
|
|
501
|
+
recordUpdate(latest);
|
|
429
502
|
const header = fill(COMPARISON, { program: PROGRAM, installed, latest });
|
|
430
503
|
const order = compareVersions(installed, latest);
|
|
431
504
|
if (order === 0)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { compareVersions } from './npminstall.js';
|
|
2
|
+
export { compareVersions };
|
|
3
|
+
/**
|
|
4
|
+
* The command the operator types — the one word that is true on npm and on PyPI both.
|
|
5
|
+
*
|
|
6
|
+
* Equal to `selfupdate.PROGRAM` by construction and by a test, never by an import: see the
|
|
7
|
+
* module docstring. A sentence naming a PACKAGE could not be byte-identical across runtimes,
|
|
8
|
+
* because the PyPI distribution is `bantamkit` and the npm package is `bantamkit-mcp`.
|
|
9
|
+
*/
|
|
10
|
+
export declare const PROGRAM = "bantamkit-mcp";
|
|
11
|
+
/**
|
|
12
|
+
* The key of the record THIS runtime reads. Divergent by construction: the reference reads
|
|
13
|
+
* `pypi`. Registered as a `docs/porting.md` divergence row by J57-5.
|
|
14
|
+
*/
|
|
15
|
+
export declare const KEY = "npm";
|
|
16
|
+
/**
|
|
17
|
+
* The two path components, spelled once. `.bantamkit` under a HOME is this toolbox's own
|
|
18
|
+
* directory; `.bantamkit` under a cwd is a memory store, and this module never builds one.
|
|
19
|
+
*/
|
|
20
|
+
export declare const RECORD_DIR = ".bantamkit";
|
|
21
|
+
export declare const RECORD_NAME = "update-check.json";
|
|
22
|
+
/**
|
|
23
|
+
* No record at all. The state of a machine that has never been checked and of one that has
|
|
24
|
+
* been offline since it was installed — deliberately the same state, because they are the same
|
|
25
|
+
* fact about what is known.
|
|
26
|
+
*/
|
|
27
|
+
export declare const UPDATE_NEVER = "update: never checked.";
|
|
28
|
+
/**
|
|
29
|
+
* The whole point of the feature. `{latest}` and `{installed}` are BOTH here so the reader can
|
|
30
|
+
* falsify the claim, the same reason `selfupdate.COMPARISON` carries both.
|
|
31
|
+
*/
|
|
32
|
+
export declare const UPDATE_AVAILABLE: string;
|
|
33
|
+
/** Names the date because the record may be months old. */
|
|
34
|
+
export declare const UPDATE_CURRENT = "update: {program} {installed} is current as of {date}.";
|
|
35
|
+
/**
|
|
36
|
+
* A real state, not a curiosity: a checkout build, or a release not yet published, lands here —
|
|
37
|
+
* the same pair `selfupdate.AHEAD` exists for.
|
|
38
|
+
*/
|
|
39
|
+
export declare const UPDATE_AHEAD = "update: {program} {installed} is ahead of the package index, which has {latest}.";
|
|
40
|
+
/**
|
|
41
|
+
* One sentence for every malformed shape. It does not name WHICH shape: the operator cannot act
|
|
42
|
+
* differently on any of them, and a per-shape sentence would be N more strings to port.
|
|
43
|
+
*/
|
|
44
|
+
export declare const UPDATE_UNREADABLE = "update: the update record could not be read.";
|
|
45
|
+
export declare const STATE_NEVER = "never";
|
|
46
|
+
export declare const STATE_AVAILABLE = "available";
|
|
47
|
+
export declare const STATE_CURRENT = "current";
|
|
48
|
+
export declare const STATE_AHEAD = "ahead";
|
|
49
|
+
export declare const STATE_UNREADABLE = "unreadable";
|
|
50
|
+
export declare const STATES: readonly ["never", "available", "current", "ahead", "unreadable"];
|
|
51
|
+
/** One of the five above. */
|
|
52
|
+
export type State = (typeof STATES)[number];
|
|
53
|
+
export declare const SOURCE_ABSENT = "absent";
|
|
54
|
+
export declare const SOURCE_UNREADABLE = "unreadable";
|
|
55
|
+
export declare const SOURCE_RECORD = "record";
|
|
56
|
+
/** One of the three above. */
|
|
57
|
+
export type Source = typeof SOURCE_ABSENT | typeof SOURCE_UNREADABLE | typeof SOURCE_RECORD;
|
|
58
|
+
/**
|
|
59
|
+
* Which of the five states, and the one line that says so.
|
|
60
|
+
*
|
|
61
|
+
* Both halves are returned because the conformance gate needs the STATE (the bit that must not
|
|
62
|
+
* differ between runtimes) while `bantamkit_status` needs the LINE (the bit an operator reads).
|
|
63
|
+
* Deriving either from the other would be a second decision somewhere.
|
|
64
|
+
*/
|
|
65
|
+
export interface UpdateStatus {
|
|
66
|
+
state: State;
|
|
67
|
+
line: string;
|
|
68
|
+
}
|
|
69
|
+
/** What `loadRecord` found, and the record if it found one. */
|
|
70
|
+
export interface LoadedRecord {
|
|
71
|
+
source: Source;
|
|
72
|
+
record: Record<string, unknown> | null;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* `<homedir>/.bantamkit/update-check.json`. Never relative to a cwd.
|
|
76
|
+
*
|
|
77
|
+
* `homedir()` reads the environment on every call on every platform this ships to — `HOME` on
|
|
78
|
+
* POSIX, `USERPROFILE` on Windows — which is both the seam the tests use and the reason the
|
|
79
|
+
* path cannot follow a cwd. `.bantamkit` relative to a cwd is a MEMORY STORE, and creating one
|
|
80
|
+
* by accident is a defect class this repo has already paid for (J54-3, `hostinstall.ts:362`).
|
|
81
|
+
*
|
|
82
|
+
* Returns a path whether or not anything is at it. Nothing here creates either component.
|
|
83
|
+
*/
|
|
84
|
+
export declare function recordPath(home?: string): string;
|
|
85
|
+
/**
|
|
86
|
+
* What is at the path, and what it holds. Opens one file, creates nothing, and never throws.
|
|
87
|
+
*
|
|
88
|
+
* `ENOENT` and `ENOTDIR` are ABSENT — in both, nothing is at the path, and a `.bantamkit` that
|
|
89
|
+
* is a regular file leaves the operator in exactly the position of one who has never been
|
|
90
|
+
* checked. Every other error (a directory where the record should be, a mode that cannot be
|
|
91
|
+
* read) is UNREADABLE, because something IS there and this reader cannot use it. Undecodable
|
|
92
|
+
* bytes, text that is not JSON, and JSON that is not an object are UNREADABLE for the same
|
|
93
|
+
* reason.
|
|
94
|
+
*/
|
|
95
|
+
export declare function loadRecord(path?: string): LoadedRecord;
|
|
96
|
+
/**
|
|
97
|
+
* The parsed record, or `null` when there is not one. Creates nothing, never throws.
|
|
98
|
+
*
|
|
99
|
+
* `null` deliberately does NOT say which of absent-or-unreadable it was — a caller that needs
|
|
100
|
+
* the difference calls `loadRecord`, which is the one this module's own decision uses. This is
|
|
101
|
+
* the convenience surface for a caller that only wants the object.
|
|
102
|
+
*/
|
|
103
|
+
export declare function readRecord(path?: string): Record<string, unknown> | null;
|
|
104
|
+
/**
|
|
105
|
+
* The five-state decision, pure: no filesystem, no clock, no network, no TTL.
|
|
106
|
+
*
|
|
107
|
+
* Split from the reading so the conformance harness and the tests can construct a state
|
|
108
|
+
* directly and so the only thing that touches a disk is `loadRecord`.
|
|
109
|
+
*
|
|
110
|
+
* `checked_at` is validated in EVERY arm that reached a record and not only on the `current`
|
|
111
|
+
* path: a record that cannot say when it was written is not a record, and one rule is one thing
|
|
112
|
+
* for the two runtimes to reproduce instead of two.
|
|
113
|
+
*/
|
|
114
|
+
export declare function decide(installed: string, source: Source, record: Record<string, unknown> | null, key?: string): UpdateStatus;
|
|
115
|
+
/** Read the record and decide. The whole reader, in one call, for `bantamkit_status`. */
|
|
116
|
+
export declare function updateStatus(installed: string, key?: string, path?: string): UpdateStatus;
|
|
117
|
+
/** Exactly one of the five sentences. Always a line: there is no silent state here. */
|
|
118
|
+
export declare function updateLine(installed: string, key?: string, path?: string): string;
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The update record on disk, and the five-state line `bantamkit_status` prints from it.
|
|
3
|
+
*
|
|
4
|
+
* THE PORT OF `runtime-py/src/bantamkit/updatecheck.py`, AND THE SENTENCES ARE COPIED FROM IT
|
|
5
|
+
* BYTE FOR BYTE. That module's docstring carries the whole argument — why no reader here may
|
|
6
|
+
* reach the network (`selfupdate.py:29-31`, measured: one registry GET is 1.5x to 100x an
|
|
7
|
+
* entire 0.09 s cold stdio boot), why a newer version existing is NOT a degraded condition,
|
|
8
|
+
* why there is NO TTL in a reader, and why the date is sliced rather than rendered. It is not
|
|
9
|
+
* restated here; read it there. What belongs HERE is the part that could not be copied.
|
|
10
|
+
*
|
|
11
|
+
* EXACTLY ONE THING DIFFERS FROM THE REFERENCE, and it is a genuinely different object rather
|
|
12
|
+
* than a different spelling of one: `KEY` is `npm` here and `pypi` there, because npm and
|
|
13
|
+
* PyPI are two registries that can disagree at one version number — job56 shipped a record
|
|
14
|
+
* that day where they did. The record carries both, each runtime reads its own, and the split
|
|
15
|
+
* is the `docs/porting.md` divergence row J57-5 registers, the same genuinely-different-object
|
|
16
|
+
* as `--update`'s URL row.
|
|
17
|
+
*
|
|
18
|
+
* `PROGRAM` IS DECLARED HERE RATHER THAN IMPORTED, AND THAT IS NOT A SECOND SOURCE OF TRUTH.
|
|
19
|
+
* `test/selfupdate.test.mjs` pins that `cli.ts` is the ONLY module in `src/` importing
|
|
20
|
+
* `selfupdate.ts` — the structural half of AS-7(b), which says a dependency-free toolbox must
|
|
21
|
+
* not grow a network call on a path a host reaches. This module is reached by
|
|
22
|
+
* `bantamkit_status`, which is exactly such a path, so importing the module that owns the one
|
|
23
|
+
* `fetch` in this runtime would break that gate for a five-word constant. The equality is
|
|
24
|
+
* pinned in `test/updatecheck.test.mjs` instead, where importing both is free, the way the
|
|
25
|
+
* reference pins `updatecheck.PROGRAM is selfupdate.PROGRAM`.
|
|
26
|
+
*
|
|
27
|
+
* `compareVersions` IS IMPORTED, because ordering is the part that can be WRONG. It comes from
|
|
28
|
+
* `npminstall.ts`, which is where it lives and which `selfupdate.ts` re-exports rather than
|
|
29
|
+
* owns — so this is the same function the flag uses, not a copy that agrees today. There is no
|
|
30
|
+
* second comparator in this codebase and this module does not add one; `0.9.0` against
|
|
31
|
+
* `0.10.0` is the pair that says so, and `test/updatecheck.test.mjs` both runs it and pins
|
|
32
|
+
* structurally that nothing comparator-shaped is defined here.
|
|
33
|
+
*
|
|
34
|
+
* NO NEW DEPENDENCY: `node:fs`, `node:os` and `node:path`. The `str.format` substitution the
|
|
35
|
+
* reference gets from CPython is the four lines of `fill` below — `selfupdate.ts` has the same
|
|
36
|
+
* four and cannot be imported for them, for the reason two paragraphs up.
|
|
37
|
+
*
|
|
38
|
+
* Layer 5 (Composition): this module reads one file off the operator's home directory and
|
|
39
|
+
* exists to be called from `mcp/status.ts`. It imports no MCP surface and reaches no network.
|
|
40
|
+
*/
|
|
41
|
+
import { readFileSync } from 'node:fs';
|
|
42
|
+
import { homedir } from 'node:os';
|
|
43
|
+
import { join } from 'node:path';
|
|
44
|
+
import { compareVersions } from './npminstall.js';
|
|
45
|
+
export { compareVersions };
|
|
46
|
+
/**
|
|
47
|
+
* The command the operator types — the one word that is true on npm and on PyPI both.
|
|
48
|
+
*
|
|
49
|
+
* Equal to `selfupdate.PROGRAM` by construction and by a test, never by an import: see the
|
|
50
|
+
* module docstring. A sentence naming a PACKAGE could not be byte-identical across runtimes,
|
|
51
|
+
* because the PyPI distribution is `bantamkit` and the npm package is `bantamkit-mcp`.
|
|
52
|
+
*/
|
|
53
|
+
export const PROGRAM = 'bantamkit-mcp';
|
|
54
|
+
/**
|
|
55
|
+
* The key of the record THIS runtime reads. Divergent by construction: the reference reads
|
|
56
|
+
* `pypi`. Registered as a `docs/porting.md` divergence row by J57-5.
|
|
57
|
+
*/
|
|
58
|
+
export const KEY = 'npm';
|
|
59
|
+
/**
|
|
60
|
+
* The two path components, spelled once. `.bantamkit` under a HOME is this toolbox's own
|
|
61
|
+
* directory; `.bantamkit` under a cwd is a memory store, and this module never builds one.
|
|
62
|
+
*/
|
|
63
|
+
export const RECORD_DIR = '.bantamkit';
|
|
64
|
+
export const RECORD_NAME = 'update-check.json';
|
|
65
|
+
// --- the sentences ------------------------------------------------------------------------
|
|
66
|
+
// Copied from `runtime-py/src/bantamkit/updatecheck.py` byte for byte. Changing one is a
|
|
67
|
+
// change to both runtimes and to the conformance suite, never to this file alone.
|
|
68
|
+
/**
|
|
69
|
+
* No record at all. The state of a machine that has never been checked and of one that has
|
|
70
|
+
* been offline since it was installed — deliberately the same state, because they are the same
|
|
71
|
+
* fact about what is known.
|
|
72
|
+
*/
|
|
73
|
+
export const UPDATE_NEVER = 'update: never checked.';
|
|
74
|
+
/**
|
|
75
|
+
* The whole point of the feature. `{latest}` and `{installed}` are BOTH here so the reader can
|
|
76
|
+
* falsify the claim, the same reason `selfupdate.COMPARISON` carries both.
|
|
77
|
+
*/
|
|
78
|
+
export const UPDATE_AVAILABLE = 'update: {program} {installed} is running; the package index has {latest} — run ' +
|
|
79
|
+
'`{program} --update`, then reconnect the host.';
|
|
80
|
+
/** Names the date because the record may be months old. */
|
|
81
|
+
export const UPDATE_CURRENT = 'update: {program} {installed} is current as of {date}.';
|
|
82
|
+
/**
|
|
83
|
+
* A real state, not a curiosity: a checkout build, or a release not yet published, lands here —
|
|
84
|
+
* the same pair `selfupdate.AHEAD` exists for.
|
|
85
|
+
*/
|
|
86
|
+
export const UPDATE_AHEAD = 'update: {program} {installed} is ahead of the package index, which has {latest}.';
|
|
87
|
+
/**
|
|
88
|
+
* One sentence for every malformed shape. It does not name WHICH shape: the operator cannot act
|
|
89
|
+
* differently on any of them, and a per-shape sentence would be N more strings to port.
|
|
90
|
+
*/
|
|
91
|
+
export const UPDATE_UNREADABLE = 'update: the update record could not be read.';
|
|
92
|
+
// --- the states ----------------------------------------------------------------------------
|
|
93
|
+
// The names are what the non-ruled conformance companion compares: given one record and one
|
|
94
|
+
// running version, both runtimes must land in the SAME state, whichever key each one read.
|
|
95
|
+
export const STATE_NEVER = 'never';
|
|
96
|
+
export const STATE_AVAILABLE = 'available';
|
|
97
|
+
export const STATE_CURRENT = 'current';
|
|
98
|
+
export const STATE_AHEAD = 'ahead';
|
|
99
|
+
export const STATE_UNREADABLE = 'unreadable';
|
|
100
|
+
export const STATES = [
|
|
101
|
+
STATE_NEVER,
|
|
102
|
+
STATE_AVAILABLE,
|
|
103
|
+
STATE_CURRENT,
|
|
104
|
+
STATE_AHEAD,
|
|
105
|
+
STATE_UNREADABLE,
|
|
106
|
+
];
|
|
107
|
+
// --- what the loader found -------------------------------------------------------------------
|
|
108
|
+
// Three outcomes and not two, because "there is no file" and "there is a file I cannot read" are
|
|
109
|
+
// different sentences. Collapsing them would print `never checked` over a corrupted record,
|
|
110
|
+
// which is the one wrong thing a reader of this file can do.
|
|
111
|
+
export const SOURCE_ABSENT = 'absent';
|
|
112
|
+
export const SOURCE_UNREADABLE = 'unreadable';
|
|
113
|
+
export const SOURCE_RECORD = 'record';
|
|
114
|
+
/**
|
|
115
|
+
* `0.36.0`, and `0.31.0rc1` too — a first component of digits and dotted parts after it.
|
|
116
|
+
* NOT a PEP 440 or semver parse: a parser is a second, larger thing for two runtimes to
|
|
117
|
+
* reproduce exactly, in service of strings neither writer can produce. `v1.2.3` and `nightly`
|
|
118
|
+
* are not versions here, and neither registry serves either.
|
|
119
|
+
*/
|
|
120
|
+
const VERSION = /^[0-9]+(?:\.[0-9A-Za-z_+-]+)*$/;
|
|
121
|
+
/**
|
|
122
|
+
* The `YYYY-MM-DD` prefix, and only the prefix. Whatever follows it is the writer's business:
|
|
123
|
+
* this module never renders a date, it slices one. A rendered date would make the two runtimes
|
|
124
|
+
* disagree on a machine set to another locale.
|
|
125
|
+
*/
|
|
126
|
+
const DATE_PREFIX = /^([0-9]{4}-[0-9]{2}-[0-9]{2})/;
|
|
127
|
+
/**
|
|
128
|
+
* UTF-8, STRICTLY. `readFileSync(path, 'utf8')` would substitute U+FFFD for an undecodable
|
|
129
|
+
* byte and hand this module a string the reference raises on, so a half-written record would be
|
|
130
|
+
* a different state on the two sides. Decoding the bytes with `fatal` makes it the same one.
|
|
131
|
+
*
|
|
132
|
+
* `ignoreBOM` IS LEFT AT ITS DEFAULT OF `false`, WHICH STRIPS A LEADING BOM, AND THAT IS LOAD
|
|
133
|
+
* BEARING. The reference reads with `utf-8-sig` for exactly this reason: PowerShell's
|
|
134
|
+
* `Set-Content` and `Out-File` write UTF-8 WITH a BOM by default, and a record a reader can
|
|
135
|
+
* plainly act on is not "a shape this reader cannot act on". Setting `ignoreBOM: true` here
|
|
136
|
+
* would leave the BOM in the string, `JSON.parse` would refuse it, and this side would call a
|
|
137
|
+
* Windows operator's perfectly good record broken. The BOM arms in
|
|
138
|
+
* `tools/conformance/suites/updatecheck.mjs` are what hold the two sides together on it; it is
|
|
139
|
+
* NOT a `docs/porting.md` divergence, because there is no difference left to register.
|
|
140
|
+
*/
|
|
141
|
+
const UTF8 = new TextDecoder('utf-8', { fatal: true });
|
|
142
|
+
/**
|
|
143
|
+
* `{name}` substitution, the four lines of `str.format` these sentences use.
|
|
144
|
+
*
|
|
145
|
+
* `selfupdate.ts` exports the same function and this module may not import it (see the module
|
|
146
|
+
* docstring). Kept to exactly the braces the five sentences carry: a missing key is left alone
|
|
147
|
+
* rather than rendered as `undefined`, which is what makes a typo visible instead of silent.
|
|
148
|
+
*/
|
|
149
|
+
function fill(template, values) {
|
|
150
|
+
return template.replace(/\{([a-z]+)\}/g, (whole, name) => values[name] ?? whole);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* `<homedir>/.bantamkit/update-check.json`. Never relative to a cwd.
|
|
154
|
+
*
|
|
155
|
+
* `homedir()` reads the environment on every call on every platform this ships to — `HOME` on
|
|
156
|
+
* POSIX, `USERPROFILE` on Windows — which is both the seam the tests use and the reason the
|
|
157
|
+
* path cannot follow a cwd. `.bantamkit` relative to a cwd is a MEMORY STORE, and creating one
|
|
158
|
+
* by accident is a defect class this repo has already paid for (J54-3, `hostinstall.ts:362`).
|
|
159
|
+
*
|
|
160
|
+
* Returns a path whether or not anything is at it. Nothing here creates either component.
|
|
161
|
+
*/
|
|
162
|
+
export function recordPath(home = homedir()) {
|
|
163
|
+
return join(home, RECORD_DIR, RECORD_NAME);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* What is at the path, and what it holds. Opens one file, creates nothing, and never throws.
|
|
167
|
+
*
|
|
168
|
+
* `ENOENT` and `ENOTDIR` are ABSENT — in both, nothing is at the path, and a `.bantamkit` that
|
|
169
|
+
* is a regular file leaves the operator in exactly the position of one who has never been
|
|
170
|
+
* checked. Every other error (a directory where the record should be, a mode that cannot be
|
|
171
|
+
* read) is UNREADABLE, because something IS there and this reader cannot use it. Undecodable
|
|
172
|
+
* bytes, text that is not JSON, and JSON that is not an object are UNREADABLE for the same
|
|
173
|
+
* reason.
|
|
174
|
+
*/
|
|
175
|
+
export function loadRecord(path = recordPath()) {
|
|
176
|
+
let text;
|
|
177
|
+
try {
|
|
178
|
+
text = UTF8.decode(readFileSync(path));
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
const code = error.code;
|
|
182
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
183
|
+
return { source: SOURCE_ABSENT, record: null };
|
|
184
|
+
// Everything else is a state, not an exception: `EISDIR`, `EACCES`, and the `TypeError`
|
|
185
|
+
// a fatal `TextDecoder` throws on bytes that are not UTF-8.
|
|
186
|
+
return { source: SOURCE_UNREADABLE, record: null };
|
|
187
|
+
}
|
|
188
|
+
let payload;
|
|
189
|
+
try {
|
|
190
|
+
payload = JSON.parse(text);
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
return { source: SOURCE_UNREADABLE, record: null };
|
|
194
|
+
}
|
|
195
|
+
if (typeof payload !== 'object' || payload === null || Array.isArray(payload)) {
|
|
196
|
+
// A list or a bare number is well-formed JSON and is not a record. `typeof null` is
|
|
197
|
+
// `'object'` in JavaScript and `null` is not a record either.
|
|
198
|
+
return { source: SOURCE_UNREADABLE, record: null };
|
|
199
|
+
}
|
|
200
|
+
return { source: SOURCE_RECORD, record: payload };
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The parsed record, or `null` when there is not one. Creates nothing, never throws.
|
|
204
|
+
*
|
|
205
|
+
* `null` deliberately does NOT say which of absent-or-unreadable it was — a caller that needs
|
|
206
|
+
* the difference calls `loadRecord`, which is the one this module's own decision uses. This is
|
|
207
|
+
* the convenience surface for a caller that only wants the object.
|
|
208
|
+
*/
|
|
209
|
+
export function readRecord(path = recordPath()) {
|
|
210
|
+
return loadRecord(path).record;
|
|
211
|
+
}
|
|
212
|
+
/** `record[key].latest` when it is a version string, else `null`. Throws for nothing. */
|
|
213
|
+
function latestIn(record, key) {
|
|
214
|
+
const entry = record[key];
|
|
215
|
+
if (typeof entry !== 'object' || entry === null || Array.isArray(entry))
|
|
216
|
+
return null;
|
|
217
|
+
const latest = entry['latest'];
|
|
218
|
+
if (typeof latest !== 'string')
|
|
219
|
+
return null;
|
|
220
|
+
const trimmed = latest.trim();
|
|
221
|
+
return VERSION.test(trimmed) ? trimmed : null;
|
|
222
|
+
}
|
|
223
|
+
/** The `YYYY-MM-DD` prefix of `checked_at`, or `null`. Sliced, never rendered. */
|
|
224
|
+
function checkedDate(record) {
|
|
225
|
+
const checkedAt = record['checked_at'];
|
|
226
|
+
if (typeof checkedAt !== 'string')
|
|
227
|
+
return null;
|
|
228
|
+
const found = DATE_PREFIX.exec(checkedAt.trim());
|
|
229
|
+
return found?.[1] ?? null;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* The five-state decision, pure: no filesystem, no clock, no network, no TTL.
|
|
233
|
+
*
|
|
234
|
+
* Split from the reading so the conformance harness and the tests can construct a state
|
|
235
|
+
* directly and so the only thing that touches a disk is `loadRecord`.
|
|
236
|
+
*
|
|
237
|
+
* `checked_at` is validated in EVERY arm that reached a record and not only on the `current`
|
|
238
|
+
* path: a record that cannot say when it was written is not a record, and one rule is one thing
|
|
239
|
+
* for the two runtimes to reproduce instead of two.
|
|
240
|
+
*/
|
|
241
|
+
export function decide(installed, source, record, key = KEY) {
|
|
242
|
+
if (source === SOURCE_ABSENT)
|
|
243
|
+
return { state: STATE_NEVER, line: UPDATE_NEVER };
|
|
244
|
+
if (source !== SOURCE_RECORD || record === null) {
|
|
245
|
+
return { state: STATE_UNREADABLE, line: UPDATE_UNREADABLE };
|
|
246
|
+
}
|
|
247
|
+
const date = checkedDate(record);
|
|
248
|
+
const latest = latestIn(record, key);
|
|
249
|
+
if (date === null || latest === null) {
|
|
250
|
+
return { state: STATE_UNREADABLE, line: UPDATE_UNREADABLE };
|
|
251
|
+
}
|
|
252
|
+
const order = compareVersions(installed, latest);
|
|
253
|
+
if (order < 0) {
|
|
254
|
+
return {
|
|
255
|
+
state: STATE_AVAILABLE,
|
|
256
|
+
line: fill(UPDATE_AVAILABLE, { program: PROGRAM, installed, latest }),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
if (order === 0) {
|
|
260
|
+
return {
|
|
261
|
+
state: STATE_CURRENT,
|
|
262
|
+
line: fill(UPDATE_CURRENT, { program: PROGRAM, installed, date }),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
state: STATE_AHEAD,
|
|
267
|
+
line: fill(UPDATE_AHEAD, { program: PROGRAM, installed, latest }),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/** Read the record and decide. The whole reader, in one call, for `bantamkit_status`. */
|
|
271
|
+
export function updateStatus(installed, key = KEY, path = recordPath()) {
|
|
272
|
+
const { source, record } = loadRecord(path);
|
|
273
|
+
return decide(installed, source, record, key);
|
|
274
|
+
}
|
|
275
|
+
/** Exactly one of the five sentences. Always a line: there is no silent state here. */
|
|
276
|
+
export function updateLine(installed, key = KEY, path = recordPath()) {
|
|
277
|
+
return updateStatus(installed, key, path).line;
|
|
278
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bantamkit-mcp",
|
|
3
|
-
"version": "0.35.
|
|
3
|
+
"version": "0.35.2",
|
|
4
4
|
"description": "Memory MCP server for Claude Code, Cursor, VS Code Copilot and Claude Desktop: per-person memory, JSON Schema validation, shift-work tools. Pure Node; install once with npx, run offline.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mcp",
|