Skip to content

fix(execution): a terminal completed_at is floored at the row's own started_at (trinity-enterprise#882) - #3649

Merged
pavshulin merged 1 commit into
devfrom
feature/882-completed-at-floor
Oct 13, 2026
Merged

pavshulin merged 1 commit into
devfrom
feature/882-completed-at-floor

Conversation

@pavshulin

@pavshulin pavshulin commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Changes

  • src/backend/utils/helpers.py — the floor rule in terminal_completed_at; required keyword-only so a future third writer must decide, never silently skip; naive floor normalized to UTC like given.
  • src/backend/db/schedules/executions.py, src/backend/db/activities.py — both writers pass floor=started_at.
  • tests/unit/test_882_completed_at_floor.py — 15 tests: the backdated envelope lands in the completed_at COLUMN as started_at (column-level assertion) with duration_ms == 0 and the status still terminal, on both tables, real DB via db_harness (PG leg under TEST_POSTGRES_URL); in-window verbatim; future-clamp regression pins; floor-vs-clamp precedence on skew; the required-parameter pin.
  • tests/unit/test_3589_claim_completed_at.py — the four direct helper calls updated to the new signature (floor=None); all 37 behaviour assertions unchanged and green.
  • docs/memory/architecture/execution.md, docs/memory/feature-flows/cleanup-service.md, docs/memory/learnings/2026-10-13-agent-timestamps-bounded-both-ends.md, docs/security-reports/cso-diff-2026-10-13-ent882-completed-at-floor.md — the helper's rule list, the claim flow clause, the learnings fragment, the diff audit.

Test Plan

  • TDD red-first: before the fix, 10 of the new tests failed on behaviour — the column assertion failed with the backdated value written verbatim (test_a_backdated_envelope_lands_as_started_at_in_the_column, test_the_failed_arm_floors_too, test_a_backdated_close_lands_as_started_at_in_the_column, and the 7 helper-rule tests).
  • cd tests && pytest unit/test_882_completed_at_floor.py unit/test_3589_claim_completed_at.py -v — 52 passed (15 new + 37 existing).
  • Full unit island run.
  • Independent validation per the validator handoff (hostile-envelope trace to the column; hand mutations incl. floor-vs-overwrite).

Fixes abilityai/trinity-enterprise#882 (private tracker — cross-repo keyword does not auto-close; closed manually at release).

Rides rc3 together with t#3588.

🤖 Generated with Claude Code

