bs-engineering-suite 2.9.0-rc.11 → 2.9.0-rc.13

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 (179) hide show
  1. package/NOTICE.md +1 -1
  2. package/README.md +40 -38
  3. package/cordis.patch.yml +8 -0
  4. package/docs/content-packs/ONLINE_LIBRARY.md +15 -0
  5. package/docs/content-packs/TEMPLATE_PACKAGE_GUIDE.md +31 -0
  6. package/docs/rag-packs/library.md +57 -0
  7. package/docs/windows-install.md +29 -0
  8. package/docs/windows-usage.md +30 -0
  9. package/lib/ui-policy.cjs +11 -5
  10. package/node_modules/@buildseek/content-packs/lib/bundle.d.ts +3 -1
  11. package/node_modules/@buildseek/content-packs/lib/bundle.js +28 -6
  12. package/node_modules/@buildseek/content-packs/lib/bundle.js.map +1 -1
  13. package/node_modules/@buildseek/content-packs/lib/office-xml.d.ts +2 -0
  14. package/node_modules/@buildseek/content-packs/lib/office-xml.js +9 -0
  15. package/node_modules/@buildseek/content-packs/lib/office-xml.js.map +1 -0
  16. package/node_modules/@buildseek/content-packs/lib/repository.d.ts +10 -3
  17. package/node_modules/@buildseek/content-packs/lib/repository.js +395 -129
  18. package/node_modules/@buildseek/content-packs/lib/repository.js.map +1 -1
  19. package/node_modules/@buildseek/content-packs/lib/types.d.ts +1 -0
  20. package/node_modules/@buildseek/content-packs/lib/xlsx.d.ts +3 -0
  21. package/node_modules/@buildseek/content-packs/lib/xlsx.js +13 -6
  22. package/node_modules/@buildseek/content-packs/lib/xlsx.js.map +1 -1
  23. package/node_modules/@buildseek/diagnostics/lib/client.js +6 -6
  24. package/node_modules/@buildseek/diagnostics/lib/sanitize.js +1 -1
  25. package/node_modules/@buildseek/diagnostics/lib/store.js +18 -7
  26. package/node_modules/@buildseek/diagnostics/lib/updates.js +23 -11
  27. package/node_modules/@buildseek/diagnostics/lib/version.js +1 -1
  28. package/node_modules/@buildseek/diagnostics/package.json +7 -7
  29. package/node_modules/@buildseek/harness-engineering-document-plugin/dist/client/document-library.d.ts +1 -1
  30. package/node_modules/@buildseek/harness-engineering-document-plugin/dist/client/suite-settings.d.ts +1 -0
  31. package/node_modules/@buildseek/harness-engineering-document-plugin/dist/client/workbench-fact-editor.d.ts +1 -0
  32. package/node_modules/@buildseek/harness-engineering-document-plugin/dist/client.js +28 -8
  33. package/node_modules/@buildseek/harness-engineering-document-plugin/integration/licenses/deepseek-ai-0.2.2-rc.4-MIT-LICENSE +21 -0
  34. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/content-pack-rules.js +2 -1
  35. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/content-pack-validation.js +6 -2
  36. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/content-packs.js +116 -38
  37. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/files/evidence.js +3 -1
  38. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/files/materials.js +116 -39
  39. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/files/renderers.js +2 -1
  40. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/files/xlsx-preview-layout.js +1 -1
  41. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/identity.js +7 -7
  42. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/online-pack-worker.js +17 -18
  43. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/online-packs.js +6 -5
  44. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/orchestrator.js +25 -5
  45. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-content-flow.js +149 -23
  46. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/application.js +140 -81
  47. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/artifact-mirror.js +10 -6
  48. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/bridges.js +6 -4
  49. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/contract.js +1 -1
  50. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/evidence.js +34 -9
  51. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/filesystem.js +6 -0
  52. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/full-before.js +91 -34
  53. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/generation.js +54 -18
  54. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/plan.js +1 -0
  55. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/preview-leases.js +10 -3
  56. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/project-basic-reader.js +245 -47
  57. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/project-setup.js +27 -18
  58. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/project-data/store.js +4 -4
  59. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/schemas.js +8 -0
  60. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/service.js +4 -1
  61. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/typert.host.js +5 -5
  62. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/typert.remote-client.js +2 -2
  63. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/content-packs.d.ts +87 -18
  64. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/files/materials.d.ts +2 -0
  65. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/identity.d.ts +7 -7
  66. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/model.d.ts +1 -0
  67. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/project-content-flow.d.ts +75 -18
  68. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/project-data/application.d.ts +1 -0
  69. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/project-data/artifact-mirror.d.ts +1 -0
  70. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/project-data/bridges.d.ts +5 -0
  71. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/project-data/evidence.d.ts +3 -2
  72. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/project-data/filesystem.d.ts +2 -0
  73. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/project-data/plan.d.ts +1 -0
  74. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/project-data/preview-leases.d.ts +1 -0
  75. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/project-data/project-basic-reader.d.ts +14 -9
  76. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/schemas.d.ts +10 -0
  77. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/service.d.ts +1 -1
  78. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/types/workbench/application.d.ts +3 -0
  79. package/node_modules/@buildseek/harness-engineering-document-plugin/lib/workbench/application.js +45 -4
  80. package/node_modules/@buildseek/harness-engineering-document-plugin/package.json +66 -65
  81. package/node_modules/@buildseek/harness-engineering-document-plugin/policy/plugin-policy.json +3 -3
  82. package/node_modules/@buildseek/harness-engineering-document-plugin/scripts/package-contract.mjs +6 -6
  83. package/node_modules/@buildseek/harness-oag-plugin/dist/client/content-packs.d.ts +4 -0
  84. package/node_modules/@buildseek/harness-oag-plugin/dist/client/index.d.ts +2 -1
  85. package/node_modules/@buildseek/harness-oag-plugin/dist/client/suite-settings.d.ts +1 -0
  86. package/node_modules/@buildseek/harness-oag-plugin/dist/client.js +84 -19
  87. package/node_modules/@buildseek/harness-oag-plugin/dist/content-packs.js +22 -5
  88. package/node_modules/@buildseek/harness-oag-plugin/dist/native/buildseek_oag_node.darwin-arm64.node +0 -0
  89. package/node_modules/@buildseek/harness-oag-plugin/dist/native/buildseek_oag_node.win32-x64-msvc.node +0 -0
  90. package/node_modules/@buildseek/harness-oag-plugin/dist/native/buildseek_oag_node.win32-x64-msvc.node.json +3 -3
  91. package/node_modules/@buildseek/harness-oag-plugin/lib/config.js +7 -0
  92. package/node_modules/@buildseek/harness-oag-plugin/lib/remote-gateway.js +13 -11
  93. package/node_modules/@buildseek/harness-oag-plugin/lib/semantic-registry.js +67 -17
  94. package/node_modules/@buildseek/harness-oag-plugin/lib/service.js +11 -8
  95. package/node_modules/@buildseek/harness-oag-plugin/lib/tool-adapter.js +26 -10
  96. package/node_modules/@buildseek/harness-oag-plugin/lib/typert.host.js +91 -21
  97. package/node_modules/@buildseek/harness-oag-plugin/lib/typert.remote-client.d.ts +4 -1
  98. package/node_modules/@buildseek/harness-oag-plugin/lib/typert.remote-client.d.ts.map +1 -1
  99. package/node_modules/@buildseek/harness-oag-plugin/lib/typert.remote-client.js +58 -11
  100. package/node_modules/@buildseek/harness-oag-plugin/lib/types/api.d.ts +1 -1
  101. package/node_modules/@buildseek/harness-oag-plugin/lib/types/authorization.d.ts +1 -0
  102. package/node_modules/@buildseek/harness-oag-plugin/lib/types/config.d.ts +1 -1
  103. package/node_modules/@buildseek/harness-oag-plugin/lib/types/remote-gateway.d.ts +2 -1
  104. package/node_modules/@buildseek/harness-oag-plugin/lib/types/semantic-registry.d.ts +18 -7
  105. package/node_modules/@buildseek/harness-oag-plugin/lib/types/service.d.ts +6 -5
  106. package/node_modules/@buildseek/harness-oag-plugin/lib/types/tool-adapter.d.ts +1 -0
  107. package/node_modules/@buildseek/harness-oag-plugin/package.json +35 -35
  108. package/node_modules/@buildseek/harness-rag-plugin/data/library/bs-rag-national-building-2026.11-r1.zip +0 -0
  109. package/node_modules/@buildseek/harness-rag-plugin/dist/client/suite-settings.d.ts +1 -0
  110. package/node_modules/@buildseek/harness-rag-plugin/dist/client.js +309 -40
  111. package/node_modules/@buildseek/harness-rag-plugin/lib/core.js +201 -37
  112. package/node_modules/@buildseek/harness-rag-plugin/lib/local-folder.js +58 -14
  113. package/node_modules/@buildseek/harness-rag-plugin/lib/project-setup-read.js +5 -0
  114. package/node_modules/@buildseek/harness-rag-plugin/lib/rag-pack-library.js +283 -59
  115. package/node_modules/@buildseek/harness-rag-plugin/lib/retrieval-gateway.js +44 -30
  116. package/node_modules/@buildseek/harness-rag-plugin/lib/retrieval-lifecycle.js +112 -38
  117. package/node_modules/@buildseek/harness-rag-plugin/lib/retrieval-scope.js +1 -1
  118. package/node_modules/@buildseek/harness-rag-plugin/lib/retrieval-service.js +297 -139
  119. package/node_modules/@buildseek/harness-rag-plugin/lib/service.js +21 -9
  120. package/node_modules/@buildseek/harness-rag-plugin/lib/tool-adapter.js +15 -13
  121. package/node_modules/@buildseek/harness-rag-plugin/lib/typert.host.js +460 -25
  122. package/node_modules/@buildseek/harness-rag-plugin/lib/typert.remote-client.d.ts.map +1 -1
  123. package/node_modules/@buildseek/harness-rag-plugin/lib/typert.remote-client.js +255 -6
  124. package/node_modules/@buildseek/harness-rag-plugin/lib/types/api.d.ts +1 -1
  125. package/node_modules/@buildseek/harness-rag-plugin/lib/types/core.d.ts +43 -1
  126. package/node_modules/@buildseek/harness-rag-plugin/lib/types/index.d.ts +1 -1
  127. package/node_modules/@buildseek/harness-rag-plugin/lib/types/local-folder.d.ts +2 -0
  128. package/node_modules/@buildseek/harness-rag-plugin/lib/types/project-setup-read.d.ts +12 -0
  129. package/node_modules/@buildseek/harness-rag-plugin/lib/types/rag-pack-library.d.ts +14 -0
  130. package/node_modules/@buildseek/harness-rag-plugin/lib/types/retrieval-lifecycle.d.ts +15 -3
  131. package/node_modules/@buildseek/harness-rag-plugin/lib/types/retrieval-service.d.ts +27 -6
  132. package/node_modules/@buildseek/harness-rag-plugin/lib/types/service.d.ts +18 -0
  133. package/node_modules/@buildseek/harness-rag-plugin/package.json +37 -37
  134. package/node_modules/@buildseek/platform/project-setup.d.ts +10 -6
  135. package/node_modules/@buildseek/platform/project-setup.js +684 -31
  136. package/node_modules/@dsh-engineering/workbench/client/bundle.js +345 -140
  137. package/node_modules/@dsh-engineering/workbench/client/entry-connection.d.ts +1 -1
  138. package/node_modules/@dsh-engineering/workbench/client/entry.d.ts +2 -0
  139. package/node_modules/@dsh-engineering/workbench/client/native-session-observer.d.ts +2 -0
  140. package/node_modules/@dsh-engineering/workbench/client/project-controller.d.ts +8 -0
  141. package/node_modules/@dsh-engineering/workbench/client/project-workspace.d.ts +3 -2
  142. package/node_modules/@dsh-engineering/workbench/client/styles.d.ts +1 -1
  143. package/node_modules/@dsh-engineering/workbench/client/suite-updates.d.ts +2 -0
  144. package/node_modules/@dsh-engineering/workbench/package.json +15 -15
  145. package/node_modules/bs-engineering-docs/client/bundle.js +18 -13
  146. package/node_modules/bs-engineering-docs/lib/agent.js +53 -16
  147. package/node_modules/bs-engineering-docs/lib/api.js +54 -80
  148. package/node_modules/bs-engineering-docs/lib/extract/scan.js +30 -4
  149. package/node_modules/bs-engineering-docs/lib/generate/candidate.js +17 -0
  150. package/node_modules/bs-engineering-docs/lib/generate/run.js +12 -8
  151. package/node_modules/bs-engineering-docs/lib/index.js +30 -2
  152. package/node_modules/bs-engineering-docs/lib/project-tools.js +32 -5
  153. package/node_modules/bs-engineering-docs/lib/store.js +298 -243
  154. package/node_modules/bs-engineering-docs/lib/util/errors.js +2 -0
  155. package/node_modules/dsh-sidebar-office-preview/client/bundle.js +33 -33
  156. package/node_modules/dsh-sidebar-office-preview/client/src/parse.js +8 -1
  157. package/node_modules/dsh-sidebar-office-preview/cordis.patch.yml +1 -1
  158. package/node_modules/dsh-sidebar-office-preview/package.json +1 -1
  159. package/package.json +9 -4
  160. package/ui-compat/darwin-arm64/receipt.json +32 -27
  161. package/ui-compat/darwin-arm64/vendor/desktop/@deepseek-ai/dsh-client-ui-plugin-manager/lib/client.js +10 -2
  162. package/ui-compat/darwin-arm64/vendor/npm/@deepseek-ai/dsh-client-ui-chat/lib/bs-ui-guard.js +2 -2
  163. package/ui-compat/darwin-arm64/vendor/npm/@deepseek-ai/dsh-client-ui-chat/package.json +40 -40
  164. package/ui-compat/darwin-arm64/vendor/npm/@deepseek-ai/dsh-client-ui-plugin-manager/lib/bs-ui-guard.js +2 -2
  165. package/ui-compat/darwin-arm64/vendor/npm/@deepseek-ai/dsh-client-ui-plugin-manager/lib/client.js +10 -2
  166. package/ui-compat/darwin-arm64/vendor/npm/@deepseek-ai/dsh-client-ui-plugin-manager/package.json +19 -19
  167. package/ui-compat/darwin-arm64/vendor/npm/@deepseek-ai/dsh-client-ui-sidebar/lib/bs-ui-guard.js +2 -2
  168. package/ui-compat/darwin-arm64/vendor/npm/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
  169. package/ui-compat/darwin-arm64/vendor/npm/@deepseek-ai/dsh-client-ui-sidebar/package.json +13 -13
  170. package/ui-compat/win32-x64/receipt.json +32 -27
  171. package/ui-compat/win32-x64/vendor/desktop/@deepseek-ai/dsh-client-ui-plugin-manager/lib/client.js +10 -2
  172. package/ui-compat/win32-x64/vendor/npm/@deepseek-ai/dsh-client-ui-chat/lib/bs-ui-guard.js +2 -2
  173. package/ui-compat/win32-x64/vendor/npm/@deepseek-ai/dsh-client-ui-chat/package.json +40 -40
  174. package/ui-compat/win32-x64/vendor/npm/@deepseek-ai/dsh-client-ui-plugin-manager/lib/bs-ui-guard.js +2 -2
  175. package/ui-compat/win32-x64/vendor/npm/@deepseek-ai/dsh-client-ui-plugin-manager/lib/client.js +10 -2
  176. package/ui-compat/win32-x64/vendor/npm/@deepseek-ai/dsh-client-ui-plugin-manager/package.json +19 -19
  177. package/ui-compat/win32-x64/vendor/npm/@deepseek-ai/dsh-client-ui-sidebar/lib/bs-ui-guard.js +2 -2
  178. package/ui-compat/win32-x64/vendor/npm/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
  179. package/ui-compat/win32-x64/vendor/npm/@deepseek-ai/dsh-client-ui-sidebar/package.json +13 -13
