Skip to content

fix(autonudge): keep a stored loop bound's limit, or stop the loop - #19007

Merged
iamwhatever merged 2 commits into
kirodotdev:mainfrom
javenciu:fix/autonudge-malformed-stored-bound
Oct 11, 2026
Merged

iamwhatever merged 2 commits into
kirodotdev:mainfrom
javenciu:fix/autonudge-malformed-stored-bound

Conversation

@javenciu

@javenciu javenciu commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A loop bound stored in a non-int form either keeps the user's limit as that int, or stops the loop; it never crashes every timer tick and never lets the loop run or resume without its limit.

An auto-nudge loop whose stored max_cycles or max_runtime_secs is a string, such as "24", raised TypeError out of every _timer tick. The cap check compares loop.cycle_count >= loop.max_cycles (src/kiro_crew/autonudge_service/firing.py), and runtime_budget_exceeded compares the clock with the raw budget (src/kiro_crew/autonudge_service/model.py). The loop never ran a cycle and stayed persisted active.

The resume readers read the same field differently. cap_reached and budget_elapsed read the bounds through _positive_number, which returns 0 ("no limit") for anything that is not an int or float, the string "24" included. So one stored field gave two answers: a crash in the timer and "no limit" at resume.

AutoNudgeService._load (src/kiro_crew/autonudge.py) repairs every counter the timer compares (cycle_count, created_ts and both streak counters) for this same reason. It left the two bounds as stored on purpose: its comment says a malformed cap or budget repaired to 0 "would quietly remove a cost limit the user typed, and persist that". This change keeps that rule: no bound is ever repaired to "no limit".

Why it matters

The loop reports itself active and never runs, and nothing says why. The trigger is narrow: the gateway writes both fields as int (autonudge_service/mutations.py, max(0, int(...))), so only a hand-edited or migrated store reaches this path. It matters because the store is agent-writable and _load already treats it as an input boundary for every sibling field.

Not a goal

What changed (motivation → approach → change)

Symptom: a string bound crashes every timer tick, while the resume path reads it as no limit (see Tests).

Root cause: _load normalised the counters the timer compares but not the two bounds it compares them with.

Change:

  • autonudge_service/model.py: stored_bound(value) returns the limit a stored bound names, as an int >= 0, or None when it names none. A lossless integer form keeps its limit: a non-negative int, a whole finite float, or a string holding one ("24", " 24 ", "24.0"). Anything else (null, a negative or fractional number, nan, a bool, other text, a container) answers None. unreadable_bound_field(loop) names the first bound with no reading.
  • A second commit reads a string bound exactly, never through a float (_text_bound, autonudge_service/model.py:736). Plain digits are read by int() as before. Any other text is read as a decimal.Decimal and names a limit only when it is finite, non-negative, whole and no larger than the largest float (_MAX_TEXT_BOUND, autonudge_service/model.py:733). A float reading turned "1e-400" into 0, "24.0000000000000001" into 24 and "9007199254740993.0" into 9007199254740992. The first two now answer None, so the loop stops at load like any other unreadable bound, and the third keeps its value. Past the largest float, the float reading gave infinity and answered None; the exact reading keeps that answer, which also stops int() from building an integer of any size from an exponent such as "1e999999999".
  • _load (autonudge.py): a lossless bound is kept as that int, so _timer, cap_reached and budget_elapsed compare the same number. An unreadable bound fails closed, in the shape of the existing dropped-sentinel stop in the same function: an active row is deactivated under structural_terminal, its schedule is cleared (so the torn-write recovery cannot resume it), the value is left as stored for inspection, and a WARNING names the loop, the field and the stored value. A row that was already stopped keeps its own reason.
  • update (autonudge_service/mutations.py): a revival is declined while a bound stays unreadable, next to the finished-loop refusal, so Play, a reconciler and monitor_update cannot run the loop without its limit. A call that brings a fresh bound applies it first, so it revives under that bound.
  • The _load comment states the rule and its reason: a limit the user typed is never repaired to a number nobody chose.
  • docs/system-specs/modules/learn-cron-dashboard.md, "Monitor runtime ceiling": the spec said a persisted budget is left as stored on load. It now says that a budget is still checked against monitoring.limits only when it is written, how _load reads max_cycles and max_runtime_secs, and that every other persisted budget is left as stored.

