diff --git a/CHANGELOG.md b/CHANGELOG.md index 85ac511f..51dc76d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,13 @@ This file is the canonical source for unreleased changes and published release n - `Runner.NoModelError`, reported as `no-model-error`: a run that has no model, because settings name none and no provider is configured. The CLI prints it with a hint. - Queued steers and follow-ups stack above the desktop composer as cards: the newest in front, up to three edges behind, opening into one list on hover or focus without moving the conversation. Parked prompts show a pulsing dot, the lane (Steer or Follow Up) trails each row, and a row is edited in place (Enter saves, Esc cancels). - A notice about the session (No Model, Something Went Wrong, Not Sent, Not Forked, a broken settings file) is the front card of that stack, tinted by its tone, with queued prompts behind it. +- Space daemons: builtin `git` and `skills` daemons keep each space's state current from inside CodeWork. A space's daemons start with the first run there and stop after an hour without one; they read a sandbox only while a run, or a short handover after it, already holds it, so they never wake one. +- `plugin.git.status` events: a space's branch, head and changed files with line counts, a wholly untracked folder as one entry. `plugin.skills.list` events: the skills sessions in a space see. Both carry `spaceId` and `observedAt`. +- `daemon.call({ plugin, spaceId, method, input })`. The git daemon's `diff` returns per-file patches; while the space's sandbox is not running it answers from the last read, marked `cached`. +- `event.subscribe` takes `types`: only those types, and first the last event of each type per space. +- `space.mount.attached` and `space.mount.detached` events: a run's drain mounting and releasing its space. +- `@codeworksh/plugin/plugin/daemon`: the daemon plugin contract (space scope). +- The desktop's project settings list each space's Skills as its skills daemon last published them, kept in `app.db`: they update as runs change them, a stopped sandbox shows its last list and its age, and a space never read says "not synced yet". ### Changed diff --git a/apps/contract/src/contract.ts b/apps/contract/src/contract.ts index 1adf9c42..6af025d5 100644 --- a/apps/contract/src/contract.ts +++ b/apps/contract/src/contract.ts @@ -380,6 +380,8 @@ export const syncTopics = { delta: (threadId: string) => `sync/delta/${threadId}`, /** `SyncTracking` whenever the user edits a desktop column other than status; stored, in the transaction that wrote it. */ tracking: "sync/tracking", + /** `SyncState` whenever a space daemon's mirrored state changed (git status, skills); stored. */ + state: "sync/state", }; /** @@ -403,6 +405,14 @@ export const SyncChanged = Schema.Struct({ }); export type SyncChanged = typeof SyncChanged.Type; +/** A space daemon's last event of `type` for a space was mirrored anew. */ +export const SyncState = Schema.Struct({ + machineId: Schema.String, + spaceId: Schema.String, + type: Schema.String, +}); +export type SyncState = typeof SyncState.Type; + /** A thread's status moved; `at` is the app server's clock. */ export const SyncStatus = Schema.Struct({ threadId: Schema.String, diff --git a/apps/server/src/db.ts b/apps/server/src/db.ts index 474708d3..30b595f9 100644 --- a/apps/server/src/db.ts +++ b/apps/server/src/db.ts @@ -134,14 +134,16 @@ const migrations = { ) `; yield* sql`CREATE INDEX space_project_idx ON space (machine_id, project_id)`; - // The last skills read from each space: a stopped sandbox is shown from here, never woken. + // What each space's daemons last published (git status, skills), mirrored from CodeWork's event + // stream: a stopped sandbox is shown from here, never woken. yield* sql` - CREATE TABLE skill ( + CREATE TABLE space_state ( machine_id TEXT NOT NULL, space_id TEXT NOT NULL, - skills TEXT NOT NULL, - synced_at INTEGER NOT NULL, - PRIMARY KEY (machine_id, space_id), + type TEXT NOT NULL, + data TEXT NOT NULL, + observed_at INTEGER NOT NULL, + PRIMARY KEY (machine_id, space_id, type), FOREIGN KEY (machine_id, space_id) REFERENCES space(machine_id, id) ON DELETE CASCADE ) `; diff --git a/apps/server/src/handlers.ts b/apps/server/src/handlers.ts index f7b06604..644b5560 100644 --- a/apps/server/src/handlers.ts +++ b/apps/server/src/handlers.ts @@ -97,7 +97,8 @@ export const layer = Contract.Api.toLayer( "sidebar.list": (input) => Reads.sidebar(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)), "projects.spaces": (input) => Reads.spaces(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)), - "projects.skills": (input) => Skills.list(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)), + "projects.skills": (input) => + Skills.list(input, sync.client(input.machineId)).pipe(Effect.provideService(SqlClient.SqlClient, sql)), "threads.list": (input) => Reads.threads(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)), "threads.line": (input) => Reads.line(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)), diff --git a/apps/server/src/skills.ts b/apps/server/src/skills.ts index c99f50db..569ad10e 100644 --- a/apps/server/src/skills.ts +++ b/apps/server/src/skills.ts @@ -1,56 +1,55 @@ -import { SpaceSkill, type SpaceSkills } from "@codeworksh/contract"; +import { type CodeworkUnavailable, SpaceSkill, type SpaceSkills } from "@codeworksh/contract"; import { Effect, Option, Schema } from "effect"; import { SqlClient } from "effect/sql"; -import { Codework } from "./codework.ts"; +import type { Codework } from "@codeworksh/widget/backend"; import { Reads } from "./reads.ts"; -const Stored = Schema.fromJsonString(Schema.Array(SpaceSkill)); -const encode = Schema.encodeSync(Stored); +const Stored = Schema.fromJsonString(Schema.Struct({ skills: Schema.Array(SpaceSkill) })); const decode = Schema.decodeUnknownOption(Stored); /** - * Each active space's skills, read live where that costs nothing: the host every time, a sandbox - * only while CodeWork reports it running. Every read is kept, so a stopped sandbox, or a server that - * is down, shows the last one with when it was taken. + * Each active space's skills as its skills daemon last published them, mirrored from CodeWork's + * event stream: nothing here reads a space. `syncedAt` is when the daemon read them, so a stopped + * sandbox shows its last list and how old it is. */ -export const list = Effect.fn("Skills.list")(function* (input: { - readonly machineId: string; - readonly projectId: string; -}) { +export const list = Effect.fn("Skills.list")(function* ( + input: { readonly machineId: string; readonly projectId: string }, + /** The machine's own connection: its sandboxes are not the default machine's. */ + machine: Effect.Effect, +) { const sql = yield* SqlClient.SqlClient; const spaces = (yield* Reads.spaces(input)).filter((space) => space.status === "active"); - return yield* Effect.forEach( - spaces, - Effect.fnUntraced(function* (space) { - // Mounting a running sandbox can take a while; the host answers at once. - const live = yield* Codework.call((client) => client("skill.list", { spaceId: space.id }), { - timeout: "30 seconds", - }).pipe(Effect.option); - const sandbox = Option.isSome(live) ? live.value.sandbox : null; - if (Option.isSome(live) && live.value.skills !== null) { - const skills = live.value.skills.map(({ name, description, hidden }) => ({ name, description, hidden })); - const syncedAt = Date.now(); - yield* sql` - INSERT INTO skill (machine_id, space_id, skills, synced_at) - VALUES (${input.machineId}, ${space.id}, ${encode(skills)}, ${syncedAt}) - ON CONFLICT (machine_id, space_id) DO UPDATE SET skills = excluded.skills, synced_at = excluded.synced_at - `.pipe(Effect.orDie); - return { space, sandbox, skills, syncedAt } satisfies SpaceSkills; - } - const [kept] = yield* sql<{ skills: string; syncedAt: number }>` - SELECT skills, synced_at AS "syncedAt" FROM skill - WHERE machine_id = ${input.machineId} AND space_id = ${space.id} - `.pipe(Effect.orDie); - const skills = kept === undefined ? Option.none() : decode(kept.skills); - return { - space, - sandbox, - skills: Option.getOrNull(skills), - syncedAt: Option.isSome(skills) && kept !== undefined ? kept.syncedAt : null, - } satisfies SpaceSkills; - }), - { concurrency: "unbounded" }, + // Sandbox rows from CodeWork's store, never from a sandbox: listing them wakes nothing. + const sandboxes = yield* machine.pipe( + Effect.flatMap((client) => client("sandbox.list", {})), + Effect.timeout("10 seconds"), + Effect.orElseSucceed(() => []), ); + const kept = yield* sql<{ spaceId: string; data: string; observedAt: number }>` + SELECT space_id, data, observed_at FROM space_state + WHERE machine_id = ${input.machineId} AND type = 'plugin.skills.list' + `.pipe(Effect.orDie); + return spaces.map((space): SpaceSkills => { + const row = kept.find((state) => state.spaceId === space.id); + const stored = row === undefined ? Option.none() : decode(row.data); + const sandbox = space.env === null ? undefined : sandboxes.find((info) => info.id === space.env); + return { + space, + sandbox: + sandbox === undefined + ? null + : { + driver: sandbox.driver, + providerResourceId: Option.getOrNull(sandbox.providerResourceId), + status: sandbox.status, + }, + skills: Option.match(stored, { + onNone: () => null, + onSome: (value) => value.skills.map(({ name, description, hidden }) => ({ name, description, hidden })), + }), + syncedAt: Option.isSome(stored) && row !== undefined ? row.observedAt : null, + }; + }); }); export * as Skills from "./skills.ts"; diff --git a/apps/server/src/sync.ts b/apps/server/src/sync.ts index a4f3134f..1bbaef67 100644 --- a/apps/server/src/sync.ts +++ b/apps/server/src/sync.ts @@ -1,6 +1,20 @@ import { Codework as Client } from "@codeworksh/widget/backend"; import type { Codework as Wire } from "@codeworksh/widget/codework"; -import { Clock, Context, Deferred, Effect, FiberMap, FileSystem, Layer, Option, Stream, SubscriptionRef } from "effect"; +import { + Cause, + Clock, + Context, + Deferred, + Effect, + FiberHandle, + FiberMap, + FileSystem, + Layer, + Option, + Schema, + Stream, + SubscriptionRef, +} from "effect"; import { SqlClient } from "effect/sql"; import type { Publish } from "@codeworksh/widget/event"; import { CodeworkUnavailable, SyncError, type SyncThread, syncTopics } from "@codeworksh/contract"; @@ -10,6 +24,12 @@ import { Mirror } from "./mirror.ts"; import { Servers } from "./servers.ts"; type Connection = typeof Client.Service; + +/** The space daemon events this server mirrors: the builtin ones the desktop shows. */ +const daemonTypes = ["plugin.git.status", "plugin.skills.list"]; +/** Where a daemon event belongs and when it was read; the rest is stored as it came. */ +const decodePoint = Schema.decodeUnknownOption(Schema.Struct({ spaceId: Schema.String, observedAt: Schema.Number })); +const DataText = Schema.fromJsonString(Schema.Unknown); type IndexSynced = Extract; type IndexRow = Exclude; type ThreadSynced = Extract; @@ -239,6 +259,50 @@ export const layer = Layer.effect( } }); + /** + * The builtin space daemons' events, into `space_state`. A subscription opens with each space's + * last event: it is (re)started after an index batch mirrored spaces, so that replay finds their + * rows, and again whenever it fails or ends. Only a newer event replaces a row. + */ + const followDaemons = (machineId: string, client: Connection) => + client("event.subscribe", { types: daemonTypes }).pipe( + Stream.filter((frame) => daemonTypes.includes(frame.type)), + Stream.runForEach((frame) => + Effect.gen(function* () { + const point = decodePoint(frame.data); + if (Option.isNone(point)) return; + const { spaceId, observedAt } = point.value; + const text = yield* Schema.encodeEffect(DataText)(frame.data).pipe(Effect.orDie); + yield* transact((append) => + Effect.gen(function* () { + const written = yield* sql<{ spaceId: string }>` + INSERT INTO space_state (machine_id, space_id, type, data, observed_at) + SELECT ${machineId}, ${spaceId}, ${frame.type}, ${text}, ${observedAt} + WHERE EXISTS (SELECT 1 FROM space WHERE machine_id = ${machineId} AND id = ${spaceId}) + ON CONFLICT (machine_id, space_id, type) DO UPDATE + SET data = excluded.data, observed_at = excluded.observed_at + WHERE space_state.observed_at < excluded.observed_at + RETURNING space_id + `; + if (written.length === 0) return; + yield* append({ + topic: syncTopics.state, + source: "sync", + payload: { machineId, spaceId, type: frame.type }, + }); + }), + ); + }), + ), + Effect.catchCause((cause) => + Cause.hasInterruptsOnly(cause) + ? Effect.interrupt + : Effect.logWarning(`Space daemon events of machine ${machineId} stopped`, cause), + ), + Effect.andThen(Effect.sleep("1 second")), + Effect.forever, + ); + /** One connection's sync: decides between resume and full sync, then follows the index. */ const connected = (machineId: string, client: Connection) => Effect.gen(function* () { @@ -263,6 +327,8 @@ export const layer = Layer.effect( if (lives.get(machineId) === live) lives.delete(machineId); }), ); + const daemons = yield* FiberHandle.make(); + let following = false; yield* batches( client("index.sync", { after: full ? 0 : machine.cursor }), (item) => item._tag === "Synced", @@ -271,6 +337,7 @@ export const layer = Layer.effect( Effect.gen(function* () { const now = yield* Clock.currentTimeMillis; const wasFull = full; + let spaces = false; yield* transact((append) => Effect.gen(function* () { const changes = yield* Mirror.index( @@ -280,6 +347,7 @@ export const layer = Layer.effect( append, ); if (wasFull) yield* Mirror.unseen(machineId, changes, now); + spaces = changes.space.size > 0; yield* sql` UPDATE machine SET cursor = ${synced.rev}, store_id = ${synced.storeId} WHERE id = ${machineId} `; @@ -292,6 +360,10 @@ export const layer = Layer.effect( }), ); full = false; + if (wasFull || spaces || !following) { + following = true; + yield* FiberHandle.run(daemons, followDaemons(machineId, client)); + } lives.set(machineId, live); yield* SubscriptionRef.update(applied, (cursors) => new Map(cursors).set(machineId, synced.rev)); yield* startOpened(machineId, live); diff --git a/apps/server/test/__artifacts__/daemon.machines.daytona.json b/apps/server/test/__artifacts__/daemon.machines.daytona.json new file mode 100644 index 00000000..4ddbb5b0 --- /dev/null +++ b/apps/server/test/__artifacts__/daemon.machines.daytona.json @@ -0,0 +1,79 @@ +{ + "the sandbox's own folder is not a repository": { + "available": false, + "files": [], + "reason": "not-a-repository" + }, + "pushed after an edit in the sandbox": { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "sandbox.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + }, + "diff right after the run": { + "cached": true, + "files": [ + { + "file": "README.md", + "patch": "diff --git a/README.md b/README.md\nindex \n--- a/README.md\n+++ b/README.md\n@@ -1,2 +1,3 @@\n # 69th\n 69th Testing Repository for Agents\n+from daytona\n", + "status": "modified", + "additions": 1, + "deletions": 0 + } + ] + }, + "the machine's row after the owner stopped it": "online", + "diff once it is stopped": { + "cached": true, + "files": [ + { + "file": "README.md", + "patch": "diff --git a/README.md b/README.md\nindex \n--- a/README.md\n+++ b/README.md\n@@ -1,2 +1,3 @@\n # 69th\n 69th Testing Repository for Agents\n+from daytona\n", + "status": "modified", + "additions": 1, + "deletions": 0 + } + ] + }, + "provider status after that call": "offline", + "replayed while it is stopped": [ + "server.connected", + { + "available": false, + "files": [], + "reason": "not-a-repository" + }, + { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "sandbox.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + } + ] +} \ No newline at end of file diff --git a/apps/server/test/__artifacts__/daemon.machines.host.json b/apps/server/test/__artifacts__/daemon.machines.host.json new file mode 100644 index 00000000..ce9b613a --- /dev/null +++ b/apps/server/test/__artifacts__/daemon.machines.host.json @@ -0,0 +1,63 @@ +{ + "pushed after an edit": { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "remote.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + }, + "diff while the space is idle": { + "files": [ + { + "file": "README.md", + "patch": "diff --git a/README.md b/README.md\nindex \n--- a/README.md\n+++ b/README.md\n@@ -1,2 +1,3 @@\n # 69th\n 69th Testing Repository for Agents\n+from another machine\n", + "status": "modified", + "additions": 1, + "deletions": 0 + } + ] + }, + "replayed to a late subscriber": [ + "server.connected", + { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "remote.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + } + ], + "replayed after a restart": [ + "server.connected" + ], + "pushed after the next run": { + "head": "", + "available": true, + "branch": "main", + "files": [] + } +} \ No newline at end of file diff --git a/apps/server/test/__artifacts__/daemon.mirror.json b/apps/server/test/__artifacts__/daemon.mirror.json new file mode 100644 index 00000000..509fb031 --- /dev/null +++ b/apps/server/test/__artifacts__/daemon.mirror.json @@ -0,0 +1,35 @@ +{ + "mirrored types": [ + "plugin.git.status", + "plugin.skills.list" + ], + "projects.skills when the skill came before the machine": [ + { + "kind": "primary", + "skills": [ + { + "name": "deploy", + "description": "Deploy the 69th app.", + "hidden": false + } + ], + "read": true + } + ], + "projects.skills after a run removed it": [ + { + "kind": "primary", + "skills": [], + "read": true + } + ], + "a sync/state notice names the space": true, + "new sync/state notices after an app restart": 0, + "projects.skills after an app restart": [ + { + "kind": "primary", + "skills": [], + "read": true + } + ] +} diff --git a/apps/server/test/__artifacts__/schema.json b/apps/server/test/__artifacts__/schema.json index 802a3285..1ba7321b 100644 --- a/apps/server/test/__artifacts__/schema.json +++ b/apps/server/test/__artifacts__/schema.json @@ -126,12 +126,6 @@ "tbl_name": "session", "sql": "CREATE INDEX session_space_idx ON session (machine_id, space_id)" }, - { - "type": "table", - "name": "skill", - "tbl_name": "skill", - "sql": "CREATE TABLE skill ( machine_id TEXT NOT NULL, space_id TEXT NOT NULL, skills TEXT NOT NULL, synced_at INTEGER NOT NULL, PRIMARY KEY (machine_id, space_id), FOREIGN KEY (machine_id, space_id) REFERENCES space(machine_id, id) ON DELETE CASCADE )" - }, { "type": "table", "name": "space", @@ -144,6 +138,12 @@ "tbl_name": "space", "sql": "CREATE INDEX space_project_idx ON space (machine_id, project_id)" }, + { + "type": "table", + "name": "space_state", + "tbl_name": "space_state", + "sql": "CREATE TABLE space_state ( machine_id TEXT NOT NULL, space_id TEXT NOT NULL, type TEXT NOT NULL, data TEXT NOT NULL, observed_at INTEGER NOT NULL, PRIMARY KEY (machine_id, space_id, type), FOREIGN KEY (machine_id, space_id) REFERENCES space(machine_id, id) ON DELETE CASCADE )" + }, { "type": "table", "name": "sqlite_sequence", diff --git a/apps/server/test/daemon.machines.live.test.ts b/apps/server/test/daemon.machines.live.test.ts new file mode 100644 index 00000000..eb4eaea9 --- /dev/null +++ b/apps/server/test/daemon.machines.live.test.ts @@ -0,0 +1,256 @@ +/* @effect-diagnostics nodeBuiltinImport:off -- the suite runs CodeWork servers on OrbStack Linux machines. */ +import { Effect, type Layer, ManagedRuntime, Option, type Scope, Stream } from "effect"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Harness, Sandbox } from "../../../packages/harness/src/effect.ts"; +import * as SandboxController from "../../../packages/harness/src/sandbox/control.ts"; +import * as DaytonaDriver from "../../../packages/harness/src/sandboxes/daytona/index.ts"; +import { afterAll, beforeAll, describe, expect, it } from "vite-plus/test"; +import type * as Client from "../../../packages/codework/src/server/client.ts"; +import type { EventEnvelope } from "../../../packages/codework/src/server/envelope.ts"; +import "../../../packages/harness/test/utils/env.ts"; +import { available, ensure, machine } from "./machines/orb.ts"; + +/* + * Space daemons with the CodeWork server itself on another computer: a CodeWork server (scripted + * model, real `bash`) on an OrbStack Linux machine, driven from the Mac over its RPC endpoint. + * - A host space on that machine: git status pushed, replayed, diffed, and gone after a restart. + * - A Daytona sandbox driven from that machine (its credentials moved there): the same, plus a + * stopped sandbox the daemon must never wake. + */ + +const REPO = "https://github.com/codeworksh/69th"; +const PINNED = "cdaec6762e3502f6aa3bbf851e855c9ba12b3a6b"; +const STATUS = "plugin.git.status"; +const cw = machine("cw-a"); + +interface Status { + readonly spaceId: string; + readonly available: boolean; + readonly head?: string; + readonly branch?: string; + readonly files: ReadonlyArray<{ readonly file: string; readonly status: string; readonly additions: number }>; + readonly reason?: string; +} + +type SessionId = Parameters[0]["sessionId"]; + +const statusOf = (frame: EventEnvelope) => frame.data as Status; +const stable = (status: Status) => { + const { spaceId: _spaceId, head, ...rest } = status as Status & { observedAt?: number; message?: string }; + const { + observedAt: _observedAt, + message: _message, + ...kept + } = rest as typeof rest & { + observedAt?: number; + message?: string; + }; + return { head: head === PINNED ? "" : head === undefined ? undefined : "", ...kept }; +}; + +/** Statuses a subscription saw; `settle` waits for one matching `until` that nothing newer followed. */ +const watch = (rpc: Client.Client) => + Effect.gen(function* () { + const seen: Array = []; + yield* rpc["event.subscribe"]({ types: [STATUS] }).pipe( + Stream.runForEach((frame) => Effect.sync(() => seen.push(frame))), + Effect.forkScoped, + ); + yield* Effect.sleep("500 millis"); + const settle = (until: (status: Status) => boolean, spaceId?: string) => + Effect.gen(function* () { + const deadline = Date.now() + 300_000; + for (;;) { + const mine = seen.filter( + (frame) => frame.type === STATUS && (spaceId === undefined || statusOf(frame).spaceId === spaceId), + ); + const latest = mine.at(-1); + if (latest !== undefined && until(statusOf(latest))) { + yield* Effect.sleep("1500 millis"); + const after = seen.filter( + (frame) => frame.type === STATUS && (spaceId === undefined || statusOf(frame).spaceId === spaceId), + ); + if (after.length === mine.length) return statusOf(latest); + continue; + } + if (Date.now() > deadline) + return yield* Effect.die(`no settled status; last: ${JSON.stringify(latest?.data ?? null)}`); + yield* Effect.sleep("200 millis"); + } + }); + return { seen, settle }; + }); + +const prompt = (rpc: Client.Client, sessionId: SessionId, command: string) => + Effect.gen(function* () { + yield* rpc["thread.prompt"]({ sessionId, text: `bash ${command}` }); + yield* rpc["thread.wait"]({ sessionId }); + }); + +/** A fresh subscription's first frames: the replay, before anything new happens. */ +const replay = (rpc: Client.Client) => + rpc["event.subscribe"]({ types: [STATUS] }).pipe( + Stream.interruptWhen(Effect.sleep("2 seconds")), + Stream.runCollect, + Effect.map((frames) => + [...frames].map((frame) => (frame.type === STATUS ? stable(statusOf(frame)) : frame.type)), + ), + ); + +const diffOf = (rpc: Client.Client, spaceId: string, files?: ReadonlyArray) => + rpc["daemon.call"]({ plugin: "git", spaceId, method: "diff", input: files === undefined ? {} : { files } }).pipe( + Effect.map((output) => { + const result = output as { + files: ReadonlyArray<{ file: string; patch: string; additions: number; deletions: number; status: string }>; + cached?: true; + }; + return { + ...(result.cached === true ? { cached: true } : {}), + files: result.files.map((file) => ({ ...file, patch: file.patch.replace(/^index .*$/m, "index ") })), + }; + }), + Effect.catchTag("DaemonCallError", (error) => Effect.succeed({ error: error.reason })), + ); + +/** One shared harness on the Mac, owning the Daytona sandboxes the suite creates. */ +let macRuntime: ManagedRuntime.ManagedRuntime>, unknown> | undefined; +const mac = (effect: Effect.Effect>>) => { + macRuntime ??= ManagedRuntime.make( + Harness.layer({ + database: ":memory:", + home: mkdtempSync(join(tmpdir(), "daemon-mac-")), + plugins: [], + sandboxes: [DaytonaDriver.make()], + }), + ); + return macRuntime.runPromise(effect); +}; + +/** `cw.run` for a program that keeps subscriptions open until it returns. */ +const scoped = (use: (rpc: Client.Client) => Effect.Effect) => + cw.run((rpc) => Effect.scoped(use(rpc))); + +const describeMachines = available ? describe : describe.skip; + +describeMachines("space daemons with the server on another machine", () => { + beforeAll(async () => { + await ensure(cw.name); + await cw.setup(); + }, 1_800_000); + + afterAll(async () => { + await cw.stop("KILL").catch(() => undefined); + await macRuntime?.dispose(); + }, 120_000); + + it("a host space on the server's machine: push, replay, diff, restart", async () => { + await cw.stop("KILL").catch(() => undefined); + await cw.wipe(); + const base = `/home/${process.env.USER}/daemon-e2e`; + await cw.sh( + `rm -rf ${base} && mkdir -p ${base} && cd ${base} && git clone -q ${REPO} 69th && git -C 69th checkout -q -B main ${PINNED}`, + ); + const repo = `${base}/69th`; + await cw.start(); + const artifact: Record = {}; + const ids = await scoped((rpc) => + Effect.gen(function* () { + const { settle } = yield* watch(rpc); + const created = yield* rpc["session.create"]({ directory: repo, hostDir: repo as never }); + const sessionId = created.session.id; + yield* prompt( + rpc, + sessionId, + "printf 'from another machine\\n' >> README.md && printf 'x\\n' > remote.txt", + ); + const edited = yield* settle((status) => status.files.some((file) => file.file === "remote.txt")); + artifact["pushed after an edit"] = stable(edited); + artifact["diff while the space is idle"] = yield* diffOf(rpc, edited.spaceId, ["README.md"]); + artifact["replayed to a late subscriber"] = yield* replay(rpc); + return { sessionId, spaceId: edited.spaceId }; + }), + ); + await cw.stop("TERM"); + await cw.start(); + await scoped((rpc) => + Effect.gen(function* () { + artifact["replayed after a restart"] = yield* replay(rpc); + const { settle } = yield* watch(rpc); + yield* prompt(rpc, ids.sessionId, "git checkout -q -- README.md && rm remote.txt"); + artifact["pushed after the next run"] = stable(yield* settle((status) => status.files.length === 0)); + }), + ); + await expect(JSON.stringify(artifact, null, "\t")).toMatchFileSnapshot( + "./__artifacts__/daemon.machines.host.json", + ); + }, 900_000); + + const daytona = process.env.DAYTONA_API_KEY?.trim(); + (daytona ? it : it.skip)( + "a daytona sandbox driven from the server's machine: push, cached diff, and no wake", + async () => { + await cw.stop("KILL").catch(() => undefined); + await cw.wipe(); + await cw.start({ DAYTONA_API_KEY: daytona ?? "" }); + const artifact: Record = {}; + // Created and destroyed from the Mac, which owns it; the machine's server attaches it as an existing sandbox. + const created = await mac(Sandbox.create({ driver: "daytona", config: { language: "typescript" } })); + const providerResourceId = Option.getOrThrow(created.providerResourceId); + try { + await scoped((rpc) => + Effect.gen(function* () { + const { settle } = yield* watch(rpc); + const sandbox = yield* rpc["sandbox.register"]({ driver: "daytona", providerResourceId }); + const home = "/home/daytona"; + const setup = yield* rpc["session.create"]({ sandbox: { id: sandbox.id }, directory: home }); + yield* prompt( + rpc, + setup.session.id, + `git clone -q ${REPO} 69th && git -C 69th checkout -q -B main ${PINNED}`, + ); + artifact["the sandbox's own folder is not a repository"] = stable( + yield* settle((status) => !status.available), + ); + const session = yield* rpc["session.create"]({ + sandbox: { id: sandbox.id }, + directory: `${home}/69th`, + }); + yield* prompt( + rpc, + session.session.id, + "printf 'from daytona\\n' >> README.md && printf 'y\\n' > sandbox.txt", + ); + const edited = yield* settle((status) => status.files.some((file) => file.file === "sandbox.txt")); + artifact["pushed after an edit in the sandbox"] = stable(edited); + artifact["diff right after the run"] = yield* diffOf(rpc, edited.spaceId, ["README.md"]); + + // Let the handover and the warm transport close, then the owner stops it. The machine's server only + // attached it, so it may not stop it, and its own row still says it is online. + yield* Effect.sleep("40 seconds"); + yield* Effect.promise(() => mac(Sandbox.stop(created.id))); + artifact["the machine's row after the owner stopped it"] = (yield* rpc["sandbox.list"]({})).find( + (info) => info.id === sandbox.id, + )?.status; + artifact["diff once it is stopped"] = yield* diffOf(rpc, edited.spaceId, ["README.md"]); + yield* Effect.sleep("5 seconds"); + // Asked of the provider from the Mac, not the machine's stored row: a woken sandbox would show here. + artifact["provider status after that call"] = (yield* Effect.promise(() => + mac(Effect.flatMap(SandboxController.Controller, (controller) => controller.refresh(created.id))), + )).status; + artifact["replayed while it is stopped"] = yield* replay(rpc); + }), + ); + } finally { + await mac(Sandbox.stop(created.id).pipe(Effect.ignore, Effect.andThen(Sandbox.destroy(created.id)))).catch( + (error: unknown) => console.warn(`daytona sandbox ${providerResourceId} may have leaked`, error), + ); + } + await expect(JSON.stringify(artifact, null, "\t")).toMatchFileSnapshot( + "./__artifacts__/daemon.machines.daytona.json", + ); + }, + 1_200_000, + ); +}); diff --git a/apps/server/test/daemon.test.ts b/apps/server/test/daemon.test.ts new file mode 100644 index 00000000..db9f936f --- /dev/null +++ b/apps/server/test/daemon.test.ts @@ -0,0 +1,113 @@ +/* @effect-diagnostics nodeBuiltinImport:off -- the suite clones a repository and runs real servers. */ +import { syncTopics } from "@codeworksh/contract"; +import { Effect } from "effect"; +import { execFileSync } from "node:child_process"; +import { join } from "node:path"; +import { afterAll, expect, it } from "vite-plus/test"; +import { + addMachine, + app, + cleanup, + codework, + freePort, + lastSeq, + notices, + query, + snapshot, + stop, + synced, + tempDir, + waitFor, +} from "./fixtures/apps.ts"; + +const REPO = "https://github.com/codeworksh/69th"; +const PINNED = "cdaec6762e3502f6aa3bbf851e855c9ba12b3a6b"; + +afterAll(cleanup); + +/* + * The desktop's Skills section without a read on demand: a real CodeWork server's skills daemon + * publishes, the app server mirrors it into `space_state` and tells the UI which space changed, and + * `projects.skills` answers from the mirror. + */ +it("mirrors a space's skills from its daemon into the app and serves them from there", async () => { + const cwHome = tempDir("daemon-cw"); + const appHome = tempDir("daemon-app"); + const repo = join(tempDir("daemon-repo"), "69th"); + execFileSync("git", ["clone", "-q", REPO, repo], { stdio: "pipe" }); + execFileSync("git", ["-C", repo, "checkout", "-q", "-B", "main", PINNED], { stdio: "pipe" }); + + const server = await codework(cwHome, await freePort()); + const created = await server.run((rpc) => rpc["session.create"]({ directory: repo, hostDir: repo as never })); + const { spaceId, project } = created.session; + const projectId = project?.id ?? ""; + const prompt = (command: string) => + server.run((rpc) => + Effect.gen(function* () { + yield* rpc["thread.prompt"]({ sessionId: created.session.id, text: `bash ${command}` }); + yield* rpc["thread.wait"]({ sessionId: created.session.id }); + }), + ); + const artifact: Record = {}; + + // Published before the app knows the machine: its replay arrives ahead of the index that mirrors the space. + await prompt( + "mkdir -p .agents/skills/deploy && printf -- '---\\nname: deploy\\ndescription: Deploy the 69th app.\\n---\\n' > .agents/skills/deploy/SKILL.md", + ); + let desk = await app(appHome); + const machineId = await addMachine(appHome, server, desk.run); + const stored = () => + query( + appHome, + "SELECT type, data FROM space_state WHERE machine_id = ? AND space_id = ? ORDER BY type", + machineId, + spaceId, + ).map((row) => ({ type: row.type, data: JSON.parse(row.data as string) as Record })); + const listed = (until: (count: number) => boolean) => () => { + const list = stored().find((row) => row.type === "plugin.skills.list"); + return Array.isArray(list?.data.skills) && until(list.data.skills.length) ? list : undefined; + }; + const skills = () => + desk + .run((rpc) => rpc["projects.skills"]({ machineId, projectId })) + .then((spaces) => + spaces.map((space) => ({ kind: space.space.kind, skills: space.skills, read: space.syncedAt !== null })), + ); + const stateNotices = (after = 0) => + notices(appHome, syncTopics.state, after).filter((notice) => notice.spaceId === spaceId); + + await waitFor( + listed((count) => count > 0), + "the skill published before the machine was added", + ); + artifact["mirrored types"] = stored().map((row) => row.type); + artifact["projects.skills when the skill came before the machine"] = await skills(); + + const before = lastSeq(appHome); + await prompt("rm -rf .agents"); + await waitFor( + listed((count) => count === 0), + "the emptied skill list", + ); + artifact["projects.skills after a run removed it"] = await skills(); + artifact["a sync/state notice names the space"] = stateNotices(before).some( + (notice) => notice.type === "plugin.skills.list", + ); + + // A restart replays every space's last event: nothing newer, so nothing is written or noticed. + // The run's git status can land just after its skills list: count once the notices are quiet. + let noticed = -1; + while (noticed !== stateNotices().length) { + noticed = stateNotices().length; + await new Promise((resolve) => setTimeout(resolve, 2000)); + } + await stop(desk.proc); + desk = await app(appHome); + await synced(appHome, machineId, server); + await new Promise((resolve) => setTimeout(resolve, 2000)); + artifact["new sync/state notices after an app restart"] = stateNotices().length - noticed; + artifact["projects.skills after an app restart"] = await skills(); + + await snapshot("daemon.mirror", artifact); + expect(artifact["a sync/state notice names the space"]).toBe(true); +}, 240_000); diff --git a/apps/server/test/fixtures/codework.ts b/apps/server/test/fixtures/codework.ts index b8ad7905..8d81816f 100644 --- a/apps/server/test/fixtures/codework.ts +++ b/apps/server/test/fixtures/codework.ts @@ -10,6 +10,8 @@ import { testModel } from "../../../../packages/harness/test/fixtures/model.ts"; import type * as LLM from "../../../../packages/harness/src/runner/llm.ts"; import * as Server from "../../../../packages/codework/src/server/server.ts"; import { Control, Harness } from "../../../../packages/harness/src/effect.ts"; +import { bashPlugin } from "../../../../packages/harness/src/plugin/builtin/tool/bash.ts"; +import * as DaytonaDriver from "../../../../packages/harness/src/sandboxes/daytona/index.ts"; /* * `node --conditions=development codework.ts [host] [sweep]`: a real CodeWork server @@ -18,6 +20,7 @@ import { Control, Harness } from "../../../../packages/harness/src/effect.ts"; * - `gate`: "Hello " | "wor" | "ld" | "!" | end, held before each step after the first; * - `hold`: held after its start, until interrupted; * - `long `: n chunks of 32 characters, one every 10 ms; + * - `bash `: one `bash` tool call running `command`, then `done` once its result is back; * - anything else: `reply to `, at once. * Each line `release` on stdin lets the oldest held reply take its next step. * With `sweep`, it only ends the runs the last server left open, as a start does before it listens, @@ -55,6 +58,14 @@ const promptOf = (input: LLM.Input) => { return user?.parts.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("") ?? ""; }; +/** The `bash ` a prompt asks for, unless its call was already made. */ +const bashOf = (input: LLM.Input) => { + const messages = input.context.messages; + const last = messages.findLastIndex((message) => message.role === "user"); + if (messages.slice(last + 1).some((message) => message.role === "assistant")) return undefined; + return /^bash (.+)$/s.exec(promptOf(input))?.[1]; +}; + // Ids unique across restarts of this process: entry ids are the reply's message ids. let count = 0; const scripted: LLM.Open = (input, signal) => @@ -63,6 +74,30 @@ const scripted: LLM.Open = (input, signal) => const index = count; const messageId = `assistant_${process.pid}_${index}`; const text = promptOf(input); + const command = bashOf(input); + if (command !== undefined || text.startsWith("bash ")) { + const reply = + command === undefined + ? assistant(input, index, { messageId, parts: [{ type: "text", text: "done" }] }) + : assistant(input, index, { + messageId, + stopReason: "toolUse", + parts: [ + { + type: "toolCall", + callID: `call_${process.pid}_${index}`, + name: "bash", + arguments: { command }, + status: "pending", + time: { start: index, end: index }, + }, + ], + }); + const stream = createAssistantMessageEventStream(); + stream.push({ type: "start", partial: reply }); + stream.push({ type: "done", reason: command === undefined ? "stop" : "toolUse", message: reply }); + return stream; + } const long = /^long (\d+)$/.exec(text); const chunks = text === "gate" @@ -107,7 +142,10 @@ const scripted: LLM.Open = (input, signal) => return events; }); -const harness = { home, hostCwd: home, plugins: [], llm: scripted }; +// A remote sandbox driver when this machine was given its credentials. +const sandboxes = process.env.DAYTONA_API_KEY?.trim() ? [DaytonaDriver.make()] : []; +// Only the bash tool: the scripted model's `bash ` prompts call it, and nothing else varies the prompt. +const harness = { home, hostCwd: home, plugins: [bashPlugin], llm: scripted, sandboxes }; NodeRuntime.runMain( mode === "sweep" diff --git a/apps/server/test/machines/orb.ts b/apps/server/test/machines/orb.ts index bfbece43..16f3cb28 100644 --- a/apps/server/test/machines/orb.ts +++ b/apps/server/test/machines/orb.ts @@ -55,7 +55,28 @@ export const machine = (name: string) => { cwHome, database, setup: () => script("setup.sh", [checkout], 1_800_000), - start: () => script("start.sh", [checkout]), + /** + * `env` reaches the server as its environment (credentials a test moves onto the machine). It + * goes through stdin, so it never shows on a command line or in an error. + */ + start: (env: Readonly> = {}) => + orb( + [ + "-m", + name, + "bash", + "-c", + 'while IFS= read -r line && [ -n "$line" ]; do export "$line"; done; exec bash "$0" "$1"', + join(scripts, "start.sh"), + checkout, + ], + { + input: `${Object.entries(env) + .map(([key, value]) => `${key}=${value}\n`) + .join("")}\n`, + timeout: 120_000, + }, + ), /** Sweeps the store's open runs as a start would, without listening. */ sweep: () => script("start.sh", [checkout, "sweep"]), stop: (signal: "TERM" | "KILL" = "TERM") => script("stop.sh", [signal]), diff --git a/apps/web/src/widgets/session/skills.tsx b/apps/web/src/widgets/session/skills.tsx index d7c923b8..d7e7fbc0 100644 --- a/apps/web/src/widgets/session/skills.tsx +++ b/apps/web/src/widgets/session/skills.tsx @@ -1,6 +1,7 @@ -import type { SpaceSkills } from "@codeworksh/contract"; +import { type SpaceSkills, SyncState, syncTopics } from "@codeworksh/contract"; import { useAtomValue } from "@effect/atom-react"; -import { AsyncResult } from "effect/reactivity"; +import { Effect, Option, Schedule, Schema, Stream } from "effect"; +import { AsyncResult, Atom } from "effect/reactivity"; import { type ReactNode, useId, useState } from "react"; import { ChevronDown } from "@/components/vuesax"; @@ -10,7 +11,35 @@ import { useNowMinute } from "@/hooks/now"; import { ago } from "../time"; import { Row, Section, Tag, useTilde } from "./config"; -const skillsOf = (machineId: string, projectId: string) => Server.query("projects.skills", { machineId, projectId }); +const decodeState = Schema.decodeUnknownOption(SyncState); + +/** + * A project's skills by `/`: read when subscribed, and again whenever a + * skills daemon of that machine published a new list. + */ +const skillsAtom = Atom.family((key: string) => { + const [machineId = "", projectId = ""] = key.split("/"); + return Server.runtime.atom( + Stream.unwrap( + Server.use((client) => + Effect.succeed( + client("event.subscribe", { topics: [syncTopics.state] }).pipe( + Stream.filter( + (item) => + "_tag" in item || + Option.exists( + decodeState(item.payload), + (state) => state.machineId === machineId && state.type === "plugin.skills.list", + ), + ), + Stream.debounce("50 millis"), + Stream.mapEffect(() => client("projects.skills", { machineId, projectId })), + ), + ), + ), + ).pipe(Stream.retry(Schedule.spaced("1 second"))), + ); +}); /** A checkout's role, as its group says it; a folder that is not a repository needs none. */ const roles: Record = { @@ -40,10 +69,10 @@ const capitalized = (value: string) => value.charAt(0).toUpperCase() + value.sli /** * The skills sessions in this project see, one group per space: read-only, since a skill is a file - * people write. Host checkouts are read each time; a sandbox shows its last read while it is stopped. + * people write. Each space's skills daemon keeps them current; a stopped sandbox shows its last list. */ export function Skills({ machineId, projectId }: { readonly machineId: string; readonly projectId: string }) { - const result = useAtomValue(skillsOf(machineId, projectId)); + const result = useAtomValue(skillsAtom(`${machineId}/${projectId}`)); const [open, setOpen] = useState(false); if (AsyncResult.isFailure(result)) return null; // A memory sandbox keeps nothing, so there is nothing to show for it. @@ -122,7 +151,7 @@ function SpaceGroup({ entry }: { readonly entry: SpaceSkills }) { aria-labelledby={id} className="flex flex-col divide-y divide-edge/60 transition-opacity duration-200 ease-out starting:opacity-0" > - {/* Where the space is, then its folder; a sandbox says when it was last read. */} + {/* Where the space is, then its folder; a sandbox says when it was last read, any space that it never was. */}
@@ -134,7 +163,7 @@ function SpaceGroup({ entry }: { readonly entry: SpaceSkills }) { {sandboxIdOf(entry)} )} - {remote && ( + {(remote || entry.syncedAt === null) && ( {entry.syncedAt === null ? "not synced yet" : } diff --git a/apps/widget/src/codework.ts b/apps/widget/src/codework.ts index eb63016d..224cb9e8 100644 --- a/apps/widget/src/codework.ts +++ b/apps/widget/src/codework.ts @@ -420,20 +420,6 @@ export const CreateError = Schema.Union([ ]); export type CreateError = typeof CreateError.Type; -/** Why `skill.list` failed: the space is gone, or its sandbox could not be read. */ -export const SkillListError = Schema.Union([ - Schema.TaggedStruct("SpaceNotFoundError", { spaceId: Schema.String }), - Schema.TaggedStruct("SandboxNotFoundError", { id: Schema.String }), - Schema.TaggedStruct("SandboxDriverNotRegisteredError", { driver: Schema.String }), - Schema.TaggedStruct("SandboxDriverRegistrationError", { driver: Schema.String, reason: Schema.String }), - Schema.TaggedStruct("SandboxUnsupportedError", { driver: Schema.String, operation: Schema.String }), - Schema.TaggedStruct("SandboxProviderError", { driver: Schema.String, operation: Schema.String }), - Schema.TaggedStruct("SandboxUnavailError", { id: Schema.String, reason: Schema.String }), - Schema.TaggedStruct("SandboxRemovedError", { id: Schema.String }), - Schema.TaggedStruct("SandboxTransitionConflictError", { id: Schema.String }), - Schema.TaggedStruct("SandboxBusyError", { id: Schema.String }), -]); - /** A directory on the host, by its name and absolute path. */ export const Folder = Schema.Struct({ name: Schema.String, path: Schema.String }); export type Folder = typeof Folder.Type; @@ -566,30 +552,6 @@ export const Api = RpcGroup.make( success: ThreadInfo, error: CreateError, }), - /** The skills sessions in a space see; `skills` is null while its sandbox is not running. */ - Rpc.make("skill.list", { - payload: { spaceId: Schema.String }, - success: Schema.Struct({ - sandbox: Schema.NullOr( - Schema.Struct({ - driver: Schema.String, - providerResourceId: Schema.NullOr(Schema.String), - status: Schema.String, - }), - ), - skills: Schema.NullOr( - Schema.Array( - Schema.Struct({ - name: Schema.String, - description: Schema.String, - path: Schema.String, - hidden: Schema.Boolean, - }), - ), - ), - }), - error: SkillListError, - }), /** The `origin` URL of a project's main checkout on the host, credentials taken out; null when there is none. */ Rpc.make("project.remote", { payload: { projectId: Schema.String }, @@ -802,12 +764,50 @@ export const Api = RpcGroup.make( success: Repository, error: HostError, }), + /** The sandboxes CodeWork knows, from its own store: listing them never wakes one. */ + Rpc.make("sandbox.list", { + payload: {}, + success: Schema.Array( + Schema.Struct({ + id: Schema.String, + driver: Schema.String, + providerResourceId: Schema.OptionFromNullOr(Schema.String), + status: Schema.String, + }), + ), + }), + /** + * Calls a method of a space daemon running in `spaceId` (such as the git daemon's `diff`). Never + * starts a daemon or wakes a sandbox: `unavailable` when none runs there, `detached` when the space + * is not mounted. + */ + Rpc.make("daemon.call", { + payload: { + plugin: Schema.String, + spaceId: Schema.String, + method: Schema.String, + input: Schema.optional(Schema.Unknown), + }, + success: Schema.Unknown, + error: Schema.TaggedStruct("DaemonCallError", { + reason: Schema.Literals([ + "unavailable", + "detached", + "method_not_found", + "invalid_input", + "invalid_output", + "failed", + ]), + message: Schema.String, + }), + }), /** * Every event as it happens, durable and live-only alike (text deltas, execution state), after a - * `server.connected` frame. Nothing is replayed: what a reader misses while away is gone. + * `server.connected` frame. With `types`, only those, and first the last event of each that a space + * daemon published; otherwise nothing is replayed: what a reader misses while away is gone. */ Rpc.make("event.subscribe", { - payload: {}, + payload: { types: Schema.optional(Schema.Array(Schema.String)) }, success: EventEnvelope, error: Schema.Union([SubscriptionOverflowError, EventEncodingError]), stream: true, diff --git a/packages/codework/src/server/contract.ts b/packages/codework/src/server/contract.ts index 604290df..6d71d2f4 100644 --- a/packages/codework/src/server/contract.ts +++ b/packages/codework/src/server/contract.ts @@ -1,13 +1,13 @@ import { Model } from "@codeworksh/aikit"; import { Control, + DaemonHost, Event, EventList, Location, McpError, optional, Project, - Skill, PromptSchema, Runner, Sandbox, @@ -512,34 +512,6 @@ export const Api = RpcGroup.make( success: Schema.Struct({ url: Schema.NullOr(Schema.String) }), error: Project.ProjectNotFoundError, }), - /** - * The skills sessions in a space see from its folder. A sandbox is read only while it is running: - * otherwise `skills` is null and `sandbox.status` says why. `sandbox` is null for the host. - */ - Rpc.make("skill.list", { - payload: { spaceId: Schema.String }, - success: Schema.Struct({ - sandbox: Schema.NullOr( - Schema.Struct({ - driver: Schema.String, - providerResourceId: Schema.NullOr(Schema.String), - status: Sandbox.SandboxInstance.Status, - }), - ), - skills: Schema.NullOr( - Schema.Array( - Schema.Struct({ - name: Schema.String, - description: Schema.String, - path: Schema.String, - /** Its frontmatter keeps it out of the prompt. */ - hidden: Schema.Boolean, - }), - ), - ), - }), - error: Schema.Union([Skill.SpaceNotFoundError, SandboxErrors]), - }), Rpc.make("project.rename", { payload: { projectId: Schema.String, name: ProjectName }, success: Schema.Struct({ id: Schema.String, name: Schema.String }), @@ -941,12 +913,35 @@ export const Api = RpcGroup.make( success: Schema.Void, error: SandboxErrors, }), + /** + * Calls a method of a space daemon running in `spaceId`, such as `git.diff`. Never starts a daemon + * and never wakes a sandbox: `unavailable` when none runs there, `detached` when the space is not + * mounted. + */ + Rpc.make("daemon.call", { + payload: { + plugin: Schema.String, + spaceId: Schema.String, + method: Schema.String, + input: optional(Schema.Unknown), + }, + success: Schema.Unknown, + error: DaemonHost.DaemonCallError, + }), /** * Every event the server publishes, live, after a `server.connected` frame. With `threadId`, * only the events naming that thread; the frame still comes first. */ Rpc.make("event.subscribe", { - payload: { threadId: optional(ThreadSchema.ID) }, + payload: { + threadId: optional(ThreadSchema.ID), + /** + * Only these event types. The last event of each type that a space daemon published is sent + * first, after `server.connected`: a client that was away catches up without a change. + */ + types: optional(Schema.Array(Schema.String)), + // With `threadId` too, only events naming that thread pass: daemon events name none. + }, success: EventEnvelope, error: Schema.Union([Event.SubscriptionOverflowError, EncodingError]), stream: true, diff --git a/packages/codework/src/server/feed.ts b/packages/codework/src/server/feed.ts index 8d7e574c..b8e8dfbd 100644 --- a/packages/codework/src/server/feed.ts +++ b/packages/codework/src/server/feed.ts @@ -1,5 +1,5 @@ import { Event, EventRegistry, EventSchema } from "@codeworksh/harness/effect"; -import { Cause, Context, Effect, Layer, Queue, Scope, Stream } from "effect"; +import { Cause, Context, Effect, Layer, Option, Queue, type Scope, Stream } from "effect"; import * as Envelope from "./envelope.ts"; import { EncodingError, type EventEnvelope } from "./envelope.ts"; @@ -7,9 +7,24 @@ const SubscriberCapacity = 4096; export type Error = Event.SubscriptionOverflowError | EncodingError; +/** What one client receives; checked before an event takes a place in its queue. */ +export interface Filter { + /** Only these types. */ + readonly types?: ReadonlySet; + /** Only events naming this thread. */ + readonly threadId?: string; +} + +type Stream_ = Effect.Effect, never, Scope.Scope>; + export interface Interface { /** A live envelope stream for one client; releasing the scope unregisters it. */ - readonly subscribe: Effect.Effect, never, Scope.Scope>; + readonly subscribe: Stream_; + /** + * {@link subscribe}, with a filter applied before an event takes a place in the queue. `replay` is + * read in the step that registers the client, and its events lead the stream. + */ + readonly filtered: (filter: Filter, replay?: () => ReadonlyArray) => Stream_; } export class Service extends Context.Service()("@codeworksh/cli/server/feed/Service") {} @@ -32,11 +47,11 @@ export const make = Effect.fn("EventFeed.make")(function* ( const capacity = options?.capacity ?? SubscriberCapacity; // Whatever this server knows how to describe, plugin registrations included. const registry = options?.registry ?? Envelope.Core; - const subscribers = new Set>(); + const subscribers = new Map, Filter>(); const failAll = (error: Error) => Effect.sync(() => { - const current = Array.from(subscribers); + const current = Array.from(subscribers.keys()); subscribers.clear(); for (const subscriber of current) Queue.failCauseUnsafe(subscriber, Cause.fail(error)); }); @@ -47,7 +62,8 @@ export const make = Effect.fn("EventFeed.make")(function* ( // cannot describe must not spend a client's capacity on its way to nowhere. if (registry(event.type) === undefined) return; const envelope = yield* Envelope.encode(event, registry); - for (const subscriber of subscribers) { + for (const [subscriber, filter] of subscribers) { + if (!accepts(filter, envelope)) continue; if (Queue.offerUnsafe(subscriber, envelope)) continue; subscribers.delete(subscriber); Queue.failCauseUnsafe(subscriber, Cause.fail(new Event.SubscriptionOverflowError({ capacity }))); @@ -66,16 +82,35 @@ export const make = Effect.fn("EventFeed.make")(function* ( }), ); + const accepts = (filter: Filter, envelope: EventEnvelope) => + (filter.types === undefined || filter.types.has(envelope.type)) && + (filter.threadId === undefined || Envelope.ofThread(filter.threadId)(envelope)); + + const filtered: Interface["filtered"] = (filter, replay = () => []) => + Effect.gen(function* () { + const queue = yield* Effect.acquireRelease(Queue.dropping(capacity), (queue) => + Effect.sync(() => subscribers.delete(queue)).pipe(Effect.andThen(Queue.shutdown(queue))), + ); + // Read and registered in one step: an event published after the snapshot is delivered live. + const missed = yield* Effect.sync(() => { + subscribers.set(queue, filter); + return replay(); + }); + const replayed: Array = []; + for (const event of missed) { + if (registry(event.type) === undefined) continue; + const envelope = yield* Envelope.encode(event, registry).pipe(Effect.option); + if (Option.isSome(envelope) && accepts(filter, envelope.value)) replayed.push(envelope.value); + } + return Stream.fromIterable(replayed).pipe(Stream.concat(Stream.fromQueue(queue))); + }); + const unsubscribe = yield* listen(publish); yield* Effect.addFinalizer(() => unsubscribe); return Service.of({ - subscribe: Effect.acquireRelease( - Queue.dropping(capacity).pipe( - Effect.tap((queue) => Effect.sync(() => subscribers.add(queue))), - ), - (queue) => Effect.sync(() => subscribers.delete(queue)).pipe(Effect.andThen(Queue.shutdown(queue))), - ).pipe(Effect.map(Stream.fromQueue)), + subscribe: filtered({}), + filtered, }); }); diff --git a/packages/codework/src/server/handlers.ts b/packages/codework/src/server/handlers.ts index 3717e242..2db276f3 100644 --- a/packages/codework/src/server/handlers.ts +++ b/packages/codework/src/server/handlers.ts @@ -1,6 +1,7 @@ import { Model } from "@codeworksh/aikit"; import { Control, + DaemonHost, Event, EventList, EventRegistry, @@ -15,7 +16,6 @@ import { SessionStore, Settings, SettingsFile, - Skill, State, Store, Sync, @@ -221,6 +221,7 @@ export const layer = Contract.Api.toLayer( Effect.gen(function* () { const control = yield* Control.Service; const feed = yield* EventFeed.Service; + const daemons = yield* DaemonHost.Service; const sessions = yield* SessionStore.Service; const threads = yield* ThreadStore.Service; const resolve = (ref: Control.ThreadRef) => @@ -332,16 +333,6 @@ export const layer = Contract.Api.toLayer( const url = checkout === undefined ? undefined : yield* host.remote(checkout); return { url: url ?? null }; }), - "skill.list": Effect.fnUntraced(function* ({ spaceId }) { - const listed = yield* exported("Server.skill.list", Skill.list(spaceId)); - return { - sandbox: - listed.sandbox === undefined - ? null - : { ...listed.sandbox, providerResourceId: listed.sandbox.providerResourceId ?? null }, - skills: listed.skills ?? null, - }; - }), "project.rename": Effect.fnUntraced(function* ({ projectId, name }) { const renamed = yield* Project.rename({ projectId, name }); return { id: renamed.id, name: renamed.name }; @@ -603,18 +594,27 @@ export const layer = Contract.Api.toLayer( // Acquiring the feed subscription is the readiness barrier: `server.connected` // is only emitted once this client is registered, so a prompt admitted after // it cannot land in the gap. - "event.subscribe": ({ threadId }) => - Stream.unwrap( - feed.subscribe.pipe( - Effect.map((events) => - Stream.make({ id: EventSchema.ID.create(), type: Envelope.Connected.type, data: {} }).pipe( - Stream.concat( - threadId === undefined ? events : events.pipe(Stream.filter(Envelope.ofThread(threadId))), + "event.subscribe": ({ threadId, types }) => { + const only = types === undefined ? undefined : new Set(types); + return Stream.unwrap( + feed + .filtered( + { + ...(only === undefined ? {} : { types: only }), + ...(threadId === undefined ? {} : { threadId }), + }, + only === undefined ? undefined : () => daemons.last(only), + ) + .pipe( + Effect.map((events) => + Stream.make({ id: EventSchema.ID.create(), type: Envelope.Connected.type, data: {} }).pipe( + Stream.concat(events), ), ), ), - ), - ), + ); + }, + "daemon.call": (input) => daemons.call(input), }); }), ); diff --git a/packages/codework/test/__artifacts__/daemon.git.rpc.json b/packages/codework/test/__artifacts__/daemon.git.rpc.json new file mode 100644 index 00000000..8949ea36 --- /dev/null +++ b/packages/codework/test/__artifacts__/daemon.git.rpc.json @@ -0,0 +1,79 @@ +{ + "pushed after an edit": { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "added.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + }, + "types seen by the filtered subscriber": [ + "server.connected", + "plugin.git.status" + ], + "daemon.call diff": [ + { + "file": "README.md", + "patch": "diff --git a/README.md b/README.md\nindex \n--- a/README.md\n+++ b/README.md\n@@ -1,2 +1,3 @@\n # 69th\n 69th Testing Repository for Agents\n+from the rpc e2e\n", + "status": "modified", + "additions": 1, + "deletions": 0 + } + ], + "replayed to a late subscriber": [ + "server.connected", + { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "added.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + } + ], + "daemon.call errors": { + "unknown space": "unavailable", + "unknown method": "method_not_found", + "invalid input": "invalid_input" + }, + "replayed after a restart, before any run": [ + "server.connected" + ], + "pushed after the next run": { + "head": "", + "available": true, + "branch": "main", + "files": [] + }, + "replayed after that run": [ + "server.connected", + { + "head": "", + "available": true, + "branch": "main", + "files": [] + } + ] +} \ No newline at end of file diff --git a/packages/codework/test/__artifacts__/server.threads.json b/packages/codework/test/__artifacts__/server.threads.json index b3c44dc8..34128fac 100644 --- a/packages/codework/test/__artifacts__/server.threads.json +++ b/packages/codework/test/__artifacts__/server.threads.json @@ -28,6 +28,10 @@ "type": "session.execution.started", "thread": "" }, + { + "type": "space.mount.attached", + "thread": "" + }, { "type": "session.prompt.promoted", "thread": "" diff --git a/packages/codework/test/daemon.test.ts b/packages/codework/test/daemon.test.ts new file mode 100644 index 00000000..b6465cfd --- /dev/null +++ b/packages/codework/test/daemon.test.ts @@ -0,0 +1,180 @@ +/* @effect-diagnostics nodeBuiltinImport:off -- the suite clones a repository and runs the CLI server. */ +import { Clock, Effect, Layer, Stream } from "effect"; +import { execFileSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, realpathSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { expect, it, onTestFinished } from "vite-plus/test"; +import * as Client from "../src/server/client.ts"; +import type { EventEnvelope } from "../src/server/envelope.ts"; +import { models, serveCli, streaming, toolCalls, writeSettings } from "./fixtures/cli.ts"; + +const REPO = "https://github.com/codeworksh/69th"; +const PINNED = "cdaec6762e3502f6aa3bbf851e855c9ba12b3a6b"; +const STATUS = "plugin.git.status"; + +interface Status { + readonly spaceId: string; + readonly observedAt: number; + readonly available: boolean; + readonly head?: string; + readonly branch?: string; + readonly files: ReadonlyArray<{ readonly file: string; readonly status: string }>; +} + +type SessionId = Parameters[0]["sessionId"]; + +const statusOf = (envelope: EventEnvelope) => envelope.data as Status; + +/** The last status once it satisfies `until` and nothing newer came for a quiet period. */ +const settle = (seen: ReadonlyArray, until: (status: Status) => boolean) => + Effect.gen(function* () { + const deadline = (yield* Clock.currentTimeMillis) + 120_000; + for (;;) { + const statuses = seen.filter((envelope) => envelope.type === STATUS); + const latest = statuses.at(-1); + if (latest !== undefined && until(statusOf(latest))) { + yield* Effect.sleep("750 millis"); + if (seen.filter((envelope) => envelope.type === STATUS).length === statuses.length) return statusOf(latest); + continue; + } + if ((yield* Clock.currentTimeMillis) > deadline) + return yield* Effect.die(`no settled status after ${statuses.length} statuses`); + yield* Effect.sleep("100 millis"); + } + }); + +const stable = (status: Status) => { + const { observedAt: _observedAt, spaceId: _spaceId, head, ...rest } = status; + return { head: head === PINNED ? "" : head === undefined ? undefined : "", ...rest }; +}; + +/* + * The git daemon through the real server: `event.subscribe` filtered to its type (nothing else + * takes the client's queue), the last status replayed to a client that subscribes later, patches + * pulled with `daemon.call`, and the replay gone after a restart until the next run. + */ +it("serves the git daemon over RPC: filtered push, replay, daemon.call and restart", async () => { + const root = realpathSync(mkdtempSync(join(tmpdir(), "codework-daemon-"))); + onTestFinished(() => rmSync(root, { recursive: true, force: true })); + const home = join(root, "home"); + const repo = join(root, "69th"); + mkdirSync(home); + execFileSync("git", ["clone", "-q", REPO, repo], { stdio: "pipe" }); + execFileSync("git", ["-C", repo, "checkout", "-q", "-B", "main", PINNED], { stdio: "pipe" }); + + let command = "true"; + // One bash call per prompt: a request whose last message is a tool result answers, any other asks. + const fake = await streaming((body, response) => { + if (body.messages.at(-1)?.role === "tool") { + response.send({ role: "assistant", content: "done" }); + response.finish("stop"); + return; + } + response.send(toolCalls([{ name: "bash", arguments: { command } }])); + response.finish("tool_calls"); + }); + writeSettings(home, fake.url); + const start = () => + serveCli(["--home", home, "--user-config-dir", home], { + cwd: repo, + env: { ...process.env, HOME: home, CODEWORK_MODELS_FILE: models }, + }); + + const artifact: Record = {}; + const connect = (url: string) => + Effect.gen(function* () { + const context = yield* Layer.build(Client.layer(url)); + return yield* Client.make.pipe(Effect.provideContext(context)); + }); + const prompt = (rpc: Client.Client, sessionId: SessionId, next: string) => + Effect.gen(function* () { + command = next; + yield* rpc["thread.prompt"]({ sessionId, text: next }); + yield* rpc["thread.wait"]({ sessionId }); + }); + /** A fresh subscription: the frames it delivers right away, before anything new happens. */ + const replay = (rpc: Client.Client) => + rpc["event.subscribe"]({ types: [STATUS] }).pipe( + Stream.interruptWhen(Effect.sleep("1500 millis")), + Stream.runCollect, + Effect.map((frames) => + [...frames].map((frame) => (frame.type === STATUS ? stable(statusOf(frame)) : frame.type)), + ), + ); + + const first = await start(); + let sessionId = "" as SessionId; + let spaceId = ""; + await Effect.runPromise( + Effect.scoped( + Effect.gen(function* () { + const rpc = yield* connect(first.url); + const seen: Array = []; + yield* rpc["event.subscribe"]({ types: [STATUS] }).pipe( + Stream.runForEach((frame) => Effect.sync(() => seen.push(frame))), + Effect.forkScoped, + ); + yield* Effect.sleep("300 millis"); + + const created = yield* rpc["session.create"]({ directory: repo, hostDir: repo as never }); + sessionId = created.session.id; + yield* prompt(rpc, sessionId, "printf 'from the rpc e2e\\n' >> README.md && printf 'new\\n' > added.txt"); + const edited = yield* settle(seen, (status) => status.files.some((file) => file.file === "added.txt")); + spaceId = edited.spaceId; + artifact["pushed after an edit"] = stable(edited); + artifact["types seen by the filtered subscriber"] = [...new Set(seen.map((frame) => frame.type))]; + + const diff = (yield* rpc["daemon.call"]({ + plugin: "git", + spaceId, + method: "diff", + input: { files: ["README.md"] }, + })) as { + files: ReadonlyArray<{ file: string; patch: string; additions: number }>; + }; + artifact["daemon.call diff"] = diff.files.map((file) => ({ + ...file, + patch: file.patch.replace(/^index .*$/m, "index "), + })); + + artifact["replayed to a late subscriber"] = yield* replay(rpc); + + const reason = (input: Parameters[0]) => + rpc["daemon.call"](input).pipe( + Effect.as("ok"), + Effect.catchTag("DaemonCallError", (error) => Effect.succeed(error.reason)), + ); + const reasons = { + "unknown space": yield* reason({ plugin: "git", spaceId: "spc_missing", method: "diff", input: {} }), + "unknown method": yield* reason({ plugin: "git", spaceId, method: "nope", input: {} }), + "invalid input": yield* reason({ plugin: "git", spaceId, method: "diff", input: { context: "x" } }), + }; + artifact["daemon.call errors"] = reasons; + }), + ), + ); + await first.stop(); + + const second = await start(); + await Effect.runPromise( + Effect.scoped( + Effect.gen(function* () { + const rpc = yield* connect(second.url); + artifact["replayed after a restart, before any run"] = yield* replay(rpc); + const seen: Array = []; + yield* rpc["event.subscribe"]({ types: [STATUS] }).pipe( + Stream.runForEach((frame) => Effect.sync(() => seen.push(frame))), + Effect.forkScoped, + ); + yield* Effect.sleep("300 millis"); + yield* prompt(rpc, sessionId, "git checkout -q -- README.md && rm added.txt"); + artifact["pushed after the next run"] = stable(yield* settle(seen, (status) => status.files.length === 0)); + artifact["replayed after that run"] = yield* replay(rpc); + }), + ), + ); + await second.stop(); + + await expect(JSON.stringify(artifact, null, "\t")).toMatchFileSnapshot("./__artifacts__/daemon.git.rpc.json"); +}, 300_000); diff --git a/packages/codework/test/server.test.ts b/packages/codework/test/server.test.ts index dc51d126..0369eeb6 100644 --- a/packages/codework/test/server.test.ts +++ b/packages/codework/test/server.test.ts @@ -819,53 +819,6 @@ describe("server", () => { }); expect(found.found).toBe(false); }).pipe(Effect.scoped, Effect.provide(layer()), Effect.runPromise)); - - it("skill.list reads a space's skills as sessions see them, on the host and in a running sandbox", () => { - const root = realpathSync(mkdtempSync(join(tmpdir(), "codework-server-skills-"))); - homes.push(root); - const app = join(root, "app"); - const skill = (dir: string, name: string, extra = "") => { - mkdirSync(join(dir, name), { recursive: true }); - writeFileSync( - join(dir, name, "SKILL.md"), - `---\nname: ${name}\ndescription: Use for ${name}.\n${extra}---\n\n# ${name}\n`, - ); - }; - skill(join(app, ".codework", "skills"), "release"); - skill(join(app, ".agents", "skills"), "deploy"); - skill(join(app, ".agents", "skills"), "internal", "disable-model-invocation: true\n"); - // Not a folder the skill plugin reads. - skill(join(app, ".claude", "skills"), "ignored"); - - return Effect.gen(function* () { - const rpc = yield* RpcTest.makeClient(Contract.Api); - const local = yield* rpc["session.create"]({ directory: app }); - const memory = yield* rpc["session.create"]({ sandbox: { driver: "memory" } }); - const host = yield* rpc["skill.list"]({ spaceId: local.session.spaceId }); - const running = yield* rpc["skill.list"]({ spaceId: memory.session.spaceId }); - const missing = yield* rpc["skill.list"]({ spaceId: "spc_missing" }).pipe( - Effect.flip, - Effect.map((error) => error._tag), - ); - - expect(host.sandbox).toBeNull(); - expect(host.skills?.map(({ name, hidden }) => ({ name, hidden }))).toEqual([ - { name: "release", hidden: false }, - { name: "deploy", hidden: false }, - { name: "internal", hidden: true }, - ]); - expect(running.sandbox).toMatchObject({ driver: "memory", status: "online" }); - expect(running.skills).toEqual([]); - expect(missing).toBe("SpaceNotFoundError"); - return { host, running: running.skills, missing }; - }) - .pipe(Effect.scoped, Effect.provide(layer()), Effect.runPromise) - .then((artifact) => - expect(`${JSON.stringify(artifact, null, "\t").replaceAll(root, "")}\n`).toMatchFileSnapshot( - "./__artifacts__/server.skills.json", - ), - ); - }); }); const websocket = (options: Harness.Options = {}, port = 0) => { diff --git a/packages/harness/src/daemon/builtin.ts b/packages/harness/src/daemon/builtin.ts new file mode 100644 index 00000000..7e3ac69f --- /dev/null +++ b/packages/harness/src/daemon/builtin.ts @@ -0,0 +1,12 @@ +import type { Daemon } from "@codeworksh/plugin/plugin"; +import * as Git from "./git.ts"; +import * as Skills from "./skills.ts"; + +/** The space daemons every space runs. Not configurable yet: no settings select or tune them. */ +export const daemons: ReadonlyArray = [ + Git.daemon as unknown as Daemon.DaemonPlugin, + Skills.daemon as unknown as Daemon.DaemonPlugin, +]; + +/** Their events: registered with the kernel's, so the feed can describe them from boot. */ +export const definitions = daemons.flatMap((daemon) => daemon.events ?? []); diff --git a/packages/harness/src/daemon/git.ts b/packages/harness/src/daemon/git.ts new file mode 100644 index 00000000..8df494f6 --- /dev/null +++ b/packages/harness/src/daemon/git.ts @@ -0,0 +1,402 @@ +/** + * The builtin git daemon: keeps a space's working-tree summary current and serves its patches. + * + * Pushes `plugin.git.status` (branch, head, and each changed file with its line counts) after the + * space is mounted, after each turn, and when a drain lets go; pulls are `diff`, one patch per + * file. Like opencode's VCS service it pushes small and serves big: patches never go out on the + * event stream. It publishes only when the summary changed. + */ +import { Daemon } from "@codeworksh/plugin/plugin"; +import { quote } from "@codeworksh/plugin/sandbox/shell"; +import { Effect, Schema } from "effect"; +import * as EventList from "../event/list.ts"; +import { EventSchema } from "../event/schema.ts"; +import type { ISandboxExe } from "@codeworksh/plugin/sandbox/shell"; + +/** Patches past this many bytes are cut, like opencode's `MAX_TOTAL_PATCH_BYTES`. */ +export const MAX_PATCH_BYTES = 10_000_000; + +const FileStatus = Schema.Struct({ + file: Schema.String, + status: Schema.Literals(["added", "deleted", "modified"]), + additions: Schema.Int, + deletions: Schema.Int, +}); +export type FileStatus = typeof FileStatus.Type; + +/** One space's working tree, or why it could not be read. */ +export const Status = EventSchema.define({ + type: "plugin.git.status", + schema: { + spaceId: Schema.String, + observedAt: Schema.Int, + available: Schema.Boolean, + /** Absent before the first commit. */ + head: Schema.optional(Schema.String), + /** Absent on a detached HEAD. */ + branch: Schema.optional(Schema.String), + files: Schema.Array(FileStatus), + reason: Schema.optional(Schema.Literals(["not-a-repository", "git-missing", "failed"])), + message: Schema.optional(Schema.String), + }, +}); +export type Status = typeof Status.Type; + +const FileDiff = Schema.Struct({ + file: Schema.String, + patch: Schema.String, + status: Schema.Literals(["added", "deleted", "modified"]), + additions: Schema.Int, + deletions: Schema.Int, +}); +export type FileDiff = typeof FileDiff.Type; + +export const DiffInput = Schema.Struct({ + /** Default: every changed file. */ + files: Schema.optional(Schema.Array(Schema.String)), + /** Lines of context. Default: 3. */ + context: Schema.optional(Schema.Int), +}); +export const DiffOutput = Schema.Struct({ + files: Schema.Array(FileDiff), + /** + * Set when the patches passed {@link MAX_PATCH_BYTES}, or there were more untracked files than + * {@link MAX_COUNTED_UNTRACKED}, and the rest were left out. + */ + truncated: Schema.optional(Schema.Literal(true)), + /** + * Served from the patches read at the last change, because the space is not mounted and reading + * it now would wake a sandbox. As old as the last `plugin.git.status`. + */ + cached: Schema.optional(Schema.Literal(true)), +}); +export type DiffOutput = typeof DiffOutput.Type; + +/** A record of its own between {@link SUMMARY}'s NUL-separated sections: no status or numstat record is this. */ +const SEPARATOR = "\u0001"; + +/** + * Every script starts at the repository root, so every path is root-relative even for a space that + * is a subfolder. `GIT_OPTIONAL_LOCKS=0` keeps a background read from taking the index lock an + * agent's next `git add` needs; `LC_ALL=C` keeps git's messages matchable. + */ +const PRELUDE = [ + "export GIT_OPTIONAL_LOCKS=0 LC_ALL=C", + "top=$(git rev-parse --show-toplevel) || exit $?", + 'cd "$top" || exit $?', +].join("\n"); + +/** + * The patches kept per space for `diff` while it is not mounted, read when the summary changes and + * dropped past this size: a remote space's diff is wanted after its run, when nothing holds it. + */ +export const MAX_CACHED_PATCH_BYTES = 1_000_000; + +/** + * Untracked files past this many get no line counts in the summary and no patch in a `diff` (which + * then says `truncated`): each costs one git process. + */ +export const MAX_COUNTED_UNTRACKED = 200; + +/** The commit to diff against: `HEAD`, or the empty tree before the first commit. */ +const BASE = `base=$(git rev-parse -q --verify 'HEAD^{commit}' 2>/dev/null || git hash-object -t tree /dev/null)`; + +/** + * At most {@link MAX_COUNTED_UNTRACKED} untracked paths, NUL-separated and intact. NUL and newline + * are swapped so `head` (and `grep`) can count whole names: a newline in a name never splits it. + */ +const untracked = (args: string, filter = "") => + `git -c core.quotepath=false ls-files -z -o --exclude-standard ${args} | tr '\\n\\0' '\\0\\n'${filter} | head -n ${MAX_COUNTED_UNTRACKED} | tr '\\n\\0' '\\0\\n'`; + +/** Status, tracked line counts and untracked line counts in one exec, sections split by a {@link SEPARATOR} record. */ +const SUMMARY = [ + PRELUDE, + // A wholly untracked folder is one `dir/` entry, as git shows it by default: a run that fills one + // with thousands of files must not turn every published summary into thousands of entries. + "git -c core.quotepath=false status --porcelain=v2 -z --branch --untracked-files=normal --no-renames || exit $?", + "printf '\\001\\000'", + BASE, + 'git -c core.quotepath=false diff "$base" --numstat -z --no-renames || exit $?', + "printf '\\001\\000'", + // Listed the way status lists them, folders collapsed: only untracked files get line counts. + `${untracked("--directory --no-empty-directory", " | grep -av '/$'")} | xargs -0 -n 1 git diff --no-index --numstat -z -- /dev/null 2>/dev/null`, + "exit 0", +].join("\n"); + +type Counts = Map; + +/** `added\tdeleted\tpath\0`, or `added\tdeleted\t\0from\0to\0` for a pair (an untracked file against /dev/null). */ +const parseNumstat = (tokens: ReadonlyArray): Counts => { + const counts: Counts = new Map(); + for (let index = 0; index < tokens.length; index++) { + const match = /^(-|\d+)\t(-|\d+)\t(.*)$/s.exec(tokens[index] ?? ""); + if (match === null) continue; + let path = match[3] ?? ""; + if (path === "") { + path = tokens[index + 2] ?? ""; + index += 2; + } + counts.set(path, { + additions: match[1] === "-" ? 0 : Number(match[1]), + deletions: match[2] === "-" ? 0 : Number(match[2]), + }); + } + return counts; +}; + +const statusOf = (xy: string): FileStatus["status"] | undefined => + // Added to the index, then deleted from the tree: in neither HEAD nor the working tree. + xy === "AD" ? undefined : xy.includes("D") ? "deleted" : xy.includes("A") ? "added" : "modified"; + +export interface Summary { + readonly head: string | undefined; + readonly branch: string | undefined; + readonly files: ReadonlyArray; +} + +/** Parses {@link SUMMARY}'s output: porcelain v2 status, then tracked and untracked numstat. */ +export const parseSummary = (stdout: string): Summary => { + const records = stdout.split("\0"); + const first = records.indexOf(SEPARATOR); + const second = first === -1 ? -1 : records.indexOf(SEPARATOR, first + 1); + const status = first === -1 ? records : records.slice(0, first); + const tracked = first === -1 ? [] : records.slice(first + 1, second === -1 ? undefined : second); + const counts = new Map([...parseNumstat(tracked), ...parseNumstat(second === -1 ? [] : records.slice(second + 1))]); + let head: string | undefined; + let branch: string | undefined; + const files: Array = []; + const add = (file: string, kind: FileStatus["status"] | undefined) => { + if (kind !== undefined) + files.push({ file, status: kind, ...(counts.get(file) ?? { additions: 0, deletions: 0 }) }); + }; + for (const entry of status) { + if (entry.startsWith("# branch.oid ")) { + const oid = entry.slice("# branch.oid ".length); + head = oid === "(initial)" ? undefined : oid; + } else if (entry.startsWith("# branch.head ")) { + const name = entry.slice("# branch.head ".length); + branch = name === "(detached)" ? undefined : name; + } else if (entry.startsWith("1 ")) { + // 1 XY sub mH mI mW hH hI path + const fields = entry.split(" "); + add(fields.slice(8).join(" "), statusOf(fields[1] ?? "")); + } else if (entry.startsWith("u ")) { + // u XY sub m1 m2 m3 mW h1 h2 h3 path + add(entry.split(" ").slice(10).join(" "), "modified"); + } else if (entry.startsWith("? ")) { + // A wholly untracked folder (`dir/`) carries no line counts: its files are not read. + add(entry.slice(2), "added"); + } + } + files.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : 0)); + return { head, branch, files }; +}; + +// Some remote shells deliver git's message on stdout rather than stderr: both are read. +const unavailable = (exitCode: number, output: string) => + ({ + available: false as const, + files: [], + reason: + exitCode === 127 + ? ("git-missing" as const) + : /not a git repository/i.test(output) + ? ("not-a-repository" as const) + : ("failed" as const), + message: output.trim() || `git exited ${exitCode}`, + }) satisfies Omit; + +const ESCAPES: Record = { a: "\x07", b: "\b", t: "\t", n: "\n", v: "\v", f: "\f", r: "\r" }; + +/** A path git C-quoted in a patch (`"b/with\ttab"`), as the name. With `core.quotepath=false` an octal escape is one byte. */ +const unquote = (path: string) => + path.length >= 2 && path.startsWith('"') && path.endsWith('"') + ? path + .slice(1, -1) + .replace(/\\([0-7]{3}|.)/g, (_, escape: string) => + escape.length === 3 ? String.fromCharCode(Number.parseInt(escape, 8)) : (ESCAPES[escape] ?? escape), + ) + : path; + +const failed = (message: string) => ({ available: false as const, files: [], reason: "failed" as const, message }); + +/** Splits a combined patch into one entry per file. */ +export const parsePatch = (patch: string): Array => { + const chunks = patch.split(/^(?=diff --git )/m).filter((chunk) => chunk.startsWith("diff --git ")); + return chunks.map((chunk) => { + const lines = chunk.split("\n"); + const plus = lines.find((line) => line.startsWith("+++ ")); + const minus = lines.find((line) => line.startsWith("--- ")); + const strip = (line: string | undefined, prefix: string) => { + if (line === undefined || line.endsWith("/dev/null")) return undefined; + const path = unquote(line.slice(4).replace(/\t$/, "")); + return path.startsWith(prefix) ? path.slice(prefix.length) : path; + }; + // A binary or mode-only change has no `---`/`+++` lines: its name is in the header. + const quoted = /^diff --git ("(?:[^"\\]|\\.)*")/.exec(lines[0] ?? "")?.[1]; + const header = + quoted === undefined + ? /^diff --git a\/(.*) b\//.exec(lines[0] ?? "")?.[1] + : unquote(quoted).slice("a/".length); + const file = strip(plus, "b/") ?? strip(minus, "a/") ?? header ?? ""; + let additions = 0; + let deletions = 0; + let inHunk = false; + for (const line of lines) { + if (line.startsWith("@@")) inHunk = true; + else if (inHunk && line.startsWith("+")) additions++; + else if (inHunk && line.startsWith("-")) deletions++; + } + const status: FileDiff["status"] = lines.some((line) => line.startsWith("new file mode")) + ? "added" + : lines.some((line) => line.startsWith("deleted file mode")) + ? "deleted" + : "modified"; + return { file, patch: chunk, status, additions, deletions }; + }); +}; + +const diffScript = (input: typeof DiffInput.Type) => { + const context = Math.max(0, Math.floor(input.context ?? 3)); + const paths = input.files === undefined ? "" : ` -- ${input.files.map(quote).join(" ")}`; + const flags = `--no-ext-diff --no-color --src-prefix=a/ --dst-prefix=b/ --unified=${context}`; + return [ + PRELUDE, + BASE, + // git's errors (a bad pathspec, an unreadable index) are said, not shown as no changes: the + // pipeline below cannot report them. 1 means there are changes. + `git diff "$base" --quiet --no-renames${paths}; rc=$?; [ "$rc" -le 1 ] || exit "$rc"`, + "{", + `git -c core.quotepath=false diff "$base" --no-renames ${flags}${paths}`, + `${untracked(paths)} | xargs -0 -n 1 git -c core.quotepath=false diff --no-index ${flags} -- /dev/null 2>/dev/null`, + `} | head -c ${MAX_PATCH_BYTES + 1}`, + // Said after the patches, outside the byte cap: more untracked files than were read. + `[ "$(git ls-files -o --exclude-standard${paths} | head -n ${MAX_COUNTED_UNTRACKED + 1} | wc -l)" -gt ${MAX_COUNTED_UNTRACKED} ] && printf '${SEPARATOR}cut'`, + "exit 0", + ].join("\n"); +}; + +const run = (shell: ISandboxExe, script: string) => shell.exec(script); + +const bytes = (text: string) => new TextEncoder().encode(text).length; + +/** git could not produce the patches (not a repository, git missing): said, not shown as no changes. */ +export class DiffFailed extends Schema.TaggedError()("GitDiffFailed", { message: Schema.String }) {} + +const readPatches = (shell: ISandboxExe, input: typeof DiffInput.Type) => + run(shell, diffScript(input)).pipe( + Effect.filterOrFail( + (result) => result.exitCode === 0, + (result) => + new DiffFailed({ message: `${result.stderr}\n${result.stdout}`.trim() || `git exited ${result.exitCode}` }), + ), + Effect.map((result): DiffOutput => { + const cut = result.stdout.endsWith(`${SEPARATOR}cut`); + const stdout = cut ? result.stdout.slice(0, -`${SEPARATOR}cut`.length) : result.stdout; + // `head -c` counts bytes; so does this. + const overflow = bytes(stdout) > MAX_PATCH_BYTES; + const files = parsePatch(stdout); + // A cut patch ends mid-file: that file is left out rather than shown half. + if (overflow) files.pop(); + return overflow || cut ? { files, truncated: true } : { files }; + }), + ); + +const keyOf = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); + +export const daemon = Daemon.define({ + id: "git", + kind: "daemon", + scope: "space", + events: [Status], + methods: { diff: { input: DiffInput, output: DiffOutput } }, + start: (ctx) => + Effect.gen(function* () { + let last: string | undefined; + let cached: DiffOutput | undefined; + const publish = (data: Omit) => + Effect.gen(function* () { + const key = yield* keyOf(data).pipe(Effect.orDie); + if (key === last) return; + yield* ctx.events.publish(Status, data); + last = key; + }); + const summary = ctx.io + .use( + ({ shell }) => + run(shell, SUMMARY).pipe( + Effect.map((result) => + result.exitCode === 0 + ? { available: true, ...parseSummary(result.stdout) } + : unavailable(result.exitCode, `${result.stderr}\n${result.stdout}`), + ), + ), + { budget: "20 seconds" }, + ) + .pipe( + // Said, not left stale: a repository too big to read in time, or a deleted folder, is a + // state the client shows. + Effect.catchTags({ + "Daemon.BudgetExceeded": (error) => + Effect.succeed(failed(`git did not answer within ${error.budget}`)), + ShellError: (error) => + Effect.succeed( + failed( + error.cause instanceof Error + ? error.cause.message + : `could not run git in ${ctx.space.location}`, + ), + ), + }), + ); + /** + * Read on every refresh, while the space is held, for a `diff` asked after it is let go: the + * same line counts can hide a different patch. Best effort; without a fresh read there is none. + */ + const keepPatches = (data: Status["data"] | Omit) => + !data.available || data.files.length === 0 + ? Effect.sync(() => { + cached = data.available ? { files: [] } : undefined; + }) + : ctx.io + .use(({ shell }) => readPatches(shell, {}), { budget: "20 seconds" }) + .pipe( + Effect.map((patches) => + patches.truncated === undefined && + patches.files.reduce((total, file) => total + bytes(file.patch), 0) <= MAX_CACHED_PATCH_BYTES + ? patches + : undefined, + ), + Effect.orElseSucceed(() => undefined), + Effect.map((patches) => { + cached = patches; + }), + ); + const refresh = summary.pipe(Effect.tap(publish), Effect.flatMap(keepPatches)); + const status = yield* ctx.work("status", refresh); + yield* ctx.events.on( + [EventList.SpaceMountAttached, EventList.TurnEnded, EventList.SpaceMountDetached], + () => status.mark, + ); + yield* ctx.handle("diff", (input) => + ctx.io + .use(({ shell }) => readPatches(shell, input), { budget: "60 seconds" }) + .pipe( + Effect.catchTag("Daemon.Detached", (error) => { + if (cached === undefined || (input.context ?? 3) !== 3) return Effect.fail(error); + const wanted = input.files; + // Matched like the live pathspecs: a file, or everything under a folder. + const files = + wanted === undefined + ? cached.files + : cached.files.filter((file) => + wanted.some( + (path) => file.file === path || file.file.startsWith(`${path.replace(/\/$/, "")}/`), + ), + ); + return Effect.succeed({ files, cached: true as const }); + }), + ), + ); + }), +}); diff --git a/packages/harness/src/daemon/host.ts b/packages/harness/src/daemon/host.ts new file mode 100644 index 00000000..c43d8b74 --- /dev/null +++ b/packages/harness/src/daemon/host.ts @@ -0,0 +1,532 @@ +/** + * Runs space daemons: one instance of each builtin daemon per space, started on the first drain + * that mounts the space and stopped after the space has been idle. + * + * Attribution: a thread's session events belong to a space from its drain's `space.mount.attached` + * to its `space.mount.detached`. Those, and the mount events themselves, go to the space's daemons + * in publish order. + * + * Reads never wake a sandbox. A daemon borrows the sandbox's I/O only while a drain holds it; when + * the last drain of a space lets go with work still pending, the host keeps the sandbox borrowable + * for one handover window, taken while that drain's lease is still held. + */ +import type { Daemon } from "@codeworksh/plugin/plugin"; +import { SqlClient } from "effect/sql"; +import { Cause, Clock, Context, Duration, Effect, Exit, Fiber, Layer, Option, Queue, Schema, Scope } from "effect"; +import * as Event from "../event/event.ts"; +import * as EventList from "../event/list.ts"; +import { EventSchema } from "../event/schema.ts"; +import * as SandboxController from "../sandbox/control.ts"; +import { SandboxInstance } from "../sandbox/instance.ts"; +import { SandboxIO } from "../sandbox/io.ts"; +import * as Work from "./work.ts"; +import { Detached, BudgetExceeded } from "@codeworksh/plugin/plugin/daemon"; + +export class DaemonCallError extends Schema.TaggedError()("DaemonCallError", { + reason: Schema.Literals([ + "unavailable", + "detached", + "method_not_found", + "invalid_input", + "invalid_output", + "failed", + ]), + message: Schema.String, +}) {} + +/** A daemon's `start` ran past its deadline; it is left out until the next mount. */ +class DaemonStartTimeout extends Schema.TaggedError()("DaemonStartTimeout", { + daemon: Schema.String, +}) {} + +export interface CallInput { + readonly plugin: string; + readonly spaceId: string; + readonly method: string; + readonly input?: unknown; +} + +export interface Interface { + /** Call a running daemon's method; never starts an instance and never wakes a sandbox. */ + readonly call: (input: CallInput) => Effect.Effect; + /** The last event of each of `types` every space daemon published, for a client that subscribes now. */ + readonly last: (types: ReadonlySet) => ReadonlyArray; +} + +export class Service extends Context.Service()("@codeworksh/harness/daemon/host/Service") {} + +export interface Options { + readonly daemons: ReadonlyArray; + /** A space with no drain for this long stops its daemons. Default: 60 minutes. */ + readonly idleTimeout?: Duration.Input; + /** How long a sandbox stays borrowable after its last drain let go with work pending. Default: 30 seconds. */ + readonly handover?: Duration.Input; +} + +type Handler = (event: EventSchema.Payload) => Effect.Effect; +type MethodHandler = (input: unknown) => Effect.Effect; + +interface Running { + readonly daemon: Daemon.DaemonPlugin; + readonly queue: Queue.Queue; + readonly handlers: Map>; + readonly methods: Map; + readonly works: Array; + started: boolean; + /** Events offered and not yet handled. */ + pending: number; +} + +interface Instance { + readonly spaceId: string; + readonly scope: Scope.Closeable; + readonly plugins: Map; + space: Daemon.SpaceInfo | undefined; + env: SandboxInstance.ID | undefined; + idle: Fiber.Fiber | undefined; +} + +const isDetached = Schema.is(Detached); + +const threadOf = (event: EventSchema.Payload): string | undefined => { + const data: unknown = event.data; + return typeof data === "object" && data !== null && "threadId" in data && typeof data.threadId === "string" + ? data.threadId + : undefined; +}; + +export const layer = (options: Options) => + Layer.effect( + Service, + Effect.gen(function* () { + const events = yield* Event.Service; + const controller = yield* SandboxController.Controller; + const sql = yield* SqlClient.SqlClient; + const root = yield* Effect.scope; + const idleTimeout = Duration.fromInputUnsafe(options.idleTimeout ?? "60 minutes"); + const handover = Duration.fromInputUnsafe(options.handover ?? "30 seconds"); + + const instances = new Map(); + /** The space each running thread's events belong to. */ + const threads = new Map(); + /** Last published event per `spaceId \0 type`. */ + const lastEvents = new Map(); + + const readable = (instance: Instance) => + instance.env === undefined ? Effect.succeed(false) : controller.held(instance.env); + + const io = (instance: Instance, space: Daemon.SpaceInfo, env: SandboxInstance.ID): Daemon.SpaceIO => ({ + attached: controller.held(env), + use: (use, useOptions) => { + const budget = Duration.fromInputUnsafe(useOptions?.budget ?? "20 seconds"); + return Effect.gen(function* () { + const context = yield* Layer.build(controller.borrow(env, { cwd: space.location })).pipe( + Effect.catchTag("SandboxNotHeldError", () => + Effect.fail(new Detached({ spaceId: instance.spaceId })), + ), + Effect.catchTags({ + SandboxNotFoundError: Effect.die, + SandboxDriverNotRegisteredError: Effect.die, + SandboxUnavailError: Effect.die, + SandboxRemovedError: Effect.die, + SandboxProviderError: Effect.die, + }), + ); + return yield* use({ + fs: Context.get(context, SandboxIO.FileSystem), + shell: Context.get(context, SandboxIO.Shell), + }); + }).pipe( + Effect.scoped, + Effect.timeoutOrElse({ + duration: budget, + orElse: () => + Effect.fail(new BudgetExceeded({ spaceId: instance.spaceId, budget: Duration.format(budget) })), + }), + ); + }, + }); + + const context = ( + instance: Instance, + running: Running, + space: Daemon.SpaceInfo, + env: SandboxInstance.ID, + /** The daemon's own scope: a start that fails closes it, and its work with it. */ + scope: Scope.Scope, + ) => { + const ctx: Daemon.SpaceContext = { + plugin: { id: running.daemon.id, options: {} }, + space, + io: io(instance, space, env), + events: { + on: (definitions, handler) => + Effect.gen(function* () { + const types = (Array.isArray(definitions) ? definitions : [definitions]).map( + (definition: EventSchema.Definition) => definition.type, + ); + const entry = handler as Handler; + for (const type of types) + running.handlers.set(type, [...(running.handlers.get(type) ?? []), entry]); + yield* Effect.addFinalizer(() => + Effect.sync(() => { + for (const type of types) + running.handlers.set( + type, + (running.handlers.get(type) ?? []).filter((item) => item !== entry), + ); + }), + ); + }), + publish: (definition, data) => + Effect.gen(function* () { + const observedAt = yield* Clock.currentTimeMillis; + const full = { ...data, spaceId: instance.spaceId, observedAt }; + // Checked before it reaches the bus: an event the feed cannot encode disconnects every client. + const valid = yield* Schema.encodeUnknownEffect(definition.data)(full).pipe( + Effect.as(true), + Effect.catchCause((cause) => + Effect.logWarning( + `daemon ${running.daemon.id} published an invalid ${definition.type}`, + cause, + ).pipe(Effect.as(false)), + ), + ); + if (!valid) return; + // Kept before it publishes, so a client subscribing meanwhile gets it from the replay if + // not live; a copy received twice is the same value. + const id = EventSchema.ID.create(); + lastEvents.set(`${instance.spaceId}\0${definition.type}`, { + id, + type: definition.type, + data: full, + }); + yield* events + .publish(definition, full as never, { id }) + .pipe( + Effect.catchCause((cause) => + Effect.logWarning( + `daemon ${running.daemon.id} could not publish ${definition.type}`, + cause, + ), + ), + ); + }), + }, + work: (name, run) => + Effect.gen(function* () { + const work = yield* Work.make({ + name: `${running.daemon.id}/${name}`, + scope, + readable: readable(instance), + run, + }); + running.works.push(work); + yield* Effect.addFinalizer(() => + Effect.sync(() => { + const index = running.works.indexOf(work); + if (index !== -1) running.works.splice(index, 1); + }), + ); + return { mark: work.mark }; + }), + handle: (method, handler) => + Effect.gen(function* () { + running.methods.set(method, handler as MethodHandler); + yield* Effect.addFinalizer(() => Effect.sync(() => running.methods.delete(method))); + }), + }; + return ctx; + }; + + const handle = (running: Running, event: EventSchema.Payload) => + Effect.forEach(running.handlers.get(event.type) ?? [], (handler) => + handler(event).pipe( + Effect.catchCause((cause) => + Cause.hasInterruptsOnly(cause) + ? Effect.interrupt + : Effect.logWarning(`daemon ${running.daemon.id} failed on ${event.type}`, cause), + ), + ), + ).pipe( + Effect.ensuring( + Effect.sync(() => { + running.pending -= 1; + }), + ), + ); + + /** The space's folder and env, read once per instance. */ + const resolve = (instance: Instance) => + Effect.gen(function* () { + if (instance.space !== undefined && instance.env !== undefined) + return Option.some({ space: instance.space, env: instance.env }); + const rows = yield* sql<{ id: string; projectId: string; location: string; env: string | null }>` + SELECT id, project_id, location, env FROM space WHERE id = ${instance.spaceId} AND status = 'active' + `; + const row = rows[0]; + if (row === undefined) return Option.none(); + const env = SandboxInstance.ID.make(row.env ?? SandboxInstance.ID.local); + const driver = + env === SandboxInstance.ID.local + ? undefined + : (yield* controller.get(env)).pipe( + Option.map((info) => info.driver), + Option.getOrElse(() => "unknown"), + ); + instance.env = env; + instance.space = { + id: row.id, + projectId: row.projectId, + location: row.location, + placement: driver === undefined ? { kind: "local" } : { kind: "remote", driver }, + }; + return Option.some({ space: instance.space, env }); + }).pipe(Effect.orDie); + + /** + * Starts one daemon in its own scope under the instance's. One that does not start (no space, + * a failed or hung `start`) is taken out with its queue, so the next mount tries again. + */ + const startDaemon = (instance: Instance, running: Running) => + Effect.gen(function* () { + const scope = Scope.forkUnsafe(instance.scope, "sequential"); + const started = yield* Effect.gen(function* () { + const found = yield* resolve(instance); + if (Option.isNone(found)) return false; + yield* running.daemon + .start(context(instance, running, found.value.space, found.value.env, scope) as never) + .pipe( + Scope.provide(scope), + Effect.timeoutOrElse({ + duration: "30 seconds", + orElse: () => Effect.fail(new DaemonStartTimeout({ daemon: running.daemon.id })), + }), + ); + return true; + }).pipe( + Effect.catchCause((cause) => + Effect.logWarning( + `daemon ${running.daemon.id} failed to start in space ${instance.spaceId}`, + cause, + ).pipe(Effect.as(false)), + ), + ); + if (!started) { + if (instance.plugins.get(running.daemon.id) === running) instance.plugins.delete(running.daemon.id); + yield* Queue.shutdown(running.queue); + return yield* Scope.close(scope, Exit.void); + } + running.started = true; + yield* Queue.take(running.queue).pipe( + Effect.flatMap((event) => handle(running, event)), + Effect.forever, + Effect.forkIn(scope), + ); + }); + + /** The space's instance, created synchronously so two first mounts cannot make two. */ + const ensure = (spaceId: string) => + Effect.gen(function* () { + let instance = instances.get(spaceId); + if (instance === undefined) { + instance = { + spaceId, + scope: Scope.forkUnsafe(root, "parallel"), + plugins: new Map(), + space: undefined, + env: undefined, + idle: undefined, + }; + instances.set(spaceId, instance); + } + const target = instance; + for (const daemon of options.daemons) { + if (target.plugins.has(daemon.id)) continue; + const queue = yield* Queue.unbounded(); + // Checked again: making the queue yielded, and another mount may have started it. + if (target.plugins.has(daemon.id)) { + yield* Queue.shutdown(queue); + continue; + } + const running: Running = { + daemon, + queue, + handlers: new Map(), + methods: new Map(), + works: [], + started: false, + pending: 0, + }; + target.plugins.set(daemon.id, running); + yield* startDaemon(target, running).pipe(Effect.forkIn(target.scope)); + } + return target; + }); + + const deliver = (instance: Instance, event: EventSchema.Payload) => + Effect.forEach( + instance.plugins.values(), + (running) => { + // Before its handlers exist, everything is queued; after, only what it subscribed to. + if (running.started && !running.handlers.has(event.type)) return Effect.void; + running.pending += 1; + return Queue.offer(running.queue, event).pipe(Effect.asVoid); + }, + { discard: true }, + ); + + const settled = (instance: Instance) => + Effect.gen(function* () { + for (const running of instance.plugins.values()) { + if (running.pending > 0) return false; + for (const work of running.works) if (!(yield* work.settled)) return false; + } + return true; + }); + + const stop = (instance: Instance) => + Effect.gen(function* () { + // Its last events stay: a client subscribing later still sees how the space was, and how long ago. + instances.delete(instance.spaceId); + yield* Scope.close(instance.scope, Exit.void); + }); + + const occupied = (spaceId: string) => [...threads.values()].includes(spaceId); + + const scheduleIdle = (instance: Instance): Effect.Effect => + Effect.gen(function* () { + if (instance.idle !== undefined) yield* Fiber.interrupt(instance.idle); + instance.idle = yield* Effect.sleep(idleTimeout).pipe( + Effect.andThen( + Effect.suspend(() => { + instance.idle = undefined; + if (occupied(instance.spaceId)) return Effect.void; + // Pending work finishes first: it is waited for another idle period. + return settled(instance).pipe( + Effect.flatMap((done) => (done ? stop(instance) : scheduleIdle(instance))), + ); + }), + ), + Effect.forkIn(root), + ); + }); + + /** + * Keeps the sandbox borrowable after the drain lets go, while work is pending. Taken here, + * inside the publish of `detached`, while that drain's lease is still held. + */ + const openHandover = (instance: Instance) => + Effect.gen(function* () { + const found = yield* resolve(instance); + if (Option.isNone(found) || found.value.env === SandboxInstance.ID.local) return; + const env = found.value.env; + const scope = yield* Scope.fork(root, "sequential"); + const taken = yield* controller.hold(env).pipe( + Scope.provide(scope), + Effect.as(true), + Effect.catchCause(() => Effect.succeed(false)), + ); + if (!taken) return yield* Scope.close(scope, Exit.void); + yield* Effect.gen(function* () { + yield* Effect.sleep("50 millis"); + while (!(yield* settled(instance))) yield* Effect.sleep("100 millis"); + }).pipe( + Effect.timeoutOrElse({ duration: handover, orElse: () => Effect.void }), + Effect.ensuring(Scope.close(scope, Exit.void)), + Effect.forkIn(root), + ); + }); + + const onEvent = (event: EventSchema.Payload): Effect.Effect => + Effect.gen(function* () { + if (event.type === EventList.SpaceMountAttached.type) { + const data = event.data as EventList.SpaceMountAttached["data"]; + threads.set(data.threadId, data.spaceId); + const instance = yield* ensure(data.spaceId); + if (instance.idle !== undefined) { + yield* Fiber.interrupt(instance.idle); + instance.idle = undefined; + } + yield* deliver(instance, event); + for (const running of instance.plugins.values()) for (const work of running.works) yield* work.wake; + return; + } + if (event.type === EventList.SpaceMountDetached.type) { + const data = event.data as EventList.SpaceMountDetached["data"]; + const instance = instances.get(data.spaceId); + threads.delete(data.threadId); + if (instance === undefined) return; + yield* deliver(instance, event); + if (!occupied(data.spaceId)) { + yield* openHandover(instance); + yield* scheduleIdle(instance); + } + return; + } + const threadId = threadOf(event); + if (threadId === undefined) return; + const spaceId = threads.get(threadId); + if (spaceId === undefined) return; + const instance = instances.get(spaceId); + if (instance !== undefined) yield* deliver(instance, event); + }).pipe( + Effect.catchCause((cause) => + Cause.hasInterruptsOnly(cause) + ? Effect.void + : Effect.logWarning("space daemon host failed on an event", cause), + ), + ); + + const unlisten = yield* events.listen(onEvent); + yield* Effect.addFinalizer(() => unlisten); + + const call: Interface["call"] = (input) => + Effect.gen(function* () { + const running = instances.get(input.spaceId)?.plugins.get(input.plugin); + if (running === undefined || !running.started) { + return yield* new DaemonCallError({ + reason: "unavailable", + message: `No ${input.plugin} daemon is running in space ${input.spaceId}`, + }); + } + const method = running.daemon.methods?.[input.method]; + const handler = running.methods.get(input.method); + if (method === undefined || handler === undefined) { + return yield* new DaemonCallError({ + reason: "method_not_found", + message: `The ${input.plugin} daemon has no method ${input.method}`, + }); + } + const decoded = yield* Schema.decodeEffect(method.input)(input.input ?? {}).pipe( + Effect.mapError((error) => new DaemonCallError({ reason: "invalid_input", message: String(error) })), + ); + const output = yield* handler(decoded).pipe( + Effect.catchCause((cause) => { + if (Cause.hasInterruptsOnly(cause)) return Effect.interrupt; + const error: unknown = Cause.squash(cause); + if (isDetached(error)) + return Effect.fail( + new DaemonCallError({ + reason: "detached", + message: `Space ${input.spaceId} is not mounted`, + }), + ); + return Effect.fail( + new DaemonCallError({ + reason: "failed", + message: error instanceof Error ? error.message : String(error), + }), + ); + }), + ); + return yield* Schema.encodeUnknownEffect(method.output)(output).pipe( + Effect.mapError((error) => new DaemonCallError({ reason: "invalid_output", message: String(error) })), + ); + }); + + const last: Interface["last"] = (types) => [...lastEvents.values()].filter((event) => types.has(event.type)); + + return Service.of({ call, last }); + }), + ); diff --git a/packages/harness/src/daemon/skills.ts b/packages/harness/src/daemon/skills.ts new file mode 100644 index 00000000..9b25e572 --- /dev/null +++ b/packages/harness/src/daemon/skills.ts @@ -0,0 +1,65 @@ +/** + * The builtin skills daemon: keeps the skills a space's sessions can see current, read exactly the + * way the skill prompt plugin reads them (`.codework/skills` at the space's folder, `.agents/skills` + * up to the git root), through the space's own filesystem. Publishes `plugin.skills.list` when the + * list changed. + */ +import { Daemon } from "@codeworksh/plugin/plugin"; +import { Effect, References, Schema } from "effect"; +import * as EventList from "../event/list.ts"; +import { EventSchema } from "../event/schema.ts"; +import * as Skill from "../plugin/builtin/prompt/skill.ts"; + +const Found = Schema.Struct({ + name: Schema.String, + description: Schema.String, + path: Schema.String, + /** Its frontmatter keeps it out of the prompt. */ + hidden: Schema.Boolean, +}); + +export const List = EventSchema.define({ + type: "plugin.skills.list", + schema: { + spaceId: Schema.String, + observedAt: Schema.Int, + skills: Schema.Array(Found), + }, +}); +export type List = typeof List.Type; + +const keyOf = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); + +export const daemon = Daemon.define({ + id: "skills", + kind: "daemon", + scope: "space", + events: [List], + start: (ctx) => + Effect.gen(function* () { + let last: string | undefined; + const refresh = ctx.io.use(({ fs }) => + Effect.gen(function* () { + // The skill prompt plugin reports a broken skill file each turn; this read stays quiet. + const discovered = yield* Skill.discover(fs, ctx.space.location).pipe( + Effect.provideService(References.MinimumLogLevel, "Error"), + ); + const skills = discovered.map((skill) => ({ + name: skill.name, + description: skill.description, + path: skill.filePath, + hidden: skill.disableModelInvocation, + })); + const key = yield* keyOf(skills).pipe(Effect.orDie); + if (key === last) return; + yield* ctx.events.publish(List, { skills }); + last = key; + }), + ); + const list = yield* ctx.work("list", refresh); + yield* ctx.events.on( + [EventList.SpaceMountAttached, EventList.TurnEnded, EventList.SpaceMountDetached], + () => list.mark, + ); + }), +}); diff --git a/packages/harness/src/daemon/work.ts b/packages/harness/src/daemon/work.ts new file mode 100644 index 00000000..19be15a7 --- /dev/null +++ b/packages/harness/src/daemon/work.ts @@ -0,0 +1,91 @@ +import { Detached } from "@codeworksh/plugin/plugin/daemon"; +import { Cause, Effect, Schema, type Scope } from "effect"; + +const isDetached = Schema.is(Detached); + +/** + * A daemon's single-flight job. `mark` runs it now when it is idle and the space is readable, + * once more after the run in flight (any number of marks collapse into one rerun), and leaves it + * dirty when the space is not readable; `wake` runs dirty work when the space is mounted again. + * + * A run that fails is logged and not retried: the next mark is the retry. One that fails because + * the space stopped being readable is dirty again instead. + */ +export interface Work { + readonly mark: Effect.Effect; + readonly wake: Effect.Effect; + /** Neither running nor due to run again: what a handover waits for. */ + readonly settled: Effect.Effect; +} + +export const make = (input: { + readonly name: string; + readonly scope: Scope.Scope; + readonly readable: Effect.Effect; + readonly run: Effect.Effect; +}): Effect.Effect => + Effect.sync(() => { + const state = { running: false, again: false, dirty: false }; + + const detached = (cause: Cause.Cause) => isDetached(Cause.squash(cause)); + + const once = input.run.pipe( + Effect.catchCause((cause) => + Cause.hasInterruptsOnly(cause) + ? Effect.interrupt + : detached(cause) + ? Effect.sync(() => { + state.dirty = true; + }) + : Effect.logWarning(`daemon work ${input.name} failed`, cause), + ), + ); + + const loop: Effect.Effect = Effect.gen(function* () { + state.dirty = false; + yield* once; + if (!state.again) return; + state.again = false; + if (yield* input.readable) return yield* loop; + state.dirty = true; + }).pipe( + Effect.ensuring( + Effect.sync(() => { + state.running = false; + }), + ), + ); + + const start = Effect.suspend(() => { + state.running = true; + state.dirty = false; + return Effect.forkIn(loop, input.scope).pipe(Effect.asVoid); + }); + + const mark = Effect.suspend(() => { + if (state.running) { + state.again = true; + return Effect.void; + } + return input.readable.pipe( + Effect.flatMap((readable) => { + // Re-checked: another mark may have started a run while `readable` was read. + if (state.running) { + state.again = true; + return Effect.void; + } + if (readable) return start; + state.dirty = true; + return Effect.void; + }), + ); + }); + + const wake = Effect.suspend(() => (state.dirty && !state.running ? mark : Effect.void)); + + return { + mark, + wake, + settled: Effect.sync(() => !state.running && !state.again), + }; + }); diff --git a/packages/harness/src/effect.ts b/packages/harness/src/effect.ts index 20d78d99..94d6d3e6 100644 --- a/packages/harness/src/effect.ts +++ b/packages/harness/src/effect.ts @@ -1,6 +1,5 @@ export * as Harness from "./effect/harness.ts"; export * as Sandbox from "./effect/sandbox.ts"; -export * as Skill from "./effect/skill.ts"; export * as Session from "./effect/session.ts"; export * as Sync from "./effect/sync.ts"; export * as Control from "./control.ts"; @@ -19,6 +18,9 @@ export * as ThreadSchema from "./thread/schema.ts"; export * as DocumentSchema from "./document/schema.ts"; export * as Runner from "./runner/run.ts"; export * as Git from "./git/git.ts"; +export * as DaemonHost from "./daemon/host.ts"; +export * as GitDaemon from "./daemon/git.ts"; +export * as SkillsDaemon from "./daemon/skills.ts"; export * as Location from "./location/location.ts"; export * as Project from "./project/project.ts"; export * as McpConfig from "./mcp/config.ts"; diff --git a/packages/harness/src/effect/harness.ts b/packages/harness/src/effect/harness.ts index 74659444..93cee3e1 100644 --- a/packages/harness/src/effect/harness.ts +++ b/packages/harness/src/effect/harness.ts @@ -1,3 +1,5 @@ +import * as Daemons from "../daemon/builtin.ts"; +import * as DaemonHost from "../daemon/host.ts"; import { type Duration, Effect, Layer, Option, Ref } from "effect"; import * as Context from "../context/context.ts"; import * as Control from "../control.ts"; @@ -66,6 +68,8 @@ export interface Options { readonly watchModels?: boolean; /** How long an MCP server no running session holds stays open. Default: 10 minutes. */ readonly mcpIdleTimeout?: Duration.Input; + /** Space daemon timings: the handover after a space's last drain, and the idle stop. */ + readonly daemons?: Pick; } export const layer = (options: Options = {}) => @@ -258,6 +262,7 @@ export const layer = (options: Options = {}) => return Control.layer.pipe( Layer.provideMerge(RunnerExecute.layer.pipe(Layer.provide(loop))), Layer.provideMerge(State.layer({}, pool, references, followStore, rebuild, viewOf)), + Layer.provideMerge(DaemonHost.layer({ ...options.daemons, daemons: Daemons.daemons })), Layer.provideMerge( Mcp.layer({ hostCwd, home: paths.home, authFile, idleTimeout: options.mcpIdleTimeout }).pipe( Layer.provide(Process.host), diff --git a/packages/harness/src/effect/skill.ts b/packages/harness/src/effect/skill.ts deleted file mode 100644 index c630e7b3..00000000 --- a/packages/harness/src/effect/skill.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { Effect, Option, Schema } from "effect"; -import * as Skill from "../plugin/builtin/prompt/skill.ts"; -import * as Project from "../project/project.ts"; -import * as SandboxController from "../sandbox/control.ts"; -import { SandboxInstance } from "../sandbox/instance.ts"; -import { SandboxIO } from "../sandbox/io.ts"; - -export class SpaceNotFoundError extends Schema.TaggedError()("SpaceNotFoundError", { - spaceId: Schema.String, -}) {} - -export interface Found { - readonly name: string; - readonly description: string; - readonly path: string; - /** Its frontmatter keeps it out of the prompt. */ - readonly hidden: boolean; -} - -/** The sandbox a space is in, as last observed; absent for the host. */ -export interface Placement { - readonly driver: string; - readonly providerResourceId: string | undefined; - readonly status: SandboxInstance.Status; -} - -export interface Listed { - readonly sandbox: Placement | undefined; - /** Absent when the sandbox is not running: listing never wakes one. */ - readonly skills: ReadonlyArray | undefined; -} - -const placement = (info: SandboxInstance.Info): Placement => ({ - driver: info.driver, - providerResourceId: Option.getOrUndefined(info.providerResourceId), - status: info.status, -}); - -/** - * The skills sessions in a space see from its folder, read the way the skill plugin reads them. A - * sandbox is read only while it is running; one that is stopped, expired or unreachable is left as - * it is and reported with its last observed state. - */ -export const list = Effect.fn("Skill.list")(function* (spaceId: string) { - const space = yield* Project.space(spaceId); - if (space === undefined) return yield* new SpaceNotFoundError({ spaceId }); - const read = Effect.gen(function* () { - const fs = yield* SandboxIO.FileSystem; - const skills = yield* Skill.discover(fs, space.location); - return skills.map((skill): Found => ({ - name: skill.name, - description: skill.description, - path: skill.filePath, - hidden: skill.disableModelInvocation, - })); - }); - const controller = yield* SandboxController.Controller; - if (space.env === null) { - const skills = yield* controller.withMount(undefined, read, { cwd: space.location }); - return { sandbox: undefined, skills } satisfies Listed; - } - const id = SandboxInstance.ID.make(space.env); - // A fresh state when the driver can give one without waking the sandbox, else the last observed. - const info = yield* controller.refresh(id).pipe( - Effect.asSome, - Effect.catch(() => controller.get(id)), - Effect.orElseSucceed(() => Option.none()), - ); - if (Option.isNone(info)) return yield* new SpaceNotFoundError({ spaceId }); - const sandbox = placement(info.value); - if (sandbox.status !== "online") return { sandbox, skills: undefined } satisfies Listed; - const skills = yield* controller.withMount(id, read, { cwd: space.location }); - return { sandbox, skills } satisfies Listed; -}); diff --git a/packages/harness/src/event/list.ts b/packages/harness/src/event/list.ts index ad8a81e8..ccecc43f 100644 --- a/packages/harness/src/event/list.ts +++ b/packages/harness/src/event/list.ts @@ -459,6 +459,23 @@ export const McpServerStatus = EventSchema.define({ }); export type McpServerStatus = typeof McpServerStatus.Type; +/** + * A drain of the thread mounted a space and its directory checks passed: from here until the + * matching `detached`, the thread's session events belong to that space. + */ +export const SpaceMountAttached = EventSchema.define({ + type: "space.mount.attached", + schema: { ...baseOptions, spaceId: Schema.String }, +}); +export type SpaceMountAttached = typeof SpaceMountAttached.Type; + +/** The drain is releasing the space; its lease is still held while this publishes. */ +export const SpaceMountDetached = EventSchema.define({ + type: "space.mount.detached", + schema: { ...baseOptions, spaceId: Schema.String }, +}); +export type SpaceMountDetached = typeof SpaceMountDetached.Type; + /** * Everything the kernel can publish. `PublicDefinitions` is the narrower set * that reaches clients -- adding an event here does not publish it. @@ -498,6 +515,8 @@ export const Definitions = EventSchema.inventory( ToolSettled, PluginUpdated, McpServerStatus, + SpaceMountAttached, + SpaceMountDetached, ); /** diff --git a/packages/harness/src/event/registry.ts b/packages/harness/src/event/registry.ts index 1caa7368..eeaf209e 100644 --- a/packages/harness/src/event/registry.ts +++ b/packages/harness/src/event/registry.ts @@ -1,5 +1,6 @@ import { Context, Effect, Layer, Schema } from "effect"; import type { Plugin } from "../plugin/plugin.ts"; +import * as Daemons from "../daemon/builtin.ts"; import * as EventList from "./list.ts"; import { EventSchema } from "./schema.ts"; @@ -27,7 +28,10 @@ export class EventRegistrationError extends Schema.TaggedError `plugin.${pluginId}.`; -const kernel = new Set(EventList.Definitions.map((definition) => definition.type)); +/** The kernel's events and the builtin space daemons': present whatever plugins load. */ +const builtin: ReadonlyArray = [...EventList.Definitions, ...Daemons.definitions]; + +const kernel = new Set(builtin.map((definition) => definition.type)); /** * Collects plugin event definitions into one set with the kernel's. @@ -65,10 +69,10 @@ export const flatten = Effect.fn("EventRegistry.flatten")(function* (plugins: Re definitions.push(definition); } } - return [...EventList.Definitions, ...definitions] as ReadonlyArray; + return [...builtin, ...definitions] as ReadonlyArray; }); -export const layer = (definitions: ReadonlyArray = EventList.Definitions) => +export const layer = (definitions: ReadonlyArray = builtin) => Layer.sync(Service, () => { let current = definitions; let latest = EventSchema.latest(current); diff --git a/packages/harness/src/runner/execute.ts b/packages/harness/src/runner/execute.ts index 41213067..88608949 100644 --- a/packages/harness/src/runner/execute.ts +++ b/packages/harness/src/runner/execute.ts @@ -200,6 +200,21 @@ export const layer = Layer.effect( const scopedRun = Effect.gen(function* () { const mountContext = yield* Layer.build(mount); + // Space daemons see this thread's events from `attached` to `detached`. `detached` is + // published here, ahead of the lease's own finalizer, so the lease is still held while its + // listeners run, whether the drain succeeded, failed or was stopped. + const spaceEvent = { sessionId, threadId, spaceId: placement.value.space.id }; + let attached = false; + yield* Effect.addFinalizer(() => + attached + ? DateTime.now.pipe( + Effect.flatMap((timestamp) => + events.publish(EventList.SpaceMountDetached, { ...spaceEvent, timestamp }), + ), + Effect.asVoid, + ) + : Effect.void, + ); const current = Context.get(mountContext, SandboxIO.Current); const fs = Context.get(mountContext, SandboxIO.FileSystem); if (!(yield* fs.exists(current.cwd))) { @@ -227,6 +242,9 @@ export const layer = Layer.effect( ), ); + // Set first: interrupted mid-publish, the drain still sends `detached`; a stray one is harmless. + attached = true; + yield* events.publish(EventList.SpaceMountAttached, { ...spaceEvent, timestamp: yield* DateTime.now }); // The thread's MCP servers stay open while its drain runs, and idle out after. yield* mcp.lease({ sessionId, threadId }); return yield* ending( diff --git a/packages/harness/src/sandbox/control.ts b/packages/harness/src/sandbox/control.ts index b675caf4..b1387921 100644 --- a/packages/harness/src/sandbox/control.ts +++ b/packages/harness/src/sandbox/control.ts @@ -7,6 +7,8 @@ import { Layer, LayerMap, Option, + RcMap, + type Scope, Schema, Semaphore, Stream, @@ -28,6 +30,7 @@ import { type SandboxMountError, SandboxMustBeStoppedError, SandboxNotFoundError, + SandboxNotHeldError, SandboxProviderError, type SandboxReadError, type SandboxRefreshError, @@ -106,6 +109,22 @@ export interface Interface { use: Effect.Effect, options?: MountOptions, ) => Effect.Effect>; + /** + * The sandbox's I/O without taking a lease: allowed only while a drain holds the sandbox or a + * {@link Interface.hold} is open, so the transport is already attached and nothing can wake it. + * The host is always allowed. Never counts toward `refCount`, so it never blocks `stop`. + */ + readonly borrow: ( + id: SandboxInstance.ID | undefined, + options?: MountOptions, + ) => SandboxIO.Layer; + /** Whether `borrow` is allowed right now: always for the host. */ + readonly held: (id: SandboxInstance.ID) => Effect.Effect; + /** + * Keep the sandbox borrowable for the calling scope after its drain lets go: taken only while a + * drain still holds it, and pins the attached transport. Not a lease: `stop` ends it. + */ + readonly hold: (id: SandboxInstance.ID) => Effect.Effect; readonly wake: (id: SandboxInstance.ID) => Effect.Effect; readonly stop: (id: SandboxInstance.ID) => Effect.Effect; readonly destroy: (id: SandboxInstance.ID, options?: DestroyOptions) => Effect.Effect; @@ -185,6 +204,16 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt const store = yield* SandboxStore.make; const gate = yield* Semaphore.make(1); const refs = new Map(); + // Handovers open after a drain let go: they let `borrow` in, and are never leases. One token per + // hold, so a hold `stop` already ended cannot end a newer one when it closes. + const holds = new Map>(); + // Leases whose transport is attached: with one, `borrow` pins a built transport and never attaches. + const attachedLeases = new Map(); + const countAttached = (id: SandboxInstance.ID, delta: 1 | -1) => { + const next = (attachedLeases.get(id) ?? 0) + delta; + if (next <= 0) attachedLeases.delete(id); + else attachedLeases.set(id, next); + }; const hostDefaultCwd = SandboxIO.resolveMountCwd(options.hostCwd); const hostCreatedAt = asDate(yield* DateTime.now); @@ -224,6 +253,7 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt if (next === 0) refs.delete(id); else refs.set(id, next); lease.held = false; + if (lease.mounted) countAttached(id, -1); return next === 0; }), ); @@ -690,7 +720,13 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt ); const fs = Context.get(context, SandboxIO.FileSystem); const shell = Context.get(context, SandboxIO.Shell); - lease.mounted = true; + // With the count, in one step: a release between the two would uncount another lease. + yield* gate.withPermits(1)( + Effect.sync(() => { + lease.mounted = true; + if (id !== SandboxInstance.ID.local && lease.held) countAttached(id, 1); + }), + ); if (id !== SandboxInstance.ID.local) { const now = yield* Effect.clockWith((clock) => clock.currentTimeMillis); @@ -717,6 +753,83 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt const mount: Interface["mount"] = (id, mountOptions) => mountLayer(id, mountOptions); + const holding = (id: SandboxInstance.ID) => (holds.get(id)?.size ?? 0) > 0; + const held = (id: SandboxInstance.ID) => refCount(id) > 0 || holding(id); + + /** + * Under the gate `stop` claims with, so a stop either lands first (the sandbox is no longer + * online) or after the transport is pinned. Only a transport some lease or hold already attached + * is pinned, so this returns at once and never attaches. + */ + const borrowable = (id: SandboxInstance.ID, allow: () => boolean) => + gate.withPermits(1)( + Effect.gen(function* () { + const row = yield* requireRow(id); + // A lease or hold says one was attached; the map says it still is (an invalidated one would be rebuilt). + const attached = + ((attachedLeases.get(id) ?? 0) > 0 || holding(id)) && (yield* RcMap.has(transports.rcMap, id)); + if (!allow() || !attached || row.status !== "online") return yield* new SandboxNotHeldError({ id }); + const context = yield* transports.contextEffect(id); + return { row, context }; + }), + ); + + const borrow: Interface["borrow"] = (id = SandboxInstance.ID.local, mountOptions = {}) => + Layer.unwrap( + Effect.gen(function* () { + let identity: SandboxIO.Identity; + let context: Context.Context; + if (id === SandboxInstance.ID.local) { + identity = { + id, + driver: SandboxDriver.Name.make("local"), + kind: "local", + cwd: SandboxIO.resolveMountCwd(hostDefaultCwd, mountOptions.cwd), + spillPath: SandboxIO.DEFAULT_SPILL_PATH, + }; + context = yield* transports.contextEffect(id); + } else { + const borrowed = yield* borrowable(id, () => held(id)); + const attached = yield* runtime(borrowed.row, "mount"); + identity = { + id, + driver: attached.driver.name, + kind: borrowed.row.kind, + cwd: SandboxIO.resolveMountCwd(attached.input.runtimeConfig.defaultCwd, mountOptions.cwd), + spillPath: attached.input.runtimeConfig.spillPath ?? SandboxIO.DEFAULT_SPILL_PATH, + }; + context = borrowed.context; + } + const fs = Context.get(context, SandboxIO.FileSystem); + const shell = Context.get(context, SandboxIO.Shell); + return Layer.mergeAll( + Layer.succeed(SandboxIO.Current, identity), + Layer.succeed(SandboxIO.FileSystem, SandboxFileSystem.withCwd(fs, identity.cwd)), + // Untimed: the exec metric measures what runs do, not daemon reads between them. + Layer.succeed(SandboxIO.Shell, shellWithCwd(shell, identity.cwd)), + ).pipe(SandboxIO.withMutation); + }), + ); + + const isHeld: Interface["held"] = (id) => Effect.sync(() => id === SandboxInstance.ID.local || held(id)); + + const hold: Interface["hold"] = (id) => + Effect.gen(function* () { + if (id === SandboxInstance.ID.local) return; + yield* borrowable(id, () => refCount(id) > 0); + const token = {}; + holds.set(id, (holds.get(id) ?? new Set()).add(token)); + yield* Effect.addFinalizer(() => + gate.withPermits(1)( + Effect.sync(() => { + const tokens = holds.get(id); + tokens?.delete(token); + if (tokens?.size === 0) holds.delete(id); + }), + ), + ); + }); + const withMount: Interface["withMount"] = (id, use, mountOptions) => use.pipe(Effect.provide(mount(id, mountOptions)), Effect.scoped); @@ -773,7 +886,7 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt operation: "wake", }); } - if (refCount(id) === 0) yield* transports.invalidate(id); + if (!held(id)) yield* transports.invalidate(id); const observed = yield* provision(attached.driver.name, "wake", attached.driver.wake(attached.input)).pipe( Effect.catch((error): Effect.Effect => providerErrorIsNotFound(error) ? markUnavailable(id, error) : persistProviderFailure(id, "faulted", error), @@ -804,6 +917,8 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt const latest = yield* requireRow(id); const count = refCount(id); if (count > 0) return yield* new SandboxBusyError({ id, refCount: count }); + // A handover never keeps a sandbox up against a stop: it ends here. + holds.delete(id); if (latest.status === "offline") return { claimed: false as const, row: latest }; if (latest.status === "removed") { return yield* new SandboxRemovedError({ @@ -959,6 +1074,9 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt refresh, mount, withMount, + borrow, + held: isHeld, + hold, wake, stop, destroy, diff --git a/packages/harness/test/__artifacts__/daemon.git.concurrent.json b/packages/harness/test/__artifacts__/daemon.git.concurrent.json new file mode 100644 index 00000000..20c733b0 --- /dev/null +++ b/packages/harness/test/__artifacts__/daemon.git.concurrent.json @@ -0,0 +1,17 @@ +{ + "files": [ + { + "file": "from-first.txt", + "status": "added", + "additions": 1, + "deletions": 0 + }, + { + "file": "from-second.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ], + "statuses published": "at most 4" +} \ No newline at end of file diff --git a/packages/harness/test/__artifacts__/daemon.git.daytona.wake.json b/packages/harness/test/__artifacts__/daemon.git.daytona.wake.json new file mode 100644 index 00000000..4c3f98ab --- /dev/null +++ b/packages/harness/test/__artifacts__/daemon.git.daytona.wake.json @@ -0,0 +1,30 @@ +{ + "wake": { + "borrowable after the handover closed": false, + "stopped": "offline", + "diff on a stopped sandbox": "served from the cache", + "diff with other context on a stopped sandbox": "detached", + "provider status after that call": "offline" + }, + "after": { + "step": "the next run wakes it and the daemon catches up", + "status": { + "head": "", + "available": true, + "files": [ + { + "file": "outside.txt", + "status": "added", + "additions": 1, + "deletions": 0 + }, + { + "file": "woken.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + } + } +} \ No newline at end of file diff --git a/packages/harness/test/__artifacts__/daemon.git.e2e.json b/packages/harness/test/__artifacts__/daemon.git.e2e.json new file mode 100644 index 00000000..a9fe5e58 --- /dev/null +++ b/packages/harness/test/__artifacts__/daemon.git.e2e.json @@ -0,0 +1,159 @@ +[ + { + "step": "first run: clean clone", + "status": { + "head": "", + "available": true, + "branch": "main", + "files": [] + } + }, + { + "step": "edit, add and nest files", + "status": { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "docs/", + "status": "added", + "additions": 0, + "deletions": 0 + }, + { + "file": "notes.md", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + } + }, + { + "step": "diff of two files", + "diff": [ + { + "file": "README.md", + "patch": "diff --git a/README.md b/README.md\nindex \n--- a/README.md\n+++ b/README.md\n@@ -1,2 +1,3 @@\n # 69th\n 69th Testing Repository for Agents\n+one more line\n", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "notes.md", + "patch": "diff --git a/notes.md b/notes.md\nnew file mode 100644\nindex \n--- /dev/null\n+++ b/notes.md\n@@ -0,0 +1 @@\n+hello from the daemon e2e\n", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + }, + { + "step": "commit everything", + "status": { + "head": "", + "available": true, + "branch": "main", + "files": [] + } + }, + { + "step": "branch, then delete a tracked file", + "status": { + "head": "", + "available": true, + "branch": "feature/e2e", + "files": [ + { + "file": "README.md", + "status": "deleted", + "additions": 0, + "deletions": 3 + } + ] + } + }, + { + "step": "reset and detach HEAD", + "status": { + "head": "", + "available": true, + "files": [] + } + }, + { + "step": "a session in a subfolder: a new file in a new folder shows as the folder", + "status": { + "head": "", + "available": true, + "files": [ + { + "file": "sub/", + "status": "added", + "additions": 0, + "deletions": 0 + } + ] + } + }, + { + "step": "an untracked folder of 2000 files", + "status": { + "head": "", + "available": true, + "files": [ + { + "file": "build-cache/", + "status": "added", + "additions": 0, + "deletions": 0 + } + ] + } + }, + { + "step": "outside edit, picked up on the next run", + "status": { + "head": "", + "available": true, + "files": [ + { + "file": "outside.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + } + }, + { + "step": "a skill added by a run", + "skills": [ + { + "name": "deploy", + "description": "Deploy the 69th app.", + "path": "/.agents/skills/deploy/SKILL.md", + "hidden": false + } + ] + }, + { + "step": "the skill removed by a run", + "skills": [] + }, + { + "step": "a space that is not a repository", + "status": { + "available": false, + "files": [], + "reason": "not-a-repository" + } + } +] \ No newline at end of file diff --git a/packages/harness/test/__artifacts__/daemon.git.relink.json b/packages/harness/test/__artifacts__/daemon.git.relink.json new file mode 100644 index 00000000..dfa20c69 --- /dev/null +++ b/packages/harness/test/__artifacts__/daemon.git.relink.json @@ -0,0 +1,79 @@ +{ + "daytona after its run": { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "where.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + }, + "vercel after its run": { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "where.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + }, + "host after its run": { + "head": "", + "available": true, + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "deleted", + "additions": 0, + "deletions": 2 + }, + { + "file": "where.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + }, + "three distinct spaces": true, + "daytona published nothing after the session left": true, + "handovers closed": { + "daytona": true, + "vercel": true + }, + "diff of where.txt in each space": { + "daytona": { + "cached": true, + "lines": [ + "+on daytona" + ] + }, + "vercel": { + "cached": true, + "lines": [ + "+on vercel" + ] + }, + "host": { + "cached": false, + "lines": [ + "+on host" + ] + } + } +} \ No newline at end of file diff --git a/packages/harness/test/__artifacts__/daemon.git.vercel.wake.json b/packages/harness/test/__artifacts__/daemon.git.vercel.wake.json new file mode 100644 index 00000000..4c3f98ab --- /dev/null +++ b/packages/harness/test/__artifacts__/daemon.git.vercel.wake.json @@ -0,0 +1,30 @@ +{ + "wake": { + "borrowable after the handover closed": false, + "stopped": "offline", + "diff on a stopped sandbox": "served from the cache", + "diff with other context on a stopped sandbox": "detached", + "provider status after that call": "offline" + }, + "after": { + "step": "the next run wakes it and the daemon catches up", + "status": { + "head": "", + "available": true, + "files": [ + { + "file": "outside.txt", + "status": "added", + "additions": 1, + "deletions": 0 + }, + { + "file": "woken.txt", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + } + } +} \ No newline at end of file diff --git a/packages/harness/test/daemon.git.e2e.test.ts b/packages/harness/test/daemon.git.e2e.test.ts new file mode 100644 index 00000000..8f52cefc --- /dev/null +++ b/packages/harness/test/daemon.git.e2e.test.ts @@ -0,0 +1,124 @@ +import { expect, it } from "vite-plus/test"; +import * as Sandbox from "../src/effect/sandbox.ts"; +import * as DaytonaDriver from "../src/sandboxes/daytona/index.ts"; +import * as VercelDriver from "../src/sandboxes/vercel/index.ts"; +import { Effect } from "effect"; +import { execFileSync } from "node:child_process"; +import * as Session from "../src/effect/session.ts"; +import { gitScenario, PINNED, REPO, relinkScenario, run, statuses, withWorkspace } from "./fixtures/daemon.spec.ts"; +import { remoteSuite } from "./fixtures/live.ts"; +import { hasLiveOidc } from "./fixtures/vercel.ts"; +import "./utils/env.ts"; + +const TIMEOUT = 900_000; + +const gitScenarioClone = async (into: string) => { + execFileSync("git", ["clone", "-q", REPO, into], { stdio: "pipe" }); + execFileSync("git", ["-C", into, "checkout", "-q", "-B", "main", PINNED], { stdio: "pipe" }); +}; + +/* + * The git daemon on a real clone of codeworksh/69th, changed by real `bash` calls through the + * runner. Host and every remote sandbox record the same steps into one artifact; a remote one also + * shows that a stopped sandbox is never woken by the daemon. + */ +it( + "keeps a host space's git status current across runs, commits, branches and outside edits", + () => + withWorkspace(async (workspace) => { + const outcome = await gitScenario({})(workspace); + await expect(JSON.stringify(outcome.steps, null, "\t")).toMatchFileSnapshot( + "./__artifacts__/daemon.git.e2e.json", + ); + }), + TIMEOUT, +); + +it( + "two threads running in one space at once: both changes land in one settled status", + () => + withWorkspace(async (workspace) => { + const repo = `${workspace.root}/69th`; + await gitScenarioClone(repo); + const outcome = await run( + workspace, + [], + Effect.gen(function* () { + const git = yield* statuses; + const first = yield* Session.create({ directory: repo, hostDir: workspace.root }); + const second = yield* Session.create({ directory: repo, hostDir: workspace.root }); + yield* Effect.all( + [ + first.main.run("sleep 1 && printf 'one\\n' > from-first.txt"), + second.main.run("sleep 1 && printf 'two\\n' > from-second.txt"), + ], + { concurrency: "unbounded" }, + ); + const settled = yield* git.settle(undefined, (status) => status.files.length === 2); + return { + files: settled.files, + "statuses published": git.seen.length <= 4 ? "at most 4" : `${git.seen.length}`, + }; + }), + ); + await expect(JSON.stringify(outcome, null, "\t")).toMatchFileSnapshot( + "./__artifacts__/daemon.git.concurrent.json", + ); + }), + TIMEOUT, +); + +remoteSuite("DAYTONA_API_KEY", Boolean(process.env.DAYTONA_API_KEY?.trim()))("space daemons — daytona", () => { + it( + "keeps a daytona space's git status current and never wakes a stopped sandbox", + () => + withWorkspace(async (workspace) => { + const outcome = await gitScenario({ + drivers: [DaytonaDriver.make()], + provision: Sandbox.create({ driver: "daytona", config: { language: "typescript" } }), + })(workspace); + const shared = outcome.steps.slice(0, -1); + await expect(JSON.stringify(shared, null, "\t")).toMatchFileSnapshot("./__artifacts__/daemon.git.e2e.json"); + await expect( + JSON.stringify({ wake: outcome.wake, after: outcome.steps.at(-1) }, null, "\t"), + ).toMatchFileSnapshot("./__artifacts__/daemon.git.daytona.wake.json"); + }), + TIMEOUT, + ); +}); + +remoteSuite("VERCEL_OIDC_TOKEN", hasLiveOidc(process.env.VERCEL_OIDC_TOKEN))("space daemons — vercel", () => { + it( + "keeps a vercel space's git status current and never wakes a stopped sandbox", + () => + withWorkspace(async (workspace) => { + const outcome = await gitScenario({ + drivers: [VercelDriver.make()], + provision: Sandbox.create({ driver: "vercel", config: { runtime: "node24", timeout: 15 * 60 * 1000 } }), + })(workspace); + const shared = outcome.steps.slice(0, -1); + await expect(JSON.stringify(shared, null, "\t")).toMatchFileSnapshot("./__artifacts__/daemon.git.e2e.json"); + await expect( + JSON.stringify({ wake: outcome.wake, after: outcome.steps.at(-1) }, null, "\t"), + ).toMatchFileSnapshot("./__artifacts__/daemon.git.vercel.wake.json"); + }), + TIMEOUT, + ); +}); + +remoteSuite( + "VERCEL_OIDC_TOKEN and DAYTONA_API_KEY", + hasLiveOidc(process.env.VERCEL_OIDC_TOKEN) && Boolean(process.env.DAYTONA_API_KEY?.trim()), +)("space daemons — relink daytona → vercel → host", () => { + it( + "attributes each run to the space the session is on, and keeps every space's own state", + () => + withWorkspace(async (workspace) => { + const outcome = await relinkScenario([DaytonaDriver.make(), VercelDriver.make()])(workspace); + await expect(JSON.stringify(outcome, null, "\t")).toMatchFileSnapshot( + "./__artifacts__/daemon.git.relink.json", + ); + }), + TIMEOUT, + ); +}); diff --git a/packages/harness/test/fixtures/daemon.spec.ts b/packages/harness/test/fixtures/daemon.spec.ts new file mode 100644 index 00000000..9816609e --- /dev/null +++ b/packages/harness/test/fixtures/daemon.spec.ts @@ -0,0 +1,473 @@ +import { createAssistantMessageEventStream } from "@codeworksh/aikit"; +import { Effect, type Layer, type Scope } from "effect"; +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import * as DaemonHost from "../../src/daemon/host.ts"; +import * as Git from "../../src/daemon/git.ts"; +import * as Skills from "../../src/daemon/skills.ts"; +import * as Harness from "../../src/effect/harness.ts"; +import * as Sandbox from "../../src/effect/sandbox.ts"; +import * as Session from "../../src/effect/session.ts"; +import * as SandboxController from "../../src/sandbox/control.ts"; +import type { SandboxInstance } from "../../src/sandbox/instance.ts"; +import { SandboxIO } from "../../src/sandbox/io.ts"; +import * as Event from "../../src/event/event.ts"; +import type * as LLM from "../../src/runner/llm.ts"; +import { pendingCall } from "../tools.fixture.ts"; +import { assistant } from "./llm.ts"; +import { testModel } from "./model.ts"; + +/** The pinned commit of codeworksh/69th every run starts from. */ +export const PINNED = "cdaec6762e3502f6aa3bbf851e855c9ba12b3a6b"; +export const REPO = "https://github.com/codeworksh/69th"; + +/** + * A model that runs one `bash` command per prompt: the prompt text is the command. The first + * request of a prompt asks for the call; the request after its result answers and stops. + */ +export const bashModel = (): LLM.Open => { + let index = 0; + return (input) => + Effect.sync(() => { + index += 1; + const messages = input.context.messages; + const lastUser = messages.findLastIndex((message) => message.role === "user"); + const answered = messages.slice(lastUser + 1).some((message) => message.role === "assistant"); + const prompt = lastUser === -1 || answered ? undefined : messages[lastUser]; + const command = + prompt === undefined || prompt.role !== "user" + ? undefined + : prompt.parts + .filter((part) => part.type === "text") + .map((part) => part.text) + .join(""); + const message = + command === undefined + ? assistant(input, index) + : assistant(input, index, { + stopReason: "toolUse", + parts: [pendingCall("bash", { command }, `call_${index}`)], + }); + const stream = createAssistantMessageEventStream(); + stream.push({ type: "start", partial: message }); + stream.push({ type: "done", reason: command === undefined ? "stop" : "toolUse", message }); + return stream; + }); +}; + +export interface Workspace { + readonly root: string; + readonly home: string; +} + +/** A temp home with the test model selected; removed afterwards. */ +export const withWorkspace = async (use: (workspace: Workspace) => Promise) => { + const root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), "codework-daemon-e2e-"))); + const home = path.join(root, "home"); + await fs.mkdir(home, { recursive: true }); + await fs.writeFile(path.join(home, "settings.jsonc"), JSON.stringify({ model: testModel })); + try { + await use({ root, home }); + } finally { + await fs.rm(root, { recursive: true, force: true }); + } +}; + +export type Services = Layer.Success>; + +export const run = ( + workspace: Workspace, + drivers: ReadonlyArray, + program: Effect.Effect, +) => + Effect.runPromise( + program.pipe( + Effect.timeout("9 minutes"), + Effect.scoped, + Effect.provide( + Harness.layer({ + database: ":memory:", + home: workspace.home, + hostCwd: workspace.root, + sandboxes: drivers, + llm: bashModel(), + daemons: { handover: "5 seconds" }, + }), + ), + ), + ); + +type Status = Git.Status["data"]; + +/** + * Every `plugin.git.status` published, in order. `settle` returns the last one for a space once + * it satisfies `until` and nothing newer arrived for a quiet period: a run's refreshes (after each + * turn and when the drain lets go) can publish more than once, and only the settled one is stable. + */ +export const statuses = Effect.gen(function* () { + const events = yield* Event.Service; + const seen: Array = []; + const unlisten = yield* events.listen((event) => + Effect.sync(() => { + if (event.type === Git.Status.type) seen.push(event.data as Status); + }), + ); + yield* Effect.addFinalizer(() => unlisten); + const settle = (spaceId: string | undefined, until: (status: Status) => boolean, timeout = 120_000) => + Effect.gen(function* () { + const deadline = Date.now() + timeout; + for (;;) { + const mine = seen.filter((status) => spaceId === undefined || status.spaceId === spaceId); + const latest = mine[mine.length - 1]; + if (latest !== undefined && until(latest)) { + const count = mine.length; + yield* Effect.sleep("750 millis"); + const after = seen.filter((status) => spaceId === undefined || status.spaceId === spaceId); + if (after.length === count) return latest; + continue; + } + if (Date.now() > deadline) + return yield* Effect.die( + `no settled git status for ${spaceId ?? "any space"}; last: ${JSON.stringify(latest ?? null)}`, + ); + yield* Effect.sleep("100 millis"); + } + }); + return { seen, settle }; +}); + +type SkillList = Skills.List["data"]; + +/** Every `plugin.skills.list` published; `settle` like {@link statuses}. */ +export const skillLists = Effect.gen(function* () { + const events = yield* Event.Service; + const seen: Array = []; + const unlisten = yield* events.listen((event) => + Effect.sync(() => { + if (event.type === Skills.List.type) seen.push(event.data as SkillList); + }), + ); + yield* Effect.addFinalizer(() => unlisten); + const settle = (spaceId: string, until: (list: SkillList) => boolean) => + Effect.gen(function* () { + const deadline = Date.now() + 120_000; + for (;;) { + const mine = seen.filter((list) => list.spaceId === spaceId); + const latest = mine.at(-1); + if (latest !== undefined && until(latest)) { + yield* Effect.sleep("750 millis"); + if (seen.filter((list) => list.spaceId === spaceId).length === mine.length) return latest; + continue; + } + if (Date.now() > deadline) + return yield* Effect.die(`no settled skill list; last: ${JSON.stringify(latest ?? null)}`); + yield* Effect.sleep("100 millis"); + } + }); + return { settle }; +}); + +/** Calls the git daemon's `diff` for a space. */ +export const diff = (spaceId: string, input: typeof Git.DiffInput.Type = {}) => + Effect.gen(function* () { + const host = yield* DaemonHost.Service; + return (yield* host.call({ plugin: "git", spaceId, method: "diff", input })) as Git.DiffOutput; + }); + +/** A status without its per-run values: `observedAt`, the space id, commit ids made during the run. */ +export const stable = (status: Status, heads: Map) => { + // The message is git's own wording, which differs between versions; the reason is ours. + const { observedAt: _observedAt, spaceId: _spaceId, head, message: _message, ...rest } = status; + const label = + head === undefined + ? undefined + : head === PINNED + ? "" + : (heads.get(head) ?? (heads.set(head, ``), heads.get(head))); + return label === undefined ? rest : { head: label, ...rest }; +}; + +/** Where the scenario runs: the host sandbox, or a provisioned remote one. */ +export interface Target { + readonly drivers?: ReadonlyArray; + readonly provision?: ReturnType; +} + +const provisioned = (provision: ReturnType) => + Effect.tap(provision, (sandbox) => + Effect.addFinalizer(() => + Sandbox.stop(sandbox.id).pipe( + Effect.ignore, + Effect.andThen(Sandbox.destroy(sandbox.id)), + Effect.tapCause((cause) => Effect.logWarning(`sandbox ${sandbox.id} may have leaked`, cause)), + Effect.ignore, + ), + ), + ); + +/** Runs `script` in the target's sandbox (taking a lease, as a person editing there would). */ +const shell = (sandbox: SandboxInstance.ID | undefined, cwd: string | undefined, script: string) => + Effect.gen(function* () { + const controller = yield* SandboxController.Controller; + const result = yield* controller.withMount( + sandbox, + Effect.gen(function* () { + const exe = yield* SandboxIO.Shell; + return yield* exe.exec(script); + }), + cwd === undefined ? {} : { cwd }, + ); + if (result.exitCode !== 0) + return yield* Effect.die(`setup failed (${result.exitCode}): ${result.stderr}${result.stdout}`); + return result.stdout.trim(); + }); + +const commit = "git -c user.email=e2e@codework.sh -c user.name=e2e -c commit.gpgsign=false commit -q"; + +export interface Outcome { + readonly steps: ReadonlyArray<{ + readonly step: string; + readonly status?: unknown; + readonly diff?: unknown; + readonly skills?: unknown; + }>; + /** Remote targets only: what happened to a stopped sandbox when the daemon was asked to read it. */ + readonly wake?: Record; +} + +/** + * The git daemon against a real clone of codeworksh/69th, changed by real `bash` calls through the + * runner. Every target records the same steps, so a remote one that diverges from the host fails. + */ +export const gitScenario = (target: Target) => (workspace: Workspace) => + run( + workspace, + target.drivers ?? [], + Effect.gen(function* () { + const controller = yield* SandboxController.Controller; + const sandbox = target.provision === undefined ? undefined : yield* provisioned(target.provision); + const id = sandbox?.id; + const base = + id === undefined + ? workspace.root + : yield* shell(id, undefined, `dir=daemon-e2e-${Date.now()} && mkdir -p "$dir" && cd "$dir" && pwd`); + const repo = `${base}/69th`; + const plain = `${base}/plain`; + yield* shell( + id, + base, + `git clone -q ${REPO} 69th && git -C 69th checkout -q -B main ${PINNED} && mkdir -p plain 69th/sub`, + ); + const on = sandbox === undefined ? {} : { sandbox }; + + const git = yield* statuses; + const skills = yield* skillLists; + const heads = new Map(); + const steps: Array = []; + const record = (step: string, status: Status) => steps.push({ step, status: stable(status, heads) }); + const session = yield* Session.create({ ...on, directory: repo, hostDir: workspace.root }); + + yield* session.main.run("git status --short"); + const first = yield* git.settle(undefined, (status) => status.available && status.files.length === 0); + const spaceId = first.spaceId; + record("first run: clean clone", first); + + yield* session.main.run( + "printf 'hello from the daemon e2e\\n' > notes.md && printf 'one more line\\n' >> README.md && mkdir -p docs/deep && printf 'a\\nb\\nc\\n' > docs/deep/new.txt", + ); + record( + "edit, add and nest files", + yield* git.settle(spaceId, (status) => status.files.some((file) => file.file === "notes.md")), + ); + + const patches = yield* diff(spaceId, { files: ["README.md", "notes.md"] }); + steps.push({ + step: "diff of two files", + diff: patches.files.map((file) => ({ ...file, patch: file.patch.replace(/^index .*$/m, "index ") })), + }); + + yield* session.main.run(`git add -A && ${commit} -m 'e2e: notes'`); + record("commit everything", yield* git.settle(spaceId, (status) => status.files.length === 0)); + + yield* session.main.run("git checkout -q -b feature/e2e && git rm -q --cached README.md && rm README.md"); + record( + "branch, then delete a tracked file", + yield* git.settle(spaceId, (status) => status.branch === "feature/e2e"), + ); + + yield* session.main.run("git reset -q --hard && git checkout -q --detach"); + record( + "reset and detach HEAD", + yield* git.settle(spaceId, (status) => status.branch === undefined && status.files.length === 0), + ); + + const nested = yield* Session.create({ ...on, directory: `${repo}/sub`, hostDir: workspace.root }); + yield* nested.main.run("printf 'from a subfolder\\n' > 'here and there.txt'"); + record( + "a session in a subfolder: a new file in a new folder shows as the folder", + yield* git.settle(spaceId, (status) => status.files.some((file) => file.file === "sub/")), + ); + yield* nested.main.run("rm 'here and there.txt'"); + yield* git.settle(spaceId, (status) => status.files.length === 0); + + // A run that fills an untracked folder (an install with no .gitignore entry): one entry, not thousands. + yield* session.main.run( + "mkdir -p build-cache && for i in $(seq 1 2000); do echo x > build-cache/f$i.js; done", + ); + record( + "an untracked folder of 2000 files", + yield* git.settle(spaceId, (status) => status.files.some((file) => file.file === "build-cache/")), + ); + yield* session.main.run("rm -rf build-cache"); + yield* git.settle(spaceId, (status) => status.files.length === 0); + + // Changed outside any run, as an editor would: no event says so. The next mount catches it. + yield* shell(id, repo, "printf 'typed in an editor\\n' > outside.txt"); + yield* session.main.run("true"); + record( + "outside edit, picked up on the next run", + yield* git.settle(spaceId, (status) => status.files.some((file) => file.file === "outside.txt")), + ); + + yield* session.main.run( + "mkdir -p .agents/skills/deploy && printf -- '---\\nname: deploy\\ndescription: Deploy the 69th app.\\n---\\nSteps.\\n' > .agents/skills/deploy/SKILL.md", + ); + const added = yield* skills.settle(spaceId, (list) => list.skills.length > 0); + steps.push({ + step: "a skill added by a run", + skills: added.skills.map((skill) => ({ ...skill, path: skill.path.replace(repo, "") })), + }); + yield* session.main.run("rm -rf .agents"); + const removed = yield* skills.settle(spaceId, (list) => list.skills.length === 0); + steps.push({ step: "the skill removed by a run", skills: removed.skills }); + + const other = yield* Session.create({ ...on, directory: plain, hostDir: workspace.root }); + yield* other.main.run("true"); + record("a space that is not a repository", yield* git.settle(undefined, (status) => !status.available)); + + if (id === undefined) return { steps } satisfies Outcome; + + // Remote only: once the handover closed, a stopped sandbox is never woken by the daemon. + const wake: Record = {}; + const released = yield* Effect.gen(function* () { + for (let attempt = 0; attempt < 120; attempt++) { + if (!(yield* controller.held(id))) return true; + yield* Effect.sleep("500 millis"); + } + return false; + }); + wake["borrowable after the handover closed"] = !released; + wake["stopped"] = (yield* controller.stop(id)).status; + const host = yield* DaemonHost.Service; + wake["diff on a stopped sandbox"] = yield* host + .call({ plugin: "git", spaceId, method: "diff", input: {} }) + .pipe( + Effect.map((output) => + (output as Git.DiffOutput).cached === true ? "served from the cache" : "read live", + ), + Effect.catchTag("DaemonCallError", (error) => Effect.succeed(error.reason)), + ); + wake["diff with other context on a stopped sandbox"] = yield* host + .call({ plugin: "git", spaceId, method: "diff", input: { context: 10 } }) + .pipe( + Effect.map(() => "answered"), + Effect.catchTag("DaemonCallError", (error) => Effect.succeed(error.reason)), + ); + wake["provider status after that call"] = (yield* controller.refresh(id)).status; + yield* session.main.run("printf 'after a wake\\n' > woken.txt"); + record( + "the next run wakes it and the daemon catches up", + yield* git.settle(spaceId, (status) => status.files.some((file) => file.file === "woken.txt")), + ); + return { steps, wake } satisfies Outcome; + }), + ); + +/** + * One session relinked across spaces: a Daytona clone, then a Vercel clone, then a host clone, a + * change made by a run in each. Every space keeps its own status, a space the session left never + * picks up a later change, and `diff` answers per space: from the cache for the sandboxes it left, + * live on the host. + */ +export const relinkScenario = (drivers: ReadonlyArray) => (workspace: Workspace) => + run( + workspace, + drivers, + Effect.gen(function* () { + const daytona = yield* provisioned(Sandbox.create({ driver: "daytona", config: { language: "typescript" } })); + const vercel = yield* provisioned( + Sandbox.create({ driver: "vercel", config: { runtime: "node24", timeout: 15 * 60 * 1000 } }), + ); + const clone = (id: SandboxInstance.ID | undefined, base: string) => + shell(id, base, `git clone -q ${REPO} 69th && git -C 69th checkout -q -B main ${PINNED}`).pipe( + Effect.as(`${base}/69th`), + ); + const base = (id: SandboxInstance.ID) => + shell(id, undefined, `dir=daemon-relink-${Date.now()} && mkdir -p "$dir" && cd "$dir" && pwd`); + const onDaytona = yield* clone(daytona.id, yield* base(daytona.id)); + const onVercel = yield* clone(vercel.id, yield* base(vercel.id)); + const onHost = yield* clone(undefined, workspace.root); + + const git = yield* statuses; + const heads = new Map(); + const record: Record = {}; + const settleIn = (spaceId: string, file: string) => + git.settle(spaceId, (status) => status.files.some((entry) => entry.file === file)); + const seenFor = (spaceId: string) => git.seen.filter((status) => status.spaceId === spaceId).length; + + const session = yield* Session.create({ sandbox: daytona, directory: onDaytona, hostDir: workspace.root }); + const inDaytona = (yield* session.info).spaceId; + yield* session.main.run("printf 'on daytona\\n' > where.txt"); + record["daytona after its run"] = stable(yield* settleIn(inDaytona, "where.txt"), heads); + const daytonaPublished = seenFor(inDaytona); + + // Changed after the session left: no drain mounts Daytona again, so its daemon must not publish. + yield* shell(daytona.id, onDaytona, "printf 'after the session left\\n' > left.txt"); + const toVercel = yield* Session.relink({ sessionId: session.id, sandbox: vercel, directory: onVercel }); + const inVercel = (yield* toVercel.info).spaceId; + yield* toVercel.main.run("printf 'on vercel\\n' > where.txt && printf 'from vercel\\n' >> README.md"); + record["vercel after its run"] = stable(yield* settleIn(inVercel, "README.md"), heads); + + const toHost = yield* Session.relink({ sessionId: session.id, directory: onHost }); + const inHost = (yield* toHost.info).spaceId; + yield* toHost.main.run("printf 'on host\\n' > where.txt && git rm -q README.md"); + record["host after its run"] = stable( + yield* git.settle( + inHost, + (status) => status.files.some((entry) => entry.file === "where.txt") && status.files.length === 2, + ), + heads, + ); + + record["three distinct spaces"] = new Set([inDaytona, inVercel, inHost]).size === 3; + record["daytona published nothing after the session left"] = seenFor(inDaytona) === daytonaPublished; + + // The sandboxes the session left stay borrowable for their handover; once it closes, `diff` + // answers from the cache. + const controller = yield* SandboxController.Controller; + const released = (id: SandboxInstance.ID) => + Effect.gen(function* () { + for (let attempt = 0; attempt < 120 && (yield* controller.held(id)); attempt++) + yield* Effect.sleep("500 millis"); + return !(yield* controller.held(id)); + }); + record["handovers closed"] = { daytona: yield* released(daytona.id), vercel: yield* released(vercel.id) }; + const patch = (spaceId: string) => + diff(spaceId, { files: ["where.txt"] }).pipe( + Effect.map((output) => ({ + cached: output.cached === true, + lines: output.files.flatMap((file) => + file.patch.split("\n").filter((line) => line.startsWith("+") && !line.startsWith("+++")), + ), + })), + Effect.catchTag("DaemonCallError", (error) => Effect.succeed({ error: error.reason })), + ); + record["diff of where.txt in each space"] = { + daytona: yield* patch(inDaytona), + vercel: yield* patch(inVercel), + host: yield* patch(inHost), + }; + return record; + }), + ); diff --git a/packages/plugin/package.json b/packages/plugin/package.json index 0ecf9243..3fcc5355 100644 --- a/packages/plugin/package.json +++ b/packages/plugin/package.json @@ -53,6 +53,12 @@ "import": "./dist/pack/plugin/prompt.mjs", "default": "./dist/pack/plugin/prompt.mjs" }, + "./plugin/daemon": { + "development": "./src/plugin/daemon.ts", + "types": "./dist/pack/plugin/daemon.d.mts", + "import": "./dist/pack/plugin/daemon.mjs", + "default": "./dist/pack/plugin/daemon.mjs" + }, "./plugin/mcp": { "development": "./src/plugin/mcp.ts", "types": "./dist/pack/plugin/mcp.d.mts", diff --git a/packages/plugin/src/plugin.ts b/packages/plugin/src/plugin.ts index 70df15a6..3aafbefe 100644 --- a/packages/plugin/src/plugin.ts +++ b/packages/plugin/src/plugin.ts @@ -8,6 +8,7 @@ */ export type { Config, Events, Paths, PromptResolver, SharedPluginContext } from "./plugin/context.ts"; export { define, domains, type Mount, type Plugin, type PluginKind, rank } from "./plugin/plugin.ts"; +export * as Daemon from "./plugin/daemon.ts"; export * as Mcp from "./plugin/mcp.ts"; export * as Prompt from "./plugin/prompt.ts"; export * as Section from "./plugin/section.ts"; diff --git a/packages/plugin/src/plugin/daemon.ts b/packages/plugin/src/plugin/daemon.ts new file mode 100644 index 00000000..0ab76989 --- /dev/null +++ b/packages/plugin/src/plugin/daemon.ts @@ -0,0 +1,125 @@ +/** + * Daemon plugins: long-running workers the harness starts per scope, instead of setting up per turn. + * + * Only the `space` scope exists today. A space daemon has one instance per space (one folder in one + * env), started on the first drain that mounts the space and stopped after it has been idle. It + * reacts to what runs in the space, reads the space through I/O it can only borrow while something + * else holds the sandbox, and publishes what it found as its own events. + */ +import type { Duration, Effect, Schema, Scope } from "effect"; +import { Schema as S } from "effect"; +import type { Data, Definition, Payload } from "../event.ts"; +import type { Interface as FileSystem } from "../sandbox/filesystem.ts"; +import type { ISandboxExe as Shell } from "../sandbox/shell.ts"; +import type { PluginOptions } from "./ref.ts"; + +/** The space has no lease held: reading it would wake a sandbox, so the read is refused. */ +export class Detached extends S.TaggedError()("Daemon.Detached", { + spaceId: S.String, +}) {} + +/** A borrowed read ran past its budget and was cut off. */ +export class BudgetExceeded extends S.TaggedError()("Daemon.BudgetExceeded", { + spaceId: S.String, + budget: S.String, +}) {} + +/** How a daemon's own code may fail: a tagged error or an `Error`. */ +export type Failure = Error | { readonly _tag: string }; + +export interface SpaceInfo { + /** `hash(env, location)`. */ + readonly id: string; + readonly projectId: string; + /** The space's folder: every borrowed read runs relative to it. */ + readonly location: string; + readonly placement: { readonly kind: "local" } | { readonly kind: "remote"; readonly driver: string }; +} + +export interface SpaceIO { + /** Whether a read right now would be allowed. */ + readonly attached: Effect.Effect; + /** + * Runs `use` with the space's filesystem and shell. Never wakes a sandbox: it fails with + * {@link Detached} unless a drain holds the sandbox or the harness opened a handover after one, + * and it is cut off with {@link BudgetExceeded} after `budget` (default 20 seconds). + */ + readonly use: ( + use: (io: { readonly fs: FileSystem; readonly shell: Shell }) => Effect.Effect, + options?: { readonly budget?: Duration.Input }, + ) => Effect.Effect; +} + +/** A single-flight job with a dirty flag, created by {@link SpaceContext.work}. */ +export interface Work { + /** + * Ask for a run. Runs now when idle and the space is readable, once more after the current run + * when one is in flight (any number of marks make one rerun), and on the space's next mount when + * it is not readable. + */ + readonly mark: Effect.Effect; +} + +/** A method clients call through `daemon.call`: its input and output, validated by the harness. */ +export interface Method< + I extends Schema.Codec = Schema.Codec, + O extends Schema.Codec = Schema.Codec, +> { + readonly input: I; + readonly output: O; +} + +export type Methods = Readonly>; + +export interface SpaceContext { + readonly plugin: { readonly id: string; readonly options: PluginOptions }; + readonly space: SpaceInfo; + readonly io: SpaceIO; + readonly events: { + /** + * Mount events of this space (`space.mount.attached`, `space.mount.detached`) and the session + * events of threads while they run here. Handlers run one at a time per daemon; the + * subscription ends with the instance. + */ + readonly on: ( + definitions: D | ReadonlyArray, + handler: (event: Payload) => Effect.Effect, + ) => Effect.Effect; + /** + * One of the plugin's own `events`. The harness fills `spaceId` and `observedAt`, keeps the + * event as this space's last of its type (replayed to clients that subscribe later), and drops + * one that does not encode. + */ + readonly publish: ( + definition: D, + data: Omit, "spaceId" | "observedAt">, + ) => Effect.Effect; + }; + /** A single-flight job for `run`; runs in the instance's scope and stops with it. */ + readonly work: (name: string, run: Effect.Effect) => Effect.Effect; + /** The handler for one of the plugin's declared `methods`. */ + readonly handle: ( + method: K, + handler: (input: Schema.Schema.Type) => Effect.Effect, E>, + ) => Effect.Effect; +} + +export interface DaemonPlugin { + readonly id: string; + readonly kind: "daemon"; + readonly scope: "space"; + /** + * Events this daemon publishes, namespaced `plugin..*`. Each carries `spaceId` and + * `observedAt`, which the harness fills. + */ + readonly events?: ReadonlyArray; + /** Methods clients call through `daemon.call`. */ + readonly methods?: M; + /** + * Runs once per instance: registers subscriptions, work and handlers, then returns. The instance + * keeps running until its scope closes. + */ + readonly start: (ctx: SpaceContext) => Effect.Effect; +} + +export const define = (daemon: DaemonPlugin): DaemonPlugin => daemon; diff --git a/packages/plugin/src/sandbox/errors.ts b/packages/plugin/src/sandbox/errors.ts index 61a7baee..96068645 100644 --- a/packages/plugin/src/sandbox/errors.ts +++ b/packages/plugin/src/sandbox/errors.ts @@ -56,6 +56,11 @@ export class SandboxBusyError extends Schema.TaggedError()("Sa refCount: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)), }) {} +/** No drain holds the sandbox and no handover is open: borrowing its I/O would have to attach, which can wake it. */ +export class SandboxNotHeldError extends Schema.TaggedError()("SandboxNotHeldError", { + id: SandboxInstance.ID, +}) {} + export class SandboxMustBeStoppedError extends Schema.TaggedError()( "SandboxMustBeStoppedError", { diff --git a/packages/plugin/vite.config.ts b/packages/plugin/vite.config.ts index d0454f7c..15ecb5cf 100644 --- a/packages/plugin/vite.config.ts +++ b/packages/plugin/vite.config.ts @@ -28,6 +28,7 @@ export default defineConfig({ "src/index.ts", "src/location.ts", "src/plugin.ts", + "src/plugin/daemon.ts", "src/plugin/mcp.ts", "src/plugin/prompt.ts", "src/plugin/section.ts",