@fungi.computer/inkcap 0.11.7 → 0.13.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.
@@ -26,6 +26,9 @@ import {
26
26
  listOutputSchema,
27
27
  readBytesInputSchema,
28
28
  writeOutputSchema,
29
+ mkdirOutputSchema,
30
+ gitChangesInputSchema,
31
+ gitChangesOutputSchema,
29
32
  } from "@fungi.computer/computer-contract";
30
33
  export type InkcapBackendContext = {
31
34
  activity: DocumentActivityStore;
@@ -36,6 +39,10 @@ export type InkcapBackendContext = {
36
39
  list(input: z.output<typeof listInput>): Promise<WhistleJsonValue>;
37
40
  read(input: z.output<typeof readInput>): Promise<WhistleJsonValue>;
38
41
  write(input: z.output<typeof writeInput>): Promise<WhistleJsonValue>;
42
+ mkdir(input: { path: string }): Promise<WhistleJsonValue>;
43
+ gitChanges(
44
+ input: z.output<typeof gitChangesInput>,
45
+ ): Promise<WhistleJsonValue>;
39
46
  };
40
47
  };
41
48
  };
@@ -59,6 +66,9 @@ const readInput = readBytesInputSchema.extend({
59
66
  .max(128 * 1024)
60
67
  .default(128 * 1024),
61
68
  });
69
+ const gitChangesInput = gitChangesInputSchema.extend({
70
+ path: computerPath.default("/"),
71
+ });
62
72
  const text = z.string().max(48 * 1024);
63
73
  const writeInput = z
