File size: 12,274 Bytes
ff34739
 
8ffc9f7
 
 
ff34739
ee0c38c
b5d4bb5
 
ff34739
 
8ffc9f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37a1395
 
 
 
 
8ffc9f7
 
 
 
 
 
 
 
37a1395
 
 
 
 
8ffc9f7
 
 
 
 
 
 
 
bd7cf06
b303f51
 
bd7cf06
 
 
b303f51
 
 
 
 
 
bd7cf06
 
b303f51
37a1395
 
b303f51
 
bd7cf06
 
 
 
b303f51
 
8ffc9f7
b303f51
 
8ffc9f7
 
 
 
 
b303f51
 
 
 
 
 
 
 
8ffc9f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff34739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b303f51
ff34739
 
b303f51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff34739
 
 
 
 
 
 
 
 
 
 
b303f51
ff34739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5d4bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff34739
b5d4bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff34739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import path from "node:path";
import { promises as fs } from "node:fs";
import { assets, DEFAULT_MODEL, PYTHON, REPO_ROOT } from "./config";
import { inProject, runDir } from "./paths";
import { allocateRun, getProject, getRun, updateRun } from "./store";
import { killGroup, spawnLogged } from "./spawn";
import { generateAllFixes } from "./remediate";
import { appendConversation } from "./chat";
import { knowledgeFor } from "./reportHtml";
import type { QcReport, RunRecord, StepName, StepStatus } from "./types";

async function fileExists(p: string): Promise<boolean> {
  try {
    await fs.access(p);
    return true;
  } catch {
    return false;
  }
}

/**
 * Allocate + launch a QC run against the project's confirmed spec and reads.
 * Shared by POST /runs and POST /confirm-spec. Returns the new runId, or an error.
 */
export async function startQcRun(
  projectId: string,
  opts: {
    useLlm?: boolean;
    fastqSource?: "control" | "sim" | "upload" | "remediated";
    maxReads?: number | null;
  } = {},
): Promise<{ runId: string } | { error: string }> {
  let project;
  try {
    project = await getProject(projectId);
  } catch {
    return { error: "project not found" };
  }
  const useLlm = opts.useLlm !== false;
  const fastqSource = (
    ["control", "upload", "remediated"] as const
  ).includes(opts.fastqSource as never)
    ? (opts.fastqSource as "control" | "upload" | "remediated")
    : "sim";
  const maxReads =
    typeof opts.maxReads === "number" && opts.maxReads > 0 ? Math.floor(opts.maxReads) : null;

  // Spec: the project's extracted spec if present, else the packaged reference.
  const specSource =
    project.activeSpecPath && (await fileExists(inProject(projectId, project.activeSpecPath)))
      ? inProject(projectId, project.activeSpecPath)
      : assets.referenceSpec;

  // Nanopore = a single long-read file (no R2, no whitelist); short-read = paired R1/R2.
  const single = project.platform === "nanopore";
  let r1: string, r2: string;
  if (fastqSource === "upload") {
    const fq = project.inputs.fastq;
    if (!fq || fq.source !== "upload" || !fq.r1 || (!single && !fq.r2)) {
      return {
        error: single
          ? "upload your reads FASTQ before running"
          : "upload both R1 and R2 FASTQ before running on your reads",
      };
    }
    r1 = inProject(projectId, fq.r1);
    r2 = single ? "" : inProject(projectId, fq.r2!);
  } else if (fastqSource === "remediated") {
    // Cleaned reads written by a remediation script.
    r1 = inProject(projectId, single ? "remediated/reads.fastq.gz" : "remediated/R1.fastq.gz");
    r2 = single ? "" : inProject(projectId, "remediated/R2.fastq.gz");
  } else {
    r1 = fastqSource === "control" ? assets.control.r1 : assets.sim.r1;
    r2 = fastqSource === "control" ? assets.control.r2 : assets.sim.r2;
  }
  const whitelist = !single && (await fileExists(assets.whitelist)) ? assets.whitelist : null;
  // Ground-truth labels only exist for the simulated (short-read) dataset (enables the eval panel).
  const labels =
    !single && fastqSource === "sim" && (await fileExists(assets.sim.labels)) ? assets.sim.labels : null;
  if (!(await fileExists(r1)) || (!single && !(await fileExists(r2)))) {
    return { error: `reads not found for source "${fastqSource}"` };
  }

  const run = await allocateRun(projectId, {
    pipeline: ["qc"],
    options: {
      useLlm,
      maxReads,
      withLabels: !!labels,
      withWhitelist: !!whitelist,
      fastqSource,
      platform: project.platform,
    },
    inputsSnapshot: { specPath: "", r1, r2, whitelist, labels },
    steps: { qc: { name: "qc", status: "queued", log: "logs/qc.log" } },
    overallStatus: "queued",
  });

  // Snapshot the spec into the run dir for immutable provenance, then record its path.
  const snapshot = path.join(runDir(projectId, run.id), "spec.json");
  await fs.copyFile(specSource, snapshot);
  await updateRun(projectId, run.id, (r) => ({
    ...r,
    inputsSnapshot: { ...r.inputsSnapshot, specPath: snapshot },
  }));

  launchRun(projectId, run.id);
  return { runId: run.id };
}

