mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-05 23:06:17 +01:00
Merge current Runner backlog and recovery-census changes
* commit 'da8cfda42e499e0c3a5206ef44b7d67407022585': perf(recovery): stop counting a run start as a change to every task of its agent (#21831) perf(runner): check a listening Runner's backlog every 10 ms, not every reconnect delay (#21821)
This commit is contained in:
commit
3d640106a6
7 files changed
+372
-6
No files matched your search
@@ -8,6 +8,12 @@ agent or recovery of its persisted run. On a fresh launch or restart,
|
||||
- With `--listen-address 0.0.0.0 --listen-path /api/runner/v1/connect/<run-id>`,
|
||||
the Runner listens for Core. `--listen-port` defaults to `43127`.
|
||||
|
||||
A dialing Runner waits `--reconnect-delay-ms` (default 250) between
|
||||
connection attempts and backs off after a failed one. A listening Runner
|
||||
checks for an incoming connection every 10 ms (or sooner, when that delay is
|
||||
smaller). It never redials, so a connection does not wait out the reconnect
|
||||
delay after a disconnect or an authority rotation.
|
||||
|
||||
Choose one form. Both require `PAPERCLIP_RUNNER_BOOTSTRAP_TICKET`.
|
||||
Recovery uses the same entry point. The Runner rejects `--source-reconcile`.
|
||||
|
||||
|
||||
@@ -730,7 +730,8 @@ pub fn run_durable_runner_with_host_control<E: CommandExecutor>(
|
||||
let (mut transport, welcome) = match connection {
|
||||
Ok(Some(connection)) => connection,
|
||||
Ok(None) => {
|
||||
next_connection_attempt = Instant::now() + config.reconnect_delay;
|
||||
next_connection_attempt =
|
||||
Instant::now() + endpoint.retry_delay(config.reconnect_delay);
|
||||
continue;
|
||||
}
|
||||
Err(error) => {
|
||||
@@ -749,7 +750,7 @@ pub fn run_durable_runner_with_host_control<E: CommandExecutor>(
|
||||
}
|
||||
next_connection_attempt = Instant::now()
|
||||
+ reconnect_delay(
|
||||
config.reconnect_delay,
|
||||
endpoint.retry_delay(config.reconnect_delay),
|
||||
connect_deadline.saturating_duration_since(Instant::now()),
|
||||
&mut reconnect_attempt,
|
||||
);
|
||||
@@ -1055,8 +1056,8 @@ pub fn run_durable_runner_with_host_control<E: CommandExecutor>(
|
||||
.map(|credential| credential.expires_at_unix_ms),
|
||||
);
|
||||
next_connection_attempt = Instant::now()
|
||||
+ config
|
||||
.reconnect_delay
|
||||
+ endpoint
|
||||
.retry_delay(config.reconnect_delay)
|
||||
.min(reconnect_deadline.saturating_duration_since(Instant::now()));
|
||||
continue;
|
||||
}
|
||||
@@ -1419,8 +1420,8 @@ pub fn run_durable_runner_with_host_control<E: CommandExecutor>(
|
||||
.map(|credential| credential.expires_at_unix_ms),
|
||||
);
|
||||
next_connection_attempt = Instant::now()
|
||||
+ config
|
||||
.reconnect_delay
|
||||
+ endpoint
|
||||
.retry_delay(config.reconnect_delay)
|
||||
.min(reconnect_deadline.saturating_duration_since(Instant::now()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ const QUEUED_CONTROL_READ_TIMEOUT: Duration = Duration::from_millis(1);
|
||||
const CONNECT_ATTEMPT_TIMEOUT: Duration = Duration::from_secs(3);
|
||||
const CONNECT_TOTAL_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
const MAX_CA_BUNDLE_BYTES: u64 = 4 * 1024 * 1024;
|
||||
const LISTENER_ACCEPT_INTERVAL: Duration = Duration::from_millis(10);
|
||||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
@@ -252,6 +253,13 @@ impl RunnerTransportEndpoint {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn retry_delay(&self, configured: Duration) -> Duration {
|
||||
match self {
|
||||
Self::Dial(_) => configured,
|
||||
Self::Listen { .. } => configured.min(LISTENER_ACCEPT_INTERVAL),
|
||||
}
|
||||
}
|
||||
|
||||
fn open(
|
||||
&self,
|
||||
max_frame_bytes: usize,
|
||||
@@ -2172,6 +2180,27 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn listener_checks_its_backlog_sooner_than_a_dial_reconnects() {
|
||||
let configured = Duration::from_millis(250);
|
||||
let listener = RunnerTransportEndpoint::Listen {
|
||||
listener: TcpListener::bind("0.0.0.0:0").unwrap(),
|
||||
path: "/api/runner/v1/connect/run_1".to_owned(),
|
||||
};
|
||||
assert_eq!(listener.retry_delay(configured), LISTENER_ACCEPT_INTERVAL);
|
||||
assert!(LISTENER_ACCEPT_INTERVAL <= Duration::from_millis(25));
|
||||
assert_eq!(
|
||||
listener.retry_delay(Duration::from_millis(1)),
|
||||
Duration::from_millis(1)
|
||||
);
|
||||
let dial = RunnerTransportEndpoint::new(
|
||||
"ws://127.0.0.1:43127/api/runner/v1/connect/run_1",
|
||||
"run_1",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(dial.retry_delay(configured), configured);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn listener_path_is_exact_and_unambiguous() {
|
||||
validate_listener_path("/api/runner/v1/connect/run_1").unwrap();
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
#![cfg(unix)]
|
||||
|
||||
use std::fs;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
struct Runner {
|
||||
process: Child,
|
||||
directory: PathBuf,
|
||||
}
|
||||
|
||||
impl Drop for Runner {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.process.kill();
|
||||
let _ = self.process.wait();
|
||||
let _ = fs::remove_dir_all(&self.directory);
|
||||
}
|
||||
}
|
||||
|
||||
const RUN_ID: &str = "run_accept";
|
||||
|
||||
fn upgrade(stream: &mut TcpStream) -> Duration {
|
||||
let started = Instant::now();
|
||||
stream
|
||||
.write_all(
|
||||
format!(
|
||||
"GET /api/runner/v1/connect/{RUN_ID} HTTP/1.1\r\nHost: 127.0.0.1\r\n\
|
||||
Upgrade: websocket\r\nConnection: Upgrade\r\n\
|
||||
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n"
|
||||
)
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||
.unwrap();
|
||||
let mut response = Vec::new();
|
||||
let mut byte = [0_u8; 1];
|
||||
while !response.ends_with(b"\r\n\r\n") {
|
||||
assert_eq!(
|
||||
stream.read(&mut byte).unwrap(),
|
||||
1,
|
||||
"Runner closed the upgrade"
|
||||
);
|
||||
response.push(byte[0]);
|
||||
}
|
||||
assert!(
|
||||
response.starts_with(b"HTTP/1.1 101"),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&response)
|
||||
);
|
||||
started.elapsed()
|
||||
}
|
||||
|
||||
fn spawn_listening_runner() -> (Runner, u16, TcpStream) {
|
||||
for _ in 0..10 {
|
||||
let reservation = TcpListener::bind("0.0.0.0:0").unwrap();
|
||||
let port = reservation.local_addr().unwrap().port();
|
||||
drop(reservation);
|
||||
let directory =
|
||||
std::env::temp_dir().join(format!("runner-accept-{}", uuid::Uuid::new_v4()));
|
||||
fs::create_dir(&directory).unwrap();
|
||||
fs::set_permissions(&directory, fs::Permissions::from_mode(0o700)).unwrap();
|
||||
let process = Command::new(env!("CARGO_BIN_EXE_paperclip-runnerd"))
|
||||
.env(
|
||||
"PAPERCLIP_RUNNER_BOOTSTRAP_TICKET",
|
||||
"fixture-bootstrap-ticket",
|
||||
)
|
||||
.args([
|
||||
"--listen-address",
|
||||
"0.0.0.0",
|
||||
"--listen-port",
|
||||
&port.to_string(),
|
||||
"--listen-path",
|
||||
&format!("/api/runner/v1/connect/{RUN_ID}"),
|
||||
"--state-dir",
|
||||
directory.to_str().unwrap(),
|
||||
"--runner-id",
|
||||
"runner",
|
||||
"--environment-lease-id",
|
||||
"lease",
|
||||
"--run-id",
|
||||
RUN_ID,
|
||||
"--session-id",
|
||||
"session",
|
||||
"--turn-id",
|
||||
"turn",
|
||||
"--item-id",
|
||||
"item",
|
||||
"--runner-version",
|
||||
"0.0.0",
|
||||
"--runner-digest",
|
||||
&format!("sha256:{}", "a".repeat(64)),
|
||||
"--max-runtime-ms",
|
||||
"20000",
|
||||
])
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let mut runner = Runner { process, directory };
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
loop {
|
||||
if let Some(status) = runner.process.try_wait().unwrap() {
|
||||
match TcpListener::bind(("0.0.0.0", port)) {
|
||||
Err(error) if error.kind() == std::io::ErrorKind::AddrInUse => break,
|
||||
_ => panic!("Runner exited before listening: {status}"),
|
||||
}
|
||||
}
|
||||
if let Ok(stream) = TcpStream::connect(("127.0.0.1", port)) {
|
||||
return (runner, port, stream);
|
||||
}
|
||||
assert!(Instant::now() < deadline, "Runner did not bind its port");
|
||||
thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
panic!("could not acquire an available Runner port");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn listener_answers_the_next_connection_without_waiting_out_the_reconnect_delay() {
|
||||
let (_runner, port, mut stale) = spawn_listening_runner();
|
||||
upgrade(&mut stale);
|
||||
drop(stale);
|
||||
thread::sleep(Duration::from_millis(30));
|
||||
|
||||
let mut current = TcpStream::connect(("127.0.0.1", port)).unwrap();
|
||||
let waited = upgrade(&mut current);
|
||||
assert!(
|
||||
waited < Duration::from_millis(120),
|
||||
"listener answered after {waited:?}"
|
||||
);
|
||||
}
|
||||
@@ -240,6 +240,21 @@ describeIssueRecovery("issue recovery production producers and deadlines", () =>
|
||||
await state.db.update(agents).set({ status: "idle", updatedAt: new Date(heartbeatAt.getTime() + 1000) }).where(eq(agents.id, f.agentId));
|
||||
expect(await changed()).toEqual(tasks.map(row => row.id).sort());
|
||||
});
|
||||
it("counts an agent's run start as no change to its other tasks, and a pause during the run as a change", async () => {
|
||||
const f = await fixture();
|
||||
const old = new Date(Date.now() - 60 * 60_000);
|
||||
await state.db.update(agents).set({ lastHeartbeatAt: old, updatedAt: old }).where(eq(agents.id, f.agentId));
|
||||
const tasks = await state.db.insert(issues).values(Array.from({ length: 5 }, (_, index) => ({ companyId: f.companyId,
|
||||
title: `Settled ${index}`, status: "in_review", assigneeAgentId: f.agentId, createdAt: old, updatedAt: old }))).returning({ id: issues.id });
|
||||
const since = new Date(Date.now() - 5 * 60_000);
|
||||
const changed = async () => (await state.db.select({ id: issues.id }).from(issues)
|
||||
.where(and(eq(issues.companyId, f.companyId), changedTaskCondition(since)))).map(row => row.id).sort();
|
||||
// The write of execution start in heartbeat.ts: a status flip with no heartbeat time.
|
||||
await state.db.update(agents).set({ status: "running", updatedAt: new Date() }).where(eq(agents.id, f.agentId));
|
||||
expect(await changed()).toEqual([]);
|
||||
await state.db.update(agents).set({ status: "paused", updatedAt: new Date() }).where(eq(agents.id, f.agentId));
|
||||
expect(await changed()).toEqual(tasks.map(row => row.id).sort());
|
||||
});
|
||||
it("resolves a run without a task to no tasks and a missing task to the whole company", async () => {
|
||||
const f = await fixture();
|
||||
const [taskless] = await state.db.insert(heartbeatRuns).values({ companyId: f.companyId, agentId: f.agentId, status: "running",
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
import { setImmediate as immediate } from "node:timers/promises";
|
||||
import { and, eq, sql } from "drizzle-orm";
|
||||
import { expect, it, vi } from "vitest";
|
||||
import { agents, heartbeatRuns, issues } from "@paperclipai/db";
|
||||
import { workSignalDebug } from "@paperclipai/db/work-signal-debug";
|
||||
import { ISSUE_RECOVERY_BACKSTOP_MS, ISSUE_RECOVERY_FULL_BACKSTOP_MS, notifyIssueGraphRecovery }
|
||||
from "../services/issue-recovery-notifications.js";
|
||||
import { describeIssueRecovery, issueRecoveryFixture } from "./issue-recovery.test-fixture.js";
|
||||
import { seedRecoveryPopulation } from "./helpers/recovery-population.js";
|
||||
|
||||
vi.hoisted(() => { process.env.PAPERCLIP_DEBUG_WORK_SIGNALS = "stranded-assignments:1,assigned-work-invariant:1"; });
|
||||
|
||||
const MINUTE = 60_000;
|
||||
const TOPICS = new Set(["stranded-assignments", "assigned-work-invariant"]);
|
||||
type Recorded = { at: number; topic: string; name: string; fields: Record<string, unknown> };
|
||||
|
||||
/**
|
||||
* Staging shape: about 860 settled assigned tasks, most of them held by the
|
||||
* same few agents, and one agent whose native run stays live for the whole
|
||||
* window, publishing a run hint every 10 seconds. Nothing about the settled
|
||||
* tasks changes, so the recovery queues must not re-check them.
|
||||
*/
|
||||
describeIssueRecovery("stranded-task checks beside one long live run (census)", () => {
|
||||
const state = issueRecoveryFixture({ meter: true });
|
||||
|
||||
async function advance(ms: number, onTick?: () => Promise<unknown>) {
|
||||
const until = Date.now() + ms;
|
||||
let nextTick = Date.now();
|
||||
while (Date.now() < until) {
|
||||
if (onTick && Date.now() >= nextTick) { await onTick(); nextTick = Date.now() + 10_000; }
|
||||
await vi.advanceTimersByTimeAsync(Math.min(1000, until - Date.now()));
|
||||
await Promise.allSettled([...state.passes]); await immediate();
|
||||
}
|
||||
}
|
||||
|
||||
function record() {
|
||||
const events: Recorded[] = [];
|
||||
vi.spyOn(workSignalDebug, "event").mockImplementation((topic, name, fields = {}) => {
|
||||
if (TOPICS.has(topic)) events.push({ at: Date.now(), topic, name, fields });
|
||||
});
|
||||
vi.spyOn(workSignalDebug, "signal").mockImplementation(topic => { events.push({ at: Date.now(), topic, name: "signal", fields: {} }); });
|
||||
return events;
|
||||
}
|
||||
|
||||
const checksOf = (events: Recorded[], topic: string) => events.filter(event => event.topic === topic && event.name === "check");
|
||||
function summarize(events: Recorded[]) {
|
||||
const byWake = (topic: string) => {
|
||||
const counts: Record<string, number> = {};
|
||||
for (const event of checksOf(events, topic)) counts[String(event.fields.wake)] = (counts[String(event.fields.wake)] ?? 0) + 1;
|
||||
return counts;
|
||||
};
|
||||
return { stranded: checksOf(events, "stranded-assignments").length, assignedWork: checksOf(events, "assigned-work-invariant").length,
|
||||
strandedByWake: byWake("stranded-assignments"), assignedWorkByWake: byWake("assigned-work-invariant") };
|
||||
}
|
||||
|
||||
async function seed() {
|
||||
const f = await state.fixture();
|
||||
// 12 agents leave 3 workers, so each holds about 266 of the settled reviews.
|
||||
const population = await seedRecoveryPopulation(state.db, f.companyId, { agents: 12, inReview: 800, inProgress: 30, todo: 30 });
|
||||
const liveAgentId = population.agentIds[7]!;
|
||||
const [liveTask] = await state.db.insert(issues).values({ companyId: f.companyId, title: "Live native task", status: "in_progress",
|
||||
assigneeAgentId: liveAgentId }).returning({ id: issues.id });
|
||||
// A paused agent with one waiting task, both settled long before the window.
|
||||
const old = new Date(Date.now() - 2 * 60 * MINUTE);
|
||||
const [held] = await state.db.insert(agents).values({ companyId: f.companyId, name: "Held worker", role: "engineer", status: "paused",
|
||||
lifecycleHolds: ["manual"], adapterType: "process", reportsTo: population.agentIds[1], createdAt: old, updatedAt: old,
|
||||
adapterConfig: { command: process.execPath, args: ["-e", "0"], cwd: process.cwd() },
|
||||
runtimeConfig: { heartbeat: { enabled: false, intervalSec: 0, wakeOnDemand: true } } }).returning({ id: agents.id });
|
||||
const [waiting] = await state.db.insert(issues).values({ companyId: f.companyId, title: "Waits for its agent", status: "todo",
|
||||
assigneeAgentId: held!.id, createdAt: old, updatedAt: old }).returning({ id: issues.id });
|
||||
return { ...f, population, liveAgentId, liveTaskId: liveTask!.id, heldAgentId: held!.id, waitingTaskId: waiting!.id };
|
||||
}
|
||||
|
||||
async function boot() {
|
||||
const f = await seed();
|
||||
const events = record();
|
||||
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] });
|
||||
await state.start({});
|
||||
await advance(10 * MINUTE);
|
||||
events.length = 0;
|
||||
return { ...f, events };
|
||||
}
|
||||
|
||||
/** What execution start writes (heartbeat.ts): the run, then the agent's
|
||||
* status flip to running without a heartbeat time. */
|
||||
async function startLiveRun(f: Awaited<ReturnType<typeof boot>>) {
|
||||
const [run] = await state.db.insert(heartbeatRuns).values({ companyId: f.companyId, agentId: f.liveAgentId, status: "running",
|
||||
runtimeMode: "native", startedAt: new Date(), contextSnapshot: { issueId: f.liveTaskId }, nativeIssueId: f.liveTaskId })
|
||||
.returning({ id: heartbeatRuns.id });
|
||||
await state.db.update(issues).set({ executionRunId: run!.id }).where(eq(issues.id, f.liveTaskId));
|
||||
await state.db.update(agents).set({ status: "running", updatedAt: new Date() }).where(eq(agents.id, f.liveAgentId));
|
||||
const runId = run!.id;
|
||||
return {
|
||||
runId,
|
||||
/** A run event: the run row moves and its writers publish a run hint. */
|
||||
tick: () => state.db.transaction(async tx => {
|
||||
await tx.update(heartbeatRuns).set({ updatedAt: new Date() }).where(eq(heartbeatRuns.id, runId));
|
||||
await notifyIssueGraphRecovery(tx, { companyId: f.companyId, runId });
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
it("does not re-check the live agent's settled tasks while its run stays live", async () => {
|
||||
const f = await boot();
|
||||
const live = await startLiveRun(f);
|
||||
let ticks = 0;
|
||||
const before = state.meter.read();
|
||||
await advance(30 * MINUTE, () => { ticks += 1; return live.tick(); });
|
||||
const after = state.meter.read();
|
||||
// Each run event publishes one signal to each of the 7 recovery queues; anything above that is the sweep's own.
|
||||
const signals = f.events.filter(event => event.name === "signal").length;
|
||||
const result = { ...summarize(f.events), statements: after.statements - before.statements, ticks, signals,
|
||||
signalsBeyondRunEvents: signals - ticks * 7 };
|
||||
console.info(`live-run census (30 min): ${JSON.stringify(result)}`);
|
||||
const settled = new Set([...f.population.inReview, ...f.population.inProgress, ...f.population.todo, f.waitingTaskId]);
|
||||
const settledChecks = f.events.filter(event => event.name === "check" && settled.has(String(event.fields.issueId)));
|
||||
// Before the fix: 534 settled checks and 540 backstop checks. After: 0 and 6 (the live task itself).
|
||||
expect(settledChecks.length).toBe(0);
|
||||
expect(result.strandedByWake.backstop ?? 0).toBeLessThanOrEqual(10);
|
||||
// The live task's own run hints: about one check per run event.
|
||||
expect(result.strandedByWake.hint ?? 0).toBeLessThanOrEqual(ticks + 10);
|
||||
expect(result.assignedWork).toBeLessThanOrEqual(10);
|
||||
expect(result.signalsBeyondRunEvents).toBe(0);
|
||||
}, 1_800_000);
|
||||
|
||||
it("still checks a task of the live agent within the backstop bound when its own rows change without a hint", async () => {
|
||||
const f = await boot();
|
||||
const live = await startLiveRun(f);
|
||||
await advance(6 * MINUTE, live.tick);
|
||||
f.events.length = 0;
|
||||
const target = f.population.inReview.find((_, index) => index % 3 === 0)!;
|
||||
const [current] = await state.db.select({ assignee: issues.assigneeAgentId }).from(issues).where(eq(issues.id, target));
|
||||
expect(current!.assignee).toBe(f.liveAgentId);
|
||||
const changedAt = Date.now();
|
||||
// A direct write that sends no hint (for example an edit by an older server).
|
||||
await state.db.update(issues).set({ status: "todo", updatedAt: new Date() }).where(eq(issues.id, target));
|
||||
await advance(ISSUE_RECOVERY_BACKSTOP_MS + 2 * MINUTE, live.tick);
|
||||
const checked = f.events.find(event => event.topic === "stranded-assignments" && event.name === "check" && event.fields.issueId === target);
|
||||
expect(checked).toBeDefined();
|
||||
expect(checked!.at - changedAt).toBeLessThanOrEqual(ISSUE_RECOVERY_BACKSTOP_MS + 2 * MINUTE);
|
||||
}, 1_800_000);
|
||||
|
||||
it("still checks a resumed agent's task within the backstop bound when the resume sends no hint", async () => {
|
||||
const f = await boot();
|
||||
const live = await startLiveRun(f);
|
||||
await advance(6 * MINUTE, live.tick);
|
||||
f.events.length = 0;
|
||||
const waiting = f.waitingTaskId;
|
||||
const changedAt = Date.now();
|
||||
await state.db.update(agents).set({ status: "idle", lifecycleHolds: [], updatedAt: new Date() })
|
||||
.where(eq(agents.id, f.heldAgentId));
|
||||
await advance(ISSUE_RECOVERY_BACKSTOP_MS + 2 * MINUTE, live.tick);
|
||||
const checked = f.events.find(event => event.topic === "stranded-assignments" && event.name === "check" && event.fields.issueId === waiting);
|
||||
expect(checked).toBeDefined();
|
||||
expect(checked!.at - changedAt).toBeLessThanOrEqual(ISSUE_RECOVERY_BACKSTOP_MS + 2 * MINUTE);
|
||||
}, 1_800_000);
|
||||
|
||||
it("still re-checks every settled task in the hourly full pass", async () => {
|
||||
const f = await boot();
|
||||
const live = await startLiveRun(f);
|
||||
await advance(ISSUE_RECOVERY_FULL_BACKSTOP_MS + 5 * MINUTE, live.tick);
|
||||
const checked = new Set(checksOf(f.events, "stranded-assignments").map(event => String(event.fields.issueId)));
|
||||
const missing = f.population.inReview.filter(id => !checked.has(id));
|
||||
expect(missing).toEqual([]);
|
||||
const runs = await state.db.select({ id: heartbeatRuns.id }).from(heartbeatRuns)
|
||||
.where(and(eq(heartbeatRuns.companyId, f.companyId), sql`${heartbeatRuns.status} = 'running'`));
|
||||
expect(runs.map(run => run.id)).toContain(live.runId);
|
||||
}, 1_800_000);
|
||||
});
|
||||
@@ -1,6 +1,13 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import { issues } from "@paperclipai/db";
|
||||
|
||||
/** Tasks whose own rows changed since `since`, for the backstop between full
|
||||
* passes. An assignee agent's row counts only for a change that can make its
|
||||
* tasks need recovery (a pause, resume, hold or configuration change). Run
|
||||
* lifecycle writes are left out: a run's finish records a heartbeat, and a run's
|
||||
* start flips the agent to running, which makes none of its other tasks
|
||||
* stranded. Every run start would otherwise re-check all of the agent's tasks
|
||||
* for two backstop windows. */
|
||||
export function changedTaskCondition(since: Date) {
|
||||
const at = sql`${since.toISOString()}::timestamptz`;
|
||||
return sql`(${issues.updatedAt} >= ${at}
|
||||
@@ -15,5 +22,6 @@ export function changedTaskCondition(since: Date) {
|
||||
or exists (select 1 from issue_comments c where c.company_id = ${issues.companyId}
|
||||
and c.issue_id = ${issues.id} and c.created_at >= ${at})
|
||||
or exists (select 1 from agents ag where ag.id = ${issues.assigneeAgentId} and ag.updated_at >= ${at}
|
||||
and ag.status <> 'running'
|
||||
and (ag.last_heartbeat_at is null or ag.last_heartbeat_at < ag.updated_at)))`;
|
||||
}
|
||||
Reference in new issue
Block a user