Why structural_terminal: of the existing stops it is the one that means a deterministic malformed state that re-firing cannot clear, which only a fresh configuration ends. A directive re-arm may displace it, a later pause does not overwrite it, and the dashboard words it "the last cycle could not run". Its comment names a malformed prompt, not a stored bound; #11582's dedicated reason would make that exact.

Rejected alternatives:

  • Reading an unreadable bound as 0: it runs the loop with no limit, which the _load rule exists to prevent.
  • Catching TypeError in _timer: the loop stays active and never fires, only more quietly.
  • Switching _timer to cap_reached and budget_elapsed: a lossless "24" would read as no limit and the user's cap would be lost.
  • A new stop reason: it adds a dashboard state; feat(members): Perpetual mode switch on the crewmate detail page #11582 proposes one.

Backwards compatibility

Compatible: an int bound, the only form the gateway writes, loads unchanged and is not logged; a missing key still takes the dataclass default 0 (test_a_numeric_bound_loads_unchanged).
Compatible: a bound stored in a lossless non-int form keeps its limit, where the timer crashed and the resume readers read no limit before (test_a_bound_stored_in_a_lossless_form_keeps_its_limit).
Compatible: a loop whose stored bound has no lossless integer reading is stopped at load instead of crashing every tick or running without its limit (the fail-closed rule: a limit nobody can read is never treated as no limit); re-arming, or reviving with a fresh bound, runs it again. A stored null is one of those values, and no version has written one.
Reader: src/kiro_crew/autonudge.py:AutoNudgeService._load -- release -- no writer has ever stored null for either bound: both fields are declared int = 0 from the first commit through v0.6.0 and every release branch, add and update store max(0, int(...)) (autonudge_service/mutations.py), None in update means "unchanged" and is never assigned, and the store writes rows only through asdict(loop) (autonudge_service/store.py); an omitted key still takes the default 0, no limit.
Removes nothing: no function, field or stop reason is removed.

Tests

New tests in test/test_autonudge_start_failures.py, beside test_a_malformed_persisted_counter_is_normalised_at_load, with a _reloaded_with_bound helper that arms a loop, rewrites the stored field and reloads:

  • test_a_bound_stored_in_a_lossless_form_keeps_its_limit ("24", " 24 ", 24.0, "3600", "3600.0", "9007199254740993.0", "3.6e3"): loads as the int, stays active, budget_elapsed reads a lossless budget as spent at its end, and _timer runs without raising.
  • test_a_cycle_cap_stored_as_a_string_fires_and_stops_at_its_cap: "2" fires twice, then stops with cycle_cap, and cap_reached agrees.
  • test_an_unreadable_bound_stops_the_loop_at_load_and_keeps_the_value ("abc", None, -5, 2.9, True, [], nan for the budget, and the strings "1e-400", "24.0000000000000001" and "1e400"): inactive under structural_terminal with no schedule, the value kept as stored, a WARNING naming the field and the value.
  • test_resume_does_not_revive_a_loop_with_an_unreadable_bound (both fields): update(active=True), with and without fresh_run, leaves it stopped and fires nothing; update(active=True) with a fresh bound revives it under that bound.
  • test_a_numeric_bound_loads_unchanged (control).

Red, the whole file against the unchanged source on main (7612507), in 3 of 3 runs: every new test case fails except the control, and the 34 existing tests pass:

E           AssertionError: assert ('24' == 24)
E           AssertionError: assert (24.0 == 24 and <class 'float'> is int)
E           AssertionError: assert (True, '', 1791687333.9814744) == (False, 'stru...erminal', 0.0)
22 failed, 35 passed

The second commit's cases against the first commit's source (b91714a, this file copied in), in 3 of 3 runs: 3 failed, 54 passed. "9007199254740993.0" loads as 9007199254740992 (assert (9007199254740992 == 9007199254740993)), and "1e-400" and "24.0000000000000001" load as 0 and 24 and leave the loop active. "3.6e3" and "1e400" pass on both commits.

Green on this branch:

  • pytest -n 0 test/test_autonudge_start_failures.py: 57 passed in 3 of 3 runs; the 27 test/test_autonudge*.py files other than test_autonudge_authz_cov80.py, which builds paths under the home directory, run serially: 1582 passed.
  • mypy (3 changed source files), flake8, isort, black (no changed file is in the black baseline), scripts/check_black_formatting.py, COMMENT_HISTORY_BASE_REF=origin/main scripts/check_comment_history.py and ./scripts/docs-lint.sh: all exit 0.

