@staix/agent-hosts 0.2.0 → 0.4.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/src/entry.ts CHANGED
@@ -6,7 +6,7 @@ import { openCodeRouteReason, type HostPaths, type ResolveHostPathsOptions } fro
6
6
  import { createJsonMcpCodec, type ConfigImage } from "./json.ts";
7
7
  import { SETUP_CONFIG_BYTES, setTomlPath } from "./toml.ts";
8
8
  import { publishImage, readImage, sameImage, withConfigLock } from "./fs-safe.ts";
9
- import { digest, findPackageReceipt, imageDigest, readRecovery, writeReceipt, writeRecovery, type Receipt, type ReceiptTarget } from "./receipt.ts";
9
+ import { digest, findPackageReceipt, imageDigest, isReceiptOwner, readReceipt, readRecovery, receiptPath, writeReceipt, writeRecovery, type Receipt, type ReceiptTarget } from "./receipt.ts";
10
10
  import { readLegacyReceipt, type LegacyReceipt } from "./receipt-legacy.ts";
11
11
  import { asEntryRefusal, EntryRefusal, type RefusalCode } from "./refusal.ts";
12
12
 
@@ -17,6 +17,7 @@ export interface EntryTarget {
17
17
  }
18
18
 
19
19
  export interface PlanEntryOptions extends EntryTarget {
20
+ owner?: string;
20
21
  entry: Readonly<Record<string, unknown>> | null;
21
22
  }
22
23
 
@@ -31,6 +32,8 @@ export type UndoNoopCode = typeof UNDO_NOOP_CODES[number];
31
32
 
32
33
  type Change = "add" | "replace" | "remove" | "unchanged";
