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
39 changes: 39 additions & 0 deletions apps/contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ export const ProjectSpace = Schema.Struct({
});
export type ProjectSpace = typeof ProjectSpace.Type;

/** One skill as a space's sessions see it: its frontmatter `name` and `description`. */
export const SpaceSkill = Schema.Struct({
name: Schema.String,
description: Schema.String,
/** Its frontmatter keeps it out of the prompt. */
hidden: Schema.Boolean,
});
export type SpaceSkill = typeof SpaceSkill.Type;

/**
* A space's skills. A host space is read each time; a sandbox only while it runs, else its last read
* is kept. `skills` is null when it was never read; `syncedAt` is when it was, in epoch milliseconds.
*/
export const SpaceSkills = Schema.Struct({
space: ProjectSpace,
/** The sandbox it is in, as last observed; null for the host. */
sandbox: Schema.NullOr(
Schema.Struct({
driver: Schema.String,
providerResourceId: Schema.NullOr(Schema.String),
status: Schema.String,
}),
),
skills: Schema.NullOr(Schema.Array(SpaceSkill)),
syncedAt: Schema.NullOr(Schema.Finite),
});
export type SpaceSkills = typeof SpaceSkills.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,
Expand Down Expand Up @@ -139,6 +167,12 @@ export const SettingsFile = Schema.Struct({
retry: Schema.Struct({ maxRetries: Schema.Int, baseDelayMs: Schema.Int, maxDelayMs: Schema.Int }),
}),
),
/** For `mcp.json`: the servers as sessions use them, user and project merged. */
servers: Schema.optionalKey(
Schema.Array(
Schema.Struct({ name: Schema.String, from: Schema.Literals(["user", "project"]), enabled: Schema.Boolean }),
),
),
});
export type SettingsFile = typeof SettingsFile.Type;

Expand Down Expand Up @@ -498,6 +532,11 @@ export const Api = RpcGroup.make(
payload: { machineId: Schema.String, projectId: Schema.String },
success: Schema.Array(ProjectSpace),
}),
/** Each active space's skills, its main checkout first. Never wakes a sandbox. */
Rpc.make("projects.skills", {
payload: { machineId: Schema.String, projectId: Schema.String },
success: Schema.Array(SpaceSkills),
}),
Rpc.make("sidebar.list", {
payload: { organizationId: Schema.String, machines: Schema.Literals(["default", "all"]) },
success: Schema.Array(SidebarProject),
Expand Down
30 changes: 12 additions & 18 deletions apps/server/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Migrator, SqlClient } from "effect/sql";
// "YYYYMMDD<counter>_<label>"; the migrator runs by high-water mark, so a new
// id must sort after every applied one.
const migrations = {
"202610170001_init": Effect.gen(function* () {
"202611120001_init": Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`
CREATE TABLE kv (
Expand Down Expand Up @@ -134,6 +134,17 @@ 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.
yield* 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
)
`;
yield* sql`
CREATE TABLE session (
id TEXT PRIMARY KEY,
Expand Down Expand Up @@ -261,23 +272,6 @@ const migrations = {
// The one organization the UI uses until auth exists.
yield* sql`INSERT INTO organization ${sql.insert({ id: "personal", name: "Personal", createdAt: now, updatedAt: now })}`;
}),
// Statuses become Linear-like: only the user sets review, settled and cancelled, so each other status
// is derived again from the run. A settled or cancelled one stays only when the user's edit set it.
"202611100001_status": Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`
UPDATE thread SET status = CASE
WHEN run_state = 'idle' AND (run_started_at IS NULL OR parked = 1) THEN 'enqueued'
ELSE 'working'
END
WHERE NOT (status IN ('settled', 'cancelled') AND (
SELECT event.source = 'tracking' AND json_extract(event.payload, '$.status') = thread.status
FROM event
WHERE event.topic = 'sync/status' AND json_extract(event.payload, '$.threadId') = thread.id
ORDER BY event.seq DESC LIMIT 1
) IS 1)
`;
}),
};

const setup = Effect.gen(function* () {
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Event } from "./event.ts";
import { Health } from "./health.ts";
import { Kv } from "./kv.ts";
import { Reads } from "./reads.ts";
import { Skills } from "./skills.ts";
import { Servers } from "./servers.ts";
import { Sync } from "./sync.ts";
import { Tracking } from "./tracking.ts";
Expand Down Expand Up @@ -96,6 +97,7 @@ 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)),
"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
56 changes: 56 additions & 0 deletions apps/server/src/skills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { SpaceSkill, type SpaceSkills } from "@codeworksh/contract";
import { Effect, Option, Schema } from "effect";
import { SqlClient } from "effect/sql";
import { Codework } from "./codework.ts";
import { Reads } from "./reads.ts";

const Stored = Schema.fromJsonString(Schema.Array(SpaceSkill));
const encode = Schema.encodeSync(Stored);
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.
*/
export const list = Effect.fn("Skills.list")(function* (input: {
readonly machineId: string;
readonly projectId: string;
}) {
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" },
);
});

export * as Skills from "./skills.ts";
6 changes: 6 additions & 0 deletions apps/server/test/__artifacts__/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
"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",
Expand Down
9 changes: 5 additions & 4 deletions apps/server/test/__artifacts__/sync.machines.deleted.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"changed": [
{
"machineId": "<machine fedora>",
"machineId": "<machine ubuntu>",
"full": false,
"items": 1,
"project": [],
Expand All @@ -27,7 +27,7 @@
"session": [
{
"id": "<ses 2>",
"machine_id": "<machine fedora>",
"machine_id": "<machine ubuntu>",
"organization_id": "personal",
"space_id": "<id 1>",
"slug": "<id 2>",
Expand All @@ -42,7 +42,7 @@
},
{
"id": "<ses 1>",
"machine_id": "<machine fedora>",
"machine_id": "<machine ubuntu>",
"organization_id": "personal",
"space_id": "<id 1>",
"slug": "<id 3>",
Expand Down Expand Up @@ -73,6 +73,7 @@
"foundation": null,
"placement_space_id": null,
"placement_dir": null,
"parked": 0,
"restarts": 0,
"attempt": null,
"run_state": "idle",
Expand All @@ -86,7 +87,7 @@
"tokens_output": 1,
"tokens_cache_read": 0,
"tokens_cache_write": 0,
"status": "attention",
"status": "working",
"status_at": "<time>",
"priority": 0,
"grouping_id": null,
Expand Down
6 changes: 4 additions & 2 deletions apps/server/test/__artifacts__/sync.machines.partition.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"foundation": null,
"placement_space_id": null,
"placement_dir": null,
"parked": 0,
"restarts": 0,
"attempt": null,
"run_state": "idle",
Expand All @@ -82,7 +83,7 @@
"tokens_output": 2,
"tokens_cache_read": 0,
"tokens_cache_write": 0,
"status": "attention",
"status": "working",
"status_at": "<time>",
"priority": 0,
"grouping_id": null,
Expand Down Expand Up @@ -111,6 +112,7 @@
"foundation": null,
"placement_space_id": null,
"placement_dir": null,
"parked": 0,
"restarts": 0,
"attempt": null,
"run_state": "idle",
Expand All @@ -124,7 +126,7 @@
"tokens_output": 0,
"tokens_cache_read": 0,
"tokens_cache_write": 0,
"status": "settled",
"status": "enqueued",
"status_at": "<time>",
"priority": 0,
"grouping_id": null,
Expand Down
6 changes: 4 additions & 2 deletions apps/server/test/__artifacts__/sync.machines.restored.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"foundation": null,
"placement_space_id": null,
"placement_dir": null,
"parked": 0,
"restarts": 0,
"attempt": null,
"run_state": "idle",
Expand All @@ -119,7 +120,7 @@
"tokens_output": 1,
"tokens_cache_read": 0,
"tokens_cache_write": 0,
"status": "attention",
"status": "review",
"status_at": "<time>",
"priority": 2,
"grouping_id": null,
Expand Down Expand Up @@ -148,6 +149,7 @@
"foundation": null,
"placement_space_id": null,
"placement_dir": null,
"parked": 0,
"restarts": 0,
"attempt": null,
"run_state": "idle",
Expand All @@ -161,7 +163,7 @@
"tokens_output": 1,
"tokens_cache_read": 0,
"tokens_cache_write": 0,
"status": "settled",
"status": "working",
"status_at": "<time>",
"priority": 0,
"grouping_id": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"itemsInRevOrder": true,
"itemTags": [
"Project",
"Session",
"Space",
"Thread"
],
Expand Down
6 changes: 3 additions & 3 deletions apps/server/test/__artifacts__/sync.machines.several.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"projects": [
{
"machine_id": "<machine fedora>",
"machine_id": "<machine ubuntu>",
"id": "<id 1>",
"name": "codework"
},
Expand All @@ -12,8 +12,8 @@
}
],
"routed": {
"fedora": {
"machine": "<machine fedora>",
"ubuntu": {
"machine": "<machine ubuntu>",
"sameAsItsMachine": true
},
"cw-a": {
Expand Down
Loading