π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import type { VerifyCheckItem } from '@lobechat/types';
import { isAgentOperationInFlight } from '@lobechat/types';
import debug from 'debug';
import { AgentOperationModel } from '@/database/models/agentOperation';
import { MessageModel } from '@/database/models/message';
import { VerifyCheckResultModel } from '@/database/models/verifyCheckResult';
import { VerifyRunModel } from '@/database/models/verifyRun';
import type { VerifyRunItem } from '@/database/schemas/verify';
import type { LobeChatDatabase } from '@/database/type';
import { redriveClaimedVerification } from './lifecycle';
import { settleFailedRepair } from './repairTerminal';
import { planItemToPendingResult } from './resultSnapshot';
import { finalizeVerifyRun } from './settle';
import { VERIFY_ABANDONED_MS, VERIFY_ROLLUP_GRACE_MS } from './staleness';
import { VerifyStatusService } from './statusService';
import { releaseUndeliveredRound } from './undeliveredRound';
const log = debug('lobe-server:verify-sweep');
const PENDING_RESULT_STATUSES = new Set(['pending', 'running']);
const SWEEP_PAGE_SIZE = 100;
/** Bound one tick's work; whatever is left is still there on the next one. */
const SWEEP_MAX_RUNS = 1000;
export interface VerifySweepOutcome {
/** Runs whose outstanding checks were closed as `errored` before the rollup. */
abandoned: string[];
/** Runs whose judging never started a verifier and was started again. */
redriven: string[];
/** Task rounds taken off their acceptance because the run never delivered. */
released: string[];
/** Runs whose checks had all landed and only needed the missing rollup. */
settled: string[];
/** Runs left alone β still plausibly mid-flight. */
skipped: number;
}
/**
* Recover verification runs stranded in `verifying`, planned repairs whose
* operation has already failed or been interrupted, and task rounds whose run
* ended without a delivery (see {@link releaseUndeliveredRound}).
*
* Entering `verifying` is a durable write; the judging that leaves it runs as
* post-response work, so any host-level interruption β instance recycled,
* deploy, OOM, provider hang β strands the run. Nothing downstream re-reads it:
* the rollup is denormalized, the read paths trust it, and the task watchdog
* only looks at tasks carrying a heartbeat timeout. Without this sweep such a
* run is stuck for good, and so is the acceptance and goal card above it.
*
* Two shapes, deliberately treated differently:
*
* - **the rollup was lost** β every required check already holds a terminal
* verdict, so the truth is on disk and only the denormalized status disagrees.
* Recomputing is pure derivation; the only reason to wait out
* {@link VERIFY_ROLLUP_GRACE_MS} first is to stay clear of a live finalizer.
* - **the verifier died mid-flight** β checks are still pending/running. Those
* carry no verdict and never will, so after {@link VERIFY_ABANDONED_MS} they
* are closed as `errored` (an infra failure, not a delivery judgment, so they
* neither gate delivery nor seed auto-repair) and the rollup follows.
* - **no verifier ever started** β the attempt died between claiming
* `verifying` and spawning its verifiers, so the builder's evidence was never
* judged at all. Closing it as `errored` would end the round without a
* verdict, so the latest round of a successful run is judged again, once
* (see {@link canRedrive}). A second stall falls back to `errored`.
*
* A round a later round of its acceptance superseded only gets its rollup; the
* task is driven by the latest round alone.
*
* A check bound to a verifier operation that is still live keeps the whole run
* out of the sweep however old it is β an agent verifier is a full sub-agent run
* and its own terminal hook is what settles it.
*
* Recovery is leased per run (see {@link recoverRun}) so overlapping deliveries
* of the cron cannot both drive the same run's finalizer.
*/
export const sweepStuckVerifyRuns = async (
db: LobeChatDatabase,
options?: { now?: Date; pageSize?: number },
): Promise<VerifySweepOutcome> => {
const now = options?.now ?? new Date();
const pageSize = options?.pageSize ?? SWEEP_PAGE_SIZE;
const staleBefore = new Date(now.getTime() - VERIFY_ROLLUP_GRACE_MS);
const outcome: VerifySweepOutcome = {
abandoned: [],
redriven: [],
released: [],
settled: [],
skipped: 0,
};
// Walk the whole stranded set, not just its oldest page: rows the sweep leaves
// alone keep their timestamp, so a single fixed-size read would return the same
// untouchable rows forever and never reach the runs behind them.
let after: { id: string; updatedAt: Date } | undefined;
let scanned = 0;
while (scanned < SWEEP_MAX_RUNS) {
const page = await VerifyRunModel.findStuckVerifying(db, staleBefore, {
after,
limit: pageSize,
});
if (page.length === 0) break;
for (const run of page) {
scanned += 1;
try {
const action = await recoverRun(db, run, now);
if (action === 'skipped') outcome.skipped += 1;
else outcome[action].push(run.id);
} catch (error) {
// One poisoned run must not stop the sweep for the rest.
log('recovering run %s failed (non-fatal): %O', run.id, error);
outcome.skipped += 1;
}
}
const last = page.at(-1)!;
after = { id: last.id, updatedAt: last.updatedAt };
if (page.length < pageSize) break;
}
if (scanned >= SWEEP_MAX_RUNS) {
log('sweep hit the per-run cap (%d) β the tail is left for the next tick', SWEEP_MAX_RUNS);
}
return outcome;
};
const recoverRun = async (
db: LobeChatDatabase,
run: VerifyRunItem,
now: Date,
): Promise<'abandoned' | 'redriven' | 'released' | 'settled' | 'skipped'> => {
const operationId = run.operationId;
if (!operationId) return 'skipped';
const workspaceId = run.workspaceId ?? undefined;
if (run.status === 'planned') {
if (await settleFailedRepair(db, run.userId, operationId, workspaceId)) return 'abandoned';
return (await releaseUndeliveredRound(db, run.userId, operationId, workspaceId))
? 'released'
: 'skipped';
}
const plan = (run.plan ?? []) as VerifyCheckItem[];
if (plan.length === 0) return 'skipped';
const resultModel = new VerifyCheckResultModel(db, run.userId, workspaceId);
const results = await resultModel.listByRun(run.id);
const byItem = new Map(results.map((result) => [result.checkItemId, result]));
// Only required items decide the rollup β the same gate `recompute` applies.
const outstanding = plan
.filter((item) => item.required)
.filter((item) => {
const result = byItem.get(item.id);
return !result || PENDING_RESULT_STATUSES.has(result.status);
});
const operationModel = new AgentOperationModel(db, run.userId, workspaceId);
const latest = await isLatestRound(db, run);
let redrive = false;
if (outstanding.length > 0) {
if (now.getTime() - run.updatedAt.getTime() < VERIFY_ABANDONED_MS) return 'skipped';
for (const item of outstanding) {
const verifierOperationId = byItem.get(item.id)?.verifierOperationId;
if (!verifierOperationId) continue;
const verifierOp = await operationModel.findById(verifierOperationId);
// Its verifier is still working β `settleVerifierCheckFromTerminal` owns
// this row's ending, and stamping it `errored` now would discard a verdict
// that is still coming.
// A still-live verifier (parked ones included) can still produce a verdict.
if (verifierOp && isAgentOperationInFlight(verifierOp.status)) return 'skipped';
}
redrive = latest && (await canRedrive(run, outstanding, byItem, operationModel));
}
// Committed to acting β take the lease first. Everything below has side effects
// beyond this run: `finalizeVerifyRun` spawns the repair round, and
// `triggerAutoRepair` has no claim of its own, so two overlapping sweep
// deliveries reaching it would launch duplicate repair agents against the same
// failures. The claim re-stamps `updated_at`, which is exactly what a
// concurrent sweep's CAS tests against, so the loser drops the run.
//
// Deliberately after the skip decisions: claiming a run we then leave alone
// would push its `updated_at` forward every tick and it would never reach the
// abandoned bound.
const statusService = new VerifyStatusService(db, run.userId, workspaceId);
if (
!(await statusService.claimVerifying(
operationId,
new Date(now.getTime() - VERIFY_ROLLUP_GRACE_MS),
))
)
return 'skipped';
if (redrive) {
// Stamped before judging so a re-drive that dies the same way is not
// re-driven forever: the next tick sees the stamp and closes it as errored.
await new VerifyRunModel(db, run.userId, workspaceId).recordVerifierRedrive(run.id, now);
const builder = await operationModel.findById(operationId);
const deliverable = await resolveFinalDeliverable(
db,
run.userId,
{ operationId, topicId: builder?.topicId ?? undefined },
workspaceId,
);
await redriveClaimedVerification(db, run.userId, { deliverable, operationId }, workspaceId);
log('re-drove run %s (op %s): no verifier had started', run.id, operationId);
return 'redriven';
}
if (outstanding.length > 0) {
await Promise.all(
outstanding.map((item) =>
// Upsert, not update: a run interrupted between entering `verifying` and
// creating its pending rows has plan items with no row at all. Updating
// would touch nothing, `recompute` would still read them as pending, and
// the run would stay stranded while the sweep reported it recovered.
resultModel.upsertByCheckItem({
...planItemToPendingResult(run.id, operationId, item),
// Re-asserted after the spread: the upsert key is required, and the
// snapshot's own fields are optional-nullable.
checkItemId: item.id,
verifyRunId: run.id,
completedAt: now,
status: 'errored',
suggestion: 'Rerun verification for this delivery.',
toulmin: { limitation: 'Verification was interrupted before this check was judged.' },
}),
),
);
}
await statusService.recompute(operationId);
// No report context β the sweep holds no deliverable. The task is still driven,
// which is the whole point: the goal has been waiting on this verdict. Not for
// a superseded round, though: its task now waits on the later round, and
// driving it from here would pause the task under that round's feet.
if (latest) await finalizeVerifyRun(db, run.userId, operationId, {}, workspaceId);
const action = outstanding.length > 0 ? 'abandoned' : 'settled';
log('recovered run %s (op %s) as %s', run.id, operationId, action);
return action;
};
/** Whether no later round of the same acceptance superseded this one. */
const isLatestRound = async (db: LobeChatDatabase, run: VerifyRunItem): Promise<boolean> => {
if (!run.acceptanceId) return true;
const rounds = await new VerifyRunModel(
db,
run.userId,
run.workspaceId ?? undefined,
).listByAcceptance(run.acceptanceId);
return rounds.every((round) => (round.roundIndex ?? 0) <= (run.roundIndex ?? 0));
};
/**
* Whether a stalled latest round should be judged again rather than closed as
* errored. Only when nothing was ever judged (no outstanding check reached a
* verifier), the builder run finished successfully, and the round was not
* re-driven before.
*/
const canRedrive = async (
run: VerifyRunItem,
outstanding: VerifyCheckItem[],
byItem: Map<string, { verifierOperationId: string | null }>,
operationModel: AgentOperationModel,
): Promise<boolean> => {
if (run.metadata?.verifierRedrivenAt) return false;
if (outstanding.some((item) => byItem.get(item.id)?.verifierOperationId)) return false;
const builder = await operationModel.findById(run.operationId!);
return builder?.status === 'done';
};
/**
* The builder's final reply, read back the way the completion gate received it:
* the run's delivery-checker card hangs off the assistant message that
* completion carried.
*/
const resolveFinalDeliverable = async (
db: LobeChatDatabase,
userId: string,
{ operationId, topicId }: { operationId: string; topicId?: string },
workspaceId?: string,
): Promise<string> => {
const messageModel = new MessageModel(db, userId, workspaceId);
const card = await messageModel.findVerifyMessageByOperationId(operationId, topicId);
if (!card?.parentId) return '';
const reply = await messageModel.findById(card.parentId);
return typeof reply?.content === 'string' ? reply.content : '';
};