diff --git a/bun.lock b/bun.lock index 6c17645..da398b6 100644 --- a/bun.lock +++ b/bun.lock @@ -36,7 +36,7 @@ }, "packages/daemon": { "name": "ework-daemon", - "version": "0.4.211", + "version": "0.4.217", "bin": { "ework-daemon": "./src/cli.ts", "ework-daemon-server": "./bin/ework-daemon-server.js", diff --git a/packages/daemon/src/db.ts b/packages/daemon/src/db.ts index fba4065..2233edd 100644 --- a/packages/daemon/src/db.ts +++ b/packages/daemon/src/db.ts @@ -551,6 +551,18 @@ async function runMigrations(db: AsyncDatabase): Promise { sqlite ? "reset_at INTEGER NOT NULL DEFAULT 0" : "reset_at BIGINT NOT NULL DEFAULT 0" ); + // issues.dispatch_priority — boost lane (owner 2026-11-12, e-work#63). 0 = + // normal FIFO; >0 = boosted (root-cause aggregations). The global drain + // orders by dispatch_priority DESC before created_at, so a boosted issue + // jumps ahead of both older normal messages and higher project-priority + // messages; within the same dispatch_priority bucket FIFO is preserved + // (owner 2026-10-30 anti-starvation policy unchanged). + await ensureColumn( + tIssues, + "dispatch_priority", + sqlite ? "dispatch_priority INTEGER NOT NULL DEFAULT 0" : "dispatch_priority INT NOT NULL DEFAULT 0" + ); + // op_sessions runtime-state columns (previously in-memory Maps; now persisted // so a restarted daemon can recover the nudge/generation state). await ensureColumn(tSessions, "last_output_at", "last_output_at VARCHAR(40)"); diff --git a/packages/daemon/src/op.ts b/packages/daemon/src/op.ts index a41d42c..3b56c71 100644 --- a/packages/daemon/src/op.ts +++ b/packages/daemon/src/op.ts @@ -12,6 +12,7 @@ interface IssueRow { tracker_issue_id: string; state: string; title: string; + dispatch_priority: number; created_at: string; updated_at: string; owner_daemon_id: number | null; @@ -67,6 +68,7 @@ function rowToIssue(row: IssueRow): Issue { trackerIssueId: row.tracker_issue_id, state: row.state as IssueState, title: row.title, + dispatchPriority: row.dispatch_priority ?? 0, createdAt: new Date(row.created_at), updatedAt: new Date(row.updated_at), ownerDaemonId: row.owner_daemon_id ?? null, @@ -204,6 +206,18 @@ export class Store { ]); } + /** Boost lane (e-work#63): set an issue's dispatch_priority. 0 = normal FIFO, + * >0 = boosted (root-cause aggregations). Idempotent — the classifier board + * re-stamps the current RC aggregation set every round. Does not touch + * updated_at: this is a dispatch concern, not a content change, so re-stamps + * must not churn the web-side sync clock. */ + async setIssueDispatchPriority(id: string, priority: number): Promise { + await getDB().run("UPDATE {{issues}} SET dispatch_priority = ? WHERE uid = ?", [ + priority, + id, + ]); + } + async listActiveIssues(): Promise { const rows = await getDB().all("SELECT * FROM {{issues}} WHERE state != 'closed'"); return rows.map(rowToIssue); @@ -399,7 +413,12 @@ export class Store { const rows = this.projectPriorities.length > 0 ? await this.priorityOrderedPending(now, limit) : await getDB().all( - `SELECT * FROM {{messages}} WHERE status = 'pending' AND ${Store.RETRY_HOLD_SQL} ORDER BY created_at ASC LIMIT ?`, + `SELECT m.* FROM {{messages}} m + JOIN {{op_sessions}} s ON s.uid = m.session_id + JOIN {{issues}} i ON i.uid = s.issue_id + WHERE m.status = 'pending' AND (m.retry_after IS NULL OR m.retry_after <= ?) + ORDER BY i.dispatch_priority DESC, m.created_at ASC + LIMIT ?`, [now, limit] ); return rows.map(rowToMessage); @@ -423,12 +442,12 @@ export class Store { } return getDB().all( `SELECT m.* FROM {{messages}} m - JOIN {{op_sessions}} s ON s.uid = m.session_id - JOIN {{issues}} i ON i.uid = s.issue_id - WHERE m.status = 'pending' AND (m.retry_after IS NULL OR m.retry_after <= ?) - ORDER BY m.created_at ASC, - CASE ${whens.join(" ")} ELSE 0 END DESC - LIMIT ?`, + JOIN {{op_sessions}} s ON s.uid = m.session_id + JOIN {{issues}} i ON i.uid = s.issue_id + WHERE m.status = 'pending' AND (m.retry_after IS NULL OR m.retry_after <= ?) + ORDER BY i.dispatch_priority DESC, m.created_at ASC, + CASE ${whens.join(" ")} ELSE 0 END DESC + LIMIT ?`, [nowIso, ...caseParams, limit] ); } diff --git a/packages/daemon/src/opencode.ts b/packages/daemon/src/opencode.ts index 763f06d..38f009c 100644 --- a/packages/daemon/src/opencode.ts +++ b/packages/daemon/src/opencode.ts @@ -4343,6 +4343,30 @@ Check states: read the mirror cache (see AGENTS.md), do not poll gh for check st return { issues, killed, superseded }; } + /** Boost lane (e-work#63): set dispatch_priority on the given mirror issues so + * their pending messages jump ahead of older normal work in the global drain + * (dispatch_priority DESC, then created_at ASC). The classifier board re-stamps + * the current RC aggregation set every round; the write is skipped when the + * value is already correct, so a re-stamp of an unchanged set is a no-op + * (idempotent). Silent on purpose — no thread comments. */ + async boostIssuePriority(mirrorIssueIds: string[], priority = 1): Promise<{ issues: number; boosted: number }> { + let issues = 0; + let boosted = 0; + for (const id of [...new Set(mirrorIssueIds.map((s) => String(s)))]) { + for (const issue of await this.store.findIssuesByTrackerIssueId(id)) { + issues++; + if ((issue.dispatchPriority ?? 0) !== priority) { + await this.store.setIssueDispatchPriority(issue.id, priority); + boosted++; + } + } + } + if (issues > 0) { + log.info(`engine: boost — ${issues} issue(s), ${boosted} dispatch_priority re-stamp(s) to ${priority}`); + } + return { issues, boosted }; + } + private buildForwardPrompt( opName: string, commentBody: string, diff --git a/packages/daemon/src/schema-mysql.sql b/packages/daemon/src/schema-mysql.sql index 7350166..e32eb31 100644 --- a/packages/daemon/src/schema-mysql.sql +++ b/packages/daemon/src/schema-mysql.sql @@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS {{issues}} ( tracker_issue_id VARCHAR(64) NOT NULL, state VARCHAR(16) NOT NULL DEFAULT 'created', title VARCHAR(512) NOT NULL DEFAULT '', + dispatch_priority INT NOT NULL DEFAULT 0, created_at VARCHAR(40) NOT NULL, updated_at VARCHAR(40) NOT NULL, UNIQUE (tracker_type, tracker_scope_key, tracker_issue_id) diff --git a/packages/daemon/src/schema-sqlite.sql b/packages/daemon/src/schema-sqlite.sql index 5527ef9..fb0983e 100644 --- a/packages/daemon/src/schema-sqlite.sql +++ b/packages/daemon/src/schema-sqlite.sql @@ -12,6 +12,7 @@ CREATE TABLE IF NOT EXISTS {{issues}} ( tracker_issue_id TEXT NOT NULL, state TEXT NOT NULL DEFAULT 'created', title TEXT NOT NULL DEFAULT '', + dispatch_priority INTEGER NOT NULL DEFAULT 0, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, UNIQUE(tracker_type, tracker_scope_key, tracker_issue_id) diff --git a/packages/daemon/src/server.ts b/packages/daemon/src/server.ts index 140fe33..763852f 100644 --- a/packages/daemon/src/server.ts +++ b/packages/daemon/src/server.ts @@ -245,6 +245,15 @@ export function createServer( const report = await engine.stopIssueWork(ids.map(String)); return json({ ok: true, ...report }); } + if (pathname === "/api/admin/boost" && req.method === "POST") { + const body = await req.json().catch(() => ({} as unknown)); + const ids = (body as { issue_ids?: unknown })?.issue_ids; + if (!Array.isArray(ids) || ids.some((x) => typeof x !== "string" && typeof x !== "number")) { + return json({ error: "issue_ids must be an array of mirror issue ids" }, 400); + } + const report = await engine.boostIssuePriority(ids.map(String)); + return json({ ok: true, ...report }); + } return json({ error: "not found" }, 404); } diff --git a/packages/daemon/src/trackers/types.ts b/packages/daemon/src/trackers/types.ts index a5f95da..04a89e7 100644 --- a/packages/daemon/src/trackers/types.ts +++ b/packages/daemon/src/trackers/types.ts @@ -99,6 +99,10 @@ export interface Issue { trackerIssueId: string; state: IssueState; title: string; + /** Boost lane (e-work#63): 0 = normal FIFO, >0 = boosted (root-cause + * aggregations). The global drain orders by dispatch_priority DESC before + * created_at, so a boosted issue jumps ahead of older normal work. */ + dispatchPriority?: number; createdAt: Date; updatedAt: Date; /** Daemon id currently leasing this issue (null/undefined = unclaimed). */ diff --git a/packages/daemon/tests/queue-priority.test.ts b/packages/daemon/tests/queue-priority.test.ts index b3e490e..5b6506e 100644 --- a/packages/daemon/tests/queue-priority.test.ts +++ b/packages/daemon/tests/queue-priority.test.ts @@ -120,6 +120,97 @@ describe("getGlobalPendingMessages ordering", () => { }); }); +describe("dispatch_priority boost lane (e-work#63)", () => { + it("boosted issue beats older normal work (branch 1: no project priorities)", async () => { + const older = await seedPending("ranxianglei/other", "1", "2026-09-29T10:00:00.000Z"); + const newer = await seedPending("ranxianglei/billion-context", "2", "2026-09-29T11:00:00.000Z"); + const store = new Store(); + const issues2 = await store.findIssuesByTrackerIssueId("2"); + expect(issues2).toHaveLength(1); + await store.setIssueDispatchPriority(issues2[0]!.id, 1); + const picked = await store.getGlobalPendingMessages(2); + expect(picked.map((m) => m.id)).toEqual([newer, older]); + await store.close(); + }); + + it("boosted issue beats older normal AND higher project-priority work (branch 2)", async () => { + const olderNormal = await seedPending("ranxianglei/other", "1", "2026-09-29T10:00:00.000Z"); + const highPriority = await seedPending("ranxianglei/billion-context", "2", "2026-09-29T10:30:00.000Z"); + const boosted = await seedPending("ranxianglei/opencode-acp", "3", "2026-09-29T11:00:00.000Z"); + const store = new Store([{ scope: "ranxianglei/billion-context", priority: 100 }]); + const issues3 = await store.findIssuesByTrackerIssueId("3"); + await store.setIssueDispatchPriority(issues3[0]!.id, 1); + const picked = await store.getGlobalPendingMessages(3); + expect(picked.map((m) => m.id)).toEqual([boosted, olderNormal, highPriority]); + await store.close(); + }); + + it("same dispatch_priority bucket keeps strict FIFO (owner 2026-10-30 anti-starvation)", async () => { + const a = await seedPending("ranxianglei/other", "1", "2026-09-29T10:00:00.000Z"); + const b = await seedPending("ranxianglei/billion-context", "2", "2026-09-29T11:00:00.000Z"); + const c = await seedPending("ranxianglei/opencode-acp", "3", "2026-09-29T12:00:00.000Z"); + const store = new Store(); + for (const num of ["1", "2", "3"]) { + const issues = await store.findIssuesByTrackerIssueId(num); + await store.setIssueDispatchPriority(issues[0]!.id, 5); + } + const picked = await store.getGlobalPendingMessages(3); + expect(picked.map((m) => m.id)).toEqual([a, b, c]); + await store.close(); + }); + + it("boosting '645' does not misfire on '6453' (exact match, not prefix)", async () => { + await seedPending("ranxianglei/other", "645", "2026-09-29T10:00:00.000Z"); + await seedPending("ranxianglei/other", "6453", "2026-09-29T11:00:00.000Z"); + const store = new Store(); + const found645 = await store.findIssuesByTrackerIssueId("645"); + expect(found645).toHaveLength(1); + await store.setIssueDispatchPriority(found645[0]!.id, 1); + // Re-fetch: the in-memory object is stale after the UPDATE. + const after645 = await store.findIssuesByTrackerIssueId("645"); + const found6453 = await store.findIssuesByTrackerIssueId("6453"); + expect(found6453).toHaveLength(1); + expect(found6453[0]!.dispatchPriority ?? 0).toBe(0); + expect(after645[0]!.dispatchPriority ?? 0).toBe(1); + await store.close(); + }); + + it("boost is idempotent (re-stamping to the same value is a no-op)", async () => { + await seedPending("ranxianglei/other", "1", "2026-09-29T10:00:00.000Z"); + const store = new Store(); + const issues = await store.findIssuesByTrackerIssueId("1"); + const id = issues[0]!.id; + await store.setIssueDispatchPriority(id, 1); + await store.setIssueDispatchPriority(id, 1); + const after = await store.findIssuesByTrackerIssueId("1"); + expect(after[0]!.dispatchPriority).toBe(1); + await store.close(); + }); + + it("boosting an empty list is a no-op (no issues touched)", async () => { + await seedPending("ranxianglei/other", "1", "2026-09-29T10:00:00.000Z"); + const store = new Store(); + // Mirror of Engine.boostIssuePriority([]): dedupe → empty → loop body never runs. + const ids: string[] = []; + let issues = 0, boosted = 0; + for (const id of [...new Set(ids)]) { + const found = await store.findIssuesByTrackerIssueId(id); + for (const issue of found) { + issues++; + if ((issue.dispatchPriority ?? 0) !== 1) { + await store.setIssueDispatchPriority(issue.id, 1); + boosted++; + } + } + } + expect(issues).toBe(0); + expect(boosted).toBe(0); + const after = await store.findIssuesByTrackerIssueId("1"); + expect(after[0]!.dispatchPriority ?? 0).toBe(0); + await store.close(); + }); +}); + it("age order spans lanes: infra sessions do not leapfrog older ordinary work (owner 2026-10-30)", async () => { const store = new Store([{ scope: "acme/widgets", priority: 100 }]); const issueA = await store.findOrCreateIssue(ref("1", "acme/widgets"), "acme/widgets", "normal"); diff --git a/scripts/classifier-board.py b/scripts/classifier-board.py index d9441de..154551a 100644 --- a/scripts/classifier-board.py +++ b/scripts/classifier-board.py @@ -285,6 +285,7 @@ def open_numbers(): # and stop-work stops any live/queued work on each render (idempotent, silent). SUPPRESSION_PATH = "/var/lib/ework-daemon/share/classifier/suppressed.json" DAEMON_STOP_URL = "http://127.0.0.1:3101/api/admin/stop-work" +DAEMON_BOOST_URL = "http://127.0.0.1:3101/api/admin/boost" def incorporated_numbers(merged, open_set): """{mirror number: reason} for every open issue absorbed into a root-cause @@ -330,6 +331,61 @@ def write_suppression(merged, open_set): time.sleep(5 * (attempt + 1)) print(f"classifier-board: suppression set = {len(nums)} issue(s) -> {SUPPRESSION_PATH}") +def gh_to_mirror(gh_num): + """GitHub issue number -> e-work mirror number (None if not synced yet). + rc-agg.db stores the GitHub number (issues are created/verified via the + GitHub API), but the daemon's boost endpoint is told mirror numbers (the + gitea tracker_issue_id) — same convention as stop-work.""" + if not gh_num: + return None + c = sqlite3.connect(f"file:{DB}?mode=ro", uri=True) + row = c.execute("""SELECT number FROM issues + WHERE project_id=(SELECT id FROM projects WHERE name='billion-context') + AND upstream_issue_number=?""", (gh_num,)).fetchone() + c.close() + return row[0] if row else None + +def boost_rc_aggs(): + """Boost lane (e-work#63): every round, read the live RC aggregation numbers + from rc-agg.db, convert to mirror numbers, and POST them to the daemon's + boost endpoint so their pending messages jump ahead of older normal work in + the global drain (dispatch_priority DESC, then FIFO). Idempotent re-stamp — + the daemon skips the write when the value is already correct. A not-yet- + synced (newly created this round) aggregation is skipped here and picked up + next round once it lands in the mirror; a leftover mark on a closed issue + is harmless (a closed issue has no pending messages).""" + try: + c = rcdb() + gh_nums = [r[0] for r in c.execute("SELECT issue_number FROM rc_agg").fetchall()] + c.close() + except Exception as e: + print(f"classifier-board: boost read failed: {e}") + return + nums = [] + for gh_num in gh_nums: + m = gh_to_mirror(gh_num) + if m is not None: + nums.append(str(m)) + if not nums: + print("classifier-board: boost set empty (no synced RC aggregations) — skipping") + return + for attempt in range(3): + try: + req = urllib.request.Request( + DAEMON_BOOST_URL, + data=json.dumps({"issue_ids": nums}).encode(), + headers={"Content-Type": "application/json"}, + method="POST", + ) + with urllib.request.urlopen(req, timeout=60) as r: + print(f"classifier-board: boost -> {r.read().decode()[:200]}") + break + except Exception as e: + print(f"classifier-board: boost POST failed (attempt {attempt+1}/3): {e}") + if attempt < 2: + time.sleep(5 * (attempt + 1)) + print(f"classifier-board: boost set = {len(nums)} issue(s)") + def mirror_row(n): c = sqlite3.connect(f"file:{DB}?mode=ro", uri=True) row = c.execute("""SELECT title, state, upstream_issue_number, upstream_ci_state, @@ -673,6 +729,10 @@ def main(): # issues and post member marks — so no member comment can wake a worker. write_suppression(merged, open_set) agg_by_cat = update_agg(merged, open_set) + # Boost the RC aggregation issues so their pending work jumps the FIFO tail + # (e-work#63). Runs after update_agg so same-round new aggregations are in + # rc-agg.db; idempotent re-stamp, silent on the daemon side. + boost_rc_aggs() ensure_label(ABSORBED_LABEL, "d9d9d9") mark_members(agg_by_cat, merged, open_set) ensure_label(AGG_LABEL)