@sublang/spex 5.0.0 → 7.0.0-beta.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.
Files changed (114) hide show
  1. package/README.md +453 -41
  2. package/dist/cli.js +12 -1
  3. package/dist/commands/add.d.ts +1 -1
  4. package/dist/commands/add.js +3 -3
  5. package/dist/commands/auth-target.d.ts +1 -0
  6. package/dist/commands/auth-target.js +4 -2
  7. package/dist/commands/environment.d.ts +15 -21
  8. package/dist/commands/environment.js +43 -57
  9. package/dist/commands/export.js +2 -2
  10. package/dist/commands/home.d.ts +2 -2
  11. package/dist/commands/home.js +3 -3
  12. package/dist/commands/list.js +1 -1
  13. package/dist/commands/login.js +12 -7
  14. package/dist/commands/logout.js +2 -3
  15. package/dist/commands/publish.js +16 -16
  16. package/dist/commands/remove.js +1 -1
  17. package/dist/commands/run.d.ts +1 -1
  18. package/dist/commands/run.js +22 -3
  19. package/dist/commands/search.js +1 -1
  20. package/dist/commands/settle.d.ts +1 -1
  21. package/dist/commands/settle.js +3 -3
  22. package/dist/commands/whoami.js +2 -6
  23. package/dist/guards/approval.d.ts +56 -0
  24. package/dist/guards/approval.js +158 -0
  25. package/dist/guards/checks.d.ts +207 -0
  26. package/dist/guards/checks.js +681 -0
  27. package/dist/guards/commands.d.ts +15 -0
  28. package/dist/guards/commands.js +313 -0
  29. package/dist/guards/copy.d.ts +67 -0
  30. package/dist/guards/copy.js +306 -0
  31. package/dist/guards/declaration.d.ts +45 -0
  32. package/dist/guards/declaration.js +224 -0
  33. package/dist/guards/files.d.ts +137 -0
  34. package/dist/guards/files.js +544 -0
  35. package/dist/guards/harness.d.ts +58 -0
  36. package/dist/guards/harness.js +420 -0
  37. package/dist/guards/items.d.ts +61 -0
  38. package/dist/guards/items.js +161 -0
  39. package/dist/guards/patch.d.ts +55 -0
  40. package/dist/guards/patch.js +258 -0
  41. package/dist/guards/processes.d.ts +49 -0
  42. package/dist/guards/processes.js +233 -0
  43. package/dist/guards/proofs.d.ts +88 -0
  44. package/dist/guards/proofs.js +503 -0
  45. package/dist/guards/records.d.ts +176 -0
  46. package/dist/guards/records.js +513 -0
  47. package/dist/guards/report.d.ts +26 -0
  48. package/dist/guards/report.js +492 -0
  49. package/dist/guards/runner.d.ts +42 -0
  50. package/dist/guards/runner.js +167 -0
  51. package/dist/guards/tla.d.ts +51 -0
  52. package/dist/guards/tla.js +352 -0
  53. package/dist/guards/tlapm.d.ts +81 -0
  54. package/dist/guards/tlapm.js +172 -0
  55. package/dist/guards/tlc.d.ts +191 -0
  56. package/dist/guards/tlc.js +457 -0
  57. package/dist/guards/translation.d.ts +66 -0
  58. package/dist/guards/translation.js +202 -0
  59. package/dist/guards/types.d.ts +185 -0
  60. package/dist/guards/types.js +437 -0
  61. package/dist/guards/verify.d.ts +290 -0
  62. package/dist/guards/verify.js +1191 -0
  63. package/dist/guards/xml.d.ts +18 -0
  64. package/dist/guards/xml.js +105 -0
  65. package/dist/lib/environment/archive.d.ts +16 -3
  66. package/dist/lib/environment/archive.js +39 -8
  67. package/dist/lib/environment/builtins.d.ts +10 -12
  68. package/dist/lib/environment/builtins.js +18 -20
  69. package/dist/lib/environment/exports.d.ts +14 -13
  70. package/dist/lib/environment/exports.js +31 -16
  71. package/dist/lib/environment/format.d.ts +2 -2
  72. package/dist/lib/environment/format.js +3 -3
  73. package/dist/lib/environment/git-source.js +6 -3
  74. package/dist/lib/environment/install.d.ts +14 -16
  75. package/dist/lib/environment/install.js +66 -61
  76. package/dist/lib/environment/lock.d.ts +4 -3
  77. package/dist/lib/environment/lock.js +6 -5
  78. package/dist/lib/environment/marker.js +1 -2
  79. package/dist/lib/environment/own-paths.d.ts +7 -10
  80. package/dist/lib/environment/own-paths.js +10 -13
  81. package/dist/lib/environment/placed-record.d.ts +3 -2
  82. package/dist/lib/environment/placed-record.js +5 -4
  83. package/dist/lib/environment/registries.d.ts +7 -9
  84. package/dist/lib/environment/registries.js +9 -36
  85. package/dist/lib/environment/registry.d.ts +6 -7
  86. package/dist/lib/environment/registry.js +12 -12
  87. package/dist/lib/environment/requests.d.ts +6 -5
  88. package/dist/lib/environment/requests.js +6 -8
  89. package/dist/lib/environment/resolve.d.ts +4 -4
  90. package/dist/lib/environment/resolve.js +3 -4
  91. package/dist/lib/environment/store.d.ts +34 -8
  92. package/dist/lib/environment/store.js +138 -43
  93. package/dist/lib/files.d.ts +72 -12
  94. package/dist/lib/files.js +200 -65
  95. package/dist/lib/git-host.d.ts +52 -258
  96. package/dist/lib/git-host.js +52 -265
  97. package/dist/lib/home-file.d.ts +13 -10
  98. package/dist/lib/home-file.js +28 -18
  99. package/dist/lib/instance.d.ts +1 -1
  100. package/dist/lib/instance.js +1 -1
  101. package/dist/lint.d.ts +22 -0
  102. package/dist/lint.js +36 -0
  103. package/dist/testing/standin-registry.js +1 -1
  104. package/package.json +7 -3
  105. package/scaffold/.file-history.json +14 -7
  106. package/scaffold/i18n/zh/source-pins.json +41 -0
  107. package/scaffold/i18n/zh/specs/decisions/000-spec-structure-format.md +0 -1
  108. package/scaffold/i18n/zh/specs/intents/000-spdx-headers.md +0 -1
  109. package/scaffold/i18n/zh/specs/map.md +0 -1
  110. package/scaffold/i18n/zh/specs/meta.md +18 -35
  111. package/scaffold/i18n/zh/specs/packages/git.md +0 -1
  112. package/scaffold/i18n/zh/specs/packages/licensing.md +0 -1
  113. package/scaffold/specs/meta.md +18 -3
  114. package/scripts/test-exit-guard.mjs +28 -0
