diff --git a/apps/contract/src/contract.ts b/apps/contract/src/contract.ts index 6af025d5..f0198831 100644 --- a/apps/contract/src/contract.ts +++ b/apps/contract/src/contract.ts @@ -116,6 +116,92 @@ export const SpaceSkills = Schema.Struct({ }); export type SpaceSkills = typeof SpaceSkills.Type; +const ChangeStatus = Schema.Literals(["added", "deleted", "modified"]); + +/** A changed file in a space's working tree. A wholly untracked folder is one `dir/` entry with no counts. */ +export const ChangedFile = Schema.Struct({ + file: Schema.String, + status: ChangeStatus, + additions: Schema.Int, + deletions: Schema.Int, +}); +export type ChangedFile = typeof ChangedFile.Type; + +/** + * A space's working-tree changes against `HEAD`, as its git daemon last read them: after each run in + * the space, so a stopped sandbox shows its last read. `observedAt` is when, in epoch milliseconds. + */ +export const SpaceChanges = Schema.Struct({ + observedAt: Schema.Finite, + available: Schema.Boolean, + /** Absent before the first commit. */ + head: Schema.optionalKey(Schema.String), + /** Absent on a detached HEAD. */ + branch: Schema.optionalKey(Schema.String), + files: Schema.Array(ChangedFile), + reason: Schema.optionalKey(Schema.Literals(["not-a-repository", "git-missing", "failed"])), + message: Schema.optionalKey(Schema.String), +}); +export type SpaceChanges = typeof SpaceChanges.Type; + +/** One file's patch: a `diff --git` chunk with 3 lines of context. */ +export const FileDiff = Schema.Struct({ + file: Schema.String, + /** Empty when the file was left out (past the caps): read it alone with `files`. */ + patch: Schema.String, + status: ChangeStatus, + additions: Schema.Int, + deletions: Schema.Int, +}); +export type FileDiff = typeof FileDiff.Type; + +export const SpaceDiff = Schema.Struct({ + files: Schema.Array(FileDiff), + /** Files were left out: past the daemon's caps (10 MB of patches, 1 MB cached; a 5 MB untracked file), or past 200 untracked files. */ + truncated: Schema.optionalKey(Schema.Literal(true)), + /** The space is not mounted: these are the patches read after its last run, as old as its changes. */ + cached: Schema.optionalKey(Schema.Literal(true)), +}); +export type SpaceDiff = typeof SpaceDiff.Type; + +/** A thread working in a space: its session's, or one placed there. */ +export const SpaceThread = Schema.Struct({ + id: Schema.String, + sessionId: Schema.String, + /** Its session's main thread, whose address is the session's. */ + main: Schema.Boolean, + /** Its own, else its nearest titled ancestor's; absent while none has one. */ + title: Schema.optionalKey(Schema.String), + /** Its session's name: the main thread's title, else the session's slug. */ + sessionName: Schema.String, + runState: RunState, + /** Epoch milliseconds. */ + updatedAt: Schema.Finite, +}); +export type SpaceThread = typeof SpaceThread.Type; + +/** Where a space's changes are, and whose they can be: every thread there shares its working tree. */ +export const ChangesSpace = Schema.Struct({ + location: Schema.String, + kind: SpaceInfo.fields.kind, + /** Its sandbox, as the machine's store has it: null for the host, empty when the machine did not answer. */ + env: Schema.NullOr( + Schema.Struct({ driver: Schema.optionalKey(Schema.String), status: Schema.optionalKey(Schema.String) }), + ), + /** Newest activity first; subagents left out. */ + threads: Schema.Array(SpaceThread), +}); +export type ChangesSpace = typeof ChangesSpace.Type; + +/** + * The patches could not be read: no git daemon runs in the space yet (`unavailable`, until a run + * mounts it), it is not mounted and nothing was kept (`detached`), or git failed. + */ +export class ChangesError extends Schema.TaggedError()("Server.ChangesError", { + reason: Schema.Literals(["unavailable", "detached", "failed"]), + message: Schema.String, +}) {} + /** A host folder operation that failed: unreadable, not a directory, taken, or git/gh refused. */ export class HostError extends Schema.TaggedError()("Server.HostError", { message: Schema.String, @@ -547,6 +633,26 @@ export const Api = RpcGroup.make( payload: { machineId: Schema.String, projectId: Schema.String }, success: Schema.Array(SpaceSkills), }), + /** A space's changes from the mirror of its git daemon; null when it was never read. Never wakes a sandbox. */ + Rpc.make("changes.status", { + payload: { machineId: Schema.String, spaceId: Schema.String }, + success: Schema.NullOr(SpaceChanges), + }), + /** A space's folder, environment and threads, from the app's own rows and the machine's store; null when unknown. Never wakes a sandbox. */ + Rpc.make("changes.space", { + payload: { machineId: Schema.String, spaceId: Schema.String }, + success: Schema.NullOr(ChangesSpace), + }), + /** A space's patches, from its git daemon on the space's machine: all files, or `files`. Never wakes a sandbox. */ + Rpc.make("changes.diff", { + payload: { + machineId: Schema.String, + spaceId: Schema.String, + files: Schema.optionalKey(Schema.Array(Schema.String)), + }, + success: SpaceDiff, + error: Schema.Union([CodeworkUnavailable, ChangesError]), + }), Rpc.make("sidebar.list", { payload: { organizationId: Schema.String, machines: Schema.Literals(["default", "all"]) }, success: Schema.Array(SidebarProject), diff --git a/apps/server/src/changes.ts b/apps/server/src/changes.ts new file mode 100644 index 00000000..0d7b9fc7 --- /dev/null +++ b/apps/server/src/changes.ts @@ -0,0 +1,134 @@ +import { + type ChangesSpace, + ChangesError, + type CodeworkUnavailable, + SpaceChanges, + SpaceDiff, + type SpaceThread, +} from "@codeworksh/contract"; +import { Codework as Client } from "@codeworksh/widget/backend"; +import { Effect, Option, Schema } from "effect"; +import { SqlClient } from "effect/sql"; +import { Codework } from "./codework.ts"; +import { Reads } from "./reads.ts"; + +// The daemon's Status event as it came: its `spaceId` is dropped, being the row's key. +const decodeStored = Schema.decodeUnknownOption(Schema.fromJsonString(SpaceChanges)); +const decodeDiff = Schema.decodeUnknownEffect(SpaceDiff); + +/** A space's changes as its git daemon last published them, mirrored from CodeWork's event stream. */ +export const status = Effect.fn("Changes.status")(function* (input: { + readonly machineId: string; + readonly spaceId: string; +}) { + const sql = yield* SqlClient.SqlClient; + const [row] = yield* sql<{ data: string }>` + SELECT data FROM machine_space_state + WHERE machine_id = ${input.machineId} AND space_id = ${input.spaceId} AND type = 'plugin.git.status' + `.pipe(Effect.orDie); + return row === undefined ? null : Option.getOrNull(decodeStored(row.data)); +}); + +/** + * The space a changes panel shows and the threads working in it, from the app's own rows; a sandbox's + * driver and status come from the machine's store, which wakes nothing. + */ +export const space = Effect.fn("Changes.space")(function* ( + input: { readonly machineId: string; readonly spaceId: string }, + machine: Effect.Effect, +) { + const sql = yield* SqlClient.SqlClient; + const [row] = yield* sql & { env: string | null }>` + SELECT location, kind, env FROM space WHERE machine_id = ${input.machineId} AND id = ${input.spaceId} + `.pipe(Effect.orDie); + if (row === undefined) return null; + // Whole sessions, so a fork placed here still reads its title from an ancestor placed elsewhere. + const rows = yield* sql<{ + id: string; + sessionId: string; + originKind: string; + originThreadId: string | null; + title: string | null; + runState: SpaceThread["runState"]; + updatedAt: number; + slug: string; + spaceId: string; + }>` + SELECT thread.id, thread.session_id, thread.origin_kind, thread.origin_thread_id, thread.title, + thread.run_state, thread.updated_at, session.slug, + COALESCE(thread.placement_space_id, session.space_id) AS space_id + FROM thread JOIN session ON session.id = thread.session_id + WHERE session.machine_id = ${input.machineId} AND session.deleted_at IS NULL AND thread.deleted_at IS NULL + AND thread.session_id IN ( + SELECT placed.session_id FROM thread AS placed JOIN session AS home ON home.id = placed.session_id + WHERE home.machine_id = ${input.machineId} + AND COALESCE(placed.placement_space_id, home.space_id) = ${input.spaceId} + ) + ORDER BY thread.updated_at DESC, thread.id + `.pipe(Effect.orDie); + const titleOf = Reads.titles(rows); + const mains = new Map( + rows.filter((thread) => thread.originKind === "main").map((thread) => [thread.sessionId, thread]), + ); + const sandbox = + row.env === null + ? undefined + : (yield* machine.pipe( + Effect.flatMap((client) => client("sandbox.list", {})), + Effect.timeout("10 seconds"), + Effect.orElseSucceed(() => []), + )).find((info) => info.id === row.env); + return { + location: row.location, + kind: row.kind, + env: row.env === null ? null : sandbox === undefined ? {} : { driver: sandbox.driver, status: sandbox.status }, + threads: rows + .filter((thread) => thread.spaceId === input.spaceId && thread.originKind !== "subagent") + .map((thread): SpaceThread => { + const title = titleOf(thread); + return { + id: thread.id, + sessionId: thread.sessionId, + main: thread.originKind === "main", + ...(title === undefined ? {} : { title }), + sessionName: mains.get(thread.sessionId)?.title ?? thread.slug, + runState: thread.runState, + updatedAt: thread.updatedAt, + }; + }), + } satisfies ChangesSpace; +}); + +/** A space's patches from its git daemon, over the connection to the space's machine. */ +export const diff = Effect.fn("Changes.diff")(function* ( + input: { readonly spaceId: string; readonly files?: ReadonlyArray | undefined }, + machine: Effect.Effect, +) { + const client = yield* machine; + const reply = yield* Codework.call( + (rpc) => + rpc("daemon.call", { + plugin: "git", + spaceId: input.spaceId, + method: "diff", + input: input.files === undefined ? {} : { files: input.files }, + }).pipe( + Effect.map((output) => ({ output })), + Effect.catchTag("DaemonCallError", (error) => Effect.succeed({ failed: error })), + ), + // Past the daemon's own 60-second budget, so its failure is the one that shows. + { timeout: "70 seconds" }, + ).pipe(Effect.provideService(Client, client)); + if ("failed" in reply) { + const { reason, message } = reply.failed; + return yield* new ChangesError({ + reason: reason === "unavailable" || reason === "detached" ? reason : "failed", + message, + }); + } + return yield* decodeDiff(reply.output).pipe( + Effect.mapError((error) => new ChangesError({ reason: "failed", message: error.message })), + ); +}); + +export * as Changes from "./changes.ts"; diff --git a/apps/server/src/handlers.ts b/apps/server/src/handlers.ts index 644b5560..52e24bdb 100644 --- a/apps/server/src/handlers.ts +++ b/apps/server/src/handlers.ts @@ -1,6 +1,7 @@ import type * as Mirror from "@codeworksh/widget/codework"; import { Effect, Predicate, Stream } from "effect"; import { SqlClient } from "effect/sql"; +import { Changes } from "./changes.ts"; import { Cli } from "./cli.ts"; import { Codework } from "./codework.ts"; import { @@ -99,6 +100,10 @@ export const layer = Contract.Api.toLayer( "projects.spaces": (input) => Reads.spaces(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)), "projects.skills": (input) => Skills.list(input, sync.client(input.machineId)).pipe(Effect.provideService(SqlClient.SqlClient, sql)), + "changes.status": (input) => Changes.status(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)), + "changes.space": (input) => + Changes.space(input, sync.client(input.machineId)).pipe(Effect.provideService(SqlClient.SqlClient, sql)), + "changes.diff": (input) => Changes.diff(input, sync.client(input.machineId)), "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/reads.ts b/apps/server/src/reads.ts index 57323e5d..b411a0cc 100644 --- a/apps/server/src/reads.ts +++ b/apps/server/src/reads.ts @@ -149,6 +149,26 @@ const SPLIT = { grouping: "thread.grouping_id, thread.ordering", } satisfies Record; +interface Titled { + readonly id: string; + readonly title: string | null; + readonly originThreadId: string | null; +} + +/** Each thread's title among `rows`: an untitled fork reads as its nearest titled ancestor, as its line does. */ +export const titles = (rows: ReadonlyArray) => { + const byId = new Map(rows.map((row) => [row.id, row])); + return (row: Row) => { + const seen = new Set(); + for (let at: Row | undefined = row; at !== undefined && !seen.has(at.id);) { + if (at.title !== null) return at.title; + seen.add(at.id); + at = at.originThreadId === null ? undefined : byId.get(at.originThreadId); + } + return undefined; + }; +}; + export const threads = Effect.fn("Reads.threads")(function* ({ organizationId, by, @@ -190,23 +210,13 @@ export const threads = Effect.fn("Reads.threads")(function* ({ SELECT id, organization_id, name, color, ordering FROM grouping WHERE organization_id = ${organizationId} ORDER BY ordering `; - // An untitled fork reads as its nearest titled ancestor, as its line does. - const byId = new Map(rows.map((row) => [row.id, row])); - const titleOf = (row: (typeof rows)[number]) => { - const seen = new Set(); - for (let at: typeof row | undefined = row; at !== undefined && !seen.has(at.id);) { - if (at.title !== null) return at.title; - seen.add(at.id); - at = at.originThreadId === null ? undefined : byId.get(at.originThreadId); - } - return UNTITLED; - }; + const titleOf = titles(rows); const listed = rows.map((row): ListedThread => ({ id: row.id, sessionId: row.sessionId, originKind: row.originKind, originThreadId: row.originThreadId, - title: titleOf(row), + title: titleOf(row) ?? UNTITLED, machine: { id: row.machineId, name: row.machineName }, project: { id: row.projectId, name: row.projectName }, runState: row.runState, @@ -307,6 +317,7 @@ const head = (sql: SqlClient.SqlClient, { sessionId, threadId }: LineRequest) => id: string; sessionId: string; machineId: string; + spaceId: string; title: string | null; modelProvider: string | null; modelId: string | null; @@ -325,7 +336,7 @@ const head = (sql: SqlClient.SqlClient, { sessionId, threadId }: LineRequest) => projectName: string; removed: number; }>` - SELECT thread.id, thread.session_id, session.machine_id, thread.title, thread.model_provider, thread.model_id, + SELECT thread.id, thread.session_id, session.machine_id, session.space_id, thread.title, thread.model_provider, thread.model_id, thread.thinking_level, thread.run_state, thread.run_started_at, thread.run_ended_at, thread.run_outcome, thread.run_error, thread.parked, thread.updated_at, thread.floor, session.dir, session.hostdir, project.id AS project_id, project.name AS project_name, @@ -408,6 +419,7 @@ export const line = Effect.fn("Reads.line")(function* (request: LineRequest) { id: row.id, sessionId: row.sessionId, machineId: row.machineId, + spaceId: row.spaceId, ...(titled === undefined ? {} : { title: titled.title }), ...model(row.modelProvider, row.modelId), ...(row.thinkingLevel === null ? {} : { thinkingLevel: row.thinkingLevel }), diff --git a/apps/server/test/__artifacts__/changes.json b/apps/server/test/__artifacts__/changes.json new file mode 100644 index 00000000..f2d440e4 --- /dev/null +++ b/apps/server/test/__artifacts__/changes.json @@ -0,0 +1,114 @@ +{ + "changes.status before any run": null, + "changes.status after a run edited README.md and added notes.txt": { + "available": true, + "head": "cdaec6762e3502f6aa3bbf851e855c9ba12b3a6b", + "branch": "main", + "files": [ + { + "file": "README.md", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "notes.txt", + "status": "added", + "additions": 2, + "deletions": 0 + } + ], + "observed": true + }, + "changes.diff": { + "files": [ + { + "file": "README.md", + "patch": "diff --git a/README.md b/README.md\nindex e91ebc3..02297c0 100644\n--- a/README.md\n+++ b/README.md\n@@ -1,2 +1,3 @@\n # 69th\n 69th Testing Repository for Agents\n+Edited by a run.\n", + "status": "modified", + "additions": 1, + "deletions": 0 + }, + { + "file": "notes.txt", + "patch": "diff --git a/notes.txt b/notes.txt\nnew file mode 100644\nindex 0000000..94954ab\n--- /dev/null\n+++ b/notes.txt\n@@ -0,0 +1,2 @@\n+hello\n+world\n", + "status": "added", + "additions": 2, + "deletions": 0 + } + ] + }, + "changes.diff of notes.txt": { + "files": [ + { + "file": "notes.txt", + "patch": "diff --git a/notes.txt b/notes.txt\nnew file mode 100644\nindex 0000000..94954ab\n--- /dev/null\n+++ b/notes.txt\n@@ -0,0 +1,2 @@\n+hello\n+world\n", + "status": "added", + "additions": 2, + "deletions": 0 + } + ] + }, + "threads.line carries the session's space": true, + "changes.space of the host folder with its one thread": { + "location": "", + "kind": "primary", + "env": null, + "threads": [ + { + "id": "", + "sessionId": "", + "main": true, + "title": "bash printf 'Edited by a run.\\n' >> README.md…", + "sessionName": "bash printf 'Edited by a run.\\n' >> README.md…", + "runState": "idle", + "updated": true + } + ] + }, + "changes.space after a second session in the same folder": { + "location": "", + "kind": "primary", + "env": null, + "threads": [ + { + "id": "", + "sessionId": "", + "main": true, + "sessionName": "", + "runState": "idle", + "updated": true + }, + { + "id": "", + "sessionId": "", + "main": true, + "title": "bash printf 'Edited by a run.\\n' >> README.md…", + "sessionName": "bash printf 'Edited by a run.\\n' >> README.md…", + "runState": "idle", + "updated": true + } + ] + }, + "the second session shares the space": true, + "changes.space of an unknown space": null, + "changes.status of a space no run mounted": null, + "changes.diff of a local space no run mounted": { + "files": [ + { + "file": "idea.txt", + "patch": "diff --git a/idea.txt b/idea.txt\nnew file mode 100644\nindex 0000000..5626abf\n--- /dev/null\n+++ b/idea.txt\n@@ -0,0 +1 @@\n+one\n", + "status": "added", + "additions": 1, + "deletions": 0 + } + ] + }, + "changes.status after a run reverted everything": { + "available": true, + "head": "cdaec6762e3502f6aa3bbf851e855c9ba12b3a6b", + "branch": "main", + "files": [], + "observed": true + } +} diff --git a/apps/server/test/__artifacts__/reads.line.json b/apps/server/test/__artifacts__/reads.line.json index 7ed3dde5..158e8734 100644 --- a/apps/server/test/__artifacts__/reads.line.json +++ b/apps/server/test/__artifacts__/reads.line.json @@ -4,19 +4,20 @@ "id": "", "sessionId": "", "machineId": "", + "spaceId": "", "title": "Fork prompt", "runState": "idle", "parked": false, "updatedAt": "