User-local lease lifecycle: outage-aware expiry, time limit from the run, idempotent claims, tombstones, failed replies requeue, cool-off per login

The dashboard stamps its liveness every 5 s; job processes expire leases only while
it serves, and for missing heartbeats only after it served the whole idle window.
After a gap every lease's heartbeat, claim and run start move forward by the gap.
A token's drops count at most once in 10 minutes.

The time limit counts from the first running heartbeat; preparing may last 15 min.
A claim repeated with its request id within 5 minutes gets the same reply (kept in
the dashboard's memory; after a restart the orphaned lease goes back unused).
Ended and requeued leases keep their secret hash, so every lease route answers 410.
ok:false requeues with the next attempt and skips that login; a repeated accepted
submit is accepted again. Cool-off is per login, counts drops and 5 failures in a
row within 30 min, and is cleared by login.
This commit is contained in:
aiongg committed 2026-11-19 23:16:13 +00:00
1 parent aaa9fae512
commit 325d7b6529
12 files changed
+382 -142

No files matched your search

+28 -4
View File
@@ -240,14 +240,36 @@ CREATE TABLE IF NOT EXISTS mcp_tokens (
created_at TEXT NOT NULL, last_used_at TEXT, expires_at TEXT NOT NULL,
can_run INTEGER NOT NULL DEFAULT 0, -- the token's helper may claim model calls in the user-local inference mode
idle_claim_at INTEGER, -- ms since epoch: the token's last claim that found nothing to take (src/remote/room.ts)
idle_tiers TEXT, idle_repos TEXT, -- JSON: the tiers and repos (null: any) that empty claim asked for
cooloff_until INTEGER -- ms since epoch: the token gets no calls until then, after repeated lost leases (src/remote/cooloff.ts)
idle_tiers TEXT, idle_repos TEXT -- JSON: the tiers and repos (null: any) that empty claim asked for
);
-- Each lease of a token that ended without a release or a reply (lost heartbeat, past its time limit), newest 2 hours kept.
-- Leases that ended without a release or a reply (lost heartbeat, past a time limit), at most one per token in 10 minutes,
-- newest 2 hours kept; counted per login toward its cool-off (src/remote/cooloff.ts).
CREATE TABLE IF NOT EXISTS remote_lease_drops (
token_id INTEGER NOT NULL, at INTEGER NOT NULL
token_id INTEGER NOT NULL, at INTEGER NOT NULL,
login TEXT COLLATE NOCASE
);
CREATE INDEX IF NOT EXISTS remote_lease_drops_token ON remote_lease_drops(token_id, at);
-- The cool-off of a login's helpers in the user-local inference mode (src/remote/cooloff.ts): no calls until `until`.
CREATE TABLE IF NOT EXISTS remote_cooloffs (
login TEXT PRIMARY KEY COLLATE NOCASE,
until INTEGER, -- ms since epoch; null or past when not cooling off
reason TEXT, -- why, as the claim reply and the Connect page show it
failures INTEGER NOT NULL DEFAULT 0, -- failed replies (ok: false) in a row
failures_since INTEGER -- the first of them
);
-- The secret hashes of leases that ended or went back to the queue, so their routes answer 410 rather than 401 (kept 24 hours).
CREATE TABLE IF NOT EXISTS remote_lease_tombstones (
secret_hash TEXT PRIMARY KEY,
call_id INTEGER NOT NULL,
at INTEGER NOT NULL -- ms since epoch
);
-- The dashboard's liveness stamp (src/remote/liveness.ts), one row: written every 5 seconds; `since` is when the current
-- unbroken run of stamps began. Leases expire only while the dashboard was alive for the whole idle window.
CREATE TABLE IF NOT EXISTS remote_liveness (
id INTEGER PRIMARY KEY CHECK (id = 1),
alive_at INTEGER NOT NULL,
since INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS mcp_tokens_login ON mcp_tokens(login);
-- The last status of each helper (an agent token that may run tasks) in the user-local inference mode (src/remote/helpers.ts):
-- readiness (src/remote/protocol.ts Readiness), until and reason as the helper sent them, JSON of its models, reserve and usage.
@@ -760,6 +782,8 @@ CREATE TABLE IF NOT EXISTS remote_calls (
lease_secret_hash TEXT, -- sha256 of the lease secret
token_id INTEGER, -- the agent token that claimed the lease; the lease ends when it may no longer run tasks
leased_at INTEGER, heartbeat_at INTEGER,
started_at INTEGER, -- the lease's first `running` heartbeat: the call's time limit counts from here
claim_id TEXT, -- the claim's request id (protocol Claim.requestId) that leased it
attempt INTEGER NOT NULL DEFAULT 1,
max_attempts INTEGER NOT NULL, -- the prompt's attempts limit
timeout_ms INTEGER NOT NULL, -- the bundle's wall-clock limit