33
34
  type PlannedEntry = ReceiptTarget & {
35
+ owner?: string;
36
+ suiteReceipts?: string;
34
37
  change: Change;
35
38
  entry: Readonly<Record<string, unknown>> | null;
36
39
  current: unknown;
@@ -150,7 +153,12 @@ function makePlan(
150
153
  before: ConfigImage,
151
154
  opencodeRoutes?: readonly string[],
152
155
  legacyPaths: readonly string[] = [],
156
+ owner?: string,
157
+ suiteReceipts?: string,
153
158
  ): PlannedEntry {
159
+ if (owner !== undefined && !isReceiptOwner(owner)) {
160
+ throw new EntryRefusal("invalid-entry", "provide a nonempty receipt owner of up to 256 bytes without control characters");
161
+ }
154
162
  assertHostEdit(selection, requested, opencodeRoutes);
155
163
  return editConfig(selection.file, () => {
156
164
  const encoded = JSON.stringify(requested);
@@ -165,10 +173,28 @@ function makePlan(
165
173
  if (!isDeepStrictEqual(codec.read(after), desired)) throw new EntryRefusal("invalid-entry", "codec did not produce the requested entry");
166
174
  const intent = digest([selection.host, selection.file, selection.name, entry]);
167
175
  return { ...selection, entry, change: sameImage(before, after) ? "unchanged" : entry === null ? "remove" : current === undefined ? "add" : "replace",
168
- before, after, current, intent, legacyPaths, ...(opencodeRoutes ? { opencodeRoutes } : {}) };
176
+ before, after, current, intent, legacyPaths, ...(owner === undefined ? {} : { owner }),
177
+ ...(suiteReceipts === undefined ? {} : { suiteReceipts }), ...(opencodeRoutes ? { opencodeRoutes } : {}) };
169
178
  });
170
179
  }
171
180
 
181
+ function assertSuiteOwnership(selection: ReceiptTarget, directory: string | undefined, owner: string | undefined): void {
182
+ if (directory === undefined) return;
183
+ let receipt: Receipt | undefined;
184
+ try {
185
+ receipt = readReceipt(receiptPath(directory, selection), selection).receipt;
186
+ } catch (cause) {
187
+ throw new EntryRefusal("invalid-receipt", `${selection.file}: cannot read the suite receipt; ${message(cause)}`, {
188
+ cause, file: selection.file,
189
+ });
190
+ }
191
+ if (receipt?.owner === "asuite" && owner !== "asuite") {
192
+ throw new EntryRefusal("suite-managed", `${selection.file}: this entry is managed by asuite; run asuite sync`, {
193
+ file: selection.file,
194
+ });
195
+ }
196
+ }
197
+
172
198
  export function planEntry(options: PlanEntryOptions): EntryPlan {
173
199
  let file: string | undefined;
174
200
  try {
@@ -177,7 +203,11 @@ export function planEntry(options: PlanEntryOptions): EntryPlan {
177
203
  const routes = selection.host === "opencode"
178
204
  ? options.paths.refusals.flatMap((item) => item.host === "opencode" && item.code === "opencode-config-route" && item.variable ? [item.variable] : [])
179
205
  : undefined;
180
- return makePlan(selection, options.entry, readImage(selection.file, SETUP_CONFIG_BYTES), routes, [...options.paths.legacyPaths[selection.host] ?? []]);
206
+ const legacyPaths = [...options.paths.legacyPaths[selection.host] ?? []];
207
+ const { suiteReceipts, suiteReceiptRefusal } = options.paths;
208
+ if (suiteReceiptRefusal) throw new EntryRefusal(suiteReceiptRefusal.code, suiteReceiptRefusal.reason, { file });
209
+ assertSuiteOwnership(selection, suiteReceipts, options.owner);
210
+ return makePlan(selection, options.entry, readImage(file, SETUP_CONFIG_BYTES), routes, legacyPaths, options.owner, suiteReceipts);
181
211
  } catch (error) {
182
212
  const refusal = asEntryRefusal(error, file ?? options.host, file);
183
213
  return { change: "refused", code: refusal.code, reason: refusal.reason, ...(refusal.file ? { file: refusal.file } : {}) };
@@ -222,6 +252,7 @@ function applyPlan(plan: PlannedEntry, { receiptDir, env }: ApplyEntryOptions):
222
252
  if (!Object.hasOwn(HOSTS, plan.host)) throw new EntryRefusal("invalid-target", `${plan.host}: select a supported host and replan`);
223
253
  const selection = { host: plan.host, file: plan.file, name: plan.name };
224
254
  return withConfigLock(plan.file, () => {
255
+ assertSuiteOwnership(selection, plan.suiteReceipts, plan.owner);
225
256
  const current = readImage(plan.file, SETUP_CONFIG_BYTES);
226
257
  const { path, image, receipt } = findPackageReceipt(receiptDir, selection, plan.legacyPaths);
227
258
  let routes: readonly string[] | undefined;
@@ -233,7 +264,7 @@ function applyPlan(plan: PlannedEntry, { receiptDir, env }: ApplyEntryOptions):
233
264
  const updated = HOSTS.opencode.files[0].routeVariables.filter((variable) => env[variable]?.trim());
234
265
  routes = [...plan.opencodeRoutes, ...updated];
235
266
  }
236
- const next = makePlan(selection, plan.entry, current, routes);
267
+ const next = makePlan(selection, plan.entry, current, routes, [], plan.owner);
237
268
  if (next.intent !== plan.intent) throw new EntryRefusal("invalid-plan", `${plan.file}: plan intent changed; generate a fresh plan`);
238
269
  if (receipt?.pending) {
239
270
  if (receipt.pending.kind !== "apply" || receipt.intent !== plan.intent) {
@@ -250,7 +281,7 @@ function applyPlan(plan: PlannedEntry, { receiptDir, env }: ApplyEntryOptions):
250
281
  if (backup.receipt) writeReceipt(backup.path, backup.image);
251
282
  return appliedResult(selection, path, next.change, receipt.intent, legacy);
252
283
  }
253
- if (receipt && receipt.intent === plan.intent && next.change === "unchanged") {
284
+ if (receipt && receipt.intent === plan.intent && receipt.owner === plan.owner && next.change === "unchanged") {
254
285
  return result(selection, path, "unchanged", receipt.intent);
255
286
  }
256
287
  if (!sameImage(current, plan.before) || !sameImage(next.after, plan.after)) {
@@ -259,9 +290,13 @@ function applyPlan(plan: PlannedEntry, { receiptDir, env }: ApplyEntryOptions):
259
290
 
260
291
  const legacy = receipt ? undefined : readLegacyReceipt(receiptDir, selection, plan.legacyPaths);
261
292
  const baseline = receipt ? restoreOwned(receipt, current, plan.file) : legacy ? legacyBaseline(selection, legacy, current) : current;
262
- if (next.change === "unchanged" && !legacy) return result(selection, path, "unchanged", next.intent);
293
+ const { owner } = plan;
294
+ if (next.change === "unchanged" && !legacy && (!receipt || receipt.owner === owner)) {
295
+ return result(selection, path, "unchanged", next.intent);
296
+ }
263
297
  const completed: Receipt = {
264
298
  version: 1, ...selection, file: receipt?.file ?? selection.file, intent: next.intent, before: baseline, after: next.after,
299
+ ...(owner === undefined ? {} : { owner }),
265
300
  };
266
301
  if (next.change === "unchanged") {
267
302
  writeReceipt(path, image, completed);
@@ -388,6 +423,7 @@ function legacyBaseline(selection: ReceiptTarget, legacy: LegacyReceipt, current
388
423
  }
389
424
 
390
425
  export interface EntryReceipt {
426
+ owner?: string;
391
427
  file: string;
392
428
  receipt: string;
393
429
  intent: string;
@@ -424,8 +460,8 @@ export function readEntryReceipt(options: EntryTarget & { receiptDir: string }):
424
460
  const legacyPaths = options.paths.legacyPaths[options.host];
425
461
  const { path, receipt } = findPackageReceipt(options.receiptDir, selection, legacyPaths);
426
462
  if (receipt) {
427
- const { file, intent, before, after, pending } = receipt;
428
- return { file, receipt: path, intent, before, after, ...(pending ? { pending } : {}) };
463
+ const { file, intent, before, after, owner, pending } = receipt;
464
+ return { file, receipt: path, intent, before, after, ...(owner === undefined ? {} : { owner }), ...(pending ? { pending } : {}) };
429
465
  }
430
466
  const legacy = readLegacyReceipt(options.receiptDir, selection, legacyPaths);
431
467
  return legacy ? legacyView(selection, legacy) : undefined;
package/src/fs-safe.ts CHANGED
@@ -43,13 +43,18 @@ function checkPath(file: string, directory = false): Stats | undefined {
43
43
  return stat;
44
44
  }
45
45
 
46
+ /** Inspect a directory chain without following a link at any point. */
47
+ export function checkPhysicalDirectory(directory: string): Stats | undefined {
48
+ return checkPath(directory, true);
49
+ }
50
+
46
51
  function assertPhysical(file: string, existing: string): void {
47
52
  if (realPath(existing) !== existing) {
48
53
  throw new EntryRefusal("unsafe-path", `${file}: redirected path; use the exact physical path spelling`, { file: file });
49
54
  }
50
55
  }
51
56
 
52
- function ensureDirectory(directory: string): void {
57
+ export function ensureDirectory(directory: string): void {
53
58
  checkPath(directory, true);
54
59
  mkdirSync(directory, { recursive: true, mode: 0o700 });
55
60
  checkPath(directory, true);
package/src/hosts.ts CHANGED
@@ -109,6 +109,13 @@ export const HOSTS = {
109
109
  entry: stdio,
110
110
  verified: ["0.61.0", "0.67.0"],
111
111
  },
112
+ copilot: {
113
+ id: "copilot",
114
+ files: [{ scope: "user", path: "mcp-config.json", override: { key: "COPILOT_HOME", rule: "absolute-only", directory: ".copilot" } }],
115
+ format: mcpServers,
116
+ entry: { ...stdio, type: "local", tools: "string[]" },
117
+ verified: ["1.0.107"],
118
+ },
112
119
  agy: {
113
120
  id: "agy",
114
121
  files: [{ scope: "project", path: ".agents/mcp_config.json" }],
@@ -119,3 +126,50 @@ export const HOSTS = {
119
126
  } as const satisfies Record<string, HostDefinition>;
120
127
 
121
128
  export type HostId = keyof typeof HOSTS;
129
+
130
+ export interface HostSkillDefinition {
131
+ readonly path: string;
132
+ readonly override: HomeOverride;
133
+ readonly preferredOverride?: HomeOverride;
134
+ readonly managedProfile?: "claude" | "codex";
135
+ readonly verified: readonly [string, ...string[]];
136
+ }
137
+
138
+ /** Verified user installation roots. Hosts may discover skills from additional locations. */
139
+ export const SKILL_HOSTS = {
140
+ claude: {
141
+ path: "skills",
142
+ override: { key: "CLAUDE_CONFIG_DIR", rule: "trim-presence", directory: ".claude" },
143
+ managedProfile: "claude",
144
+ verified: ["2.1.332"],
145
+ },
146
+ codex: {
147
+ path: "skills",
148
+ override: HOSTS.codex.files[0].override,
149
+ managedProfile: "codex",
150
+ verified: ["0.172.0"],
151
+ },
152
+ pi: {
153
+ path: "skills",
154
+ override: HOSTS.pi.files[0].override,
155
+ verified: ["1.6.1"],
156
+ },
157
+ qwen: {
158
+ path: "skills",
159
+ override: HOSTS.qwen.files[0].override,
160
+ verified: ["0.26.9", "0.27.1"],
161
+ },
162
+ opencode: {
163
+ path: "opencode/skills",
164
+ override: HOSTS.opencode.files[0].override,
165
+ preferredOverride: { key: "OPENCODE_CONFIG_DIR", rule: "absolute-only", directory: "" },
166
+ verified: ["1.19.5"],
167
+ },
168
+ gemini: {
169
+ path: ".gemini/skills",
170
+ override: HOSTS.gemini.files[0].override,
171
+ verified: ["0.67.0"],
172
+ },
173
+ } as const satisfies Partial<Record<HostId, HostSkillDefinition>>;
174
+
175
+ export type SkillHostId = keyof typeof SKILL_HOSTS;
package/src/index.ts CHANGED
@@ -5,3 +5,6 @@ export { HOSTS, type HostConfig, type HostDefinition, type HostFormat, type Host
5
5
  export { resolveHostPaths, type HostPaths, type HostRedirect, type HostRefusal, type ResolveHostPathsOptions } from "./paths.ts";
6
6
  export { applyEntry, planEntry, readEntryReceipt, undoEntry, UNDO_NOOP_CODES, type ApplyEntryOptions, type EntryPlan, type EntryReceipt, type EntryResult, type EntryTarget, type PlanEntryOptions, type UndoNoopCode } from "./entry.ts";
7
7
  export { EntryRefusal, REFUSAL_CODES, type RefusalCode } from "./refusal.ts";
8
+ export { SKILL_HOSTS, type HostSkillDefinition, type SkillHostId } from "./hosts.ts";
9
+ export { resolveSkillRoots, type ResolveSkillRootsOptions, type SkillRoots } from "./paths.ts";
10
+ export { applySkillEntry, classifySkillTarget, planSkillEntry, undoSkillEntry, type PlanSkillEntryOptions, type SkillEntryPlan, type SkillEntryResult, type SkillEntryTarget, type SkillTargetClassification } from "./skills.ts";
package/src/paths.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
2
- import { HOSTS, type HostDefinition, type HostId, type HostScope, type HomeOverride } from "./hosts.ts";
2
+ import { HOSTS, SKILL_HOSTS, type HostSkillDefinition, type SkillHostId, type HostDefinition, type HostId, type HostScope, type HomeOverride } from "./hosts.ts";
3
3
  import { realPath } from "./platform.ts";
4
4
  import type { RefusalCode } from "./refusal.ts";
5
5
 
@@ -33,6 +33,8 @@ export interface HostRefusal {
33
33
 
34
34
  export interface HostPaths {
35
35
  home: string;
36
+ suiteReceipts?: string;
37
+ suiteReceiptRefusal?: Omit<HostRefusal, "host">;
36
38
  paths: Partial<Record<HostId, string[]>>;
37
39
  /** Pre-redirect paths for validating receipts from older setup versions. */
38
40
  legacyPaths: Partial<Record<HostId, string[]>>;
@@ -140,5 +142,68 @@ export function resolveHostPaths(options: ResolveHostPathsOptions): HostPaths {
140
142
  legacyPaths[target] = legacy;
141
143
  roots[target] = { current: currentRoots, legacy: legacyRoots };
142
144
  }
143
- return { home, paths, legacyPaths, redirects, refusals, roots };
145
+ const suiteHome = env.AGENT_SUITE_HOME?.trim();
146
+ const hubHome = env.AGENTHUB_HOME?.trim();
147
+ const variable = suiteHome ? "AGENT_SUITE_HOME" : "AGENTHUB_HOME";
148
+ const selectedHome = suiteHome || hubHome;
149
+ if (selectedHome && !isAbsolute(selectedHome)) {
150
+ return { home, paths, legacyPaths, redirects, refusals, roots, suiteReceiptRefusal: {
151
+ code: "relative-override", variable, reason: `${variable} is relative; unset it or supply an absolute directory`,
152
+ } };
153
+ }
154
+ const suiteRoot = suiteHome || join(hubHome || join(home, ".agenthub"), "suite");
155
+ return { home, paths, legacyPaths, redirects, refusals, roots, suiteReceipts: join(canonical(suiteRoot), "receipts") };
156
+ }
157
+
158
+ export interface ResolveSkillRootsOptions extends Omit<ResolveHostPathsOptions, "hosts"> {
159
+ hosts?: readonly HostId[];
160
+ }
161
+
162
+ export interface SkillRoots {
163
+ roots: Partial<Record<SkillHostId, string[]>>;
164
+ redirects: HostRedirect[];
165
+ refusals: HostRefusal[];
166
+ }
167
+
168
+ /** Select one verified installation root per host without writing to disk. */
169
+ export function resolveSkillRoots(options: ResolveSkillRootsOptions): SkillRoots {
170
+ const { env, scopes = ["user"], redirectManagedProfiles = true } = options;
171
+ const home = canonical(options.home);
172
+ const result: SkillRoots = { roots: {}, redirects: [], refusals: [] };
173
+ for (const id of new Set(options.hosts ?? (Object.keys(SKILL_HOSTS) as SkillHostId[]))) {
174
+ if (!Object.hasOwn(SKILL_HOSTS, id)) continue;
175
+ const host = id as SkillHostId;
176
+ const definition: HostSkillDefinition = SKILL_HOSTS[host];
177
+ if (!scopes.includes("user")) {
178
+ result.roots[host] = [];
179
+ continue;
180
+ }
181
+ const preferred = definition.preferredOverride;
182
+ const override = preferred && env[preferred.key] ? preferred : definition.override;
183
+ if (env[override.key]?.includes(String.fromCharCode(0))) {
184
+ result.refusals.push({
185
+ host, variable: override.key, code: "invalid-skill-override",
186
+ reason: `${override.key} contains a NUL character; supply an absolute directory or unset the override`,
187
+ });
188
+ continue;
189
+ }
190
+ const root = configRoot(override, env, home);
191
+ if (typeof root !== "string") {
192
+ result.refusals.push({ host, ...root });
193
+ continue;
194
+ }
195
+ const from = join(root, override === preferred ? "skills" : definition.path);
196
+ const personal = join(home, definition.override.directory, definition.path);
197
+ const orcaRoot = env.ORCA_USER_DATA_PATH?.trim() || join(home, "Library", "Application Support", "orca");
198
+ if (redirectManagedProfiles && definition.managedProfile && from !== personal
199
+ && orcaHome(root, definition.managedProfile, orcaRoot)) {
200
+ result.roots[host] = [personal];
201
+ result.redirects.push({
202
+ target: host, from, to: personal, reason: "The managed profile uses the personal skill root",
203
+ });
204
+ continue;
205
+ }
206
+ result.roots[host] = [from];
207
+ }
208
+ return result;
144
209
  }
package/src/receipt.ts CHANGED
@@ -22,6 +22,7 @@ export interface ReceiptTarget {
22
22
 
23
23
  export interface Receipt extends ReceiptTarget {
24
24
  version: 1;
25
+ owner?: string;
25
26
  intent: string;
26
27
  before: ConfigImage;
27
28
  after: ConfigImage;
@@ -39,6 +40,10 @@ const isRecord = (value: unknown): value is Record<string, unknown> =>
39
40
  value !== null && typeof value === "object" && !Array.isArray(value);
40
41
  const isDigest = (value: unknown): value is string => typeof value === "string" && hashPattern.test(value);
41
42
 
43
+ export function isReceiptOwner(value: unknown): value is string {
44
+ return typeof value === "string" && value.length > 0 && !/[\p{Cc}\p{Cf}]/u.test(value) && Buffer.byteLength(value) <= 256;
45
+ }
46
+
42
47
  export function isConfigImage(value: unknown): value is ConfigImage {
43
48
  if (!isRecord(value) || typeof value.mode !== "number" || !Number.isInteger(value.mode)
44
49
  || value.mode < 0 || value.mode > 0o7777) return false;
@@ -66,7 +71,8 @@ export function readReceipt(path: string, target: ReceiptTarget): { image: Confi
66
71
  throw new EntryRefusal("invalid-receipt", `${path}: invalid or unsupported receipt; inspect or restore the original receipt before retrying`, { file: path });
67
72
  };
68
73
  if (!isRecord(value) || value.version !== 1 || value.host !== target.host || value.file !== target.file
69
- || value.name !== target.name || !isDigest(value.intent) || !isConfigImage(value.before) || !isConfigImage(value.after)) return refuse();
74
+ || value.name !== target.name || (value.owner !== undefined && !isReceiptOwner(value.owner))
75
+ || !isDigest(value.intent) || !isConfigImage(value.before) || !isConfigImage(value.after)) return refuse();
70
76
  const pending = value.pending;
71
77
  if (pending !== undefined && (!isRecord(pending) || (pending.kind !== "apply" && pending.kind !== "undo")
72
78
  || !isDigest(pending.start) || !isDigest(pending.result)
package/src/refusal.ts CHANGED
@@ -16,9 +16,16 @@ export const REFUSAL_CODES = [
16
16
  "file-changed",
17
17
  "invalid-plan",
18
18
  "invalid-receipt",
19
+ "suite-managed",
19
20
  "managed-entry-changed",
20
21
  "pending-operation",
21
22
  "io-error",
23
+ "invalid-skill-override",
24
+ "foreign-skill-link",
25
+ "skill-tool-marker",
26
+ "system-reserved-skill",
27
+ "unmanaged-skill-target",
28
+ "profile-root-changed",
22
29
  ] as const;
23
30
 
24
31
  export type RefusalCode = typeof REFUSAL_CODES[number];