@xingular/harness 2.1.2 → 3.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 (132) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/NOTICE.md +22 -0
  3. package/README.md +67 -36
  4. package/dist/cli.js +6023 -6621
  5. package/dist/terminal-pi.js +19 -24
  6. package/docs/terminal-tui.md +27 -7
  7. package/package.json +2 -2
  8. package/src/accounts/accounts.ts +4 -3
  9. package/src/capabilities/api.ts +1 -1
  10. package/src/cli.ts +7 -3
  11. package/src/core/caller.ts +34 -0
  12. package/src/core/fs.ts +17 -1
  13. package/src/harness.ts +55 -66
  14. package/src/{memory → history}/recall.test.ts +1 -1
  15. package/src/{memory → history}/recall.ts +1 -1
  16. package/src/{memory → history}/trajectory.test.ts +4 -4
  17. package/src/{memory → history}/trajectory.ts +1 -1
  18. package/src/hub/hub.test.ts +4 -5
  19. package/src/hub/hub.ts +2 -2
  20. package/src/index.ts +28 -14
  21. package/src/mcp/connections.ts +1 -1
  22. package/src/mcp/service.ts +1 -1
  23. package/src/pi/proxy.test.ts +68 -0
  24. package/src/pi/runtime.test.ts +61 -0
  25. package/src/pi/runtime.ts +50 -0
  26. package/src/plugins/api.ts +11 -9
  27. package/src/plugins/builtin.ts +22 -40
  28. package/src/plugins/catalogue.test.ts +13 -41
  29. package/src/plugins/catalogue.ts +3 -32
  30. package/src/plugins/discover.ts +2 -1
  31. package/src/plugins/firstParty.test.ts +1 -1
  32. package/src/plugins/hooks.test.ts +21 -71
  33. package/src/plugins/hooks.ts +6 -7
  34. package/src/plugins/identity.ts +16 -2
  35. package/src/plugins/install.test.ts +1 -1
  36. package/src/plugins/install.ts +2 -1
  37. package/src/plugins/inventory.ts +19 -18
  38. package/src/plugins/plugins.ts +38 -2
  39. package/src/prompt/compile.test.ts +82 -193
  40. package/src/prompt/compile.ts +98 -363
  41. package/src/refine/cli.ts +114 -0
  42. package/src/refine/demo.ts +124 -0
  43. package/src/refine/evidence.ts +349 -0
  44. package/src/refine/host.ts +102 -0
  45. package/src/refine/ledger.ts +104 -0
  46. package/src/refine/library.ts +192 -0
  47. package/src/refine/pi.test.ts +90 -0
  48. package/src/refine/pi.ts +218 -0
  49. package/src/refine/prompts.ts +430 -0
  50. package/src/refine/recursion.ts +353 -0
  51. package/src/refine/refine.test.ts +140 -0
  52. package/src/refine/runner.ts +205 -0
  53. package/src/refine/selection.ts +291 -0
  54. package/src/refine/service.ts +194 -0
  55. package/src/refine/trajectory.ts +71 -0
  56. package/src/refine/types.ts +91 -0
  57. package/src/server/methods.ts +137 -123
  58. package/src/server/server.test.ts +37 -30
  59. package/src/sessions/contracts.ts +6 -1
  60. package/src/sessions/conversation.test.ts +2 -27
  61. package/src/sessions/policy.ts +1 -1
  62. package/src/sessions/pricing.ts +3 -1
  63. package/src/sessions/registry.ts +5 -1
  64. package/src/sessions/runStore.ts +3 -1
  65. package/src/sessions/runner.ts +10 -5
  66. package/src/sessions/sessions.test.ts +45 -6
  67. package/src/sessions/sessions.ts +40 -17
  68. package/src/sessions/subagentService.test.ts +22 -1
  69. package/src/sessions/subagentService.ts +14 -1
  70. package/src/sessions/subagents.ts +2 -0
  71. package/src/terminal/cli.ts +24 -48
  72. package/src/terminal/pluginControl.test.ts +7 -8
  73. package/src/terminal/socket.ts +3 -3
  74. package/src/terminal/terminal.test.ts +21 -39
  75. package/src/terminal/terminals.ts +20 -13
  76. package/src/terminal/tui/application.test.ts +58 -0
  77. package/src/terminal/tui/application.ts +10 -4
  78. package/src/terminal/tui/clipboardFiles.test.ts +94 -0
  79. package/src/terminal/tui/clipboardFiles.ts +86 -0
  80. package/src/terminal/tui/piAdapter.ts +7 -4
  81. package/src/terminal/tui/runtime.ts +2 -0
  82. package/src/tools/definitions.ts +49 -132
  83. package/src/tools/dispatch.ts +78 -0
  84. package/src/tools/harnessTools.ts +23 -0
  85. package/src/tools/spawnSubagent.ts +3 -3
  86. package/src/tools/types.ts +1 -1
  87. package/src/tree/components.ts +221 -0
  88. package/src/tree/materialise.ts +122 -0
  89. package/src/tree/seed.ts +123 -0
  90. package/src/tree/store.ts +193 -0
  91. package/src/tree/surface.ts +135 -0
  92. package/src/tree/tree.test.ts +126 -0
  93. package/src/tree/tree.ts +346 -0
  94. package/src/tree/trust.ts +86 -0
  95. package/src/wiki/notes.ts +313 -0
  96. package/src/memory/api.test.ts +0 -82
  97. package/src/memory/api.ts +0 -303
  98. package/src/memory/blocks.test.ts +0 -78
  99. package/src/memory/blocks.ts +0 -408
  100. package/src/memory/caller.ts +0 -69
  101. package/src/memory/curation.test.ts +0 -206
  102. package/src/memory/curation.ts +0 -64
  103. package/src/memory/dispatch.ts +0 -64
  104. package/src/memory/facts.test.ts +0 -41
  105. package/src/memory/facts.ts +0 -248
  106. package/src/memory/graph.ts +0 -103
  107. package/src/memory/health.test.ts +0 -130
  108. package/src/memory/health.ts +0 -74
  109. package/src/memory/maintainer.ts +0 -272
  110. package/src/memory/maintenance.test.ts +0 -192
  111. package/src/memory/maintenance.ts +0 -679
  112. package/src/memory/migrate.ts +0 -79
  113. package/src/memory/operations.ts +0 -150
  114. package/src/memory/proposals.test.ts +0 -168
  115. package/src/memory/proposals.ts +0 -584
  116. package/src/memory/references.ts +0 -77
  117. package/src/memory/runContext.ts +0 -93
  118. package/src/memory/store.test.ts +0 -66
  119. package/src/memory/store.ts +0 -385
  120. package/src/memory/wiki.test.ts +0 -47
  121. package/src/memory/wiki.ts +0 -423
  122. package/src/memory/wikiIndex.test.ts +0 -88
  123. package/src/memory/wikiIndex.ts +0 -117
  124. package/src/plugins/proposal.ts +0 -121
  125. package/src/skills/registry.test.ts +0 -104
  126. package/src/skills/registry.ts +0 -413
  127. package/src/tools/maintenance.ts +0 -29
  128. package/src/tools/memoryTools.ts +0 -22
  129. package/src/tools/pluginPropose.ts +0 -41
  130. package/src/tools/registryWrite.test.ts +0 -101
  131. package/src/tools/registryWrite.ts +0 -182
  132. /package/src/{memory → core}/secrets.ts +0 -0
@@ -1,121 +0,0 @@
1
- // A plugin an agent wrote for a person to consider. Nothing here installs anything on its own:
2
- // the proposal waits in the Inbox; accepting it writes a package beside the person's data,
3
- // switched off and (when it brings code) untrusted. The person then reads it, switches it on,
4
- // and trusts it. No agent has a tool for either of those.
5
- import { existsSync, mkdirSync, mkdtempSync, renameSync, rmSync, writeFileSync } from 'node:fs'
6
- import { dirname, join } from 'node:path'
7
- import { z } from 'zod'
8
-
9
- import { invalid } from '../core/errors'
10
- import { now, safeRelative, writeJson } from '../core/fs'
11
- import { slugify } from '../core/projects'
12
- import { inbox } from '../memory/operations'
13
- import { looksSecret } from '../memory/secrets'
14
- import * as store from '../memory/store'
15
- import * as attachments from './attachments'
16
-
17
- /** The parts a plugin can say it extends. */
18
- export const EXTENDABLE = ['memory', 'wiki', 'history'] as const
19
-
20
- const MAX_FILES = 40
21
- const MAX_BYTES = 256 * 1024
22
- const HOOKS_FILES = ['hooks.mjs', 'hooks.js']
23
-
24
- export const pluginProposalSchema = z.object({
25
- name: z.string(),
26
- description: z.string().default(''),
27
- extends: z.array(z.string()).default([]),
28
- reason: z.string().default(''),
29
- files: z.array(z.object({ path: z.string(), content: z.string() }))
30
- })
31
- export type PluginProposal = z.infer<typeof pluginProposalSchema>
32
-
33
- interface Checked {
34
- slug: string
35
- extends: string[]
36
- hooks: string | null
37
- runsCode: boolean
38
- files: { path: string; content: string }[]
39
- }
40
-
41
- /** The proposal with its paths made safe, or the reason it cannot be a plugin. */
42
- export function checkPluginProposal(proposal: PluginProposal): Checked {
43
- const name = proposal.name.trim()
44
- const slug = slugify(name)
45
- if (!name || slug.startsWith('page-') || name.length > 64) throw invalid('Give the plugin a short name of letters and numbers.')
46
- if (!proposal.description.trim()) throw invalid('Say in one line what the plugin does; that line is what the user sees first.')
47
- const unknown = proposal.extends.find((part) => !(EXTENDABLE as readonly string[]).includes(part))
48
- if (unknown !== undefined) throw invalid(`A plugin can extend ${EXTENDABLE.join(', ')}; "${unknown}" is none of them.`)
49
- if (!proposal.files.length || proposal.files.length > MAX_FILES) throw invalid(`A plugin needs between 1 and ${MAX_FILES} files.`)
50
- const files: Checked['files'] = []
51
- let bytes = 0
52
- for (const file of proposal.files) {
53
- let path: string
54
- try {
55
- path = safeRelative(file.path.replace(/^\.\//, ''))
56
- } catch {
57
- throw invalid(`"${file.path}" is not a path inside the plugin.`)
58
- }
59
- const parts = path.split('/')
60
- if (parts.some((part) => part.startsWith('.') || part.includes(':') || part === 'node_modules')) throw invalid(`"${file.path}" cannot be part of a plugin.`)
61
- if (path === 'package.json') throw invalid('Leave package.json out; it is written from the name and description you gave.')
62
- if (files.some((existing) => existing.path.toLowerCase() === path.toLowerCase())) throw invalid(`"${file.path}" is listed twice.`)
63
- bytes += Buffer.byteLength(file.content, 'utf8')
64
- if (bytes > MAX_BYTES) throw invalid('The plugin is larger than 256 KiB.')
65
- if (looksSecret(file.content)) throw invalid(`"${file.path}" contains text that looks like a credential. A plugin reaches outside through a connected app, never with a key of its own.`)
66
- files.push({ path, content: file.content })
67
- }
68
- const hooks = files.find((file) => HOOKS_FILES.includes(file.path))?.path ?? null
69
- return { slug, extends: [...new Set(proposal.extends)], hooks, runsCode: hooks !== null || files.some((file) => file.path.startsWith('extensions/')), files }
70
- }
71
-
72
- /** Puts a proposed plugin in the Inbox, for a person to read before anything is written. */
73
- export function parkPluginProposal(wiki: string, from: string, proposal: PluginProposal): string {
74
- const checked = checkPluginProposal(proposal)
75
- const listed = checked.files.map((file) => `- ${file.path} (${file.content.length} characters)`).join('\n')
76
- const code = checked.runsCode ? '\n\nIt brings code. Accepting only saves the files: the code runs after you also switch the plugin on and trust it.' : ''
77
- inbox(
78
- wiki,
79
- 'proposed',
80
- `Proposed plugin "${proposal.name.trim()}"`,
81
- `${proposal.description.trim()}\n\n${proposal.reason.trim() || 'Review it before it is saved.'}${checked.extends.length ? `\n\nExtends: ${checked.extends.join(', ')}.` : ''}\n\nFiles:\n${listed}${code}\n\nFrom ${from}.`,
82
- [],
83
- null,
84
- { kind: 'plugin', proposal: { ...proposal, files: checked.files }, trigger: from }
85
- )
86
- store.log(wiki, `- ${now()} · ${from} · proposed plugin \`${checked.slug}\` · waiting in Inbox`)
87
- return checked.slug
88
- }
89
-
90
- /** Writes an accepted proposal as a package beside the person's data, switched off everywhere. */
91
- export function acceptPluginProposal(root: string, proposal: PluginProposal): string {
92
- const checked = checkPluginProposal(proposal)
93
- const directory = join(root, 'plugins')
94
- const target = join(directory, checked.slug)
95
- if (existsSync(target)) throw invalid(`There is already a plugin folder called "${checked.slug}". Remove it or rename the proposal.`)
96
- mkdirSync(directory, { recursive: true })
97
- // Written beside its place and moved in whole, so a half-written plugin is never found.
98
- const staging = mkdtempSync(join(directory, '.accepting-'))
99
- try {
100
- writeJson(join(staging, 'package.json'), {
101
- name: checked.slug,
102
- version: '0.1.0',
103
- description: proposal.description.trim(),
104
- keywords: ['pi-package'],
105
- ...(checked.extends.length || checked.hooks ? { harness: { ...(checked.extends.length ? { extends: checked.extends } : {}), ...(checked.hooks ? { hooks: `./${checked.hooks}` } : {}) } } : {})
106
- })
107
- for (const file of checked.files) {
108
- const path = join(staging, ...file.path.split('/'))
109
- mkdirSync(dirname(path), { recursive: true })
110
- writeFileSync(path, file.content, { flag: 'wx' })
111
- }
112
- renameSync(staging, target)
113
- } catch (error) {
114
- rmSync(staging, { recursive: true, force: true })
115
- throw error
116
- }
117
- const choices = attachments.load(root)
118
- attachments.set(choices, [`package:${checked.slug}`], {}, false, true)
119
- attachments.save(root, choices)
120
- return `Saved the plugin "${checked.slug}", switched off.${checked.runsCode ? ' Its code does not run until you switch it on and trust it.' : ' Switch it on to use it.'}`
121
- }
@@ -1,104 +0,0 @@
1
- import { existsSync, mkdirSync, mkdtempSync, readFileSync, symlinkSync, writeFileSync } from 'node:fs'
2
- import { tmpdir } from 'node:os'
3
- import { join } from 'node:path'
4
- import { expect, test } from 'vitest'
5
-
6
- import { writeJson } from '../core/fs'
7
- import { isDir, isFile } from '../memory/store'
8
- import { enabledFor, importSkills, itemBody, list, removeLinkedSkills, resources } from './registry'
9
-
10
- const temp = () => mkdtempSync(join(tmpdir(), 'harness-registry-'))
11
-
12
- test('lists items and resolves enablement', () => {
13
- const root = temp()
14
- mkdirSync(join(root, 'registry/rules'), { recursive: true })
15
- mkdirSync(join(root, 'registry/skills/release-notes'), { recursive: true })
16
- writeFileSync(join(root, 'registry/rules/tests.md'), '---\nname: Run tests\n---\n\nAlways run the test suite before finishing.\n')
17
- writeFileSync(join(root, 'registry/skills/release-notes/SKILL.md'), '# Release notes\n\nWrite release notes from commits.\n')
18
- const items = list(root)
19
- expect(items.map((item) => item.id)).toEqual(['rules/tests', 'skills/release-notes'])
20
- expect(items[0].name).toBe('Run tests')
21
- expect(items[1].description).toBe('Write release notes from commits.')
22
-
23
- writeJson(join(root, 'config/registry-enabled.json'), { p1: ['rules/tests'], global: ['skills/release-notes'] })
24
- const ids = (project: string | null) => enabledFor(root, project).map((item) => item.id)
25
- expect(ids('p1')).toEqual(['rules/tests', 'skills/release-notes'])
26
- expect(ids(null)).toEqual(['skills/release-notes'])
27
- expect(itemBody(items[0]).trim()).toBe('Always run the test suite before finishing.')
28
- expect(resources(root, null).skillPaths[0].endsWith('registry/skills/release-notes/SKILL.md')).toBe(true)
29
- })
30
-
31
- test('removes the links an earlier version made and keeps the user\'s own skills', () => {
32
- const root = temp()
33
- mkdirSync(join(root, 'registry/skills/release-notes'), { recursive: true })
34
- writeFileSync(join(root, 'registry/skills/release-notes/SKILL.md'), '# Release notes\n')
35
- writeFileSync(join(root, 'registry/skills/triage.md'), '# Triage\n')
36
- const skills = join(root, 'shared/skills')
37
- mkdirSync(join(skills, 'mine'), { recursive: true })
38
- mkdirSync(join(skills, 'agentx-handmade'), { recursive: true })
39
- writeFileSync(join(skills, 'agentx-handmade/SKILL.md'), "user's")
40
- symlinkSync(join(root, 'registry/skills/release-notes'), join(skills, 'agentx-release-notes'))
41
- mkdirSync(join(skills, 'agentx-triage'))
42
- symlinkSync(join(root, 'registry/skills/triage.md'), join(skills, 'agentx-triage/SKILL.md'))
43
-
44
- removeLinkedSkills(skills)
45
- expect(existsSync(join(skills, 'agentx-release-notes')) || existsSync(join(skills, 'agentx-triage'))).toBe(false)
46
- expect(isDir(join(skills, 'mine')) && isFile(join(skills, 'agentx-handmade/SKILL.md'))).toBe(true)
47
- expect(isFile(join(root, 'registry/skills/release-notes/SKILL.md')) && isFile(join(root, 'registry/skills/triage.md'))).toBe(true)
48
- removeLinkedSkills(join(root, 'no-such-folder'))
49
- })
50
-
51
- test('imports complete skill folders without replacing registry items', () => {
52
- const root = temp()
53
- const state = importSkills(root, [{
54
- name: 'Source Review',
55
- files: [
56
- { path: 'SKILL.md', content: '# Source review\n', encoding: 'utf8' },
57
- { path: 'references/checklist.md', content: 'Check the source first.\n', encoding: 'utf8' },
58
- { path: 'assets/icon.bin', content: Buffer.from([0, 1, 255]).toString('base64'), encoding: 'base64' }
59
- ]
60
- }])
61
-
62
- expect(state.items.map((item) => item.id)).toContain('skills/source-review')
63
- expect(readFileSync(join(root, 'registry/skills/source-review/references/checklist.md'), 'utf8')).toBe('Check the source first.\n')
64
- expect(readFileSync(join(root, 'registry/skills/source-review/assets/icon.bin'))).toEqual(Buffer.from([0, 1, 255]))
65
- expect(() => importSkills(root, [{ name: 'Source Review', files: [{ path: 'SKILL.md', content: 'replacement', encoding: 'utf8' }] }])).toThrow('already exists')
66
- expect(readFileSync(join(root, 'registry/skills/source-review/SKILL.md'), 'utf8')).toBe('# Source review\n')
67
- })
68
-
69
- test('rejects unsafe or incomplete skill folders without writing partial files', () => {
70
- const root = temp()
71
- expect(() => importSkills(root, [{
72
- name: 'Unsafe',
73
- files: [
74
- { path: 'SKILL.md', content: 'valid', encoding: 'utf8' },
75
- { path: '../escape.md', content: 'no', encoding: 'utf8' }
76
- ]
77
- }])).toThrow('path outside')
78
- expect(() => importSkills(root, [{ name: 'Missing root skill', files: [{ path: 'references/SKILL.md', content: 'no', encoding: 'utf8' }] }])).toThrow('must include SKILL.md')
79
- expect(list(root)).toEqual([])
80
- })
81
-
82
- test('does not replace an unindexed folder at the imported skill path', () => {
83
- const root = temp()
84
- const orphan = join(root, 'registry/skills/orphan')
85
- mkdirSync(orphan, { recursive: true })
86
- writeFileSync(join(orphan, 'notes.txt'), 'keep')
87
-
88
- expect(() => importSkills(root, [{ name: 'orphan', files: [{ path: 'SKILL.md', content: 'replacement', encoding: 'utf8' }] }])).toThrow('already exists')
89
- expect(readFileSync(join(orphan, 'notes.txt'), 'utf8')).toBe('keep')
90
- expect(existsSync(join(orphan, 'SKILL.md'))).toBe(false)
91
- })
92
-
93
- test('rejects portable path collisions before writing a skill folder', () => {
94
- const root = temp()
95
- expect(() => importSkills(root, [{
96
- name: 'Case collision',
97
- files: [
98
- { path: 'SKILL.md', content: 'valid', encoding: 'utf8' },
99
- { path: 'assets/Icon.png', content: 'one', encoding: 'utf8' },
100
- { path: 'assets/icon.png', content: 'two', encoding: 'utf8' }
101
- ]
102
- }])).toThrow('duplicate file path')
103
- expect(list(root)).toEqual([])
104
- })
@@ -1,413 +0,0 @@
1
- // Canonical skills, prompts, and rules under `<data root>/registry/`. Each item is a Markdown
2
- // file (a skill may also be a folder with `SKILL.md`) with optional `name`/`description`
3
- // frontmatter. These files are the single source of truth. Items are enabled globally and per
4
- // project; enabled items are loaded automatically, and any item can be retrieved on demand.
5
- //
6
- // Built-in items ship with the harness or the application that embeds it, in folders laid out
7
- // like `registry/`. They are read-only and on everywhere until a person switches one off; an
8
- // item of the same id in the data root replaces the built-in one.
9
- import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, unlinkSync, writeFileSync } from 'node:fs'
10
- import { basename, dirname, join } from 'node:path'
11
-
12
- import { invalid } from '../core/errors'
13
- import * as frontmatter from '../core/frontmatter'
14
- import { atomicWrite, now, readJsonOr, safeRelative } from '../core/fs'
15
- import { findProject } from '../core/projects'
16
- import { compareText, lines, takeChars } from '../core/text'
17
- import * as store from '../memory/store'
18
- import * as attachments from '../plugins/attachments'
19
-
20
- export const KINDS = ['skills', 'prompts', 'rules'] as const
21
- export type RegistryKind = (typeof KINDS)[number]
22
-
23
- export interface RegistryItem {
24
- /** `<kind>/<name>`, stable across edits. */
25
- id: string
26
- kind: string
27
- name: string
28
- description: string
29
- path: string
30
- /** Set on an item that ships with the harness or the application, not on a person's own. */
31
- builtin?: true
32
- }
33
-
34
- /** The capability id used when this registry item is shown in the plugin catalogue. */
35
- export function contributionId(item: Pick<RegistryItem, 'id' | 'builtin'>): string {
36
- return `${item.builtin ? 'included' : 'yours'}/${item.id}`
37
- }
38
-
39
- /** Limits registry items to the contributions frozen for a run. `undefined` means no limit. */
40
- export function limitToContributions(items: readonly RegistryItem[], contributions?: readonly string[]): RegistryItem[] {
41
- if (contributions === undefined) return [...items]
42
- const allowed = new Set(contributions)
43
- return items.filter((item) => allowed.has(contributionId(item)))
44
- }
45
-
46
- export interface SkillBundleInput {
47
- name: string
48
- files: { path: string; content: string; encoding: 'utf8' | 'base64' }[]
49
- }
50
-
51
- const MAX_SKILL_BUNDLES = 20
52
- const MAX_SKILL_FILES = 100
53
- const MAX_SKILL_IMPORT_BYTES = 10 * 1024 * 1024
54
-
55
- let builtinDirs: string[] = []
56
-
57
- /** The folders built-in items are read from, each laid out like `registry/`. Later folders win. */
58
- export function configureBuiltins(directories: readonly string[]): void {
59
- builtinDirs = [...directories]
60
- }
61
-
62
- function itemFrom(kind: string, stem: string, file: string): RegistryItem | null {
63
- let text: string
64
- try {
65
- text = readFileSync(file, 'utf8')
66
- } catch {
67
- return null
68
- }
69
- const parsed = frontmatter.parse(text)
70
- const name = parsed?.front.name
71
- const description = parsed?.front.description
72
- // A frontmatter block whose name or description is not text is treated as no frontmatter.
73
- const usable = parsed !== null && (name === undefined || name === null || typeof name === 'string') && (description === undefined || description === null || typeof description === 'string')
74
- const body = usable ? parsed.body : text
75
- return {
76
- id: `${kind}/${stem}`,
77
- kind,
78
- name: usable && typeof name === 'string' ? name : stem.replace(/[-_]/g, ' '),
79
- description: usable && typeof description === 'string' ? description : takeChars(lines(body).find((line) => line.trim() && !line.startsWith('#')) ?? '', 160),
80
- path: file
81
- }
82
- }
83
-
84
- const stemOf = (name: string): string => (name.lastIndexOf('.') > 0 ? name.slice(0, name.lastIndexOf('.')) : name)
85
-
86
- /** The name and description of a skill or prompt file anywhere on disk, read the way a registry item is. */
87
- export function describeFile(file: string): { name: string; description: string } | null {
88
- const stem = basename(file) === 'SKILL.md' ? basename(dirname(file)) : stemOf(basename(file))
89
- const item = itemFrom('', stem, file)
90
- return item && { name: item.name, description: item.description }
91
- }
92
-
93
- function itemsIn(directory: string): RegistryItem[] {
94
- const items: RegistryItem[] = []
95
- for (const kind of KINDS) {
96
- let names: string[]
97
- try {
98
- names = readdirSync(join(directory, kind))
99
- } catch {
100
- continue
101
- }
102
- for (const name of names) {
103
- const path = join(directory, kind, name)
104
- const stem = stemOf(name)
105
- if (stem.startsWith('.')) continue
106
- const file = store.isDir(path) ? join(path, 'SKILL.md') : name.endsWith('.md') ? path : null
107
- const item = file && itemFrom(kind, stem, file)
108
- if (item) items.push(item)
109
- }
110
- }
111
- return items
112
- }
113
-
114
- export function list(root: string): RegistryItem[] {
115
- const items = new Map<string, RegistryItem>()
116
- for (const directory of builtinDirs) for (const item of itemsIn(directory)) items.set(item.id, { ...item, builtin: true })
117
- for (const item of itemsIn(join(root, 'registry'))) items.set(item.id, item)
118
- return [...items.values()].sort((left, right) => compareText(left.id, right.id))
119
- }
120
-
121
- function enablementPath(root: string): string {
122
- return join(root, 'config/registry-enabled.json')
123
- }
124
-
125
- /** Key for items enabled everywhere. Older data roots used `personal`, which is read the same way. */
126
- export const GLOBAL_KEY = 'global'
127
-
128
- /**
129
- * The registry's earlier enablement file as it is on disk: ids keyed by project id, or `global`
130
- * for everyone. The choices now live with every other plugin's (`plugins/attachments.ts`), which
131
- * reads this file until the first change is saved.
132
- */
133
- export function enablement(root: string): Record<string, unknown> {
134
- const map = readJsonOr<unknown>(enablementPath(root), {})
135
- return map && typeof map === 'object' && !Array.isArray(map) ? (map as Record<string, unknown>) : {}
136
- }
137
-
138
- export function enablementIds(map: Record<string, unknown>, key: string): string[] {
139
- const ids = map[key]
140
- return Array.isArray(ids) ? ids.filter((id): id is string => typeof id === 'string') : []
141
- }
142
-
143
- /** Built-in items a person switched off. Everything else built in is on everywhere. */
144
- export const BUILTIN_OFF_KEY = 'builtinOff'
145
-
146
- /** Whether an id names a registry item, as `<kind>/<name>`. */
147
- export const isItemId = (id: string): boolean => KINDS.some((kind) => id.startsWith(`${kind}/`))
148
-
149
- /**
150
- * Items active for a caller. A built-in is on until it is switched off; a person's own is off
151
- * until it is switched on, everywhere, for the caller's project, or for the calling agent.
152
- */
153
- export function enabledFor(root: string, projectId: string | null, agentId: string | null = null, contributions?: readonly string[]): RegistryItem[] {
154
- if (contributions !== undefined) return limitToContributions(list(root), contributions)
155
- const choices = attachments.load(root)
156
- return list(root).filter((item) => attachments.attached(choices, [item.id], { agentId, projectId }, !!item.builtin))
157
- }
158
-
159
- /** An item's Markdown body without frontmatter. */
160
- export function itemBody(item: RegistryItem): string {
161
- try {
162
- const text = readFileSync(item.path, 'utf8')
163
- return frontmatter.split(text)?.body ?? text
164
- } catch {
165
- return ''
166
- }
167
- }
168
-
169
- export interface RegistryResources {
170
- ids: string[]
171
- skillPaths: string[]
172
- promptPaths: string[]
173
- }
174
-
175
- /** What a Pi session needs to load enabled items natively. Rule text reaches the model through the compiled prompt. */
176
- export function resources(root: string, projectId: string | null, agentId: string | null = null, contributions?: readonly string[]): RegistryResources {
177
- const items = enabledFor(root, projectId, agentId, contributions)
178
- return {
179
- ids: items.map((item) => item.id),
180
- skillPaths: items.filter((item) => item.kind === 'skills').map((item) => item.path),
181
- promptPaths: items.filter((item) => item.kind === 'prompts').map((item) => item.path)
182
- }
183
- }
184
-
185
- // Prefix of the entries an earlier version of the harness linked into a Pi agent folder's `skills/`.
186
- const LINKED_SKILL_PREFIX = 'agentx-'
187
-
188
- function isLinkedSkill(path: string): boolean {
189
- let meta
190
- try {
191
- meta = lstatSync(path)
192
- } catch {
193
- return false
194
- }
195
- if (meta.isSymbolicLink()) return true
196
- if (!meta.isDirectory()) return false
197
- // A single-file skill was linked as `agentx-<name>/SKILL.md` inside a folder the harness made.
198
- try {
199
- const entries = readdirSync(path)
200
- return entries.length === 1 && entries[0] === 'SKILL.md' && lstatSync(join(path, 'SKILL.md')).isSymbolicLink()
201
- } catch {
202
- return false
203
- }
204
- }
205
-
206
- /**
207
- * Removes the `agentx-<name>` links an earlier version kept in a terminal session's `skills/`
208
- * folder, leaving the user's own skills alone. Enabled skills now reach a session as paths.
209
- */
210
- export function removeLinkedSkills(skillsDir: string): void {
211
- let names: string[]
212
- try {
213
- names = readdirSync(skillsDir)
214
- } catch {
215
- return
216
- }
217
- for (const name of names) {
218
- const path = join(skillsDir, name)
219
- if (!name.startsWith(LINKED_SKILL_PREFIX) || !isLinkedSkill(path)) continue
220
- if (lstatSync(path).isSymbolicLink()) unlinkSync(path)
221
- else rmSync(path, { recursive: true, force: true })
222
- }
223
- }
224
-
225
- export interface RegistryState {
226
- items: RegistryItem[]
227
- enabled: Record<string, unknown>
228
- }
229
-
230
- /**
231
- * Enablement as a person sees it, in the earlier file's shape: ids keyed by project id, `global`
232
- * for everyone (built-ins that are on count as enabled everywhere), and the built-ins switched off.
233
- */
234
- function shown(items: readonly RegistryItem[], choices: attachments.Attachments): Record<string, unknown> {
235
- const builtin = items.filter((item) => item.builtin).map((item) => item.id)
236
- const off = choices.global.off.filter((id) => builtin.includes(id))
237
- const global = choices.global.on.filter((id) => isItemId(id) && !builtin.includes(id))
238
- const map: Record<string, unknown> = { [GLOBAL_KEY]: [...global, ...builtin.filter((id) => !off.includes(id))] }
239
- for (const [projectId, entries] of Object.entries(choices.projects)) map[projectId] = entries.on.filter(isItemId)
240
- if (off.length) map[BUILTIN_OFF_KEY] = off
241
- return map
242
- }
243
-
244
- export function state(root: string): RegistryState {
245
- const items = list(root)
246
- return { items, enabled: shown(items, attachments.load(root)) }
247
- }
248
-
249
- /** Sets enabled items for a project, or globally when `projectId` is `null`. */
250
- export function setEnabled(root: string, projectId: string | null, ids: string[]): RegistryState {
251
- if (projectId) findProject(root, projectId)
252
- const items = list(root)
253
- const unknown = ids.find((id) => !items.some((item) => item.id === id))
254
- if (unknown !== undefined) throw invalid(`Unknown registry item ${unknown}.`)
255
- const builtin = items.filter((item) => item.builtin).map((item) => item.id)
256
- const choices = attachments.load(root)
257
- const entries = projectId ? (choices.projects[projectId] ??= { on: [], off: [] }) : choices.global
258
- // Only the registry's own ids are replaced; other plugins' choices in the same scope stay.
259
- entries.on = [...entries.on.filter((id) => !isItemId(id)), ...ids.filter((id) => !builtin.includes(id))]
260
- entries.off = entries.off.filter((id) => !isItemId(id) || (!ids.includes(id) && !(!projectId && builtin.includes(id))))
261
- // A built-in left out of the global list was switched off.
262
- if (!projectId) entries.off.push(...builtin.filter((id) => !ids.includes(id)))
263
- attachments.save(root, choices)
264
- return { items, enabled: shown(items, choices) }
265
- }
266
-
267
- function kindDir(root: string, kind: string): string {
268
- if (!(KINDS as readonly string[]).includes(kind)) throw invalid('Unknown registry kind.')
269
- return join(root, 'registry', kind)
270
- }
271
-
272
- /** Creates a starter item from a name: a Markdown file, or a skill folder with `SKILL.md`. */
273
- export function create(root: string, kind: string, rawName: string): RegistryState {
274
- const directory = kindDir(root, kind)
275
- const name = rawName.trim()
276
- const slug = Array.from(name.toLowerCase(), (character) => (/^[a-z0-9]$/.test(character) ? character : '-')).join('').split('-').filter(Boolean).join('-')
277
- if (!slug || slug.length > 64) throw invalid('Use a short name with letters or numbers.')
278
- const description = 'Describe when to use this.'
279
- const target = kind === 'skills' ? join(directory, slug) : join(directory, `${slug}.md`)
280
- let exists = true
281
- try {
282
- lstatSync(target)
283
- } catch {
284
- exists = false
285
- }
286
- if (exists) throw invalid('An item with that name already exists.')
287
- if (kind === 'skills') atomicWrite(join(target, 'SKILL.md'), `---\nname: ${slug}\ndescription: ${description}\n---\n\n# ${name}\n\nSteps the agent should follow.\n`)
288
- else atomicWrite(target, `---\nname: ${name}\ndescription: ${description}\n---\n\n`)
289
- return state(root)
290
- }
291
-
292
- /** Imports complete user-owned skill folders without replacing existing or built-in items. */
293
- export function importSkills(root: string, bundles: SkillBundleInput[]): RegistryState {
294
- return importItems(root, 'skills', bundles)
295
- }
296
-
297
- /** Imports inert registry resources. Prompts and rules are single UTF-8 Markdown documents. */
298
- export function importItems(root: string, kind: RegistryKind, bundles: SkillBundleInput[]): RegistryState {
299
- if (!Array.isArray(bundles) || bundles.length === 0 || bundles.length > MAX_SKILL_BUNDLES) {
300
- throw invalid(`Import between 1 and ${MAX_SKILL_BUNDLES} skills at a time.`)
301
- }
302
-
303
- const directory = kindDir(root, kind)
304
- mkdirSync(directory, { recursive: true })
305
- const existing = new Set(list(root).map((item) => item.id))
306
- const seen = new Set<string>()
307
- let totalBytes = 0
308
- const prepared = bundles.map((bundle) => {
309
- if (!bundle || typeof bundle.name !== 'string' || !Array.isArray(bundle.files) || bundle.files.length === 0 || bundle.files.length > MAX_SKILL_FILES) {
310
- throw invalid('Each skill needs a name and a bounded list of files.')
311
- }
312
- const name = bundle.name.trim()
313
- if (name.length > 64) throw invalid('Use a skill name with at most 64 letters or numbers.')
314
- const slug = Array.from(name.toLowerCase(), (character) => (/^[a-z0-9]$/.test(character) ? character : '-')).join('').split('-').filter(Boolean).join('-')
315
- if (!slug || slug.length > 64) throw invalid('Use a skill name with at most 64 letters or numbers.')
316
- const id = `${kind}/${slug}`
317
- if (existing.has(id) || seen.has(id)) throw invalid(`The skill “${name}” already exists.`)
318
- try {
319
- lstatSync(join(directory, kind === 'skills' ? slug : `${slug}.md`))
320
- throw invalid(`The skill “${name}” already exists.`)
321
- } catch (error) {
322
- if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
323
- }
324
- seen.add(id)
325
-
326
- const files = new Map<string, Uint8Array>()
327
- for (const file of bundle.files) {
328
- if (!file || typeof file.path !== 'string' || file.path.length > 240 || typeof file.content !== 'string' || !['utf8', 'base64'].includes(file.encoding)) {
329
- throw invalid(`The skill “${name}” contains an invalid file.`)
330
- }
331
- let relative: string
332
- try {
333
- relative = safeRelative(file.path)
334
- } catch {
335
- throw invalid(`The skill “${name}” contains a path outside its folder.`)
336
- }
337
- if (relative.includes('\0') || relative.split('/').some((part) => part.startsWith('.') || part.includes(':'))) {
338
- throw invalid(`The skill “${name}” contains an unsupported file path.`)
339
- }
340
- const portablePath = relative.normalize('NFC').toLowerCase()
341
- if ([...files.keys()].some((path) => path.normalize('NFC').toLowerCase() === portablePath)) throw invalid(`The skill “${name}” contains a duplicate file path.`)
342
- let byteLength: number
343
- if (file.encoding === 'base64') {
344
- if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(file.content)) {
345
- throw invalid(`The file “${relative}” is not valid base64.`)
346
- }
347
- if (file.content.length > Math.ceil(MAX_SKILL_IMPORT_BYTES / 3) * 4) throw invalid('The skill import is larger than 10 MiB.')
348
- byteLength = Math.floor(file.content.length * 3 / 4) - (file.content.endsWith('==') ? 2 : file.content.endsWith('=') ? 1 : 0)
349
- } else {
350
- byteLength = Buffer.byteLength(file.content, 'utf8')
351
- }
352
- totalBytes += byteLength
353
- if (totalBytes > MAX_SKILL_IMPORT_BYTES) throw invalid('The skill import is larger than 10 MiB.')
354
- const bytes = file.encoding === 'base64' ? Buffer.from(file.content, 'base64') : Buffer.from(file.content, 'utf8')
355
- if (file.encoding === 'base64' && bytes.toString('base64') !== file.content) throw invalid(`The file “${relative}” is not valid base64.`)
356
- files.set(relative, bytes)
357
- }
358
- if (kind === 'skills' && !files.has('SKILL.md')) throw invalid(`The skill “${name}” must include SKILL.md at its root.`)
359
- if (kind !== 'skills' && (bundle.files.length !== 1 || bundle.files[0].encoding !== 'utf8' || bundle.files[0].path !== 'CONTENT.md')) throw invalid('A prompt or rule must contain one UTF-8 CONTENT.md file.')
360
- return { name, slug, files }
361
- })
362
-
363
- const staging = mkdtempSync(join(directory, '.agent-template-import-'))
364
- const installed: string[] = []
365
- try {
366
- for (const bundle of prepared) {
367
- const target = join(staging, bundle.slug)
368
- for (const [relative, bytes] of bundle.files) {
369
- const path = join(target, ...relative.split('/'))
370
- mkdirSync(dirname(path), { recursive: true })
371
- writeFileSync(path, bytes, { flag: 'wx' })
372
- }
373
- }
374
- for (const bundle of prepared) {
375
- const target = join(directory, kind === 'skills' ? bundle.slug : `${bundle.slug}.md`)
376
- renameSync(kind === 'skills' ? join(staging, bundle.slug) : join(staging, bundle.slug, 'CONTENT.md'), target)
377
- installed.push(target)
378
- }
379
- } catch (error) {
380
- for (const target of installed) rmSync(target, { recursive: true, force: true })
381
- throw error
382
- } finally {
383
- rmSync(staging, { recursive: true, force: true })
384
- }
385
- return state(root)
386
- }
387
-
388
- /**
389
- * Writes a skill, prompt, or rule (an accepted proposal), keeping an existing item's location.
390
- * The previous content and the new content are recorded as Wiki revisions of the item.
391
- */
392
- export function writeItem(root: string, kind: string, name: string, description: string, body: string, reason: string, actor = 'user'): string {
393
- const plural = kind === 'skill' ? 'skills' : kind === 'prompt' ? 'prompts' : kind === 'rule' ? 'rules' : null
394
- if (!plural) throw invalid('Only skills, prompts, and rules can be written.')
395
- const stem = store.slug(name)
396
- const id = `${plural}/${stem}`
397
- const existing = list(root).find((item) => item.id === id)
398
- // A built-in is never edited in place: the accepted text becomes the person's own item.
399
- const path = existing && !existing.builtin ? existing.path : plural === 'skills' ? join(kindDir(root, plural), stem, 'SKILL.md') : join(kindDir(root, plural), `${stem}.md`)
400
- const text = frontmatter.serialize({ name: plural === 'skills' ? stem : name.trim(), ...(description.trim() ? { description: description.trim() } : {}) }, body)
401
- const wiki = store.wikiDir(root)
402
- if (existing) {
403
- try {
404
- store.snapshot(wiki, id, readFileSync(existing.path, 'utf8'), actor, 'Before accepted proposal')
405
- } catch {
406
- // Nothing readable to keep.
407
- }
408
- }
409
- atomicWrite(path, text)
410
- store.snapshot(wiki, id, text, actor, reason)
411
- store.log(wiki, `- ${now()} · ${actor} · ${existing ? 'updated' : 'created'} \`${id}\` · ${reason}`)
412
- return id
413
- }