prismalens 0.5.8 → 0.5.9

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 (2) hide show
  1. package/dist/bin/launcher.mjs +150 -35
  2. package/package.json +9 -9
@@ -38,6 +38,74 @@ const HOUR = 3_600_000;
38
38
  export class LauncherError extends Error {}
39
39
  class NotFound extends LauncherError {}
40
40
 
41
+ // --- versions ----------------------------------------------------------------------------------
42
+
43
+ // @prismalens/config's version-order.ts, inlined: the launcher has no dependencies (#1239).
44
+ const VERSION =
45
+ /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-nightly\.([2-9]\d{7})\.(0|[1-9]\d*))?$/;
46
+
47
+ /** Semver precedence over the one release grammar; null when either is outside it. */
48
+ export function compareVersions(a, b) {
49
+ const x = VERSION.exec(a);
50
+ const y = VERSION.exec(b);
51
+ if (!x || !y) return null;
52
+ for (let i = 1; i <= 3; i++)
53
+ if (x[i] !== y[i]) return Number(x[i]) - Number(y[i]);
54
+ if (x[4] === undefined || y[4] === undefined)
55
+ return (x[4] === undefined ? 0 : -1) - (y[4] === undefined ? 0 : -1);
56
+ return Number(x[4]) - Number(y[4]) || Number(x[5]) - Number(y[5]);
57
+ }
58
+
59
+ const older = (a, b) => (compareVersions(a, b) ?? 0) < 0;
60
+
61
+ /**
62
+ * A move to `want` from the committed `current` that would go back a version, refused unless the
63
+ * user opted in or it restores a failed trial's runtime: an older build can't open a workspace
64
+ * a newer one migrated.
65
+ */
66
+ export function downgradeRefusal(
67
+ want,
68
+ current,
69
+ { restore = false, env = process.env } = {},
70
+ ) {
71
+ if (!current || restore || env.PRISMALENS_ALLOW_DOWNGRADE === "1")
72
+ return null;
73
+ if (!older(want.version, current.version)) return null;
74
+ return `${want.channel} ${want.version} is older than the ${current.channel} ${current.version} this copy runs, and a workspace ${current.version} migrated may not open in it; nothing was changed. Set PRISMALENS_ALLOW_DOWNGRADE=1 to go back anyway, after restoring the prismalens.db.bak-* file made before those migrations.`;
75
+ }
76
+
77
+ /** The workspace a switch checks when not given one: the app's default, as pl up resolves it. */
78
+ export function defaultWorkspace(env = process.env) {
79
+ return env.PRISMALENS_WORKSPACE_DIR || path.join(os.homedir(), ".prismalens");
80
+ }
81
+
82
+ const RELEASES_API =
83
+ "https://api.github.com/repos/prismalens/prismalens/releases?per_page=30";
84
+
85
+ /**
86
+ * The newest nightly on GitHub with this target's archive and SHA256SUMS attached, not older than
87
+ * `floor`; null when none is.
88
+ */
89
+ export function newestNightly(releases, target, floor = null) {
90
+ return (
91
+ releases
92
+ .filter((r) => r && r.draft === false && typeof r.tag_name === "string")
93
+ .map((r) => ({
94
+ version: r.tag_name.replace(/^v/, ""),
95
+ names: new Set((r.assets ?? []).map((a) => a?.name)),
96
+ }))
97
+ .filter(
98
+ ({ version, names }) =>
99
+ VERSION.exec(version)?.[4] !== undefined &&
100
+ names.has("SHA256SUMS") &&
101
+ names.has(archiveName(version, target)) &&
102
+ !(floor && older(version, floor)),
103
+ )
104
+ .map(({ version }) => version)
105
+ .sort((a, b) => compareVersions(b, a))[0] ?? null
106
+ );
107
+ }
108
+
41
109
  /** `{target}` or `{error}`; musl is told apart by the C library node itself was built against. */