Manual verification

Ran the new tests against the unchanged source (red, above) and on this branch (green), with pytest -n 0. They write autonudge.json under a temporary directory and drive AutoNudgeService.start, _load, _timer and update in-process with the idle wait collapsed; no gateway, model or network.

Related Issues

Related: #16353 (recorded why _load leaves the bounds as stored), #8980 (the agent-writable store), #11582 (the dedicated "invalid bounds" stop, open), #9052 and #10966 (open, other edits to _load).

Pattern harvest

Rule candidate: review-prompt
Pattern: a load boundary that repairs the counters a comparison reads but not the limit it compares them with; a limit that cannot be read must stop the work, not lift the limit.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

An auto-nudge loop whose stored max_cycles or max_runtime_secs is a
string, such as "24", raised TypeError out of every timer tick: the
cap check compares cycle_count >= max_cycles and
runtime_budget_exceeded compares the clock with the raw budget. The
loop never ran a cycle and stayed persisted active, while the resume
readers cap_reached and budget_elapsed read the same field as no
limit.

_load repairs every counter the timer compares, but left the two
bounds as stored, because repairing one to 0 would quietly remove a
cost limit the user typed.

The model's stored_bound reads a stored bound. A lossless integer form
(an int, a whole float, or a string holding one) is the user's limit,
and _load keeps it as that int. Any other value cannot be read as a
limit, so the loop fails closed: _load deactivates an active row under
structural_terminal, clears its schedule, keeps the value as stored
and logs a WARNING naming the field and the value, and update declines
a revival while a bound stays unreadable, unless the same call
supplies a fresh one. Every version declares both fields int = 0 and
every writer stores max(0, int(...)), never null, so only an edited
or migrated store reaches this path.
@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners October 11, 2026 01:53
@javenciu
javenciu requested a review from bolichen97 October 11, 2026 01:53
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Oct 11, 2026
@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision e3cc4a01f79453be44b3de5c4f77464b429aa36a touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of e3cc4a01f79453be44b3de5c4f77464b429aa36a via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that the open PR #11582 ("invalid bounds" stop reason) does not end up with a second, conflicting fail-closed branch in the same _load block once both PRs land.

[DESIGN-REVIEWED] e3cc4a0

Description read

[DESCRIPTION-READ] dc16354c6c02230859ce376a4ddfa8da6962ebc17be382907cee80144f220315

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🟡 CONCERNS

Premise-level review of e3cc4a01f79453be44b3de5c4f77464b429aa36a via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The exact-Decimal string reader (_text_bound, _MAX_TEXT_BOUND) goes further than the Goal needs. Accepting only plain digits keeps every limit a real writer stores.

Not justified as shipped

  • Item 4 — oversized: The Goal is "keeps the user's limit as that int, or stops the loop." Any string that is not plain digits can safely take the stop path. No writer stores "3.6e3", "9007199254740993.0" or "1e-400"; the description says "the gateway writes both fields as int". Reading them exactly brings in decimal, sys.float_info.max and an overflow clamp, and the only harm it removes is an imagined one.
    Subtraction: shrink _text_bound to text.strip() plus isascii() and isdigit() → int(...), and return None for everything else. Delete _MAX_TEXT_BOUND and the decimal/sys imports.
    Clears when: the Decimal branch is removed, or a stored non-digit string bound is shown to exist in a real store.
  • Item 5 — duplicate of unreadable_bound_field (model.py, added here): _load writes its own for bound_field in LOOP_BOUND_FIELDS: … stored_bound(...) scan, and the helper has 1 consumer (mutations.py). That makes two spellings of the same check in one diff.
    Subtraction: give the one scan to _load's loop, or inline the helper at its single call site in update.
    Clears when: one bound-readability scan remains.

What this change ships

Inventory (5 items) — 3 justified

Intent: stop a hand-edited or migrated loop bound from crashing every timer tick or silently dropping its limit. FIX.

  1. A string or whole-float bound ("24", 24.0) now loads as that int and the loop runs to its cap — justified
  2. An unreadable bound now stops an active loop at load under structural_terminal, clears its schedule and logs a warning — justified
  3. Play, a reconciler or monitor_update can no longer revive the loop until a fresh bound replaces the unreadable one — justified
  4. Exponent, decimal and huge-digit strings are read exactly through Decimal — oversized
  5. New exported helpers stored_bound, unreadable_bound_field, LOOP_BOUND_FIELDS — duplicate of the _load scan

