@ipab/cli 0.1.0-rc.1 → 0.1.0-rc.11
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 +234 -9
- package/dist/app-authoring.d.ts +15 -0
- package/dist/app-authoring.d.ts.map +1 -1
- package/dist/app-authoring.js +144 -58
- package/dist/app-authoring.js.map +1 -1
- package/dist/app-definition.d.ts +52 -57
- package/dist/app-definition.d.ts.map +1 -1
- package/dist/app-definition.js +33 -38
- package/dist/app-definition.js.map +1 -1
- package/dist/app-prebuild.d.ts +18 -0
- package/dist/app-prebuild.d.ts.map +1 -1
- package/dist/app-prebuild.js +46 -0
- package/dist/app-prebuild.js.map +1 -1
- package/dist/app-templates.d.ts +0 -1
- package/dist/app-templates.d.ts.map +1 -1
- package/dist/app-templates.js +8 -46
- package/dist/app-templates.js.map +1 -1
- package/dist/assembly.d.ts +7 -0
- package/dist/assembly.d.ts.map +1 -0
- package/dist/assembly.js +130 -0
- package/dist/assembly.js.map +1 -0
- package/dist/authoring.d.ts +7 -8
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +59 -16
- package/dist/authoring.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- package/dist/builtins/template.bundle-viewer.car +0 -0
- package/dist/builtins/template.check.car +0 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +1 -23
- package/dist/builtins.js.map +1 -1
- package/dist/bundled-skills.d.ts +14 -0
- package/dist/bundled-skills.d.ts.map +1 -0
- package/dist/bundled-skills.js +34 -0
- package/dist/bundled-skills.js.map +1 -0
- package/dist/cli.d.ts +8 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1967 -258
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +3 -0
- package/dist/config-secret.d.ts.map +1 -0
- package/dist/config-secret.js +12 -0
- package/dist/config-secret.js.map +1 -0
- package/dist/config.d.ts +11 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +166 -8
- package/dist/config.js.map +1 -1
- package/dist/content-provider.d.ts +32 -0
- package/dist/content-provider.d.ts.map +1 -0
- package/dist/content-provider.js +222 -0
- package/dist/content-provider.js.map +1 -0
- package/dist/context-types.d.ts +12 -0
- package/dist/context-types.d.ts.map +1 -0
- package/dist/context-types.js +2 -0
- package/dist/context-types.js.map +1 -0
- package/dist/context.d.ts +26 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +54 -10
- package/dist/context.js.map +1 -1
- package/dist/daemon-ui.d.ts +93 -0
- package/dist/daemon-ui.d.ts.map +1 -0
- package/dist/daemon-ui.js +713 -0
- package/dist/daemon-ui.js.map +1 -0
- package/dist/daemon.d.ts +21 -0
- package/dist/daemon.d.ts.map +1 -0
- package/dist/daemon.js +317 -0
- package/dist/daemon.js.map +1 -0
- package/dist/doctor.d.ts +32 -0
- package/dist/doctor.d.ts.map +1 -0
- package/dist/doctor.js +688 -0
- package/dist/doctor.js.map +1 -0
- package/dist/errors.d.ts +1 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -20
- package/dist/errors.js.map +1 -1
- package/dist/get.d.ts +3 -0
- package/dist/get.d.ts.map +1 -0
- package/dist/get.js +80 -0
- package/dist/get.js.map +1 -0
- package/dist/history.d.ts.map +1 -1
- package/dist/history.js +6 -14
- package/dist/history.js.map +1 -1
- package/dist/host-error.d.ts +9 -0
- package/dist/host-error.d.ts.map +1 -0
- package/dist/host-error.js +21 -0
- package/dist/host-error.js.map +1 -0
- package/dist/identity.d.ts +16 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +119 -0
- package/dist/identity.js.map +1 -0
- package/dist/ipc.d.ts +5 -2
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +124 -12
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +26 -0
- package/dist/kubo-install.d.ts.map +1 -0
- package/dist/kubo-install.js +724 -0
- package/dist/kubo-install.js.map +1 -0
- package/dist/kubo-peering.d.ts +11 -0
- package/dist/kubo-peering.d.ts.map +1 -0
- package/dist/kubo-peering.js +93 -0
- package/dist/kubo-peering.js.map +1 -0
- package/dist/kubo-peers.d.ts +12 -0
- package/dist/kubo-peers.d.ts.map +1 -0
- package/dist/kubo-peers.js +119 -0
- package/dist/kubo-peers.js.map +1 -0
- package/dist/kubo-release.d.ts +22 -0
- package/dist/kubo-release.d.ts.map +1 -0
- package/dist/kubo-release.js +176 -0
- package/dist/kubo-release.js.map +1 -0
- package/dist/kubo-topology.d.ts +19 -0
- package/dist/kubo-topology.d.ts.map +1 -0
- package/dist/kubo-topology.js +113 -0
- package/dist/kubo-topology.js.map +1 -0
- package/dist/kubo.d.ts +85 -0
- package/dist/kubo.d.ts.map +1 -0
- package/dist/kubo.js +755 -0
- package/dist/kubo.js.map +1 -0
- package/dist/pins.d.ts +16 -18
- package/dist/pins.d.ts.map +1 -1
- package/dist/pins.js +144 -106
- package/dist/pins.js.map +1 -1
- package/dist/pivot-rpc.d.ts +62 -0
- package/dist/pivot-rpc.d.ts.map +1 -0
- package/dist/pivot-rpc.js +407 -0
- package/dist/pivot-rpc.js.map +1 -0
- package/dist/pivot-service.d.ts +16 -0
- package/dist/pivot-service.d.ts.map +1 -0
- package/dist/pivot-service.js +50 -0
- package/dist/pivot-service.js.map +1 -0
- package/dist/publish-source.d.ts +3 -0
- package/dist/publish-source.d.ts.map +1 -0
- package/dist/publish-source.js +85 -0
- package/dist/publish-source.js.map +1 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +9 -4
- package/dist/runtime.js.map +1 -1
- package/dist/search-index.d.ts +64 -0
- package/dist/search-index.d.ts.map +1 -0
- package/dist/search-index.js +409 -0
- package/dist/search-index.js.map +1 -0
- package/dist/search-service.d.ts +23 -0
- package/dist/search-service.d.ts.map +1 -0
- package/dist/search-service.js +116 -0
- package/dist/search-service.js.map +1 -0
- package/dist/skill-definition.d.ts +6 -0
- package/dist/skill-definition.d.ts.map +1 -0
- package/dist/skill-definition.js +38 -0
- package/dist/skill-definition.js.map +1 -0
- package/dist/skill-source.d.ts +9 -0
- package/dist/skill-source.d.ts.map +1 -0
- package/dist/skill-source.js +24 -0
- package/dist/skill-source.js.map +1 -0
- package/dist/skills.d.ts +104 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +894 -0
- package/dist/skills.js.map +1 -0
- package/dist/source-bundle.d.ts +3 -8
- package/dist/source-bundle.d.ts.map +1 -1
- package/dist/source-bundle.js +17 -12
- package/dist/source-bundle.js.map +1 -1
- package/dist/space-path.d.ts +3 -0
- package/dist/space-path.d.ts.map +1 -0
- package/dist/space-path.js +17 -0
- package/dist/space-path.js.map +1 -0
- package/dist/space.d.ts +16 -4
- package/dist/space.d.ts.map +1 -1
- package/dist/space.js +170 -24
- package/dist/space.js.map +1 -1
- package/dist/state-storage-windows.d.ts +2 -0
- package/dist/state-storage-windows.d.ts.map +1 -0
- package/dist/state-storage-windows.js +114 -0
- package/dist/state-storage-windows.js.map +1 -0
- package/dist/state-storage.d.ts +5 -0
- package/dist/state-storage.d.ts.map +1 -0
- package/dist/state-storage.js +333 -0
- package/dist/state-storage.js.map +1 -0
- package/dist/state.d.ts +135 -13
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +577 -92
- package/dist/state.js.map +1 -1
- package/dist/swarm-envelope.d.ts +57 -0
- package/dist/swarm-envelope.d.ts.map +1 -0
- package/dist/swarm-envelope.js +299 -0
- package/dist/swarm-envelope.js.map +1 -0
- package/dist/swarm-files.d.ts +6 -0
- package/dist/swarm-files.d.ts.map +1 -0
- package/dist/swarm-files.js +116 -0
- package/dist/swarm-files.js.map +1 -0
- package/dist/swarm-meta.d.ts +15 -0
- package/dist/swarm-meta.d.ts.map +1 -0
- package/dist/swarm-meta.js +68 -0
- package/dist/swarm-meta.js.map +1 -0
- package/dist/swarm-observations.d.ts +40 -0
- package/dist/swarm-observations.d.ts.map +1 -0
- package/dist/swarm-observations.js +271 -0
- package/dist/swarm-observations.js.map +1 -0
- package/dist/swarm.d.ts +537 -0
- package/dist/swarm.d.ts.map +1 -0
- package/dist/swarm.js +4991 -0
- package/dist/swarm.js.map +1 -0
- package/dist/template-definition.d.ts +47 -52
- package/dist/template-definition.d.ts.map +1 -1
- package/dist/template-definition.js +36 -41
- package/dist/template-definition.js.map +1 -1
- package/dist/template-paths.d.ts +7 -0
- package/dist/template-paths.d.ts.map +1 -0
- package/dist/template-paths.js +23 -0
- package/dist/template-paths.js.map +1 -0
- package/dist/template-presentation.d.ts +20 -0
- package/dist/template-presentation.d.ts.map +1 -0
- package/dist/template-presentation.js +133 -0
- package/dist/template-presentation.js.map +1 -0
- package/dist/template-source.d.ts +4 -1
- package/dist/template-source.d.ts.map +1 -1
- package/dist/template-source.js +11 -4
- package/dist/template-source.js.map +1 -1
- package/dist/templates.d.ts +11 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +53 -0
- package/dist/templates.js.map +1 -0
- package/dist/unixfs-tree.d.ts +67 -0
- package/dist/unixfs-tree.d.ts.map +1 -0
- package/dist/unixfs-tree.js +329 -0
- package/dist/unixfs-tree.js.map +1 -0
- package/package.json +14 -7
- package/src/app-authoring.ts +240 -76
- package/src/app-definition.ts +33 -39
- package/src/app-prebuild.ts +61 -0
- package/src/app-templates.ts +17 -73
- package/src/assembly.ts +185 -0
- package/src/authoring.ts +99 -18
- package/src/builtins.ts +2 -27
- package/src/bundled-skills.ts +60 -0
- package/src/cli.ts +2461 -323
- package/src/config-secret.ts +14 -0
- package/src/config.ts +214 -8
- package/src/content-provider.ts +299 -0
- package/src/context-types.ts +11 -0
- package/src/context.ts +98 -10
- package/src/daemon-ui.ts +812 -0
- package/src/daemon.ts +386 -0
- package/src/doctor.ts +1092 -0
- package/src/errors.ts +1 -25
- package/src/get.ts +91 -0
- package/src/history.ts +5 -15
- package/src/host-error.ts +26 -0
- package/src/identity.ts +175 -0
- package/src/ipc.ts +160 -11
- package/src/kubo-install.ts +936 -0
- package/src/kubo-peering.ts +126 -0
- package/src/kubo-peers.ts +144 -0
- package/src/kubo-release.ts +230 -0
- package/src/kubo-topology.ts +148 -0
- package/src/kubo.ts +1088 -0
- package/src/pins.ts +188 -126
- package/src/pivot-rpc.ts +601 -0
- package/src/pivot-service.ts +87 -0
- package/src/publish-source.ts +103 -0
- package/src/runtime.ts +16 -4
- package/src/search-index.ts +560 -0
- package/src/search-service.ts +152 -0
- package/src/skill-definition.ts +53 -0
- package/src/skill-source.ts +29 -0
- package/src/skills.ts +1236 -0
- package/src/source-bundle.ts +26 -13
- package/src/space-path.ts +30 -0
- package/src/space.ts +230 -42
- package/src/state-storage-windows.ts +134 -0
- package/src/state-storage.ts +378 -0
- package/src/state.ts +875 -108
- package/src/swarm-envelope.ts +461 -0
- package/src/swarm-files.ts +126 -0
- package/src/swarm-meta.ts +98 -0
- package/src/swarm-observations.ts +393 -0
- package/src/swarm.ts +7392 -0
- package/src/template-definition.ts +36 -41
- package/src/template-paths.ts +43 -0
- package/src/template-presentation.ts +151 -0
- package/src/template-source.ts +19 -5
- package/src/templates.ts +77 -0
- package/src/unixfs-tree.ts +401 -0
package/src/skills.ts
ADDED
|
@@ -0,0 +1,1236 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { execFile, spawn } from "node:child_process";
|
|
3
|
+
import { lstat, mkdir, readFile, readdir, rm } from "node:fs/promises";
|
|
4
|
+
import {
|
|
5
|
+
basename,
|
|
6
|
+
dirname,
|
|
7
|
+
isAbsolute,
|
|
8
|
+
join,
|
|
9
|
+
resolve,
|
|
10
|
+
delimiter,
|
|
11
|
+
} from "node:path";
|
|
12
|
+
import { homedir } from "node:os";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
import { promisify } from "node:util";
|
|
15
|
+
import { CID } from "multiformats/cid";
|
|
16
|
+
import { relativePath } from "./space.js";
|
|
17
|
+
import { swarmUrl } from "./config.js";
|
|
18
|
+
import type { AgentInfo } from "skills";
|
|
19
|
+
import { HostError, message, object } from "./errors.js";
|
|
20
|
+
import type { DaemonSyncSummary } from "./daemon-ui.js";
|
|
21
|
+
import { nodeError, scaffoldFiles } from "./authoring.js";
|
|
22
|
+
import type { bundledSkillSource } from "./skill-source.js";
|
|
23
|
+
import { getBundle } from "./get.js";
|
|
24
|
+
import {
|
|
25
|
+
privateDirectory,
|
|
26
|
+
readPrivate,
|
|
27
|
+
withPrivateLock,
|
|
28
|
+
writePrivate,
|
|
29
|
+
} from "./swarm-files.js";
|
|
30
|
+
import {
|
|
31
|
+
swarmHeader,
|
|
32
|
+
swarmSkills,
|
|
33
|
+
syncPivotReplicationAndIndex,
|
|
34
|
+
syncSwarmRegistry,
|
|
35
|
+
syncSwarmTemplates,
|
|
36
|
+
type SharedSkill,
|
|
37
|
+
type SwarmContext,
|
|
38
|
+
} from "./swarm.js";
|
|
39
|
+
|
|
40
|
+
export const skillsInstaller = "skills@1.9.1";
|
|
41
|
+
export const bundledSkillOrigin = "bundled:ipab";
|
|
42
|
+
type Fingerprint = Record<string, string>;
|
|
43
|
+
interface Installation extends SharedSkill {
|
|
44
|
+
swarm: string;
|
|
45
|
+
path: string;
|
|
46
|
+
agents: string[];
|
|
47
|
+
files: Fingerprint;
|
|
48
|
+
pending?: { before: Fingerprint | null; expected: Fingerprint };
|
|
49
|
+
retentions?: { store: string; roots: string[] }[];
|
|
50
|
+
}
|
|
51
|
+
interface Ledger {
|
|
52
|
+
v: 1;
|
|
53
|
+
installations: Record<string, Installation>;
|
|
54
|
+
}
|
|
55
|
+
const ledgerBytes = 64 * 1024 * 1024;
|
|
56
|
+
async function saveLedger(directory: string, ledger: Ledger): Promise<void> {
|
|
57
|
+
if (Buffer.byteLength(JSON.stringify(ledger)) > ledgerBytes)
|
|
58
|
+
throw new HostError(
|
|
59
|
+
"Skills attribution exceeds its 64 MiB limit; preserve the existing ledger and installations.",
|
|
60
|
+
);
|
|
61
|
+
await writePrivate(directory, "installations.json", ledger);
|
|
62
|
+
}
|
|
63
|
+
export interface SkillsPort {
|
|
64
|
+
home: string;
|
|
65
|
+
catalog: () => Promise<{
|
|
66
|
+
supported: readonly AgentInfo[];
|
|
67
|
+
detected: readonly AgentInfo[];
|
|
68
|
+
}>;
|
|
69
|
+
install: (
|
|
70
|
+
source: string,
|
|
71
|
+
identity: string,
|
|
72
|
+
agent: string,
|
|
73
|
+
env: NodeJS.ProcessEnv,
|
|
74
|
+
) => Promise<{ code: number; stdout: string; stderr: string }>;
|
|
75
|
+
}
|
|
76
|
+
export function requireSkillsNode(version = process.versions.node): void {
|
|
77
|
+
const [major = 0, minor = 0] = version.split(".").map(Number);
|
|
78
|
+
if (major < 22 || (major === 22 && minor < 20))
|
|
79
|
+
throw new HostError(
|
|
80
|
+
"Skills operations require Node >=22.20.0, the minimum supported IPAB runtime, for the pinned official skills@1.9.1 installer.",
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
async function npxCommand(): Promise<{ command: string; prefix: string[] }> {
|
|
84
|
+
if (process.platform !== "win32") return { command: "npx", prefix: [] };
|
|
85
|
+
// Avoid cmd.exe interpolation of local paths; use npm's actual npx entry.
|
|
86
|
+
for (const directory of [
|
|
87
|
+
dirname(process.execPath),
|
|
88
|
+
...(process.env.PATH ?? "").split(delimiter),
|
|
89
|
+
]) {
|
|
90
|
+
const entry = join(directory, "node_modules", "npm", "bin", "npx-cli.js");
|
|
91
|
+
try {
|
|
92
|
+
if ((await lstat(entry)).isFile())
|
|
93
|
+
return { command: process.execPath, prefix: [entry] };
|
|
94
|
+
} catch (cause) {
|
|
95
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
throw new HostError(
|
|
99
|
+
"Cannot locate npm's npx-cli.js. Install npm alongside Node before synchronizing Skills.",
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
export const officialSkillsPort: SkillsPort = {
|
|
103
|
+
home: homedir(),
|
|
104
|
+
async catalog() {
|
|
105
|
+
requireSkillsNode();
|
|
106
|
+
const api = await import("skills");
|
|
107
|
+
return {
|
|
108
|
+
supported: api.getSupportedAgents(),
|
|
109
|
+
detected: (await api.detectAgents({ cwd: process.cwd() })).agents,
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
async install(source, identity, agent, env) {
|
|
113
|
+
requireSkillsNode();
|
|
114
|
+
const { command, prefix } = await npxCommand();
|
|
115
|
+
return new Promise((accept, reject) => {
|
|
116
|
+
const child = spawn(
|
|
117
|
+
command,
|
|
118
|
+
[
|
|
119
|
+
...prefix,
|
|
120
|
+
"--yes",
|
|
121
|
+
"--package",
|
|
122
|
+
skillsInstaller,
|
|
123
|
+
"skills",
|
|
124
|
+
"add",
|
|
125
|
+
source,
|
|
126
|
+
"--global",
|
|
127
|
+
"--agent",
|
|
128
|
+
agent,
|
|
129
|
+
"--skill",
|
|
130
|
+
identity,
|
|
131
|
+
"--copy",
|
|
132
|
+
"--yes",
|
|
133
|
+
"--json",
|
|
134
|
+
],
|
|
135
|
+
{
|
|
136
|
+
shell: false,
|
|
137
|
+
cwd: fileURLToPath(new URL("..", import.meta.url)),
|
|
138
|
+
windowsHide: true,
|
|
139
|
+
detached: process.platform !== "win32",
|
|
140
|
+
env: {
|
|
141
|
+
...env,
|
|
142
|
+
DISABLE_TELEMETRY: "1",
|
|
143
|
+
DO_NOT_TRACK: "1",
|
|
144
|
+
npm_config_ignore_scripts: "true",
|
|
145
|
+
NPM_CONFIG_IGNORE_SCRIPTS: "true",
|
|
146
|
+
npm_config_audit: "false",
|
|
147
|
+
NPM_CONFIG_AUDIT: "false",
|
|
148
|
+
npm_config_fund: "false",
|
|
149
|
+
NPM_CONFIG_FUND: "false",
|
|
150
|
+
},
|
|
151
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
let stdout = "";
|
|
155
|
+
let stderr = "";
|
|
156
|
+
let excessive = false;
|
|
157
|
+
let interrupted = 0;
|
|
158
|
+
let failure: Error | undefined;
|
|
159
|
+
let stopping: Promise<void> | undefined;
|
|
160
|
+
const stop = () => {
|
|
161
|
+
if (!child.pid || stopping) return;
|
|
162
|
+
const pid = child.pid;
|
|
163
|
+
stopping = (async () => {
|
|
164
|
+
if (process.platform === "win32") {
|
|
165
|
+
try {
|
|
166
|
+
await promisify(execFile)(
|
|
167
|
+
join(
|
|
168
|
+
process.env.SystemRoot ?? "C:\\Windows",
|
|
169
|
+
"System32",
|
|
170
|
+
"taskkill.exe",
|
|
171
|
+
),
|
|
172
|
+
["/PID", String(pid), "/T", "/F"],
|
|
173
|
+
);
|
|
174
|
+
} catch (cause) {
|
|
175
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
176
|
+
throw cause;
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
try {
|
|
180
|
+
process.kill(-pid, "SIGKILL");
|
|
181
|
+
} catch (cause) {
|
|
182
|
+
if (!nodeError(cause, "ESRCH")) throw cause;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
})();
|
|
186
|
+
void stopping.catch((cause: unknown) => {
|
|
187
|
+
failure =
|
|
188
|
+
cause instanceof Error
|
|
189
|
+
? cause
|
|
190
|
+
: new Error("Cannot stop Skills installer process tree.", {
|
|
191
|
+
cause,
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
const fail = (cause: unknown) => {
|
|
196
|
+
failure ??=
|
|
197
|
+
cause instanceof Error
|
|
198
|
+
? cause
|
|
199
|
+
: new Error("Skills installer failed.", { cause });
|
|
200
|
+
stop();
|
|
201
|
+
};
|
|
202
|
+
const interrupt = () => {
|
|
203
|
+
interrupted = 130;
|
|
204
|
+
stop();
|
|
205
|
+
};
|
|
206
|
+
const terminate = () => {
|
|
207
|
+
interrupted = 143;
|
|
208
|
+
stop();
|
|
209
|
+
};
|
|
210
|
+
const cleanup = () => {
|
|
211
|
+
process.removeListener("SIGINT", interrupt);
|
|
212
|
+
process.removeListener("SIGTERM", terminate);
|
|
213
|
+
};
|
|
214
|
+
process.once("SIGINT", interrupt);
|
|
215
|
+
process.once("SIGTERM", terminate);
|
|
216
|
+
const collect = (chunk: Buffer, error: boolean) => {
|
|
217
|
+
if (stdout.length + stderr.length + chunk.length > 1024 * 1024) {
|
|
218
|
+
excessive = true;
|
|
219
|
+
stop();
|
|
220
|
+
} else if (error) stderr += chunk.toString();
|
|
221
|
+
else stdout += chunk.toString();
|
|
222
|
+
};
|
|
223
|
+
child.stdout.on("data", (chunk: Buffer) => {
|
|
224
|
+
collect(chunk, false);
|
|
225
|
+
});
|
|
226
|
+
child.stderr.on("data", (chunk: Buffer) => {
|
|
227
|
+
collect(chunk, true);
|
|
228
|
+
});
|
|
229
|
+
child.stdout.once("error", fail);
|
|
230
|
+
child.stderr.once("error", fail);
|
|
231
|
+
child.once("error", fail);
|
|
232
|
+
child.once("close", (code) => {
|
|
233
|
+
cleanup();
|
|
234
|
+
void (async () => {
|
|
235
|
+
await stopping;
|
|
236
|
+
if (failure) throw failure;
|
|
237
|
+
if (excessive)
|
|
238
|
+
throw new HostError(
|
|
239
|
+
"Skills installer output exceeded its limit; pending attribution was preserved.",
|
|
240
|
+
);
|
|
241
|
+
accept({ code: interrupted || (code ?? 1), stdout, stderr });
|
|
242
|
+
})().catch(reject);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
function pathKey(path: string): string {
|
|
248
|
+
const absolute = resolve(path);
|
|
249
|
+
return process.platform === "win32" ? absolute.toLowerCase() : absolute;
|
|
250
|
+
}
|
|
251
|
+
async function safeAncestors(path: string): Promise<void> {
|
|
252
|
+
for (let current = resolve(path); ; current = dirname(current)) {
|
|
253
|
+
try {
|
|
254
|
+
const stat = await lstat(current);
|
|
255
|
+
if (stat.isSymbolicLink() || !stat.isDirectory())
|
|
256
|
+
throw new HostError(
|
|
257
|
+
`Protected Skill path crosses a symlink, junction or file: ${current}`,
|
|
258
|
+
);
|
|
259
|
+
} catch (cause) {
|
|
260
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
261
|
+
}
|
|
262
|
+
if (current === dirname(current)) return;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function hash(bytes: Uint8Array): string {
|
|
266
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
267
|
+
}
|
|
268
|
+
export async function skillFingerprint(
|
|
269
|
+
path: string,
|
|
270
|
+
): Promise<Fingerprint | null> {
|
|
271
|
+
await safeAncestors(path);
|
|
272
|
+
try {
|
|
273
|
+
const stat = await lstat(path);
|
|
274
|
+
if (stat.isSymbolicLink() || !stat.isDirectory())
|
|
275
|
+
throw new HostError(
|
|
276
|
+
`Protected Skill path is not a regular directory: ${path}`,
|
|
277
|
+
);
|
|
278
|
+
} catch (cause) {
|
|
279
|
+
if (nodeError(cause, "ENOENT")) return null;
|
|
280
|
+
throw cause;
|
|
281
|
+
}
|
|
282
|
+
const files = new Map<string, string>();
|
|
283
|
+
let total = 0;
|
|
284
|
+
const visit = async (
|
|
285
|
+
directory: string,
|
|
286
|
+
prefix: string,
|
|
287
|
+
depth: number,
|
|
288
|
+
): Promise<void> => {
|
|
289
|
+
if (depth > 64) throw new HostError("Installed Skill exceeds depth limit.");
|
|
290
|
+
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
|
291
|
+
const name = prefix + entry.name;
|
|
292
|
+
if (files.size >= 4096)
|
|
293
|
+
throw new HostError("Installed Skill exceeds file limit.");
|
|
294
|
+
if (entry.isSymbolicLink())
|
|
295
|
+
throw new HostError(
|
|
296
|
+
`Protected Skill contains a symlink/junction: ${join(directory, entry.name)}`,
|
|
297
|
+
);
|
|
298
|
+
if (entry.isDirectory()) {
|
|
299
|
+
files.set(`${name}/`, "directory");
|
|
300
|
+
await visit(join(directory, entry.name), `${name}/`, depth + 1);
|
|
301
|
+
} else if (entry.isFile()) {
|
|
302
|
+
if (
|
|
303
|
+
(await lstat(join(directory, entry.name))).size + total >
|
|
304
|
+
64 * 1024 * 1024
|
|
305
|
+
)
|
|
306
|
+
throw new HostError("Installed Skill exceeds byte limit.");
|
|
307
|
+
const data = await readFile(join(directory, entry.name));
|
|
308
|
+
total += data.length;
|
|
309
|
+
if (total > 64 * 1024 * 1024)
|
|
310
|
+
throw new HostError("Installed Skill exceeds byte limit.");
|
|
311
|
+
files.set(name, hash(data));
|
|
312
|
+
} else
|
|
313
|
+
throw new HostError(`Protected Skill contains a special file: ${name}`);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
await visit(path, "", 0);
|
|
317
|
+
return Object.fromEntries(files);
|
|
318
|
+
}
|
|
319
|
+
function equal(left: Fingerprint | null, right: Fingerprint | null): boolean {
|
|
320
|
+
if (!left || !right) return left === right;
|
|
321
|
+
const keys = Object.keys(left);
|
|
322
|
+
return (
|
|
323
|
+
keys.length === Object.keys(right).length &&
|
|
324
|
+
keys.every((key) => left[key] === right[key])
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
// Version-specific prediction is only for interruption/conflict checks. The
|
|
328
|
+
// official installer alone writes agent destinations.
|
|
329
|
+
async function expectedFiles(source: string): Promise<Fingerprint> {
|
|
330
|
+
const files = (await skillFingerprint(source))!;
|
|
331
|
+
for (const path of Object.keys(files)) {
|
|
332
|
+
const parts = path.replace(/\/$/, "").split("/");
|
|
333
|
+
if (
|
|
334
|
+
parts.some(
|
|
335
|
+
(part, index) =>
|
|
336
|
+
part === "metadata.json" ||
|
|
337
|
+
((index < parts.length - 1 || path.endsWith("/")) &&
|
|
338
|
+
[".git", "__pycache__", "__pypackages__"].includes(part)),
|
|
339
|
+
)
|
|
340
|
+
)
|
|
341
|
+
delete files[path];
|
|
342
|
+
}
|
|
343
|
+
return files;
|
|
344
|
+
}
|
|
345
|
+
function recoverable(
|
|
346
|
+
files: Fingerprint | null,
|
|
347
|
+
installation: Installation,
|
|
348
|
+
): boolean {
|
|
349
|
+
const pending = installation.pending;
|
|
350
|
+
return Boolean(
|
|
351
|
+
pending &&
|
|
352
|
+
(!files ||
|
|
353
|
+
Object.entries(files).every(
|
|
354
|
+
([path, value]) =>
|
|
355
|
+
value === pending.expected[path] || value === pending.before?.[path],
|
|
356
|
+
)),
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
function targetPath(agent: AgentInfo, identity: string, home: string): string {
|
|
360
|
+
// Pinned v1.9.1 API exposes declared directories; these two universal agents
|
|
361
|
+
// explicitly opt out of global canonical reads in that exact installer.
|
|
362
|
+
const canonical =
|
|
363
|
+
agent.universal && !["antigravity", "antigravity-cli"].includes(agent.id);
|
|
364
|
+
return join(
|
|
365
|
+
canonical ? join(home, ".agents", "skills") : agent.globalSkillsDir!,
|
|
366
|
+
identity,
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
function locations(
|
|
370
|
+
supported: readonly AgentInfo[],
|
|
371
|
+
home: string,
|
|
372
|
+
identity: string,
|
|
373
|
+
): string[] {
|
|
374
|
+
return [
|
|
375
|
+
...new Map(
|
|
376
|
+
[
|
|
377
|
+
join(home, ".agents", "skills", identity),
|
|
378
|
+
...supported
|
|
379
|
+
.filter((agent) => agent.globalSkillsDir)
|
|
380
|
+
.flatMap((agent) => [
|
|
381
|
+
join(agent.globalSkillsDir!, identity),
|
|
382
|
+
targetPath(agent, identity, home),
|
|
383
|
+
]),
|
|
384
|
+
].map((path) => [pathKey(path), path]),
|
|
385
|
+
).values(),
|
|
386
|
+
];
|
|
387
|
+
}
|
|
388
|
+
function ledgerString(value: unknown, label: string, maximum = 4096): string {
|
|
389
|
+
if (
|
|
390
|
+
typeof value !== "string" ||
|
|
391
|
+
!value ||
|
|
392
|
+
value.length > maximum ||
|
|
393
|
+
[...value].some(
|
|
394
|
+
(character) =>
|
|
395
|
+
character.charCodeAt(0) < 32 || character.charCodeAt(0) === 127,
|
|
396
|
+
)
|
|
397
|
+
)
|
|
398
|
+
throw new HostError(`Invalid ${label} in Skills attribution ledger.`);
|
|
399
|
+
return value;
|
|
400
|
+
}
|
|
401
|
+
function ledgerStrings(value: unknown, label: string): string[] {
|
|
402
|
+
if (!Array.isArray(value) || !value.length || value.length > 4096)
|
|
403
|
+
throw new HostError(`Invalid ${label} in Skills attribution ledger.`);
|
|
404
|
+
const values = value.map((item: unknown) => ledgerString(item, label));
|
|
405
|
+
if (new Set(values).size !== values.length)
|
|
406
|
+
throw new HostError(`Duplicate ${label} in Skills attribution ledger.`);
|
|
407
|
+
return values;
|
|
408
|
+
}
|
|
409
|
+
function ledgerCid(value: unknown): string {
|
|
410
|
+
const cid = ledgerString(value, "payload CID", 128);
|
|
411
|
+
try {
|
|
412
|
+
CID.parse(cid);
|
|
413
|
+
} catch {
|
|
414
|
+
throw new HostError("Invalid payload CID in Skills attribution ledger.");
|
|
415
|
+
}
|
|
416
|
+
return cid;
|
|
417
|
+
}
|
|
418
|
+
function ledgerFingerprint(value: unknown, label: string): Fingerprint {
|
|
419
|
+
const data = object(value, label);
|
|
420
|
+
if (Object.keys(data).length > 4096)
|
|
421
|
+
throw new HostError(`Oversized ${label}.`);
|
|
422
|
+
const entries = Object.entries(data).map(
|
|
423
|
+
([path, digest]): [string, string] => {
|
|
424
|
+
relativePath(path.endsWith("/") ? path.slice(0, -1) : path);
|
|
425
|
+
if (
|
|
426
|
+
typeof digest !== "string" ||
|
|
427
|
+
(path.endsWith("/")
|
|
428
|
+
? digest !== "directory"
|
|
429
|
+
: !/^[a-f0-9]{64}$/u.test(digest))
|
|
430
|
+
)
|
|
431
|
+
throw new HostError(`Invalid ${label} entry "${path}".`);
|
|
432
|
+
return [path, digest];
|
|
433
|
+
},
|
|
434
|
+
);
|
|
435
|
+
const files = Object.fromEntries(entries);
|
|
436
|
+
for (const [path] of entries) {
|
|
437
|
+
const parts = path.replace(/\/$/u, "").split("/");
|
|
438
|
+
for (let count = 1; count < parts.length; count++)
|
|
439
|
+
if (files[`${parts.slice(0, count).join("/")}/`] !== "directory")
|
|
440
|
+
throw new HostError(`Invalid ${label} parent directory.`);
|
|
441
|
+
}
|
|
442
|
+
return files;
|
|
443
|
+
}
|
|
444
|
+
export function parseSkillsLedger(value: unknown): Ledger {
|
|
445
|
+
if (value === undefined) return { v: 1, installations: {} };
|
|
446
|
+
const data = object(value, "Skills attribution ledger");
|
|
447
|
+
if (data.v !== 1)
|
|
448
|
+
throw new HostError(
|
|
449
|
+
"Unsupported Skills attribution ledger; preserve it before recovery.",
|
|
450
|
+
);
|
|
451
|
+
const installations: Record<string, Installation> = {};
|
|
452
|
+
for (const [key, value] of Object.entries(
|
|
453
|
+
object(data.installations, "Skills installations"),
|
|
454
|
+
)) {
|
|
455
|
+
const item = object(value, "Skill installation");
|
|
456
|
+
const path = ledgerString(item.path, "installation path");
|
|
457
|
+
const identity = ledgerString(item.identity, "installation identity", 64);
|
|
458
|
+
if (
|
|
459
|
+
!/^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(identity) ||
|
|
460
|
+
!isAbsolute(path) ||
|
|
461
|
+
basename(path) !== identity ||
|
|
462
|
+
key !== pathKey(path)
|
|
463
|
+
)
|
|
464
|
+
throw new HostError(
|
|
465
|
+
"Invalid Skills attribution identity/path; refusing to overwrite installs.",
|
|
466
|
+
);
|
|
467
|
+
const installation: Installation = {
|
|
468
|
+
swarm:
|
|
469
|
+
item.swarm === bundledSkillOrigin
|
|
470
|
+
? bundledSkillOrigin
|
|
471
|
+
: swarmUrl(ledgerString(item.swarm, "Swarm URL")),
|
|
472
|
+
path,
|
|
473
|
+
identity,
|
|
474
|
+
name: relativePath(ledgerString(item.name, "shared Name", 256)),
|
|
475
|
+
cid: ledgerCid(item.cid),
|
|
476
|
+
agents: ledgerStrings(item.agents, "agent IDs"),
|
|
477
|
+
files: ledgerFingerprint(item.files, "Installed Skill fingerprint"),
|
|
478
|
+
};
|
|
479
|
+
if (
|
|
480
|
+
installation.agents.some(
|
|
481
|
+
(agent) => !/^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(agent),
|
|
482
|
+
)
|
|
483
|
+
)
|
|
484
|
+
throw new HostError("Invalid agent ID in Skills attribution ledger.");
|
|
485
|
+
if (item.pending !== undefined) {
|
|
486
|
+
const pending = object(item.pending, "Pending Skill installation");
|
|
487
|
+
installation.pending = {
|
|
488
|
+
before:
|
|
489
|
+
pending.before === null
|
|
490
|
+
? null
|
|
491
|
+
: ledgerFingerprint(pending.before, "Previous Skill fingerprint"),
|
|
492
|
+
expected: ledgerFingerprint(
|
|
493
|
+
pending.expected,
|
|
494
|
+
"Expected Skill fingerprint",
|
|
495
|
+
),
|
|
496
|
+
};
|
|
497
|
+
if (
|
|
498
|
+
!/^[a-f0-9]{64}$/u.test(installation.pending.expected["SKILL.md"] ?? "")
|
|
499
|
+
)
|
|
500
|
+
throw new HostError(
|
|
501
|
+
"Expected Skill fingerprint requires a regular root SKILL.md.",
|
|
502
|
+
);
|
|
503
|
+
} else if (!/^[a-f0-9]{64}$/u.test(installation.files["SKILL.md"] ?? ""))
|
|
504
|
+
throw new HostError(
|
|
505
|
+
"Installed Skill fingerprint requires a regular root SKILL.md.",
|
|
506
|
+
);
|
|
507
|
+
if (item.retentions !== undefined) {
|
|
508
|
+
if (!Array.isArray(item.retentions))
|
|
509
|
+
throw new HostError("Invalid Skill Store retentions.");
|
|
510
|
+
const stores = new Set<string>();
|
|
511
|
+
installation.retentions = item.retentions.map((value: unknown) => {
|
|
512
|
+
const receipt = object(value, "Skill Store retention");
|
|
513
|
+
const store = ledgerString(receipt.store, "Store identity", 64);
|
|
514
|
+
if (!/^[a-f0-9]{64}$/u.test(store) || stores.has(store))
|
|
515
|
+
throw new HostError("Invalid or duplicate Skill Store identity.");
|
|
516
|
+
stores.add(store);
|
|
517
|
+
return {
|
|
518
|
+
store,
|
|
519
|
+
roots: ledgerStrings(receipt.roots, "retained roots").map(ledgerCid),
|
|
520
|
+
};
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
installations[key] = installation;
|
|
524
|
+
}
|
|
525
|
+
return { v: 1, installations };
|
|
526
|
+
}
|
|
527
|
+
export interface SkillsOptions {
|
|
528
|
+
sync: boolean;
|
|
529
|
+
names: readonly string[];
|
|
530
|
+
agents: readonly string[];
|
|
531
|
+
overrideExisting: boolean;
|
|
532
|
+
dryRun: boolean;
|
|
533
|
+
yes: boolean;
|
|
534
|
+
tty: boolean;
|
|
535
|
+
confirm: (text: string) => Promise<boolean>;
|
|
536
|
+
preview: (text: string) => void;
|
|
537
|
+
}
|
|
538
|
+
export async function syncDaemonSkills(
|
|
539
|
+
context: SwarmContext,
|
|
540
|
+
swarm: string,
|
|
541
|
+
port: SkillsPort = officialSkillsPort,
|
|
542
|
+
refresh = true,
|
|
543
|
+
): Promise<number> {
|
|
544
|
+
if (refresh) await swarmHeader(context, swarm, true, "local-first");
|
|
545
|
+
context.forceRefresh = false;
|
|
546
|
+
const result = await manageSkills(
|
|
547
|
+
context,
|
|
548
|
+
swarm,
|
|
549
|
+
{
|
|
550
|
+
sync: true,
|
|
551
|
+
names: [],
|
|
552
|
+
agents: [],
|
|
553
|
+
overrideExisting: false,
|
|
554
|
+
dryRun: false,
|
|
555
|
+
yes: true,
|
|
556
|
+
tty: false,
|
|
557
|
+
confirm: () => Promise.resolve(false),
|
|
558
|
+
preview: (text) => context.diagnostic?.(`[${swarm}] ${text}`),
|
|
559
|
+
},
|
|
560
|
+
port,
|
|
561
|
+
);
|
|
562
|
+
for (const entry of result.results)
|
|
563
|
+
context.diagnostic?.(
|
|
564
|
+
`[${swarm}] Skill ${entry.name} (${entry.agent}): ${entry.status}${entry.error ? `: ${entry.error}` : ""}`,
|
|
565
|
+
);
|
|
566
|
+
if (!result.ok)
|
|
567
|
+
throw new HostError(
|
|
568
|
+
"Skills synchronization failed; inspect conflicts and installer diagnostics above. Existing protected installations were preserved.",
|
|
569
|
+
);
|
|
570
|
+
return result.items.length;
|
|
571
|
+
}
|
|
572
|
+
export async function syncDaemonSwarm(
|
|
573
|
+
context: SwarmContext,
|
|
574
|
+
swarm: string,
|
|
575
|
+
port: SkillsPort = officialSkillsPort,
|
|
576
|
+
report?: (summary: DaemonSyncSummary) => void,
|
|
577
|
+
): Promise<DaemonSyncSummary> {
|
|
578
|
+
const header = await swarmHeader(context, swarm, true, "local-first");
|
|
579
|
+
context.forceRefresh = false;
|
|
580
|
+
const headerInfo = header as Partial<{
|
|
581
|
+
head?: { root?: string };
|
|
582
|
+
stateCid?: string;
|
|
583
|
+
}>;
|
|
584
|
+
const summary: DaemonSyncSummary = {
|
|
585
|
+
headCid: headerInfo.head?.root ?? null,
|
|
586
|
+
stateCid: headerInfo.stateCid ?? null,
|
|
587
|
+
skills: { status: "skipped", count: 0, error: null },
|
|
588
|
+
templates: { status: "skipped", count: 0, error: null },
|
|
589
|
+
registry: { status: "skipped", count: 0, error: null },
|
|
590
|
+
};
|
|
591
|
+
const errors: string[] = [];
|
|
592
|
+
for (const [label, task] of [
|
|
593
|
+
["pivot-index", () => syncPivotReplicationAndIndex(context, swarm, header)],
|
|
594
|
+
["skills", () => syncDaemonSkills(context, swarm, port, false)],
|
|
595
|
+
["templates", () => syncSwarmTemplates(context, swarm, header)],
|
|
596
|
+
["registry", () => syncSwarmRegistry(context, swarm, header)],
|
|
597
|
+
] as const) {
|
|
598
|
+
try {
|
|
599
|
+
const count = await task();
|
|
600
|
+
if (label !== "pivot-index")
|
|
601
|
+
summary[label] = {
|
|
602
|
+
status: "ok",
|
|
603
|
+
count: typeof count === "number" ? count : 0,
|
|
604
|
+
error: null,
|
|
605
|
+
};
|
|
606
|
+
} catch (cause) {
|
|
607
|
+
const detail = message(cause);
|
|
608
|
+
context.diagnostic?.(`[${swarm}] ${label} sync failed: ${detail}`);
|
|
609
|
+
if (label !== "pivot-index")
|
|
610
|
+
summary[label] = { status: "error", count: 0, error: detail };
|
|
611
|
+
errors.push(`${label}: ${detail}`);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
report?.(summary);
|
|
615
|
+
if (errors.length)
|
|
616
|
+
throw new HostError(`Swarm sync incomplete (${errors.join("; ")}).`);
|
|
617
|
+
return summary;
|
|
618
|
+
}
|
|
619
|
+
function installRetention(path: string): string {
|
|
620
|
+
return `skills-install:${hash(Buffer.from(pathKey(path)))}`;
|
|
621
|
+
}
|
|
622
|
+
function storeIdentity(context: SwarmContext): string {
|
|
623
|
+
const configuration = context.store.configuration;
|
|
624
|
+
return hash(
|
|
625
|
+
Buffer.from(
|
|
626
|
+
JSON.stringify([
|
|
627
|
+
pathKey(configuration.dataDir),
|
|
628
|
+
pathKey(
|
|
629
|
+
configuration.coordinationDir ??
|
|
630
|
+
join(configuration.dataDir, "retention"),
|
|
631
|
+
),
|
|
632
|
+
]),
|
|
633
|
+
),
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
function recordRetention(
|
|
637
|
+
installation: Installation,
|
|
638
|
+
store: string,
|
|
639
|
+
roots: readonly string[],
|
|
640
|
+
): void {
|
|
641
|
+
installation.retentions = [
|
|
642
|
+
...(installation.retentions ?? []).filter(
|
|
643
|
+
(receipt) => receipt.store !== store,
|
|
644
|
+
),
|
|
645
|
+
{ store, roots: [...new Set(roots)] },
|
|
646
|
+
];
|
|
647
|
+
}
|
|
648
|
+
export interface SkillStatus extends SharedSkill {
|
|
649
|
+
status: string;
|
|
650
|
+
agents: {
|
|
651
|
+
id: string;
|
|
652
|
+
path: string;
|
|
653
|
+
status: string;
|
|
654
|
+
previousCid?: string;
|
|
655
|
+
changes?: { added: string[]; updated: string[]; removed: string[] };
|
|
656
|
+
}[];
|
|
657
|
+
problems: string[];
|
|
658
|
+
}
|
|
659
|
+
export interface SkillsResult {
|
|
660
|
+
swarm: string;
|
|
661
|
+
installer: string;
|
|
662
|
+
agents: string[];
|
|
663
|
+
dryRun: boolean;
|
|
664
|
+
items: SkillStatus[];
|
|
665
|
+
orphaned: (Installation & { status: "source removed" })[];
|
|
666
|
+
results: {
|
|
667
|
+
name: string;
|
|
668
|
+
agent: string;
|
|
669
|
+
status: string;
|
|
670
|
+
path: string;
|
|
671
|
+
error?: string;
|
|
672
|
+
}[];
|
|
673
|
+
ok: boolean;
|
|
674
|
+
}
|
|
675
|
+
export async function inspectBundledSkill(
|
|
676
|
+
source: Awaited<ReturnType<typeof bundledSkillSource>>,
|
|
677
|
+
port: SkillsPort,
|
|
678
|
+
) {
|
|
679
|
+
const catalog = await port.catalog();
|
|
680
|
+
const supported = catalog.supported.filter(
|
|
681
|
+
(agent) => agent.globalSkillsDir !== null,
|
|
682
|
+
);
|
|
683
|
+
const agents = catalog.detected.filter((agent) =>
|
|
684
|
+
supported.some((entry) => entry.id === agent.id),
|
|
685
|
+
);
|
|
686
|
+
if (!agents.length)
|
|
687
|
+
return {
|
|
688
|
+
status: "not-checked" as const,
|
|
689
|
+
repairable: false,
|
|
690
|
+
message:
|
|
691
|
+
"No globally supported Agents detected for the bundled IPAB Skill.",
|
|
692
|
+
guidance: "Install an Agent, then run ipab doctor --fix.",
|
|
693
|
+
};
|
|
694
|
+
const directory = join(port.home, ".ipab", "skills");
|
|
695
|
+
await safeAncestors(directory);
|
|
696
|
+
const ledger = parseSkillsLedger(
|
|
697
|
+
await readPrivate<unknown>(
|
|
698
|
+
directory,
|
|
699
|
+
"installations.json",
|
|
700
|
+
[],
|
|
701
|
+
ledgerBytes,
|
|
702
|
+
),
|
|
703
|
+
);
|
|
704
|
+
const current = new Map<string, Fingerprint | null>();
|
|
705
|
+
const problems: string[] = [];
|
|
706
|
+
for (const saved of Object.values(ledger.installations))
|
|
707
|
+
if (saved.identity === "ipab" && saved.swarm !== bundledSkillOrigin)
|
|
708
|
+
problems.push(
|
|
709
|
+
`IPAB Skill belongs to ${saved.swarm}; use skills sync for that source.`,
|
|
710
|
+
);
|
|
711
|
+
for (const path of locations(supported, port.home, "ipab")) {
|
|
712
|
+
const files = await skillFingerprint(path);
|
|
713
|
+
current.set(pathKey(path), files);
|
|
714
|
+
const saved = ledger.installations[pathKey(path)];
|
|
715
|
+
if (
|
|
716
|
+
files &&
|
|
717
|
+
(!saved || (!equal(files, saved.files) && !recoverable(files, saved)))
|
|
718
|
+
)
|
|
719
|
+
problems.push(`Custom or locally edited IPAB Skill: ${path}`);
|
|
720
|
+
}
|
|
721
|
+
const targets = agents.map((agent) => {
|
|
722
|
+
const path = targetPath(agent, "ipab", port.home);
|
|
723
|
+
const saved = ledger.installations[pathKey(path)];
|
|
724
|
+
return {
|
|
725
|
+
path,
|
|
726
|
+
id: agent.id,
|
|
727
|
+
current: Boolean(
|
|
728
|
+
current.get(pathKey(path)) &&
|
|
729
|
+
saved?.cid === source.cid &&
|
|
730
|
+
saved.agents.includes(agent.id) &&
|
|
731
|
+
!saved.pending,
|
|
732
|
+
),
|
|
733
|
+
};
|
|
734
|
+
});
|
|
735
|
+
const description = targets
|
|
736
|
+
.map((target) => `${target.id} (${target.path})`)
|
|
737
|
+
.join(", ");
|
|
738
|
+
return {
|
|
739
|
+
status: problems.length
|
|
740
|
+
? ("invalid" as const)
|
|
741
|
+
: targets.every((target) => target.current)
|
|
742
|
+
? ("ok" as const)
|
|
743
|
+
: ("missing" as const),
|
|
744
|
+
repairable: !problems.length && targets.some((target) => !target.current),
|
|
745
|
+
message: `Bundled IPAB Skill ${source.cid}, installer ${skillsInstaller}: ${description}.${problems.length ? ` ${problems.join(" ")}` : ""}`,
|
|
746
|
+
guidance: problems.length
|
|
747
|
+
? "Preserve the existing installation; resolve its ownership or local edits before retrying. --yes never overrides conflicts."
|
|
748
|
+
: targets.every((target) => target.current)
|
|
749
|
+
? ""
|
|
750
|
+
: "Run ipab doctor --fix to install/update the CLI-bundled Skill globally for these Agents (no Swarm required; hooks/scripts are not executed).",
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
export async function manageSkills(
|
|
754
|
+
context:
|
|
755
|
+
| SwarmContext
|
|
756
|
+
| {
|
|
757
|
+
env: NodeJS.ProcessEnv;
|
|
758
|
+
bundledSkill: Awaited<ReturnType<typeof bundledSkillSource>>;
|
|
759
|
+
},
|
|
760
|
+
swarm: string,
|
|
761
|
+
options: SkillsOptions,
|
|
762
|
+
port: SkillsPort = officialSkillsPort,
|
|
763
|
+
): Promise<SkillsResult> {
|
|
764
|
+
const catalog = await port.catalog();
|
|
765
|
+
const supported = catalog.supported.filter(
|
|
766
|
+
(agent) => agent.globalSkillsDir !== null,
|
|
767
|
+
);
|
|
768
|
+
const ids = [
|
|
769
|
+
...new Set(options.agents.length ? options.agents : ["universal"]),
|
|
770
|
+
];
|
|
771
|
+
const agents = ids.map((id) => {
|
|
772
|
+
const agent = supported.find((agent) => agent.id === id);
|
|
773
|
+
if (!agent)
|
|
774
|
+
throw new HostError(
|
|
775
|
+
`Agent "${id}" is not globally supported by ${skillsInstaller}.`,
|
|
776
|
+
);
|
|
777
|
+
return agent;
|
|
778
|
+
});
|
|
779
|
+
const parent = await privateDirectory(port.home, ".ipab");
|
|
780
|
+
const directory = await privateDirectory(parent, "skills");
|
|
781
|
+
await safeAncestors(directory);
|
|
782
|
+
return withPrivateLock(directory, async () => {
|
|
783
|
+
const ledger = parseSkillsLedger(
|
|
784
|
+
await readPrivate<unknown>(
|
|
785
|
+
directory,
|
|
786
|
+
"installations.json",
|
|
787
|
+
[],
|
|
788
|
+
ledgerBytes,
|
|
789
|
+
),
|
|
790
|
+
);
|
|
791
|
+
const currentStore =
|
|
792
|
+
"bundledSkill" in context ? undefined : storeIdentity(context);
|
|
793
|
+
const retainInstallation = async (installation: Installation) => {
|
|
794
|
+
if ("bundledSkill" in context) return;
|
|
795
|
+
const owner = installRetention(installation.path);
|
|
796
|
+
await context.store.retain(owner, [installation.cid]);
|
|
797
|
+
await context.store.confirmRetained(owner, [installation.cid]);
|
|
798
|
+
};
|
|
799
|
+
const commitInstallation = async (
|
|
800
|
+
installation: Installation,
|
|
801
|
+
files: Fingerprint,
|
|
802
|
+
) => {
|
|
803
|
+
await retainInstallation(installation);
|
|
804
|
+
installation.files = files;
|
|
805
|
+
delete installation.pending;
|
|
806
|
+
if (currentStore)
|
|
807
|
+
recordRetention(installation, currentStore, [installation.cid]);
|
|
808
|
+
await saveLedger(directory, ledger);
|
|
809
|
+
if ("bundledSkill" in context) return;
|
|
810
|
+
const owner = installRetention(installation.path);
|
|
811
|
+
await context.store.recoverOwner(owner, [installation.cid]);
|
|
812
|
+
await context.store.reconcile(owner, [installation.cid]);
|
|
813
|
+
};
|
|
814
|
+
const owner = `skills-discovery:${hash(Buffer.from(swarm))}`;
|
|
815
|
+
let staging: string | undefined;
|
|
816
|
+
try {
|
|
817
|
+
if (!("bundledSkill" in context))
|
|
818
|
+
await context.store.recoverOwner(owner, []);
|
|
819
|
+
const discovered =
|
|
820
|
+
"bundledSkill" in context
|
|
821
|
+
? [{ name: "ipab", identity: "ipab", cid: context.bundledSkill.cid }]
|
|
822
|
+
: await swarmSkills(context, swarm, options.names, owner);
|
|
823
|
+
const items: SkillStatus[] = [];
|
|
824
|
+
const snapshots = new Map<string, Fingerprint | null>();
|
|
825
|
+
for (const skill of discovered) {
|
|
826
|
+
const problems: string[] = [];
|
|
827
|
+
const targets = new Set(
|
|
828
|
+
agents.map((agent) =>
|
|
829
|
+
pathKey(targetPath(agent, skill.identity, port.home)),
|
|
830
|
+
),
|
|
831
|
+
);
|
|
832
|
+
const canOverride = (path: string) =>
|
|
833
|
+
options.sync &&
|
|
834
|
+
options.overrideExisting &&
|
|
835
|
+
targets.has(pathKey(path));
|
|
836
|
+
for (const saved of Object.values(ledger.installations).filter(
|
|
837
|
+
(entry) => entry.identity === skill.identity,
|
|
838
|
+
))
|
|
839
|
+
if (saved.swarm !== swarm || saved.name !== skill.name)
|
|
840
|
+
problems.push(
|
|
841
|
+
`Cross-source collision with ${saved.swarm} / ${saved.name} at ${saved.path}`,
|
|
842
|
+
);
|
|
843
|
+
const paths = [
|
|
844
|
+
...new Set([
|
|
845
|
+
...locations(supported, port.home, skill.identity),
|
|
846
|
+
...Object.values(ledger.installations)
|
|
847
|
+
.filter((entry) => entry.identity === skill.identity)
|
|
848
|
+
.map((entry) => entry.path),
|
|
849
|
+
]),
|
|
850
|
+
];
|
|
851
|
+
const current = new Map<string, Fingerprint | null>();
|
|
852
|
+
for (const path of paths) {
|
|
853
|
+
try {
|
|
854
|
+
const files = await skillFingerprint(path);
|
|
855
|
+
current.set(pathKey(path), files);
|
|
856
|
+
snapshots.set(pathKey(path), files);
|
|
857
|
+
const saved = ledger.installations[pathKey(path)];
|
|
858
|
+
if (files && !saved && !canOverride(path))
|
|
859
|
+
problems.push(`Non-IPAB-managed installation: ${path}`);
|
|
860
|
+
if (
|
|
861
|
+
saved &&
|
|
862
|
+
files &&
|
|
863
|
+
!canOverride(path) &&
|
|
864
|
+
!equal(files, saved.files) &&
|
|
865
|
+
!recoverable(files, saved)
|
|
866
|
+
)
|
|
867
|
+
problems.push(
|
|
868
|
+
`Local edits or removed managed installation: ${path}`,
|
|
869
|
+
);
|
|
870
|
+
} catch (cause) {
|
|
871
|
+
problems.push(message(cause));
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
const statuses = agents.map((agent) => {
|
|
875
|
+
const path = targetPath(agent, skill.identity, port.home);
|
|
876
|
+
const saved = ledger.installations[pathKey(path)];
|
|
877
|
+
const files = current.get(pathKey(path)) ?? null;
|
|
878
|
+
return {
|
|
879
|
+
id: agent.id,
|
|
880
|
+
path,
|
|
881
|
+
...(saved ? { previousCid: saved.cid } : {}),
|
|
882
|
+
status: !files
|
|
883
|
+
? "not installed"
|
|
884
|
+
: canOverride(path) &&
|
|
885
|
+
(!saved ||
|
|
886
|
+
(!equal(files, saved.files) && !recoverable(files, saved)))
|
|
887
|
+
? "overwrite"
|
|
888
|
+
: saved?.pending
|
|
889
|
+
? "pending"
|
|
890
|
+
: saved?.cid === skill.cid && saved.agents.includes(agent.id)
|
|
891
|
+
? "current"
|
|
892
|
+
: "update",
|
|
893
|
+
};
|
|
894
|
+
});
|
|
895
|
+
const own = Object.values(ledger.installations).filter(
|
|
896
|
+
(entry) =>
|
|
897
|
+
entry.swarm === swarm &&
|
|
898
|
+
entry.name === skill.name &&
|
|
899
|
+
current.get(pathKey(entry.path)) !== null,
|
|
900
|
+
);
|
|
901
|
+
items.push({
|
|
902
|
+
...skill,
|
|
903
|
+
agents: statuses,
|
|
904
|
+
problems,
|
|
905
|
+
status: problems.length
|
|
906
|
+
? "conflict / local edits"
|
|
907
|
+
: statuses.length
|
|
908
|
+
? statuses.every((agent) => agent.status === "current")
|
|
909
|
+
? "current"
|
|
910
|
+
: statuses.some(
|
|
911
|
+
(agent) =>
|
|
912
|
+
agent.status === "update" ||
|
|
913
|
+
agent.status === "pending" ||
|
|
914
|
+
agent.status === "overwrite",
|
|
915
|
+
)
|
|
916
|
+
? "update"
|
|
917
|
+
: "not installed"
|
|
918
|
+
: own.length
|
|
919
|
+
? own.every(
|
|
920
|
+
(entry) => entry.cid === skill.cid && !entry.pending,
|
|
921
|
+
)
|
|
922
|
+
? "current"
|
|
923
|
+
: "update"
|
|
924
|
+
: "not installed",
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
const result: SkillsResult = {
|
|
928
|
+
swarm,
|
|
929
|
+
installer: skillsInstaller,
|
|
930
|
+
agents: ids,
|
|
931
|
+
dryRun: options.dryRun,
|
|
932
|
+
items,
|
|
933
|
+
orphaned: Object.values(ledger.installations)
|
|
934
|
+
.filter(
|
|
935
|
+
(entry) =>
|
|
936
|
+
entry.swarm === swarm &&
|
|
937
|
+
!discovered.some(
|
|
938
|
+
(skill) =>
|
|
939
|
+
skill.name === entry.name &&
|
|
940
|
+
skill.identity === entry.identity,
|
|
941
|
+
),
|
|
942
|
+
)
|
|
943
|
+
.map((installation) => ({
|
|
944
|
+
...installation,
|
|
945
|
+
status: "source removed",
|
|
946
|
+
})),
|
|
947
|
+
results: [],
|
|
948
|
+
ok: true,
|
|
949
|
+
};
|
|
950
|
+
if (!options.sync) return result;
|
|
951
|
+
const selected = items.filter(
|
|
952
|
+
(skill) =>
|
|
953
|
+
!options.names.length ||
|
|
954
|
+
options.names.includes(skill.name) ||
|
|
955
|
+
options.names.includes(skill.identity),
|
|
956
|
+
);
|
|
957
|
+
staging = join(directory, `source-${randomUUID()}`);
|
|
958
|
+
await mkdir(staging, { mode: 0o700 });
|
|
959
|
+
const sources = new Map<string, string>();
|
|
960
|
+
for (const skill of selected) {
|
|
961
|
+
const source =
|
|
962
|
+
"bundledSkill" in context
|
|
963
|
+
? await scaffoldFiles(
|
|
964
|
+
join(staging, skill.identity),
|
|
965
|
+
context.bundledSkill.files,
|
|
966
|
+
)
|
|
967
|
+
: await getBundle(
|
|
968
|
+
context.store,
|
|
969
|
+
skill.cid,
|
|
970
|
+
join(staging, skill.identity),
|
|
971
|
+
);
|
|
972
|
+
sources.set(skill.identity, source);
|
|
973
|
+
for (const agent of skill.agents) {
|
|
974
|
+
if (agent.status === "current" || skill.problems.length) continue;
|
|
975
|
+
const before = (await skillFingerprint(agent.path)) ?? {};
|
|
976
|
+
const after = await expectedFiles(source);
|
|
977
|
+
agent.changes = {
|
|
978
|
+
added: Object.keys(after).filter(
|
|
979
|
+
(path) => !Object.hasOwn(before, path),
|
|
980
|
+
),
|
|
981
|
+
updated: Object.keys(after).filter(
|
|
982
|
+
(path) =>
|
|
983
|
+
Object.hasOwn(before, path) && before[path] !== after[path],
|
|
984
|
+
),
|
|
985
|
+
removed: Object.keys(before).filter(
|
|
986
|
+
(path) => !Object.hasOwn(after, path),
|
|
987
|
+
),
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
const changes = selected.flatMap((skill) =>
|
|
992
|
+
skill.agents
|
|
993
|
+
.filter((agent) => agent.status !== "current")
|
|
994
|
+
.map(
|
|
995
|
+
(agent) =>
|
|
996
|
+
`${skill.name} ${agent.previousCid ?? "(none)"} → ${skill.cid}: ${agent.status} → ${agent.id} (${agent.path})${agent.changes ? `\n add: ${agent.changes.added.join(", ") || "(none)"}; change: ${agent.changes.updated.join(", ") || "(none)"}; remove: ${agent.changes.removed.join(", ") || "(none)"}` : ""}`,
|
|
997
|
+
),
|
|
998
|
+
);
|
|
999
|
+
options.preview(
|
|
1000
|
+
[
|
|
1001
|
+
`Source ${swarm === bundledSkillOrigin ? "CLI" : "Swarm"}: ${swarm}`,
|
|
1002
|
+
`Official installer: npx --yes --package ${skillsInstaller} skills add LOCAL --global --copy --yes --json`,
|
|
1003
|
+
`Agents: ${ids.join(", ")}`,
|
|
1004
|
+
...changes,
|
|
1005
|
+
...selected.flatMap((skill) =>
|
|
1006
|
+
skill.problems.map(
|
|
1007
|
+
(problem) => `CONFLICT ${skill.name}: ${problem}`,
|
|
1008
|
+
),
|
|
1009
|
+
),
|
|
1010
|
+
"Downloaded Skill hooks/scripts are not executed. The authorized third-party installer runs with your permissions; IPAB is not a sandbox.",
|
|
1011
|
+
].join("\n"),
|
|
1012
|
+
);
|
|
1013
|
+
if (selected.some((skill) => skill.problems.length)) {
|
|
1014
|
+
result.ok = false;
|
|
1015
|
+
return result;
|
|
1016
|
+
}
|
|
1017
|
+
if (options.dryRun) return result;
|
|
1018
|
+
if (
|
|
1019
|
+
changes.length &&
|
|
1020
|
+
!options.yes &&
|
|
1021
|
+
(!options.tty ||
|
|
1022
|
+
!(await options.confirm(
|
|
1023
|
+
`Authorize ${skillsInstaller} and the changes above?`,
|
|
1024
|
+
)))
|
|
1025
|
+
)
|
|
1026
|
+
throw new HostError(
|
|
1027
|
+
"Skill sync requires --yes or interactive consent. Use --override-existing to authorize replacing existing Skills; source and link conflicts remain protected.",
|
|
1028
|
+
);
|
|
1029
|
+
for (const skill of selected) {
|
|
1030
|
+
const source = sources.get(skill.identity)!;
|
|
1031
|
+
const targets = new Set(
|
|
1032
|
+
skill.agents.map((agent) => pathKey(agent.path)),
|
|
1033
|
+
);
|
|
1034
|
+
const canOverride = (path: string) =>
|
|
1035
|
+
options.overrideExisting && targets.has(pathKey(path));
|
|
1036
|
+
for (const agent of skill.agents) {
|
|
1037
|
+
const key = pathKey(agent.path);
|
|
1038
|
+
try {
|
|
1039
|
+
await safeAncestors(agent.path);
|
|
1040
|
+
let before = await skillFingerprint(agent.path);
|
|
1041
|
+
const old = ledger.installations[key];
|
|
1042
|
+
if (
|
|
1043
|
+
canOverride(agent.path) &&
|
|
1044
|
+
!equal(before, snapshots.get(key) ?? null)
|
|
1045
|
+
)
|
|
1046
|
+
throw new HostError(
|
|
1047
|
+
`Installation changed since preview: ${agent.path}`,
|
|
1048
|
+
);
|
|
1049
|
+
if (agent.status === "current") {
|
|
1050
|
+
if (!old || !before || !equal(before, old.files))
|
|
1051
|
+
throw new HostError(
|
|
1052
|
+
`Installation changed since preview: ${agent.path}`,
|
|
1053
|
+
);
|
|
1054
|
+
await commitInstallation(old, before);
|
|
1055
|
+
continue;
|
|
1056
|
+
}
|
|
1057
|
+
const expected = await expectedFiles(source);
|
|
1058
|
+
if (
|
|
1059
|
+
old?.pending &&
|
|
1060
|
+
equal(before, old.pending.expected) &&
|
|
1061
|
+
old.cid === skill.cid &&
|
|
1062
|
+
old.swarm === swarm &&
|
|
1063
|
+
old.name === skill.name
|
|
1064
|
+
) {
|
|
1065
|
+
old.agents = [...new Set([...old.agents, agent.id])];
|
|
1066
|
+
await commitInstallation(old, before!);
|
|
1067
|
+
agent.status = "current";
|
|
1068
|
+
skill.status = skill.agents.every(
|
|
1069
|
+
(entry) => entry.status === "current",
|
|
1070
|
+
)
|
|
1071
|
+
? "current"
|
|
1072
|
+
: "update";
|
|
1073
|
+
result.results.push({
|
|
1074
|
+
name: skill.name,
|
|
1075
|
+
agent: agent.id,
|
|
1076
|
+
status: "recovered",
|
|
1077
|
+
path: agent.path,
|
|
1078
|
+
});
|
|
1079
|
+
continue;
|
|
1080
|
+
}
|
|
1081
|
+
if (
|
|
1082
|
+
before &&
|
|
1083
|
+
((!old && !canOverride(agent.path)) ||
|
|
1084
|
+
(old &&
|
|
1085
|
+
(old.swarm !== swarm ||
|
|
1086
|
+
old.name !== skill.name ||
|
|
1087
|
+
(!canOverride(agent.path) &&
|
|
1088
|
+
!equal(before, old.files) &&
|
|
1089
|
+
!recoverable(before, old)))))
|
|
1090
|
+
)
|
|
1091
|
+
throw new HostError(
|
|
1092
|
+
`Installation changed since preview: ${agent.path}`,
|
|
1093
|
+
);
|
|
1094
|
+
// Recheck every alias/collateral location immediately before invoking
|
|
1095
|
+
// the overwriting installer, not merely before the consent prompt.
|
|
1096
|
+
for (const path of locations(
|
|
1097
|
+
supported,
|
|
1098
|
+
port.home,
|
|
1099
|
+
skill.identity,
|
|
1100
|
+
)) {
|
|
1101
|
+
const files = await skillFingerprint(path);
|
|
1102
|
+
const saved = ledger.installations[pathKey(path)];
|
|
1103
|
+
if (
|
|
1104
|
+
canOverride(path) &&
|
|
1105
|
+
!equal(files, snapshots.get(pathKey(path)) ?? null)
|
|
1106
|
+
)
|
|
1107
|
+
throw new HostError(
|
|
1108
|
+
`Protected installation changed since preview: ${path}`,
|
|
1109
|
+
);
|
|
1110
|
+
if (
|
|
1111
|
+
files &&
|
|
1112
|
+
((!saved && !canOverride(path)) ||
|
|
1113
|
+
(saved &&
|
|
1114
|
+
(saved.swarm !== swarm ||
|
|
1115
|
+
saved.name !== skill.name ||
|
|
1116
|
+
(!canOverride(path) &&
|
|
1117
|
+
!equal(files, saved.files) &&
|
|
1118
|
+
!recoverable(files, saved)))))
|
|
1119
|
+
)
|
|
1120
|
+
throw new HostError(
|
|
1121
|
+
`Protected installation changed since preview: ${path}`,
|
|
1122
|
+
);
|
|
1123
|
+
}
|
|
1124
|
+
before = await skillFingerprint(agent.path);
|
|
1125
|
+
if (
|
|
1126
|
+
canOverride(agent.path) &&
|
|
1127
|
+
!equal(before, snapshots.get(key) ?? null)
|
|
1128
|
+
)
|
|
1129
|
+
throw new HostError(
|
|
1130
|
+
`Installation changed since preview: ${agent.path}`,
|
|
1131
|
+
);
|
|
1132
|
+
const pending: Installation = {
|
|
1133
|
+
name: skill.name,
|
|
1134
|
+
cid: skill.cid,
|
|
1135
|
+
identity: skill.identity,
|
|
1136
|
+
swarm,
|
|
1137
|
+
path: agent.path,
|
|
1138
|
+
agents: [...new Set([...(old?.agents ?? []), agent.id])],
|
|
1139
|
+
files: old?.files ?? {},
|
|
1140
|
+
pending: { before, expected },
|
|
1141
|
+
retentions: (old?.retentions ?? []).map((receipt) => ({
|
|
1142
|
+
store: receipt.store,
|
|
1143
|
+
roots: [...receipt.roots],
|
|
1144
|
+
})),
|
|
1145
|
+
};
|
|
1146
|
+
const previousRoots =
|
|
1147
|
+
pending.retentions?.find(
|
|
1148
|
+
(receipt) => receipt.store === currentStore,
|
|
1149
|
+
)?.roots ?? [];
|
|
1150
|
+
if (currentStore)
|
|
1151
|
+
recordRetention(pending, currentStore, [
|
|
1152
|
+
...previousRoots,
|
|
1153
|
+
skill.cid,
|
|
1154
|
+
]);
|
|
1155
|
+
ledger.installations[key] = pending;
|
|
1156
|
+
await retainInstallation(pending);
|
|
1157
|
+
await saveLedger(directory, ledger);
|
|
1158
|
+
const installed = await port.install(
|
|
1159
|
+
source,
|
|
1160
|
+
skill.identity,
|
|
1161
|
+
agent.id,
|
|
1162
|
+
context.env,
|
|
1163
|
+
);
|
|
1164
|
+
const actual = await skillFingerprint(agent.path);
|
|
1165
|
+
if ([130, 143].includes(installed.code))
|
|
1166
|
+
throw new HostError(
|
|
1167
|
+
"Skill sync interrupted; pending attribution and source content were preserved. Retry the same sync.",
|
|
1168
|
+
installed.code,
|
|
1169
|
+
"ABORTED",
|
|
1170
|
+
);
|
|
1171
|
+
const receipts: unknown = JSON.parse(installed.stdout);
|
|
1172
|
+
if (!Array.isArray(receipts) || receipts.length !== 1)
|
|
1173
|
+
throw new HostError(
|
|
1174
|
+
"Official installer returned an invalid receipt; pending attribution preserved.",
|
|
1175
|
+
);
|
|
1176
|
+
const receipt = object(receipts[0], "Official installer receipt");
|
|
1177
|
+
if (
|
|
1178
|
+
installed.code !== 0 ||
|
|
1179
|
+
receipt.status !== "installed" ||
|
|
1180
|
+
receipt.name !== skill.identity ||
|
|
1181
|
+
typeof receipt.path !== "string" ||
|
|
1182
|
+
pathKey(receipt.path) !== key ||
|
|
1183
|
+
!Array.isArray(receipt.agents) ||
|
|
1184
|
+
!receipt.agents.includes(
|
|
1185
|
+
supported.find((entry) => entry.id === agent.id)!.name,
|
|
1186
|
+
) ||
|
|
1187
|
+
!actual ||
|
|
1188
|
+
!equal(actual, expected)
|
|
1189
|
+
)
|
|
1190
|
+
throw new HostError(
|
|
1191
|
+
`Official installer did not confirm the expected installation (exit ${installed.code}). Pending attribution preserved. ${installed.stderr.slice(-1000)}`,
|
|
1192
|
+
);
|
|
1193
|
+
await commitInstallation(pending, actual);
|
|
1194
|
+
snapshots.set(key, actual);
|
|
1195
|
+
agent.status = "current";
|
|
1196
|
+
skill.status = skill.agents.every(
|
|
1197
|
+
(entry) => entry.status === "current",
|
|
1198
|
+
)
|
|
1199
|
+
? "current"
|
|
1200
|
+
: "update";
|
|
1201
|
+
result.results.push({
|
|
1202
|
+
name: skill.name,
|
|
1203
|
+
agent: agent.id,
|
|
1204
|
+
status: "installed",
|
|
1205
|
+
path: agent.path,
|
|
1206
|
+
});
|
|
1207
|
+
} catch (cause) {
|
|
1208
|
+
result.ok = false;
|
|
1209
|
+
agent.status = "failed";
|
|
1210
|
+
skill.status = skill.agents.some(
|
|
1211
|
+
(entry) => entry.status === "current",
|
|
1212
|
+
)
|
|
1213
|
+
? "partial / failed"
|
|
1214
|
+
: "failed";
|
|
1215
|
+
result.results.push({
|
|
1216
|
+
name: skill.name,
|
|
1217
|
+
agent: agent.id,
|
|
1218
|
+
status: "failed",
|
|
1219
|
+
path: agent.path,
|
|
1220
|
+
error: message(cause),
|
|
1221
|
+
});
|
|
1222
|
+
if (cause instanceof HostError && cause.code === "ABORTED")
|
|
1223
|
+
return result;
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
return result;
|
|
1228
|
+
} finally {
|
|
1229
|
+
if (staging) await rm(staging, { recursive: true, force: true });
|
|
1230
|
+
if (!("bundledSkill" in context)) {
|
|
1231
|
+
await context.store.recoverOwner(owner, []);
|
|
1232
|
+
await context.store.reconcile(owner, []);
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
});
|
|
1236
|
+
}
|