42
110
  export function detectTarget({
43
111
  platform = process.platform,
@@ -389,9 +457,12 @@ export function makeNet({
389
457
  ...bases[0],
390
458
  stallMs: 0,
391
459
  bases,
392
- /** The proxy/CA settings in force, applied once before the first request. */
393
- ready() {
394
- if (!applied && !parsed.base.startsWith("file:")) {
460
+ /**
461
+ * The proxy/CA settings in force, applied once before the first request. A file: base needs
462
+ * none for its artifacts; `remote` asks for them anyway, for a request to GitHub itself.
463
+ */
464
+ ready(remote = false) {
465
+ if (!applied && (remote || !parsed.base.startsWith("file:"))) {
395
466
  applied = networkSettings(env);
396
467
  applyNetwork(applied);
397
468
  }
@@ -1188,8 +1259,9 @@ has no copy, checks it against the SHA256SUMS of its GitHub release,
1188
1259
  keeps it in ${root}, and runs it. Later runs start from that copy offline.
1189
1260
 
1190
1261
  npm carries stable releases. Inside the app, pl upgrade --channel nightly moves this
1191
- copy to the newest nightly and pl upgrade --channel stable moves it back; the
1192
- switch refuses when the workspace database is newer than the target.
1262
+ copy to the newest nightly, and pl upgrade --channel stable moves it back once a
1263
+ stable release newer than that nightly is out. No switch goes to an older version,
1264
+ and a switch refuses when the workspace database is newer than the target.
1193
1265
 
1194
1266
  Commands
1195
1267
  pl --launcher-help This text.
@@ -1210,6 +1282,7 @@ Environment
1210
1282
  HTTPS_PROXY, NO_PROXY Proxy for the download; npm's https-proxy, proxy and
1211
1283
  noproxy are used when these are unset.
1212
1284
  NODE_EXTRA_CA_CERTS Extra CA certificates; npm's cafile when unset.
1285
+ PRISMALENS_ALLOW_DOWNGRADE=1 Let a switch or this launcher go to an older version.
1213
1286
 
1214
1287
  Behind Artifactory, Nexus or another registry proxy
1215
1288
  1. Create a generic remote repository whose URL is
@@ -1234,14 +1307,38 @@ function option(argv, name) {
1234
1307
  async function switchTo(argv, ctx) {
1235
1308
  const { root, target, own, net } = ctx;
1236
1309
  const kind = option(argv, "--launcher-switch");
1310
+ const restore = argv.includes("--restore");
1237
1311
  const pointer = readPointer(root, target);
1238
1312
  let want;
1239
1313
  if (kind === "stable") want = own;
1240
1314
  else if (kind === "nightly") {
1241
- const version = option(argv, "--version");
1242
- if (!version?.includes("-nightly."))
1315
+ let version = option(argv, "--version");
1316
+ if (version === undefined) {
1317
+ // The proxy and CA settings apply before the first request, as install() does for its own.
1318
+ net.ready(true);
1319
+ const trusted = net.bases?.at(-1) ?? net;
1320
+ let releases;
1321
+ try {
1322
+ releases = await (await get(RELEASES_API, trusted)).json();
1323
+ } catch (error) {
1324
+ if (!(error instanceof LauncherError)) throw error;
1325
+ throw new LauncherError(
1326
+ `couldn't list the nightlies on GitHub (${error.message}); pass --version <nightly version>`,
1327
+ );
1328
+ }
1329
+ version = newestNightly(
1330
+ Array.isArray(releases) ? releases : [],
1331
+ target,
1332
+ pointer?.current?.version,
1333
+ );
1334
+ if (!version)
1335
+ throw new LauncherError(
1336
+ `no nightly for ${target} is newer than ${pointer?.current?.version ?? "this copy"}; nothing was changed`,
1337
+ );
1338
+ }
1339
+ if (VERSION.exec(version)?.[4] === undefined)
1243
1340
  throw new LauncherError(
1244
- "--launcher-switch nightly needs --version <nightly version>",
1341
+ `--launcher-switch nightly takes a nightly version like 0.5.2-nightly.20261111.1, not "${version}"`,
1245
1342
  );
1246
1343
  // A nightly this copy verified before must keep the digest it was verified with.
1247
1344
  const known = [pointer?.current, pointer?.previous].find(
@@ -1258,49 +1355,56 @@ async function switchTo(argv, ctx) {
1258
1355
  if (!pointer?.previous)
1259
1356
  throw new LauncherError("there is no previous runtime to switch to");
1260
1357
  want = pointer.previous;
1358
+ // Refused before the npm advice below, which an older launcher would only meet again at start.
1359
+ const refused = downgradeRefusal(want, pointer.current, { restore });
1360
+ if (refused) throw new LauncherError(refused);
1261
1361
  // A stable runtime is this launcher's own version; another one comes from npm.
1262
1362
  if (want.channel === "stable" && want.version !== own.version)
1263
1363
  throw new LauncherError(
1264
- `the previous runtime is stable ${want.version}, but this launcher runs stable ${own.version}; install that launcher with npm install -g prismalens@${want.version}, then run pl again`,
1364
+ `the previous runtime is stable ${want.version}, but this launcher runs stable ${own.version}; install that launcher with npm install -g prismalens@${want.version}, then run prismalens --launcher-switch previous${older(want.version, pointer.current.version) ? " with PRISMALENS_ALLOW_DOWNGRADE=1 set" : ""}`,
1265
1365
  );
1266
1366
  } else
1267
1367
  throw new LauncherError(
1268
1368
  `--launcher-switch takes stable, nightly or previous, not "${kind}"`,
1269
1369
  );
1370
+ // Checked before any download, then again under the pointer lock against what is committed then.
1371
+ const early = downgradeRefusal(want, pointer?.current, { restore });
1372
+ if (early) throw new LauncherError(early);
1270
1373
  const rt = await ensureRuntime(root, want, net);
1271
1374
  if (!(await claim(root, target, net, rt)))
1272
1375
  throw new LauncherError(
1273
1376
  `the ${want.version} runtime changed while switching; run the switch again`,
1274
1377
  );
1275
- const workspace = option(argv, "--workspace");
1276
- if (workspace) {
1277
- const dbFile = path.join(workspace, "prismalens.db");
1278
- const skew = await migrationSkew(rt.dir, dbFile);
1279
- if (skew.length)
1280
- throw new LauncherError(
1281
- skewMessage(dbFile, want, skew, pointer?.current),
1282
- );
1283
- }
1378
+ const workspace = option(argv, "--workspace") ?? defaultWorkspace();
1379
+ const dbFile = path.join(workspace, "prismalens.db");
1380
+ const skew = await migrationSkew(rt.dir, dbFile);
1381
+ if (skew.length)
1382
+ throw new LauncherError(skewMessage(dbFile, want, skew, pointer?.current));
1284
1383
  const current = record(want, rt);
1285
- await claim(root, target, net, rt, (now) => ({
1286
- current,
1287
- previous:
1288
- (same(now?.current, current) ? now?.previous : now?.current) ?? null,
1289
- }));
1384
+ await claim(root, target, net, rt, (now) => {
1385
+ const refused = downgradeRefusal(want, now?.current, { restore });
1386
+ if (refused) throw new LauncherError(refused);
1387
+ return {
1388
+ current,
1389
+ previous:
1390
+ (same(now?.current, current) ? now?.previous : now?.current) ?? null,
1391
+ };
1392
+ });
1290
1393
  net.log(`now running ${current.channel} ${current.version}`);
1291
1394
  process.stdout.write(`${JSON.stringify(current)}\n`);
1292
1395
  }
1293
1396
 
1294
1397
  /**
1295
- * The runtime to run now: the pointer's nightly, else this launcher's own stable version.
1296
- * A nightly that is gone runs the recorded previous this once and leaves the pointer alone;
1297
- * the app's migration runner still refuses a database newer than that runtime.
1398
+ * The runtime to run now: the pointer's nightly, else this launcher's own stable version, never
1399
+ * older than the committed pointer. A nightly that is gone and not cached starts nothing.
1298
1400
  */
1299
1401
  async function resolveRuntime({ root, target, own, net }) {
1300
1402
  for (let attempt = 0; ; attempt++) {
1301
1403
  const pointer = readPointer(root, target);
1302
1404
  const want =
1303
1405
  pointer?.current?.channel === "nightly" ? pointer.current : own;
1406
+ const stale = downgradeRefusal(want, pointer?.current);
1407
+ if (stale) throw new LauncherError(staleLauncher(stale, own));
1304
1408
  let moved = false;
1305
1409
  try {
1306
1410
  const rt = await ensureClaimed(root, want, net, (now, claimed) => {
@@ -1331,12 +1435,9 @@ async function resolveRuntime({ root, target, own, net }) {
1331
1435
  } catch (error) {
1332
1436
  if (!(error instanceof NotFound) || want.channel !== "nightly")
1333
1437
  throw error;
1334
- const fallback = pointer.previous ?? own;
1335
- net.log(
1336
- `nightly ${want.version} is no longer on the release server (nightlies are pruned) and is not cached here. Running ${fallback.channel} ${fallback.version}, recorded as previous, this once; if the nightly migrated this workspace, pl up refuses to start it and says how to restore. pl upgrade --channel nightly moves to the newest nightly, --channel stable stays on stable.`,
1438
+ throw new LauncherError(
1439
+ `nightly ${want.version} is no longer on the release server (nightlies are pruned) and is not cached here, so nothing was started. Run prismalens --launcher-switch nightly to move to a newer nightly, or install a stable newer than it with npm install -g prismalens@latest and run prismalens --launcher-switch stable. If neither is out yet, wait for one.`,
1337
1440
  );
1338
- const rt = await ensureClaimed(root, fallback, net);
1339
- return { ...rt, current: record(fallback, rt) };
1340
1441
  }
1341
1442
  }
1342
1443
  }
@@ -1349,6 +1450,8 @@ async function resolveRuntime({ root, target, own, net }) {
1349
1450
  async function repair({ root, target, own, net }) {
1350
1451
  const pointer = readPointer(root, target);
1351
1452
  const want = pointer?.current?.channel === "nightly" ? pointer.current : own;
1453
+ const stale = downgradeRefusal(want, pointer?.current);
1454
+ if (stale) throw new LauncherError(staleLauncher(stale, own));
1352
1455
  const old = cachedAll(root, want);
1353
1456
  for (const f of old) {
1354
1457
  const problems = damage(f.dir, target, f.meta, { full: true });
@@ -1368,16 +1471,23 @@ async function repair({ root, target, own, net }) {
1368
1471
  unlock();
1369
1472
  }
1370
1473
  const current = record(want, rt);
1371
- await claim(root, target, net, rt, (now) =>
1372
- now?.current?.dir === current.dir
1474
+ await claim(root, target, net, rt, (now) => {
1475
+ // A switch that landed during the download wins: repair rewrites only the runtime it repaired.
1476
+ if (now?.current && !same(now.current, current)) {
1477
+ net.log(
1478
+ `the pointer moved to ${now.current.channel} ${now.current.version} while repairing ${want.version}; it was left as it is`,
1479
+ );
1480
+ return null;
1481
+ }
1482
+ return now?.current?.dir === current.dir
1373
1483
  ? null
1374
1484
  : {
1375
1485
  current,
1376
1486
  previous:
1377
1487
  (same(now?.current, current) ? now?.previous : now?.current) ??
1378
1488
  null,
1379
- },
1380
- );
1489
+ };
1490
+ });
1381
1491
  for (const f of old) {
1382
1492
  if (!fs.existsSync(f.dir)) net.log(`removed ${f.dir}`);
1383
1493
  else if (leased(f.dir))
@@ -1389,6 +1499,11 @@ async function repair({ root, target, own, net }) {
1389
1499
  net.log(`repaired ${want.channel} ${want.version} in ${rt.dir}`);
1390
1500
  }
1391
1501
 
1502
+ /** A launcher older than the runtime its pointer commits: npm put an older prismalens in place. */
1503
+ function staleLauncher(refusal, own) {
1504
+ return `this launcher is prismalens ${own.version}. ${refusal} Or install the newer launcher with npm install -g prismalens@latest.`;
1505
+ }
1506
+
1392
1507
  function run(rt, ctx, args) {
1393
1508
  const { node, app } = entryOf(rt.dir, ctx.target);
1394
1509
  const env = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prismalens",
3
- "version": "0.5.8",
3
+ "version": "0.5.9",
4
4
  "description": "Run the coding agents you already use, such as Claude Code, Codex and OpenCode, in threads and git worktrees on your machine.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -38,16 +38,16 @@
38
38
  "prismalens": {
39
39
  "release": {
40
40
  "channel": "stable",
41
- "tag": "v0.5.8",
42
- "version": "0.5.8",
41
+ "tag": "v0.5.9",
42
+ "version": "0.5.9",
43
43
  "sha256": {
44
- "darwin-arm64": "780c01155bcc14e5d3cadb17fd47b48f7458cb12af642c496d47bf22c10bafc9",
45
- "darwin-x64": "ff2daf1873638a6acb2bdbef95d1ef57c586be5bbe365dc0b75f878c52414c75",
46
- "linux-arm64": "d629a8c1c8c75720b18cb5404c7297755459683cb6b3e6d648109023e8daec4f",
47
- "linux-x64": "2d69e6406f9b41738c9d6a76751530297e45f6c12a637a31f670dc9f605e92ed",
48
- "win32-x64": "42050f90a7d9ed3eae278ab38bab7e2c4d20280ad5f47b8e1dde41d3bcd259e3"
44
+ "darwin-arm64": "585a85f7b000c7672b8d6e0ea9a195b1209242638cc3433e36a3b9d27d903c25",
45
+ "darwin-x64": "0ab5dbc3a22612790c8acd4134e1acc92309c20bd0e52e4bc95d9e1ae06b7174",
46
+ "linux-arm64": "5d5b8eaa12c653a8604a4637bacfc2fe85a6d97041f9d6f1512a524dec57ae82",
47
+ "linux-x64": "65c89ecc91056216710c97da4fca0a9b76bd19d3c3080f5a191f55d34ac6b95d",
48
+ "win32-x64": "e47b2037f4093cdf653b909ad7ebfd8cc159ce6983f755321f38eeb2a3bbcbe3"
49
49
  }
50
50
  }
51
51
  },
52
- "build": "a193d90"
52
+ "build": "d9c0295"
53
53
  }