Here is the full picture of every relevant layer, with file paths and line numbers:
Route Handler — POST /api/traces/:id/pause
File: packages/backend/src/routes/handoff.ts
Lines 244–285
// Line 244
/**
* POST /api/traces/:id/pause
* 暂停 trace(用户主动暂停)
*/
app.post<{ Params: PauseResumeParams }>(
"/:id/pause", // line 249
{
schema: {
params: {
type: "object",
required: ["id"],
properties: {
id: { type: "string", minLength: 1 },
},
},
},
},
async (req: FastifyRequest<{ Params: PauseResumeParams }>, reply: FastifyReply) => {
const traceId = req.params.id; // line 262
const trace = getTraceById(traceId); // line 264 — fetch to verify existence
if (!trace) {
return reply.status(404).send({ // line 265–268 — 404 if not found
success: false,
error: "Trace not found",
});
}
const updatedTrace = transitionToPaused(traceId); // line 272 — core service call
if (!updatedTrace) {
return reply.status(400).send({ // line 273–277 — 400 if state invalid
success: false,
error: "Cannot pause trace in current state",
});
}
return reply.send({ // line 280–283 — 200 success
success: true,
data: updatedTrace,
});
}
);
Key imports at the top of the file (lines 14–25):
import {
getTraceById,
updateTrace,
transitionToPaused,
transitionFromPaused,
// ...
} from "../services/traceService.js";
Service Logic — transitionToPaused
File: packages/backend/src/services/traceService.ts, lines 704–721
/**
* 将 trace 转为 paused 状态。
* 用于用户主动暂停的场景。
*/
export function transitionToPaused(traceId: string): Trace | null {
const trace = getTraceById(traceId); // line 709 — re-fetch for state check
if (!trace) {
return null;
}
// Only allows pausing from 'running' or 'in_progress'
if (trace.status !== 'running' && trace.status !== 'in_progress') { // line 714
return null;
}
const updated = updateTrace(traceId, { status: 'paused' }); // line 718
console.log(`[traceService] transitionToPaused: ${traceId}`); // line 719
return updated;
}
Database Logic — updateTrace
File: packages/backend/src/services/traceService.ts, lines 175–203
export function updateTrace(id: string, req: UpdateTraceRequest): Trace | null {
const db = getDatabase();
const existing = getTraceById(id); // line 177 — confirm still exists
if (!existing) return null;
const now = new Date().toISOString();
const taskGoal = req.taskGoal ?? existing.taskGoal;
const status = req.status ?? existing.status; // line 184 — 'paused' is applied here
// Merge affected_files: union of existing and new, deduplicated
const mergedFiles = req.affectedFiles !== undefined
? [...new Set([...existing.affectedFiles, ...req.affectedFiles])]
: existing.affectedFiles;
db.prepare(`
UPDATE traces
SET task_goal = @task_goal,
status = @status,
affected_files = @affected_files,
updated_at = @updated_at
WHERE id = @id
`).run({ // lines 191–200 — parameterised SQLite UPDATE
id,
task_goal: taskGoal,
status,
affected_files: toJson(mergedFiles),
updated_at: now,
});
return getTraceById(id); // line 202 — re-fetch and return updated trace
}
Summary of the call chain
POST /api/traces/:id/pause
packages/backend/src/routes/handoff.ts (lines 248–285)
└── getTraceById(traceId) — existence check (line 264)
└── transitionToPaused(traceId) — service call (line 272)
packages/backend/src/services/traceService.ts (lines 708–721)
└── getTraceById(traceId) — state check (line 709)
└── guard: status must be 'running' or 'in_progress' (line 714)
└── updateTrace(traceId, { status: 'paused' }) (line 718)
traceService.ts (lines 175–203)
└── db.prepare('UPDATE traces SET status=... WHERE id=?').run(...)
— parameterised better-sqlite3 UPDATE (lines 191–200)