@nebutra/agent-runtime 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/LICENSE +21 -676
  2. package/README.md +2 -0
  3. package/dist/adapters/dispatcher-sse.js +1 -0
  4. package/dist/adapters/index.d.ts +24 -5
  5. package/dist/adapters/index.js +28 -0
  6. package/dist/adapters/index.js.map +1 -1
  7. package/dist/adapters/mcp-catalog.js +1 -0
  8. package/dist/adapters/prisma-rollout.js +1 -0
  9. package/dist/chunk-424PT5DM.js +23 -0
  10. package/dist/chunk-424PT5DM.js.map +1 -0
  11. package/dist/{chunk-NN7DATXA.js → chunk-4Y25ZTKI.js} +3 -3
  12. package/dist/chunk-4Y25ZTKI.js.map +1 -0
  13. package/dist/{chunk-BJBBR3QA.js → chunk-D4YAPLOW.js} +4 -4
  14. package/dist/chunk-D4YAPLOW.js.map +1 -0
  15. package/dist/{chunk-ZMYX5VBU.js → chunk-GQZKYWFT.js} +23 -7
  16. package/dist/chunk-GQZKYWFT.js.map +1 -0
  17. package/dist/chunk-KCNN4QUQ.js +255 -0
  18. package/dist/chunk-KCNN4QUQ.js.map +1 -0
  19. package/dist/{chunk-PGGWSUTM.js → chunk-NI4EDT4T.js} +2 -2
  20. package/dist/chunk-NI4EDT4T.js.map +1 -0
  21. package/dist/chunk-Q62VKHIT.js +178 -0
  22. package/dist/chunk-Q62VKHIT.js.map +1 -0
  23. package/dist/{chunk-MUF7ZZTO.js → chunk-R5HOSQUW.js} +2 -1
  24. package/dist/{chunk-MUF7ZZTO.js.map → chunk-R5HOSQUW.js.map} +1 -1
  25. package/dist/{chunk-5N4644PB.js → chunk-SD2ZJ7XG.js} +4 -4
  26. package/dist/cli.d.ts +2 -0
  27. package/dist/cli.js +52 -0
  28. package/dist/cli.js.map +1 -0
  29. package/dist/commands.js +1 -0
  30. package/dist/definitions.js +1 -0
  31. package/dist/dispatcher.js +1 -0
  32. package/dist/durable-turn.js +3 -2
  33. package/dist/hook-pipeline.js +1 -0
  34. package/dist/index.d.ts +8 -85
  35. package/dist/index.js +249 -134
  36. package/dist/index.js.map +1 -1
  37. package/dist/loop.d.ts +2 -2
  38. package/dist/loop.js +3 -2
  39. package/dist/mcp-bridge.d.ts +3 -3
  40. package/dist/mcp-bridge.js +3 -2
  41. package/dist/model.js +1 -0
  42. package/dist/orchestration.d.ts +84 -0
  43. package/dist/orchestration.js +16 -0
  44. package/dist/orchestration.js.map +1 -0
  45. package/dist/policy.js +1 -0
  46. package/dist/protocol.js +1 -0
  47. package/dist/pulsar.d.ts +78 -0
  48. package/dist/pulsar.js +17 -0
  49. package/dist/pulsar.js.map +1 -0
  50. package/dist/rollout-store-persistent.js +1 -0
  51. package/dist/rollout.js +1 -0
  52. package/dist/sandbox.js +2 -1
  53. package/dist/skills.js +2 -1
  54. package/dist/subagents.js +1 -0
  55. package/dist/tools.d.ts +4 -3
  56. package/dist/tools.js +5 -3
  57. package/package.json +84 -27
  58. package/.turbo/turbo-build.log +0 -115
  59. package/.turbo/turbo-test.log +0 -44
  60. package/.turbo/turbo-typecheck.log +0 -4
  61. package/CHANGELOG.md +0 -253
  62. package/dist/chunk-BJBBR3QA.js.map +0 -1
  63. package/dist/chunk-NN7DATXA.js.map +0 -1
  64. package/dist/chunk-PGGWSUTM.js.map +0 -1
  65. package/dist/chunk-ZMYX5VBU.js.map +0 -1
  66. package/src/adapters/dispatcher-sse.test.ts +0 -218
  67. package/src/adapters/dispatcher-sse.ts +0 -222
  68. package/src/adapters/index.ts +0 -18
  69. package/src/adapters/mcp-catalog.test.ts +0 -213
  70. package/src/adapters/mcp-catalog.ts +0 -188
  71. package/src/adapters/prisma-rollout.test.ts +0 -153
  72. package/src/adapters/prisma-rollout.ts +0 -104
  73. package/src/agent-runtime.test.ts +0 -176
  74. package/src/artifact-stream.test.ts +0 -330
  75. package/src/artifact-stream.ts +0 -453
  76. package/src/channel-gateway.test.ts +0 -432
  77. package/src/channel-gateway.ts +0 -357
  78. package/src/code-review.test.ts +0 -501
  79. package/src/code-review.ts +0 -495
  80. package/src/command-suggestions.test.ts +0 -251
  81. package/src/command-suggestions.ts +0 -338
  82. package/src/commands.test.ts +0 -184
  83. package/src/commands.ts +0 -140
  84. package/src/commit-message.test.ts +0 -249
  85. package/src/commit-message.ts +0 -180
  86. package/src/context-compaction.test.ts +0 -522
  87. package/src/context-compaction.ts +0 -434
  88. package/src/definitions.test.ts +0 -78
  89. package/src/definitions.ts +0 -190
  90. package/src/deployment-status.test.ts +0 -215
  91. package/src/deployment-status.ts +0 -227
  92. package/src/design-context.test.ts +0 -195
  93. package/src/design-context.ts +0 -198
  94. package/src/dispatcher.test.ts +0 -234
  95. package/src/dispatcher.ts +0 -189
  96. package/src/durable-turn.test.ts +0 -209
  97. package/src/durable-turn.ts +0 -135
  98. package/src/edit-planner.test.ts +0 -204
  99. package/src/edit-planner.ts +0 -325
  100. package/src/fuzzy-match.test.ts +0 -311
  101. package/src/fuzzy-match.ts +0 -444
  102. package/src/hook-pipeline.test.ts +0 -279
  103. package/src/hook-pipeline.ts +0 -373
  104. package/src/inbound-admission.test.ts +0 -394
  105. package/src/inbound-admission.ts +0 -246
  106. package/src/index.ts +0 -47
  107. package/src/loop.test.ts +0 -161
  108. package/src/loop.ts +0 -211
  109. package/src/mcp-bridge.test.ts +0 -165
  110. package/src/mcp-bridge.ts +0 -76
  111. package/src/memory-provider.test.ts +0 -232
  112. package/src/memory-provider.ts +0 -257
  113. package/src/model.ts +0 -168
  114. package/src/permission-ruleset.test.ts +0 -301
  115. package/src/permission-ruleset.ts +0 -200
  116. package/src/policy.ts +0 -151
  117. package/src/project-repo.test.ts +0 -232
  118. package/src/project-repo.ts +0 -311
  119. package/src/protocol.ts +0 -159
  120. package/src/rollout-store-persistent.test.ts +0 -217
  121. package/src/rollout-store-persistent.ts +0 -166
  122. package/src/rollout.ts +0 -150
  123. package/src/sandbox.ts +0 -113
  124. package/src/session-share.test.ts +0 -360
  125. package/src/session-share.ts +0 -310
  126. package/src/skill-distillation.test.ts +0 -177
  127. package/src/skill-distillation.ts +0 -369
  128. package/src/skills.test.ts +0 -277
  129. package/src/skills.ts +0 -255
  130. package/src/subagents.test.ts +0 -290
  131. package/src/subagents.ts +0 -332
  132. package/src/tools.ts +0 -126
  133. package/src/workbench.test.ts +0 -0
  134. package/src/workbench.ts +0 -0
  135. package/tsconfig.json +0 -12
  136. package/tsup.config.ts +0 -33
  137. /package/dist/{chunk-5N4644PB.js.map → chunk-SD2ZJ7XG.js.map} +0 -0