…tarted_at (trinity-enterprise#882)

#3629 made the producer's terminal instant authoritative; terminal_completed_at
clamped only the future, so a past value reached the completed_at column
verbatim. The helper gains a REQUIRED keyword-only floor: parameter — the
fourth rule in the one place both writers resolve through — and
update_execution_status / complete_activity pass the row's own server-written
started_at. A floored write logs a warning and remains a successful claim; an
in-window instant (the #3589 behaviour) is recorded verbatim; the future-clamp
is unchanged; a naive floor is normalized to UTC like the given instant.

TDD: 10 of the 15 new tests in tests/unit/test_882_completed_at_floor.py were
red before the fix — the column assertion failed with the backdated value
written verbatim. test_3589_claim_completed_at.py stays green (37/37; four
direct helper calls updated to the new signature, assertions unchanged).
test_1482_write_params.py's persistence fixture moves to an in-window instant.

Fixes abilityai/trinity-enterprise#882

Co-Authored-By: Claude Fable 5 <[email protected]>
@pavshulin

Copy link
Copy Markdown
Contributor Author

Independent validation — APPROVE ✅

Validated per the ent#882 validator handoff, in an isolated worktree at head 35a76c6, by a session independent of the author session.

The hole is closed (the deciding check). Hostile-envelope trace: a backdated completed_at (2026-01-01) resolves through terminal_completed_at(…, floor=started_at) and lands in the completed_at column as the row's own started_at (db/schedules/executions.py:429-443, utils/helpers.py:225-234). started_at is server-written at every admission/restamp site (executions.py:168/:235/:337, all utc_now_iso(); the standalone scheduler stamps its own clock) — the floor cannot be attacker-chosen. The retention predicates (retention.py:37-39, 59-61) read the column, and a sweep of every other completed_at writer (queue.py, cleanup.py, loops.py, scheduler) found only server-now stamps: no remaining path lets agent data move a row past the cutoff.

Both writers, both floors — and the activity close floors at the activity row's own started_at (activities.py:259-271), not the execution's.

#3589 preserved, and provably distinct from an overwrite. In-window values land verbatim, pinned column-level in both writer suites.

Caller sweep: exactly two production call sites, both floored; no caller in the scheduler or agent-server trees; the required keyword-only floor: breaks no unconverted caller because none exists.

Tests + mutations (fresh py3.13 venv; SQLite — no PG env on this machine):

Floor fires loudly: the warning names the delta in seconds — the audit signal is there.

Scope + hygiene: exactly the brief (2 writers + helper + tests + the tiered docs); no disclosure concerns — class described, no recipe.

Notes for the merger: Tier-1 pytest (head, seed 12345) was still in flight at validation time — green before merge as usual. Cross-tracker Fixes needs the manual close + status-in-dev bump on ent#882.

Full report: .local/testing/validate-pr-floor-report.md (validator machine).

🤖 Generated with Claude Code

@dolho dolho 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.

/review on 35a76c684: approve. This is the independent validation the body asks for.

Scope: clean. One rule added in the one helper both terminal writers resolve through (terminal_completed_at), and both writers pass the row's own started_at. A grep finds no other caller of the helper under src/ or docker/, so the required keyword-only floor can't be skipped by a third writer today, and won't be tomorrow (see M5).

Logic:

  • Order is right. The future-clamp applies first, then the floor. On skew (started_at ahead of our clock) the floor wins, which is the invariant retention reads ("cannot end before it starts"). Duration is 0 either way.
  • A naive floor is treated as UTC, the same as given, so the terminal path never raises on mixed naive and aware values.
  • A floored write is still a claim: the status stays terminal and only the instant moves. In-window [started_at, now] values are recorded verbatim, so #3589's behaviour is unchanged.
  • Both floors are server-written: schedule_executions.started_at / agent_activities.started_at, read back from the row inside the writer (parse_iso_timestamp(row["started_at"])). Never the envelope.

Validation: hostile envelope traced to the column, and hand mutations

The 52-test battery is green locally, 71 including test_1482_write_params. test_882 asserts on the completed_at column through the real DB (db_harness), not on the helper's return value, which is the trace the handoff asked for.

Mutation Red
M1: floor rule disabled in the helper 6
M2: update_execution_status passes floor=None (floor-vs-overwrite, executions) 2
M3: complete_activity passes floor=None (activities) 1
M4: clamp re-applied after the floor (clamp wins on skew) 1
M5: floor made optional (= None default), so a third writer could skip it silently 1

Every arm has a test that fails without it.

Informational (non-blocking)

  1. A floored skewed row can carry a future completed_at (confidence 5/10, low): when started_at is ahead of our clock, the floor deliberately wins over the clamp, so completed_at = that future started_at. That delays pruning; it never hastens it, which is the safe direction for this issue. Readers that compare completed_at against "now" (a "finished N ago" label) could briefly read slightly odd. Stated in the docstring, so this is just for the record.
  2. Log wording: both warnings carry the [#3589] tag. The floor's line would be easier to find under [ent#882]. Cosmetic.

CI: 18 passed, 2 pending at review time. It's still a draft with "DO NOT MERGE — independent validation pending" in the body: from my side validation is done, so it's yours to mark ready and drop the banner once CI lands.

🤖 Generated with Claude Code

@pavshulin
pavshulin merged commit fa60374 into dev Oct 13, 2026
26 checks passed
obasilakis added a commit that referenced this pull request Oct 13, 2026
tests/registry.json: keep both appended entries (trinity-enterprise#882 and #3588).

tests/unit/test_3588_close_cancelled_claimed_activities.py: seed execution
and activity start times before the fixed instant _T. A terminal
completed_at is floored at the row's own started_at (trinity-enterprise#882,
PR #3649), so rows seeded at "now" had the past instant raised to their
start and three timestamp-agreement assertions failed on the merged tree.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants