@xingular/harness 0.1.6 → 2.0.0

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 (131) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/NOTICE.md +38 -0
  3. package/README.md +139 -28
  4. package/dist/cli.js +9598 -2201
  5. package/dist/terminal-pi.js +808 -16
  6. package/docs/README.md +15 -0
  7. package/docs/decisions/ADR-0001-owned-terminal.md +87 -0
  8. package/docs/decisions/ADR-0002-native-subagent-lifecycle.md +49 -0
  9. package/docs/decisions/ADR-0003-shared-asynchronous-subagents.md +68 -0
  10. package/docs/decisions/ADR-0004-fullscreen-terminal-layout.md +49 -0
  11. package/docs/decisions/ADR-0005-app-provided-oauth-clients.md +54 -0
  12. package/docs/terminal-tui.md +344 -0
  13. package/package.json +11 -4
  14. package/src/accounts/accounts.test.ts +197 -1
  15. package/src/accounts/accounts.ts +266 -28
  16. package/src/cli.ts +7 -3
  17. package/src/core/bus.test.ts +22 -0
  18. package/src/core/bus.ts +17 -2
  19. package/src/harness.ts +27 -4
  20. package/src/hub/catalog.json +123 -0
  21. package/src/hub/cli.ts +59 -0
  22. package/src/hub/hub.test.ts +205 -0
  23. package/src/hub/hub.ts +256 -0
  24. package/src/hub/schema.ts +81 -0
  25. package/src/index.ts +12 -2
  26. package/src/mcp/connections.ts +39 -12
  27. package/src/mcp/service.test.ts +49 -3
  28. package/src/mcp/service.ts +14 -3
  29. package/src/mcp/types.ts +10 -0
  30. package/src/memory/api.ts +16 -2
  31. package/src/memory/blocks.ts +3 -1
  32. package/src/memory/caller.ts +13 -1
  33. package/src/memory/curation.test.ts +206 -0
  34. package/src/memory/curation.ts +64 -0
  35. package/src/memory/dispatch.ts +5 -0
  36. package/src/memory/maintenance.test.ts +17 -1
  37. package/src/memory/maintenance.ts +367 -18
  38. package/src/memory/operations.ts +12 -0
  39. package/src/memory/proposals.ts +16 -8
  40. package/src/memory/recall.test.ts +11 -0
  41. package/src/memory/recall.ts +11 -3
  42. package/src/memory/trajectory.test.ts +178 -0
  43. package/src/memory/trajectory.ts +220 -0
  44. package/src/memory/wiki.ts +2 -1
  45. package/src/memory/wikiIndex.ts +3 -2
  46. package/src/plugins/builtin.ts +4 -2
  47. package/src/plugins/catalogue.test.ts +1 -1
  48. package/src/server/methods.ts +43 -2
  49. package/src/server/server.test.ts +20 -1
  50. package/src/sessions/approvals.test.ts +49 -0
  51. package/src/sessions/approvals.ts +26 -4
  52. package/src/sessions/contracts.ts +29 -3
  53. package/src/sessions/conversation.test.ts +83 -0
  54. package/src/sessions/lineageJoin.ts +1 -1
  55. package/src/sessions/policy.ts +4 -0
  56. package/src/sessions/questionBatches.test.ts +110 -0
  57. package/src/sessions/registry.ts +145 -27
  58. package/src/sessions/runStore.ts +14 -0
  59. package/src/sessions/runner.ts +45 -4
  60. package/src/sessions/sessions.test.ts +137 -4
  61. package/src/sessions/sessions.ts +51 -15
  62. package/src/sessions/subagentService.test.ts +159 -0
  63. package/src/sessions/subagentService.ts +236 -0
  64. package/src/sessions/subagents.ts +97 -0
  65. package/src/skills/registry.ts +16 -10
  66. package/src/terminal/accountsMenu.test.ts +225 -0
  67. package/src/terminal/accountsMenu.ts +382 -0
  68. package/src/terminal/control.test.ts +23 -0
  69. package/src/terminal/control.ts +23 -0
  70. package/src/terminal/controller.ts +134 -7
  71. package/src/terminal/hubMenu.ts +24 -0
  72. package/src/terminal/launch.test.ts +5 -14
  73. package/src/terminal/launch.ts +2 -42
  74. package/src/terminal/piExtension.lifecycle.test.ts +55 -0
  75. package/src/terminal/piExtension.ts +91 -6
  76. package/src/terminal/socket.test.ts +23 -0
  77. package/src/terminal/socket.ts +55 -20
  78. package/src/terminal/standalone.test.ts +37 -0
  79. package/src/terminal/standalone.ts +28 -7
  80. package/src/terminal/terminal.test.ts +220 -16
  81. package/src/terminal/terminals.ts +234 -16
  82. package/src/terminal/trajectory.test.ts +66 -0
  83. package/src/terminal/trajectory.ts +44 -0
  84. package/src/terminal/tui/application.test.ts +290 -0
  85. package/src/terminal/tui/application.ts +653 -0
  86. package/src/terminal/tui/commands.test.ts +278 -0
  87. package/src/terminal/tui/commands.ts +443 -0
  88. package/src/terminal/tui/dialogs.ts +301 -0
  89. package/src/terminal/tui/editor.test.ts +113 -0
  90. package/src/terminal/tui/editor.ts +109 -0
  91. package/src/terminal/tui/keybindings.test.ts +30 -0
  92. package/src/terminal/tui/keybindings.ts +59 -0
  93. package/src/terminal/tui/piAdapter.ts +62 -0
  94. package/src/terminal/tui/planMode.test.ts +10 -0
  95. package/src/terminal/tui/planMode.ts +12 -0
  96. package/src/terminal/tui/presentation.test.ts +79 -0
  97. package/src/terminal/tui/questions.test.ts +41 -0
  98. package/src/terminal/tui/questions.ts +47 -0
  99. package/src/terminal/tui/runtime.socket.test.ts +178 -0
  100. package/src/terminal/tui/runtime.test.ts +225 -0
  101. package/src/terminal/tui/runtime.ts +403 -0
  102. package/src/terminal/tui/screen.test.ts +143 -0
  103. package/src/terminal/tui/screen.ts +121 -0
  104. package/src/terminal/tui/sessionUi.ts +24 -0
  105. package/src/terminal/tui/settings.test.ts +150 -0
  106. package/src/terminal/tui/settings.ts +188 -0
  107. package/src/terminal/tui/skillInvocation.test.ts +71 -0
  108. package/src/terminal/tui/skillInvocation.ts +51 -0
  109. package/src/terminal/tui/status.test.ts +48 -0
  110. package/src/terminal/tui/status.ts +82 -0
  111. package/src/terminal/tui/theme.ts +185 -0
  112. package/src/terminal/tui/tools.test.ts +145 -0
  113. package/src/terminal/tui/tools.ts +142 -0
  114. package/src/terminal/tui/trajectoryPanel.test.ts +99 -0
  115. package/src/terminal/tui/trajectoryPanel.ts +226 -0
  116. package/src/terminal/tui/transcript.test.ts +39 -0
  117. package/src/terminal/tui/transcript.ts +135 -0
  118. package/src/terminal/tui/ui.test.ts +313 -0
  119. package/src/terminal/tui/ui.ts +166 -0
  120. package/src/terminal/tui/welcome.ts +102 -0
  121. package/src/terminal/tui/workingStatus.test.ts +44 -0
  122. package/src/terminal/tui/workingStatus.ts +238 -0
  123. package/src/tools/askUser.test.ts +69 -0
  124. package/src/tools/askUser.ts +31 -17
  125. package/src/tools/definitions.ts +42 -1
  126. package/src/tools/maintenance.ts +29 -0
  127. package/src/tools/memoryTools.ts +1 -1
  128. package/src/tools/registryWrite.test.ts +101 -0
  129. package/src/tools/registryWrite.ts +182 -0
  130. package/src/tools/spawnSubagent.ts +47 -0
  131. package/src/tools/types.ts +2 -0
