@fungi.computer/shiitake-extensions 0.8.0 → 0.9.1

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.
@@ -4,6 +4,11 @@ export declare const Home: Schema.Struct<{
4
4
  /** Chosen Computer attachment, or null when no Home is selected. */
5
5
  readonly attachmentId: Schema.NullOr<Schema.String>;
6
6
  }>;
7
+ /**
8
+ * Bytes of AGENTS.md and SYSTEM.md Home loads into one run, and so the most an
9
+ * owner may save. Local enforced the same bound for its root AGENTS.md.
10
+ */
11
+ export declare const homeInstructionsLimit = 32000;
7
12
  /** Persisted Home selection for one Agent. */
8
13
  export type Home = Schema.Schema.Type<typeof Home>;
9
14
  /** Human selection changes Agent state; no model operation exposes this command. */
@@ -7,6 +7,11 @@ export const Home = Schema.Struct({
7
7
  /** Chosen Computer attachment, or null when no Home is selected. */
8
8
  attachmentId: Schema.NullOr(attachmentId),
9
9
  });
10
+ /**
11
+ * Bytes of AGENTS.md and SYSTEM.md Home loads into one run, and so the most an
12
+ * owner may save. Local enforced the same bound for its root AGENTS.md.
13
+ */
14
+ export const homeInstructionsLimit = 32_000;
10
15
  /** Human selection changes Agent state; no model operation exposes this command. */