/** In-memory live-process registry (for cancel) — run.json remains the durable truth. */
const livePids = new Map<string, number>(); // `${projectId}:${runId}:${step}` -> pid
const canceled = new Set<string>(); // `${projectId}:${runId}`

const stepKey = (p: string, r: string, s: string) => `${p}:${r}:${s}`;
const runKey = (p: string, r: string) => `${p}:${r}`;
const now = () => new Date().toISOString();

// ---- crude global concurrency cap (pipeline steps stream whole FASTQs + cost money) ----
const MAX_ACTIVE = 2;
let active = 0;
const waiters: Array<() => void> = [];

async function acquire(): Promise<void> {
  if (active < MAX_ACTIVE) {
    active++;
    return;
  }
  await new Promise<void>((res) => waiters.push(res)); // slot handed over on release
}
function release(): void {
  const w = waiters.shift();
  if (w) w();
  else active--;
}

// ---- argv construction per step (uses the granular module CLIs, never the wrapper) ----

function buildArgs(step: StepName, run: RunRecord, dir: string): string[] {
  const snap = run.inputsSnapshot;
  const jsonOut = path.join(dir, "qc_report.json");
  switch (step) {
    case "qc":
      // Nanopore: the single-long-read QC engine (no --r2 / --whitelist / --max-reads).
      if (run.options.platform === "nanopore") {
        return [
          "-m",
          "qc.nanopore",
          "--spec",
          snap.specPath,
          "--reads",
          snap.r1,
          "--json-out",
          jsonOut,
          "--model",
          DEFAULT_MODEL,
          ...(snap.labels ? ["--labels", snap.labels] : []),
          ...(run.options.useLlm ? [] : ["--no-llm"]),
        ];
      }
      return [
        "-m",
        "qc",
        "run",
        "--spec",
        snap.specPath,
        "--r1",
        snap.r1,
        "--r2",
        snap.r2,
        "--json-out",
        jsonOut,
        "--model",
        DEFAULT_MODEL,
        ...(snap.whitelist ? ["--whitelist", snap.whitelist] : []),
        ...(snap.labels ? ["--labels", snap.labels] : []),
        ...(run.options.useLlm ? [] : ["--no-llm"]),
        ...(run.options.maxReads ? ["--max-reads", String(run.options.maxReads)] : []),
      ];
    default:
      // extract / simulate wired in Phase 2
      throw new Error(`step not implemented: ${step}`);
  }
}

async function readQcOverall(dir: string): Promise<QcReport["overall"] | null> {
  try {
    const report = JSON.parse(
      await fs.readFile(path.join(dir, "qc_report.json"), "utf8"),
    ) as QcReport;
    return report.overall ?? null;
  } catch {
    return null;
  }
}

async function runStep(
  projectId: string,
  runId: string,
  step: StepName,
): Promise<StepStatus> {
  const dir = runDir(projectId, runId);
  const logFile = path.join(dir, "logs", `${step}.log`);
  const run = await getRun(projectId, runId);
  const args = buildArgs(step, run, dir);

  await updateRun(projectId, runId, (r) => ({
    ...r,
    steps: {
      ...r.steps,
      [step]: { ...r.steps[step]!, status: "running", startedAt: now() },
    },
  }));

  const started = Date.now();
  const proc = spawnLogged({ cmd: PYTHON, args, cwd: REPO_ROOT, logFile });
  livePids.set(stepKey(projectId, runId, step), proc.pid);
  await updateRun(projectId, runId, (r) => ({
    ...r,
    steps: { ...r.steps, [step]: { ...r.steps[step]!, pid: proc.pid } },
  }));

  const code = await proc.done;
  livePids.delete(stepKey(projectId, runId, step));
  const durationMs = Date.now() - started;

  const wasCanceled = canceled.has(runKey(projectId, runId));
  const status: StepStatus = wasCanceled ? "canceled" : code === 0 ? "succeeded" : "failed";

  const overall = step === "qc" && status === "succeeded" ? await readQcOverall(dir) : undefined;

  await updateRun(projectId, runId, (r) => ({
    ...r,
    overall: overall !== undefined ? overall : r.overall,
    steps: {
      ...r.steps,
      [step]: {
        ...r.steps[step]!,
        status,
        exitCode: code,
        finishedAt: now(),
        durationMs,
        error:
          status === "failed"
            ? `${step} exited with code ${code} — see the log`
            : undefined,
      },
    },
  }));

  return status;
}

