@fungi.computer/computer 0.7.0 → 0.8.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.
@@ -22,6 +22,7 @@ export declare const computerActionSchema: z.ZodEnum<{
22
22
  processStart: "processStart";
23
23
  processList: "processList";
24
24
  processKill: "processKill";
25
+ processLogs: "processLogs";
25
26
  exposePort: "exposePort";
26
27
  unexposePort: "unexposePort";
27
28
  listPorts: "listPorts";
@@ -57,6 +58,7 @@ export declare const computerGrantSchema: z.ZodObject<{
57
58
  processStart: "processStart";
58
59
  processList: "processList";
59
60
  processKill: "processKill";
61
+ processLogs: "processLogs";
60
62
  exposePort: "exposePort";
61
63
  unexposePort: "unexposePort";
62
64
  listPorts: "listPorts";
@@ -10,6 +10,7 @@ export const computerActionSchema = z.enum([
10
10
  "processStart",
11
11
  "processList",
12
12
  "processKill",
13
+ "processLogs",
13
14
  "exposePort",
14
15
  "unexposePort",
15
16
  "listPorts",
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@ import { type IWorkspaceContainerAPI } from "@fungi.computer/cloudflare-computer
6
6
  import { DurableObject } from "cloudflare:workers";
7
7
  import type { FilesPort } from "@fungi.computer/knapsack";
8
8
  import { z } from "zod";
9
- import { type ComputerExecutionBudget, type ComputerExecutionHostHooks, type ComputerExecRequest, type ComputerOperationResult, type ComputerFileReadInput, type ComputerFileByteReadInput, type ComputerFileReplacement, type ComputerFileReplacementResult, type ComputerValue, writeInputSchema, writeOutputSchema, type ComputerProcess } from "@fungi.computer/computer-contract";
9
+ import { type ComputerExecutionBudget, type ComputerExecutionHostHooks, type ComputerExecRequest, type ComputerOperationResult, type ComputerFileReadInput, type ComputerFileByteReadInput, type ComputerFileReplacement, type ComputerFileReplacementResult, type ComputerValue, writeInputSchema, writeOutputSchema, processLogsOutputSchema, type ComputerProcess } from "@fungi.computer/computer-contract";
10
10
  import { createWorkspaceFilesPort } from "./files.js";
11
11
  import { OnBoxVmContainerHost, type OnBoxVmEnv, type OnBoxVmPlacement } from "./on-box-vm.js";
12
12
  import { type AttachedBackend, type AttachedBackendView, type BackendNeed, type BackendRoute, type ContainerRuntimeStart, type HostedBacking } from "./computer-backends.js";
@@ -275,6 +275,9 @@ export declare class ComputerDO extends ContainerComputer implements ComputerFil
275
275
  protected listProcessesAfterAcceptance(accept: () => void): Promise<{
276
276
  processes: ComputerProcess[];
277
277
  }>;
278
+ /** The retained last output lines of one process, with its record. */
279
+ processLogs(input: ComputerValue): z.output<typeof processLogsOutputSchema>;
280
+ protected processLogsAfterAcceptance(input: ComputerValue, accept: () => void): z.output<typeof processLogsOutputSchema>;
278
281
  /** Send SIGTERM to one running process; its exit is recorded when observed. */
279
282
  killProcess(input: ComputerValue): Promise<{
280
283
  process: ComputerProcess;
package/dist/index.js CHANGED
@@ -67,13 +67,13 @@ import { z } from "zod";
67
67
  import { SystemTrustContainerBackend } from "./container-system-trust.js";
68
68
  import { computerTransportBackend } from "./transport-backend.js";
69
69
  import { PullPendingTracker } from "./pull-pending.js";
70
- import { ComputerFileConflict, ComputerFileRangeError, ComputerFileTooLargeError, capabilityNameSchema, computerValueSchema, computerCapabilities, computerPortInputSchema, computerPortOutputSchema, computerPortRemovalOutputSchema, computerPortsOutputSchema, computerPortSchema, ComputerPortLimitError, ComputerPortReservedError, computerTerminalPort, isComputerReservedPort, maximumExposedPorts, maximumProcessRecords, execInputSchema, execOutputSchema, computerToolResult, computerExecutionRequestSchema, genericComputerExecutionBudget, mintComputerRequestId, listInputSchema, listOutputSchema, maximumFileBytes, mkdirInputSchema, mkdirOutputSchema, readInputSchema, readBytesInputSchema, readOutputSchema, replaceInputSchema, writeInputSchema, writeOutputSchema, processIdInputSchema, processKillOutputSchema, processListOutputSchema, processStartInputSchema, processStartToolInputSchema, ComputerProcessNotFoundError, } from "@fungi.computer/computer-contract";
70
+ import { ComputerFileConflict, ComputerFileRangeError, ComputerFileTooLargeError, capabilityNameSchema, computerValueSchema, computerCapabilities, computerPortInputSchema, computerPortOutputSchema, computerPortRemovalOutputSchema, computerPortsOutputSchema, computerPortSchema, ComputerPortLimitError, ComputerPortReservedError, computerTerminalPort, isComputerReservedPort, maximumExposedPorts, maximumProcessRecords, execInputSchema, execOutputSchema, computerToolResult, computerExecutionRequestSchema, genericComputerExecutionBudget, mintComputerRequestId, listInputSchema, listOutputSchema, maximumFileBytes, mkdirInputSchema, mkdirOutputSchema, readInputSchema, readBytesInputSchema, readOutputSchema, replaceInputSchema, writeInputSchema, writeOutputSchema, processIdInputSchema, processKillOutputSchema, processListOutputSchema, processLogsInputSchema, processLogsOutputSchema, maximumProcessLogLines, processStartInputSchema, processStartToolInputSchema, ComputerProcessNotFoundError, } from "@fungi.computer/computer-contract";
71
71
  import { createWorkspaceFilesPort, readBoundedWorkspaceText, listWorkspaceFiles, } from "./files.js";
72
72
  import { computerExecutionResult } from "./execution-result.js";
73
73
  import { ComputerOperationRecord } from "./operation-record.js";
74
74
  import { createComputerExecutionLifecycle } from "./execution-lifecycle.js";
75
75
  import { applyWorkspacePatch } from "./files-patch.js";
76
- import { ComputerProcessInventory, drainProcessExit, failedObservation, observeProcess, } from "./process-inventory.js";
76
+ import { ComputerProcessInventory, drainProcessExit, ProcessLogTail, failedObservation, observeProcess, } from "./process-inventory.js";
77
77
  import { assertComputerContainersEnabled, ComputerContainersPausedError, configureComputerContainer, hostedComputerContainersPaused, } from "./container-policy.js";
78
78
  import { OnBoxVmContainerHost, onBoxVmPlacement, } from "./on-box-vm.js";
79
79
  import { computerKeepAwake } from "./fleet/keep-awake.js";
@@ -1276,6 +1276,24 @@ export class ComputerDO extends ContainerComputer {
1276
1276
  await this.#renewExecution(row.id);
1277
1277
  return process;
1278
1278
  }
1279
+ /** The retained last output lines of one process, with its record. */
1280
+ processLogs(input) {
1281
+ return this.processLogsAfterAcceptance(input, () => undefined);
1282
+ }
1283
+ processLogsAfterAcceptance(input, accept) {
1284
+ const { id, lines } = processLogsInputSchema.parse(input);
1285
+ accept();
1286
+ const row = this.#processes.get(id);
1287
+ const log = this.#processes.logOf(id);
1288
+ if (row === undefined || log === undefined)
1289
+ throw new ComputerProcessNotFoundError(id);
1290
+ const wanted = lines ?? maximumProcessLogLines;
1291
+ return processLogsOutputSchema.parse({
1292
+ process: this.#processReceipt(row),
1293
+ lines: log.lines.slice(-wanted),
1294
+ truncated: log.truncated || log.lines.length > wanted,
1295
+ });
1296
+ }
1279
1297
  /** Send SIGTERM to one running process; its exit is recorded when observed. */
1280
1298
  async killProcess(input) {
1281
1299
  return await this.killProcessAfterAcceptance(input, () => undefined);
@@ -1406,7 +1424,18 @@ export class ComputerDO extends ContainerComputer {
1406
1424
  */
1407
1425
  async #watchProcess(id, run) {
1408
1426
  try {
1409
- const settled = await drainProcessExit(run);
1427
+ const tail = new ProcessLogTail();
1428
+ let lastSaved = 0;
1429
+ const settled = await drainProcessExit(run, (text) => {
1430
+ tail.push(text);
1431
+ // Output is saved at most once a second while the process runs.
1432
+ const now = Date.now();
1433
+ if (now - lastSaved >= 1_000) {
1434
+ lastSaved = now;
1435
+ this.#processes.setLog(id, tail.snapshot());
1436
+ }
1437
+ });
1438
+ this.#processes.setLog(id, tail.snapshot());
1410
1439
  if (settled !== undefined) {
1411
1440
  this.#settle(id, {
1412
1441
  state: "exited",
@@ -3,6 +3,23 @@ import { z } from "zod";
3
3
  import { processStartInputSchema, type ComputerProcess, type ComputerProcessId, type ComputerProcessSync } from "@fungi.computer/computer-contract";
4
4
  /** The durable SQLite surface owned by processes and their port exposures. */
5
5
  export type ComputerProcessStorage = Pick<DurableObjectStorage, "sql" | "transactionSync">;
6
+ /** The retained last output lines of one process. */
7
+ export type ProcessLogSnapshot = Readonly<{
8
+ lines: readonly string[];
9
+ truncated: boolean;
10
+ }>;
11
+ /**
12
+ * Keeps the last lines a process wrote, bounded by line count, line length
13
+ * and total size. A partial last line is held until its newline arrives or
14
+ * the snapshot is read.
15
+ */
16
+ export declare class ProcessLogTail {
17
+ #private;
18
+ /** Add a chunk of stdout or stderr text. */
19
+ push(chunk: string): void;
20
+ /** The retained lines, including any unfinished last line. */
21
+ snapshot(): ProcessLogSnapshot;
22
+ }
6
23
  /** Computer-owned replay identities; computerd owns execution and exit observations. */
7
24
  export declare class ComputerProcessInventory {
8
25
  #private;
@@ -15,6 +32,10 @@ export declare class ComputerProcessInventory {
15
32
  created: boolean;
16
33
  }>;
17
34
  exited(id: ComputerProcessId, exitCode: number | null, sync?: ComputerProcessSync): ComputerProcess;
35
+ /** Replace the retained output tail of one process, if its record is still kept. */
36
+ setLog(id: ComputerProcessId, tail: ProcessLogSnapshot): void;
37
+ /** The retained output tail of one process, or undefined when no record is kept. */
38
+ logOf(id: ComputerProcessId): ProcessLogSnapshot | undefined;
18
39
  /** Retain a failed dispatch receipt: retrying that key cannot create new work. */
19
40
  release(id: ComputerProcessId): void;
20
41
  /**
@@ -75,7 +96,7 @@ export declare function observeProcess(runtime: ProcessRuntime, id: string): Pro
75
96
  * Workspace. `undefined` if the stream broke or was replaced before any
76
97
  * exit was observed -- there is nothing to record.
77
98
  */
78
- export declare function drainProcessExit(run: WorkspaceRuntimeExecHandle<"utf8">): Promise<Readonly<{
99
+ export declare function drainProcessExit(run: WorkspaceRuntimeExecHandle<"utf8">, onOutput?: (text: string) => void): Promise<Readonly<{
79
100
  exitCode: number;
80
101
  sync: ComputerProcessSync;
81
102
  settleLater?: Promise<ComputerProcessSync>;
@@ -51,7 +51,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
51
51
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
52
  });
53
53
  import { z } from "zod";
54
- import { computerProcessSchema, processStartInputSchema, mintComputerProcessId, ComputerProcessLimitError, ComputerPortReservedError, ComputerPortConflictError, ComputerPortLimitError, isComputerReservedPort, maximumExposedPorts, maximumProcessRecords, maximumRunningProcesses, } from "@fungi.computer/computer-contract";
54
+ import { computerProcessSchema, processStartInputSchema, mintComputerProcessId, ComputerProcessLimitError, ComputerPortReservedError, ComputerPortConflictError, ComputerPortLimitError, isComputerReservedPort, maximumExposedPorts, maximumProcessLogLineCharacters, maximumProcessLogLines, maximumProcessRecords, maximumRunningProcesses, } from "@fungi.computer/computer-contract";
55
55
  import { ComputerOperationConflictError } from "./operation-record.js";
56
56
  function toProcess(row) {
57
57
  return computerProcessSchema.parse({
@@ -75,7 +75,52 @@ function toProcess(row) {
75
75
  * storage (confirmed in workerd; node:sqlite does not enforce the same
76
76
  * sandbox, so a unit test against it alone would hide this).
77
77
  */
78
- const processSchemaVersion = 2;
78
+ const processSchemaVersion = 3;
79
+ /** At most this many bytes of output text are retained per process. */
80
+ const maximumLogBytes = 64 * 1024;
81
+ /**
82
+ * Keeps the last lines a process wrote, bounded by line count, line length
83
+ * and total size. A partial last line is held until its newline arrives or
84
+ * the snapshot is read.
85
+ */
86
+ export class ProcessLogTail {
87
+ #lines = [];
88
+ #partial = "";
89
+ #truncated = false;
90
+ /** Add a chunk of stdout or stderr text. */
91
+ push(chunk) {
92
+ const parts = (this.#partial + chunk).split("\n");
93
+ this.#partial = parts.pop() ?? "";
94
+ for (const part of parts)
95
+ this.#lines.push(clipLine(part));
96
+ this.#partial = clipLine(this.#partial);
97
+ this.#trim();
98
+ }
99
+ /** The retained lines, including any unfinished last line. */
100
+ snapshot() {
101
+ const lines = this.#partial === "" ? this.#lines : [...this.#lines, this.#partial];
102
+ return {
103
+ lines: lines.slice(-maximumProcessLogLines),
104
+ truncated: this.#truncated,
105
+ };
106
+ }
107
+ #trim() {
108
+ let bytes = 0;
109
+ for (const line of this.#lines)
110
+ bytes += line.length + 1;
111
+ while (this.#lines.length > maximumProcessLogLines ||
112
+ (bytes > maximumLogBytes && this.#lines.length > 1)) {
113
+ const dropped = this.#lines.shift();
114
+ bytes -= (dropped?.length ?? 0) + 1;
115
+ this.#truncated = true;
116
+ }
117
+ }
118
+ }
119
+ function clipLine(line) {
120
+ return line.length > maximumProcessLogLineCharacters
121
+ ? line.slice(0, maximumProcessLogLineCharacters)
122
+ : line;
123
+ }
79
124
  /** Computer-owned replay identities; computerd owns execution and exit observations. */
80
125
  export class ComputerProcessInventory {
81
126
  #storage;
@@ -94,7 +139,7 @@ export class ComputerProcessInventory {
94
139
  storage.sql.exec("DELETE FROM computer_schema_meta WHERE id = 0");
95
140
  storage.sql.exec("INSERT INTO computer_schema_meta (id, version) VALUES (0, ?)", processSchemaVersion);
96
141
  }
97
- storage.sql.exec("CREATE TABLE IF NOT EXISTS computer_processes (id TEXT PRIMARY KEY, request_scope TEXT NOT NULL, request_id TEXT NOT NULL, input_hash TEXT NOT NULL, command TEXT NOT NULL, cwd TEXT, port INTEGER, state TEXT NOT NULL CHECK (state IN ('running','exited')), exit_code INTEGER, sync TEXT NOT NULL DEFAULT 'unknown' CHECK (sync IN ('synced','pending','unknown')), started_at INTEGER NOT NULL, ended_at INTEGER, CHECK ((state = 'running') = (ended_at IS NULL)), UNIQUE(request_scope, request_id)) STRICT");
142
+ storage.sql.exec("CREATE TABLE IF NOT EXISTS computer_processes (id TEXT PRIMARY KEY, request_scope TEXT NOT NULL, request_id TEXT NOT NULL, input_hash TEXT NOT NULL, command TEXT NOT NULL, cwd TEXT, port INTEGER, state TEXT NOT NULL CHECK (state IN ('running','exited')), exit_code INTEGER, sync TEXT NOT NULL DEFAULT 'unknown' CHECK (sync IN ('synced','pending','unknown')), started_at INTEGER NOT NULL, ended_at INTEGER, log_tail TEXT NOT NULL DEFAULT '', log_truncated INTEGER NOT NULL DEFAULT 0, CHECK ((state = 'running') = (ended_at IS NULL)), UNIQUE(request_scope, request_id)) STRICT");
98
143
  storage.sql.exec("CREATE TABLE IF NOT EXISTS computer_exposed_ports (port INTEGER PRIMARY KEY CHECK (port BETWEEN 1 AND 65535), process_id TEXT) STRICT");
99
144
  });
100
145
  }
@@ -163,6 +208,22 @@ export class ComputerProcessInventory {
163
208
  return this.#require(id);
164
209
  });
165
210
  }
211
+ /** Replace the retained output tail of one process, if its record is still kept. */
212
+ setLog(id, tail) {
213
+ this.#storage.sql.exec("UPDATE computer_processes SET log_tail = ?, log_truncated = ? WHERE id = ?", tail.lines.join("\n"), tail.truncated ? 1 : 0, id);
214
+ }
215
+ /** The retained output tail of one process, or undefined when no record is kept. */
216
+ logOf(id) {
217
+ const row = this.#storage.sql
218
+ .exec("SELECT * FROM computer_processes WHERE id = ?", id)
219
+ .toArray()[0];
220
+ if (row === undefined)
221
+ return undefined;
222
+ return {
223
+ lines: row.log_tail === "" ? [] : row.log_tail.split("\n"),
224
+ truncated: row.log_truncated === 1,
225
+ };
226
+ }
166
227
  /** Retain a failed dispatch receipt: retrying that key cannot create new work. */
167
228
  release(id) {
168
229
  const row = this.get(id);
@@ -392,7 +453,7 @@ export async function observeProcess(runtime, id) {
392
453
  * Workspace. `undefined` if the stream broke or was replaced before any
393
454
  * exit was observed -- there is nothing to record.
394
455
  */
395
- export async function drainProcessExit(run) {
456
+ export async function drainProcessExit(run, onOutput) {
396
457
  const reader = run.getReader();
397
458
  const dispose = () => {
398
459
  reader.releaseLock();
@@ -406,6 +467,8 @@ export async function drainProcessExit(run) {
406
467
  const next = await reader.read();
407
468
  if (next.done)
408
469
  break;
470
+ if (next.value.name === "stdout" || next.value.name === "stderr")
471
+ onOutput?.(next.value.value);
409
472
  if (next.value.name === "exit") {
410
473
  exitCode = next.value.code;
411
474
  const drained = await drainPastExit(reader);
@@ -19898,6 +19898,10 @@ var gillResponseSchema = Union2([
19898
19898
  title: rowLabelSchemaV1,
19899
19899
  state: Literal2("empty"),
19900
19900
  rows: Tuple([]),
19901
+ /** One line naming the first action, in the App's own words. */
19902
+ emptyText: optional2(String4.pipe(check(isMinLength(1)), check(isMaxLength(120)))),
19903
+ /** The host supplies the button; the App only says it belongs. */
19904
+ action: optional2(Struct({ kind: Literal2("open") })),
19901
19905
  next: optional2(String4.pipe(check(isMaxLength(1024))))
19902
19906
  }),
19903
19907
  Struct({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/computer",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Cloudflare Computer host for one durable Fungi Computer",
6
6
  "type": "module",
@@ -59,11 +59,11 @@
59
59
  "@fungi.computer/cairn": "0.1.0",
60
60
  "@fungi.computer/capnweb": "0.12.0-fungi.0",
61
61
  "@fungi.computer/cloudflare-computer": "0.2.1-fungi.2",
62
- "@fungi.computer/cordyceps": "0.4.1",
63
- "@fungi.computer/knapsack": "0.4.8",
62
+ "@fungi.computer/cordyceps": "0.4.2",
63
+ "@fungi.computer/knapsack": "0.4.9",
64
64
  "@fungi.computer/seam": "0.2.5",
65
65
  "@fungi.computer/watchdog": "0.1.3",
66
- "@fungi.computer/computer-contract": "0.0.13",
66
+ "@fungi.computer/computer-contract": "0.1.0",
67
67
  "effect": "4.0.2",
68
68
  "just-bash": "3.4.2",
69
69
  "zod": "4.4.3"