@@ -0,0 +1,123 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "entries": [
4
+ {
5
+ "id": "xingular/evidence-first",
6
+ "title": "Evidence first",
7
+ "description": "Keep observations, inferences, and unknowns distinct.",
8
+ "publisher": "Xingular",
9
+ "license": "MIT",
10
+ "version": "1.0.0",
11
+ "category": "rule",
12
+ "channel": "official",
13
+ "tags": [
14
+ "rule"
15
+ ],
16
+ "requires": {
17
+ "harness": "0.1.6"
18
+ },
19
+ "sourceUrl": "https://github.com/tanntnny/xingular-hub/tree/main/entries/xingular/evidence-first/1.0.0",
20
+ "delivery": {
21
+ "kind": "registry",
22
+ "registryKind": "rules",
23
+ "url": "https://raw.githubusercontent.com/tanntnny/xingular-hub/main/entries/xingular/evidence-first/1.0.0/payload.json",
24
+ "sha256": "0f77661ea41e27bcb51aad982d15d493b893c5c1ba1eb6046a21315ddac07c5b",
25
+ "bundled": "{\n \"items\": [\n {\n \"name\": \"hub-evidence-first\",\n \"files\": [\n {\n \"path\": \"CONTENT.md\",\n \"encoding\": \"utf8\",\n \"content\": \"---\\nname: Evidence first\\ndescription: Distinguish observations, inferences, and unknowns.\\n---\\n\\nSupport factual claims with inspectable evidence. Label inferences and uncertainties. Correct a claim when better evidence changes it.\\n\"\n }\n ]\n }\n ]\n}\n"
26
+ }
27
+ },
28
+ {
29
+ "id": "xingular/github-connector",
30
+ "title": "GitHub connector",
31
+ "description": "A setup recipe for the GitHub MCP server; provide your own credentials.",
32
+ "publisher": "Xingular",
33
+ "license": "MIT",
34
+ "version": "1.0.0",
35
+ "category": "connector",
36
+ "channel": "official",
37
+ "tags": [
38
+ "connector"
39
+ ],
40
+ "requires": {
41
+ "harness": "0.1.6"
42
+ },
43
+ "sourceUrl": "https://github.com/tanntnny/xingular-hub/tree/main/entries/xingular/github-connector/1.0.0",
44
+ "delivery": {
45
+ "kind": "document",
46
+ "url": "https://raw.githubusercontent.com/tanntnny/xingular-hub/main/entries/xingular/github-connector/1.0.0/payload.json",
47
+ "sha256": "ec5bd9675e2977b9d880cf81732d84ac72b04af271048499415eaa6288896223",
48
+ "bundled": "{\n \"id\": \"github-hub\",\n \"name\": \"GitHub\",\n \"description\": \"Repositories, issues, and pull requests\",\n \"kind\": \"remote\",\n \"docsUrl\": \"https://github.com/github/github-mcp-server\",\n \"url\": \"https://api.githubcopilot.com/mcp/\",\n \"auth\": \"headers\",\n \"oauth\": null,\n \"fields\": [\n {\n \"header\": \"Authorization\",\n \"label\": \"Personal access token\",\n \"prefix\": \"Bearer \",\n \"help\": \"Provide a token for only the repositories and permissions you need.\"\n }\n ],\n \"setup\": [\n \"Create a personal access token in GitHub with the access you want to grant.\",\n \"Review the connection before enabling it.\"\n ],\n \"notice\": null,\n \"provenance\": null,\n \"suggestedReadTools\": []\n}\n"
49
+ }
50
+ },
51
+ {
52
+ "id": "xingular/research-starter",
53
+ "title": "Research starter",
54
+ "description": "A starting agent for comparing sources and writing supported answers.",
55
+ "publisher": "Xingular",
56
+ "license": "MIT",
57
+ "version": "1.0.0",
58
+ "category": "agent-template",
59
+ "channel": "official",
60
+ "tags": [
61
+ "agent-template"
62
+ ],
63
+ "requires": {
64
+ "harness": "0.1.6"
65
+ },
66
+ "sourceUrl": "https://github.com/tanntnny/xingular-hub/tree/main/entries/xingular/research-starter/1.0.0",
67
+ "delivery": {
68
+ "kind": "document",
69
+ "url": "https://raw.githubusercontent.com/tanntnny/xingular-hub/main/entries/xingular/research-starter/1.0.0/payload.json",
70
+ "sha256": "a2f672583df6f4049cc153ba1e173192301220e5f8817f9ad5a4fe87af25f52b",
71
+ "bundled": "{\n \"schemaVersion\": 1,\n \"id\": \"research-starter\",\n \"title\": \"Research starter\",\n \"description\": \"Compare sources and explain the evidence behind an answer.\",\n \"version\": \"1.0.0\",\n \"author\": \"Xingular\",\n \"license\": \"MIT\",\n \"agent\": {\n \"name\": \"Research starter\",\n \"icon\": {\n \"kind\": \"mascot\",\n \"shape\": \"prism\",\n \"color\": \"violet\",\n \"material\": \"glass\"\n },\n \"description\": \"Investigates questions with sources.\",\n \"instructions\": \"Help the user investigate a question. Start with reliable sources, explain what the evidence supports, cite the sources, and state unresolved questions. Ask the user when their goal is unclear.\",\n \"model\": null,\n \"effort\": \"high\",\n \"tools\": [\n \"read\",\n \"grep\",\n \"find\",\n \"ls\",\n \"web_search\",\n \"web_fetch\"\n ]\n },\n \"skills\": [],\n \"connections\": []\n}\n"
72
+ }
73
+ },
74
+ {
75
+ "id": "xingular/source-review",
76
+ "title": "Review a source",
77
+ "description": "Check who wrote a source, what it supports, and what remains uncertain.",
78
+ "publisher": "Xingular",
79
+ "license": "MIT",
80
+ "version": "1.0.0",
81
+ "category": "skill",
82
+ "channel": "official",
83
+ "tags": [
84
+ "skill"
85
+ ],
86
+ "requires": {
87
+ "harness": "0.1.6"
88
+ },
89
+ "sourceUrl": "https://github.com/tanntnny/xingular-hub/tree/main/entries/xingular/source-review/1.0.0",
90
+ "delivery": {
91
+ "kind": "registry",
92
+ "registryKind": "skills",
93
+ "url": "https://raw.githubusercontent.com/tanntnny/xingular-hub/main/entries/xingular/source-review/1.0.0/payload.json",
94
+ "sha256": "bb0617889ee1ca39611a10952bec3086fa3986e9ce10a809c473c57acf47bc07",
95
+ "bundled": "{\n \"items\": [\n {\n \"name\": \"hub-source-review\",\n \"files\": [\n {\n \"path\": \"SKILL.md\",\n \"encoding\": \"utf8\",\n \"content\": \"---\\nname: hub-source-review\\ndescription: Review the evidence behind a source before using its claims.\\n---\\n\\n# Review a source\\n\\nRecord its author and date. Identify the original evidence. Write what the source supports and which uncertainties remain. Cite the source so the reader can inspect it.\\n\"\n }\n ]\n }\n ]\n}\n"
96
+ }
97
+ },
98
+ {
99
+ "id": "xingular/summarize-source",
100
+ "title": "Summarize a source",
101
+ "description": "Summarize a provided source and explain its limitations.",
102
+ "publisher": "Xingular",
103
+ "license": "MIT",
104
+ "version": "1.0.0",
105
+ "category": "prompt",
106
+ "channel": "official",
107
+ "tags": [
108
+ "prompt"
109
+ ],
110
+ "requires": {
111
+ "harness": "0.1.6"
112
+ },
113
+ "sourceUrl": "https://github.com/tanntnny/xingular-hub/tree/main/entries/xingular/summarize-source/1.0.0",
114
+ "delivery": {
115
+ "kind": "registry",
116
+ "registryKind": "prompts",
117
+ "url": "https://raw.githubusercontent.com/tanntnny/xingular-hub/main/entries/xingular/summarize-source/1.0.0/payload.json",
118
+ "sha256": "12a646d474fe3cd822970869c972d1d9400c7f6350388dc735dce5c31a22680a",
119
+ "bundled": "{\n \"items\": [\n {\n \"name\": \"hub-summarize-source\",\n \"files\": [\n {\n \"path\": \"CONTENT.md\",\n \"encoding\": \"utf8\",\n \"content\": \"---\\nname: summarize-source\\ndescription: Summarize the supplied source and its limits.\\n---\\n\\nSummarize the source below. Include its main claims, supporting evidence, limitations, and questions it leaves unanswered.\\n\\n$@\\n\"\n }\n ]\n }\n ]\n}\n"
120
+ }
121
+ }
122
+ ]
123
+ }
package/src/hub/cli.ts ADDED
@@ -0,0 +1,59 @@
1
+ import { homedir } from 'node:os'
2
+ import { join, resolve } from 'node:path'
3
+ import { Harness, type HarnessOptions } from '../harness'
4
+
5
+ export const HUB_USAGE = `Usage: xingular-harness hub <list | show | install> [id@version] [options]
6
+
7
+ list [--refresh] Browse the curated catalog and locally installed versions
8
+ show <id@version> Show publisher, license, source, and delivery details
9
+ install <id@version> Install the exact reviewed version, disabled and untrusted
10
+
11
+ --data <dir> Target data library (default: ~/.xingular-harness/data)
12
+ --state <dir> Target device state (default: ~/.xingular-harness/state)
13
+ --refresh Refresh the public catalog; cached items remain usable offline
14
+ `
15
+
16
+ export async function runHubCli(argv: string[], dependencies: { home?: string; write?: (text: string) => void; createHarness?: (options: HarnessOptions) => Harness } = {}): Promise<number> {
17
+ const write = dependencies.write ?? ((text: string) => void process.stdout.write(text))
18
+ const args: string[] = []
19
+ const values: Record<string, string> = {}
20
+ let refresh = false
21
+ for (let index = 0; index < argv.length; index += 1) {
22
+ const arg = argv[index]
23
+ if (arg === '--refresh') refresh = true
24
+ else if (arg === '--data' || arg === '--state') {
25
+ const value = argv[++index]
26
+ if (!value || value.startsWith('--')) { write(`${arg} needs a directory.\n`); return 2 }
27
+ values[arg.slice(2)] = resolve(value)
28
+ } else if (arg.startsWith('--')) { write(`Unknown option ${arg}.\n`); return 2 }
29
+ else args.push(arg)
30
+ }
31
+ const [command, target] = args
32
+ if (!command || command === 'help') { write(HUB_USAGE); return 0 }
33
+ if (!['list', 'show', 'install'].includes(command)) { write(HUB_USAGE); return 2 }
34
+ const root = join(dependencies.home ?? homedir(), '.xingular-harness')
35
+ const options: HarnessOptions = { dataRoot: values.data ?? join(root, 'data'), stateDir: values.state ?? join(root, 'state') }
36
+ const harness = dependencies.createHarness?.(options) ?? new Harness(options)
37
+ try {
38
+ const overview = await harness.hub.list(refresh)
39
+ if (overview.error) write(`Catalog refresh failed: ${overview.error} Using ${overview.origin} content.\n`)
40
+ if (command === 'list') {
41
+ write(`Library: ${options.dataRoot}\n${overview.entries.map((entry) => `${entry.id}@${entry.version} ${entry.category} ${entry.title}${overview.installed.some((receipt) => receipt.entry.id === entry.id && receipt.entry.version === entry.version) ? ' · installed' : ''}`).join('\n')}\n`)
42
+ return 0
43
+ }
44
+ const split = target?.lastIndexOf('@') ?? -1
45
+ if (!target || split < 1) throw new Error('Name an exact item as publisher/name@version. Run hub list to see versions.')
46
+ const id = target.slice(0, split)
47
+ const version = target.slice(split + 1)
48
+ const entry = await harness.hub.get(id, version)
49
+ if (command === 'show') {
50
+ const { bundled: _bundled, ...delivery } = entry.delivery.kind === 'pi-package' ? { ...entry.delivery, bundled: undefined } : entry.delivery
51
+ write(`${entry.title} (${target})\n${entry.description}\nPublisher: ${entry.publisher}\nLicense: ${entry.license}\nSource: ${entry.sourceUrl}\nDelivery: ${JSON.stringify(delivery)}\n`)
52
+ } else {
53
+ const receipt = await harness.hub.install(id, version)
54
+ write(receipt.document ? `Saved ${target} in ${options.dataRoot}/hub/documents/${receipt.document}. Open this library in a compatible application to apply it.\n` : `Installed ${target}, switched off. Attach it separately${entry.delivery.kind === 'pi-package' ? ' and review/trust any code' : ''} before using it.\n`)
55
+ }
56
+ return 0
57
+ } catch (error) { write(`${error instanceof Error ? error.message : String(error)}\n`); return 1 }
58
+ finally { await harness.close() }
59
+ }
@@ -0,0 +1,205 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
3
+ import { tmpdir } from 'node:os'
4
+ import { join } from 'node:path'
5
+ import { afterEach, describe, expect, it, vi } from 'vitest'
6
+ import { Bus } from '../core/bus'
7
+ import { Harness } from '../harness'
8
+ import { memorySecretStore } from '../accounts/secrets'
9
+ import * as registry from '../skills/registry'
10
+ import { Hub } from './hub'
11
+ import { hubCatalogSchema, hubEntrySchema, type HubEntry } from './schema'
12
+ import { runHubMenu } from '../terminal/hubMenu'
13
+ import { SessionSocket } from '../terminal/socket'
14
+ import { HarnessError } from '../core/errors'
15
+ import { runHubCli } from './cli'
16
+ import * as packageApi from '../plugins/api'
17
+ import * as packageInstall from '../plugins/install'
18
+ import type { Plugin } from '../plugins/types'
19
+
20
+ const folders: string[] = []
21
+ afterEach(() => { vi.restoreAllMocks(); for (const folder of folders.splice(0)) rmSync(folder, { recursive: true, force: true }) })
22
+ function entry(kind: 'document' | 'registry' = 'document', content = '{"message":"data only"}'): HubEntry {
23
+ return hubEntrySchema.parse({ id: 'example/review', title: 'Review', description: 'A reviewed item.', publisher: 'Example', license: 'MIT', version: '1.0.0', category: kind === 'document' ? 'preset' : 'skill', channel: 'community', tags: [], requires: {}, sourceUrl: 'https://github.com/example/review', delivery: { kind, ...(kind === 'registry' ? { registryKind: 'skills' } : {}), url: 'https://raw.githubusercontent.com/example/review/main/payload.json', sha256: createHash('sha256').update(content).digest('hex'), bundled: content } })
24
+ }
25
+ function fixture(items: HubEntry[], fetcher?: typeof fetch) {
26
+ const root = mkdtempSync(join(tmpdir(), 'hub-test-')); folders.push(root)
27
+ const harness = { dataRoot: join(root, 'data'), stateDir: join(root, 'state'), env: { root: join(root, 'data') }, bus: new Bus() } as Harness
28
+ const hub = new Hub(harness, { catalog: { schemaVersion: 1, entries: items }, fetch: fetcher })
29
+ return { hub, harness, root: harness.env.root }
30
+ }
31
+ const skill = (name = 'review', path = 'SKILL.md') => JSON.stringify({ items: [{ name, files: [{ path, encoding: 'utf8', content: '---\nname: review\ndescription: Review evidence.\n---\n\nRead sources.' }] }] })
32
+
33
+ describe('Hub installation', () => {
34
+ it('stores documents inertly, idempotently, and checks their digest on read', async () => {
35
+ const { hub, root } = fixture([entry()])
36
+ const receipt = await hub.install('example/review', '1.0.0')
37
+ expect(receipt.resourceIds).toEqual([])
38
+ expect(await hub.install('example/review', '1.0.0')).toEqual(receipt)
39
+ expect(hub.readDocument('example/review', '1.0.0').content).toContain('data only')
40
+ expect(existsSync(join(root, 'config', 'plugin-attachments.json'))).toBe(false)
41
+ writeFileSync(join(root, 'hub', 'documents', receipt.document!), '{"edited":true}')
42
+ expect(() => hub.readDocument('example/review', '1.0.0')).toThrow('differs')
43
+ })
44
+ it('imports complete skills disabled and refuses overwriting local files', async () => {
45
+ const { hub, root } = fixture([entry('registry', skill())])
46
+ const receipt = await hub.install('example/review', '1.0.0')
47
+ expect(receipt.resourceIds).toEqual(['skills/review'])
48
+ expect(registry.enabledFor(root, null)).toEqual([])
49
+ const content = readFileSync(join(root, 'registry', 'skills', 'review', 'SKILL.md'), 'utf8')
50
+ const collision = { ...entry('registry', skill()), id: 'other/review' }
51
+ const another = new Hub(({ dataRoot: root, env: { root }, stateDir: join(root, 'state'), bus: new Bus() }) as Harness, { catalog: { schemaVersion: 1, entries: [collision] } })
52
+ await expect(another.install(collision.id, collision.version)).rejects.toThrow('already exists')
53
+ expect(readFileSync(join(root, 'registry', 'skills', 'review', 'SKILL.md'), 'utf8')).toBe(content)
54
+ })
55
+ it.each(['../escape', 'folder/../../escape', 'C:/escape', '.hidden/SKILL.md'])('rejects registry path %s before writing', async (path) => {
56
+ const { hub, root } = fixture([entry('registry', skill('review', path))])
57
+ await expect(hub.install('example/review', '1.0.0')).rejects.toThrow()
58
+ expect(registry.list(root)).toEqual([])
59
+ expect(existsSync(join(root, 'hub', 'installed.json'))).toBe(false)
60
+ })
61
+ it('imports prompts and rules as single Markdown files, disabled', async () => {
62
+ for (const kind of ['prompts', 'rules'] as const) {
63
+ const item = entry('registry', JSON.stringify({ items: [{ name: 'review', files: [{ path: 'CONTENT.md', encoding: 'utf8', content: '# Review\nRead evidence.' }] }] }))
64
+ if (item.delivery.kind === 'registry') item.delivery.registryKind = kind
65
+ const { hub, root } = fixture([item])
66
+ await hub.install(item.id, item.version)
67
+ expect(readFileSync(join(root, 'registry', kind, 'review.md'), 'utf8')).toContain('Read evidence')
68
+ expect(registry.enabledFor(root, null)).toEqual([])
69
+ }
70
+ })
71
+ it('does not commit a download with the wrong digest', async () => {
72
+ const item = entry(); if (item.delivery.kind !== 'pi-package') delete item.delivery.bundled
73
+ const { hub, root } = fixture([item], (async () => new Response('{"different":true}')) as typeof fetch)
74
+ await expect(hub.install(item.id, item.version)).rejects.toThrow('differs')
75
+ expect(existsSync(join(root, 'hub', 'installed.json'))).toBe(false)
76
+ expect(hub.busy).toBe(false)
77
+ })
78
+ it('rejects malformed UTF-8 JSON even when its byte digest matches', async () => {
79
+ const bytes = Buffer.from([0x7b, 0x22, 0x78, 0x22, 0x3a, 0x22, 0xff, 0x22, 0x7d])
80
+ const item = entry()
81
+ if (item.delivery.kind !== 'pi-package') { delete item.delivery.bundled; item.delivery.sha256 = createHash('sha256').update(bytes).digest('hex') }
82
+ const { hub, root } = fixture([item], (async () => new Response(bytes)) as typeof fetch)
83
+ await expect(hub.install(item.id, item.version)).rejects.toThrow('valid UTF-8')
84
+ expect(existsSync(join(root, 'hub', 'installed.json'))).toBe(false)
85
+ })
86
+ it('previews reviewed bytes without creating files or receipts', async () => {
87
+ const item = entry(); if (item.delivery.kind !== 'pi-package') delete item.delivery.bundled
88
+ const { hub, root } = fixture([item], (async () => new Response('{"message":"data only"}')) as typeof fetch)
89
+ const preview = await hub.get(item.id, item.version, true)
90
+ expect(preview.delivery.kind !== 'pi-package' && preview.delivery.bundled).toContain('data only')
91
+ expect(existsSync(join(root, 'hub'))).toBe(false)
92
+ })
93
+ it('can reinstall deleted resources and clears stale attachment grants', async () => {
94
+ const { hub, root } = fixture([entry('registry', skill())])
95
+ await hub.install('example/review', '1.0.0')
96
+ registry.setEnabled(root, null, ['skills/review'])
97
+ rmSync(join(root, 'registry', 'skills', 'review'), { recursive: true })
98
+ expect((await hub.list()).installed).toEqual([])
99
+ await hub.install('example/review', '1.0.0')
100
+ expect(registry.enabledFor(root, null)).toEqual([])
101
+ expect((await hub.list()).installed).toHaveLength(1)
102
+ })
103
+ it('preserves corrupt installation records rather than overwriting them', async () => {
104
+ const { hub, root } = fixture([entry()])
105
+ mkdirSync(join(root, 'hub'), { recursive: true })
106
+ writeFileSync(join(root, 'hub', 'installed.json'), 'corrupt')
107
+ await expect(hub.install('example/review', '1.0.0')).rejects.toThrow('could not be read')
108
+ expect(readFileSync(join(root, 'hub', 'installed.json'), 'utf8')).toBe('corrupt')
109
+ })
110
+ it('requires the terminal extension control key for hub mutations', async () => {
111
+ const { hub, root } = fixture([entry()])
112
+ const socket = new SessionSocket(join(root, 'unused.sock'), { memory: () => '', apps: async () => null,
113
+ plugins: async (_grant, method, params, control) => {
114
+ if (method === 'hub.list') return hub.list()
115
+ if (!control) throw new HarnessError('unauthorized', 'Only the terminal controls installations.')
116
+ return hub.install(String(params.id), String(params.version))
117
+ } })
118
+ const token = socket.issue({ sessionId: 'terminal', caller: () => ({}), broker: null })
119
+ const install = { token, call: 'hub.install', params: { id: 'example/review', version: '1.0.0' } }
120
+ expect(await socket.handle(JSON.stringify(install))).toHaveProperty('error')
121
+ const claim = await socket.handle(JSON.stringify({ token, call: 'control.claim' })) as { result: { key: string } }
122
+ expect(await socket.handle(JSON.stringify({ ...install, control: claim.result.key }))).toHaveProperty('result.document')
123
+ await socket.close()
124
+ })
125
+ it('CLI installs into the explicit library without opening or settling runs', async () => {
126
+ const { hub, harness, root } = fixture([entry()])
127
+ const output: string[] = []
128
+ const code = await runHubCli(['install', 'example/review@1.0.0', '--data', root, '--state', harness.stateDir], { write: (text) => output.push(text), createHarness: (options) => {
129
+ expect(options.dataRoot).toBe(root)
130
+ return { ...harness, hub, close: async () => undefined } as unknown as Harness
131
+ } })
132
+ expect(code).toBe(0)
133
+ expect(output.join('')).toContain('Saved example/review@1.0.0')
134
+ expect(existsSync(join(root, 'runs'))).toBe(false)
135
+ })
136
+ it('uses the last valid catalog when refresh fails or has duplicate versions', async () => {
137
+ let response = JSON.stringify({ schemaVersion: 1, entries: [entry()] })
138
+ const { hub } = fixture([], (async () => new Response(response)) as typeof fetch)
139
+ expect((await hub.list(true)).origin).toBe('remote')
140
+ response = JSON.stringify({ schemaVersion: 1, entries: [entry(), entry()] })
141
+ const cached = await hub.list(true)
142
+ expect(cached.origin).toBe('cached')
143
+ expect(cached.error).toBeTruthy()
144
+ expect(cached.entries).toHaveLength(1)
145
+ expect((await hub.list()).entries).toHaveLength(1)
146
+ })
147
+ it('rejects simultaneous writers and releases locks after a failed download', async () => {
148
+ const item = entry(); if (item.delivery.kind !== 'pi-package') delete item.delivery.bundled
149
+ let resolve!: (response: Response) => void
150
+ const { hub } = fixture([item], (() => new Promise<Response>((done) => { resolve = done })) as typeof fetch)
151
+ const first = hub.install(item.id, item.version)
152
+ await expect(hub.install(item.id, item.version)).rejects.toThrow('Another installation')
153
+ resolve(new Response('bad digest'))
154
+ await expect(first).rejects.toThrow('differs')
155
+ expect(hub.busy).toBe(false)
156
+ })
157
+ it('keeps an in-progress installation pinned to its data root', async () => {
158
+ const item = entry(); if (item.delivery.kind !== 'pi-package') delete item.delivery.bundled
159
+ const root = mkdtempSync(join(tmpdir(), 'hub-root-pin-')); folders.push(root)
160
+ let resolve!: (response: Response) => void
161
+ const harness = new Harness({ dataRoot: join(root, 'data'), stateDir: join(root, 'state'), secrets: memorySecretStore(), hub: { catalog: { schemaVersion: 1, entries: [item] }, fetch: (() => new Promise<Response>((done) => { resolve = done })) as typeof fetch } })
162
+ try {
163
+ const pending = harness.hub.install(item.id, item.version)
164
+ expect(() => harness.open(join(root, 'other'))).toThrow('Wait for the hub installation')
165
+ expect(harness.dataRoot).toBe(join(root, 'data'))
166
+ resolve(new Response('{"message":"data only"}'))
167
+ await pending
168
+ expect((await harness.hub.list()).installed).toHaveLength(1)
169
+ expect(existsSync(join(root, 'other'))).toBe(false)
170
+ } finally { await harness.close() }
171
+ })
172
+ it('rejects unpinned packages and incompatible runtimes', async () => {
173
+ expect(() => hubEntrySchema.parse({ ...entry(), delivery: { kind: 'pi-package', source: 'npm:unknown@latest' } })).toThrow()
174
+ expect(() => hubCatalogSchema.parse({ schemaVersion: 2, entries: [] })).toThrow()
175
+ const item = { ...entry(), requires: { harness: '99.0.0' } }
176
+ const { hub } = fixture([item])
177
+ await expect(hub.install(item.id, item.version)).rejects.toThrow('requires Harness')
178
+ })
179
+ it('records only the requested package when another plugin appears during installation', async () => {
180
+ const source = 'npm:@example/review@1.0.0'
181
+ const item = hubEntrySchema.parse({ ...entry(), category: 'plugin', delivery: { kind: 'pi-package', source } })
182
+ const { hub } = fixture([item])
183
+ vi.spyOn(packageApi, 'list').mockResolvedValue([])
184
+ vi.spyOn(packageInstall, 'install').mockResolvedValue([
185
+ { id: 'package:review', install: { kind: 'settings', source } } as Plugin,
186
+ { id: 'package:another', install: { kind: 'settings', source: 'npm:@someone/other@1.0.0' } } as Plugin
187
+ ])
188
+ expect((await hub.install(item.id, item.version)).resourceIds).toEqual(['package:review'])
189
+ expect(packageInstall.install).toHaveBeenCalledWith(expect.anything(), { source })
190
+ })
191
+ it('does not treat cancelling the terminal review as an install', async () => {
192
+ const { hub } = fixture([entry()])
193
+ const calls: string[] = []
194
+ const choices = ['Review · preset · example/review@1.0.0', 'Done']
195
+ await runHubMenu({ select: async () => choices.shift(), confirm: async () => false, notify: () => undefined }, async <T>(method: string) => { calls.push(method); return await hub.list() as T })
196
+ expect(calls).toEqual(['hub.list', 'hub.list'])
197
+ })
198
+ it('refuses an already-held library lock', async () => {
199
+ const { hub, root } = fixture([entry()])
200
+ mkdirSync(join(root, 'hub'), { recursive: true })
201
+ writeFileSync(join(root, 'hub', 'install.lock'), String(process.pid))
202
+ await expect(hub.install('example/review', '1.0.0')).rejects.toThrow('Another installation')
203
+ expect(hub.busy).toBe(false)
204
+ })
205
+ })
package/src/hub/hub.ts ADDED
@@ -0,0 +1,256 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
3
+ import { join } from 'node:path'
4
+ import { watch } from 'node:fs'
5
+
6
+ import { HarnessError, invalid } from '../core/errors'
7
+ import { atomicWrite, now, readJson, readJsonOr, writeJson } from '../core/fs'
8
+ import type { Harness } from '../harness'
9
+ import * as pluginApi from '../plugins/api'
10
+ import * as packages from '../plugins/install'
11
+ import * as registry from '../skills/registry'
12
+ import * as attachments from '../plugins/attachments'
13
+ import { version as harnessVersion } from '../../package.json'
14
+ import bundled from './catalog.json'
15
+ import { HUB_CATALOG_URL, HUB_MAX_BYTES, hubCatalogSchema, hubEntrySchema, hubReceiptSchema, meetsMinimum, type HubCatalog, type HubEntry, type HubOverview, type HubReceipt } from './schema'
16
+
17
+ export interface HubOptions {
18
+ catalogUrl?: string
19
+ catalog?: HubCatalog
20
+ fetch?: typeof globalThis.fetch
21
+ }
22
+
23
+ const digest = (bytes: Uint8Array | string): string => createHash('sha256').update(bytes).digest('hex')
24
+ const receiptName = (entry: Pick<HubEntry, 'id' | 'version'>): string => digest(`${entry.id}@${entry.version}`)
25
+
26
+ /** Acquires an exclusive writer lock. A crashed writer's lock is recovered only when its PID is gone. */
27
+ function acquire(path: string): () => void {
28
+ mkdirSync(join(path, '..'), { recursive: true })
29
+ try {
30
+ const file = openSync(path, 'wx', 0o600)
31
+ try { writeFileSync(file, `${process.pid}`) } finally { closeSync(file) }
32
+ return () => rmSync(path, { force: true })
33
+ } catch (error) {
34
+ if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error
35
+ // Serialize stale-lock recovery too: two reapers must never remove a new writer's lock.
36
+ let recovery: number
37
+ try { recovery = openSync(`${path}.recovery`, 'wx', 0o600) } catch {
38
+ throw new HarnessError('hub_busy', 'Another installation is using this library. Wait and retry.')
39
+ }
40
+ try {
41
+ const pid = Number(readFileSync(path, 'utf8'))
42
+ if (Number.isInteger(pid) && pid > 0) {
43
+ try { process.kill(pid, 0) } catch (probe) {
44
+ if ((probe as NodeJS.ErrnoException).code === 'ESRCH') {
45
+ rmSync(path)
46
+ return acquire(path)
47
+ }
48
+ }
49
+ }
50
+ throw new HarnessError('hub_busy', 'Another installation is using this library. Wait for it to finish and retry.')
51
+ } finally { closeSync(recovery); rmSync(`${path}.recovery`, { force: true }) }
52
+ }
53
+ }
54
+
55
+ /** Catalog data never loads extensions or changes attachment, trust, or connector scopes. */
56
+ export class Hub {
57
+ private readonly fallback: HubCatalog
58
+ private readonly fetcher: typeof globalThis.fetch
59
+ private readonly catalogUrl: string
60
+ private watcher: ReturnType<typeof watch> | null = null
61
+ private refresh: Promise<HubOverview> | null = null
62
+ private installing = false
63
+ get busy(): boolean { return this.installing }
64
+
65
+ constructor(private readonly harness: Harness, options: HubOptions = {}) {
66
+ this.fallback = hubCatalogSchema.parse(options.catalog ?? bundled)
67
+ this.fetcher = options.fetch ?? globalThis.fetch
68
+ this.catalogUrl = options.catalogUrl ?? HUB_CATALOG_URL
69
+ }
70
+
71
+ private receipts(): HubReceipt[] {
72
+ const root = this.harness.dataRoot
73
+ if (!root) return []
74
+ const values = readJson<unknown[]>(join(root, 'hub', 'installed.json')) ?? []
75
+ if (!Array.isArray(values)) throw invalid('The hub installation records could not be read.')
76
+ return values.map((value) => hubReceiptSchema.parse(value))
77
+ }
78
+
79
+ private snapshot(): HubOverview {
80
+ const cached = readJsonOr<{ catalog: unknown; at: string } | null>(join(this.harness.stateDir, 'hub', 'catalog.json'), null)
81
+ const parsed = hubCatalogSchema.safeParse(cached?.catalog)
82
+ const catalog = parsed.success ? parsed.data : this.fallback
83
+ const installed = this.receipts().filter((receipt) => this.present(receipt))
84
+ const entries = new Map(catalog.entries.map((entry) => [`${entry.id}@${entry.version}`, entry]))
85
+ for (const receipt of installed) if (!entries.has(`${receipt.entry.id}@${receipt.entry.version}`)) entries.set(`${receipt.entry.id}@${receipt.entry.version}`, receipt.entry)
86
+ return { entries: [...entries.values()], installed, origin: parsed.success ? 'cached' : 'bundled', refreshedAt: parsed.success ? cached!.at : null, error: null, dataRoot: this.harness.dataRoot, stateDir: this.harness.stateDir }
87
+ }
88
+
89
+ private present(receipt: HubReceipt): boolean {
90
+ const root = this.harness.dataRoot
91
+ if (!root) return false
92
+ try {
93
+ if (receipt.document) {
94
+ if (receipt.entry.delivery.kind !== 'document' || receipt.document !== `${receiptName(receipt.entry)}.json`) return false
95
+ const path = join(root, 'hub', 'documents', receipt.document)
96
+ const stat = lstatSync(path)
97
+ return stat.isFile() && stat.size <= HUB_MAX_BYTES && digest(readFileSync(path)) === receipt.entry.delivery.sha256
98
+ }
99
+ if (receipt.entry.delivery.kind === 'registry') return receipt.resourceIds.length > 0 && receipt.resourceIds.every((id) => {
100
+ if (!/^(skills|prompts|rules)\/[a-z0-9-]{1,64}$/.test(id)) return false
101
+ const [kind, name] = id.split('/')
102
+ return lstatSync(join(root, 'registry', kind, kind === 'skills' ? `${name}/SKILL.md` : `${name}.md`)).isFile()
103
+ })
104
+ if (receipt.entry.delivery.kind === 'pi-package') {
105
+ const source = receipt.entry.delivery.source
106
+ const settings = readJsonOr<{ packages?: Array<string | { source: string }> }>(join(this.harness.stateDir, 'accounts', '_shared', 'pi', 'settings.json'), {})
107
+ return settings.packages?.some((item) => (typeof item === 'string' ? item : item.source) === source) ?? false
108
+ }
109
+ } catch { return false }
110
+ return false
111
+ }
112
+
113
+ async list(refresh = false): Promise<HubOverview> {
114
+ if (!refresh) return this.snapshot()
115
+ this.refresh ??= this.refreshCatalog().finally(() => { this.refresh = null })
116
+ return this.refresh
117
+ }
118
+
119
+ private async bytes(url: string): Promise<Buffer> {
120
+ const parsed = new URL(url)
121
+ if (parsed.protocol !== 'https:' || parsed.username || parsed.password || parsed.search || parsed.hash || parsed.port || parsed.hostname !== 'raw.githubusercontent.com') throw invalid('Hub downloads must use public raw.githubusercontent.com HTTPS URLs.')
122
+ const response = await this.fetcher(url, { redirect: 'error', credentials: 'omit', signal: AbortSignal.timeout(30_000) })
123
+ if (!response.ok) throw new Error(`The catalog source returned ${response.status}.`)
124
+ if (Number(response.headers.get('content-length') ?? 0) > HUB_MAX_BYTES) throw invalid('The hub download exceeds 16 MiB.')
125
+ if (!response.body) throw invalid('The hub download has no body.')
126
+ const reader = response.body.getReader()
127
+ const chunks: Uint8Array[] = []
128
+ let size = 0
129
+ try {
130
+ for (;;) {
131
+ const { done, value } = await reader.read()
132
+ if (done) break
133
+ size += value.byteLength
134
+ if (size > HUB_MAX_BYTES) throw invalid('The hub download exceeds 16 MiB.')
135
+ chunks.push(value)
136
+ }
137
+ } finally { await reader.cancel().catch(() => undefined) }
138
+ return Buffer.concat(chunks)
139
+ }
140
+
141
+ private async refreshCatalog(): Promise<HubOverview> {
142
+ try {
143
+ const catalog = hubCatalogSchema.parse(JSON.parse(this.decode(await this.bytes(this.catalogUrl))))
144
+ writeJson(join(this.harness.stateDir, 'hub', 'catalog.json'), { catalog, at: now() })
145
+ this.harness.bus.emit({ type: 'hub.changed' })
146
+ return { ...this.snapshot(), origin: 'remote' }
147
+ } catch (error) {
148
+ return { ...this.snapshot(), error: error instanceof Error ? error.message : String(error) }
149
+ }
150
+ }
151
+
152
+ private decode(bytes: Buffer): string {
153
+ const text = bytes.toString('utf8')
154
+ if (!Buffer.from(text).equals(bytes)) throw invalid('Hub JSON must use valid UTF-8.')
155
+ return text
156
+ }
157
+
158
+ private find(id: string, version: string): HubEntry {
159
+ const entry = this.snapshot().entries.find((item) => item.id === id && item.version === version)
160
+ if (!entry) throw invalid('That item version is not in this catalog. Refresh the Hub and try again.')
161
+ return entry
162
+ }
163
+
164
+ async get(id: string, version: string, preview = false): Promise<HubEntry> {
165
+ const entry = this.find(id, version)
166
+ if (!preview || entry.delivery.kind === 'pi-package' || entry.delivery.bundled !== undefined) return entry
167
+ const bytes = await this.bytes(entry.delivery.url)
168
+ if (digest(bytes) !== entry.delivery.sha256) throw invalid('The downloaded item differs from the reviewed version.')
169
+ return { ...entry, delivery: { ...entry.delivery, bundled: this.decode(bytes) } }
170
+ }
171
+
172
+ async install(id: string, version: string): Promise<HubReceipt> {
173
+ const root = this.harness.env.root
174
+ const entry = hubEntrySchema.parse(this.find(id, version))
175
+ if (entry.requires.harness && !meetsMinimum(harnessVersion, entry.requires.harness)) throw invalid(`This item requires Harness ${entry.requires.harness} or newer.`)
176
+ const releaseState = acquire(join(this.harness.stateDir, 'hub', 'install.lock'))
177
+ this.installing = true
178
+ let releaseData: (() => void) | null = null
179
+ try {
180
+ releaseData = acquire(join(root, 'hub', 'install.lock'))
181
+ const records = this.receipts()
182
+ const existing = records.find((receipt) => receipt.entry.id === id && receipt.entry.version === version)
183
+ if (existing && this.present(existing)) return existing
184
+ const remaining = records.filter((record) => record !== existing)
185
+ const receipt: HubReceipt = { entry, installedAt: now(), resourceIds: [], document: null }
186
+ const choicesBefore = attachments.load(root)
187
+ if (entry.delivery.kind === 'pi-package') {
188
+ const source = entry.delivery.source
189
+ if (remaining.some((record) => record.entry.id === id && this.present(record))) throw invalid('Another version is already installed. Manage the existing plugin before replacing it.')
190
+ const before = await pluginApi.list(this.harness, {})
191
+ const identity = (value: string): string => value.slice(0, value.lastIndexOf('@'))
192
+ if (before.some((plugin) => plugin.install && identity(plugin.install.source) === identity(source))) throw invalid('This package is already installed. Manage it in Plugins.')
193
+ const after = await packages.install(this.harness, { source })
194
+ receipt.resourceIds = after.filter((plugin) => plugin.install?.source === source && !before.some((old) => old.id === plugin.id)).map((plugin) => plugin.id)
195
+ if (!receipt.resourceIds.length) throw invalid('The package did not add an installable plugin. No hub installation was recorded.')
196
+ } else {
197
+ const delivery = entry.delivery
198
+ const bytes = delivery.bundled === undefined ? await this.bytes(delivery.url) : Buffer.from(delivery.bundled)
199
+ if (digest(bytes) !== delivery.sha256) throw invalid('The downloaded item differs from the reviewed version. Nothing was installed.')
200
+ const text = this.decode(bytes)
201
+ if (delivery.kind === 'document') {
202
+ JSON.parse(text)
203
+ receipt.document = `${receiptName(entry)}.json`
204
+ atomicWrite(join(root, 'hub', 'documents', receipt.document), bytes)
205
+ } else {
206
+ const payload = JSON.parse(text)
207
+ const previous = new Set(registry.list(root).map((item) => item.id))
208
+ const imported = registry.importItems(root, delivery.registryKind, payload.items)
209
+ receipt.resourceIds = imported.items.filter((item) => !previous.has(item.id)).map((item) => item.id)
210
+ const choices = structuredClone(choicesBefore)
211
+ const ids = new Set(receipt.resourceIds)
212
+ for (const scope of [choices.global, ...Object.values(choices.agents), ...Object.values(choices.projects)]) {
213
+ scope.on = scope.on.filter((id) => !ids.has(id))
214
+ scope.off = scope.off.filter((id) => !ids.has(id))
215
+ }
216
+ attachments.save(root, choices)
217
+ }
218
+ }
219
+ try { writeJson(join(root, 'hub', 'installed.json'), [...remaining, receipt]) } catch (error) {
220
+ if (receipt.document) rmSync(join(root, 'hub', 'documents', receipt.document), { force: true })
221
+ if (entry.delivery.kind === 'registry') for (const id of receipt.resourceIds) {
222
+ const [kind, name] = id.split('/')
223
+ rmSync(join(root, 'registry', kind, kind === 'skills' ? name : `${name}.md`), { recursive: true, force: true })
224
+ }
225
+ if (entry.delivery.kind === 'registry') attachments.save(root, choicesBefore)
226
+ if (entry.delivery.kind === 'pi-package') for (const pluginId of receipt.resourceIds) await packages.remove(this.harness, { pluginId })
227
+ throw error
228
+ }
229
+ this.harness.bus.emit({ type: 'hub.changed' })
230
+ this.harness.bus.emit({ type: 'plugins.changed' })
231
+ return receipt
232
+ } finally { this.installing = false; releaseData?.(); releaseState() }
233
+ }
234
+
235
+ readDocument(id: string, version: string): { entry: HubEntry; content: string } {
236
+ const receipt = this.receipts().find((item) => item.entry.id === id && item.entry.version === version)
237
+ if (!receipt?.document || receipt.document !== `${receiptName(receipt.entry)}.json`) throw invalid('Install this document before opening it.')
238
+ const path = join(this.harness.env.root, 'hub', 'documents', receipt.document)
239
+ const stat = lstatSync(path)
240
+ if (!stat.isFile() || stat.size > HUB_MAX_BYTES) throw invalid('The saved document is not a bounded regular file.')
241
+ const content = readFileSync(path, 'utf8')
242
+ if (receipt.entry.delivery.kind === 'pi-package' || digest(content) !== receipt.entry.delivery.sha256) throw invalid('The saved document differs from the installed version. Import edited files explicitly instead.')
243
+ return { entry: receipt.entry, content }
244
+ }
245
+
246
+ watch(root: string): void {
247
+ this.watcher?.close()
248
+ mkdirSync(join(root, 'hub'), { recursive: true })
249
+ this.watcher = watch(join(root, 'hub'), { persistent: false }, (_event, name) => {
250
+ if (name === 'installed.json') this.harness.bus.emit({ type: 'hub.changed' })
251
+ })
252
+ this.watcher.on('error', () => { this.watcher?.close(); this.watcher = null })
253
+ }
254
+
255
+ close(): void { this.watcher?.close(); this.watcher = null }
256
+ }