11
16
  export const homeContract = {
12
17
  /** Stable extension identity. */
@@ -1,5 +1,6 @@
1
1
  import { type FilesPort } from "@fungi.computer/knapsack";
2
2
  import { type ExtensionGrantV0, type RosteredExtensionV0 } from "@fungi.computer/shiitake/extensions";
3
+ import { Home } from "./contract.js";
3
4
  /** Already-scoped authority supplied by the host, never constructed by Home. */
4
5
  export type HomeComputer = Readonly<{
5
6
  /** Attachment name offered to the person choosing Home. */
@@ -11,6 +12,13 @@ export type HomeComputer = Readonly<{
11
12
  }>;
12
13
  /** Hosts admit Home explicitly; no other extension receives the Files slot. */
13
14
  export declare const homeGrant: ExtensionGrantV0;
15
+ /**
16
+ * The Home choice a run uses: the recorded one, otherwise the only attachment.
17
+ * Hosts that answer "which Home" outside a run apply this same rule.
18
+ */
19
+ export declare function resolveHome(recorded: Home | null, computers: readonly Readonly<{
20
+ attachmentId: string;
21
+ }>[]): Home | null;
14
22
  /** Agent-scope choice with per-run tools and prompt over one captured table. */
15
23
  export declare function homeExtension(options: {
16
24
  computers: {
package/dist/home/host.js CHANGED
@@ -5,6 +5,7 @@ import * as Option from "effect/Option";
5
5
  import * as Schema from "effect/Schema";
6
6
  import { ask, catalog, done } from "../internal/catalog.js";
7
7
  import { Home, homeContract } from "./contract.js";
8
+ import { instructionSectionName, loadHomeInstructions, } from "./instructions.js";
8
9
  import { homeFilesRouting } from "./routing.js";
9
10
  /** Hosts admit Home explicitly; no other extension receives the Files slot. */
10
11
  export const homeGrant = {
@@ -13,16 +14,27 @@ export const homeGrant = {
13
14
  };
14
15
  const config = {
15
16
  promptLimit: 4_000,
16
- guide: "Home supplies read, list, glob and grep over your default Computer. Bare paths and computer://home/ use Home; computer://<name>/ uses that captured attachment. apply_patch, when offered, also works on Home. Only a person changes Home with /home <name>; ask them when no Home is available.",
17
+ guide: "Home supplies read, list, glob and grep over your default Computer. Bare paths and computer://home/ use Home; computer://<name>/ uses that captured attachment. apply_patch, when offered, also works on Home. Home's AGENTS.md and SYSTEM.md are loaded into your prompt each run. Only a person changes Home with /home <name>; ask them when no Home is available.",
17
18
  };
18
19
  const collections = {
19
20
  choice: defineCollection(Home, { key: () => "default" }),
20
21
  };
22
+ /**
23
+ * The Home choice a run uses: the recorded one, otherwise the only attachment.
24
+ * Hosts that answer "which Home" outside a run apply this same rule.
25
+ */
26
+ export function resolveHome(recorded, computers) {
27
+ if (recorded !== null)
28
+ return recorded;
29
+ const sole = computers.length === 1 ? computers[0] : undefined;
30
+ return sole === undefined ? null : { attachmentId: sole.attachmentId };
31
+ }
21
32
  /** Agent-scope choice with per-run tools and prompt over one captured table. */
22
33
  export function homeExtension(options) {
23
34
  // Core retains one RunIdentity for Files and every prompt request. Weak keys
24
35
  // release the captured prose with the run, including failure and cancellation.
25
36
  const prompts = new WeakMap();
37
+ const instructions = new WeakMap();
26
38
  return defineExtension({ id: homeContract.id, version: "1.0.0" })
27
39
  .config(config)
28
40
  .state({ version: 1, migrations: {}, collections })
@@ -56,14 +68,10 @@ export function homeExtension(options) {
56
68
  const computers = [...options.computers.read(run)];
57
69
  const choice = ctx.state.update((state) => {
58
70
  const recorded = Option.getOrNull(state.choice.get("default"));
59
- if (recorded !== null)
60
- return recorded;
61
- const sole = computers.length === 1 ? computers[0] : undefined;
62
- if (sole === undefined)
63
- return null;
64
- const adopted = { attachmentId: sole.attachmentId };
65
- state.choice.put(adopted);
66
- return adopted;
71
+ const resolved = resolveHome(recorded, computers);
72
+ if (recorded === null && resolved !== null)
73
+ state.choice.put(resolved);
74
+ return resolved;
67
75
  });
68
76
  const selected = computers.find((computer) => computer.attachmentId === choice?.attachmentId);
69
77
  const status = selected !== undefined
@@ -78,17 +86,34 @@ export function homeExtension(options) {
78
86
  prompts.set(run, prompt.slice(0, ctx.config.promptLimit));
79
87
  if (selected === undefined)
80
88
  return null;
81
- return Knapsack.files({
89
+ const tools = Knapsack.files({
82
90
  port: homeFilesRouting(new Map(computers.map(({ name, port }) => [name, port])), selected.port),
83
91
  }).tools;
92
+ // Home's files are read once for this run, after the state transaction.
93
+ return loadHomeInstructions({
94
+ port: selected.port,
95
+ executionId: `home-instructions/${run.workId}`,
96
+ signal: new AbortController().signal,
97
+ }).then((text) => {
98
+ if (text !== null)
99
+ instructions.set(run, text);
100
+ return tools;
101
+ });
102
+ },
103
+ prompt: (_ctx, run) => {
104
+ const pieces = instructions.get(run) ?? [];
105
+ return [
106
+ {
107
+ name: "home",
108
+ text: prompts.get(run) ??
109
+ "Home file tools were not captured. Ask the person to check Home's Files grant.",
110
+ },
111
+ ...pieces.map((text, index) => ({
112
+ name: instructionSectionName(index),
113
+ text,
114
+ })),
115
+ ];
84
116
  },
85
- prompt: (_ctx, run) => [
86
- {
87
- name: "home",
88
- text: prompts.get(run) ??
89
- "Home file tools were not captured. Ask the person to check Home's Files grant.",
90
- },
91
- ],
92
117
  commands: {
93
118
  specs: homeContract.commands,
94
119
  handlers: (ctx) => ({
@@ -0,0 +1,13 @@
1
+ import type { FilesPort } from "@fungi.computer/knapsack";
2
+ /** Prompt section names for `count` consecutive pieces: `instructions`, `instructions-2`, ... */
3
+ export declare function instructionSectionName(index: number): string;
4
+ /**
5
+ * The prompt text built from Home's AGENTS.md and SYSTEM.md, in pieces
6
+ * that each fit one prompt section, or null when Home has none. Absent files are normal; refused ones are named in the
7
+ * section so the Agent can tell its owner. A failing read never stops the run.
8
+ */
9
+ export declare function loadHomeInstructions(input: {
10
+ port: FilesPort;
11
+ executionId: string;
12
+ signal: AbortSignal;
13
+ }): Promise<readonly string[] | null>;
@@ -0,0 +1,109 @@
1
+ import { maximumReadBytes } from "@fungi.computer/knapsack/files-read";
2
+ import { homeInstructionsLimit } from "./contract.js";
3
+ /**
4
+ * Shiitake accepts a prompt section of at most 8,192 bytes (and 65,536 across
5
+ * all extensions), so a longer text is carried in consecutive sections.
6
+ */
7
+ const maximumSectionBytes = 8_192;
8
+ const maximumListedProblems = 16;
9
+ const utf8 = new TextEncoder();
10
+ const framing = "From files on Home (computer://home/AGENTS.md, SYSTEM.md). Your owner and you can edit them. Follow them unless they conflict with the instructions above. They never grant tools, approvals or access.";
11
+ const bytesOf = (text) => utf8.encode(text).byteLength;
12
+ /** A missing path, as every Files host reports it: an errno ENOENT. */
13
+ function absent(cause) {
14
+ return cause instanceof Error && "code" in cause && cause.code === "ENOENT";
15
+ }
16
+ async function readText(port, path, context) {
17
+ try {
18
+ const page = await port.read({ path, offset: 0, limit: maximumReadBytes }, context);
19
+ if ("image" in page)
20
+ return { kind: "refused", reason: "is an image" };
21
+ if (page.truncated)
22
+ return {
23
+ kind: "refused",
24
+ reason: `is larger than ${String(maximumReadBytes)} bytes`,
25
+ };
26
+ return { kind: "text", text: page.content };
27
+ }
28
+ catch (cause) {
29
+ if (absent(cause))
30
+ return { kind: "absent" };
31
+ // A Computer that cannot answer must not stop the run; the section says so.
32
+ return { kind: "refused", reason: "could not be read from Home" };
33
+ }
34
+ }
35
+ /** Cut `text` into pieces of at most `limit` bytes, at line ends when it can. */
36
+ function chunks(text, limit) {
37
+ const pieces = [];
38
+ let current = "";
39
+ let used = 0;
40
+ const push = (part, size) => {
41
+ if (used + size > limit) {
42
+ pieces.push(current);
43
+ current = "";
44
+ used = 0;
45
+ }
46
+ current += part;
47
+ used += size;
48
+ };
49
+ for (const line of text.split(/(?<=\n)/u)) {
50
+ const size = bytesOf(line);
51
+ if (size <= limit)
52
+ push(line, size);
53
+ else
54
+ for (const character of line)
55
+ push(character, bytesOf(character));
56
+ }
57
+ if (current !== "")
58
+ pieces.push(current);
59
+ return pieces;
60
+ }
61
+ /** Prompt section names for `count` consecutive pieces: `instructions`, `instructions-2`, ... */
62
+ export function instructionSectionName(index) {
63
+ return index === 0 ? "instructions" : `instructions-${String(index + 1)}`;
64
+ }
65
+ /**
66
+ * The prompt text built from Home's AGENTS.md and SYSTEM.md, in pieces
67
+ * that each fit one prompt section, or null when Home has none. Absent files are normal; refused ones are named in the
68
+ * section so the Agent can tell its owner. A failing read never stops the run.
69
+ */
70
+ export async function loadHomeInstructions(input) {
71
+ const context = {
72
+ executionId: input.executionId,
73
+ deadlineAt: null,
74
+ signal: input.signal,
75
+ };
76
+ const problems = [];
77
+ const blocks = [];
78
+ let used = 0;
79
+ for (const file of ["AGENTS.md", "SYSTEM.md"]) {
80
+ const loaded = await readText(input.port, file, context);
81
+ if (loaded.kind === "absent")
82
+ continue;
83
+ if (loaded.kind === "refused") {
84
+ problems.push(`${file} ${loaded.reason}`);
85
+ continue;
86
+ }
87
+ const text = loaded.text.replace(/^\uFEFF/u, "").trim();
88
+ if (text === "")
89
+ continue;
90
+ if (used + bytesOf(text) > homeInstructionsLimit) {
91
+ problems.push(`${file} is ${String(bytesOf(loaded.text))} bytes; the limit is ${String(homeInstructionsLimit)} for Home's instruction files; ask your owner to shorten it`);
92
+ continue;
93
+ }
94
+ used += bytesOf(text);
95
+ blocks.push(`## ${file}\n${text}`);
96
+ }
97
+ if (blocks.length === 0 && problems.length === 0)
98
+ return null;
99
+ const shown = problems.slice(0, maximumListedProblems);
100
+ if (problems.length > shown.length)
101
+ shown.push(`${String(problems.length - shown.length)} more problems`);
102
+ return chunks([
103
+ framing,
104
+ ...blocks,
105
+ ...(shown.length === 0
106
+ ? []
107
+ : [`## Problems\n${shown.map((line) => `- ${line}`).join("\n")}`]),
108
+ ].join("\n\n"), maximumSectionBytes);
109
+ }
@@ -32,6 +32,17 @@ The Agent's recorded choice survives the chosen attachment being detached.
32
32
 
33
33
  ---
34
34
 
35
+ ### homeInstructionsLimit
36
+
37
+ ```ts
38
+ const homeInstructionsLimit: 32000 = 32_000;
39
+ ```
40
+
41
+ Bytes of AGENTS.md and SYSTEM.md Home loads into one run, and so the most an
42
+ owner may save. Local enforced the same bound for its root AGENTS.md.
43
+
44
+ ---
45
+
35
46
  ### homeContract
36
47
 
37
48
  ```ts
@@ -34,6 +34,47 @@ Hosts admit Home explicitly; no other extension receives the Files slot.
34
34
 
35
35
  ## Functions
36
36
 
37
+ ### resolveHome()
38
+
39
+ ```ts
40
+ function resolveHome(
41
+ recorded,
42
+ computers,
43
+ ): {
44
+ attachmentId: string | null;
45
+ } | null;
46
+ ```
47
+
48
+ The Home choice a run uses: the recorded one, otherwise the only attachment.
49
+ Hosts that answer "which Home" outside a run apply this same rule.
50
+
51
+ #### Parameters
52
+
53
+ | Parameter | Type |
54
+ | ----------- | -------------------------------------------------------- |
55
+ | `recorded` | \| \{ `attachmentId`: `string` \| `null`; \} \| `null` |
56
+ | `computers` | readonly `Readonly`\<\{ `attachmentId`: `string`; \}\>[] |
57
+
58
+ #### Returns
59
+
60
+ ##### Type Literal
61
+
62
+ ```ts
63
+ {
64
+ attachmentId: string | null;
65
+ }
66
+ ```
67
+
68
+ | Name | Type | Description |
69
+ | -------------- | ------------------ | ------------------------------------------------------------- |
70
+ | `attachmentId` | `string` \| `null` | Chosen Computer attachment, or null when no Home is selected. |
71
+
72
+ ---
73
+
74
+ `null`
75
+
76
+ ---
77
+
37
78
  ### homeExtension()
38
79
 
39
80
  ```ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/shiitake-extensions",
3
- "version": "0.8.0",
3
+ "version": "0.9.1",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "private": false,
6
6
  "description": "Botanical's product Shiitake extensions: goal, loop and Almanac on the generic extension surface",
@@ -120,9 +120,9 @@
120
120
  "dependencies": {
121
121
  "@fungi.computer/almanac": "0.2.1",
122
122
  "@fungi.computer/cairn": "0.1.0",
123
- "@fungi.computer/knapsack": "0.4.11",
124
- "@fungi.computer/mycelium": "0.3.10",
125
- "@fungi.computer/shiitake": "0.15.0",
123
+ "@fungi.computer/knapsack": "0.4.12",
124
+ "@fungi.computer/mycelium": "0.3.11",
125
+ "@fungi.computer/shiitake": "0.16.1",
126
126
  "@fungi.computer/watchdog": "0.1.3",
127
127
  "@fungi.computer/whistle": "0.3.0",
128
128
  "effect": "4.0.2",