@@ -1,277 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import { DefinitionResolver } from "./definitions";
3
- import { buildSkillListing, expandSkill, type SkillRecord } from "./skills";
4
-
5
- const skill = (
6
- over: Partial<SkillRecord> &
7
- Pick<SkillRecord, "slug" | "tenantId" | "sourceTier"> & {
8
- name?: string;
9
- description?: string;
10
- whenToUse?: string;
11
- allowedTools?: string[];
12
- model?: string;
13
- effort?: "low" | "medium" | "high";
14
- executionMode?: "inline" | "fork";
15
- paths?: string[];
16
- },
17
- ): SkillRecord => ({
18
- bodyRef: over.bodyRef ?? `${over.slug}.body`,
19
- ...over,
20
- frontmatter: {
21
- name: over.name ?? over.slug,
22
- description: over.description ?? "",
23
- allowedTools: over.allowedTools ?? [],
24
- disallowedTools: [],
25
- argNames: [],
26
- modelInvocable: true,
27
- userInvocable: true,
28
- executionMode: over.executionMode ?? "inline",
29
- paths: over.paths ?? [],
30
- whenToUse: over.whenToUse,
31
- model: over.model,
32
- effort: over.effort,
33
- },
34
- });
35
-
36
- describe("buildSkillListing", () => {
37
- it("lists only name/description/whenToUse per skill", () => {
38
- const r = new DefinitionResolver<SkillRecord>([
39
- skill({
40
- slug: "deploy",
41
- tenantId: "t",
42
- sourceTier: "workspace",
43
- description: "ship it",
44
- whenToUse: "when releasing",
45
- }),
46
- ]);
47
- const out = buildSkillListing(r, { tenantId: "t" });
48
- expect(out.entries).toHaveLength(1);
49
- expect(out.entries[0]).toMatchObject({
50
- name: "deploy",
51
- description: "ship it",
52
- whenToUse: "when releasing",
53
- });
54
- expect(out.text).toContain("deploy");
55
- expect(out.text).toContain("ship it");
56
- });
57
-
58
- it("truncates per-entry description to maxListingDescChars", () => {
59
- const long = "x".repeat(500);
60
- const r = new DefinitionResolver<SkillRecord>([
61
- skill({ slug: "a", tenantId: "t", sourceTier: "workspace", description: long }),
62
- ]);
63
- const out = buildSkillListing(r, { tenantId: "t" }, { maxListingDescChars: 10 });
64
- expect(out.entries[0]?.description.length).toBeLessThanOrEqual(10);
65
- });
66
-
67
- it("first-party (bundled/builtin) is never truncated and always included even past budget", () => {
68
- const long = "y".repeat(400);
69
- const r = new DefinitionResolver<SkillRecord>([
70
- skill({ slug: "fp", tenantId: "t", sourceTier: "bundled", description: long }),
71
- skill({ slug: "lo", tenantId: "t", sourceTier: "workspace", description: long }),
72
- ]);
73
- const out = buildSkillListing(
74
- r,
75
- { tenantId: "t" },
76
- { contextWindowTokens: 1, budgetPercent: 0.01, maxListingDescChars: 1000 },
77
- );
78
- const fp = out.entries.find((e) => e.name === "fp");
79
- expect(fp).toBeDefined();
80
- expect(fp?.description).toBe(long);
81
- expect(fp?.degraded).toBe(false);
82
- });
83
-
84
- it("lower-tier skills degrade to names-only then drop when budget exceeded", () => {
85
- const long = "z".repeat(400);
86
- const r = new DefinitionResolver<SkillRecord>([
87
- skill({ slug: "s1", tenantId: "t", sourceTier: "workspace", description: long }),
88
- skill({ slug: "s2", tenantId: "t", sourceTier: "workspace", description: long }),
89
- skill({ slug: "s3", tenantId: "t", sourceTier: "workspace", description: long }),
90
- ]);
91
- const tight = buildSkillListing(
92
- r,
93
- { tenantId: "t" },
94
- { contextWindowTokens: 100, budgetPercent: 0.01, maxListingDescChars: 1000 },
95
- );
96
- // some degrade to names-only (no description)
97
- expect(tight.entries.some((e) => e.degraded && e.description === "")).toBe(true);
98
-
99
- const veryTight = buildSkillListing(
100
- r,
101
- { tenantId: "t" },
102
- { contextWindowTokens: 1, budgetPercent: 0.01, maxListingDescChars: 1000 },
103
- );
104
- // beyond names-only, lower-tier entries drop entirely
105
- expect(veryTight.entries.length).toBeLessThan(3);
106
- expect(tight.entries.length).toBeGreaterThanOrEqual(veryTight.entries.length);
107
- });
108
-
109
- it("suppresses skills whose slug is in ctx.loadedSlugs", () => {
110
- const r = new DefinitionResolver<SkillRecord>([
111
- skill({ slug: "loaded", tenantId: "t", sourceTier: "workspace" }),
112
- skill({ slug: "fresh", tenantId: "t", sourceTier: "workspace" }),
113
- ]);
114
- const out = buildSkillListing(r, {
115
- tenantId: "t",
116
- loadedSlugs: new Set(["loaded"]),
117
- });
118
- expect(out.entries.map((e) => e.name)).toEqual(["fresh"]);
119
- });
120
-
121
- it("path-activated skills hidden until a touched path matches its glob", () => {
122
- const r = new DefinitionResolver<SkillRecord>([
123
- skill({
124
- slug: "gated",
125
- tenantId: "t",
126
- sourceTier: "workspace",
127
- paths: ["src/**/*.ts"],
128
- }),
129
- skill({ slug: "always", tenantId: "t", sourceTier: "workspace" }),
130
- ]);
131
- const hidden = buildSkillListing(r, { tenantId: "t" });
132
- expect(hidden.entries.map((e) => e.name)).toEqual(["always"]);
133
-
134
- const shown = buildSkillListing(r, { tenantId: "t" }, { touchedPaths: ["src/foo/bar.ts"] });
135
- expect(shown.entries.map((e) => e.name).sort()).toEqual(["always", "gated"]);
136
- });
137
-
138
- it("glob supports *, ** and exact match", () => {
139
- const r = new DefinitionResolver<SkillRecord>([
140
- skill({ slug: "star", tenantId: "t", sourceTier: "workspace", paths: ["*.md"] }),
141
- skill({ slug: "exact", tenantId: "t", sourceTier: "workspace", paths: ["a/b.txt"] }),
142
- ]);
143
- const a = buildSkillListing(r, { tenantId: "t" }, { touchedPaths: ["README.md"] });
144
- expect(a.entries.map((e) => e.name)).toEqual(["star"]);
145
- const b = buildSkillListing(r, { tenantId: "t" }, { touchedPaths: ["a/b.txt"] });
146
- expect(b.entries.map((e) => e.name)).toEqual(["exact"]);
147
- const none = buildSkillListing(r, { tenantId: "t" }, { touchedPaths: ["a/c.txt"] });
148
- expect(none.entries).toHaveLength(0);
149
- });
150
-
151
- it("empty tenant fails closed", () => {
152
- const r = new DefinitionResolver<SkillRecord>([]);
153
- expect(() => buildSkillListing(r, { tenantId: "" })).toThrow(/tenant/i);
154
- });
155
-
156
- it("cross-tenant isolation (resolver enforced)", () => {
157
- const r = new DefinitionResolver<SkillRecord>([
158
- skill({ slug: "a", tenantId: "t_a", sourceTier: "workspace" }),
159
- ]);
160
- expect(buildSkillListing(r, { tenantId: "t_b" }).entries).toHaveLength(0);
161
- });
162
-
163
- it("is deterministic — same inputs produce same output", () => {
164
- const r = new DefinitionResolver<SkillRecord>([
165
- skill({ slug: "a", tenantId: "t", sourceTier: "workspace", description: "aa" }),
166
- skill({ slug: "b", tenantId: "t", sourceTier: "workspace", description: "bb" }),
167
- ]);
168
- const o1 = buildSkillListing(r, { tenantId: "t" });
169
- const o2 = buildSkillListing(r, { tenantId: "t" });
170
- expect(o1.text).toBe(o2.text);
171
- });
172
- });
173
-
174
- describe("expandSkill", () => {
175
- const base = (slug: string) => `/skills/${slug}`;
176
-
177
- it("expands body, prepends resource base line, substitutes args + vars", async () => {
178
- const s = skill({
179
- slug: "deploy",
180
- tenantId: "t",
181
- sourceTier: "workspace",
182
- allowedTools: ["Bash", "Edit"],
183
- });
184
- const bodyLoader = vi.fn(async () => "deploy ${env} via ${SESSION}");
185
- const out = await expandSkill(s, {
186
- tenantId: "t",
187
- args: { env: "prod" },
188
- vars: { SESSION: "s1" },
189
- bodyLoader,
190
- skillResourceBase: base,
191
- });
192
- expect(bodyLoader).toHaveBeenCalledTimes(1);
193
- expect(bodyLoader).toHaveBeenCalledWith("deploy.body");
194
- const content = out.messages.map((m) => m.content).join("\n");
195
- expect(content).toContain("Base directory for this skill: /skills/deploy");
196
- expect(content).toContain("deploy prod via s1");
197
- expect(out.allowedTools).toEqual(["Bash", "Edit"]);
198
- expect(out.executionMode).toBe("inline");
199
- });
200
-
201
- it("returns model override + effort + fork mode", async () => {
202
- const s = skill({
203
- slug: "heavy",
204
- tenantId: "t",
205
- sourceTier: "workspace",
206
- model: "big-model",
207
- effort: "high",
208
- executionMode: "fork",
209
- });
210
- const out = await expandSkill(s, {
211
- tenantId: "t",
212
- args: {},
213
- vars: {},
214
- bodyLoader: async () => "body",
215
- skillResourceBase: base,
216
- });
217
- expect(out.modelOverride).toBe("big-model");
218
- expect(out.effort).toBe("high");
219
- expect(out.executionMode).toBe("fork");
220
- });
221
-
222
- it("body loader is lazy — only invoked on expand", async () => {
223
- const r = new DefinitionResolver<SkillRecord>([
224
- skill({ slug: "lazy", tenantId: "t", sourceTier: "workspace" }),
225
- ]);
226
- const bodyLoader = vi.fn(async () => "b");
227
- buildSkillListing(r, { tenantId: "t" });
228
- expect(bodyLoader).not.toHaveBeenCalled();
229
- await expandSkill(r.resolveOne("lazy", { tenantId: "t" })!, {
230
- tenantId: "t",
231
- args: {},
232
- vars: {},
233
- bodyLoader,
234
- skillResourceBase: base,
235
- });
236
- expect(bodyLoader).toHaveBeenCalledTimes(1);
237
- });
238
-
239
- it("empty tenant fails closed on expand", async () => {
240
- const s = skill({ slug: "x", tenantId: "t", sourceTier: "workspace" });
241
- await expect(
242
- expandSkill(s, {
243
- tenantId: "",
244
- args: {},
245
- vars: {},
246
- bodyLoader: async () => "b",
247
- skillResourceBase: base,
248
- }),
249
- ).rejects.toThrow(/tenant/i);
250
- });
251
-
252
- it("cross-tenant expand fails closed", async () => {
253
- const s = skill({ slug: "x", tenantId: "t_a", sourceTier: "workspace" });
254
- await expect(
255
- expandSkill(s, {
256
- tenantId: "t_b",
257
- args: {},
258
- vars: {},
259
- bodyLoader: async () => "b",
260
- skillResourceBase: base,
261
- }),
262
- ).rejects.toThrow(/tenant/i);
263
- });
264
-
265
- it("does not execute any shell from the body (no command substitution)", async () => {
266
- const s = skill({ slug: "x", tenantId: "t", sourceTier: "workspace" });
267
- const out = await expandSkill(s, {
268
- tenantId: "t",
269
- args: {},
270
- vars: {},
271
- bodyLoader: async () => "literal !`rm -rf /` stays literal",
272
- skillResourceBase: base,
273
- });
274
- const content = out.messages.map((m) => m.content).join("\n");
275
- expect(content).toContain("!`rm -rf /`");
276
- });
277
- });
package/src/skills.ts DELETED
@@ -1,255 +0,0 @@
1
- /**
2
- * Skills system (WRAP — progressive-disclosure capability).
3
- *
4
- * Faithful re-expression of the source harness's two-phase skill model:
5
- *
6
- * Phase 1 — Listing (cheap, always in context): a budget-bounded catalog
7
- * exposing only {name, description, whenToUse} per skill. First-party
8
- * skills (bundled/builtin tier) are never truncated and never dropped;
9
- * lower-tier skills degrade to names-only, then drop, as the context
10
- * budget is exhausted. Path-activated skills stay hidden until a touched
11
- * file matches one of their globs.
12
- *
13
- * Phase 2 — Invocation (lazy, on demand): expand the chosen skill's body
14
- * into turn messages, with arg/var substitution and a merged tool
15
- * allowlist contributed to the permission context.
16
- *
17
- * A skill IS a `Definition` — the same tenant-scoped layered resolver backs
18
- * the command registry and the subagent registry (see ./definitions).
19
- *
20
- * SECURITY: this re-expression intentionally drops the source's body-side
21
- * command-substitution (the upstream "!`…`" inline-shell facility). Skill
22
- * bodies are inert text; they NEVER trigger host shell execution here. All
23
- * entry points require a non-empty `tenantId` and fail closed; cross-tenant
24
- * inputs are rejected by the underlying `DefinitionResolver`. Pure data/logic
25
- * — no host filesystem access (resource base is an injected indirection).
26
- */
27
-
28
- import { z } from "zod";
29
- import {
30
- type Definition,
31
- type DefinitionResolver,
32
- type ResolveContext,
33
- substituteArguments,
34
- } from "./definitions";
35
-
36
- /** A skill is just a resolvable `Definition`. */
37
- export type SkillRecord = Definition;
38
-
39
- /** Tiers treated as first-party (never truncated, never dropped). */
40
- const FIRST_PARTY_TIERS: ReadonlySet<string> = new Set(["bundled", "builtin"]);
41
-
42
- const isFirstParty = (s: SkillRecord): boolean => FIRST_PARTY_TIERS.has(s.sourceTier);
43
-
44
- // ── Phase 1: listing ─────────────────────────────────────────────────────────
45
-
46
- /** One catalog entry — only the cheap, always-in-context surface. */
47
- export interface SkillListingEntry {
48
- readonly name: string;
49
- readonly description: string;
50
- readonly whenToUse?: string | undefined;
51
- /** True when this entry was degraded to names-only by the budget. */
52
- readonly degraded: boolean;
53
- readonly firstParty: boolean;
54
- }
55
-
56
- export interface SkillListing {
57
- readonly entries: readonly SkillListingEntry[];
58
- readonly text: string;
59
- }
60
-
61
- const listingOptionsSchema = z.object({
62
- maxListingDescChars: z.number().int().positive().default(250),
63
- contextWindowTokens: z.number().int().positive().default(200_000),
64
- budgetPercent: z.number().positive().max(1).default(0.01),
65
- touchedPaths: z.array(z.string()).default([]),
66
- });
67
- export type SkillListingOptions = z.input<typeof listingOptionsSchema>;
68
-
69
- /** Cheap, deterministic token estimate (≈4 chars/token). */
70
- const estimateTokens = (text: string): number => Math.ceil(text.length / 4);
71
-
72
- /** Minimal glob: `**` = any chars, `*` = any chars except `/`, else exact. */
73
- function globToRegExp(glob: string): RegExp {
74
- let out = "^";
75
- for (let i = 0; i < glob.length; i++) {
76
- const c = glob[i]!;
77
- if (c === "*") {
78
- if (glob[i + 1] === "*") {
79
- out += ".*";
80
- i++;
81
- } else {
82
- out += "[^/]*";
83
- }
84
- } else if (".+?^${}()|[]\\".includes(c)) {
85
- out += `\\${c}`;
86
- } else {
87
- out += c;
88
- }
89
- }
90
- return new RegExp(`${out}$`);
91
- }
92
-
93
- const pathMatches = (globs: readonly string[], touched: readonly string[]): boolean =>
94
- globs.some((g) => {
95
- const re = globToRegExp(g);
96
- return touched.some((p) => re.test(p));
97
- });
98
-
99
- /** Stable order: first-party first, then by slug — deterministic output. */
100
- function orderSkills(skills: readonly SkillRecord[]): SkillRecord[] {
101
- return [...skills].sort((a, b) => {
102
- const fa = isFirstParty(a) ? 0 : 1;
103
- const fb = isFirstParty(b) ? 0 : 1;
104
- if (fa !== fb) return fa - fb;
105
- return a.slug.localeCompare(b.slug);
106
- });
107
- }
108
-
109
- const truncate = (s: string, max: number): string => (s.length <= max ? s : s.slice(0, max));
110
-
111
- function renderEntry(e: SkillListingEntry): string {
112
- const head = e.whenToUse ? `- ${e.name} — ${e.whenToUse}` : `- ${e.name}`;
113
- return e.description ? `${head}\n ${e.description}` : head;
114
- }
115
-
116
- /**
117
- * Phase 1 — build the always-in-context skill catalog. Pure & deterministic.
118
- *
119
- * Budget = `contextWindowTokens * budgetPercent`. First-party skills are
120
- * emitted in full and do NOT consume budget against being dropped. Each
121
- * remaining skill is tried at full description; if it would overflow it is
122
- * degraded to names-only; if even that overflows it is dropped. Skills in
123
- * `ctx.loadedSlugs` and unactivated path-gated skills are suppressed.
124
- */
125
- export function buildSkillListing(
126
- resolver: DefinitionResolver<SkillRecord>,
127
- ctx: ResolveContext,
128
- options: SkillListingOptions = {},
129
- ): SkillListing {
130
- if (!ctx.tenantId) throw new Error("buildSkillListing: tenantId is required");
131
- const opts = listingOptionsSchema.parse(options);
132
-
133
- const loaded = ctx.loadedSlugs ?? new Set<string>();
134
- const visible = resolver
135
- .resolve(ctx)
136
- .filter((s) => !loaded.has(s.slug))
137
- .filter((s) => {
138
- const paths = s.frontmatter.paths;
139
- return paths.length === 0 || pathMatches(paths, opts.touchedPaths);
140
- });
141
-
142
- const budget = Math.max(1, Math.floor(opts.contextWindowTokens * opts.budgetPercent));
143
-
144
- const entries: SkillListingEntry[] = [];
145
- let used = 0;
146
-
147
- for (const s of orderSkills(visible)) {
148
- const fp = isFirstParty(s);
149
- const fm = s.frontmatter;
150
- const fullDesc = fp ? fm.description : truncate(fm.description, opts.maxListingDescChars);
151
-
152
- if (fp) {
153
- const entry: SkillListingEntry = {
154
- name: fm.name,
155
- description: fm.description,
156
- whenToUse: fm.whenToUse,
157
- degraded: false,
158
- firstParty: true,
159
- };
160
- entries.push(entry);
161
- used += estimateTokens(renderEntry(entry));
162
- continue;
163
- }
164
-
165
- const full: SkillListingEntry = {
166
- name: fm.name,
167
- description: fullDesc,
168
- whenToUse: fm.whenToUse,
169
- degraded: false,
170
- firstParty: false,
171
- };
172
- const fullCost = estimateTokens(renderEntry(full));
173
- if (used + fullCost <= budget) {
174
- entries.push(full);
175
- used += fullCost;
176
- continue;
177
- }
178
-
179
- const nameOnly: SkillListingEntry = {
180
- name: fm.name,
181
- description: "",
182
- whenToUse: undefined,
183
- degraded: true,
184
- firstParty: false,
185
- };
186
- const nameCost = estimateTokens(renderEntry(nameOnly));
187
- if (used + nameCost <= budget) {
188
- entries.push(nameOnly);
189
- used += nameCost;
190
- }
191
- // else: drop entirely.
192
- }
193
-
194
- const text = entries.map(renderEntry).join("\n");
195
- return { entries, text };
196
- }
197
-
198
- // ── Phase 2: invocation ──────────────────────────────────────────────────────
199
-
200
- export interface SkillMessage {
201
- readonly role: "system" | "user";
202
- readonly content: string;
203
- }
204
-
205
- export interface ExpandedSkill {
206
- readonly messages: readonly SkillMessage[];
207
- readonly allowedTools: readonly string[];
208
- readonly modelOverride?: string | undefined;
209
- readonly effort?: "low" | "medium" | "high" | undefined;
210
- readonly executionMode: "inline" | "fork";
211
- }
212
-
213
- export interface ExpandContext {
214
- readonly tenantId: string;
215
- readonly args: Record<string, string>;
216
- readonly vars: Record<string, string>;
217
- /** Lazy body fetch — host supplies; called only here, only once. */
218
- readonly bodyLoader: (bodyRef: string) => Promise<string>;
219
- /** Host-injected resource-base indirection (NO host FS access here). */
220
- readonly skillResourceBase: (slug: string) => string;
221
- }
222
-
223
- /**
224
- * Phase 2 — expand a skill into turn messages. Body is loaded lazily via the
225
- * injected `bodyLoader` (never during listing). A `Base directory…` line is
226
- * prepended via the injected `skillResourceBase` indirection. Args + vars are
227
- * substituted with `substituteArguments`. The skill's `allowedTools` are
228
- * returned for the caller to merge into the permission context — this never
229
- * mutates global state and never executes anything from the body.
230
- */
231
- export async function expandSkill(skill: SkillRecord, ctx: ExpandContext): Promise<ExpandedSkill> {
232
- if (!ctx.tenantId) throw new Error("expandSkill: tenantId is required");
233
- if (skill.tenantId !== ctx.tenantId) {
234
- throw new Error("expandSkill: cross-tenant skill access rejected");
235
- }
236
-
237
- const rawBody = await ctx.bodyLoader(skill.bodyRef);
238
- const body = substituteArguments(rawBody, ctx.args, ctx.vars);
239
- const baseLine = `Base directory for this skill: ${ctx.skillResourceBase(skill.slug)}`;
240
-
241
- const messages: readonly SkillMessage[] = [
242
- { role: "system", content: baseLine },
243
- { role: "user", content: body },
244
- ];
245
-
246
- const allowedTools = [...skill.frontmatter.allowedTools];
247
-
248
- return {
249
- messages,
250
- allowedTools,
251
- modelOverride: skill.frontmatter.model,
252
- effort: skill.frontmatter.effort,
253
- executionMode: skill.frontmatter.executionMode,
254
- };
255
- }