@sublang/spex 4.2.0 → 5.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.
- package/README.md +433 -89
- package/dist/cli.js +80 -42
- package/dist/commands/add.d.ts +12 -0
- package/dist/commands/add.js +138 -0
- package/dist/commands/auth-target.d.ts +11 -0
- package/dist/commands/auth-target.js +50 -0
- package/dist/commands/environment.d.ts +123 -0
- package/dist/commands/environment.js +411 -0
- package/dist/commands/export.d.ts +21 -0
- package/dist/commands/export.js +663 -0
- package/dist/commands/home.d.ts +56 -0
- package/dist/commands/home.js +215 -0
- package/dist/commands/install.d.ts +2 -0
- package/dist/commands/install.js +48 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +87 -0
- package/dist/commands/login.d.ts +2 -0
- package/dist/commands/login.js +211 -0
- package/dist/commands/logout.d.ts +2 -0
- package/dist/commands/logout.js +74 -0
- package/dist/commands/publish.d.ts +2 -0
- package/dist/commands/publish.js +120 -0
- package/dist/commands/remove.d.ts +2 -0
- package/dist/commands/remove.js +34 -0
- package/dist/commands/run.d.ts +50 -0
- package/dist/commands/run.js +309 -0
- package/dist/commands/search.d.ts +2 -0
- package/dist/commands/search.js +34 -0
- package/dist/commands/settle.d.ts +124 -0
- package/dist/commands/settle.js +475 -0
- package/dist/commands/update.d.ts +2 -0
- package/dist/commands/update.js +37 -0
- package/dist/commands/whoami.d.ts +2 -0
- package/dist/commands/whoami.js +75 -0
- package/dist/lib/environment/agent-folders.d.ts +14 -0
- package/dist/lib/environment/agent-folders.js +11 -0
- package/dist/lib/environment/archive.d.ts +26 -0
- package/dist/lib/environment/archive.js +91 -0
- package/dist/lib/environment/builtins.d.ts +112 -0
- package/dist/lib/environment/builtins.js +539 -0
- package/dist/lib/environment/citations.d.ts +75 -0
- package/dist/lib/environment/citations.js +267 -0
- package/dist/lib/environment/exports.d.ts +190 -0
- package/dist/lib/environment/exports.js +1029 -0
- package/dist/lib/environment/format.d.ts +133 -0
- package/dist/lib/environment/format.js +651 -0
- package/dist/lib/environment/git-source.d.ts +41 -0
- package/dist/lib/environment/git-source.js +159 -0
- package/dist/lib/environment/index.d.ts +20 -0
- package/dist/lib/environment/index.js +27 -0
- package/dist/lib/environment/install.d.ts +140 -0
- package/dist/lib/environment/install.js +694 -0
- package/dist/lib/environment/layout.d.ts +15 -0
- package/dist/lib/environment/layout.js +50 -0
- package/dist/lib/environment/lock.d.ts +87 -0
- package/dist/lib/environment/lock.js +305 -0
- package/dist/lib/environment/marker.d.ts +15 -0
- package/dist/lib/environment/marker.js +31 -0
- package/dist/lib/environment/own-paths.d.ts +59 -0
- package/dist/lib/environment/own-paths.js +174 -0
- package/dist/lib/environment/placed-record.d.ts +32 -0
- package/dist/lib/environment/placed-record.js +72 -0
- package/dist/lib/environment/registries.d.ts +25 -0
- package/dist/lib/environment/registries.js +75 -0
- package/dist/lib/environment/registry.d.ts +173 -0
- package/dist/lib/environment/registry.js +390 -0
- package/dist/lib/environment/requests.d.ts +108 -0
- package/dist/lib/environment/requests.js +403 -0
- package/dist/lib/environment/resolve.d.ts +70 -0
- package/dist/lib/environment/resolve.js +451 -0
- package/dist/lib/environment/semver.d.ts +30 -0
- package/dist/lib/environment/semver.js +130 -0
- package/dist/lib/environment/store.d.ts +23 -0
- package/dist/lib/environment/store.js +123 -0
- package/dist/lib/environment/tar.d.ts +27 -0
- package/dist/lib/environment/tar.js +219 -0
- package/dist/lib/files.d.ts +66 -0
- package/dist/lib/files.js +233 -0
- package/dist/lib/git-host.d.ts +579 -0
- package/dist/lib/git-host.js +1363 -0
- package/dist/lib/home-file.d.ts +124 -0
- package/dist/lib/home-file.js +288 -0
- package/dist/lib/instance.d.ts +63 -0
- package/dist/lib/instance.js +130 -0
- package/dist/lib/words.d.ts +32 -0
- package/dist/lib/words.js +27 -0
- package/dist/lint.js +8 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.js +7 -0
- package/dist/testing/standin-registry.d.ts +94 -0
- package/dist/testing/standin-registry.js +449 -0
- package/package.json +37 -3
- package/dist/attached-cli.d.ts +0 -13
- package/dist/attached-cli.js +0 -249
- package/dist/attached-client.d.ts +0 -53
- package/dist/attached-client.js +0 -237
- package/dist/generated-protocol.d.ts +0 -2
- package/dist/generated-protocol.js +0 -6
- package/dist/package-init.d.ts +0 -21
- package/dist/package-init.js +0 -442
|
@@ -0,0 +1,1029 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
// Exports (environments-6, environments-8, environments-9,
|
|
4
|
+
// environments-14, environments-16, environments-30, environments-41,
|
|
5
|
+
// DR-104, DR-115, DR-124, DR-174). Only what a release provides is
|
|
6
|
+
// placed, a support artifact installed and placed nowhere: each provided
|
|
7
|
+
// skill is copied — its `name` rewritten where aliased — and one skill
|
|
8
|
+
// is generated per provided playbook, into `<root>/.spex/skills/<name>/`;
|
|
9
|
+
// those are mirrored into the folder where each agent Spex drives reads
|
|
10
|
+
// project-level skills in the working folder, kept out of Git there by
|
|
11
|
+
// its `.git/info/exclude`, and, for your own group, into the user-level
|
|
12
|
+
// folders of the agents this device has. A playbook's skill goes only
|
|
13
|
+
// where the table lets it. A manifest beside them records what Spex
|
|
14
|
+
// wrote, so a re-export removes only its own and a skill the reader put
|
|
15
|
+
// there stays, the export reporting each name such a skill kept out; an
|
|
16
|
+
// unchanged skill is left as it is, and a changed one is swapped in
|
|
17
|
+
// whole.
|
|
18
|
+
// A provided spec stands in a project's `specs/packages/<pkg>.md`,
|
|
19
|
+
// derived from the release's file — the marker line naming its package
|
|
20
|
+
// and version, a blank line, then the file with its citations rewritten
|
|
21
|
+
// for the tree — whenever the set of placed specs changes, its version
|
|
22
|
+
// and its bytes' hash kept in the install record at `.spex/placed.json`,
|
|
23
|
+
// and the file left for the reader to commit: a copy holding the
|
|
24
|
+
// derivation is the package's, record or no record; one holding the
|
|
25
|
+
// bytes recorded follows its release; one edited since, its marker line
|
|
26
|
+
// or one the record names at its path, stands as the reader's; one
|
|
27
|
+
// whose package no longer places it leaves unless edited; and a file of
|
|
28
|
+
// its basename anywhere under `specs/` that is none of these blocks it.
|
|
29
|
+
// A project not scaffolded yet, its root holding no `specs/`, places no
|
|
30
|
+
// spec and reports it; your own group's clone, which is no project, has
|
|
31
|
+
// a generated skill hold the spec file instead.
|
|
32
|
+
// Playbooks go to the launcher as module locations at launch, bound to
|
|
33
|
+
// the environment whose lock exported them.
|
|
34
|
+
import { randomUUID } from "node:crypto";
|
|
35
|
+
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
36
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
37
|
+
import { parseDocument, stringify as stringifyYaml } from "yaml";
|
|
38
|
+
import { readVersioned, sha256, writeVersionedBytes } from "../files.js";
|
|
39
|
+
import { AGENT_FOLDERS } from "./agent-folders.js";
|
|
40
|
+
import { rewriteForTree } from "./citations.js";
|
|
41
|
+
import { ensureSpexDir, installedDir, skillsDir as generatedSkillsDir } from "./layout.js";
|
|
42
|
+
import { parseManifestText, providedArtifacts } from "./format.js";
|
|
43
|
+
import { isPathSource, resolutionVersion } from "./lock.js";
|
|
44
|
+
import { placedMarker, readPlacedMarker } from "./marker.js";
|
|
45
|
+
import { placedRecordPath, readPlacedRecord, serializePlacedRecord, writePlacedRecord } from "./placed-record.js";
|
|
46
|
+
/** The built-in spec package's name (environments-11). */
|
|
47
|
+
export const BUILTIN_PACKAGE_NAME = "sublang/playbooks";
|
|
48
|
+
/** The manifest an export leaves in each agent folder it writes. */
|
|
49
|
+
export const EXPORTS_MANIFEST = ".spex-exports.json";
|
|
50
|
+
const excludeBegin = (block) => `# >>> spex ${block} (managed by Spex; do not edit)`;
|
|
51
|
+
const excludeEnd = (block) => `# <<< spex ${block}`;
|
|
52
|
+
/** Where a spec package's files stand: its installed folder, or a path
|
|
53
|
+
* source's folder where its path resolves (environments-8). */
|
|
54
|
+
export function packageRoot(name, resolution, packagesDir, pathRoot) {
|
|
55
|
+
const source = resolution.source;
|
|
56
|
+
if (isPathSource(source))
|
|
57
|
+
return pathRoot === null ? null : join(pathRoot, ...source.path.split("/"));
|
|
58
|
+
return join(packagesDir, ...name.split("/"));
|
|
59
|
+
}
|
|
60
|
+
function readManifestAt(root) {
|
|
61
|
+
const file = join(root, "meta.yaml");
|
|
62
|
+
if (!existsSync(file))
|
|
63
|
+
return null;
|
|
64
|
+
return parseManifestText(readFileSync(file, "utf8")).manifest;
|
|
65
|
+
}
|
|
66
|
+
/** Copy a folder's files, writable, links and engine links left out. */
|
|
67
|
+
function copyTree(from, to) {
|
|
68
|
+
mkdirSync(to, { recursive: true });
|
|
69
|
+
for (const name of readdirSync(from).sort()) {
|
|
70
|
+
const source = join(from, name);
|
|
71
|
+
const target = join(to, name);
|
|
72
|
+
const stat = lstatSync(source);
|
|
73
|
+
if (stat.isDirectory()) {
|
|
74
|
+
if (name === "node_modules")
|
|
75
|
+
continue;
|
|
76
|
+
copyTree(source, target);
|
|
77
|
+
}
|
|
78
|
+
else if (stat.isFile()) {
|
|
79
|
+
copyFileSync(source, target);
|
|
80
|
+
chmodSync(target, stat.mode & 0o100 ? 0o755 : 0o644);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** A `name` as a YAML string, whatever YAML would read it as bare:
|
|
85
|
+
* four agents read a skill by it (environments-8, environments-16). */
|
|
86
|
+
function yamlName(name) {
|
|
87
|
+
return `name: ${stringifyYaml(name, { lineWidth: 0 }).trimEnd()}`;
|
|
88
|
+
}
|
|
89
|
+
/** Rewrite the `name` of a SKILL.md's frontmatter (Agent Skills: the
|
|
90
|
+
* name matches the folder). */
|
|
91
|
+
export function rewriteSkillName(text, name) {
|
|
92
|
+
const match = /^(---\r?\n)([\s\S]*?)(\r?\n---[ \t]*(?:\r?\n|$))/.exec(text);
|
|
93
|
+
if (!match)
|
|
94
|
+
return `---\n${yamlName(name)}\n---\n\n${text}`;
|
|
95
|
+
const front = match[2];
|
|
96
|
+
const line = /^name[ \t]*:.*$/m;
|
|
97
|
+
let rewritten;
|
|
98
|
+
if (line.test(front))
|
|
99
|
+
rewritten = front.replace(line, yamlName(name));
|
|
100
|
+
else {
|
|
101
|
+
const doc = parseDocument(front);
|
|
102
|
+
doc.set("name", name);
|
|
103
|
+
rewritten = doc.toString().replace(/\n$/, "");
|
|
104
|
+
}
|
|
105
|
+
return `${match[1]}${rewritten}${match[3]}${text.slice(match[0].length)}`;
|
|
106
|
+
}
|
|
107
|
+
/** The SKILL.md generated for an exported playbook (environments-16):
|
|
108
|
+
* for an agent a person runs in the working folder, never for a player
|
|
109
|
+
* Spex launches there, which sees the same skills. The description
|
|
110
|
+
* leaves the version to the body, so its bounded names keep it within
|
|
111
|
+
* the Agent Skills limit. */
|
|
112
|
+
export function playbookSkill(options) {
|
|
113
|
+
const { name, id, packageName, version } = options;
|
|
114
|
+
return [
|
|
115
|
+
"---",
|
|
116
|
+
yamlName(name),
|
|
117
|
+
`description: Starts the ${id} playbook of ${packageName} through Spex in this working folder. Not for an agent already playing a role in this or any playbook; that agent does the step it was given.`,
|
|
118
|
+
"---",
|
|
119
|
+
"",
|
|
120
|
+
`Run the playbook \`${id}\` of the spec package \`${packageName}\`, version \`${version}\`, through Spex in this working folder: start the playbook's command in a Spex session of this working folder.`,
|
|
121
|
+
"",
|
|
122
|
+
"If you are already inside a Spex session as a player of a playbook, given a role and a step, this skill does not apply: do the step you were given.",
|
|
123
|
+
"",
|
|
124
|
+
].join("\n");
|
|
125
|
+
}
|
|
126
|
+
/** The Agent Skills limits on a name and a description
|
|
127
|
+
* (https://agentskills.io/specification). */
|
|
128
|
+
const SKILL_NAME_MAX = 64;
|
|
129
|
+
const SKILL_DESCRIPTION_MAX = 1024;
|
|
130
|
+
/** The name a spec package's spec is exported under where no alias
|
|
131
|
+
* names it (environments-6): `<org>-<pkg>`, or `<pkg>` where that is
|
|
132
|
+
* longer than an Agent Skills name may be. */
|
|
133
|
+
export function specExportName(packageName) {
|
|
134
|
+
const joined = packageName.replace("/", "-");
|
|
135
|
+
return joined.length <= SKILL_NAME_MAX ? joined : packageName.slice(packageName.indexOf("/") + 1);
|
|
136
|
+
}
|
|
137
|
+
/** The SKILL.md generated for an exported spec (environments-16): what
|
|
138
|
+
* spec package it is, and the spec file beside it. */
|
|
139
|
+
export function specSkill(options) {
|
|
140
|
+
const { name, packageName, version, file } = options;
|
|
141
|
+
const [org, pkg] = packageName.split("/");
|
|
142
|
+
const about = (options.description ?? "").replace(/\s+/g, " ").trim();
|
|
143
|
+
const lead = `The spec of ${packageName} ${version}`;
|
|
144
|
+
const tail = `. Read ${file} beside this skill.`;
|
|
145
|
+
// The manifest's description, cut to fit the description's limit.
|
|
146
|
+
let summary = Array.from(about.replace(/[.。]+$/, ""));
|
|
147
|
+
const room = SKILL_DESCRIPTION_MAX - lead.length - tail.length - 2;
|
|
148
|
+
if (summary.length > room)
|
|
149
|
+
summary = [...Array.from(summary.slice(0, room - 1).join("").trimEnd()), "…"];
|
|
150
|
+
const description = summary.length > 0 ? `${lead}: ${summary.join("")}${tail}` : `${lead}${tail}`;
|
|
151
|
+
return [
|
|
152
|
+
"---",
|
|
153
|
+
stringifyYaml({ name, description }, { lineWidth: 0 }).trimEnd(),
|
|
154
|
+
"---",
|
|
155
|
+
"",
|
|
156
|
+
`The spec package \`${packageName}\` — org \`${org}\`, name \`${pkg}\`, version \`${version}\`${about ? `: ${about}` : "."}`,
|
|
157
|
+
"",
|
|
158
|
+
`Its spec is \`${file}\` beside this file: read it whole when a request names the spec package or works from it. Each of its items states one requirement, cited by its id.`,
|
|
159
|
+
"",
|
|
160
|
+
].join("\n");
|
|
161
|
+
}
|
|
162
|
+
function readExportsManifest(dir) {
|
|
163
|
+
const file = join(dir, EXPORTS_MANIFEST);
|
|
164
|
+
if (!existsSync(file))
|
|
165
|
+
return [];
|
|
166
|
+
try {
|
|
167
|
+
const value = JSON.parse(readFileSync(file, "utf8"));
|
|
168
|
+
return Array.isArray(value.skills) ? value.skills.filter((item) => typeof item === "string") : [];
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/** Whether two folders hold the same files, bytes and executable bits:
|
|
175
|
+
* what copyTree writes from one it would write from the other. */
|
|
176
|
+
function sameTree(a, b) {
|
|
177
|
+
let left;
|
|
178
|
+
let right;
|
|
179
|
+
try {
|
|
180
|
+
left = readdirSync(a).sort();
|
|
181
|
+
right = readdirSync(b).sort();
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
if (left.length !== right.length || left.some((name, index) => name !== right[index]))
|
|
187
|
+
return false;
|
|
188
|
+
for (const name of left) {
|
|
189
|
+
const x = lstatSync(join(a, name));
|
|
190
|
+
const y = lstatSync(join(b, name));
|
|
191
|
+
if (x.isDirectory() && y.isDirectory()) {
|
|
192
|
+
if (!sameTree(join(a, name), join(b, name)))
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
else if (x.isFile() && y.isFile()) {
|
|
196
|
+
if (x.size !== y.size || (x.mode & 0o100) !== (y.mode & 0o100))
|
|
197
|
+
return false;
|
|
198
|
+
if (!readFileSync(join(a, name)).equals(readFileSync(join(b, name))))
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
else
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
/** The staging folders a mirror makes beside an agent folder, outside
|
|
207
|
+
* every folder an agent reads skills from (environments-30). */
|
|
208
|
+
const STAGING_PREFIX = ".spex-staging-";
|
|
209
|
+
/** Whether anything stands at a path, a link included, dangling or not. */
|
|
210
|
+
function standing(path) {
|
|
211
|
+
try {
|
|
212
|
+
lstatSync(path);
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/** Where a path is on disk: its nearest standing ancestor with every
|
|
220
|
+
* link resolved, a link whose target is not there yet followed to that
|
|
221
|
+
* target, and the rest as written. Two agent folders that are one
|
|
222
|
+
* folder by a link resolve alike, before the folder exists too. */
|
|
223
|
+
function realLocation(path, depth = 0) {
|
|
224
|
+
const rest = [];
|
|
225
|
+
let current = path;
|
|
226
|
+
for (;;) {
|
|
227
|
+
try {
|
|
228
|
+
return join(realpathSync.native(current), ...rest.reverse());
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
let link = null;
|
|
232
|
+
try {
|
|
233
|
+
if (lstatSync(current).isSymbolicLink())
|
|
234
|
+
link = readlinkSync(current);
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
// Nothing stands here: an ancestor decides.
|
|
238
|
+
}
|
|
239
|
+
if (link !== null && depth < 16)
|
|
240
|
+
return realLocation(join(resolve(dirname(current), link), ...rest.reverse()), depth + 1);
|
|
241
|
+
const parent = dirname(current);
|
|
242
|
+
if (parent === current)
|
|
243
|
+
return path;
|
|
244
|
+
rest.push(basename(current));
|
|
245
|
+
current = parent;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
function mirror(skillsDir, names, folder, named, report, rename) {
|
|
250
|
+
// The folder as it is on disk, made there where a link points at it
|
|
251
|
+
// before it exists, so the staging copy beside it shares its
|
|
252
|
+
// filesystem even where a link puts it elsewhere.
|
|
253
|
+
const location = realLocation(folder);
|
|
254
|
+
mkdirSync(location, { recursive: true });
|
|
255
|
+
const dir = realpathSync.native(location);
|
|
256
|
+
const previous = readExportsManifest(dir);
|
|
257
|
+
for (const name of previous) {
|
|
258
|
+
if (names.includes(name))
|
|
259
|
+
continue;
|
|
260
|
+
rmSync(join(dir, name), { recursive: true, force: true });
|
|
261
|
+
if (!report.removed.includes(name))
|
|
262
|
+
report.removed.push(name);
|
|
263
|
+
}
|
|
264
|
+
const written = [];
|
|
265
|
+
// A changed skill is made whole beside the agent folder and renamed
|
|
266
|
+
// into place, the replaced one moved aside first and back where the
|
|
267
|
+
// rename fails; one whose files already match is left as it stands.
|
|
268
|
+
const staging = join(dirname(dir), `${STAGING_PREFIX}${randomUUID()}`);
|
|
269
|
+
try {
|
|
270
|
+
for (const name of names) {
|
|
271
|
+
const target = join(dir, name);
|
|
272
|
+
if (standing(target) && !previous.includes(name)) {
|
|
273
|
+
for (const as of named)
|
|
274
|
+
report.skipped.push({ name, folder: as });
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
written.push(name);
|
|
278
|
+
const source = join(skillsDir, name);
|
|
279
|
+
if (sameTree(source, target))
|
|
280
|
+
continue;
|
|
281
|
+
const staged = join(staging, "new", name);
|
|
282
|
+
copyTree(source, staged);
|
|
283
|
+
const replaced = join(staging, "old", name);
|
|
284
|
+
const hadTarget = standing(target);
|
|
285
|
+
if (hadTarget) {
|
|
286
|
+
mkdirSync(dirname(replaced), { recursive: true });
|
|
287
|
+
rename(target, replaced);
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
rename(staged, target);
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
if (hadTarget)
|
|
294
|
+
renameSync(replaced, target);
|
|
295
|
+
throw error;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
finally {
|
|
300
|
+
rmSync(staging, { recursive: true, force: true });
|
|
301
|
+
}
|
|
302
|
+
const manifest = join(dir, EXPORTS_MANIFEST);
|
|
303
|
+
const text = `${JSON.stringify({ format: 1, skills: written.sort() }, null, 2)}\n`;
|
|
304
|
+
let current = null;
|
|
305
|
+
try {
|
|
306
|
+
current = readFileSync(manifest, "utf8");
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
current = null;
|
|
310
|
+
}
|
|
311
|
+
if (current !== text)
|
|
312
|
+
writeFileSync(manifest, text);
|
|
313
|
+
return written;
|
|
314
|
+
}
|
|
315
|
+
/** The `info/exclude` file of the Git work tree a working folder lies
|
|
316
|
+
* in, where it lies in one, with the folder's path below the work
|
|
317
|
+
* tree's top, `/`-separated, empty at the top itself. */
|
|
318
|
+
export function gitExclude(workingFolder) {
|
|
319
|
+
const segments = [];
|
|
320
|
+
for (let dir = resolve(workingFolder);; dir = dirname(dir)) {
|
|
321
|
+
const dotGit = join(dir, ".git");
|
|
322
|
+
let stat;
|
|
323
|
+
try {
|
|
324
|
+
stat = statSync(dotGit);
|
|
325
|
+
}
|
|
326
|
+
catch {
|
|
327
|
+
stat = undefined;
|
|
328
|
+
}
|
|
329
|
+
if (stat) {
|
|
330
|
+
const prefix = segments.reverse().join("/");
|
|
331
|
+
if (stat.isDirectory())
|
|
332
|
+
return { file: join(dotGit, "info", "exclude"), prefix };
|
|
333
|
+
const match = /^gitdir:\s*(.+)$/m.exec(readFileSync(dotGit, "utf8"));
|
|
334
|
+
if (!match)
|
|
335
|
+
return null;
|
|
336
|
+
const gitDir = resolve(dir, match[1].trim());
|
|
337
|
+
const commonFile = join(gitDir, "commondir");
|
|
338
|
+
const common = existsSync(commonFile) ? resolve(gitDir, readFileSync(commonFile, "utf8").trim()) : gitDir;
|
|
339
|
+
return { file: join(common, "info", "exclude"), prefix };
|
|
340
|
+
}
|
|
341
|
+
if (dirname(dir) === dir)
|
|
342
|
+
return null;
|
|
343
|
+
segments.push(basename(dir));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/** The `info/exclude` file of a working folder's Git, where it lies in
|
|
347
|
+
* a work tree. */
|
|
348
|
+
export function gitExcludeFile(workingFolder) {
|
|
349
|
+
return gitExclude(workingFolder)?.file ?? null;
|
|
350
|
+
}
|
|
351
|
+
/** Replace a managed block of a working folder's `info/exclude`, under
|
|
352
|
+
* the version read: the file changed meanwhile is refused as a conflict,
|
|
353
|
+
* never overwritten (DR-111). The file is the folder's, so its mode is
|
|
354
|
+
* kept; one made new is 0644. A folder below its work tree's top keeps
|
|
355
|
+
* a block of its own, its anchored entries placed under its path, so
|
|
356
|
+
* two such folders of one work tree never replace each other's. */
|
|
357
|
+
export function writeExcludeBlock(workingFolder, entries, block = "exports") {
|
|
358
|
+
const found = gitExclude(workingFolder);
|
|
359
|
+
if (!found)
|
|
360
|
+
return;
|
|
361
|
+
const { file, prefix } = found;
|
|
362
|
+
const label = prefix === "" ? block : `${block} /${prefix}/`;
|
|
363
|
+
const begin = excludeBegin(label);
|
|
364
|
+
const end = excludeEnd(label);
|
|
365
|
+
const placed = prefix === "" ? entries : entries.map((entry) => (entry.startsWith("/") ? `/${prefix}${entry}` : entry));
|
|
366
|
+
const read = readVersioned(file);
|
|
367
|
+
const text = read.bytes === null ? "" : read.bytes.toString("utf8");
|
|
368
|
+
const lines = text.split("\n");
|
|
369
|
+
const kept = [];
|
|
370
|
+
let inside = false;
|
|
371
|
+
for (const line of lines) {
|
|
372
|
+
if (line === begin) {
|
|
373
|
+
inside = true;
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (line === end) {
|
|
377
|
+
inside = false;
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (!inside)
|
|
381
|
+
kept.push(line);
|
|
382
|
+
}
|
|
383
|
+
while (kept.length > 0 && kept[kept.length - 1] === "")
|
|
384
|
+
kept.pop();
|
|
385
|
+
const managed = placed.length > 0 ? [begin, ...placed, end] : [];
|
|
386
|
+
const out = [...kept, ...managed].join("\n");
|
|
387
|
+
const next = out.length > 0 ? `${out}\n` : "";
|
|
388
|
+
if (next === text)
|
|
389
|
+
return;
|
|
390
|
+
let mode = 0o644;
|
|
391
|
+
try {
|
|
392
|
+
mode = statSync(file).mode & 0o7777;
|
|
393
|
+
}
|
|
394
|
+
catch (error) {
|
|
395
|
+
if (error.code !== "ENOENT")
|
|
396
|
+
throw error;
|
|
397
|
+
}
|
|
398
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
399
|
+
writeVersionedBytes(file, next, read.version, mode);
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Remove what exports wrote into a working folder (environments-8): each
|
|
403
|
+
* agent folder's exported skills and their manifest, and the exports'
|
|
404
|
+
* block of the folder's `info/exclude`; a skill the reader placed stays.
|
|
405
|
+
* Called when the folder stops being paired with the spex repository.
|
|
406
|
+
*/
|
|
407
|
+
export function removeExports(workingFolder) {
|
|
408
|
+
const removed = [];
|
|
409
|
+
for (const folder of new Set(Object.values(AGENT_FOLDERS).map((folders) => folders.project))) {
|
|
410
|
+
const dir = join(workingFolder, ...folder.split("/"));
|
|
411
|
+
const manifest = join(dir, EXPORTS_MANIFEST);
|
|
412
|
+
if (!existsSync(manifest))
|
|
413
|
+
continue;
|
|
414
|
+
for (const name of readExportsManifest(dir)) {
|
|
415
|
+
rmSync(join(dir, name), { recursive: true, force: true });
|
|
416
|
+
removed.push(name);
|
|
417
|
+
}
|
|
418
|
+
rmSync(manifest, { force: true });
|
|
419
|
+
}
|
|
420
|
+
writeExcludeBlock(workingFolder, [], "exports");
|
|
421
|
+
return removed.sort();
|
|
422
|
+
}
|
|
423
|
+
function userFolder(userHome, folder) {
|
|
424
|
+
if (folder.startsWith("~/"))
|
|
425
|
+
return join(userHome, ...folder.slice(2).split("/"));
|
|
426
|
+
return isAbsolute(folder) ? folder : join(userHome, ...folder.split("/"));
|
|
427
|
+
}
|
|
428
|
+
/** A project's specs folder at its root. */
|
|
429
|
+
const SPECS = "specs";
|
|
430
|
+
/** The path from the root a spec `<pkg>` is placed at (environments-41). */
|
|
431
|
+
export function placedSpecPath(id) {
|
|
432
|
+
return `${SPECS}/packages/${id}.md`;
|
|
433
|
+
}
|
|
434
|
+
function isDirectory(path) {
|
|
435
|
+
try {
|
|
436
|
+
return statSync(path).isDirectory();
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
return false;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
/** The digest of something at a path that is no file: no file's bytes
|
|
443
|
+
* have it. */
|
|
444
|
+
const NOT_A_FILE = "not a file";
|
|
445
|
+
/** What stands at a root's path, as a digest: the SHA-256 of a file's
|
|
446
|
+
* bytes; null where nothing stands; and NOT_A_FILE where something else
|
|
447
|
+
* stands — a folder, a link, or a path under a file. */
|
|
448
|
+
function standingDigest(root, path) {
|
|
449
|
+
const file = join(root, ...path.split("/"));
|
|
450
|
+
try {
|
|
451
|
+
if (!lstatSync(file).isFile())
|
|
452
|
+
return NOT_A_FILE;
|
|
453
|
+
return sha256(readFileSync(file));
|
|
454
|
+
}
|
|
455
|
+
catch (error) {
|
|
456
|
+
return error.code === "ENOENT" ? null : NOT_A_FILE;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/** Every regular file under a root's `specs/` by its basename, each
|
|
460
|
+
* with its paths from the root, in a walk of sorted entries that follows
|
|
461
|
+
* no link: what a spec's basename, unique across the tree (meta-10),
|
|
462
|
+
* meets wherever a collection holds it (environments-41). */
|
|
463
|
+
function specsByBasename(root) {
|
|
464
|
+
const out = new Map();
|
|
465
|
+
const walk = (relativeDir) => {
|
|
466
|
+
let entries;
|
|
467
|
+
try {
|
|
468
|
+
entries = readdirSync(join(root, ...relativeDir.split("/"))).sort();
|
|
469
|
+
}
|
|
470
|
+
catch {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
for (const entry of entries) {
|
|
474
|
+
const path = `${relativeDir}/${entry}`;
|
|
475
|
+
let stat;
|
|
476
|
+
try {
|
|
477
|
+
stat = lstatSync(join(root, ...path.split("/")));
|
|
478
|
+
}
|
|
479
|
+
catch {
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (stat.isDirectory())
|
|
483
|
+
walk(path);
|
|
484
|
+
else if (stat.isFile())
|
|
485
|
+
out.set(entry, [...(out.get(entry) ?? []), path]);
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
walk(SPECS);
|
|
489
|
+
return out;
|
|
490
|
+
}
|
|
491
|
+
/** A record's waiting counts as a report lists them. */
|
|
492
|
+
function waitingList(waiting) {
|
|
493
|
+
return Object.keys(waiting).sort().map((name) => ({ name, count: waiting[name] }));
|
|
494
|
+
}
|
|
495
|
+
function specMode(root, workingFolder) {
|
|
496
|
+
if (workingFolder === null || realLocation(resolve(workingFolder)) !== realLocation(resolve(root)))
|
|
497
|
+
return "skills";
|
|
498
|
+
return isDirectory(join(root, SPECS)) ? "files" : "no-specs";
|
|
499
|
+
}
|
|
500
|
+
/** The Git attributes an install keeps in a project's `.gitattributes`
|
|
501
|
+
* (environments-44): the files Spex writes with LF — `spex.yaml`,
|
|
502
|
+
* `spex.lock` and the placed specs — checked out with LF on every
|
|
503
|
+
* device, so a checkout whose Git converts line endings, Windows by
|
|
504
|
+
* default, holds the bytes the lock and the placement record digest. */
|
|
505
|
+
const LF_RULES = ["spex.yaml text eol=lf", "spex.lock text eol=lf", "specs/packages/*.md text eol=lf"];
|
|
506
|
+
/** Append to the root's `.gitattributes` each LF rule it lacks, the rest
|
|
507
|
+
* of the file kept (environments-44) — an append, as DR-111 allows — and
|
|
508
|
+
* leave it the reader's to commit. */
|
|
509
|
+
function pinLineEndings(root) {
|
|
510
|
+
const file = join(root, ".gitattributes");
|
|
511
|
+
let prior = "";
|
|
512
|
+
try {
|
|
513
|
+
prior = readFileSync(file, "utf8");
|
|
514
|
+
}
|
|
515
|
+
catch (error) {
|
|
516
|
+
if (error.code !== "ENOENT")
|
|
517
|
+
throw error;
|
|
518
|
+
}
|
|
519
|
+
const present = new Set(prior.split(/\r?\n/).map((line) => line.trim()));
|
|
520
|
+
const missing = LF_RULES.filter((rule) => !present.has(rule));
|
|
521
|
+
if (missing.length === 0)
|
|
522
|
+
return;
|
|
523
|
+
const lead = prior === "" || prior.endsWith("\n") ? "" : "\n";
|
|
524
|
+
writeFileSync(file, `${lead}${missing.join("\n")}\n`, { flag: "a" });
|
|
525
|
+
}
|
|
526
|
+
/** Remove a copy only while it still holds the bytes recorded, checked
|
|
527
|
+
* at the instant before (DR-111); whether it went. */
|
|
528
|
+
function removeUnchanged(root, path, digest) {
|
|
529
|
+
if (standingDigest(root, path) !== digest)
|
|
530
|
+
return false;
|
|
531
|
+
rmSync(join(root, ...path.split("/")), { force: true });
|
|
532
|
+
return true;
|
|
533
|
+
}
|
|
534
|
+
/** The marker on the first line of the file at a root's path, where a
|
|
535
|
+
* file stands there and opens with one (environments-41). */
|
|
536
|
+
function standingMarker(root, path) {
|
|
537
|
+
const file = join(root, ...path.split("/"));
|
|
538
|
+
try {
|
|
539
|
+
return lstatSync(file).isFile() ? readPlacedMarker(readFileSync(file, "utf8")) : null;
|
|
540
|
+
}
|
|
541
|
+
catch {
|
|
542
|
+
return null;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
/** The bytes a provided spec is placed as (environments-41): the marker
|
|
546
|
+
* line naming its package and version, a blank line, then the release's
|
|
547
|
+
* file with each address rewritten for the specs standing — `standing`
|
|
548
|
+
* giving each package's placed path below `specs/` — and the packages
|
|
549
|
+
* its links wait for. */
|
|
550
|
+
function deriveSpec(source, standing) {
|
|
551
|
+
const self = placedSpecPath(source.id).slice(SPECS.length + 1);
|
|
552
|
+
const placedPathOf = (name) => (name === source.name ? self : standing.get(name) ?? null);
|
|
553
|
+
const rewritten = rewriteForTree(readFileSync(source.file, "utf8"), { placedPathOf, selfPath: self });
|
|
554
|
+
// Written with LF, whatever the release holds: Spex writes LF.
|
|
555
|
+
const text = `${placedMarker(source.name, source.version)}\n\n${rewritten.text}`.replace(/\r\n/g, "\n");
|
|
556
|
+
return { text, sha256: sha256(text), waiting: rewritten.waiting };
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Plan the placement of a root's provided specs (environments-41) from
|
|
560
|
+
* the files, their marker lines and the install record, writing nothing
|
|
561
|
+
* but through `remove`:
|
|
562
|
+
*
|
|
563
|
+
* 1. each spec claims `specs/packages/<pkg>.md`: the package whose copy
|
|
564
|
+
* stands there — by the record, or by the marker line, as on a fresh
|
|
565
|
+
* clone — takes it, else the first in name order, and a later one is
|
|
566
|
+
* blocked, as is one whose path a copy of a package whose files are
|
|
567
|
+
* not here holds, by the record or the marker line alike;
|
|
568
|
+
* 2. a recorded copy its package no longer places there — removed, no
|
|
569
|
+
* longer providing its spec, or now blocked — leaves through `remove`
|
|
570
|
+
* while it holds the bytes recorded, else stays as the project's own,
|
|
571
|
+
* reported edited where its package places no spec now;
|
|
572
|
+
* 3. a claimed spec is blocked by a file at its path the record does not
|
|
573
|
+
* name for it and whose marker line names another package or none, or
|
|
574
|
+
* by a file of its basename elsewhere under `specs/` whose marker line
|
|
575
|
+
* does not name its package: the derivation opens with that line, so
|
|
576
|
+
* such a file is never the derivation, whatever specs stand — which
|
|
577
|
+
* specs stand is known before any is derived, and no copy differs
|
|
578
|
+
* only for another's presumed standing;
|
|
579
|
+
* 4. each spec not blocked is derived for the specs standing — those not
|
|
580
|
+
* blocked, and the copies of packages whose files are not here that
|
|
581
|
+
* hold their paths — and is placed where the file holds the derivation, recorded
|
|
582
|
+
* with or without an earlier record, as on a fresh clone; written
|
|
583
|
+
* where no file stands or the file holds the bytes recorded; and
|
|
584
|
+
* otherwise edited, the package's by its marker line or its record:
|
|
585
|
+
* left standing, its record kept as it was, the release's version
|
|
586
|
+
* waiting where the marker, else the record, names another.
|
|
587
|
+
*/
|
|
588
|
+
function planSpecs(root, sources, unread, remove) {
|
|
589
|
+
const previous = readPlacedRecord(root)?.specs ?? {};
|
|
590
|
+
const record = {};
|
|
591
|
+
const placements = [];
|
|
592
|
+
const writes = [];
|
|
593
|
+
const byName = (a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
594
|
+
const ordered = [...sources].sort(byName);
|
|
595
|
+
const names = new Set(ordered.map((source) => source.name));
|
|
596
|
+
// A copy `remove` took leaves nothing standing at its path.
|
|
597
|
+
const gone = new Set();
|
|
598
|
+
const digestOf = (path) => (gone.has(path) ? null : standingDigest(root, path));
|
|
599
|
+
// A package whose files are not here keeps what the record says of its
|
|
600
|
+
// copy, which still holds its path (environments-8).
|
|
601
|
+
const held = new Map();
|
|
602
|
+
for (const name of Object.keys(previous).sort()) {
|
|
603
|
+
if (!unread.has(name) || names.has(name))
|
|
604
|
+
continue;
|
|
605
|
+
record[name] = previous[name];
|
|
606
|
+
held.set(previous[name].path, name);
|
|
607
|
+
}
|
|
608
|
+
// So does a committed copy its marker line names it in, record or no
|
|
609
|
+
// record, as a fresh clone finds it: it stands for the links citing
|
|
610
|
+
// it, and nothing is recorded of bytes no derivation here vouches for.
|
|
611
|
+
for (const name of [...unread].sort()) {
|
|
612
|
+
if (names.has(name) || record[name] !== undefined)
|
|
613
|
+
continue;
|
|
614
|
+
const path = placedSpecPath(name.slice(name.indexOf("/") + 1));
|
|
615
|
+
if (!held.has(path) && standingMarker(root, path)?.name === name)
|
|
616
|
+
held.set(path, name);
|
|
617
|
+
}
|
|
618
|
+
// 1. The basename's claim: the package whose copy stands at the path,
|
|
619
|
+
// else the first in name order.
|
|
620
|
+
const standsFor = (source) => {
|
|
621
|
+
const path = placedSpecPath(source.id);
|
|
622
|
+
const digest = digestOf(path);
|
|
623
|
+
if (digest === null || digest === NOT_A_FILE)
|
|
624
|
+
return false;
|
|
625
|
+
return previous[source.name]?.path === path || standingMarker(root, path)?.name === source.name;
|
|
626
|
+
};
|
|
627
|
+
const claimants = [...ordered].sort((a, b) => Number(standsFor(b)) - Number(standsFor(a)) || byName(a, b));
|
|
628
|
+
const claims = new Map();
|
|
629
|
+
const blocked = new Set();
|
|
630
|
+
for (const source of claimants) {
|
|
631
|
+
const path = placedSpecPath(source.id);
|
|
632
|
+
if (claims.has(path) || held.has(path))
|
|
633
|
+
blocked.add(source.name);
|
|
634
|
+
else
|
|
635
|
+
claims.set(path, source);
|
|
636
|
+
}
|
|
637
|
+
// 2. A copy no longer placed for its package leaves unless edited.
|
|
638
|
+
for (const name of Object.keys(previous).sort()) {
|
|
639
|
+
if (record[name] !== undefined)
|
|
640
|
+
continue;
|
|
641
|
+
const entry = previous[name];
|
|
642
|
+
if (claims.get(entry.path)?.name === name)
|
|
643
|
+
continue;
|
|
644
|
+
const digest = digestOf(entry.path);
|
|
645
|
+
if (digest === null)
|
|
646
|
+
continue;
|
|
647
|
+
if (digest === entry.sha256 && remove(entry.path, entry.sha256)) {
|
|
648
|
+
gone.add(entry.path);
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
if (!names.has(name))
|
|
652
|
+
placements.push({ name, path: entry.path, state: "edited", waiting: [] });
|
|
653
|
+
}
|
|
654
|
+
// 3. What stands at each claimed path, and the files that block.
|
|
655
|
+
const candidates = new Map([...claims].map(([path, source]) => {
|
|
656
|
+
const entry = previous[source.name];
|
|
657
|
+
const found = {
|
|
658
|
+
path,
|
|
659
|
+
digest: digestOf(path),
|
|
660
|
+
recorded: entry !== undefined && entry.path === path ? entry : null,
|
|
661
|
+
marker: gone.has(path) ? null : standingMarker(root, path),
|
|
662
|
+
};
|
|
663
|
+
return [source.name, found];
|
|
664
|
+
}));
|
|
665
|
+
const elsewhere = specsByBasename(root);
|
|
666
|
+
for (const [name, { path, digest, recorded, marker }] of candidates) {
|
|
667
|
+
if (digest === NOT_A_FILE || (digest !== null && recorded === null && marker?.name !== name))
|
|
668
|
+
blocked.add(name);
|
|
669
|
+
const others = (elsewhere.get(basename(path)) ?? []).filter((other) => other !== path && !gone.has(other));
|
|
670
|
+
if (others.some((other) => standingMarker(root, other)?.name !== name))
|
|
671
|
+
blocked.add(name);
|
|
672
|
+
}
|
|
673
|
+
// 4. Each derived for the specs standing: placed, written or edited.
|
|
674
|
+
const standing = new Map();
|
|
675
|
+
for (const source of ordered)
|
|
676
|
+
if (!blocked.has(source.name))
|
|
677
|
+
standing.set(source.name, placedSpecPath(source.id).slice(SPECS.length + 1));
|
|
678
|
+
for (const [path, name] of held)
|
|
679
|
+
if (digestOf(path) !== null)
|
|
680
|
+
standing.set(name, path.slice(SPECS.length + 1));
|
|
681
|
+
for (const source of ordered) {
|
|
682
|
+
const path = placedSpecPath(source.id);
|
|
683
|
+
const candidate = candidates.get(source.name);
|
|
684
|
+
if (candidate === undefined || blocked.has(source.name)) {
|
|
685
|
+
placements.push({ name: source.name, path, state: "blocked", waiting: [] });
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
const { digest, recorded, marker } = candidate;
|
|
689
|
+
const spec = deriveSpec(source, standing);
|
|
690
|
+
if (digest !== null && digest !== spec.sha256 && digest !== recorded?.sha256) {
|
|
691
|
+
// Edited since it was placed: the reader's to merge.
|
|
692
|
+
if (recorded !== null)
|
|
693
|
+
record[source.name] = recorded;
|
|
694
|
+
const known = marker?.name === source.name ? marker.version : recorded?.version;
|
|
695
|
+
const waiting = known !== undefined && known !== source.version ? { waitingVersion: source.version } : {};
|
|
696
|
+
placements.push({ name: source.name, path, state: "edited", waiting: [], ...waiting });
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
if (digest !== spec.sha256)
|
|
700
|
+
writes.push({ path, text: spec.text, digest });
|
|
701
|
+
record[source.name] = { path, version: source.version, sha256: spec.sha256, waiting: spec.waiting };
|
|
702
|
+
placements.push({ name: source.name, path, state: "placed", waiting: waitingList(spec.waiting) });
|
|
703
|
+
}
|
|
704
|
+
return { placements: placements.sort(byName), writes, record };
|
|
705
|
+
}
|
|
706
|
+
/** Place the provided specs of a root holding `specs/` (environments-41)
|
|
707
|
+
* as `planSpecs` decides: a copy that leaves removed while it holds the
|
|
708
|
+
* bytes recorded, each file written under the version read so a hand
|
|
709
|
+
* edit meanwhile is refused (DR-111), and the record written whole only
|
|
710
|
+
* where it changed, removed once it records nothing. */
|
|
711
|
+
function placeSpecs(root, sources, unread) {
|
|
712
|
+
const plan = planSpecs(root, sources, unread, (path, digest) => removeUnchanged(root, path, digest));
|
|
713
|
+
for (const { path, text, digest } of plan.writes) {
|
|
714
|
+
// A project file the reader commits.
|
|
715
|
+
const file = join(root, ...path.split("/"));
|
|
716
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
717
|
+
writeVersionedBytes(file, text, digest, 0o644);
|
|
718
|
+
}
|
|
719
|
+
const record = { format: 1, specs: plan.record };
|
|
720
|
+
const recordFile = placedRecordPath(root);
|
|
721
|
+
let current = null;
|
|
722
|
+
try {
|
|
723
|
+
current = readFileSync(recordFile, "utf8");
|
|
724
|
+
}
|
|
725
|
+
catch {
|
|
726
|
+
current = null;
|
|
727
|
+
}
|
|
728
|
+
if (Object.keys(plan.record).length === 0) {
|
|
729
|
+
if (current !== null)
|
|
730
|
+
rmSync(recordFile, { force: true });
|
|
731
|
+
}
|
|
732
|
+
else if (current !== serializePlacedRecord(record)) {
|
|
733
|
+
writePlacedRecord(root, record);
|
|
734
|
+
}
|
|
735
|
+
return plan.placements;
|
|
736
|
+
}
|
|
737
|
+
/** The provided specs of a lock as an install places them, from release
|
|
738
|
+
* files that stand here as the lock locks them, and the spec packages
|
|
739
|
+
* whose files do not: not installed, another version installed, or a
|
|
740
|
+
* path source missing where its path resolves (environments-8). */
|
|
741
|
+
function lockedSpecs(root, lock, pathRoot) {
|
|
742
|
+
const sources = [];
|
|
743
|
+
const unread = new Set();
|
|
744
|
+
const packagesDir = installedDir(root);
|
|
745
|
+
for (const name of Object.keys(lock.packages).sort()) {
|
|
746
|
+
const resolution = lock.packages[name];
|
|
747
|
+
const dir = packageRoot(name, resolution, packagesDir, pathRoot);
|
|
748
|
+
let manifest = null;
|
|
749
|
+
try {
|
|
750
|
+
manifest = dir === null ? null : readManifestAt(dir);
|
|
751
|
+
}
|
|
752
|
+
catch {
|
|
753
|
+
manifest = null;
|
|
754
|
+
}
|
|
755
|
+
const locked = resolutionVersion(resolution);
|
|
756
|
+
if (dir === null || manifest === null || (locked !== undefined && manifest.version !== locked)) {
|
|
757
|
+
unread.add(name);
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
const provided = providedArtifacts(manifest);
|
|
761
|
+
for (const id of [...new Set(Object.values(resolution.exports))].sort()) {
|
|
762
|
+
const language = resolution.artifacts[id]?.language;
|
|
763
|
+
if (manifest.artifacts[id]?.kind !== "spec" || !provided.has(id) || !language)
|
|
764
|
+
continue;
|
|
765
|
+
const file = join(dir, "specs", language, `${id}.md`);
|
|
766
|
+
if (existsSync(file))
|
|
767
|
+
sources.push({ name, id, version: locked ?? manifest.version, file });
|
|
768
|
+
else
|
|
769
|
+
unread.add(name);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
return { sources, unread };
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* Where each provided spec of a lock stands under a project's root, read
|
|
776
|
+
* from the files, their marker lines and the install record as a
|
|
777
|
+
* placement finds them, writing nothing (environments-14,
|
|
778
|
+
* environments-41), so `spex list` and an install agree: waiting for
|
|
779
|
+
* `specs/` where the root holds none; else placed, edited or blocked as
|
|
780
|
+
* an install reports it. A spec whose package's files are not here as
|
|
781
|
+
* the lock locks them is left out, its recorded copy holding its path,
|
|
782
|
+
* and so is one whose copy is not written yet.
|
|
783
|
+
*/
|
|
784
|
+
export function readSpecPlacements(root, lock, pathRoot = root) {
|
|
785
|
+
const { sources, unread } = lockedSpecs(root, lock, pathRoot);
|
|
786
|
+
if (!isDirectory(join(root, SPECS)))
|
|
787
|
+
return sources.map(({ name, id }) => ({ name, path: placedSpecPath(id), state: "no-specs", waiting: [] }));
|
|
788
|
+
const plan = planSpecs(root, sources, unread, () => true);
|
|
789
|
+
const unwritten = new Set(plan.writes.filter((write) => write.digest === null).map((write) => write.path));
|
|
790
|
+
return plan.placements.filter((placement) => placement.state !== "placed" || !unwritten.has(placement.path));
|
|
791
|
+
}
|
|
792
|
+
/** Export an installed environment (environments-8). */
|
|
793
|
+
export async function exportEnvironment(options) {
|
|
794
|
+
const { root: envRoot, lock, workingFolder } = options;
|
|
795
|
+
const packagesDir = installedDir(envRoot);
|
|
796
|
+
const pathRoot = options.pathRoot === undefined ? workingFolder : options.pathRoot;
|
|
797
|
+
const report = { skills: [], folders: [], removed: [], skipped: [], missing: [], specs: [] };
|
|
798
|
+
const playbookSkills = new Set();
|
|
799
|
+
const mode = specMode(envRoot, workingFolder);
|
|
800
|
+
const specSources = [];
|
|
801
|
+
// Spec packages whose provided spec cannot be read here: what the
|
|
802
|
+
// record says of their copies stands.
|
|
803
|
+
const unread = new Set();
|
|
804
|
+
ensureSpexDir(envRoot);
|
|
805
|
+
// The skills are generated whole into a folder beside `.spex/skills/`
|
|
806
|
+
// and swapped in only where they differ, so an export that changes
|
|
807
|
+
// nothing rewrites no file (cli-4).
|
|
808
|
+
const finalSkillsDir = generatedSkillsDir(envRoot);
|
|
809
|
+
const skillsDir = join(dirname(finalSkillsDir), `.skills-${randomUUID()}`);
|
|
810
|
+
rmSync(skillsDir, { recursive: true, force: true });
|
|
811
|
+
mkdirSync(skillsDir, { recursive: true });
|
|
812
|
+
try {
|
|
813
|
+
for (const name of Object.keys(lock.packages).sort()) {
|
|
814
|
+
const resolution = lock.packages[name];
|
|
815
|
+
const root = packageRoot(name, resolution, packagesDir, pathRoot);
|
|
816
|
+
const manifest = root === null ? null : readManifestAt(root);
|
|
817
|
+
if (root === null || manifest === null) {
|
|
818
|
+
if (Object.keys(resolution.exports).length > 0)
|
|
819
|
+
report.missing.push(name);
|
|
820
|
+
unread.add(name);
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
const version = resolutionVersion(resolution) ?? manifest.version;
|
|
824
|
+
// A support artifact, or one of a kind the release does not offer,
|
|
825
|
+
// is installed with the rest and placed nowhere; the lock's names
|
|
826
|
+
// stay as resolved (environments-6).
|
|
827
|
+
const provided = providedArtifacts(manifest);
|
|
828
|
+
for (const exported of Object.keys(resolution.exports).sort()) {
|
|
829
|
+
const id = resolution.exports[exported];
|
|
830
|
+
const artifact = manifest.artifacts[id];
|
|
831
|
+
const language = resolution.artifacts[id]?.language;
|
|
832
|
+
if (!artifact || !language || !provided.has(id))
|
|
833
|
+
continue;
|
|
834
|
+
const target = join(skillsDir, exported);
|
|
835
|
+
if (artifact.kind === "skill") {
|
|
836
|
+
const folder = join(root, "skills", language, id);
|
|
837
|
+
if (!existsSync(folder)) {
|
|
838
|
+
if (!report.missing.includes(name))
|
|
839
|
+
report.missing.push(name);
|
|
840
|
+
continue;
|
|
841
|
+
}
|
|
842
|
+
copyTree(folder, target);
|
|
843
|
+
if (exported !== id) {
|
|
844
|
+
const skill = join(target, "SKILL.md");
|
|
845
|
+
writeFileSync(skill, rewriteSkillName(readFileSync(skill, "utf8"), exported));
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
else if (artifact.kind === "playbook") {
|
|
849
|
+
// No skill runs a playbook whose folder is not here.
|
|
850
|
+
if (!existsSync(join(root, "playbooks", language, id))) {
|
|
851
|
+
if (!report.missing.includes(name))
|
|
852
|
+
report.missing.push(name);
|
|
853
|
+
continue;
|
|
854
|
+
}
|
|
855
|
+
mkdirSync(target, { recursive: true });
|
|
856
|
+
writeFileSync(join(target, "SKILL.md"), playbookSkill({ name: exported, id, packageName: name, version }));
|
|
857
|
+
playbookSkills.add(exported);
|
|
858
|
+
}
|
|
859
|
+
else if (artifact.kind === "spec") {
|
|
860
|
+
const file = join(root, "specs", language, `${id}.md`);
|
|
861
|
+
if (!existsSync(file)) {
|
|
862
|
+
if (!report.missing.includes(name))
|
|
863
|
+
report.missing.push(name);
|
|
864
|
+
unread.add(name);
|
|
865
|
+
continue;
|
|
866
|
+
}
|
|
867
|
+
// In a project's `specs/`, a file placed after the skills
|
|
868
|
+
// (environments-41); in a project holding no `specs/`, nowhere,
|
|
869
|
+
// reported waiting for it; else, your own group's, a generated
|
|
870
|
+
// skill, the spec file in its chosen language byte for byte
|
|
871
|
+
// beside a SKILL.md saying what spec package it is (DR-115).
|
|
872
|
+
if (mode === "files") {
|
|
873
|
+
specSources.push({ name, id, version, file });
|
|
874
|
+
continue;
|
|
875
|
+
}
|
|
876
|
+
if (mode === "no-specs") {
|
|
877
|
+
report.specs.push({ name, path: placedSpecPath(id), state: "no-specs", waiting: [] });
|
|
878
|
+
continue;
|
|
879
|
+
}
|
|
880
|
+
mkdirSync(target, { recursive: true });
|
|
881
|
+
copyFileSync(file, join(target, `${id}.md`));
|
|
882
|
+
chmodSync(join(target, `${id}.md`), 0o644);
|
|
883
|
+
writeFileSync(join(target, "SKILL.md"), specSkill({ name: exported, packageName: name, version, description: manifest.description, file: `${id}.md` }));
|
|
884
|
+
}
|
|
885
|
+
else
|
|
886
|
+
continue;
|
|
887
|
+
report.skills.push(exported);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
report.skills.sort();
|
|
891
|
+
}
|
|
892
|
+
catch (error) {
|
|
893
|
+
rmSync(skillsDir, { recursive: true, force: true });
|
|
894
|
+
throw error;
|
|
895
|
+
}
|
|
896
|
+
if (sameTree(skillsDir, finalSkillsDir))
|
|
897
|
+
rmSync(skillsDir, { recursive: true, force: true });
|
|
898
|
+
else {
|
|
899
|
+
rmSync(finalSkillsDir, { recursive: true, force: true });
|
|
900
|
+
renameSync(skillsDir, finalSkillsDir);
|
|
901
|
+
}
|
|
902
|
+
const mirrored = mirrorExports(options, finalSkillsDir, report, playbookSkills);
|
|
903
|
+
// A project's `.gitattributes`, then the specs after the skills, in the
|
|
904
|
+
// project's `specs/`, never listed in its Git's exclude: they are the
|
|
905
|
+
// reader's to commit.
|
|
906
|
+
if (mode !== "skills")
|
|
907
|
+
pinLineEndings(envRoot);
|
|
908
|
+
if (mode === "files")
|
|
909
|
+
mirrored.specs = placeSpecs(envRoot, specSources, unread);
|
|
910
|
+
return mirrored;
|
|
911
|
+
}
|
|
912
|
+
function mirrorExports(options, skillsDir, report, playbookSkills) {
|
|
913
|
+
const { workingFolder, userHome } = options;
|
|
914
|
+
const readers = (level, root, present) => {
|
|
915
|
+
const byLocation = new Map();
|
|
916
|
+
for (const [agent, entry] of Object.entries(AGENT_FOLDERS).sort(([a], [b]) => a.localeCompare(b))) {
|
|
917
|
+
if (!present(agent))
|
|
918
|
+
continue;
|
|
919
|
+
const folder = level === "project" ? entry.project : entry.user;
|
|
920
|
+
const dir = level === "project" ? join(root, ...folder.split("/")) : userFolder(root, folder);
|
|
921
|
+
const location = realLocation(dir);
|
|
922
|
+
const known = byLocation.get(location) ?? { folders: [], dir, agents: [], playbooks: false };
|
|
923
|
+
if (!known.folders.includes(folder))
|
|
924
|
+
known.folders.push(folder);
|
|
925
|
+
known.agents.push(agent);
|
|
926
|
+
known.playbooks ||= entry.playbooks;
|
|
927
|
+
byLocation.set(location, known);
|
|
928
|
+
}
|
|
929
|
+
return [...byLocation.values()];
|
|
930
|
+
};
|
|
931
|
+
const exported = (playbooks) => report.skills.filter((name) => playbooks || !playbookSkills.has(name));
|
|
932
|
+
const rename = options.rename ?? renameSync;
|
|
933
|
+
const excluded = [];
|
|
934
|
+
if (workingFolder !== null) {
|
|
935
|
+
for (const { folders, dir, agents, playbooks } of readers("project", workingFolder, () => true)) {
|
|
936
|
+
const written = mirror(skillsDir, exported(playbooks), dir, folders, report, rename);
|
|
937
|
+
report.folders.push({ agents, level: "project", dir });
|
|
938
|
+
for (const folder of folders)
|
|
939
|
+
excluded.push(`/${folder}/${EXPORTS_MANIFEST}`, ...written.map((name) => `/${folder}/${name}/`), `/${dirname(folder)}/${STAGING_PREFIX}*/`);
|
|
940
|
+
// The staging copy sits beside the folder as it is on disk, which a
|
|
941
|
+
// link can move elsewhere in the working folder.
|
|
942
|
+
const staging = relative(realpathSync.native(workingFolder), dirname(realpathSync.native(dir)));
|
|
943
|
+
if (!staging.startsWith("..") && !isAbsolute(staging)) {
|
|
944
|
+
const line = `/${staging === "" ? "" : `${staging.split(sep).join("/")}/`}${STAGING_PREFIX}*/`;
|
|
945
|
+
if (!excluded.includes(line))
|
|
946
|
+
excluded.push(line);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
writeExcludeBlock(workingFolder, excluded);
|
|
950
|
+
}
|
|
951
|
+
if (userHome !== null) {
|
|
952
|
+
const present = new Set(options.agents);
|
|
953
|
+
for (const { folders, dir, agents, playbooks } of readers("user", userHome, (agent) => present.has(agent))) {
|
|
954
|
+
mirror(skillsDir, exported(playbooks), dir, folders, report, rename);
|
|
955
|
+
report.folders.push({ agents, level: "user", dir });
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
return report;
|
|
959
|
+
}
|
|
960
|
+
/** The module candidates of a playbook artifact folder, preferred first. */
|
|
961
|
+
export function playbookModuleCandidates(folder, id) {
|
|
962
|
+
return [
|
|
963
|
+
join(folder, `${id}.playbook`, `${id}.registry.js`),
|
|
964
|
+
join(folder, `${id}.playbook`, `${id}.registry.mjs`),
|
|
965
|
+
join(folder, `${id}.registry.mjs`),
|
|
966
|
+
join(folder, `${id}.registry.js`),
|
|
967
|
+
];
|
|
968
|
+
}
|
|
969
|
+
/** Each exported playbook's module location (environments-9): the
|
|
970
|
+
* installed artifact under the root's `.spex/packages/`, or a path
|
|
971
|
+
* source's where its path resolves, `pathRoot` being the folder its path
|
|
972
|
+
* is relative to. Keyed by the exported name. */
|
|
973
|
+
export function moduleLocations(lock, root, pathRoot) {
|
|
974
|
+
const out = new Map();
|
|
975
|
+
const packagesDir = installedDir(root);
|
|
976
|
+
for (const name of Object.keys(lock.packages).sort()) {
|
|
977
|
+
const resolution = lock.packages[name];
|
|
978
|
+
const root = packageRoot(name, resolution, packagesDir, pathRoot);
|
|
979
|
+
if (root === null)
|
|
980
|
+
continue;
|
|
981
|
+
const pathSource = isPathSource(resolution.source);
|
|
982
|
+
const manifest = pathSource ? readManifestAt(root) : null;
|
|
983
|
+
const version = resolutionVersion(resolution) ?? readManifestAt(root)?.version ?? "";
|
|
984
|
+
for (const exported of Object.keys(resolution.exports).sort()) {
|
|
985
|
+
const id = resolution.exports[exported];
|
|
986
|
+
const language = resolution.artifacts[id]?.language;
|
|
987
|
+
if (!language)
|
|
988
|
+
continue;
|
|
989
|
+
const prefix = `playbooks/${language}/${id}/`;
|
|
990
|
+
const isPlaybook = pathSource ? manifest?.artifacts[id]?.kind === "playbook" : resolution.files.some((file) => file.path.startsWith(prefix));
|
|
991
|
+
if (!isPlaybook)
|
|
992
|
+
continue;
|
|
993
|
+
const candidates = playbookModuleCandidates(join(root, "playbooks", language, id), id);
|
|
994
|
+
const found = candidates.find((candidate) => existsSync(candidate));
|
|
995
|
+
out.set(exported, {
|
|
996
|
+
module: found ?? candidates[0],
|
|
997
|
+
package: name,
|
|
998
|
+
version,
|
|
999
|
+
builtin: name === BUILTIN_PACKAGE_NAME,
|
|
1000
|
+
id,
|
|
1001
|
+
present: found !== undefined,
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
return out;
|
|
1006
|
+
}
|
|
1007
|
+
/** The modules a session hands the launcher (environments-9): each
|
|
1008
|
+
* enabled playbook from the project's environment, else your own
|
|
1009
|
+
* group's; one no environment exports is a config error naming the
|
|
1010
|
+
* playbook and the spex repository whose environment lacks it. */
|
|
1011
|
+
export function launchModules(enabled, project, own) {
|
|
1012
|
+
const modules = new Map();
|
|
1013
|
+
const errors = [];
|
|
1014
|
+
for (const playbook of [...new Set(enabled)].sort()) {
|
|
1015
|
+
const fromProject = project?.locations.get(playbook);
|
|
1016
|
+
if (fromProject) {
|
|
1017
|
+
modules.set(playbook, { ...fromProject, repository: project.repository });
|
|
1018
|
+
continue;
|
|
1019
|
+
}
|
|
1020
|
+
const fromOwn = own.locations.get(playbook);
|
|
1021
|
+
if (fromOwn) {
|
|
1022
|
+
modules.set(playbook, { ...fromOwn, repository: own.repository });
|
|
1023
|
+
continue;
|
|
1024
|
+
}
|
|
1025
|
+
errors.push({ playbook, repository: (project ?? own).repository });
|
|
1026
|
+
}
|
|
1027
|
+
return errors.length > 0 ? { ok: false, errors } : { ok: true, modules };
|
|
1028
|
+
}
|
|
1029
|
+
//# sourceMappingURL=exports.js.map
|