Spaces:
Runtime error
Runtime error
Trim design rationale from score.py; tighten README lineage
Browse filesKeep the scoring code identical (tests unchanged, 16/16) but cut the
verbose "why" comments that doubled as a design doc. Correct the README:
in the product, the cloud model proposes task fields and a deterministic
engine ranks — the small local model here replaces the language work.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
README.md
CHANGED
|
@@ -32,13 +32,14 @@ vague human language into structured fields) and makes the prioritization itself
|
|
| 32 |
that explodes as a deadline nears, a quick-win boost for short ready tasks, and
|
| 33 |
deadlines treated as a hard constraint rather than a number folded into a blob.
|
| 34 |
|
| 35 |
-
The problem
|
| 36 |
-
**[what-first.com](https://what-first.com)**
|
| 37 |
-
in June 2026,
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
## How it works
|
| 44 |
|
|
|
|
| 32 |
that explodes as a deadline nears, a quick-win boost for short ready tasks, and
|
| 33 |
deadlines treated as a hard constraint rather than a number folded into a blob.
|
| 34 |
|
| 35 |
+
The problem — and the prioritization approach — come from
|
| 36 |
+
**[what-first.com](https://what-first.com)**, a full web app the same team built
|
| 37 |
+
in June 2026. There, a frontier cloud model (Claude) does the language work —
|
| 38 |
+
reading your tasks and proposing their impact, readiness, and effort — and a
|
| 39 |
+
deterministic engine ranks them. This entry asks a smaller question: can a **3B
|
| 40 |
+
model running offline on a laptop** do that same language work? The ranking
|
| 41 |
+
engine here is a clean-room Python reimplementation with its own tests, not a
|
| 42 |
+
copy of the original.
|
| 43 |
|
| 44 |
## How it works
|
| 45 |
|
score.py
CHANGED
|
@@ -1,34 +1,14 @@
|
|
| 1 |
"""Priority scoring + deadline ranking — the deterministic core.
|
| 2 |
|
| 3 |
-
|
| 4 |
-
formula's documented behaviour. It depends on nothing but the standard library
|
| 5 |
-
(math, datetime) and imports nothing from the original app. Two parallel scores
|
| 6 |
-
compete; the higher one is what the user sees.
|
| 7 |
|
| 8 |
do_score = (I * U * R_eff * QW) / 10
|
| 9 |
prep_score = (I * U * (10 - R)) / 10 * 0.7 * QW
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
R_eff = R + max(0, R_floor - R) * READY_LIFT urgency lifts an unready task
|
| 16 |
-
defer = U_comp >= 9 and slack < 0.5 and I < 7 flag, not a number
|
| 17 |
-
|
| 18 |
-
READY_LIFT (< 1) keeps the readiness control live: R_eff stays strictly
|
| 19 |
-
increasing in R, so readiness always moves the do-score while urgency can still
|
| 20 |
-
drag an unready-but-urgent task up to where it competes. All inputs are coerced
|
| 21 |
-
to finite numbers and clamped to their domains (I,R in [1,10], E >= 0.05) so the
|
| 22 |
-
displayed score can never be NaN/Infinity.
|
| 23 |
-
|
| 24 |
-
Urgency is unbounded near zero: a rational decay holds for d > 2 days, then an
|
| 25 |
-
inverse-distance term takes over and grows past 10 as the deadline approaches
|
| 26 |
-
(saturating at 30 the instant before due). Crossing the deadline hands off
|
| 27 |
-
continuously: the overdue branch picks up at 30 and ramps to 40 over five days,
|
| 28 |
-
so a task never loses urgency merely by going late.
|
| 29 |
-
|
| 30 |
-
If task["execute_anyway"] is true, R_eff is forced to max(R, 9), prep_score is
|
| 31 |
-
zeroed, and the defer flag is suppressed (the user opted in to risk-on mode).
|
| 32 |
"""
|
| 33 |
|
| 34 |
from __future__ import annotations
|
|
@@ -36,26 +16,13 @@ from __future__ import annotations
|
|
| 36 |
import math
|
| 37 |
from datetime import datetime
|
| 38 |
|
| 39 |
-
# --
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
# urgency-driven target R_floor. Strictly < 1 so a fully unready task is never
|
| 45 |
-
# lifted all the way to "ready".
|
| 46 |
-
READY_LIFT = 0.7
|
| 47 |
-
|
| 48 |
-
# Action bias on the prep branch: < 1 so a tie in raw value tilts to "do".
|
| 49 |
-
PREP_BIAS = 0.7
|
| 50 |
-
|
| 51 |
-
# Upper guard on the two score branches. Sits above the formula's natural
|
| 52 |
-
# maximum on purpose, so it never clips a real task — only a backstop.
|
| 53 |
-
SCORE_CAP = 1000
|
| 54 |
-
|
| 55 |
-
# Closeness band for the value tier, realized as a transitive log-bucket.
|
| 56 |
-
TIE_BAND = 0.10
|
| 57 |
-
|
| 58 |
-
# Quick-win lens thresholds (a short, ready-to-start task for a spare moment).
|
| 59 |
QUICK_WIN_MAX_EFFORT_HOURS = 0.25 # 15 minutes
|
| 60 |
QUICK_WIN_MIN_READINESS = 7
|
| 61 |
|
|
@@ -65,8 +32,7 @@ def clamp(x: float, lo: float, hi: float) -> float:
|
|
| 65 |
|
| 66 |
|
| 67 |
def finite(x, fallback: float) -> float:
|
| 68 |
-
"""Coerce
|
| 69 |
-
enters the formula — one bad field must yield a sane number, never NaN."""
|
| 70 |
try:
|
| 71 |
xf = float(x)
|
| 72 |
except (TypeError, ValueError):
|
|
@@ -75,8 +41,7 @@ def finite(x, fallback: float) -> float:
|
|
| 75 |
|
| 76 |
|
| 77 |
def readiness_of(t: dict) -> float:
|
| 78 |
-
""
|
| 79 |
-
return finite(t.get("readiness"), 8)
|
| 80 |
|
| 81 |
|
| 82 |
# -- Urgency curves -----------------------------------------------------------
|
|
@@ -128,8 +93,6 @@ def score_components(t: dict) -> dict:
|
|
| 128 |
QW = 1 + 0.6 * (I / 10) * math.exp(-E / 1.5)
|
| 129 |
risk_on = bool(t.get("execute_anyway"))
|
| 130 |
R_floor = clamp((U - 6) / 4 * 10, 0, 10)
|
| 131 |
-
# Partial lift toward R_floor (not max(R, R_floor)): urgency pulls an unready
|
| 132 |
-
# task up so it still competes, but readiness keeps moving the score.
|
| 133 |
R_eff = max(R, 9) if risk_on else R + max(0, R_floor - R) * READY_LIFT
|
| 134 |
do_score = clamp((I * U * R_eff * QW) / 10, 0.1, SCORE_CAP)
|
| 135 |
prep_score = 0.0 if risk_on else clamp((I * U * (10 - R)) / 10 * PREP_BIAS * QW, 0, SCORE_CAP)
|
|
@@ -173,8 +136,7 @@ def mine_to_do(task: dict, current_user_id=None) -> bool:
|
|
| 173 |
# -- Dates --------------------------------------------------------------------
|
| 174 |
|
| 175 |
def normalize_iso(due_date):
|
| 176 |
-
"""A bare date ('YYYY-MM-DD')
|
| 177 |
-
date-only deadline isn't read as midnight. A full datetime passes through."""
|
| 178 |
if not isinstance(due_date, str):
|
| 179 |
return due_date
|
| 180 |
return f"{due_date}T17:00:00" if len(due_date) == 10 else due_date
|
|
@@ -198,8 +160,7 @@ def days_to_due_raw(due_date, now: datetime | None = None) -> float:
|
|
| 198 |
|
| 199 |
|
| 200 |
def deadline_status(task: dict, now: datetime | None = None):
|
| 201 |
-
"""
|
| 202 |
-
Returns 'overdue' | 'today' | 'tomorrow' | None."""
|
| 203 |
if not task or task.get("completed") or not task.get("due_date"):
|
| 204 |
return None
|
| 205 |
due = _parse(task["due_date"])
|
|
@@ -220,16 +181,12 @@ def deadline_status(task: dict, now: datetime | None = None):
|
|
| 220 |
# -- Ranking ------------------------------------------------------------------
|
| 221 |
|
| 222 |
def deadline_risk_map(sorted_active: list, now: datetime | None = None, current_user_id=None) -> dict:
|
| 223 |
-
"""
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
continuous wall-clock finish for each task. A task whose projected finish
|
| 227 |
-
lands past its own deadline is at risk even if it would have fit alone — the
|
| 228 |
-
work ahead of it ate the runway. Returns {id: 'at-risk' | 'tight'}.
|
| 229 |
-
"""
|
| 230 |
out = {}
|
| 231 |
now = now or datetime.now()
|
| 232 |
-
acc = 0.0 # cumulative effort-hours
|
| 233 |
for t in sorted_active:
|
| 234 |
if not t or t.get("completed"):
|
| 235 |
continue
|
|
@@ -244,7 +201,7 @@ def deadline_risk_map(sorted_active: list, now: datetime | None = None, current_
|
|
| 244 |
continue
|
| 245 |
hours_to_due = (due - now).total_seconds() / 3600
|
| 246 |
if not (hours_to_due > 0):
|
| 247 |
-
continue # overdue —
|
| 248 |
slack = hours_to_due - acc
|
| 249 |
if slack <= 0:
|
| 250 |
out[t["id"]] = "at-risk"
|
|
@@ -254,10 +211,9 @@ def deadline_risk_map(sorted_active: list, now: datetime | None = None, current_
|
|
| 254 |
|
| 255 |
|
| 256 |
def rank_active(tasks: list, now: datetime | None = None, current_user_id=None) -> list:
|
| 257 |
-
"""Rank
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
near-ties)."""
|
| 261 |
now = now or datetime.now()
|
| 262 |
active = [t for t in tasks if not t.get("completed")]
|
| 263 |
V = {t["id"]: priority(t) for t in active}
|
|
@@ -276,18 +232,16 @@ def rank_active(tasks: list, now: datetime | None = None, current_user_id=None)
|
|
| 276 |
tid = t["id"]
|
| 277 |
tr = tier_of(t)
|
| 278 |
if tr < 2:
|
| 279 |
-
return (tr, D[tid], 0.0) #
|
| 280 |
-
# value pack: higher bucket first, then sooner due breaks near-ties
|
| 281 |
return (tr, -value_bucket(V[tid]), D[tid])
|
| 282 |
|
| 283 |
-
#
|
| 284 |
return sorted(by_value, key=sort_key)
|
| 285 |
|
| 286 |
|
| 287 |
# -- Quick-win lens -----------------------------------------------------------
|
| 288 |
|
| 289 |
def is_quick_win(task: dict) -> bool:
|
| 290 |
-
"""Short enough and ready enough to knock out in a spare moment?"""
|
| 291 |
if not task or task.get("completed"):
|
| 292 |
return False
|
| 293 |
e = task.get("effort_hours")
|
|
@@ -297,8 +251,7 @@ def is_quick_win(task: dict) -> bool:
|
|
| 297 |
|
| 298 |
|
| 299 |
def quick_wins(tasks: list) -> list:
|
| 300 |
-
"""Quick wins
|
| 301 |
-
priority desc."""
|
| 302 |
return sorted(
|
| 303 |
(t for t in tasks if is_quick_win(t)),
|
| 304 |
key=lambda t: (t["effort_hours"], -priority(t)),
|
|
|
|
| 1 |
"""Priority scoring + deadline ranking — the deterministic core.
|
| 2 |
|
| 3 |
+
Two scores compete; the higher one is displayed:
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
do_score = (I * U * R_eff * QW) / 10
|
| 6 |
prep_score = (I * U * (10 - R)) / 10 * 0.7 * QW
|
| 7 |
|
| 8 |
+
with U = max(calendar_urgency, completion_urgency), a quick-win boost QW, and a
|
| 9 |
+
partial readiness lift R_eff. All inputs are coerced to finite numbers and
|
| 10 |
+
clamped to their domains, so the score can never be NaN/Infinity. Standard
|
| 11 |
+
library only.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
|
| 14 |
from __future__ import annotations
|
|
|
|
| 16 |
import math
|
| 17 |
from datetime import datetime
|
| 18 |
|
| 19 |
+
# -- Scoring knobs ------------------------------------------------------------
|
| 20 |
+
READY_LIFT = 0.7 # how far urgency lifts an unready task toward its target (<1)
|
| 21 |
+
PREP_BIAS = 0.7 # action bias on the prep branch (<1)
|
| 22 |
+
SCORE_CAP = 1000 # backstop guard, above the formula's natural max
|
| 23 |
+
TIE_BAND = 0.10 # value-closeness band (log-bucketed for transitivity)
|
| 24 |
|
| 25 |
+
# Quick-win lens: a short, ready-to-start task for a spare moment.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
QUICK_WIN_MAX_EFFORT_HOURS = 0.25 # 15 minutes
|
| 27 |
QUICK_WIN_MIN_READINESS = 7
|
| 28 |
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
def finite(x, fallback: float) -> float:
|
| 35 |
+
"""Coerce non-numeric / non-finite input to a default before the formula."""
|
|
|
|
| 36 |
try:
|
| 37 |
xf = float(x)
|
| 38 |
except (TypeError, ValueError):
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def readiness_of(t: dict) -> float:
|
| 44 |
+
return finite(t.get("readiness"), 8) # default: presumed mostly-ready
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
# -- Urgency curves -----------------------------------------------------------
|
|
|
|
| 93 |
QW = 1 + 0.6 * (I / 10) * math.exp(-E / 1.5)
|
| 94 |
risk_on = bool(t.get("execute_anyway"))
|
| 95 |
R_floor = clamp((U - 6) / 4 * 10, 0, 10)
|
|
|
|
|
|
|
| 96 |
R_eff = max(R, 9) if risk_on else R + max(0, R_floor - R) * READY_LIFT
|
| 97 |
do_score = clamp((I * U * R_eff * QW) / 10, 0.1, SCORE_CAP)
|
| 98 |
prep_score = 0.0 if risk_on else clamp((I * U * (10 - R)) / 10 * PREP_BIAS * QW, 0, SCORE_CAP)
|
|
|
|
| 136 |
# -- Dates --------------------------------------------------------------------
|
| 137 |
|
| 138 |
def normalize_iso(due_date):
|
| 139 |
+
"""A bare date ('YYYY-MM-DD') is treated as 5pm local; a datetime passes through."""
|
|
|
|
| 140 |
if not isinstance(due_date, str):
|
| 141 |
return due_date
|
| 142 |
return f"{due_date}T17:00:00" if len(due_date) == 10 else due_date
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
def deadline_status(task: dict, now: datetime | None = None):
|
| 163 |
+
"""Discrete deadline tier: 'overdue' | 'today' | 'tomorrow' | None."""
|
|
|
|
| 164 |
if not task or task.get("completed") or not task.get("due_date"):
|
| 165 |
return None
|
| 166 |
due = _parse(task["due_date"])
|
|
|
|
| 181 |
# -- Ranking ------------------------------------------------------------------
|
| 182 |
|
| 183 |
def deadline_risk_map(sorted_active: list, now: datetime | None = None, current_user_id=None) -> dict:
|
| 184 |
+
"""Walk the queue in priority order, accumulate effort, and flag any task
|
| 185 |
+
whose projected wall-clock finish lands past its deadline. Returns
|
| 186 |
+
{id: 'at-risk' | 'tight'}."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
out = {}
|
| 188 |
now = now or datetime.now()
|
| 189 |
+
acc = 0.0 # cumulative effort-hours so far, inclusive
|
| 190 |
for t in sorted_active:
|
| 191 |
if not t or t.get("completed"):
|
| 192 |
continue
|
|
|
|
| 201 |
continue
|
| 202 |
hours_to_due = (due - now).total_seconds() / 3600
|
| 203 |
if not (hours_to_due > 0):
|
| 204 |
+
continue # overdue — deadline_status owns that signal
|
| 205 |
slack = hours_to_due - acc
|
| 206 |
if slack <= 0:
|
| 207 |
out[t["id"]] = "at-risk"
|
|
|
|
| 211 |
|
| 212 |
|
| 213 |
def rank_active(tasks: list, now: datetime | None = None, current_user_id=None) -> list:
|
| 214 |
+
"""Rank with deadlines as a constraint, not a blended term. Three tiers:
|
| 215 |
+
0 overdue (EDF), 1 binding/at-risk (EDF, lifted above the value pack),
|
| 216 |
+
2 value pack (value bucket, then sooner due breaks near-ties)."""
|
|
|
|
| 217 |
now = now or datetime.now()
|
| 218 |
active = [t for t in tasks if not t.get("completed")]
|
| 219 |
V = {t["id"]: priority(t) for t in active}
|
|
|
|
| 232 |
tid = t["id"]
|
| 233 |
tr = tier_of(t)
|
| 234 |
if tr < 2:
|
| 235 |
+
return (tr, D[tid], 0.0) # earliest-deadline-first
|
|
|
|
| 236 |
return (tr, -value_bucket(V[tid]), D[tid])
|
| 237 |
|
| 238 |
+
# Stable sort: equal keys keep by_value (value-desc) order.
|
| 239 |
return sorted(by_value, key=sort_key)
|
| 240 |
|
| 241 |
|
| 242 |
# -- Quick-win lens -----------------------------------------------------------
|
| 243 |
|
| 244 |
def is_quick_win(task: dict) -> bool:
|
|
|
|
| 245 |
if not task or task.get("completed"):
|
| 246 |
return False
|
| 247 |
e = task.get("effort_hours")
|
|
|
|
| 251 |
|
| 252 |
|
| 253 |
def quick_wins(tasks: list) -> list:
|
| 254 |
+
"""Quick wins, shortest first, ties by priority desc."""
|
|
|
|
| 255 |
return sorted(
|
| 256 |
(t for t in tasks if is_quick_win(t)),
|
| 257 |
key=lambda t: (t["effort_hours"], -priority(t)),
|