Skip to content
155 changes: 149 additions & 6 deletions apps/contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Repository,
RunState,
SpaceInfo,
UsageItem,
} from "@codeworksh/widget/codework";
import { Event, EventTimeout, Publish, Request, Synced } from "@codeworksh/widget/event";

Expand Down Expand Up @@ -373,11 +374,21 @@ export const ServerKind = Schema.Literals(["local", "remote"]);
/** The server the app always has, which cannot be removed. */
export const LOCAL_SERVER = "local";

/** How a server is named: the user's `override`, else the `host` name its server reports, else where it is. */
const ServerNaming = {
/** As shown. */
name: Schema.String,
/** As its server last reported it; none from a server that reports no name or was never reached. */
host: Schema.NullOr(Schema.String),
/** As typed in Settings; none when the user named it nothing. */
override: Schema.NullOr(Schema.String),
};

/** A local server and how it is set up, with its executable and process as found when listed. */
export const LocalServerStatus = Schema.Struct({
id: Schema.String,
kind: Schema.Literal("local"),
name: Schema.String,
...ServerNaming,
/** The one the app connects to. */
isDefault: Schema.Boolean,
/**
Expand All @@ -398,7 +409,7 @@ export type LocalServerStatus = typeof LocalServerStatus.Type;
export const RemoteServerStatus = Schema.Struct({
id: Schema.String,
kind: Schema.Literal("remote"),
name: Schema.String,
...ServerNaming,
isDefault: Schema.Boolean,
url: Schema.String,
});
Expand All @@ -421,23 +432,23 @@ export type CliAction = typeof CliAction.Type;

/**
* A new server. A local one needs a home of its own, since a home names one server; a remote one
* needs its WebSocket URL.
* needs its WebSocket URL. A blank or absent `name` is no override.
*/
export const ServerAdd = Schema.Union([
Schema.Struct({
kind: Schema.Literal("local"),
name: Schema.String,
name: Schema.optionalKey(Schema.String),
executable: Schema.NullOr(Schema.String),
home: Schema.String,
args: Schema.String,
}),
Schema.Struct({ kind: Schema.Literal("remote"), name: Schema.String, url: Schema.String }),
Schema.Struct({ kind: Schema.Literal("remote"), name: Schema.optionalKey(Schema.String), url: Schema.String }),
]);
export type ServerAdd = typeof ServerAdd.Type;

/**
* Changes to a server; an absent field is left as it is. A `null` path goes back to the one found;
* `env` replaces every variable. `url` is for a remote server only, the rest but `name` for a local one.
* a blank `name` removes the override; `env` replaces every variable. `url` is for a remote server only, the rest but `name` for a local one.
*/
export const ServerUpdate = Schema.Struct({
id: Schema.String,
Expand Down Expand Up @@ -468,6 +479,8 @@ export const syncTopics = {
tracking: "sync/tracking",
/** `SyncState` whenever a space daemon's mirrored state changed (git status, skills); stored. */
state: "sync/state",
/** `SyncUsage` after each applied usage batch that carried rows; stored. */
usage: "sync/usage",
};

/**
Expand Down Expand Up @@ -499,6 +512,14 @@ export const SyncState = Schema.Struct({
});
export type SyncState = typeof SyncState.Type;

/** A machine's usage rows from one of its stores were mirrored anew; `items` counts them. */
export const SyncUsage = Schema.Struct({
machineId: Schema.String,
storeId: Schema.String,
items: Schema.Int,
});
export type SyncUsage = typeof SyncUsage.Type;

/** A thread's status moved; `at` is the app server's clock. */
export const SyncStatus = Schema.Struct({
threadId: Schema.String,
Expand Down Expand Up @@ -603,13 +624,123 @@ export type AllThreads = typeof AllThreads.Type;
* `sync/delta/<thread>` streams its draft between checkpoints.
* - `threads.list`: `sync/changed` naming a thread or a removed session; every `sync/status` and
* `sync/tracking`.
* - `metrics.usage` and `metrics.hours`: every `sync/usage`, and every `sync/changed` for the session
* titles, deletions and project names they carry.
*/
export const readTopics = {
sidebar: [syncTopics.changed, syncTopics.status, syncTopics.tracking],
threads: [syncTopics.changed, syncTopics.status, syncTopics.tracking],
line: (threadId: string) => [syncTopics.changed, syncTopics.delta(threadId)],
metrics: [syncTopics.usage, syncTopics.changed],
};

const [UsageWire, TallyWire] = UsageItem.members;

/** A calendar day in the request's `zone`, `YYYY-MM-DD`. */
const LocalDay = Schema.String;

/**
* What the metrics reads cover: every machine and project unless one is named. `zone` is an IANA
* time zone; days and hours are its wall clock. The rollup's resolution is a UTC hour, which counts
* whole in the local hour holding its start: in a zone 30 or 45 minutes off the hour, all of it in
* the earlier one. A day a clock change shortens has 23 hours, one it lengthens 25, the repeated
* hour summed into one.
*/
const MetricsScope = {
zone: Schema.String,
machineId: Schema.optionalKey(Schema.String),
projectId: Schema.optionalKey(Schema.String),
};

/** Requests of one session on one local day to one provider, model and thinking level, summed over its threads. */
export const MetricsRow = Schema.Struct({
day: LocalDay,
machineId: Schema.String,
sessionId: Schema.String,
provider: Schema.String,
model: Schema.String,
thinking: Schema.String,
requests: Schema.Int,
tokens: UsageWire.fields.tokens,
cost: UsageWire.fields.cost,
duration: Schema.Finite,
timed: Schema.Int,
/** Over `edges`, as `UsageItem`'s. */
durations: Schema.Array(Schema.Int),
});
export type MetricsRow = typeof MetricsRow.Type;

/** Prompts (`name` empty) or a tool's calls of one session on one local day. */
export const MetricsTally = Schema.Struct({
day: LocalDay,
machineId: Schema.String,
sessionId: Schema.String,
kind: TallyWire.fields.kind,
name: Schema.String,
count: Schema.Int,
});
export type MetricsTally = typeof MetricsTally.Type;

/** How many of a session's threads had requests or tallies on a local day. */
export const MetricsThreads = Schema.Struct({
day: LocalDay,
machineId: Schema.String,
sessionId: Schema.String,
threads: Schema.Int,
});
export type MetricsThreads = typeof MetricsThreads.Type;

/**
* A session the rows name. `title` is its main thread's, or what an untitled one is called; null
* once the session is deleted or gone from the mirror, its rows kept. Which sandbox it ran in is
* not mirrored, so it is not here.
*/
export const MetricsSession = Schema.Struct({
id: Schema.String,
machineId: Schema.String,
/** As its rows recorded it. */
projectId: Schema.String,
title: Schema.NullOr(Schema.String),
deleted: Schema.Boolean,
});
export type MetricsSession = typeof MetricsSession.Type;

/** A period's usage at the grain the Metrics page slices, with the names it shows. */
export const MetricsUsage = Schema.Struct({
rows: Schema.Array(MetricsRow),
tallies: Schema.Array(MetricsTally),
threads: Schema.Array(MetricsThreads),
sessions: Schema.Array(MetricsSession),
/**
* A project id is its repo's, so one on several machines is listed once, named as on the machine
* that changed it last; null when no machine has it any more.
*/
projects: Schema.Array(Schema.Struct({ id: Schema.String, name: Schema.NullOr(Schema.String) })),
machines: Schema.Array(Schema.Struct({ id: Schema.String, name: Schema.String })),
/** The upper bounds in ms of `durations`' buckets. */
edges: Schema.Array(Schema.Int),
});
export type MetricsUsage = typeof MetricsUsage.Type;

/** Requests of one session in one local hour (0–23) to one provider and model. */
export const MetricsHour = Schema.Struct({
hour: Schema.Int,
machineId: Schema.String,
sessionId: Schema.String,
provider: Schema.String,
model: Schema.String,
requests: Schema.Int,
/** Input, output, cache read and cache write. */
tokens: Schema.Int,
cost: Schema.Finite,
});
export type MetricsHour = typeof MetricsHour.Type;

/** A metrics read refused: an unknown time zone, a malformed day, or a period ending before it starts. */
export class MetricsError extends Schema.TaggedError<MetricsError>()("Server.MetricsError", {
message: Schema.String,
}) {}

/** A desktop edit refused: a value out of range, or a thread, session, group or organization not found. */
export class TrackingError extends Schema.TaggedError<TrackingError>()("Server.TrackingError", {
message: Schema.String,
Expand Down Expand Up @@ -668,6 +799,18 @@ export const Api = RpcGroup.make(
payload: { organizationId: Schema.String, by: ThreadsBy },
success: AllThreads,
}),
/** Usage from `since` to `until`, both local days included, summed over threads and stores. Read from app.db alone. */
Rpc.make("metrics.usage", {
payload: { ...MetricsScope, since: LocalDay, until: LocalDay },
success: MetricsUsage,
error: MetricsError,
}),
/** One local day's usage by local hour, for its chart; its sessions and threads are in `metrics.usage`. Read from app.db alone. */
Rpc.make("metrics.hours", {
payload: { ...MetricsScope, day: LocalDay },
success: Schema.Array(MetricsHour),
error: MetricsError,
}),
/** The models a new session can use; `hostDir` is its folder, whose project settings apply. */
Rpc.make("models.list", {
payload: { hostDir: Schema.optionalKey(Schema.String) },
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ export const statuses = Effect.gen(function* () {
id: row.id,
kind: row.kind,
name: row.name,
host: row.host,
override: row.override,
isDefault: row.isDefault,
home,
cli,
Expand Down
69 changes: 69 additions & 0 deletions apps/server/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,75 @@ 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 })}`;
}),

// Each machine's usage rollup, as `usage.sync` sends it, keyed by store: a recreated store starts
// new rows and keeps the old ones as history, and a removed server re-added later replaces its own.
// No foreign keys: the rows outlive their machine, project, session and thread; `machine_id` is the
// machine that last sent them.
"202611140001_usage": Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
// `rollup` is the usage cursor, counted in `rollup_store` while `rollup_boot` was its latest open:
// it moves apart from `cursor` and `store_id`.
yield* sql`ALTER TABLE machine ADD COLUMN rollup INTEGER NOT NULL DEFAULT 0`;
yield* sql`ALTER TABLE machine ADD COLUMN rollup_store TEXT`;
yield* sql`ALTER TABLE machine ADD COLUMN rollup_boot TEXT`;
yield* sql`
CREATE TABLE usage (
machine_id TEXT NOT NULL,
store_id TEXT NOT NULL,
hour INTEGER NOT NULL,
thread_id TEXT NOT NULL,
provider TEXT NOT NULL,
model TEXT NOT NULL,
thinking TEXT NOT NULL,
session_id TEXT NOT NULL,
project_id TEXT NOT NULL,
requests INTEGER NOT NULL,
input INTEGER NOT NULL,
output INTEGER NOT NULL,
cache_read INTEGER NOT NULL,
cache_write INTEGER NOT NULL,
reasoning INTEGER NOT NULL,
cost REAL NOT NULL,
cost_input REAL NOT NULL,
cost_output REAL NOT NULL,
cost_cache_read REAL NOT NULL,
cost_cache_write REAL NOT NULL,
duration REAL NOT NULL,
timed INTEGER NOT NULL,
durations TEXT NOT NULL,
rev INTEGER NOT NULL,
PRIMARY KEY (store_id, hour, thread_id, provider, model, thinking)
)
`;
yield* sql`
CREATE TABLE tally (
machine_id TEXT NOT NULL,
store_id TEXT NOT NULL,
hour INTEGER NOT NULL,
thread_id TEXT NOT NULL,
kind TEXT NOT NULL,
name TEXT NOT NULL,
session_id TEXT NOT NULL,
project_id TEXT NOT NULL,
count INTEGER NOT NULL,
rev INTEGER NOT NULL,
PRIMARY KEY (store_id, hour, thread_id, kind, name)
)
`;
}),

// A machine is named by what its server reports (`host`); `name` is only the user's override, so
// it becomes nullable. ADD + DROP + RENAME instead of rebuilding the table, which its foreign keys
// would cascade into. The built-in server's old seed name is no override.
"202611140002_host": Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`ALTER TABLE machine ADD COLUMN host TEXT`;
yield* sql`ALTER TABLE machine ADD COLUMN alias TEXT`;
yield* sql`UPDATE machine SET alias = name WHERE NOT (id = 'local' AND name = 'CodeWork')`;
yield* sql`ALTER TABLE machine DROP COLUMN name`;
yield* sql`ALTER TABLE machine RENAME COLUMN alias TO name`;
}),
};

const setup = Effect.gen(function* () {
Expand Down
3 changes: 3 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 { Desk } from "./desk.ts";
import { Event } from "./event.ts";
import { Health } from "./health.ts";
import { Kv } from "./kv.ts";
import { Metrics } from "./metrics.ts";
import { Reads } from "./reads.ts";
import { Skills } from "./skills.ts";
import { Servers } from "./servers.ts";
Expand Down Expand Up @@ -106,6 +107,8 @@ export const layer = Contract.Api.toLayer(
"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)),
"metrics.usage": (input) => Metrics.usage(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)),
"metrics.hours": (input) => Metrics.hours(input).pipe(Effect.provideService(SqlClient.SqlClient, sql)),

"host.info": Effect.fn("host.info")(function* () {
return yield* call((client) => client("host.info", {}));
Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ export const layer = Layer.effect(

yield* check.pipe(Effect.forkScoped);

// A name a server reports reaches Settings without waiting for the next check; the app's own
// edits are checked by their handlers.
yield* servers.watch.pipe(
Stream.map((rows) => rows.map((row) => `${row.id}=${row.host}`).join("\n")),
Stream.changes,
Stream.drop(1),
Stream.runForEach(() => check),
Effect.forkScoped,
);

// Every interval; a new interval starts counting at once, and 0 waits for one.
yield* Effect.forever(
Effect.gen(function* () {
Expand Down
Loading