@@ -1,4 +1,4 @@
1
- import { packageReferences } from '@buildseek/platform/project-setup';
1
+ import { packageReferences, projectPackageReferenced, withProjectPackageLock } from '@buildseek/platform/project-setup';
2
2
  import { constants } from 'node:fs';
3
3
  import * as fs from 'node:fs/promises';
4
4
  import { dirname, join, resolve } from 'node:path';
@@ -6,17 +6,22 @@ import { randomUUID } from 'node:crypto';
6
6
  import { compareReleaseVersions } from './updates.js';
7
7
  import { ContentPackError } from './types.js';
8
8
  import { packFailure, normalizePackDiagnostics, formatPackDiagnostics } from './diagnostics.js';
9
- import { assertSafePath, assertUserSource, checkBundleMembers, fail, MAX_UPLOAD_BYTES, readBundle, semanticSha256, sha256, USER_SOURCE_PATH, validateBundle, writeBundle, } from './bundle.js';
9
+ import { assertSafePath, assertUserSource, checkBundleMembers, fail, MAX_UPLOAD_BYTES, parseManifest, readBundle, semanticSha256, sha256, USER_SOURCE_PATH, validateBundle, writeBundle, } from './bundle.js';
10
+ const STAGING_TTL_MS = 24 * 60 * 60 * 1000;
11
+ const MAX_INDEX_BYTES = 64 * 1024 * 1024;
12
+ const MAX_MANIFEST_BYTES = 16 * 1024 * 1024;
10
13
  const HASH = /^[a-f0-9]{64}$/;
11
14
  const UPLOAD_ID = /^[a-f0-9-]{36}$/;
12
15
  const REVISION = /^r-[a-f0-9]{64}$/;
13
16
  const now = () => new Date().toISOString();
14
17
  const errorCode = (error) => error?.code;
15
18
  const sameRef = (a, b) => a.packId === b.packId && a.version === b.version && a.contentSha256 === b.contentSha256;
19
+ const comparePackageVersions = (a, b) => compareReleaseVersions(a, b) ?? a.localeCompare(b);
16
20
  const clone = (value) => structuredClone(value);
17
21
  /** One atomic index is the commit point. Unindexed immutable files are never visible. */
18
22
  export class ContentPackRepository {
19
23
  root;
24
+ #manifestDigests = new WeakMap();
20
25
  constructor(root) { this.root = resolve(root); }
21
26
  async #directory(path) {
22
27
  await fs.mkdir(path, { recursive: true, mode: 0o700 });
@@ -25,7 +30,7 @@ export class ContentPackRepository {
25
30
  }
26
31
  async #prepare() {
27
32
  await this.#directory(this.root);
28
- for (const dir of ['objects', 'revisions', 'uploads', 'tmp'])
33
+ for (const dir of ['objects', 'manifests', 'revisions', 'uploads', 'tmp'])
29
34
  await this.#directory(join(this.root, dir));
30
35
  }
31
36
  async #syncDir(path) {
@@ -220,22 +225,83 @@ export class ContentPackRepository {
220
225
  await fs.rm(stage, { recursive: true, force: true });
221
226
  }
222
227
  }
