replace urgency priority queue with strict FIFO

Drop the urgency score and interrupt/preempt decisions: intake is now
idle -> dispatch, busy -> append to a bounded FIFO queue (RequestQueue).
The running process always finishes before the next queued request.

- queue.py: UrgencyQueue -> RequestQueue (deque, no weight/age/recency)
- scheduler.py: remove _priority_score, _interrupt_choice, urgency
  constants, and the weight threaded through dispatch/authoring/requeue
- dashboard/static/gateway: drop weight + preempted handling
- tests + docs updated; flow-main.svg regenerated
This commit is contained in:
gabby committed 2026-10-06 20:38:49 +00:00
1 parent a354958599
commit 1dc0550659
18 files changed
+173 -341

No files matched your search

+2 -2
View File
@@ -111,8 +111,8 @@ def build_status(scheduler: Scheduler) -> dict:
else None
)
queue = [
{"id": request.id, "weight": weight, "text": request.text[:80]}
for weight, request in scheduler.queue.items()
{"id": request.id, "text": request.text[:80]}
for request in scheduler.queue.items()
]
tau = scheduler.tau
queue_max = scheduler.queue.max_size
+1 -1
View File
@@ -83,7 +83,7 @@ def _result_detail(text: str) -> str:
def _reply_kind(status: str) -> str:
"""Classify a scheduler outcome for a result-only front end."""
if status in ("running", "preempted", "ran"):
if status in ("running", "ran"):
return "result"
if status == "needs_input":
return "question"
+16 -55
View File
@@ -1,83 +1,44 @@
"""Urgency priority queue.
"""FIFO request queue with a maximum depth.
Items are sorted by descending urgency weight, then FIFO arrival, then most
recent first. Queued items age upward so they cannot starve, and the queue has
a maximum depth.
Requests are dispatched strictly in arrival order: there is no urgency scoring
and no preemption, so the running process always finishes before the next queued
request is picked up.
"""
from __future__ import annotations
import heapq
import itertools
import time
from dataclasses import dataclass
from collections import deque
from typing import Iterator
from .decisions import Request
MIN_RECENCY = 0.0
class RequestQueue:
"""A bounded first-in-first-out queue of pending requests."""
@dataclass
class QueueItem:
weight: float
seq: int
recency: float
request: Request
def key(self) -> tuple:
return (-self.weight, self.seq)
class UrgencyQueue:
"""A max-by-urgency priority queue with FIFO/recency tie-breaking."""
def __init__(self, max_size: int = 100, age_rate: float = 0.0):
def __init__(self, max_size: int = 100):
self.max_size = max_size
self.age_rate = age_rate
self._entries: list[tuple] = []
self._counter = itertools.count()
self._entries: deque[Request] = deque()
def push(self, request: Request, weight: float, recency: float | None = None) -> bool:
"""Insert an item. Returns False if the queue is full."""
def push(self, request: Request) -> bool:
"""Append an item. Returns False if the queue is full."""
if len(self._entries) >= self.max_size:
return False
item = QueueItem(
weight=weight,
seq=next(self._counter),
recency=recency if recency is not None else time.time(),
request=request,
)
entry = (*item.key(), next(self._counter), item)
heapq.heappush(self._entries, entry)
self._entries.append(request)
return True
def pop(self) -> Request | None:
if not self._entries:
return None
return heapq.heappop(self._entries)[-1].request
return self._entries.popleft()
def peek(self) -> Request | None:
if not self._entries:
return None
return self._entries[0][-1].request
def age(self, dt: float = 1.0) -> None:
"""Pull queued priorities toward critical so old items catch up."""
if self.age_rate <= 0:
return
rebuilt = []
for entry in self._entries:
item = entry[-1]
item.weight += self.age_rate * dt * (1.0 - item.weight)
rebuilt.append((*item.key(), next(self._counter), item))
self._entries = rebuilt
heapq.heapify(self._entries)
return self._entries[0]
def __len__(self) -> int:
return len(self._entries)
def items(self) -> Iterator[tuple[float, Request]]:
for entry in sorted(self._entries, key=lambda e: e[:-1]):
item = entry[-1]
yield item.weight, item.request
def items(self) -> Iterator[Request]:
yield from self._entries
+37 -108
View File
@@ -1,10 +1,11 @@
"""The scheduler: choice, score, queue, dispatch.
"""The scheduler: queue, dispatch.
Every SemIf decision (choice, score, navigation, sub-decision) is logged.
A high-priority input can preempt the current process, which requeues with its
state preserved; a deferred input is scored and queued by urgency. There is no
up-front handle/ignore gate: every input is dispatched, and inputs that are not
tasks fall through navigation into the closed `response` tree of canned replies.
Every SemIf decision (navigation, sub-decision) is logged. Input that arrives
while the single execution slot is busy waits in a strict FIFO queue and is
dispatched in arrival order; there is no urgency scoring and no preemption.
There is no up-front handle/ignore gate: every input is dispatched, and inputs
that are not tasks fall through navigation into the closed `response` tree of
canned replies.
"""
from __future__ import annotations
@@ -36,7 +37,7 @@ from .engine import EngineUnavailable, SemIfEngine
from .llm import LLMClient
from .log import DecisionLog
from .provider import ProviderError
from .queue import UrgencyQueue
from .queue import RequestQueue
from .skill import SkillRunner
from .timers import TimerService
from .skills import (
@@ -68,21 +69,11 @@ from .skills import (
)
from .trace import TraceLog
CHOICE_INTERRUPT = "interrupt"
URGENCY_OPTIONS = [
("critical", "Immediate danger or critical failure."),
("high", "Important but not dangerous."),
("medium", "Should be handled reasonably soon."),
("low", "Can wait."),
]
URGENCY_WEIGHTS = {"critical": 1.0, "high": 0.75, "medium": 0.5, "low": 0.25}
@dataclass
class Process:
request: Request
skill: str
weight: float
@dataclass
@@ -107,7 +98,6 @@ class SkillWrite:
request: Request
category: str
draft: SkillDraft
weight: float
repair_evidence: dict | None = None
# Label for a corrective rewrite: "run_failure" (repair loop), "fidelity",
# "test", or "manual" (a user-requested regen).
@@ -127,7 +117,6 @@ class DraftAuthor:
request: Request
category: str | None
weight: float
kind: str
approved: bool = False
@@ -262,9 +251,8 @@ class Scheduler:
self.contract_search = bool(
(codegen_cfg.get("contract_search", {}) or {}).get("enabled", True)
)
self.queue = UrgencyQueue(
self.queue = RequestQueue(
max_size=int(config.get("queue", {}).get("max_size", 100)),
age_rate=float(config.get("queue", {}).get("age_rate", 0.0)),
)
self.skills = build_skills(config) + build_housekeeping_skills()
self.tree = build_tree(self.skills)
@@ -326,38 +314,6 @@ class Scheduler:
self._fatal = f"decision engine not available: {exc}"
return self._fatal
# ---- decision templates (all real SemIf, all logged) ----
def _interrupt_choice(self, request: Request, current: Process) -> bool:
decision = DecisionRequest(
state=f"{request.text} [current process: {current.skill}]",
question="Should this be allowed to interrupt the current process?",
options=[
Option(CHOICE_INTERRUPT, "Yes, interrupt the current process."),
Option("defer", "No, wait until the current process finishes."),
],
)
result = self.engine.call(decision)
self.log.append(
decision,
result,
extra={"phase": "choice", "current": current.skill, "run_id": request.id},
)
return result.prob(CHOICE_INTERRUPT) >= self.tau
def _priority_score(self, request: Request, current: str | None = None) -> tuple[float, str]:
decision = DecisionRequest(
state=f"{request.text} [current process: {current}]",
question="How urgent is this request?",
options=[Option(option_id, description) for option_id, description in URGENCY_OPTIONS],
)
result = self.engine.call(decision)
self.log.append(
decision, result, extra={"phase": "score", "run_id": request.id}
)
label = result.selected
return URGENCY_WEIGHTS[label], label
# ---- intake ----
def submit(self, text: str, source: str = "typed") -> tuple[str, str]:
@@ -388,50 +344,31 @@ class Scheduler:
self.trace.append("submit", request.id, text=text, source=source)
if self.current is None:
weight, label = self._priority_score(request)
self.current = Process(request=request, skill="(scheduling)", weight=weight)
self.current = Process(request=request, skill="(scheduling)")
try:
outcome = self._dispatch(request, weight=weight)
outcome = self._dispatch(request)
finally:
if self.pending is None:
self.current = None
if outcome.kind == "needs_input":
return "needs_input", outcome.summary, request.id
self.trace.append("ran", request.id, skill=outcome.skill, summary=outcome.summary)
return "running", f"[{label}] {outcome.summary}", request.id
return "running", outcome.summary, request.id
interrupt = self._interrupt_choice(request, self.current)
if interrupt:
if self.pending is not None:
self.trace.append("pending_abandoned", self.pending.request.id)
self.pending = None
previous = self.current
previous.request.resume["from_skill"] = previous.skill
self.queue.push(previous.request, previous.weight)
self.current = Process(request=request, skill="(scheduling)", weight=1.0)
self.trace.append("preempted", request.id, preempted=previous.skill)
outcome = self._dispatch(request, weight=1.0)
if outcome.kind == "needs_input":
return "preempted", f"interrupted {previous.skill}; {outcome.summary}", request.id
self.current = None
self.trace.append("ran", request.id, skill=outcome.skill, summary=outcome.summary)
return "preempted", f"interrupted {previous.skill}; {outcome.summary}", request.id
weight, label = self._priority_score(request, current=self.current.skill)
ok = self.queue.push(request, weight)
ok = self.queue.push(request)
if not ok:
self.trace.append("rejected", request.id, reason="queue is full")
return "rejected", "queue is full", request.id
self.trace.append("queued", request.id, weight=weight, label=label)
return "queued", f"urgency {label} (weight {weight:.2f})", request.id
self.trace.append("queued", request.id)
return "queued", "queued", request.id
def busy(self, text: str, skill: str = "(driving)") -> None:
"""Set a fake in-progress process so the choice/score path is exercised."""
"""Set a fake in-progress process so the queue path is exercised."""
with self._lock:
if self.pending is not None:
self.trace.append("pending_abandoned", self.pending.request.id)
self.pending = None
self.current = Process(request=Request(text, source="busy"), skill=skill, weight=1.0)
self.current = Process(request=Request(text, source="busy"), skill=skill)
def idle(self) -> None:
with self._lock:
@@ -456,9 +393,9 @@ class Scheduler:
break
request = self.queue.pop()
self.trace.append("dequeued", request.id)
self.current = Process(request=request, skill="(scheduling)", weight=0.0)
self.current = Process(request=request, skill="(scheduling)")
try:
outcome = self._dispatch(request, weight=0.0)
outcome = self._dispatch(request)
except EngineUnavailable as exc:
self._mark_fatal(exc)
results.append(("fatal", self._fatal))
@@ -478,7 +415,7 @@ class Scheduler:
# ---- dispatch ----
def _dispatch(self, request: Request, weight: float = 0.0) -> DispatchResult:
def _dispatch(self, request: Request) -> DispatchResult:
# Explicit housekeeping commands are deterministic: they route straight
# to the meta skill, bypassing the generic guards (which misread a
# task-like skill name as the task). Natural-language phrasings still go
@@ -508,9 +445,9 @@ class Scheduler:
action_tau=self.action_tau,
)
if isinstance(navigation, CreateCategory):
return self._dispatch_create_category(request, weight)
return self._dispatch_create_category(request)
if isinstance(navigation, CreateSkill):
return self._dispatch_skill(request, navigation.category, weight)
return self._dispatch_skill(request, navigation.category)
if navigation.category in CANNED_CATEGORIES:
return self._run_skill(navigation, request)
if navigation.category in HARD_LOCKED_CATEGORIES:
@@ -530,12 +467,10 @@ class Scheduler:
)
if self._category_locked(navigation.category):
return self._blocked_create(request, navigation.category)
return self._dispatch_skill(request, navigation.category, weight)
return self._dispatch_skill(request, navigation.category)
return self._run_skill(navigation, request)
def _dispatch_create_category(
self, request: Request, weight: float = 0.0
) -> DispatchResult:
def _dispatch_create_category(self, request: Request) -> DispatchResult:
"""Queue a new top-level category for the `llm` draft worker.
The gate stays free: the small model authors the category in the
@@ -543,7 +478,7 @@ class Scheduler:
inside it and hands off to the codegen body worker. On failure the
request is not re-dispatched and the user is told.
"""
self._queue_draft(request, None, "category", weight)
self._queue_draft(request, None, "category")
return DispatchResult(
kind="create_category",
summary=(
@@ -553,7 +488,7 @@ class Scheduler:
)
def _dispatch_skill(
self, request: Request, category: str, weight: float = 0.0
self, request: Request, category: str
) -> DispatchResult:
"""Queue a new skill leaf for the `llm` draft worker.
@@ -576,7 +511,7 @@ class Scheduler:
),
skill=name,
)
self._queue_draft(request, category, "skill", weight)
self._queue_draft(request, category, "skill")
return DispatchResult(
kind="create_skill",
summary=(
@@ -685,7 +620,7 @@ class Scheduler:
question=outcome.needs_input,
pre_act=outcome.pre_act,
)
self.current = Process(request=request, skill=skill.name, weight=0.5)
self.current = Process(request=request, skill=skill.name)
self.trace.append(
"needs_input", request.id, skill=skill.name, question=outcome.needs_input
)
@@ -708,7 +643,7 @@ class Scheduler:
requeued = False
if outcome.updated_request and request.reentries < self.max_reentries:
child = _requeue(request, outcome.updated_request)
self.queue.push(child, 0.5)
self.queue.push(child)
self.trace.append("requeued", request.id, text=outcome.updated_request)
if self.on_request_requeued is not None:
self.on_request_requeued(request.id, child.id)
@@ -950,7 +885,7 @@ class Scheduler:
offer.status = "awaiting_user"
if chosen == "retry":
retry = Request(offer.request_text, source="repair_retry")
self.queue.push(retry, 0.5)
self.queue.push(retry)
with self._lock:
offer.status = "done"
self.trace.append("repair_retry", offer.run_id, skill=offer.skill)
@@ -1017,7 +952,7 @@ class Scheduler:
offer.status = "done"
request = Request(offer.request_text, source="repair")
self._start_skill_write(
request, offer.category, draft, 0.5, repair_evidence=offer.evidence
request, offer.category, draft, repair_evidence=offer.evidence
)
self.trace.append(
"repair_executed",
@@ -1046,7 +981,6 @@ class Scheduler:
request: Request,
category: str | None,
kind: str,
weight: float,
approved: bool = False,
) -> None:
"""Queue a category or skill draft for the `llm` worker.
@@ -1061,7 +995,6 @@ class Scheduler:
DraftAuthor(
request=request,
category=category,
weight=weight,
kind=kind,
approved=approved,
)
@@ -1135,7 +1068,7 @@ class Scheduler:
category=draft.name,
description=draft.description,
)
self._queue_draft(job.request, draft.name, "skill", job.weight, approved=True)
self._queue_draft(job.request, draft.name, "skill", approved=True)
def _author_skill(self, job: DraftAuthor) -> None:
"""Author a skill leaf stub, then launch the codegen body write.
@@ -1192,7 +1125,7 @@ class Scheduler:
)
if self.codegen is None:
return
self._start_skill_write(job.request, job.category, draft, job.weight)
self._start_skill_write(job.request, job.category, draft)
def _fail_draft(self, job: DraftAuthor, exc: Exception) -> None:
"""Surface a failed draft authoring; the request is not re-dispatched."""
@@ -1314,7 +1247,6 @@ class Scheduler:
request: Request,
category: str,
draft: SkillDraft,
weight: float,
repair_evidence: dict | None = None,
reason_kind: str = "run_failure",
) -> None:
@@ -1355,7 +1287,6 @@ class Scheduler:
request=request,
category=category,
draft=draft,
weight=weight,
repair_evidence=repair_evidence,
reason_kind=reason_kind,
)
@@ -1860,12 +1791,11 @@ class Scheduler:
requeued = job.request.copy_for_requeue()
requeued.meta["awaiting_skill_body"] = [job.category, job.draft.name]
requeued.meta["parent_run"] = job.request.id
self.queue.push(requeued, job.weight)
self.queue.push(requeued)
self.trace.append(
"skill_requeued",
job.request.id,
text=requeued.text,
weight=job.weight,
)
print(
f"[codegen] body ready for {job.category}.{job.draft.name}; "
@@ -1953,7 +1883,7 @@ class Scheduler:
if request_text
else Request(f"write the body for {category}.{name}: {draft.description}", source="restart")
)
self._start_skill_write(origin, category, draft, 0.5)
self._start_skill_write(origin, category, draft)
self.trace.append("skill_restarted", origin.id, category=category, skill=name)
return "running", f"restarting body write for {category}.{name}"
@@ -2092,7 +2022,6 @@ class Scheduler:
origin,
category,
draft,
0.5,
repair_evidence=evidence,
reason_kind="manual",
)
@@ -2187,8 +2116,8 @@ class Scheduler:
if offered:
lines.append(f"repair offers: {len(offered)} (use `repairs`)")
lines.append(f"queue: {len(self.queue)} pending")
for weight, request in self.queue.items():
lines.append(f" {request.id} w={weight:.2f} {request.text[:60]}")
for request in self.queue.items():
lines.append(f" {request.id} {request.text[:60]}")
pending_timers = self.timers.pending()
if pending_timers:
lines.append(f"timers: {len(pending_timers)} scheduled")
+4 -8
View File
@@ -65,7 +65,7 @@ function shortPhase(phase) {
return phase.replace("navigate:", "nav:");
}
const ALL_PHASES = ["gate", "choice", "score", "navigate:category", "navigate:leaf", "act"];
const ALL_PHASES = ["navigate:category", "navigate:leaf", "act"];
function optionProbs(row) {
return (row.options || []).map((o) => ({
@@ -130,9 +130,7 @@ function eventRow(evt) {
div.textContent = `assessed → ${evt.success ? "ok" : "fail"}`;
div.title = evt.summary || "";
} else if (evt.kind === "queued") {
div.textContent = `queued (${evt.label})`;
} else if (evt.kind === "preempted") {
div.textContent = `preempted ${evt.preempted}`;
div.textContent = "queued";
} else if (evt.kind === "skill_writing") {
div.textContent = `writing skill ${evt.skill} — ${evt.description} (${evt.model || "codegen"})`;
if (evt.contract_ref) {
@@ -420,9 +418,7 @@ function eventNode(evt) {
node.appendChild(req);
}
} else if (evt.kind === "queued") {
body.textContent = `urgency ${evt.label} (weight ${Number(evt.weight || 0).toFixed(2)})`;
} else if (evt.kind === "preempted") {
body.textContent = `interrupted ${evt.preempted}, requeued with state`;
body.textContent = "queued";
} else if (evt.kind === "dropped") {
body.textContent = evt.reason || "";
} else if (evt.kind === "skill_writing") {
@@ -825,7 +821,7 @@ function renderStatus() {
q.className = "muted";
q.style.marginTop = "8px";
q.textContent = state.status.queue
.map((item) => `${item.id} w=${item.weight.toFixed(2)} ${item.text}`)
.map((item) => `${item.id} ${item.text}`)
.join("\n") || "queue empty";
el.appendChild(q);
-2
View File
@@ -183,8 +183,6 @@ select {
}
.pill.gate { background: #2b3a52; color: #9ec1ff; }
.pill.choice { background: #4a3a2b; color: #ffd59e; }
.pill.score { background: #3a2b4a; color: #d59eff; }
.pill.navigate { background: #2b4a3a; color: #9effd5; }
.pill.act { background: #2b464a; color: #9eeaff; }
.pill.assess { background: #4a2b2b; color: #ff9e9e; }
+1 -1
View File
@@ -2,7 +2,7 @@
scheduler.
Decision rows live in `decisions.jsonl` (SemIf-compatible). Lifecycle events
(submit, dropped, queued, preempted, assessed, requeued, ...) live here in
(submit, dropped, queued, dequeued, assessed, requeued, ...) live here in
`runs.jsonl`, keyed by `run_id` (the request id) so the dashboard can rebuild
each run's flow without touching the decision-log contract. Both files are
append-only; replays read them in order.