@@ -1,7 +1,6 @@
1
1
  import { type RegistrySource } from "./registry.js";
2
- /** One place an install asks, in order: an instance's registry by its
3
- * canonical origin, or a recorded registry that names no instance's
4
- * origin and so is never asked. */
2
+ /** A pinned package registry, or an invalid address kept for an honest
3
+ * failure report without making a request. */
5
4
  export type InstallRegistry = {
6
5
  origin: string;
7
6
  } | {
@@ -9,13 +8,12 @@ export type InstallRegistry = {
9
8
  };
10
9
  /**
11
10
  * The order an install asks registries for a registry package's files
12
- * (environments-7): `instance`, the environment's instance; `recorded`,
13
- * the registry its lock records for the package; `signedIn`, the origin
14
- * of every instance the home is signed in to, in the home's order.
15
- * Independent mode instead asks only the pinned registry and explicitly
16
- * trusted equivalents, keeping sign-in separate from package trust.
11
+ * (environments-7): the registry its lock records and explicitly trusted
12
+ * equivalents. The former instance, signed-in list and independent
13
+ * option remain accepted for published API compatibility but confer no
14
+ * package trust or account authority.
17
15
  */
18
- export declare function installRegistryOrder(instance: string, recorded: string, signedIn: readonly string[], options?: {
16
+ export declare function installRegistryOrder(_instance: string, recorded: string, _signedIn: readonly string[], options?: {
19
17
  independent?: boolean;
20
18
  trusted?: readonly string[];
21
19
  }): InstallRegistry[];
@@ -1,19 +1,17 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  // SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
3
3
  // Registry installs retain the exact source in the lock (DR-168).
4
- // Legacy manifests retain their previous ordered fallback during migration;
5
- // independent manifests never scan unrelated signed-in Spaces.
4
+ // Package trust never follows the list of signed-in Spaces.
6
5
  import { canonicalOrigin } from "../instance.js";
7
6
  import { RegistryError } from "./registry.js";
8
7
  /**
9
8
  * The order an install asks registries for a registry package's files
10
- * (environments-7): `instance`, the environment's instance; `recorded`,
11
- * the registry its lock records for the package; `signedIn`, the origin
12
- * of every instance the home is signed in to, in the home's order.
13
- * Independent mode instead asks only the pinned registry and explicitly
14
- * trusted equivalents, keeping sign-in separate from package trust.
9
+ * (environments-7): the registry its lock records and explicitly trusted
10
+ * equivalents. The former instance, signed-in list and independent
11
+ * option remain accepted for published API compatibility but confer no
12
+ * package trust or account authority.
15
13
  */
16
- export function installRegistryOrder(instance, recorded, signedIn, options = {}) {
14
+ export function installRegistryOrder(_instance, recorded, _signedIn, options = {}) {
17
15
  const out = [];
18
16
  const asked = new Set();
19
17
  const add = (origin) => {
@@ -22,39 +20,14 @@ export function installRegistryOrder(instance, recorded, signedIn, options = {})
22
20
  asked.add(origin);
23
21
  out.push({ origin });
24
22
  };
25
- if (options.independent) {
26
- try {
27
- add(canonicalOrigin(recorded));
28
- }
29
- catch {
30
- out.push({ unasked: recorded });
31
- }
32
- for (const origin of options.trusted ?? [])
33
- add(canonicalOrigin(origin));
34
- return out;
35
- }
36
- add(canonicalOrigin(instance));
37
- let origin;
38
23
  try {
39
- origin = canonicalOrigin(recorded);
24
+ add(canonicalOrigin(recorded));
40
25
  }
41
26
  catch {
42
- origin = null;
43
- }
44
- if (origin !== null)
45
- add(origin);
46
- else
47
27
  out.push({ unasked: recorded });
48
- for (const entry of signedIn) {
49
- let at;
50
- try {
51
- at = canonicalOrigin(entry);
52
- }
53
- catch {
54
- continue;
55
- }
56
- add(at);
57
28
  }
29
+ for (const origin of options.trusted ?? [])
30
+ add(canonicalOrigin(origin));
58
31
  return out;
59
32
  }
60
33
  /** A registry a lock records under no instance's origin
@@ -112,8 +112,8 @@ export declare function readVersionIndex(name: string, raw: unknown): VersionInd
112
112
  /** Validate a version resource answer: a format this client does not
113
113
  * know is refused naming it (environments-12). */
114
114
  export declare function readVersionResource(name: string, version: string, raw: unknown): VersionResource;
115
- /** The codes a registry refuses an instance's token with that sign that
116
- * instance out (environments-12, git-host-4); `token_expired` is not one
115
+ /** The codes that invalidate only the presented registry grant
116
+ * (environments-12); `token_expired` is not one
117
117
  * of them, an expired access secret being a failure of its call. */
118
118
  export declare const SIGN_OUT_CODES: readonly string[];
119
119
  export interface RegistryDescriptor {
@@ -128,19 +128,18 @@ export interface SpaceDirectoryEntry {
128
128
  }
129
129
  export interface RegistryClientOptions {
130
130
  url: string;
131
- /** The app token of the instance the registry answers for, where the
132
- * home is signed in there; a getter that throws fails the call as
131
+ /** The independent registry grant; a getter that throws fails the call as
133
132
  * `unavailable`. */
134
133
  token?: () => Promise<string | null>;
135
134
  /** Told the token a call presented when the registry refused it with
136
135
  * one of `SIGN_OUT_CODES`, before that call fails (environments-12):
137
- * awaited; where it throws — the token no longer the instance's stored
138
- * one (git-host-4) — the call fails with what it threw in place of the
136
+ * awaited; where it throws — the token no longer the registry's stored
137
+ * one — the call fails with what it threw in place of the
139
138
  * registry's words, still `unauthenticated`. */
140
139
  refused?: (token: string, code: string) => void | Promise<void>;
141
140
  fetch?: typeof fetch;
142
141
  }
143
- /** Spex Space over HTTP (environments-12). */
142
+ /** The package service over HTTP (environments-12). */
144
143
  export declare class RegistryClient implements RegistrySource {
145
144
  readonly url: string;
146
145
  private readonly token?;
@@ -1,14 +1,12 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  // SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
3
- // The registry source (environments-12): Spex Space's `/api/v1` for
4
- // format 2 (Spex Space DR-016, DR-017). The version index carries every
3
+ // The package registry source (environments-12), using its independent
4
+ // registry account. The version index carries every
5
5
  // version with its dependencies and artifacts, so a dependency graph
6
6
  // resolves from indexes alone; a version resource is read only for a
7
7
  // version picked; files come by raw URL or the release archive, each
8
- // verified by its digest elsewhere. The app token of the instance the
9
- // registry answers for is presented as a bearer where the home is signed
10
- // in there; an answer refusing it as the instance's sign-out is reported
11
- // with the token it refused (environments-12, DR-127).
8
+ // verified by its digest elsewhere. Registry-grant refusals are reported
9
+ // with the token refused and never affect Space grants.
12
10
  import { canonicalOrigin } from "../instance.js";
13
11
  import { isPackageName } from "./format.js";
14
12
  export class RegistryError extends Error {
@@ -157,11 +155,11 @@ export function readVersionResource(name, version, raw) {
157
155
  ...(isPlainObject(raw.dist) && typeof raw.dist.tarball === "string" ? { dist: { tarball: raw.dist.tarball, sha256: String(raw.dist.sha256 ?? raw.checksum) } } : {}),
158
156
  };
159
157
  }
160
- /** The codes a registry refuses an instance's token with that sign that
161
- * instance out (environments-12, git-host-4); `token_expired` is not one
158
+ /** The codes that invalidate only the presented registry grant
159
+ * (environments-12); `token_expired` is not one
162
160
  * of them, an expired access secret being a failure of its call. */
163
161
  export const SIGN_OUT_CODES = ["invalid_token", "token_revoked", "reauth_required"];
164
- /** Spex Space over HTTP (environments-12). */
162
+ /** The package service over HTTP (environments-12). */
165
163
  export class RegistryClient {
166
164
  url;
167
165
  token;
@@ -249,14 +247,16 @@ export class RegistryClient {
249
247
  if (response.status === 410)
250
248
  throw new RegistryError("not_found", name, `${about}withdrawn at ${host}`, code ?? "suppressed", undefined, answered);
251
249
  if (response.status === 401) {
252
- // A refusal answers the token this call presented (git-host-4); a
250
+ // A refusal answers the token this call presented; a
253
251
  // hook that throws says why the call fails instead.
254
252
  if (token && code !== undefined && SIGN_OUT_CODES.includes(code) && this.refused) {
255
253
  try {
256
254
  await this.refused(token, code);
257
255
  }
258
256
  catch (error) {
259
- throw new RegistryError("unauthenticated", name, `${about}${error instanceof Error ? error.message : String(error)}`, code, undefined, answered);
257
+ const refusal = new RegistryError("unauthenticated", name, `${about}${error instanceof Error ? error.message : String(error)}`, code, undefined, answered);
258
+ refusal.cause = error;
259
+ throw refusal;
260
260
  }
261
261
  }
262
262
  throw new RegistryError("unauthenticated", name, `${about}sign-in expired at ${host}`, code, undefined, answered);
@@ -347,7 +347,7 @@ export class RegistryClient {
347
347
  return null;
348
348
  orgs.push({ name: org.name, path: org.path, role: org.role, ...(typeof org.provider === "string" ? { provider: org.provider } : {}) });
349
349
  }
350
- return orgs;
350
+ return orgs.sort((a, b) => a.name.localeCompare(b.name));
351
351
  })();
352
352
  return Promise.race([asked, late]);
353
353
  }
@@ -47,7 +47,7 @@ export declare function packageNameRefusal(name: string): string;
47
47
  /** Whether a request path stays inside the working folder. */
48
48
  export declare function isInsidePath(path: string): boolean;
49
49
  /** Every reason a request for a name is refused (environments-2,
50
- * environments-15): a bad name, no or several sources, a malformed
50
+ * environments-65): a bad name, no or several sources, a malformed
51
51
  * requirement, a path that is not relative or leads out of the folder
52
52
  * it is relative to, a bad select or alias. */
53
53
  export declare function requestIssues(name: string, request: unknown): Issue[];
@@ -67,9 +67,10 @@ export interface EnvironmentRegistry {
67
67
  scope: AuthScope;
68
68
  legacy: boolean;
69
69
  }
70
- /** Existing manifests without registry preserve their former source and
71
- * credential. An absent manifest starts in independent registry mode. */
72
- export declare function environmentRegistry(requests: Pick<Requests, "space" | "registry"> | null, home: Pick<HomeFile, "origin" | "instances">): EnvironmentRegistry;
70
+ /** Package routing never inherits collaboration settings or grants.
71
+ * Existing locked sources keep their provenance when installed. The
72
+ * home argument remains for compatibility with published callers. */
73
+ export declare function environmentRegistry(requests: Pick<Requests, "space" | "registry"> | null, _home: Pick<HomeFile, "origin" | "instances">): EnvironmentRegistry;
73
74
  /** Name a package registry without changing the Space binding. */
74
75
  export declare function setRegistryIn(doc: Document, origin: string): void;
75
76
  /** Parse `spex.yaml` text, or throw. */
@@ -99,7 +100,7 @@ export declare function editRequests(file: string, edit: (doc: Document) => void
99
100
  text: string;
100
101
  digest: string;
101
102
  }>;
102
- /** Set one package's request, or remove it with null (environments-15). */
103
+ /** Set one package's request, or remove it with null (environments-65). */
103
104
  export declare function setRequest(file: string, name: string, request: Request | null): Promise<{
104
105
  requests: Requests;
105
106
  text: string;
@@ -57,7 +57,7 @@ export function isInsidePath(path) {
57
57
  }
58
58
  const REQUEST_FIELDS = new Set(["version", "path", "git", "rev", "select", "alias"]);
59
59
  /** Every reason a request for a name is refused (environments-2,
60
- * environments-15): a bad name, no or several sources, a malformed
60
+ * environments-65): a bad name, no or several sources, a malformed
61
61
  * requirement, a path that is not relative or leads out of the folder
62
62
  * it is relative to, a bad select or alias. */
63
63
  export function requestIssues(name, request) {
@@ -227,12 +227,10 @@ export function environmentInstance(requests, home) {
227
227
  }
228
228
  /** The public package service used explicitly by new environments. */
229
229
  export const PUBLIC_REGISTRY_ORIGIN = "https://spex.sublang.ai";
230
- /** Existing manifests without registry preserve their former source and
231
- * credential. An absent manifest starts in independent registry mode. */
232
- export function environmentRegistry(requests, home) {
233
- if (requests !== null && requests.registry === undefined) {
234
- return { origin: environmentInstance(requests, home), scope: "space", legacy: true };
235
- }
230
+ /** Package routing never inherits collaboration settings or grants.
231
+ * Existing locked sources keep their provenance when installed. The
232
+ * home argument remains for compatibility with published callers. */
233
+ export function environmentRegistry(requests, _home) {
236
234
  return { origin: canonicalOrigin(requests?.registry ?? PUBLIC_REGISTRY_ORIGIN), scope: "registry", legacy: false };
237
235
  }
238
236
  /** Name a package registry without changing the Space binding. */
@@ -377,7 +375,7 @@ export async function editRequests(file, edit) {
377
375
  writeVersionedBytes(file, text, version);
378
376
  return { requests, text, digest: requestsDigest(text) };
379
377
  }
380
- /** Set one package's request, or remove it with null (environments-15). */
378
+ /** Set one package's request, or remove it with null (environments-65). */
381
379
  export async function setRequest(file, name, request) {
382
380
  if (request !== null) {
383
381
  const issues = requestIssues(name, request);
@@ -26,11 +26,11 @@ export interface ResolveInput {
26
26
  requestsText?: string;
27
27
  registry: RegistrySource;
28
28
  /** The folder a request's `path` is relative to (environments-2): the
29
- * paired working folder's root, or your own group's clone's; null
29
+ * paired working folder's root, or the global records repository's; null
30
30
  * where this device has none. */
31
31
  pathRoot: string | null;
32
- /** Whether `pathRoot` is your own group's clone, whose requests by
33
- * path name only a folder that syncs with it (environments-32). */
32
+ /** Whether `pathRoot` is the global records repository, whose requests by
33
+ * path name only a folder that syncs with it. */
34
34
  confined?: boolean;
35
35
  git: GitSource;
36
36
  /** The credential for a Git repository, where one applies. */
@@ -48,7 +48,7 @@ export type ResolveErrorKind = "no_working_folder" | "path_outside" | "path_conf
48
48
  * the request's folder or Git repository at its revision; whether the
49
49
  * folder itself is missing; the package a folder holds in place of the
50
50
  * one asked; the issues of a `meta.yaml` that does not read; and the
51
- * reason your own group's clone refuses a folder (environments-32). */
51
+ * reason the global records repository refuses a folder. */
52
52
  export interface ResolveErrorDetail {
53
53
  path?: string;
54
54
  git?: string;
@@ -52,8 +52,7 @@ function selectionHolds(selection, artifacts) {
52
52
  return artifact !== undefined && (entry.language === undefined || artifact.languages.includes(entry.language));
53
53
  });
54
54
  }
55
- /** A path your own group's clone refuses, in the library's words
56
- * (environments-32), with its reason for a host to phrase. */
55
+ /** A path the global records repository refuses, in the library's words, with its reason for a host to phrase. */
57
56
  function confinedError(name, path, issue) {
58
57
  return new ResolveError("path_confined", name, ownPathMessage(name, path, issue), { path, confined: issue });
59
58
  }
@@ -96,7 +95,7 @@ async function loadFixed(name, request, input) {
96
95
  if (real.startsWith("..") || isAbsolute(real))
97
96
  throw new ResolveError("path_outside", name, `${name}: ${path} leads out of ${place} through a link`, { path });
98
97
  const release = await readRequested(name, dir, path, { path });
99
- // Everything your own group's folder holds syncs with its clone.
98
+ // The global records repository syncs the files it tracks.
100
99
  if (input.confined) {
101
100
  const issue = ownFolderIssue(root, request.path, release.files.map((file) => file.path));
102
101
  if (issue)
@@ -419,7 +418,7 @@ export async function resolve(input) {
419
418
  .sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0))
420
419
  : [];
421
420
  // 4. Exports: one skill per provided skill and playbook under its
422
- // id, and per provided spec under `<org>-<pkg>` (DR-115), or alias;
421
+ // id, and per provided spec under `<org>-<pkg>` (DR-174), or alias;
423
422
  // a selected artifact its release does not provide — a sibling's
424
423
  // support, or of a kind `provides` leaves out — is installed with the
425
424
  // rest and placed nowhere, so its name clashes with none.
@@ -5,19 +5,45 @@ export declare class DigestError extends Error {
5
5
  readonly what: string;
6
6
  constructor(expected: string, actual: string, what: string);
7
7
  }
8
- /** The key a store entry goes by: `<sha>` or `x/<sha>`. */
9
- export declare function storeKey(sha256: string, executable: boolean): string;
10
- /** Every store entry the given locks select. */
8
+ /** Every stored file the given locks select, by digest. */
11
9
  export declare function lockStoreKeys(locks: Iterable<Lock>): Set<string>;
12
10
  export declare class ContentStore {
13
11
  readonly root: string;
14
12
  constructor(homeDir: string);
15
- pathOf(sha256: string, executable: boolean): string;
16
- has(sha256: string, executable: boolean): boolean;
13
+ pathOf(sha256: string): string;
14
+ has(sha256: string): boolean;
17
15
  /** Store bytes under their digest, verified first; written once. */
18
- put(bytes: Uint8Array, sha256: string, executable: boolean, what?: string): string;
19
- /** Every entry's key. */
16
+ put(bytes: Uint8Array, sha256: string, what?: string): string;
17
+ /** Take a file's former copy under `x/` as the stored file where the
18
+ * store lacks it, verified as any bytes are (environments-7): whether
19
+ * the store now holds the file. A copy that does not match its digest
20
+ * is no stored file; one that cannot be taken for another reason is
21
+ * none here either, an install then fetching the file. */
22
+ adoptFormer(sha256: string): boolean;
23
+ /** `adoptFormer`, throwing where the copy stands and matches or cannot
24
+ * be read, yet cannot be stored: the store refusing the write, or the
25
+ * device full. False where no copy stands or it does not match. */
26
+ private takeFormer;
27
+ /** Every stored file's digest. */
20
28
  keys(): string[];
21
- /** Remove every entry no lock selects; returns the removed keys. */
29
+ /** Copy a stored file to `to`, where no file stands, as an ordinary
30
+ * writable file, executable or not as the lock says, whatever the
31
+ * umask. The bytes are checked against their digest as they are read:
32
+ * a stored file that no longer matches is removed and nothing is
33
+ * written (environments-7). */
34
+ copy(sha256: string, executable: boolean, to: string, what?: string): void;
35
+ /** Remove every stored file no lock selects, read-only as it is, and
36
+ * the former `x/`, each of its copies a lock selects taken as the
37
+ * stored file first where the store lacks it — a lock whose install
38
+ * did not run, stale or with no folder here, keeps its bytes. A
39
+ * selected copy that could not be taken for another reason than its
40
+ * digest stands, with `x/`, for a later collection. Every step is
41
+ * tried whatever another's failure, and the failures are thrown once
42
+ * all are done; returns the removed digests (environments-7). */
22
43
  gc(selected: Set<string>): string[];
44
+ /** The former `x/` at a collection: each copy a lock selects taken as
45
+ * the stored file, then removed with every copy no lock selects and
46
+ * every one that does not match its digest; one that could not be
47
+ * taken stands, its cause noted in `failures`, and `x/` with it. */
48
+ private collectFormer;
23
49
  }
@@ -1,13 +1,16 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  // SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
3
- // The content-addressed store under the home (environments-7, DR-104):
4
- // each distinct file once, named by its SHA-256, written once after its
5
- // digest is verified, read-only; an executable copy apart under `x/`
6
- // because hard links share one mode. A file stays while a lock in a
7
- // spex repository on this device selects it.
3
+ // The content-addressed store under the home (environments-7, DR-104,
4
+ // DR-251): a cache of verified bytes, each distinct file once, named by
5
+ // its SHA-256, written once after its digest is verified. An install
6
+ // copies a file out, its digest checked again as it is read, so an
7
+ // installed tree shares nothing with the store. A stored file is written
8
+ // read-only against an edit by mistake; nothing relies on that mark. A
9
+ // file stays while a lock of an environment the home knows selects it.
8
10
  import { randomUUID } from "node:crypto";
9
- import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readdirSync, renameSync, rmSync, statSync, writeSync } from "node:fs";
11
+ import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync, writeSync } from "node:fs";
10
12
  import { join } from "node:path";
13
+ import { removeTree } from "../files.js";
11
14
  import { sha256Hex } from "./format.js";
12
15
  export class DigestError extends Error {
13
16
  expected;
@@ -21,45 +24,43 @@ export class DigestError extends Error {
21
24
  this.name = "DigestError";
22
25
  }
23
26
  }
24
- /** The key a store entry goes by: `<sha>` or `x/<sha>`. */
25
- export function storeKey(sha256, executable) {
26
- return executable ? `x/${sha256}` : sha256;
27
- }
28
- /** Every store entry the given locks select. */
27
+ /** Every stored file the given locks select, by digest. */
29
28
  export function lockStoreKeys(locks) {
30
29
  const keys = new Set();
31
30
  for (const lock of locks) {
32
31
  for (const resolution of Object.values(lock.packages)) {
33
32
  for (const file of resolution.files)
34
- keys.add(storeKey(file.sha256, file.executable));
33
+ keys.add(file.sha256);
35
34
  }
36
35
  }
37
36
  return keys;
38
37
  }
39
38
  const SHA_RE = /^[0-9a-f]{64}$/;
39
+ /** Where an earlier release kept an executable file's own copy, links
40
+ * sharing one mode (DR-251). */
41
+ const FORMER_COPIES = "x";
40
42
  export class ContentStore {
41
43
  root;
42
44
  constructor(homeDir) {
43
45
  this.root = join(homeDir, "store");
44
46
  }
45
- pathOf(sha256, executable) {
47
+ pathOf(sha256) {
46
48
  if (!SHA_RE.test(sha256))
47
49
  throw new Error(`"${sha256}" is not a SHA-256 digest`);
48
- return executable ? join(this.root, "x", sha256) : join(this.root, sha256);
50
+ return join(this.root, sha256);
49
51
  }
50
- has(sha256, executable) {
51
- return existsSync(this.pathOf(sha256, executable));
52
+ has(sha256) {
53
+ return existsSync(this.pathOf(sha256));
52
54
  }
53
55
  /** Store bytes under their digest, verified first; written once. */
54
- put(bytes, sha256, executable, what = "file") {
55
- const target = this.pathOf(sha256, executable);
56
+ put(bytes, sha256, what = "file") {
57
+ const target = this.pathOf(sha256);
56
58
  const actual = sha256Hex(bytes);
57
59
  if (actual !== sha256)
58
60
  throw new DigestError(sha256, actual, what);
59
61
  if (existsSync(target))
60
62
  return target;
61
- const dir = executable ? join(this.root, "x") : this.root;
62
- mkdirSync(dir, { recursive: true });
63
+ mkdirSync(this.root, { recursive: true });
63
64
  const temporary = join(this.root, `.tmp-${randomUUID()}`);
64
65
  const fd = openSync(temporary, "wx", 0o600);
65
66
  try {
@@ -71,40 +72,105 @@ export class ContentStore {
71
72
  finally {
72
73
  closeSync(fd);
73
74
  }
74
- chmodSync(temporary, executable ? 0o555 : 0o444);
75
+ chmodSync(temporary, 0o444);
75
76
  if (existsSync(target))
76
- rmSync(temporary, { force: true });
77
+ removeTree(temporary);
77
78
  else
78
79
  renameSync(temporary, target);
79
80
  return target;
80
81
  }
81
- /** Every entry's key. */
82
+ /** Take a file's former copy under `x/` as the stored file where the
83
+ * store lacks it, verified as any bytes are (environments-7): whether
84
+ * the store now holds the file. A copy that does not match its digest
85
+ * is no stored file; one that cannot be taken for another reason is
86
+ * none here either, an install then fetching the file. */
87
+ adoptFormer(sha256) {
88
+ try {
89
+ return this.takeFormer(sha256);
90
+ }
91
+ catch {
92
+ return false;
93
+ }
94
+ }
95
+ /** `adoptFormer`, throwing where the copy stands and matches or cannot
96
+ * be read, yet cannot be stored: the store refusing the write, or the
97
+ * device full. False where no copy stands or it does not match. */
98
+ takeFormer(sha256) {
99
+ if (this.has(sha256))
100
+ return true;
101
+ let bytes;
102
+ try {
103
+ bytes = readFileSync(join(this.root, FORMER_COPIES, sha256));
104
+ }
105
+ catch (error) {
106
+ if (error.code === "ENOENT")
107
+ return false;
108
+ throw error;
109
+ }
110
+ try {
111
+ this.put(bytes, sha256);
112
+ }
113
+ catch (error) {
114
+ if (error instanceof DigestError)
115
+ return false;
116
+ throw error;
117
+ }
118
+ return true;
119
+ }
120
+ /** Every stored file's digest. */
82
121
  keys() {
83
- const out = [];
84
122
  if (!existsSync(this.root))
85
- return out;
86
- for (const name of readdirSync(this.root))
87
- if (SHA_RE.test(name))
88
- out.push(name);
89
- const x = join(this.root, "x");
90
- if (existsSync(x))
91
- for (const name of readdirSync(x))
92
- if (SHA_RE.test(name))
93
- out.push(`x/${name}`);
94
- return out;
123
+ return [];
124
+ return readdirSync(this.root).filter((name) => SHA_RE.test(name));
125
+ }
126
+ /** Copy a stored file to `to`, where no file stands, as an ordinary
127
+ * writable file, executable or not as the lock says, whatever the
128
+ * umask. The bytes are checked against their digest as they are read:
129
+ * a stored file that no longer matches is removed and nothing is
130
+ * written (environments-7). */
131
+ copy(sha256, executable, to, what = "file") {
132
+ const from = this.pathOf(sha256);
133
+ const bytes = readFileSync(from);
134
+ const actual = sha256Hex(bytes);
135
+ if (actual !== sha256) {
136
+ removeTree(from);
137
+ throw new DigestError(sha256, actual, `the stored copy of ${what}`);
138
+ }
139
+ const mode = executable ? 0o755 : 0o644;
140
+ writeFileSync(to, bytes, { flag: "wx", mode });
141
+ chmodSync(to, mode);
95
142
  }
96
- /** Remove every entry no lock selects; returns the removed keys. */
143
+ /** Remove every stored file no lock selects, read-only as it is, and
144
+ * the former `x/`, each of its copies a lock selects taken as the
145
+ * stored file first where the store lacks it — a lock whose install
146
+ * did not run, stale or with no folder here, keeps its bytes. A
147
+ * selected copy that could not be taken for another reason than its
148
+ * digest stands, with `x/`, for a later collection. Every step is
149
+ * tried whatever another's failure, and the failures are thrown once
150
+ * all are done; returns the removed digests (environments-7). */
97
151
  gc(selected) {
98
152
  const removed = [];
99
- for (const key of this.keys()) {
100
- if (selected.has(key))
101
- continue;
102
- rmSync(join(this.root, ...key.split("/")), { force: true });
103
- removed.push(key);
153
+ if (!existsSync(this.root))
154
+ return removed;
155
+ const failures = [];
156
+ const step = (run) => { try {
157
+ run();
104
158
  }
159
+ catch (error) {
160
+ failures.push(error);
161
+ } };
162
+ step(() => this.collectFormer(selected, failures));
163
+ step(() => {
164
+ for (const key of this.keys()) {
165
+ if (selected.has(key))
166
+ continue;
167
+ removeTree(join(this.root, key));
168
+ removed.push(key);
169
+ }
170
+ });
105
171
  // A temporary file an interrupted put left behind, once it is old
106
172
  // enough not to be a put still running.
107
- if (existsSync(this.root)) {
173
+ step(() => {
108
174
  const before = Date.now() - 60 * 60 * 1000;
109
175
  for (const name of readdirSync(this.root)) {
110
176
  if (!name.startsWith(".tmp-"))
@@ -112,12 +178,41 @@ export class ContentStore {
112
178
  const file = join(this.root, name);
113
179
  try {
114
180
  if (statSync(file).mtimeMs < before)
115
- rmSync(file, { force: true });
181
+ removeTree(file);
116
182
  }
117
183
  catch { /* gone */ }
118
184
  }
119
- }
185
+ });
186
+ if (failures.length === 1)
187
+ throw failures[0];
188
+ if (failures.length > 1)
189
+ throw new AggregateError(failures, failures.map((failure) => (failure instanceof Error ? failure.message : String(failure))).join("; "));
120
190
  return removed;
121
191
  }
192
+ /** The former `x/` at a collection: each copy a lock selects taken as
193
+ * the stored file, then removed with every copy no lock selects and
194
+ * every one that does not match its digest; one that could not be
195
+ * taken stands, its cause noted in `failures`, and `x/` with it. */
196
+ collectFormer(selected, failures) {
197
+ const former = join(this.root, FORMER_COPIES);
198
+ if (!existsSync(former))
199
+ return;
200
+ let kept = false;
201
+ for (const name of readdirSync(former)) {
202
+ if (selected.has(name)) {
203
+ try {
204
+ this.takeFormer(name);
205
+ }
206
+ catch (error) {
207
+ kept = true;
208
+ failures.push(new Error(`store/${FORMER_COPIES}/${name} stays, not taken as the stored file: ${error instanceof Error ? error.message : String(error)}`, { cause: error }));
209
+ continue;
210
+ }
211
+ }
212
+ removeTree(join(former, name));
213
+ }
214
+ if (!kept)
215
+ removeTree(former);
216
+ }
122
217
  }
123
218
  //# sourceMappingURL=store.js.map