@fungi.computer/inkcap 0.11.7 → 0.12.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,7 @@ import {
26
26
  listOutputSchema,
27
27
  readBytesInputSchema,
28
28
  writeOutputSchema,
29
+ mkdirOutputSchema,
29
30
  } from "@fungi.computer/computer-contract";
30
31
  export type InkcapBackendContext = {
31
32
  activity: DocumentActivityStore;
@@ -36,6 +37,7 @@ export type InkcapBackendContext = {
36
37
  list(input: z.output<typeof listInput>): Promise<WhistleJsonValue>;
37
38
  read(input: z.output<typeof readInput>): Promise<WhistleJsonValue>;
38
39
  write(input: z.output<typeof writeInput>): Promise<WhistleJsonValue>;
40
+ mkdir(input: { path: string }): Promise<WhistleJsonValue>;
39
41
  };
40
42
  };
41
43
  };
@@ -111,17 +113,20 @@ const refusal = z.union([
111
113
  const listReply = z.union([listOutputSchema, refusal]);
112
114
  const readReply = z.union([readOutput, refusal]);
113
115
  const writeReply = z.union([writeOutputSchema, refusal]);
116
+ const mkdirReply = z.union([mkdirOutputSchema, refusal]);
114
117
  // The host always supplies callable bindings; admission is checked on invocation.
115
118
  const bindingNotGranted = z.object({ code: z.literal("binding_not_granted") });
116
119
  // The Computer's file system names an absent file or parent folder ENOENT. It is
117
120
  // a domain answer, so it must reach the Agent as a refusal; thrown, the sandbox
118
121
  // reports only "failed for an unrecognized reason".
119
122
  const pathAbsent = z.object({ code: z.literal("ENOENT") });
123
+ function parentFolder(path: string) {
124
+ return path.slice(0, path.lastIndexOf("/")) || "/";
125
+ }
120
126
  function parentNotFound(path: string) {
121
- const folder = path.slice(0, path.lastIndexOf("/")) || "/";
122
127
  return {
123
128
  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.`,
129
+ 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
130
  };
126
131
  }
127
132
  function fileGone(path: string) {
@@ -226,7 +231,7 @@ export const appBackend = defineAppBackend({
226
231
  }),
227
232
  writeFile: defineAppMethod({
228
233
  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.",
234
+ "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
235
  effect: "write",
231
236
  requires: ["computer.writeFile"],
232
237
  input: writeInput,
@@ -263,11 +268,31 @@ export const appBackend = defineAppBackend({
263
268
  input.expectedContent === undefined
264
269
  ? parentNotFound(input.path)
265
270
  : fileGone(input.path);
266
- const reply = writeReply.parse(
267
- await computerCall(() => context.env.COMPUTER.write(input), absent),
271
+ const attempt = async () => {
272
+ const reply = writeReply.parse(
273
+ await computerCall(() => context.env.COMPUTER.write(input), absent),
274
+ );
275
+ // Hosts answer a missing path as plain not_found.
276
+ return "error" in reply && reply.error === "not_found"
277
+ ? absent
278
+ : reply;
279
+ };
280
+ const reply = await attempt();
281
+ if (
282
+ input.expectedContent !== undefined ||
283
+ !("error" in reply) ||
284
+ reply.error !== "parent_not_found"
285
+ )
286
+ return reply;
287
+ // A new file's missing folder is created, then the write is retried once.
288
+ const made = mkdirReply.parse(
289
+ await computerCall(
290
+ () =>
291
+ context.env.COMPUTER.mkdir({ path: parentFolder(input.path) }),
292
+ absent,
293
+ ),
268
294
  );
269
- // Hosts answer a missing path as plain not_found.
270
- return "error" in reply && reply.error === "not_found" ? absent : reply;
295
+ return "error" in made ? absent : await attempt();
271
296
  },
272
297
  }),
273
298
  },
@@ -27,7 +27,12 @@ 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
+ },
31
36
  },
32
37
  };
33
38
  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,12 @@ 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
+ },
41
46
  };
42
47
  const emptyMarker = () =>
43
48
  Promise.resolve({
@@ -189,7 +194,12 @@ it.each([
189
194
  documentsChanged,
190
195
  env: {
191
196
  ...unrelated,
192
- COMPUTER: { list: invoke, read: invoke, write: invoke },
197
+ COMPUTER: {
198
+ list: invoke,
199
+ read: invoke,
200
+ write: invoke,
201
+ mkdir: invoke,
202
+ },
193
203
  },
194
204
  },
195
205
  caller,
@@ -263,7 +273,12 @@ it.each([
263
273
  documentsChanged,
264
274
  env: {
265
275
  ...unrelated,
266
- COMPUTER: { list: denied, read: denied, write: denied },
276
+ COMPUTER: {
277
+ list: denied,
278
+ read: denied,
279
+ write: denied,
280
+ mkdir: denied,
281
+ },
267
282
  },
268
283
  },
269
284
  caller,
@@ -302,7 +317,12 @@ it.each([
302
317
  documentsChanged,
303
318
  env: {
304
319
  ...unrelated,
305
- COMPUTER: { list: failed, read: failed, write: failed },
320
+ COMPUTER: {
321
+ list: failed,
322
+ read: failed,
323
+ write: failed,
324
+ mkdir: failed,
325
+ },
306
326
  },
307
327
  },
308
328
  caller,
@@ -533,7 +553,12 @@ it("refuses a missing folder or file as not_found instead of throwing an opaque
533
553
  documentsChanged,
534
554
  env: {
535
555
  ...unrelated,
536
- COMPUTER: { ...unrelated.COMPUTER, read: absent, write: absent },
556
+ COMPUTER: {
557
+ ...unrelated.COMPUTER,
558
+ read: absent,
559
+ write: absent,
560
+ mkdir: absent,
561
+ },
537
562
  },
538
563
  };
539
564
  const refused = await appBackend.call(
@@ -544,7 +569,7 @@ it("refuses a missing folder or file as not_found instead of throwing an opaque
544
569
  );
545
570
  expect(refused).toMatchObject({ error: "parent_not_found" });
546
571
  expect(JSON.stringify(refused)).toContain(
547
- "The folder /home/a does not exist; Inkcap can't create folders yet.",
572
+ "The folder /home/a does not exist and could not be created.",
548
573
  );
549
574
  // Hosts that answer in-band (not by throwing) get the same refusal.
550
575
  const inBand = await appBackend.call(
@@ -557,6 +582,7 @@ it("refuses a missing folder or file as not_found instead of throwing an opaque
557
582
  COMPUTER: {
558
583
  ...unrelated.COMPUTER,
559
584
  write: () => Promise.resolve({ error: "not_found" }),
585
+ mkdir: () => Promise.resolve({ error: "not_found" }),
560
586
  },
561
587
  },
562
588
  },
@@ -603,3 +629,67 @@ it("refuses a missing folder or file as not_found instead of throwing an opaque
603
629
  ),
604
630
  ).rejects.toThrow("disk on fire");
605
631
  });
632
+
633
+ it("creates a new file's missing folder, then writes into it once", async () => {
634
+ const calls: string[] = [];
635
+ let made = false;
636
+ const result = await appBackend.call(
637
+ "writeFile",
638
+ { path: "/notes/plan.md", content: "hi" },
639
+ {
640
+ activity,
641
+ documentsChanged,
642
+ env: {
643
+ ...unrelated,
644
+ COMPUTER: {
645
+ ...unrelated.COMPUTER,
646
+ write: (input) => {
647
+ calls.push(`write ${input.path}`);
648
+ return made
649
+ ? Promise.resolve({ path: input.path, bytes: 2 })
650
+ : Promise.reject(
651
+ Object.assign(new Error("parent missing"), {
652
+ code: "ENOENT",
653
+ }),
654
+ );
655
+ },
656
+ mkdir: (input) => {
657
+ calls.push(`mkdir ${input.path}`);
658
+ made = true;
659
+ return Promise.resolve({ path: input.path });
660
+ },
661
+ },
662
+ },
663
+ },
664
+ caller,
665
+ );
666
+ expect(result).toEqual({ path: "/notes/plan.md", bytes: 2 });
667
+ expect(calls).toEqual([
668
+ "write /notes/plan.md",
669
+ "mkdir /notes",
670
+ "write /notes/plan.md",
671
+ ]);
672
+ });
673
+
674
+ it("does not create folders for an edit of an existing file", async () => {
675
+ const mkdir = vi.fn(() => Promise.resolve({ path: "/x" }));
676
+ const gone = await appBackend.call(
677
+ "writeFile",
678
+ { path: "/x/y.md", content: "new", expectedContent: "old" },
679
+ {
680
+ activity,
681
+ documentsChanged,
682
+ env: {
683
+ ...unrelated,
684
+ COMPUTER: {
685
+ ...unrelated.COMPUTER,
686
+ write: () => Promise.resolve({ error: "not_found" }),
687
+ mkdir,
688
+ },
689
+ },
690
+ },
691
+ caller,
692
+ );
693
+ expect(gone).toMatchObject({ error: "not_found" });
694
+ expect(mkdir).not.toHaveBeenCalled();
695
+ });