Add the schulcloud CLI, and document the split

The CLI talks only to the Pi's /api surface and holds no Schulcloud
credential — only the same bearer token the Claude connector uses. That
is not layering for its own sake: a Schulcloud session dies after two
hours idle and a CLI process lives for seconds, so a CLI with its own
token would be dead most times you reached for it. Routing through the
Pi means one session, one keepalive, one monthly cookie paste.

sync is a one-way mirror, which follows from the data rather than from
scope-cutting: file records are immutable upstream, so there is no
versioning, no conflict resolution and no merge. State is keyed by file
record id with the path as derived output, so an upstream rename moves
the local file instead of duplicating it — verified against the live
server. Verification is size-only because the download endpoint exposes
no ETag and Schulcloud publishes no hash; size still catches the failure
that happens, a truncated download. Downloads land on a .part neighbour
and are renamed, so an interrupted run leaves no half-file that a later
run mistakes for complete. Deletions are reported but not propagated —
a teacher removing a worksheet is no reason to destroy the student's
copy — with --prune to opt in.

what_changed now clamps to the oldest stored generation instead of
refusing, and says it did: "what's new this week" is a reasonable
question to ask a two-day-old index.

Two build bugs caught by the checks rather than by luck: the smoke
harness constructed the app without services, so the index-backed tools
were never exercised; and the Docker build could not see
scripts/copy-assets.mjs, so the image would have shipped without
migrations and silently degraded to live-only.

67 unit tests (9 needing Postgres), smoke green both ways — 34 checks
with an index, 32 without, because graceful degradation is a supported
mode and not a fallback nobody runs.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 21:26:33 +02:00
parent c79f1b120d
commit 359c46afad
18 changed files with 1108 additions and 84 deletions

94
src/cli/client.ts Normal file
View File

@@ -0,0 +1,94 @@
import type { CliConfig } from './config.ts';
/**
* Talks to the schulcloud-mcp server's /api surface.
*
* Deliberately the only thing in the CLI that knows a network exists, and it
* never touches Schulcloud directly — the Pi holds that credential.
*/
export interface ManifestEntry {
fileId: string;
name: string;
path: string;
size: number;
mimeType: string;
courseId: string | null;
courseTitle: string;
status: 'added' | 'unchanged' | 'removed';
}
export interface Manifest {
crawlId: number;
cursor: string;
crawledAt?: string;
count: number;
entries: ManifestEntry[];
}
export class ApiError extends Error {
readonly status: number;
constructor(status: number, message: string) {
super(message);
this.status = status;
this.name = 'ApiError';
}
}
export class ApiClient {
private readonly config: CliConfig;
constructor(config: CliConfig) {
this.config = config;
}
private async request(path: string, init: RequestInit = {}): Promise<Response> {
const response = await fetch(`${this.config.server}${path}`, {
...init,
headers: { ...(init.headers ?? {}), Authorization: `Bearer ${this.config.token}` },
});
if (!response.ok) {
let detail = '';
try {
const body = (await response.json()) as { message?: string; error?: string };
detail = body.message ?? body.error ?? '';
} catch {
// Non-JSON error bodies are not worth surfacing verbatim.
}
throw new ApiError(response.status, describe(response.status, detail, this.config.server));
}
return response;
}
async status(): Promise<Record<string, unknown>> {
return (await (await this.request('/api/status')).json()) as Record<string, unknown>;
}
async manifest(since?: string): Promise<Manifest> {
const query = since ? `?since=${encodeURIComponent(since)}` : '';
return (await (await this.request(`/api/manifest${query}`)).json()) as Manifest;
}
async refresh(courseId?: string, force = false): Promise<Record<string, unknown>> {
const response = await this.request('/api/refresh', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ courseId, force }),
});
return (await response.json()) as Record<string, unknown>;
}
/** Streams one file's bytes. */
async file(fileId: string): Promise<Response> {
return this.request(`/api/files/${encodeURIComponent(fileId)}`);
}
}
function describe(status: number, detail: string, server: string): string {
if (status === 401) return `Unauthorized — the token is wrong or expired. Re-run: schulcloud login --server ${server} --token <token>`;
if (status === 503) return 'The server is running without an index, so this command is unavailable. Set DATABASE_URL on the server.';
if (status === 409) return detail || 'The sync cursor is unknown to the server. Run a full sync with --full.';
if (status === 429) return detail || 'Refreshed too recently — wait a moment, or pass --force.';
return detail ? `HTTP ${status}: ${detail}` : `HTTP ${status}`;
}

67
src/cli/config.ts Normal file
View File

@@ -0,0 +1,67 @@
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { dirname, join } from 'node:path';
/**
* CLI configuration.
*
* The laptop holds no Schulcloud credential — only this server's bearer token.
* That is the whole point of routing through the Pi: one Schulcloud session,
* kept alive in one place, refreshed by hand once a month in one place.
*/
export interface CliConfig {
/** Base URL of the schulcloud-mcp server, e.g. https://mcp.example.org */
server: string;
token: string;
/** Where `sync` mirrors files locally. */
syncDir: string;
}
export function configPath(): string {
const base = process.env.XDG_CONFIG_HOME?.trim() || join(homedir(), '.config');
return join(base, 'schulcloud', 'config.json');
}
export function defaultSyncDir(): string {
return join(homedir(), 'Schulcloud');
}
export async function loadCliConfig(): Promise<CliConfig> {
// Environment wins, so CI and one-off invocations need no file.
const fromEnv = {
server: process.env.SCHULCLOUD_SERVER?.trim(),
token: process.env.SCHULCLOUD_TOKEN?.trim(),
syncDir: process.env.SCHULCLOUD_SYNC_DIR?.trim(),
};
let fromFile: Partial<CliConfig> = {};
try {
fromFile = JSON.parse(await readFile(configPath(), 'utf8')) as Partial<CliConfig>;
} catch {
// No config file is fine as long as the environment supplies the essentials.
}
const server = fromEnv.server ?? fromFile.server;
const token = fromEnv.token ?? fromFile.token;
if (!server || !token) {
throw new Error(
`Not configured. Run:\n\n schulcloud login --server https://mcp.example.org --token <token>\n\n` +
`or set SCHULCLOUD_SERVER and SCHULCLOUD_TOKEN. Config lives at ${configPath()}.`,
);
}
return {
server: server.replace(/\/+$/, ''),
token,
syncDir: fromEnv.syncDir ?? fromFile.syncDir ?? defaultSyncDir(),
};
}
export async function saveCliConfig(config: CliConfig): Promise<string> {
const path = configPath();
await mkdir(dirname(path), { recursive: true });
// 0600: the token is a credential for an internet-facing endpoint.
await writeFile(path, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
return path;
}

191
src/cli/sync.ts Normal file
View File

@@ -0,0 +1,191 @@
import { createWriteStream } from 'node:fs';
import { mkdir, readFile, rename, stat, unlink, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { resolveWithin } from '../core/paths.ts';
import type { ApiClient, ManifestEntry } from './client.ts';
/**
* Mirrors Schulcloud files to a local directory.
*
* This is a one-way mirror, not a two-way sync, and that is a property of the
* upstream data rather than a simplification: Schulcloud file records are
* immutable — editing a file produces a *new* record — so there is no content
* versioning, no conflict resolution and no merge. "Download what I do not
* have" is the whole algorithm.
*
* Local state is keyed by file record id with the path as derived output, so a
* teacher renaming a board column moves files instead of duplicating them.
*/
export interface SyncState {
/** Server generation this state was last synced to. */
cursor?: string;
/** fileId → what we wrote, so renames move rather than re-download. */
files: Record<string, { path: string; size: number; syncedAt: string }>;
}
export interface SyncOptions {
dryRun: boolean;
prune: boolean;
full: boolean;
concurrency: number;
onEvent: (event: SyncEvent) => void;
}
export type SyncEvent =
| { type: 'download'; entry: ManifestEntry; reason: 'new' | 'size-mismatch' | 'missing' }
| { type: 'move'; entry: ManifestEntry; from: string }
| { type: 'skip'; entry: ManifestEntry }
| { type: 'remove'; path: string; kept: boolean }
| { type: 'error'; entry: ManifestEntry; message: string };
export interface SyncSummary {
downloaded: number;
moved: number;
skipped: number;
removed: number;
kept: number;
failed: number;
bytes: number;
cursor: string;
}
const STATE_FILE = '.schulcloud-sync.json';
export async function loadState(root: string): Promise<SyncState> {
try {
return JSON.parse(await readFile(join(root, STATE_FILE), 'utf8')) as SyncState;
} catch {
return { files: {} };
}
}
export async function saveState(root: string, state: SyncState): Promise<void> {
await mkdir(root, { recursive: true });
await writeFile(join(root, STATE_FILE), `${JSON.stringify(state, null, 2)}\n`);
}
export async function sync(
api: ApiClient,
root: string,
options: SyncOptions,
): Promise<SyncSummary> {
const state = options.full ? { files: {} } : await loadState(root);
const manifest = await api.manifest(options.full ? undefined : state.cursor);
const summary: SyncSummary = {
downloaded: 0, moved: 0, skipped: 0, removed: 0, kept: 0, failed: 0, bytes: 0,
cursor: manifest.cursor,
};
const present = manifest.entries.filter((entry) => entry.status !== 'removed');
const gone = manifest.entries.filter((entry) => entry.status === 'removed');
// Bounded concurrency: the Pi is serving these from disk over a home uplink.
let cursor = 0;
const workers = Array.from({ length: Math.min(options.concurrency, present.length) }, async () => {
while (cursor < present.length) {
const entry = present[cursor++];
if (!entry) continue;
try {
await syncOne(api, root, entry, state, options, summary);
} catch (error) {
summary.failed++;
options.onEvent({ type: 'error', entry, message: error instanceof Error ? error.message : String(error) });
}
}
});
await Promise.all(workers);
for (const entry of gone) {
const known = state.files[entry.fileId];
if (!known) continue;
if (options.prune) {
if (!options.dryRun) {
await unlink(resolveWithin(root, known.path)).catch(() => {});
delete state.files[entry.fileId];
}
summary.removed++;
options.onEvent({ type: 'remove', path: known.path, kept: false });
} else {
// Default is to keep: a teacher removing a worksheet is not a reason to
// destroy the student's copy of it.
summary.kept++;
options.onEvent({ type: 'remove', path: known.path, kept: true });
}
}
if (!options.dryRun) {
state.cursor = manifest.cursor;
await saveState(root, state);
}
return summary;
}
async function syncOne(
api: ApiClient,
root: string,
entry: ManifestEntry,
state: SyncState,
options: SyncOptions,
summary: SyncSummary,
): Promise<void> {
// resolveWithin is the guard: every path component originated in Schulcloud.
const target = resolveWithin(root, entry.path);
const known = state.files[entry.fileId];
if (known) {
if (known.path !== entry.path) {
// Same record, new location — the board or card was renamed upstream.
if (!options.dryRun) {
const from = resolveWithin(root, known.path);
await mkdir(dirname(target), { recursive: true });
await rename(from, target).catch(async () => {
// A failed move is not fatal; fall back to downloading afresh.
await download(api, entry, target, options);
});
state.files[entry.fileId] = { path: entry.path, size: entry.size, syncedAt: new Date().toISOString() };
}
summary.moved++;
options.onEvent({ type: 'move', entry, from: known.path });
return;
}
const info = await stat(target).catch(() => undefined);
if (info?.isFile() && info.size === entry.size) {
summary.skipped++;
options.onEvent({ type: 'skip', entry });
return;
}
// Size is the only validator available — the API exposes no checksum or
// ETag — but it reliably catches a truncated or interrupted download.
options.onEvent({ type: 'download', entry, reason: info ? 'size-mismatch' : 'missing' });
} else {
options.onEvent({ type: 'download', entry, reason: 'new' });
}
if (options.dryRun) {
summary.downloaded++;
summary.bytes += entry.size;
return;
}
await download(api, entry, target, options);
state.files[entry.fileId] = { path: entry.path, size: entry.size, syncedAt: new Date().toISOString() };
summary.downloaded++;
summary.bytes += entry.size;
}
async function download(api: ApiClient, entry: ManifestEntry, target: string, _options: SyncOptions): Promise<void> {
const response = await api.file(entry.fileId);
if (!response.body) throw new Error('empty response body');
await mkdir(dirname(target), { recursive: true });
// Write to a temporary neighbour and rename, so an interrupted sync never
// leaves a half-file that a later run would mistake for complete.
const temp = `${target}.part`;
await pipeline(Readable.fromWeb(response.body as never), createWriteStream(temp));
await rename(temp, target);
}