Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions apps/contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChangesError>()("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<HostError>()("Server.HostError", {
message: Schema.String,
Expand Down Expand Up @@ -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),
Expand Down
134 changes: 134 additions & 0 deletions apps/server/src/changes.ts
Original file line number Diff line number Diff line change
@@ -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<typeof Client.Service, CodeworkUnavailable>,
) {
const sql = yield* SqlClient.SqlClient;
const [row] = yield* sql<Pick<ChangesSpace, "location" | "kind"> & { 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<string> | undefined },
machine: Effect.Effect<typeof Client.Service, CodeworkUnavailable>,
) {
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";
5 changes: 5 additions & 0 deletions apps/server/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)),

Expand Down
38 changes: 25 additions & 13 deletions apps/server/src/reads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ const SPLIT = {
grouping: "thread.grouping_id, thread.ordering",
} satisfies Record<ThreadsBy, string>;

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 = <Row extends Titled>(rows: ReadonlyArray<Row>) => {
const byId = new Map(rows.map((row) => [row.id, row]));
return (row: Row) => {
const seen = new Set<string>();
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,
Expand Down Expand Up @@ -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<string>();
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,
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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 }),
Expand Down
Loading