async function driveRun(projectId: string, runId: string): Promise<void> {
  await acquire();
  try {
    const run = await updateRun(projectId, runId, (r) => ({
      ...r,
      overallStatus: "running",
      startedAt: now(),
    }));

    for (const step of run.pipeline) {
      if (canceled.has(runKey(projectId, runId))) {
        await finalize(projectId, runId, "canceled");
        return;
      }
      const status = await runStep(projectId, runId, step);
      if (status !== "succeeded") {
        await finalize(projectId, runId, status === "canceled" ? "canceled" : "failed");
        return;
      }
    }
    await finalize(projectId, runId, "succeeded");
  } finally {
    canceled.delete(runKey(projectId, runId));
    release();
  }
}

/** A conversational diagnosis + suggested-fix summary from the report (posted after each QC run). */
function buildDiagnosisMessage(report: QcReport): string {
  const issues = (report.findings ?? []).filter((f) => f.verdict === "fail" || f.verdict === "warn");
  const verdict = (report.overall ?? "").toUpperCase();
  const lines: string[] = [`**QC complete — overall ${verdict || "done"}.**`];
  const plan = report.plan;
  if (plan?.diagnosis) lines.push(plan.diagnosis);
  else if (plan?.root_cause) lines.push(`Likely root cause: ${plan.root_cause}.`);
  if (issues.length) {
    lines.push("", "**Issues & suggested fixes:**");
    for (const f of issues.slice(0, 6)) {
      const inlineFix = /Fix:/i.test(f.detail);
      const fix = inlineFix ? null : knowledgeFor(f.check_id)?.fix ?? null;
      lines.push(`- **${f.title}** — ${f.detail}${fix ? `\n  _Suggested fix:_ ${fix}` : ""}`);
    }
  } else {
    lines.push("", "All checks passed — the reads are consistent with the expected structure.");
  }
  lines.push(
    "",
    "The full report (root cause + suggested fix per issue) is open in the viewer. Any " +
      "computationally-fixable issue also appears in the **Computational fixes** panel below — tick " +
      "them and Apply to clean the reads and re-score.",
  );
  return lines.join("\n");
}

async function finalize(projectId: string, runId: string, status: StepStatus): Promise<void> {
  if (status === "succeeded") {
    try {
      const report = JSON.parse(
        await fs.readFile(path.join(runDir(projectId, runId), "qc_report.json"), "utf8"),
      ) as QcReport;
      // Post the diagnosis + suggested fixes into the conversation (not only in the report). Awaited
      // BEFORE the status flips so the client's post-run hydrate always sees it.
      await appendConversation(projectId, [
        { role: "assistant", text: buildDiagnosisMessage(report), ts: now() },
      ]);
      // Eager remediation (fire-and-forget): only for a first-pass run, not a re-QC on cleaned reads.
      const cur = await getRun(projectId, runId);
      if (cur.options.fastqSource !== "remediated") generateAllFixes(projectId, report);
    } catch {
      /* best effort — diagnosis/remediation are optional */
    }
  }
  await updateRun(projectId, runId, (r) => ({
    ...r,
    overallStatus: status,
    finishedAt: now(),
  }));
}

/** Fire-and-forget launcher. The HTTP request returns immediately; this drives to completion. */
export function launchRun(projectId: string, runId: string): void {
  void driveRun(projectId, runId).catch(async (err) => {
    try {
      await updateRun(projectId, runId, (r) => ({
        ...r,
        overallStatus: "failed",
        finishedAt: now(),
      }));
    } catch {
      /* best effort */
    }
    console.error(`run ${projectId}/${runId} crashed:`, err);
  });
}

/** Request cancellation: flag the run and kill any live step's process group. */
export function cancelRun(projectId: string, runId: string): boolean {
  canceled.add(runKey(projectId, runId));
  let killed = false;
  for (const [k, pid] of livePids) {
    if (k.startsWith(`${projectId}:${runId}:`)) {
      killGroup(pid);
      killed = true;
    }
  }
  return killed;
}