223
- async #locked(work, write = true) {
224
- await this.#prepare();
225
- const path = join(this.root, '.lock'), token = await this.#lock();
228
+ #manifestPath(ref) {
229
+ if (!ref || !HASH.test(ref.contentSha256))
230
+ fail('CORRUPT_REPOSITORY', 'Invalid manifest identity');
231
+ return join(this.root, 'manifests', ref.contentSha256 + '.json');
232
+ }
233
+ async #readIndex() {
234
+ let disk;
226
235
  try {
227
- let index;
236
+ // Legacy inline manifests can legitimately exceed the old 64 MiB ceiling.
237
+ // The larger bound is permitted only for migration from schema 1.
238
+ const bytes = await this.#readFile(join(this.root, 'index.json'), 4 * MAX_INDEX_BYTES);
239
+ disk = JSON.parse(Buffer.from(bytes).toString('utf8'));
240
+ if (disk.schema !== 1 && (disk.schema !== 2 || bytes.length > MAX_INDEX_BYTES))
241
+ fail('CORRUPT_REPOSITORY', 'Unsupported repository index');
242
+ }
243
+ catch (error) {
244
+ if (errorCode(error) === 'ENOENT')
245
+ return { schema: 1, packs: [], pins: {}, uploads: [] };
246
+ if (error instanceof ContentPackError && error.code === 'UNSAFE_DISK_PATH')
247
+ throw error;
248
+ return fail('CORRUPT_REPOSITORY', 'Cannot read repository index');
249
+ }
250
+ if (!Array.isArray(disk.packs))
251
+ fail('CORRUPT_REPOSITORY', 'Invalid pack index');
252
+ if (disk.schema === 1)
253
+ return disk;
254
+ const packs = [], digests = new Map();
255
+ for (const { manifestSha256, ...pack } of disk.packs) {
256
+ const bytes = await this.#readFile(this.#manifestPath(pack.ref), MAX_MANIFEST_BYTES);
257
+ if (!HASH.test(manifestSha256) || sha256(bytes) !== manifestSha256)
258
+ fail('CORRUPT_REPOSITORY', 'Stored manifest digest mismatch');
259
+ const manifest = parseManifest(JSON.parse(Buffer.from(bytes).toString('utf8')));
260
+ if (manifest.packId !== pack.ref.packId || manifest.version !== pack.ref.version)
261
+ fail('CORRUPT_REPOSITORY', 'Stored manifest identity mismatch');
262
+ packs.push({ ...pack, manifest });
263
+ digests.set(pack.ref.contentSha256, manifestSha256);
264
+ }
265
+ const index = { ...disk, schema: 1, packs };
266
+ this.#manifestDigests.set(index, digests);
267
+ return index;
268
+ }
269
+ async #writeIndex(index) {
270
+ const packs = [], manifests = [];
271
+ for (const { manifest, ...pack } of index.packs) {
272
+ const text = JSON.stringify(manifest);
273
+ if (Buffer.byteLength(text) > MAX_MANIFEST_BYTES)
274
+ fail('INDEX_LIMIT', 'Repository manifest exceeds the supported size');
275
+ const digest = sha256(text);
276
+ packs.push({ ...pack, manifestSha256: digest });
277
+ if (this.#manifestDigests.get(index)?.get(pack.ref.contentSha256) !== digest)
278
+ manifests.push({ path: this.#manifestPath(pack.ref), text, digest });
279
+ }
280
+ const uploads = index.uploads.map(upload => upload.status === 'OPEN' ? upload : { ...upload, chunks: [] });
281
+ const text = JSON.stringify({ ...index, schema: 2, packs, uploads });
282
+ if (Buffer.byteLength(text) > MAX_INDEX_BYTES)
283
+ fail('INDEX_LIMIT', 'Repository metadata exceeds the supported size');
284
+ for (const manifest of manifests) {
228
285
  try {
229
- index = await this.#json(join(this.root, 'index.json'));
286
+ if (sha256(await this.#readFile(manifest.path, MAX_MANIFEST_BYTES)) !== manifest.digest)
287
+ fail('CORRUPT_REPOSITORY', 'Stored manifest digest mismatch');
230
288
  }
231
289
  catch (error) {
232
290
  if (errorCode(error) !== 'ENOENT')
233
291
  throw error;
234
- index = { schema: 1, packs: [], pins: {}, uploads: [] };
292
+ await this.#atomic(manifest.path, manifest.text, 0o400);
235
293
  }
294
+ }
295
+ await this.#atomic(join(this.root, 'index.json'), text);
296
+ }
297
+ async #locked(work, write = true) {
298
+ await this.#prepare();
299
+ const path = join(this.root, '.lock'), token = await this.#lock();
300
+ try {
301
+ const index = await this.#readIndex();
236
302
  if (index.schema !== 1 || !Array.isArray(index.packs) || !Array.isArray(index.uploads) || !index.pins || typeof index.pins !== 'object')
237
303
  fail('CORRUPT_REPOSITORY', 'Unsupported repository index');
238
- if (index.current !== undefined && (!index.current || typeof index.current !== 'object' || Array.isArray(index.current) || Object.entries(index.current).some(([id, ref]) => !/^[a-z0-9][a-z0-9-]{0,79}$/.test(id) || ref !== null && (!ref || ref.packId !== id || !index.packs.some(p => sameRef(p.ref, ref))))))
304
+ if (index.current !== undefined && (!index.current || typeof index.current !== 'object' || Array.isArray(index.current) || Object.entries(index.current).some(([id, ref]) => !/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(id) || ref !== null && (!ref || ref.packId !== id || !index.packs.some(p => sameRef(p.ref, ref))))))
239
305
  fail('CORRUPT_REPOSITORY', 'Invalid current package pointer');
240
306
  if (index.installationRevision !== undefined && (!Number.isSafeInteger(index.installationRevision) || index.installationRevision < 0)
241
307
  || index.removals !== undefined && (!index.removals || typeof index.removals !== 'object' || Array.isArray(index.removals)
@@ -245,8 +311,14 @@ export class ContentPackRepository {
245
311
  fail('CORRUPT_REPOSITORY', 'Invalid task identity index');
246
312
  // Only this repository's transient writes are cleaned while its exclusive lock is held.
247
313
  for (const entry of await fs.readdir(join(this.root, 'tmp'))) {
314
+ const path = join(this.root, 'tmp', entry);
248
315
  if (/^install-[a-f0-9-]{36}$/.test(entry))
249
- await fs.rm(join(this.root, 'tmp', entry), { recursive: true, force: true });
316
+ await fs.rm(path, { recursive: true, force: true });
317
+ else if (/^prepare-[0-9]+-[a-f0-9-]{36}$/.test(entry)) {
318
+ const stat = await fs.lstat(path).catch(e => errorCode(e) === 'ENOENT' ? undefined : Promise.reject(e));
319
+ if (stat && Date.now() - stat.mtimeMs > STAGING_TTL_MS)
320
+ await fs.rm(path, { recursive: true, force: true });
321
+ }
250
322
  }
251
323
  for (const entry of await fs.readdir(this.root)) {
252
324
  if (/^\.write-[a-f0-9-]{36}$/.test(entry))
@@ -256,7 +328,7 @@ export class ContentPackRepository {
256
328
  if (!UPLOAD_ID.test(upload.id))
257
329
  fail('CORRUPT_REPOSITORY', 'Invalid persisted upload id');
258
330
  const dir = join(this.root, 'uploads', upload.id);
259
- if (upload.status !== 'OPEN') {
331
+ if (upload.status !== 'OPEN' && upload.cleanupPending) {
260
332
  try {
261
333
  await fs.rm(dir, { recursive: true, force: true });
262
334
  upload.cleanupPending = false;
@@ -265,7 +337,7 @@ export class ContentPackRepository {
265
337
  upload.cleanupPending = true;
266
338
  }
267
339
  }
268
- else {
340
+ else if (upload.status === 'OPEN') {
269
341
  for (const entry of await fs.readdir(dir))
270
342
  if (/^\.write-[a-f0-9-]{36}$/.test(entry))
271
343
  await fs.rm(join(dir, entry), { force: true });
@@ -273,7 +345,7 @@ export class ContentPackRepository {
273
345
  }
274
346
  const result = await work(index);
275
347
  if (typeof write === 'function' ? write() : write)
276
- await this.#atomic(join(this.root, 'index.json'), JSON.stringify(index));
348
+ await this.#writeIndex(index);
277
349
  return result;
278
350
  }
279
351
  finally {
@@ -325,42 +397,82 @@ export class ContentPackRepository {
325
397
  }
326
398
  await this.#atomic(join(base, path), bytes, 0o400);
327
399
  }
328
- async #install(index, bytes, validator, installationRevision) {
400
+ async #objectIdentity(path) {
401
+ const stat = await fs.lstat(path, { bigint: true });
402
+ if (!stat.isDirectory() || stat.isSymbolicLink())
403
+ fail('UNSAFE_DISK_PATH', 'Stored package is not a real directory');
404
+ return [stat.dev, stat.ino, stat.birthtimeNs, stat.mtimeNs, stat.ctimeNs].join(':');
405
+ }
406
+ async #objectIdentityIfPresent(path) {
407
+ try {
408
+ return await this.#objectIdentity(path);
409
+ }
410
+ catch (error) {
411
+ if (errorCode(error) === 'ENOENT')
412
+ return undefined;
413
+ throw error;
414
+ }
415
+ }
416
+ async #verifyStoredObject(ref, manifest, path, mismatchCode) {
417
+ if (sha256(await this.#readFile(join(path, 'bundle.zip'), MAX_UPLOAD_BYTES)) !== ref.contentSha256)
418
+ fail(mismatchCode, 'Stored package archive changed');
419
+ for (const member of manifest.members) {
420
+ if (sha256(await this.#readFile(join(path, 'members', member.path), member.bytes)) !== member.sha256)
421
+ fail(mismatchCode, 'Stored package member changed');
422
+ }
423
+ }
424
+ async #prepareStoredObject(bundle, removalRevision, mismatchCode) {
425
+ const path = this.#object(bundle.ref);
426
+ const identity = await this.#objectIdentityIfPresent(path);
427
+ if (!identity)
428
+ return undefined;
429
+ await this.#verifyStoredObject(bundle.ref, bundle.manifest, path, mismatchCode);
430
+ if (await this.#objectIdentityIfPresent(path) !== identity)
431
+ fail('INSTALLATION_CHANGED', 'Stored package changed during verification');
432
+ return { identity, removalRevision };
433
+ }
434
+ async #prepareInstall(bytes, validator, hooks) {
329
435
  const bundle = readBundle(bytes);
330
- if (!Number.isSafeInteger(installationRevision) || installationRevision < 0 || installationRevision > (index.installationRevision ?? 0))
331
- fail('INVALID_INSTALL_REVISION', 'Invalid installation revision');
332
- if (installationRevision < (index.removals?.[bundle.ref.contentSha256] ?? 0))
333
- fail('PACK_UNINSTALLED', '该下载或导入开始后,此版本已被卸载或删除;请重新发起安装。');
334
- const existing = index.packs.find(p => p.ref.packId === bundle.ref.packId && p.ref.version === bundle.ref.version);
436
+ hooks?.inspect?.(bundle);
437
+ const { existing, removalRevision } = await this.#locked(async (index) => ({
438
+ existing: index.packs.find(p => p.ref.packId === bundle.ref.packId && p.ref.version === bundle.ref.version),
439
+ removalRevision: index.removals?.[bundle.ref.contentSha256] ?? 0,
440
+ }), false);
335
441
  if (existing) {
336
442
  if (!sameRef(existing.ref, bundle.ref))
337
443
  fail('VERSION_DRIFT', 'Same package/version already contains different bytes');
338
444
  if (existing.deletionPending)
339
445
  fail('PACK_DELETE_INCOMPLETE', '本地文件仍待清理,请先重试删除,再重新安装。');
340
- if (existing.uninstalled) {
341
- // An explicit reinstall restores selection, never replaces a task's fixed revision.
342
- if (sha256(await this.#readFile(join(this.#object(existing.ref), 'bundle.zip'), MAX_UPLOAD_BYTES)) !== existing.ref.contentSha256)
343
- fail('CHECKSUM_MISMATCH', 'Retained package archive changed');
344
- for (const member of existing.manifest.members) {
345
- if (sha256(await this.#readFile(join(this.#object(existing.ref), 'members', member.path), member.bytes)) !== member.sha256)
346
- fail('CHECKSUM_MISMATCH', 'Retained package member changed');
347
- }
348
- delete existing.uninstalled;
349
- existing.disabled = false;
446
+ if (!existing.uninstalled)
447
+ return { bundle };
448
+ try {
449
+ const stored = await this.#prepareStoredObject(bundle, removalRevision, 'CHECKSUM_MISMATCH');
450
+ if (!stored)
451
+ fail('INSTALLATION_CHANGED', 'Retained package is missing');
452
+ return { bundle, stored };
453
+ }
454
+ catch (error) {
455
+ const removed = await this.#locked(async (index) => removalRevision !== (index.removals?.[bundle.ref.contentSha256] ?? 0), false);
456
+ if (removed)
457
+ fail('PACK_UNINSTALLED', '校验期间此版本已被卸载或删除,请重新发起安装。');
458
+ throw error;
350
459
  }
351
- // A retry after an upload response was lost never resets disabled state or revisions.
352
- return clone(existing);
353
460
  }
354
461
  const validation = await validateBundle(bundle, validator);
355
462
  if (!validation.valid) {
356
463
  const diagnostics = normalizePackDiagnostics(validation.diagnostics);
357
464
  fail('VALIDATION_FAILED', formatPackDiagnostics(diagnostics), diagnostics);
358
465
  }
359
- // Callback/caller mutation must not install bytes different from those verified.
360
466
  checkBundleMembers(bundle.manifest, bundle.members);
361
467
  if (sha256(bytes) !== bundle.ref.contentSha256)
362
468
  fail('CONTENT_DRIFT', 'Archive changed during validation');
363
- const stage = join(this.root, 'tmp', `install-${randomUUID()}`);
469
+ const semanticDigest = semanticSha256(bundle);
470
+ // An interrupted index commit may already have published these bytes.
471
+ // Read and hash that immutable directory before acquiring the index lock.
472
+ const stored = await this.#prepareStoredObject(bundle, removalRevision, 'CORRUPT_REPOSITORY');
473
+ if (stored)
474
+ return { bundle, semanticDigest, stored };
475
+ const stage = join(this.root, 'tmp', `prepare-${process.pid}-${randomUUID()}`);
364
476
  await this.#directory(stage);
365
477
  try {
366
478
  await this.#atomic(join(stage, 'bundle.zip'), bytes, 0o400);
@@ -368,36 +480,77 @@ export class ContentPackRepository {
368
480
  await this.#directory(membersDir);
369
481
  for (const [path, content] of bundle.members)
370
482
  await this.#writeMember(membersDir, path, content);
371
- const destination = this.#object(bundle.ref);
483
+ return { bundle, stage, semanticDigest };
484
+ }
485
+ catch (error) {
486
+ await fs.rm(stage, { recursive: true, force: true }).catch(() => { });
487
+ throw error;
488
+ }
489
+ }
490
+ async #discardInstall(prepared) {
491
+ if (prepared?.stage)
492
+ await fs.rm(prepared.stage, { recursive: true, force: true }).catch(() => { });
493
+ }
494
+ async #installAndSelect(index, prepared, revision) {
495
+ const previous = this.#current(index, prepared.bundle.ref.packId);
496
+ const installed = await this.#install(index, prepared, revision);
497
+ const next = this.#pack(index, installed.ref);
498
+ const advance = !previous || comparePackageVersions(next.ref.version, previous.ref.version) > 0;
499
+ if (advance && previous && !previous.uninstalled)
500
+ next.disabled = previous.disabled;
501
+ this.#setCurrent(index, next.ref.packId, advance ? next.ref : previous.ref);
502
+ return clone(next);
503
+ }
504
+ async #install(index, prepared, installationRevision) {
505
+ const { bundle, stage, semanticDigest, stored } = prepared;
506
+ const removalRevision = index.removals?.[bundle.ref.contentSha256] ?? 0;
507
+ const destination = this.#object(bundle.ref);
508
+ if (!Number.isSafeInteger(installationRevision) || installationRevision < 0 || installationRevision > (index.installationRevision ?? 0))
509
+ fail('INVALID_INSTALL_REVISION', 'Invalid installation revision');
510
+ if (installationRevision < removalRevision)
511
+ fail('PACK_UNINSTALLED', '该下载或导入开始后,此版本已被卸载或删除;请重新发起安装。');
512
+ const existing = index.packs.find(p => p.ref.packId === bundle.ref.packId && p.ref.version === bundle.ref.version);
513
+ if (existing) {
514
+ if (!sameRef(existing.ref, bundle.ref))
515
+ fail('VERSION_DRIFT', 'Same package/version already contains different bytes');
516
+ if (existing.deletionPending)
517
+ fail('PACK_DELETE_INCOMPLETE', '本地文件仍待清理,请先重试删除,再重新安装。');
518
+ if (existing.uninstalled) {
519
+ // The hash reads completed outside the repository lock. The removal
520
+ // revision and directory identity fence that exact immutable object.
521
+ if (!stored || stored.removalRevision !== removalRevision ||
522
+ await this.#objectIdentityIfPresent(destination) !== stored.identity)
523
+ fail('INSTALLATION_CHANGED', 'Retained object changed before commit');
524
+ delete existing.uninstalled;
525
+ existing.disabled = false;
526
+ }
527
+ // A retry after an upload response was lost never resets disabled state or revisions.
528
+ return clone(existing);
529
+ }
530
+ if (!semanticDigest)
531
+ fail('INSTALLATION_CHANGED', 'Package installation changed while preparing import');
532
+ if (stored) {
533
+ if (stored.removalRevision !== removalRevision || await this.#objectIdentityIfPresent(destination) !== stored.identity)
534
+ fail('INSTALLATION_CHANGED', 'Unindexed package changed before commit');
535
+ }
536
+ else {
537
+ if (!stage)
538
+ fail('INSTALLATION_CHANGED', 'Package installation changed while preparing import');
372
539
  try {
373
540
  await this.#rename(stage, destination);
374
541
  }
375
542
  catch (error) {
376
543
  if (!['ENOTEMPTY', 'EEXIST'].includes(errorCode(error) ?? '') && !(process.platform === 'win32' && errorCode(error) === 'EPERM'))
377
544
  throw error;
378
- let target;
379
- try {
380
- target = await fs.lstat(destination);
381
- }
382
- catch {
545
+ if (!await this.#objectIdentityIfPresent(destination))
383
546
  throw error;
384
- }
385
- if (!target.isDirectory() || target.isSymbolicLink())
386
- fail('UNSAFE_DISK_PATH', 'Orphan object is not a real directory');
387
- // Recover a fully-written but unindexed object from a previous interrupted commit.
388
- if (sha256(await this.#readFile(join(destination, 'bundle.zip'), MAX_UPLOAD_BYTES)) !== bundle.ref.contentSha256)
389
- fail('CORRUPT_REPOSITORY', 'Orphan object hash mismatch');
390
- for (const member of bundle.manifest.members) {
391
- if (sha256(await this.#readFile(join(destination, 'members', member.path), member.bytes)) !== member.sha256)
392
- fail('CORRUPT_REPOSITORY', 'Orphan member hash mismatch');
393
- }
547
+ // A concurrent interrupted install published this object after we
548
+ // prepared. A fresh attempt can verify it without holding this lock.
549
+ fail('INSTALLATION_CHANGED', 'An unverified package appeared before commit; retry installation');
394
550
  }
395
- await this.#syncDir(join(this.root, 'objects'));
396
- }
397
- finally {
398
- await fs.rm(stage, { recursive: true, force: true });
399
551
  }
400
- const initial = { revision: 'original', semanticSha256: semanticSha256(bundle), valid: true, issues: [], createdAt: now() };
552
+ await this.#syncDir(join(this.root, 'objects'));
553
+ const initial = { revision: 'original', semanticSha256: semanticDigest, valid: true, issues: [], createdAt: now() };
401
554
  const pack = { ref: bundle.ref, manifest: bundle.manifest, disabled: false, defaultRevision: 'original', revisions: [initial] };
402
555
  index.packs.push(pack);
403
556
  return clone(pack);
@@ -405,40 +558,55 @@ export class ContentPackRepository {
405
558
  async installationRevision() {
406
559
  return this.#locked(async (index) => index.installationRevision ?? 0, false);
407
560
  }
408
- async install(bytes, validator, installationRevision) {
409
- return this.#locked(async (index) => {
410
- const pack = await this.#install(index, bytes, validator, installationRevision ?? index.installationRevision ?? 0), current = index.current?.[pack.ref.packId];
411
- if (index.current && Object.hasOwn(index.current, pack.ref.packId) && (!current || (compareReleaseVersions(pack.ref.version, current.version) ?? -1) > 0))
412
- index.current[pack.ref.packId] = clone(pack.ref);
413
- return pack;
414
- });
561
+ async install(bytes, validator, installationRevision, hooks) {
562
+ const revision = installationRevision ?? await this.installationRevision();
563
+ const prepared = await this.#prepareInstall(bytes, validator, hooks);
564
+ try {
565
+ await hooks?.beforeCommit?.();
566
+ return await this.#locked(index => this.#installAndSelect(index, prepared, revision));
567
+ }
568
+ finally {
569
+ await this.#discardInstall(prepared);
570
+ }
415
571
  }
416
572
  async list() { return this.#locked(async (index) => clone(index.packs), false); }
573
+ #setCurrent(index, id, ref) {
574
+ (index.current ??= Object.create(null))[id] = ref ? clone(ref) : null;
575
+ }
417
576
  #current(index, id) {
418
577
  if (index.current && Object.hasOwn(index.current, id)) {
419
578
  const ref = index.current[id];
420
579
  return ref ? this.#pack(index, ref) : undefined;
421
580
  }
422
- const all = index.packs.filter(p => p.ref.packId === id), installed = all.filter(p => !p.uninstalled);
423
- return (installed.length ? installed : all).sort((a, b) => (compareReleaseVersions(b.ref.version, a.ref.version) ?? b.ref.version.localeCompare(a.ref.version)))[0];
581
+ // Older indexes did not record the current version. Infer it from the
582
+ // version order, even if that version was previously uninstalled.
583
+ return index.packs.filter(p => p.ref.packId === id).sort((a, b) => comparePackageVersions(b.ref.version, a.ref.version))[0];
424
584
  }
425
585
  async listCurrent() {
426
586
  return this.#locked(async (index) => [...new Set(index.packs.map(p => p.ref.packId))].flatMap(id => { const p = this.#current(index, id); return p ? [clone(p)] : []; }), false);
427
587
  }
428
588
  /** Validate and switch one logical version in the same atomic index commit, retaining old pins and edits. */
429
- async update(from, bytes, validator, installationRevision) {
430
- return this.#locked(async (index) => {
431
- const old = this.#pack(index, from), current = this.#current(index, from.packId), bundle = readBundle(bytes);
432
- if (!current || !sameRef(current.ref, from) || old.uninstalled || old.deletionPending)
433
- fail('UPDATE_CONFLICT', '当前模板包已变化,请刷新后重新更新。');
434
- if (bundle.ref.packId !== from.packId || (compareReleaseVersions(bundle.ref.version, from.version) ?? -1) <= 0)
435
- fail('UPDATE_VERSION_INVALID', '更新包必须是同一资料包的较新版本。');
436
- const installed = await this.#install(index, bytes, validator, installationRevision ?? index.installationRevision ?? 0);
437
- const next = this.#pack(index, installed.ref);
438
- next.disabled = old.disabled;
439
- (index.current ??= Object.create(null))[from.packId] = clone(next.ref);
440
- return clone(next);
441
- });
589
+ async update(from, bytes, validator, installationRevision, hooks) {
590
+ const revision = installationRevision ?? await this.installationRevision();
591
+ const prepared = await this.#prepareInstall(bytes, validator, hooks);
592
+ try {
593
+ await hooks?.beforeCommit?.();
594
+ return await this.#locked(async (index) => {
595
+ const old = this.#pack(index, from), current = this.#current(index, from.packId), { bundle } = prepared;
596
+ if (!current || !sameRef(current.ref, from) || old.uninstalled || old.deletionPending)
597
+ fail('UPDATE_CONFLICT', '当前模板包已变化,请刷新后重新更新。');
598
+ if (bundle.ref.packId !== from.packId || (compareReleaseVersions(bundle.ref.version, from.version) ?? -1) <= 0)
599
+ fail('UPDATE_VERSION_INVALID', '更新包必须是同一资料包的较新版本。');
600
+ const installed = await this.#install(index, prepared, revision);
601
+ const next = this.#pack(index, installed.ref);
602
+ next.disabled = old.disabled;
603
+ this.#setCurrent(index, from.packId, next.ref);
604
+ return clone(next);
605
+ });
606
+ }
607
+ finally {
608
+ await this.#discardInstall(prepared);
609
+ }
442
610
  }
443
611
  async listTasks() {
444
612
  return this.#locked(async (index) => Object.entries(index.taskIds ?? {}).map(([key, taskId]) => ({ taskId, context: clone(index.pins[key]) })), false);
@@ -487,8 +655,8 @@ export class ContentPackRepository {
487
655
  }
488
656
  async readMember(ref, path, revision = 'original') {
489
657
  return this.#locked(async (index) => {
490
- const pack = this.#pack(index, ref);
491
- return this.#member(pack, path, await this.#revision(pack, revision));
658
+ const pack = this.#pack(index, ref), view = await this.#view(pack, revision, new Set());
659
+ return view.readMember(path);
492
660
  }, false);
493
661
  }
494
662
  async #view(pack, revision, paths) {
@@ -496,24 +664,25 @@ export class ContentPackRepository {
496
664
  const manifest = clone(pack.manifest);
497
665
  if (file)
498
666
  manifest.sources = clone(file.sources);
667
+ const linked = new Set(manifest.sources.flatMap(source => source.path ? [source.path] : []));
668
+ const descriptors = new Map(manifest.members.map(member => [member.path, member]));
499
669
  for (const edit of file?.members ?? []) {
500
- if (manifest.members.some(m => m.path === edit.path))
501
- continue;
502
- const match = USER_SOURCE_PATH.exec(edit.path);
503
- if (!match)
670
+ if (!descriptors.has(edit.path) && !USER_SOURCE_PATH.test(edit.path))
504
671
  fail('CORRUPT_REPOSITORY', 'Unknown revision member is not a user source');
505
- manifest.members.push({ path: edit.path, sha256: match[1], bytes: Buffer.byteLength(edit.base64, 'base64') });
672
+ const bytes = Buffer.from(edit.base64, 'base64');
673
+ descriptors.set(edit.path, { path: edit.path, bytes: bytes.length, sha256: sha256(bytes) });
506
674
  }
675
+ manifest.members = [...descriptors.values()].filter(member => !file || !member.path.startsWith('user-sources/') || linked.has(member.path));
507
676
  const members = new Map();
508
677
  for (const member of manifest.members) {
509
678
  if (!paths || paths.has(member.path))
510
679
  members.set(member.path, await this.#member(pack, member.path, file));
511
680
  }
512
- manifest.members = manifest.members.map(m => {
513
- const bytes = members.get(m.path);
514
- return bytes ? { path: m.path, bytes: bytes.length, sha256: sha256(bytes) } : m;
515
- });
516
- return { manifest, members, readMember: async (path) => this.#member(pack, path, file) };
681
+ return { manifest, members, readMember: async (path) => {
682
+ if (!manifest.members.some(member => member.path === path))
683
+ fail('MEMBER_NOT_FOUND', `Unknown member: ${path}`);
684
+ return this.#member(pack, path, file);
685
+ } };
517
686
  }
518
687
  async saveRevision(ref, edits, expectedRevision, validator) {
519
688
  return this.#locked(async (index) => {
@@ -552,6 +721,10 @@ export class ContentPackRepository {
552
721
  fail('EDIT_LIMIT', 'Revision edit size exceeds limit');
553
722
  cumulative.set(path, Buffer.from(bytes).toString('base64'));
554
723
  }
724
+ const linked = new Set(sources.flatMap(source => source.path ? [source.path] : []));
725
+ for (const path of cumulative.keys())
726
+ if (path.startsWith('user-sources/') && !linked.has(path))
727
+ cumulative.delete(path);
555
728
  const payload = { sources, members: [...cumulative].sort(([a], [b]) => a.localeCompare(b)).map(([path, base64]) => ({ path, base64 })) };
556
729
  if (Buffer.byteLength(JSON.stringify(payload)) > 96 * 1024 * 1024)
557
730
  fail('EDIT_LIMIT', 'Cumulative revision exceeds limit');
@@ -566,6 +739,13 @@ export class ContentPackRepository {
566
739
  for (const [path, bytes] of Object.entries(edits.members ?? {}))
567
740
  view.members.set(path, new Uint8Array(bytes));
568
741
  view.manifest.sources = sources;
742
+ for (const key of view.members.keys())
743
+ if (key.startsWith('user-sources/') && !linked.has(key))
744
+ view.members.delete(key);
745
+ // A previously unlinked immutable attachment can be selected again.
746
+ for (const path of linked)
747
+ if (!view.members.has(path) && pack.manifest.members.some(member => member.path === path))
748
+ view.members.set(path, await this.#member(pack, path, previous));
569
749
  view.manifest.members = [...view.members].map(([path, b]) => ({ path, bytes: b.length, sha256: sha256(b) }));
570
750
  let valid = false, issues = [];
571
751
  if (typeof validator !== 'function')
@@ -602,11 +782,11 @@ export class ContentPackRepository {
602
782
  fail('INVALID_TASK', 'Invalid task id');
603
783
  return sha256(taskId);
604
784
  }
605
- async #taskView(pack, revision, ids) {
785
+ async #taskView(pack, revision, ids, loadMembers = true) {
606
786
  const templates = pack.manifest.templates.filter(t => ids.includes(t.id));
607
787
  const paths = new Set([pack.manifest.core.turtlePath, pack.manifest.core.shapesPath,
608
788
  ...templates.flatMap(t => [t.path, t.mappingPath, t.semantic.turtlePath, t.semantic.shapesPath])]);
609
- const view = await this.#view(pack, revision, paths);
789
+ const view = await this.#view(pack, revision, loadMembers ? paths : new Set());
610
790
  view.manifest.templates = view.manifest.templates.filter(t => ids.includes(t.id));
611
791
  const sourcePaths = new Set(view.manifest.sources.flatMap(source => source.path ? [source.path] : []));
612
792
  // Keep reference hashes available without loading large source-document bytes.
@@ -629,18 +809,33 @@ export class ContentPackRepository {
629
809
  const projectPinned = !!project && !!pin && sameRef(pin.ref, ref) && pin.revision === project.revision;
630
810
  if (project && !projectPinned)
631
811
  fail('TASK_PIN_CONFLICT', 'Project package selection changed');
632
- const current = index.current?.[ref.packId];
633
- if (!projectPinned && index.current && Object.hasOwn(index.current, ref.packId) && (!current || !sameRef(current, ref)))
812
+ const current = this.#current(index, ref.packId);
813
+ if (!projectPinned && (!current || !sameRef(current.ref, ref)))
634
814
  fail('UPDATE_CONFLICT', '此模板包已有当前版本,请刷新后使用新版创建任务。');
635
815
  if (pack.disabled)
636
816
  fail('PACK_DISABLED', 'Disabled package cannot be selected for new tasks');
637
817
  const revision = project?.revision ?? pack.defaultRevision;
638
- const view = await this.#taskView(pack, revision, ids);
818
+ const view = await this.#taskView(pack, revision, ids, false);
639
819
  const context = { ...clone(ref), revision, semanticSha256: semanticSha256(view), templateIds: ids };
640
820
  index.pins[key] = context;
641
821
  return clone(context);
642
822
  });
643
823
  }
824
+ /** Remove only an unpublished task created by the same project preparation. */
825
+ async discardUnpublishedProjectPin(taskId, ref, revision, templateId) {
826
+ const key = this.#taskKey(taskId);
827
+ let removed = false;
828
+ return this.#locked(async (index) => {
829
+ const context = index.pins[key];
830
+ if (!context || index.taskIds?.[key] !== taskId || !sameRef(context, ref) || context.revision !== revision ||
831
+ context.templateIds.length !== 1 || context.templateIds[0] !== templateId)
832
+ return false;
833
+ delete index.pins[key];
834
+ delete index.taskIds[key];
835
+ removed = true;
836
+ return true;
837
+ }, () => removed);
838
+ }
644
839
  async resolveTask(taskId, templateIds) {
645
840
  let migrated = false;
646
841
  return this.#locked(async (index) => {
@@ -702,13 +897,20 @@ export class ContentPackRepository {
702
897
  return { installed: !pack.uninstalled, deletionPending: Boolean(pack.deletionPending), taskReferences, revision: pack.defaultRevision };
703
898
  }
704
899
  async removalInfo(ref) {
705
- return this.#locked(async (index) => this.#removalInfo(index, this.#pack(index, ref)), false);
900
+ return withProjectPackageLock(this.root, async () => {
901
+ const referencedByProject = await projectPackageReferenced(this.root, 'template', ref);
902
+ return this.#locked(async (index) => ({ ...this.#removalInfo(index, this.#pack(index, ref)), referencedByProject }), false);
903
+ });
706
904
  }
707
905
  async uninstall(ref) {
708
906
  return this.#locked(async (index) => {
709
907
  const pack = this.#pack(index, ref);
710
- if (!pack.uninstalled)
908
+ if (!pack.uninstalled) {
909
+ const current = this.#current(index, ref.packId);
910
+ if (current && sameRef(current.ref, ref))
911
+ this.#setCurrent(index, ref.packId, ref);
711
912
  this.#recordRemoval(index, ref);
913
+ }
712
914
  pack.uninstalled = true;
713
915
  pack.disabled = true;
714
916
  return this.#removalInfo(index, pack);
@@ -722,9 +924,9 @@ export class ContentPackRepository {
722
924
  (index.removals ??= Object.create(null))[ref.contentSha256] = revision;
723
925
  }
724
926
  async deletePack(ref, expectedRevision) {
725
- return this.#locked(async (index) => {
927
+ return withProjectPackageLock(this.root, () => this.#locked(async (index) => {
726
928
  const pack = this.#pack(index, ref);
727
- if (this.#removalInfo(index, pack).taskReferences)
929
+ if (this.#removalInfo(index, pack).taskReferences || await projectPackageReferenced(this.root, 'template', ref))
728
930
  fail('PACK_IN_USE', '该版本仍被历史任务引用,无法删除;可卸载并保留任务所需文件。');
729
931
  if (pack.defaultRevision !== expectedRevision)
730
932
  fail('REVISION_CONFLICT', '模板包修订已变化,请刷新后重新确认删除。');
@@ -749,26 +951,31 @@ export class ContentPackRepository {
749
951
  }
750
952
  // Durable intent precedes irreversible file removal. Failed/interrupted cleanup stays
751
953
  // visible as non-selectable metadata and can be retried after a process restart.
954
+ const current = this.#current(index, ref.packId);
955
+ const wasCurrent = Boolean(current && sameRef(current.ref, ref));
956
+ if (wasCurrent)
957
+ this.#setCurrent(index, ref.packId, ref);
752
958
  if (!pack.deletionPending)
753
959
  this.#recordRemoval(index, ref);
754
960
  pack.uninstalled = true;
755
961
  pack.disabled = true;
756
962
  pack.deletionPending = true;
757
- await this.#atomic(join(this.root, 'index.json'), JSON.stringify(index));
963
+ await this.#writeIndex(index);
758
964
  try {
759
965
  for (const path of paths)
760
966
  await fs.rm(path, { recursive: true, force: true, maxRetries: 3, retryDelay: 50 });
761
967
  await this.#syncDir(join(this.root, 'objects'));
762
968
  await this.#syncDir(join(this.root, 'revisions'));
763
969
  index.packs = index.packs.filter(item => item !== pack);
764
- if (index.current?.[ref.packId] && sameRef(index.current[ref.packId], ref))
765
- index.current[ref.packId] = null;
766
- await this.#atomic(join(this.root, 'index.json'), JSON.stringify(index));
970
+ if (wasCurrent)
971
+ this.#setCurrent(index, ref.packId, null);
972
+ await this.#writeIndex(index);
973
+ await fs.rm(this.#manifestPath(ref), { force: true }).catch(() => { });
767
974
  }
768
975
  catch {
769
976
  fail('PACK_DELETE_INCOMPLETE', '删除未完成:本地文件仍待清理。该版本已退出可选模板库,请重试删除。');
770
977
  }
771
- }, false);
978
+ }, false));
772
979
  }
773
980
  async export(ref, revision) {
774
981
  return this.#locked(async (index) => {
@@ -803,6 +1010,25 @@ export class ContentPackRepository {
803
1010
  fail('UPLOAD_CLEANUP_PENDING', 'Closed upload temporary files require cleanup before accepting more bytes');
804
1011
  if (!options || !Number.isSafeInteger(options.bytes) || options.bytes <= 0 || options.bytes > MAX_UPLOAD_BYTES || options.sha256 !== undefined && !HASH.test(options.sha256))
805
1012
  fail('UPLOAD_LIMIT', 'Upload must declare at most 512 MiB and an optional SHA-256');
1013
+ const expired = [];
1014
+ for (const upload of index.uploads) {
1015
+ if (upload.status !== 'OPEN')
1016
+ continue;
1017
+ const dir = join(this.root, 'uploads', upload.id), lastActivity = (await fs.lstat(dir)).mtimeMs;
1018
+ if (Date.now() - lastActivity <= STAGING_TTL_MS)
1019
+ continue;
1020
+ upload.status = 'CANCELLED';
1021
+ upload.chunks = [];
1022
+ upload.cleanupPending = true;
1023
+ expired.push(upload);
1024
+ }
1025
+ if (expired.length) {
1026
+ await this.#writeIndex(index); // Terminal status is durable before reclaiming chunks.
1027
+ for (const upload of expired) {
1028
+ await fs.rm(join(this.root, 'uploads', upload.id), { recursive: true, force: true });
1029
+ upload.cleanupPending = false;
1030
+ }
1031
+ }
806
1032
  if (index.uploads.filter(u => u.status === 'OPEN').length >= 8 || index.uploads.filter(u => u.status === 'OPEN').reduce((n, u) => n + u.bytes, options.bytes) > 2 * 1024 ** 3)
807
1033
  fail('UPLOAD_LIMIT', 'Too many pending uploads');
808
1034
  const upload = { id: randomUUID(), bytes: options.bytes, receivedBytes: 0, chunks: [], status: 'OPEN', installationRevision: index.installationRevision ?? 0 };
@@ -820,7 +1046,7 @@ export class ContentPackRepository {
820
1046
  }, false);
821
1047
  }
822
1048
  async uploadList() {
823
- return this.#locked(async (index) => index.uploads.slice(-20).reverse().map(upload => {
1049
+ return this.#locked(async (index) => [...index.uploads.filter(upload => upload.status === 'OPEN').reverse(), ...index.uploads.filter(upload => upload.status !== 'OPEN').slice(-20).reverse()].map(upload => {
824
1050
  const { chunks, ...record } = upload;
825
1051
  return clone({ ...record, ...(record.failure ? { failure: packFailure(record.failure) } : {}) });
826
1052
  }), false);
@@ -846,8 +1072,20 @@ export class ContentPackRepository {
846
1072
  return bytes;
847
1073
  }
848
1074
  /** Read a fully received OPEN upload as verified raw bytes without installing or closing it. */
849
- async uploadRead(id) {
850
- return this.#locked(index => this.#readUpload(this.#upload(index, id)), false);
1075
+ async uploadRead(id, maxBytes = MAX_UPLOAD_BYTES) {
1076
+ return this.#locked(index => {
1077
+ const upload = this.#upload(index, id);
1078
+ if (upload.bytes > maxBytes)
1079
+ fail('UPLOAD_LIMIT', 'Requested upload exceeds material size limit');
1080
+ return this.#readUpload(upload);
1081
+ }, false);
1082
+ }
1083
+ #withUploadOperation(id, work) {
1084
+ if (!UPLOAD_ID.test(id))
1085
+ fail('INVALID_UPLOAD', 'Invalid upload id');
1086
+ // Bound retained lock directories while keeping validation outside the
1087
+ // package repository lock. Cancellation uses the index commit separately.
1088
+ return withProjectPackageLock(join(this.root, 'upload-operations', id.slice(0, 2)), work);
851
1089
  }
852
1090
  async uploadAppend(id, offset, bytes) {
853
1091
  return this.#locked(async (index) => {
@@ -875,39 +1113,64 @@ export class ContentPackRepository {
875
1113
  });
876
1114
  }
877
1115
  async uploadComplete(id, validator) {
878
- const result = await this.#locked(async (index) => {
879
- const upload = this.#upload(index, id);
880
- if (upload.status === 'COMPLETED' && upload.result)
881
- return { pack: clone(this.#pack(index, upload.result)) };
882
- if (upload.status === 'FAILED' && upload.failure)
883
- return { failure: packFailure(upload.failure) };
884
- if (upload.status !== 'OPEN')
885
- fail('UPLOAD_CLOSED', 'Upload is not open');
886
- // A resumable upload is not a completed validation attempt yet.
887
- if (upload.receivedBytes !== upload.bytes)
888
- fail('UPLOAD_INCOMPLETE', 'Upload is incomplete');
1116
+ return this.#withUploadOperation(id, () => this.#completeUpload(id, validator));
1117
+ }
1118
+ async #completeUpload(id, validator) {
1119
+ const start = await this.uploadGet(id);
1120
+ let prepared, failure;
1121
+ if (start.status === 'OPEN' && start.receivedBytes === start.bytes) {
889
1122
  try {
890
- const bytes = await this.#readUpload(upload);
891
- const pack = await this.#install(index, bytes, validator, upload.installationRevision ?? 0);
892
- upload.status = 'COMPLETED';
893
- upload.result = clone(pack.ref);
894
- upload.cleanupPending = true;
895
- return { pack };
1123
+ prepared = await this.#prepareInstall(await this.#readUpload(start), validator);
896
1124
  }
897
1125
  catch (error) {
898
- upload.status = 'FAILED';
899
- upload.failure = packFailure(error);
900
- upload.cleanupPending = true;
901
- return { failure: clone(upload.failure) };
1126
+ failure = packFailure(error);
902
1127
  }
903
- });
904
- // Never turn a durable failure/completion into cancellation because cleanup failed.
1128
+ }
1129
+ let result;
1130
+ try {
1131
+ result = await this.#locked(async (index) => {
1132
+ const upload = this.#upload(index, id);
1133
+ if (upload.status === 'COMPLETED' && upload.result)
1134
+ return { pack: clone(this.#pack(index, upload.result)) };
1135
+ if (upload.status === 'FAILED' && upload.failure)
1136
+ return { failure: packFailure(upload.failure) };
1137
+ if (upload.status !== 'OPEN')
1138
+ fail('UPLOAD_CLOSED', 'Upload is not open');
1139
+ if (!prepared && !failure)
1140
+ fail('UPLOAD_INCOMPLETE', 'Upload is incomplete');
1141
+ try {
1142
+ if (failure)
1143
+ throw new ContentPackError(failure.code, formatPackDiagnostics(failure.diagnostics), failure.diagnostics);
1144
+ if (!prepared)
1145
+ fail('UPLOAD_INCOMPLETE', 'Upload is incomplete');
1146
+ const pack = await this.#installAndSelect(index, prepared, upload.installationRevision ?? 0);
1147
+ upload.status = 'COMPLETED';
1148
+ upload.result = clone(pack.ref);
1149
+ upload.chunks = [];
1150
+ upload.cleanupPending = true;
1151
+ return { pack };
1152
+ }
1153
+ catch (error) {
1154
+ upload.status = 'FAILED';
1155
+ upload.failure = packFailure(error);
1156
+ upload.chunks = [];
1157
+ upload.cleanupPending = true;
1158
+ return { failure: clone(upload.failure) };
1159
+ }
1160
+ });
1161
+ }
1162
+ finally {
1163
+ await this.#discardInstall(prepared);
1164
+ }
905
1165
  await this.uploadCleanup(id).catch(() => { });
906
1166
  if (result.failure)
907
1167
  throw new ContentPackError(result.failure.code, formatPackDiagnostics(result.failure.diagnostics), result.failure.diagnostics);
908
1168
  return result.pack;
909
1169
  }
910
1170
  async uploadFail(id) {
1171
+ return this.#withUploadOperation(id, () => this.#uploadFail(id));
1172
+ }
1173
+ async #uploadFail(id) {
911
1174
  await this.#locked(async (index) => {
912
1175
  const upload = this.#upload(index, id);
913
1176
  if (upload.status === 'OPEN') {
@@ -931,12 +1194,15 @@ export class ContentPackRepository {
931
1194
  return upload;
932
1195
  }
933
1196
  async uploadCancel(id) {
1197
+ // An OPEN upload can still be validating without the index lock. Close it
1198
+ // here so the validator cannot publish after cancellation commits.
934
1199
  await this.#locked(async (index) => {
935
1200
  const upload = this.#upload(index, id);
936
1201
  if (upload.status === 'COMPLETED')
937
1202
  fail('UPLOAD_CLOSED', 'Completed uploads cannot be cancelled');
938
1203
  if (upload.status === 'OPEN') {
939
1204
  upload.status = 'CANCELLED';
1205
+ upload.chunks = [];
940
1206
  upload.cleanupPending = true;
941
1207
  }
942
1208
  });