Skip to content
Merged
  •  
  •  
  •  
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ This file is the canonical source for unreleased changes and published release n

## [Unreleased]

### Changed

- `vp run knip:check` reports unused exports in `packages/aikit`, `packages/codework`, `packages/harness` and `packages/plugin` as well as the apps. Modules other than a package's entry points no longer re-export themselves as a namespace (`export * as X from "./x.ts"`); their importers use `import * as X`, so knip sees which members are used.

### Removed

- Internal exports nothing imported, across aikit, codework, harness and plugin. Those also unused inside their own module are deleted, among them harness's `PluginStore.remove` and `PluginStore.all`, the `ToolRegistry` service and its layer, `Space.layerWith`, `Worktree.layerWith` and `Worktree.defaultLayer`. Package entry points are unchanged except as below.
- `@codeworksh/harness/effect`: `Global.defaultLayer`, a duplicate of `Global.layer`.

## [@codeworksh/desktop@0.4.0] - 2026-11-06

### Added
Expand Down
95 changes: 94 additions & 1 deletion apps/contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const SidebarSession = Schema.Struct({
/** Its main thread. */
threadId: Schema.String,
spaceKind: SpaceInfo.fields.kind,
/** Hex; null is the default. */
/** Its main thread's color, else its own, as All Threads shows it; hex, null is the default. */
color: Schema.NullOr(Schema.String),
/** The main thread's title, or what an untitled one is called. */
title: Schema.String,
Expand Down Expand Up @@ -77,11 +77,27 @@ export const SidebarProject = Schema.Struct({
});
export type SidebarProject = typeof SidebarProject.Type;

/** One of a project's directories: its main checkout, a worktree, a copy or a plain folder. */
export const ProjectSpace = Schema.Struct({
id: Schema.String,
location: Schema.String,
kind: SpaceInfo.fields.kind,
/** The sandbox it is in; null for the host. */
env: Schema.NullOr(Schema.String),
status: SpaceInfo.fields.status,
});
export type ProjectSpace = typeof ProjectSpace.Type;

/** 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,
}) {}

/** A project operation that failed, such as renaming one the server no longer has. */
export class ProjectError extends Schema.TaggedError<ProjectError>()("Server.ProjectError", {
message: Schema.String,
}) {}

export { Clone, ClonePhase, CloneStage, Folder, Repository };

/** The models could not be listed: a settings file or the model catalog, at `path`, is broken. */
Expand All @@ -91,6 +107,44 @@ export class ModelsError extends Schema.TaggedError<ModelsError>()("Server.Model
message: Schema.String,
}) {}

/** A settings file the runtime would reject, at `path`; nothing was written. */
export class SettingsError extends Schema.TaggedError<SettingsError>()("Server.SettingsError", {
path: Schema.String,
message: Schema.String,
}) {}

/** A settings file changed since it was read; nothing was written. */
export class SettingsConflict extends Schema.TaggedError<SettingsConflict>()("Server.SettingsConflict", {
path: Schema.String,
}) {}

/** `settings.jsonc` or `mcp.json`: the project's with `projectId`, else the user's. */
export const SettingsFileRef = {
projectId: Schema.optionalKey(Schema.String),
file: Schema.Literals(["settings", "mcp"]),
};

/** A settings file as written. `version` is what a write is held to; `error` is what the runtime would reject. */
export const SettingsFile = Schema.Struct({
path: Schema.String,
source: Schema.String,
exists: Schema.Boolean,
version: Schema.String,
/** The file's JSONC as a value; absent when it does not parse. */
value: Schema.optionalKey(Schema.Unknown),
error: Schema.optionalKey(Schema.String),
/** For `settings.jsonc`: what applies where this file sets nothing. */
inherited: Schema.optionalKey(
Schema.Struct({
retry: Schema.Struct({ maxRetries: Schema.Int, baseDelayMs: Schema.Int, maxDelayMs: Schema.Int }),
}),
),
});
export type SettingsFile = typeof SettingsFile.Type;

/** Every settings call's: only a write can conflict, but one set keeps the app server's mapping single. */
const SettingsErrors = [CodeworkUnavailable, HostError, ProjectError, SettingsError, SettingsConflict] as const;

export const Layout = Schema.Literals(["tile", "monocle"]);
export type Layout = typeof Layout.Type;

Expand Down Expand Up @@ -439,6 +493,11 @@ export const Api = RpcGroup.make(
* An organization's active projects, newest activity first, each with its sessions that are not
* deleted: on the default machine only, or on every machine. Read from app.db alone.
*/
/** A project's directories, its main checkout first; read from app.db alone. */
Rpc.make("projects.spaces", {
payload: { machineId: Schema.String, projectId: Schema.String },
success: Schema.Array(ProjectSpace),
}),
Rpc.make("sidebar.list", {
payload: { organizationId: Schema.String, machines: Schema.Literals(["default", "all"]) },
success: Schema.Array(SidebarProject),
Expand Down Expand Up @@ -477,6 +536,40 @@ export const Api = RpcGroup.make(
success: Schema.Struct({ path: Schema.String, commitError: Schema.optionalKey(Schema.String) }),
error: Schema.Union([CodeworkUnavailable, HostError]),
}),
/** The `origin` URL of a project's main checkout, credentials taken out; null when it has none. */
Rpc.make("projects.remote", {
payload: { projectId: Schema.String },
success: Schema.Struct({ url: Schema.NullOr(Schema.String) }),
error: Schema.Union([CodeworkUnavailable, HostError, ProjectError]),
}),
/** A settings file on the CodeWork server, as written. */
Rpc.make("settings.read", {
payload: SettingsFileRef,
success: SettingsFile,
error: Schema.Union(SettingsErrors),
}),
/** The whole file, written only when it is still at `version` and the runtime would accept it. */
Rpc.make("settings.write", {
payload: { ...SettingsFileRef, source: Schema.String, version: Schema.String },
success: Schema.Struct({ version: Schema.String }),
error: Schema.Union(SettingsErrors),
}),
/** One value at `at` set in place, the rest of the file kept as written; no `value` removes it. */
Rpc.make("settings.set", {
payload: {
...SettingsFileRef,
at: Schema.NonEmptyArray(Schema.Union([Schema.String, Schema.Int])),
value: Schema.optionalKey(Schema.Unknown),
},
success: Schema.Struct({ version: Schema.String }),
error: Schema.Union(SettingsErrors),
}),
/** Renames a project on the CodeWork server; the sidebar follows once the change syncs back. */
Rpc.make("projects.rename", {
payload: { projectId: Schema.String, name: Schema.Trimmed.check(Schema.isNonEmpty()) },
success: Schema.Struct({ id: Schema.String, name: Schema.String }),
error: Schema.Union([CodeworkUnavailable, HostError, ProjectError]),
}),
/** An existing folder as an absolute path; a missing one is created. */
Rpc.make("host.add", {
payload: { path: Schema.String },
Expand Down
76 changes: 74 additions & 2 deletions apps/server/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { Effect, Stream } from "effect";
import type * as Mirror from "@codeworksh/widget/codework";
import { Effect, Predicate, Stream } from "effect";
import { SqlClient } from "effect/sql";
import { Cli } from "./cli.ts";
import { Codework } from "./codework.ts";
import { type CliAction, Contract, ModelsError, type ServerAdd, type ServerUpdate } from "@codeworksh/contract";
import {
type CliAction,
Contract,
ModelsError,
ProjectError,
type ServerAdd,
SettingsConflict,
SettingsError,
type ServerUpdate,
} from "@codeworksh/contract";
import { Desk } from "./desk.ts";
import { Event } from "./event.ts";
import { Health } from "./health.ts";
Expand All @@ -13,6 +23,34 @@ import { Sync } from "./sync.ts";
import { Tracking } from "./tracking.ts";
import { Workspaces } from "./workspaces.ts";

type SettingsRefusal =
| Mirror.SettingsError
| Mirror.SettingsConflictError
| Mirror.ProjectNotFoundError
| Mirror.ProjectCheckoutError;

const refusals: ReadonlyArray<string> = [
"SettingsError",
"SettingsConflictError",
"ProjectNotFoundError",
"ProjectCheckoutError",
];
const isRefusal = (error: unknown): error is SettingsRefusal =>
Predicate.hasProperty(error, "_tag") && refusals.includes(error._tag as string);

const refusal = (error: SettingsRefusal) => {
switch (error._tag) {
case "SettingsError":
return new SettingsError({ path: error.path, message: error.detail });
case "SettingsConflictError":
return new SettingsConflict({ path: error.path });
case "ProjectNotFoundError":
return new ProjectError({ message: "This project is no longer on its server." });
case "ProjectCheckoutError":
return new ProjectError({ message: "This project has no folder on its server." });
}
};

export const layer = Contract.Api.toLayer(
Effect.gen(function* () {
const workspaces = yield* Workspaces.handlers;
Expand All @@ -31,6 +69,18 @@ export const layer = Contract.Api.toLayer(
Codework.callHost(use, options).pipe(
Effect.tapError((error) => (error._tag === "Server.CodeworkUnavailable" ? health.poke : Effect.void)),
);
/** A settings call: the server's refusals reach the app with their reason; anything else is an unreachable server. */
const settingsCall = <A, E>(use: Parameters<typeof Codework.call<A, E>>[0]) =>
call((client) =>
use(client).pipe(
Effect.map((value) => ({ value })),
Effect.catch((error) =>
isRefusal(error) ? Effect.succeed({ failed: refusal(error) }) : Effect.fail(error),
),
),
).pipe(
Effect.flatMap((reply) => ("failed" in reply ? Effect.fail(reply.failed) : Effect.succeed(reply.value))),
);
// A changed default reconnects the app; the status read after waits for it to reach the server.
const changingDefault = <A, E, R>(change: Effect.Effect<A, E, R>) =>
Effect.gen(function* () {
Expand All @@ -45,6 +95,7 @@ export const layer = Contract.Api.toLayer(
...tracking,

"sidebar.list": (input) => Reads.sidebar(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)),
"projects.spaces": (input) => Reads.spaces(input).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)),

Expand All @@ -58,6 +109,27 @@ export const layer = Contract.Api.toLayer(
"host.create": Effect.fn("host.create")(function* ({ name }) {
return yield* callHost((client) => client("host.create", { name }), { timeout: "60 seconds" });
}),
"projects.remote": Effect.fn("projects.remote")(function* ({ projectId }) {
return yield* callHost((client) =>
client("project.remote", { projectId }).pipe(
Effect.catchTag("ProjectNotFoundError", () =>
Effect.fail(new ProjectError({ message: "This project is no longer on its server." })),
),
),
);
}),
"settings.read": (input) => settingsCall((client) => client("settings.read", input)),
"settings.write": (input) => settingsCall((client) => client("settings.write", input)),
"settings.set": (input) => settingsCall((client) => client("settings.set", input)),
"projects.rename": Effect.fn("projects.rename")(function* ({ projectId, name }) {
return yield* callHost((client) =>
client("project.rename", { projectId, name }).pipe(
Effect.catchTag("ProjectNotFoundError", () =>
Effect.fail(new ProjectError({ message: "This project is no longer on its server." })),
),
),
);
}),
"host.add": Effect.fn("host.add")(function* ({ path }) {
return yield* callHost((client) => client("host.add", { path }));
}),
Expand Down
20 changes: 19 additions & 1 deletion apps/server/src/reads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type LineRequest,
LineThread,
type Priority,
type ProjectSpace,
type SidebarProject,
type SidebarSession,
SyncError,
Expand Down Expand Up @@ -80,7 +81,8 @@ export const sidebar = Effect.fn("Reads.sidebar")(function* ({
createdAt: number;
updatedAt: number;
}>`
SELECT session.id, session.machine_id, space.project_id, space.kind AS space_kind, session.color,
SELECT session.id, session.machine_id, space.project_id, space.kind AS space_kind,
COALESCE(thread.color, session.color) AS color,
session.dir, session.hostdir, thread.id AS thread_id, thread.title, thread.model_provider,
thread.model_id, thread.run_state, thread.status, thread.priority, thread.created_at,
(SELECT MAX(activity.updated_at) FROM thread AS activity
Expand Down Expand Up @@ -427,4 +429,20 @@ export const line = Effect.fn("Reads.line")(function* (request: LineRequest) {
}).pipe(Effect.orDie);
});

/** A project's directories, its main checkout first, then by folder. */
export const spaces = Effect.fn("Reads.spaces")(function* ({
machineId,
projectId,
}: {
readonly machineId: string;
readonly projectId: string;
}) {
const sql = yield* SqlClient.SqlClient;
return yield* sql<ProjectSpace>`
SELECT id, location, kind, env, status FROM space
WHERE machine_id = ${machineId} AND project_id = ${projectId}
ORDER BY kind <> 'primary', location
`;
}, Effect.orDie);

export * as Reads from "./reads.ts";
2 changes: 1 addition & 1 deletion apps/server/test/fixtures/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { DatabaseSync } from "node:sqlite";
import { fileURLToPath } from "node:url";
import { expect } from "vite-plus/test";
import { Contract } from "@codeworksh/contract";
import { Client } from "../../../../packages/codework/src/server/client.ts";
import * as Client from "../../../../packages/codework/src/server/client.ts";

const appEntry = fileURLToPath(new URL("../../src/index.ts", import.meta.url));
const fixture = fileURLToPath(new URL("./codework.ts", import.meta.url));
Expand Down
4 changes: 2 additions & 2 deletions apps/server/test/fixtures/codework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { NodeRuntime } from "@effect/platform-node";
import { Effect, Layer } from "effect";
import { createInterface } from "node:readline";
import { assistant } from "../../../../packages/harness/test/fixtures/llm.ts";
import type { LLM } from "../../../../packages/harness/src/runner/llm.ts";
import { Server } from "../../../../packages/codework/src/server/server.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";

/*
Expand Down
2 changes: 1 addition & 1 deletion apps/server/test/machines/drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { NodeRuntime } from "@effect/platform-node";
import { Effect } from "effect";
import { readFileSync } from "node:fs";
import { Client } from "../../../../packages/codework/src/server/client.ts";
import * as Client from "../../../../packages/codework/src/server/client.ts";

/*
* `node --conditions=development drive.ts < calls.json`: runs `[[method, payload], ...]` in order
Expand Down
2 changes: 1 addition & 1 deletion apps/server/test/machines/orb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawn, spawnSync } from "node:child_process";
import { userInfo } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "../../../../packages/codework/src/server/client.ts";
import * as Client from "../../../../packages/codework/src/server/client.ts";

const scripts = fileURLToPath(new URL(".", import.meta.url));
/** The checkout under test: each machine overlays its sources from here through the Mac mount. */
Expand Down
2 changes: 1 addition & 1 deletion apps/server/test/sync.machines.live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { join } from "node:path";
import { userInfo } from "node:os";
import { afterAll, beforeAll, describe, expect, it } from "vite-plus/test";
import { Contract, LOCAL_SERVER } from "@codeworksh/contract";
import type { Client } from "../../../packages/codework/src/server/client.ts";
import type * as Client from "../../../packages/codework/src/server/client.ts";
import {
app,
cleanup,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function DisplayMenu({
<SlidersHorizontal aria-hidden />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align={align} side={side} className="min-w-40">
<DropdownMenuContent align={align} side={side} className="min-w-44">
{children}
</DropdownMenuContent>
</DropdownMenu>
Expand Down
15 changes: 12 additions & 3 deletions apps/web/src/components/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,37 @@ export function MenuCheck() {

/**
* One choice of a menu: its mark and name, opening its picks beside it with the current one checked.
* `after` holds actions rather than picks, such as clearing it, below a separator.
* `current` names the pick on the row itself, before the chevron. `after` holds actions rather than
* picks, such as clearing it, below a separator.
*/
export function MenuChoice<A extends string>({
label,
mark,
value,
current,
options,
onChange,
after,
}: {
readonly label: string;
readonly mark: ReactNode;
readonly value: A | undefined;
readonly current?: string | undefined;
readonly options: ReadonlyArray<{ readonly value: A; readonly label: string; readonly mark: ReactNode }>;
readonly onChange: (value: A) => void;
readonly after?: ReactNode;
}) {
return (
<DropdownMenuSub>
<DropdownMenuSubTrigger className={`${MENU_ROW} [&>svg:last-child]:size-3.5`}>
{mark}
<DropdownMenuSubTrigger
className={`${MENU_ROW} [&>svg:last-child]:size-3.5 ${current === undefined ? "" : "[&>svg:last-child]:ml-0"}`}
>
{/* One width for every mark, so a dot's label lines up with an icon's. */}
<span className="grid size-[15px] shrink-0 place-items-center">{mark}</span>
{label}
{current !== undefined && (
<span className="ml-auto max-w-28 truncate pl-4 text-[12px] text-ink-muted">{current}</span>
)}
</DropdownMenuSubTrigger>
{/* Shifted up by its padding and border, so its first row lines up with the trigger. */}
<DropdownMenuSubContent alignOffset={-5} className="min-w-40">
Expand Down
Loading