64
74
  .strictObject({
@@ -111,17 +121,21 @@ const refusal = z.union([
111
121
  const listReply = z.union([listOutputSchema, refusal]);
112
122
  const readReply = z.union([readOutput, refusal]);
113
123
  const writeReply = z.union([writeOutputSchema, refusal]);
124
+ const mkdirReply = z.union([mkdirOutputSchema, refusal]);
125
+ const gitChangesReply = z.union([gitChangesOutputSchema, refusal]);
114
126
  // The host always supplies callable bindings; admission is checked on invocation.
115
127
  const bindingNotGranted = z.object({ code: z.literal("binding_not_granted") });
116
128
  // The Computer's file system names an absent file or parent folder ENOENT. It is
117
129
  // a domain answer, so it must reach the Agent as a refusal; thrown, the sandbox
118
130
  // reports only "failed for an unrecognized reason".
119
131
  const pathAbsent = z.object({ code: z.literal("ENOENT") });
132
+ function parentFolder(path: string) {
133
+ return path.slice(0, path.lastIndexOf("/")) || "/";
134
+ }
120
135
  function parentNotFound(path: string) {
121
- const folder = path.slice(0, path.lastIndexOf("/")) || "/";
122
136
  return {
123
137
  error: "parent_not_found" as const,
124
- message: `The folder ${folder} does not exist; Inkcap can't create folders yet. Save into an existing folder (for example /notes.md at the top level), or list folders with listFiles.`,
138
+ message: `The folder ${parentFolder(path)} does not exist and could not be created. Save into an existing folder (for example /notes.md at the top level), or list folders with listFiles.`,
125
139
  };
126
140
  }
127
141
  function fileGone(path: string) {
@@ -210,6 +224,20 @@ export const appBackend = defineAppBackend({
210
224
  await computerCall(() => context.env.COMPUTER.list(input)),
211
225
  ),
212
226
  }),
227
+ gitChanges: defineAppMethod({
228
+ description:
229
+ "Read everything changed in the Git repository holding one folder on the attached Computer, for review before commit or push: changed files and one unified diff against the last commit, untracked files as additions. Read-only; no Computer selector is accepted.",
230
+ effect: "read",
231
+ requires: ["computer.readFile"],
232
+ input: gitChangesInput,
233
+ output: gitChangesReply,
234
+ handler: async (input, context: InkcapBackendContext, caller) =>
235
+ caller.computerId === null
236
+ ? refusal.parse({ error: "not_granted" })
237
+ : gitChangesReply.parse(
238
+ await computerCall(() => context.env.COMPUTER.gitChanges(input)),
239
+ ),
240
+ }),
213
241
  readFile: defineAppMethod({
214
242
  description:
215
243
  "Read up to 128 KiB of a file on the attached Computer as base64. Use byteOffset for the next chunk.",
@@ -226,7 +254,7 @@ export const appBackend = defineAppBackend({
226
254
  }),
227
255
  writeFile: defineAppMethod({
228
256
  description:
229
- "Write UTF-8 text on the attached Computer. The folder must already exist (Inkcap cannot create folders): a missing folder is refused as parent_not_found, and the path is on the Computer, not the host machine. Supply expectedContent for a compare-and-swap; combined content is limited to 48 KiB. The host owns attribution and the attachment write budget.",
257
+ "Write UTF-8 text on the attached Computer. A missing folder is created first; if it cannot be created the write is refused as parent_not_found, and the path is on the Computer, not the host machine. Supply expectedContent for a compare-and-swap; combined content is limited to 48 KiB. The host owns attribution and the attachment write budget.",
230
258
  effect: "write",
231
259
  requires: ["computer.writeFile"],
232
260
  input: writeInput,
@@ -263,11 +291,31 @@ export const appBackend = defineAppBackend({
263
291
  input.expectedContent === undefined
264
292
  ? parentNotFound(input.path)
265
293
  : fileGone(input.path);
266
- const reply = writeReply.parse(
267
- await computerCall(() => context.env.COMPUTER.write(input), absent),
294
+ const attempt = async () => {
295
+ const reply = writeReply.parse(
296
+ await computerCall(() => context.env.COMPUTER.write(input), absent),
297
+ );
298
+ // Hosts answer a missing path as plain not_found.
299
+ return "error" in reply && reply.error === "not_found"
300
+ ? absent
301
+ : reply;
302
+ };
303
+ const reply = await attempt();
304
+ if (
305
+ input.expectedContent !== undefined ||
306
+ !("error" in reply) ||
307
+ reply.error !== "parent_not_found"
308
+ )
309
+ return reply;
310
+ // A new file's missing folder is created, then the write is retried once.
311
+ const made = mkdirReply.parse(
312
+ await computerCall(
313
+ () =>
314
+ context.env.COMPUTER.mkdir({ path: parentFolder(input.path) }),
315
+ absent,
316
+ ),
268
317
  );
269
- // Hosts answer a missing path as plain not_found.
270
- return "error" in reply && reply.error === "not_found" ? absent : reply;
318
+ return "error" in made ? absent : await attempt();
271
319
  },
272
320
  }),
273
321
  },
@@ -27,7 +27,13 @@ it("projects the saved Recent Files source with stable identities and its own cu
27
27
  documentsChanged: () => Promise.resolve(),
28
28
  env: {
29
29
  APP_STORAGE: { read: unavailable, write: unavailable },
30
- COMPUTER: { list: unavailable, read: unavailable, write: unavailable },
30
+ COMPUTER: {
31
+ list: unavailable,
32
+ read: unavailable,
33
+ write: unavailable,
34
+ mkdir: unavailable,
35
+ gitChanges: unavailable,
36
+ },
31
37
  },
32
38
  };
33
39
  const caller = {
@@ -1,6 +1,6 @@
1
1
  import { markDocumentsChanged } from "../documents-changed.js";
2
2
  import { documentsRecentSchema } from "../../src/document-activity-contract.js";
3
- import { afterAll, expect, it } from "vitest";
3
+ import { afterAll, expect, it, vi } from "vitest";
4
4
  import * as z from "zod";
5
5
  import { DatabaseSync } from "node:sqlite";
6
6
  import { createDocumentActivityStore } from "../document-activity.js";
@@ -37,7 +37,13 @@ const unavailable = (): Promise<never> =>
37
37
  Promise.reject(new Error("Computer calls must not access repositories"));
38
38
  const unrelated = {
39
39
  APP_STORAGE: { read: unavailable, write: unavailable },
40
- COMPUTER: { list: unavailable, read: unavailable, write: unavailable },
40
+ COMPUTER: {
41
+ list: unavailable,
42
+ read: unavailable,
43
+ write: unavailable,
44
+ mkdir: unavailable,
45
+ gitChanges: unavailable,
46
+ },
41
47
  };
42
48
  const emptyMarker = () =>
43
49
  Promise.resolve({
@@ -84,6 +90,61 @@ it("lists files only through the admitted pinned Computer binding", async () =>
84
90
  expect(calls).toEqual([{ path: "/", limit: 100, offset: 0 }]);
85
91
  });
86
92
 
93
+ it("reads repository changes only through the admitted pinned Computer binding", async () => {
94
+ const calls: unknown[] = [];
95
+ const reply = {
96
+ kind: "changes" as const,
97
+ root: "/work",
98
+ branch: "main",
99
+ files: [{ path: "a.txt", status: "modified" as const }],
100
+ patch: "diff --git a/a.txt b/a.txt\n",
101
+ truncated: false,
102
+ };
103
+ const context = (gitChanges: ComputerBinding["gitChanges"]) => ({
104
+ activity,
105
+ documentsChanged,
106
+ env: { ...unrelated, COMPUTER: { ...unrelated.COMPUTER, gitChanges } },
107
+ });
108
+ await expect(
109
+ appBackend.call(
110
+ "gitChanges",
111
+ { path: "/work" },
112
+ context((input) => {
113
+ calls.push(input);
114
+ return Promise.resolve(reply);
115
+ }),
116
+ caller,
117
+ ),
118
+ ).resolves.toEqual(reply);
119
+ expect(calls).toEqual([{ path: "/work" }]);
120
+ await expect(
121
+ appBackend.call(
122
+ "gitChanges",
123
+ {},
124
+ context((input) => {
125
+ calls.push(input);
126
+ return Promise.resolve({ kind: "not_repository" });
127
+ }),
128
+ caller,
129
+ ),
130
+ ).resolves.toEqual({ kind: "not_repository" });
131
+ expect(calls[1]).toEqual({ path: "/" });
132
+ await expect(
133
+ appBackend.call(
134
+ "gitChanges",
135
+ { path: "/work/../etc" },
136
+ context(unavailable),
137
+ caller,
138
+ ),
139
+ ).rejects.toThrow();
140
+ await expect(
141
+ appBackend.call("gitChanges", { path: "/work" }, context(unavailable), {
142
+ ...caller,
143
+ computerId: null,
144
+ }),
145
+ ).resolves.toEqual({ error: "not_granted" });
146
+ });
147
+
87
148
  it("does not dispatch when the caller has no pinned Computer", async () => {
88
149
  let calls = 0;
89
150
  const result = await appBackend.call(
@@ -189,7 +250,13 @@ it.each([
189
250
  documentsChanged,
190
251
  env: {
191
252
  ...unrelated,
192
- COMPUTER: { list: invoke, read: invoke, write: invoke },
253
+ COMPUTER: {
254
+ list: invoke,
255
+ read: invoke,
256
+ write: invoke,
257
+ mkdir: invoke,
258
+ gitChanges: invoke,
259
+ },
193
260
  },
194
261
  },
195
262
  caller,
@@ -201,7 +268,12 @@ it.each([
201
268
  it("edits repositories only through Bonsai, never through its own Git methods", () => {
202
269
  const methods = Object.keys(buildAppApi(appBackend).methods);
203
270
  expect(methods).toEqual(
204
- expect.arrayContaining(["listFiles", "readFile", "writeFile"]),
271
+ expect.arrayContaining([
272
+ "listFiles",
273
+ "readFile",
274
+ "writeFile",
275
+ "gitChanges",
276
+ ]),
205
277
  );
206
278
  for (const retired of [
207
279
  "snapshot",
@@ -263,7 +335,13 @@ it.each([
263
335
  documentsChanged,
264
336
  env: {
265
337
  ...unrelated,
266
- COMPUTER: { list: denied, read: denied, write: denied },
338
+ COMPUTER: {
339
+ list: denied,
340
+ read: denied,
341
+ write: denied,
342
+ mkdir: denied,
343
+ gitChanges: denied,
344
+ },
267
345
  },
268
346
  },
269
347
  caller,
@@ -302,7 +380,13 @@ it.each([
302
380
  documentsChanged,
303
381
  env: {
304
382
  ...unrelated,
305
- COMPUTER: { list: failed, read: failed, write: failed },
383
+ COMPUTER: {
384
+ list: failed,
385
+ read: failed,
386
+ write: failed,
387
+ mkdir: failed,
388
+ gitChanges: failed,
389
+ },
306
390
  },
307
391
  },
308
392
  caller,
@@ -533,7 +617,12 @@ it("refuses a missing folder or file as not_found instead of throwing an opaque
533
617
  documentsChanged,
534
618
  env: {
535
619
  ...unrelated,
536
- COMPUTER: { ...unrelated.COMPUTER, read: absent, write: absent },
620
+ COMPUTER: {
621
+ ...unrelated.COMPUTER,
622
+ read: absent,
623
+ write: absent,
624
+ mkdir: absent,
625
+ },
537
626
  },
538
627
  };
539
628
  const refused = await appBackend.call(
@@ -544,7 +633,7 @@ it("refuses a missing folder or file as not_found instead of throwing an opaque
544
633
  );
545
634
  expect(refused).toMatchObject({ error: "parent_not_found" });
546
635
  expect(JSON.stringify(refused)).toContain(
547
- "The folder /home/a does not exist; Inkcap can't create folders yet.",
636
+ "The folder /home/a does not exist and could not be created.",
548
637
  );
549
638
  // Hosts that answer in-band (not by throwing) get the same refusal.
550
639
  const inBand = await appBackend.call(
@@ -557,6 +646,7 @@ it("refuses a missing folder or file as not_found instead of throwing an opaque
557
646
  COMPUTER: {
558
647
  ...unrelated.COMPUTER,
559
648
  write: () => Promise.resolve({ error: "not_found" }),
649
+ mkdir: () => Promise.resolve({ error: "not_found" }),
560
650
  },
561
651
  },
562
652
  },
@@ -603,3 +693,67 @@ it("refuses a missing folder or file as not_found instead of throwing an opaque
603
693
  ),
604
694
  ).rejects.toThrow("disk on fire");
605
695
  });
696
+
697
+ it("creates a new file's missing folder, then writes into it once", async () => {
698
+ const calls: string[] = [];
699
+ let made = false;
700
+ const result = await appBackend.call(
701
+ "writeFile",
702
+ { path: "/notes/plan.md", content: "hi" },
703
+ {
704
+ activity,
705
+ documentsChanged,
706
+ env: {
707
+ ...unrelated,
708
+ COMPUTER: {
709
+ ...unrelated.COMPUTER,
710
+ write: (input) => {
711
+ calls.push(`write ${input.path}`);
712
+ return made
713
+ ? Promise.resolve({ path: input.path, bytes: 2 })
714
+ : Promise.reject(
715
+ Object.assign(new Error("parent missing"), {
716
+ code: "ENOENT",
717
+ }),
718
+ );
719
+ },
720
+ mkdir: (input) => {
721
+ calls.push(`mkdir ${input.path}`);
722
+ made = true;
723
+ return Promise.resolve({ path: input.path });
724
+ },
725
+ },
726
+ },
727
+ },
728
+ caller,
729
+ );
730
+ expect(result).toEqual({ path: "/notes/plan.md", bytes: 2 });
731
+ expect(calls).toEqual([
732
+ "write /notes/plan.md",
733
+ "mkdir /notes",
734
+ "write /notes/plan.md",
735
+ ]);
736
+ });
737
+
738
+ it("does not create folders for an edit of an existing file", async () => {
739
+ const mkdir = vi.fn(() => Promise.resolve({ path: "/x" }));
740
+ const gone = await appBackend.call(
741
+ "writeFile",
742
+ { path: "/x/y.md", content: "new", expectedContent: "old" },
743
+ {
744
+ activity,
745
+ documentsChanged,
746
+ env: {
747
+ ...unrelated,
748
+ COMPUTER: {
749
+ ...unrelated.COMPUTER,
750
+ write: () => Promise.resolve({ error: "not_found" }),
751
+ mkdir,
752
+ },
753
+ },
754
+ },
755
+ caller,
756
+ );
757
+ expect(gone).toMatchObject({ error: "not_found" });
758
+ expect(mkdir).not.toHaveBeenCalled();
759
+ });