[FIRST-PRINCIPLES-REVIEWED] e3cc4a0

Description read

[DESCRIPTION-READ] 2fdce117dfb374d89196cfe41dd58e53e5176f71ba5e6a13e69b127c3bf943e2

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed e3cc4a01f79453be44b3de5c4f77464b429aa36a via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] e3cc4a0

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed e3cc4a01f79453be44b3de5c4f77464b429aa36a via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] e3cc4a0

A stored loop bound held as a string that is not plain digits was read
with float(), which rounds: "1e-400" read as 0, "24.0000000000000001"
as 24, and "9007199254740993.0" lost its last digit. Such a string is now
read as an exact decimal. It names a limit only when it is finite,
non-negative, whole and no larger than the largest float, the range the
float reading accepted, so "1e-400" and "24.0000000000000001" stop the
loop at load like any other unreadable bound, and "9007199254740993.0"
keeps its value. Plain digits are still read by int().
@javenciu

Copy link
Copy Markdown
Contributor Author

Fixed in a second commit, e3cc4a0. stored_bound now hands a string to _text_bound (autonudge_service/model.py:736), which reads it exactly and never through a float. Plain digits are read by int() as before. Any other text is read as a decimal.Decimal and names a limit only when it is finite, non-negative, whole and no larger than the largest float (_MAX_TEXT_BOUND, autonudge_service/model.py:733).

  • "1e-400" and "24.0000000000000001" now answer None. _load therefore stops an active loop under structural_terminal, as for any other unreadable bound, instead of reading 0 ("no limit") or 24.
  • "9007199254740993.0" loads as 9007199254740993.
  • Past the largest float, the float reading gave infinity and answered None. The exact reading keeps that answer, which also stops int() from building an integer of any size from an exponent such as "1e999999999".

As the fenced note says, the gateway's own writers store ints (max(0, int(...)) at autonudge_service/mutations.py:370, :923 and :925), so only a hand-edited store can hold such a string. The parse is exact now anyway.

Tests: the cases are added to the two existing tests (test/test_autonudge_start_failures.py:466-467 and :521-524). On the first commit, b91714a, the three new failing cases fail in each of 3 runs (3 failed, 54 passed). On this commit the file passes 57 in each of 3 runs, and the 27 test/test_autonudge*.py files pass 1582.

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention labels Oct 11, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today a hand-edited or migrated store can hold a non-int max_cycles or max_runtime_secs. That makes every _timer tick raise TypeError, while the resume readers treat the same value as "no limit". This PR fixes the root cause in _load. A lossless integer form ("24", 24.0, " 24 ") is normalized to that int, so the timer, cap_reached and budget_elapsed all compare the same number. Any other value deactivates an active loop under structural_terminal, clears its schedule and keeps the stored value, and update() refuses to revive the loop until a fresh bound replaces it. Because this change can only stop more loops, I tried to find a path that runs a loop without its limit. Every activation path is covered: _load's torn-deactivation resume needs next_due_ts > 0, which this change zeroes; the update revival is refused through unreadable_bound_field; and a fresh bound passed in the same call is applied before the check, so the loop revives under it. I also tried odd strings against _text_bound. "NaN", "Infinity" and "sNaN" fail the is_finite check before any comparison. "1e999999999" exceeds _MAX_TEXT_BOUND. Digit strings over int()'s limit return None, and fractional or negative text returns None. A valid int bound, the only form any writer produces, loads unchanged. First Principles' CONCERNS calls the Decimal reader oversized, but that is a scope nit, not a defect: the reader is correct, and it fails closed. The tests are red on main, all checks are green, base is main, and there are no change requests. — crew-pr-reviewer (deep re-check)

@iamwhatever
iamwhatever enabled auto-merge (squash) October 11, 2026 06:05
@iamwhatever
iamwhatever merged commit 0851531 into kirodotdev:main Oct 11, 2026
110 of 117 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 11, 2026
@bolichen97 bolichen97 mentioned this pull request Oct 17, 2026
5 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants