Files
learnharness/app/api/activity/abort/route.ts
T
amosblomqvist 27c5d9df19 practice slice 4 — pipeline, registry, API, feed
src/agent/subagents/practice-pipeline.ts (sibling of practice.ts —
orchestration vs calls): createPracticePipeline(graphStore+practiceStore
+chatStore+PracticeModels). Fire-and-forget front doors returning
{set, done}: createProblemSet (refuse → resolve selection (chatId =
ANY-provenance membership per brief; nodeIds raw) → chains → snapshot →
createSet partitioning → registry row → runPartitioner → stubs land
(generating) → ONE generator per problem behind the process-wide
3-permit abort-aware semaphore (__lhPracticeSlots, extraction idiom) →
per-problem failure isolation → ready (errored problems included);
abortPractice (idempotent: controllers + 'Aborted' marks; dropped
grades keep answers, re-submittable); submitAnswers (ready-only,
already-graded rejects, one grading job per answer under the same
semaphore); deleteProblemSet (abort-first). Per-set write mutex on
globalThis (__lhPracticeLocks, audit-lock idiom); registry
__lhPracticeRuns one row per set per kind; boot sweep marks orphaned
partitioning/generating sets 'Interrupted (server restarted)'. Typed
errors: EmptySelection/UnknownChat/SetNotFound/SetNotReady/BadAnswers.
Usage folds stage logs into the registry row (v1: completion-folded).

src/server/practice.ts: thin service — strict params parsing, typed
error → response mapping (503 envelope family, 400/404/409), list/get.
Routes: POST+GET /api/practice/sets (202 {id}), GET+DELETE
/api/practice/sets/[id], POST /api/practice/sets/[id]/answers (202).
Composition root: chat-handler wires createPracticePipeline (engineFor
fresh + providerKeyMissing gate) exported as ; barrel re-
exports. Feed: observability activityFeed() += practice rows
(ActivityPractice); dock blobs (process 'practice', GraduationCap
icon, set-keyed abort); /api/activity/abort kind 'practice'.

tests/practice-pipeline.test.ts — 13 tests: end-to-end (stubs mid-
flight via gated generators, coverage complete), failure isolation,
3-permit semaphore pin, abort mid-generation, registry shape pins,
refusal-before-create, chatId any-provenance selection, typed errors,
answers→grades land + graded rejects, boot sweep (orphan + live-row),
delete. 1114/1114, tsc 0, lint 0, build 0.
2026-09-29 18:31:53 +03:00

62 lines
2.7 KiB
TypeScript

/**
* POST /api/activity/abort — the dock's END/ABORT control, uniform across
* process kinds (user-decided contract): body {kind: "job"|"extraction"|"turn", id}
* where id is the jobId for sub-agent jobs, the chatId for extraction runs
* (the pipeline registry is chat-keyed), and the chatId for turns
* (background-turn wave, 2026-09-25: a running turn is a background
* object — any client may stop it here; a view leaving NEVER kills it).
* Mechanisms live in their domains — abortJob (server/jobs.ts, agents
* stream), abortExtraction (the pipeline module, graph stream), and
* abortTurn (server/turn-runs.ts, the running-turn registry); this route
* is the thin composition.
*
* Outcomes: a job's not_found -> 404, not_running -> 409, aborted -> 200.
* A turn mirrors the job semantics: not_running -> 409 (honest "nothing to
* abort" — the caller refetches), aborted -> 200 (partial persists via
* the usual settle path, every subscriber closes cleanly).
* Extraction abort is idempotent by design (aborting with nothing in
* flight is success — the blob is gone either way) -> 200.
*/
import { abortExtraction, abortJob, abortTurn, practice } from "@/src/server";
export async function POST(req: Request) {
let body: unknown;
try {
body = await req.json();
} catch {
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
}
const { kind, id } = (body ?? {}) as { kind?: string; id?: string };
if ((kind !== "job" && kind !== "extraction" && kind !== "turn" && kind !== "practice") || typeof id !== "string" || id === "") {
return Response.json({ error: 'Body must be {kind: "job"|"extraction"|"turn"|"practice", id: string}' }, { status: 400 });
}
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
return Response.json({ error: "Invalid id" }, { status: 400 });
}
if (kind === "job") {
const outcome = abortJob(id);
if (!outcome.ok) {
const status = outcome.reason === "not_found" ? 404 : 409;
return Response.json({ ok: false, reason: outcome.reason }, { status });
}
return Response.json({ ok: true });
}
if (kind === "turn") {
const outcome = abortTurn(id);
if (!outcome.ok) return Response.json({ ok: false, reason: outcome.reason }, { status: 409 });
return Response.json({ ok: true });
}
// kind === "extraction": true cancellation; idempotent (nothing in
// flight is success — same dock outcome)
if (kind === "practice") {
// practice slice 4 (2026-09-29): abort BOTH of a set's live runs
// (generation + grading) and mark the persisted "Aborted" state —
// idempotent like extraction (nothing in flight is success).
practice.abortPractice(id);
return Response.json({ ok: true });
}
abortExtraction(id);
return Response.json({ ok: true });
}