fix(insight): fresh cadence publications and legacy actor invalidation #442
<h3>PR Summary by Qodo</h3>
Publish fresh insight builds and invalidate on legacy actor statements
<code>🐞 Bug fix</code> <code>🧪 Tests</code> <code>🕐 20-40 Minutes</code>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<details>
<summary>AI Description</summary>
<dl>
<dd>
<br/>
><pre>
>• Stamp each cadence observation so unchanged and returning census views publish as new builds.
>• Include legacy non-machine actor statements in freshness checks without granting them human
authority.
>• Pin the updated contracts and document the additive index requirement and freshness limitation.
></pre>
</dd>
</dl>
</details>
<details>
<summary>Diagram</summary>
<dl>
<dd>
<br/>
graph TD
D["Decision rows"] --> Q["Nonmachine watermark"] --> F["Cadence fold"] --> T["Stamp built_at"] --> A["Artifact publication"] --> C{"Fresh build?"} -->|yes| S["Stored view"]
C -->|no| L["Live fold"]
</dd>
</dl>
</details>
<details>
<summary>High-Level Assessment</summary>
<dl>
<dd>
<br/>
>The following are alternative approaches to this PR:
<details>
<summary><b>1. Force a new artifact row for every cadence run</b></summary>
<dl>
<dd>
</dd>
</dl>
</details>
>Recommendation: Stamping built_at in the contract-required document is the narrower fix: it distinguishes cadence observations while preserving existing artifact publication behavior. The non-machine index predicate also avoids rewriting historical actor data. This does not resolve the separately identified commit-order freshness limitation.
</dd>
</dl>
</details>
<details>
<summary> Files changed (8) <code> +108 / -44 </code> </summary>
<dl>
<dd>
<br/>
<details>
<summary>Bug fix (2) <code> +11 / -22 </code></summary>
<dl>
<dd>
<details>
<summary>decision.py<code>Include legacy non-machine actors in the freshness lookup</code> <code>+6/-5</code></summary>
<br/>
>Include legacy non-machine actors in the freshness lookup
><pre>
>• Changes the latest-recording-time query from a human-prefix filter to non-null actors excluding canonical machine: actors. Keeps the partial-index predicate literal for prepared-query index use.
></pre>
><a href='https://github.com/traust-security/traust-core/pull/442/files#diff-d05c15c6b2e504891e2e2a3d2ba97106bd583e309ac8ea7dfdf0133311a31462'>src/traust_core/v1/repositories/traust/findings/decision.py</a>
</details>
<details>
<summary>insight.py<code>Stamp every completed cadence observation</code> <code>+5/-17</code></summary>
<br/>
>Stamp every completed cadence observation
><pre>
>• Adds built_at to each published insight-build document so unchanged and returning views do not reuse an earlier digest. Keeps the pre-fold freshness watermark separate from the artifact row timestamp used for serving age.
></pre>
><a href='https://github.com/traust-security/traust-core/pull/442/files#diff-33baccc4b7229ac6a008e1deb9111adb23d99dc4414fb04187a3db0db1ce0f73'>src/traust_core/v1/services/insight.py</a>
</details>
</dd>
</dl>
</details>
<details>
<summary>Tests (3) <code> +79 / -6 </code></summary>
<dl>
<dd>
<details>
<summary>test_findings.py<code>Check the non-machine partial index in the query profile</code> <code>+2/-5</code></summary>
<br/>
>Check the non-machine partial index in the query profile
><pre>
>• Changes the freshness-query performance assertion to expect idx_decision_nonmachine_recorded instead of the human-only index.
></pre>
><a href='https://github.com/traust-security/traust-core/pull/442/files#diff-121d7b3785ca93ee7fbe2551f593cf3654225a6dd53d9987e9d16c33d7258c16'>perf/queries/test_findings.py</a>
</details>
<details>
<summary>test_findings.py<code>Cover legacy actors in the recording-time query</code> <code>+11/-1</code></summary>
<br/>
>Cover legacy actors in the recording-time query
><pre>
>• Adds cases for legacy and unknown actor spellings, including backdated claimed times, while retaining canonical machine exclusion.
></pre>
><a href='https://github.com/traust-security/traust-core/pull/442/files#diff-e9f7daad1c4fd0a956529c39c435578adb57382cb254f1ea68acb9fe3de915be'>tests/v1/repositories/tables/test_findings.py</a>
</details>
<details>
<summary>test_insight_builds.py<code>Regress publication identity and legacy invalidation</code> <code>+66/-0</code></summary>
<br/>
>Regress publication identity and legacy invalidation
><pre>
>• Tests that unchanged and A-B-A census observations create distinct publications and that legacy actor statements invalidate stored builds without gaining human authority.
></pre>
><a href='https://github.com/traust-security/traust-core/pull/442/files#diff-cc1d9b91929eae414e367f1374eb3c22cd9eda54bce6785bb7b3a6636606b6e6'>tests/v1/services/test_insight_builds.py</a>
</details>
</dd>
</dl>
</details>
<details>
<summary>Documentation (1) <code> +15 / -13 </code></summary>
<dl>
<dd>
<details>
<summary>README.md<code>Clarify insight publication and freshness guarantees</code> <code>+15/-13</code></summary>
<br/>
>Clarify insight publication and freshness guarantees
><pre>
>• Documents built_at as the observation time, distinct from database publication time and the pre-fold watermark. Explains legacy actor inclusion, additive index deployment, and the lack of a commit-ordered snapshot guarantee.
></pre>
><a href='https://github.com/traust-security/traust-core/pull/442/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5'>README.md</a>
</details>
</dd>
</dl>
</details>
<details>
<summary>Other (2) <code> +3 / -3 </code></summary>
<dl>
<dd>
<details>
<summary>pyproject.toml<code>Pin the updated insight-build contracts</code> <code>+1/-1</code></summary>
<br/>
>Pin the updated insight-build contracts
><pre>
>• Advances traust-contracts to commit 4c8f205, which supplies the required build timestamp and additive index contract.
></pre>
><a href='https://github.com/traust-security/traust-core/pull/442/files#diff-50c86b7ed8ac2cf95bd48334961bf0530cdc77b5a56f852c5c61b89d735fd711'>pyproject.toml</a>
</details>
<details>
<summary>uv.lock<code>Lock the updated traust-contracts revision</code> <code>+2/-2</code></summary>
<br/>
>Lock the updated traust-contracts revision
><pre>
>• Updates the resolved source and dependency reference to the same traust-contracts commit pinned in pyproject.toml.
></pre>
><a href='https://github.com/traust-security/traust-core/pull/442/files#diff-84321598744d84dbee2318e634c74c9aae39a1c253f1c4bd17ebf9ef2f807b11'>uv.lock</a>
</details>
</dd>
</dl>
</details>
</dd>
</dl>
</details>
Pre-push verification completed: full unit suite 13,808 passed/one skipped; targeted SQLite/PostgreSQL and 22 REST checks passed. The sparse legacy-actor query used the new partial index. The separately reproduced delayed-commit watermark issue remains documented as a pre-existing limitation, not a solved part of this patch.
<h3>Code Review by Qodo</h3>
<br/>
<details><summary><ins><strong>View medium (1)</strong></ins></summary><br/>
<img src="https://img.shields.io/badge/Medium-634FD1?style=flat-square" height="20px" alt="Remediation recommended">
<details>
<summary> 1. Repeated builds reuse old publications <code>🔗 Cross-repo conflict</code></summary>
<br/>
<details>
><summary>Description</summary>
><br/>
><pre>
><b><i>record_builds()</i></b> uses <b><i>Clock.now()</i></b> as <b><i>built_at</i></b> without ensuring a fresh value for each
>observation, contrary to the pinned traust-contracts artifact contract. If two unchanged builds
>receive the same timestamp, or an A-B-A sequence returns to both the original content and timestamp,
>artifact deduplication reuses the earlier row and the latest build can remain B.
></pre>
></details>
<details>
><summary>Agent prompt</summary>
><br/>
>>The issue below was found during a code review. Follow the provided context and guidance below and implement a solution > >## Issue description >The pinned artifact contract requires a fresh `built_at` for every cadence observation, but equal clock readings can produce identical documents and reuse an older publication. >## Fix Focus Areas >- src/traust_core/v1/services/insight.py[394-405] >- tests/v1/services/test_insight_builds.py[287-330] >## Recommended Fix >Ensure each completed observation has a distinct `built_at` even when clock readings repeat, while keeping the artifact row's `created_at` as publication time. Add a regression with unchanged and A-B-A builds under a repeated clock value that verifies distinct artifact IDs and selection of the final build. >
<code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
></details>
</details>
</details>
<br/>
<!-- qodo-self-review-slot -->
<!-- https://github.com/traust-security/traust-core/commit/4fd2391039cc384721c2d281c4d27fe0bb407e46 -->
<h3>Code Review by Qodo</h3>
<code>🐞 Bugs (2)</code> <code>📘 Rule violations (0)</code> <code>📜 Skill insights (0)</code>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<br/>
<img src="https://img.shields.io/badge/Medium-634FD1?style=flat-square" height="20px" alt="Remediation recommended">
<details>
<summary> 1. Existing databases can scan every decision <code>🐞 Bug</code> <code>➹ Performance</code></summary>
<br/>
<details open>
><summary>Description</summary>
><br/>
><pre>
><b><i>newest_person_statement()</i></b> now uses a predicate that cannot use the former human-only partial
>index, but the existing-database initialization path does not install the new non-machine index. On
>an existing store without the separately deployed index, each eligible unscoped insight read runs
>this freshness query without its intended index; the revised performance case exercises only a
>database initialized with the new schema.
></pre>
></details>
<details>
><summary>Code</summary>
><br/>
><code>[src/traust_core/v1/repositories/traust/findings/decision.py[R181-183]](https://github.com/traust-security/traust-core/pull/442/files#diff-d05c15c6b2e504891e2e2a3d2ba97106bd583e309ac8ea7dfdf0133311a31462R181-R183)</code>
>>+ .where( >+ c.actor.is_not(None), >+ sa.text("substr(actor, 1, 8) <> 'machine:'"), >
></details>
<details>
><summary>Evidence</summary>
><br/>
><pre>
>Schema creation from contracts DDL is restricted to empty local databases; the existing-store branch
>applies selected upgrades but not this index. The changed query is reached when serving an unscoped
>stored census, and the revised performance assertion explicitly expects the new index. The README
>acknowledges that existing databases require a separate bootstrap; that rollout is not present or
>verifiable in this PR.
></pre>
> <code>[src/traust_core/v1/context/context.py\[740-753\]](https://github.com/traust-security/traust-core/blob/4fd2391039cc384721c2d281c4d27fe0bb407e46/src/traust_core/v1/context/context.py/#L740-L753)</code>
<code>[src/traust_core/v1/services/insight.py\[373-382\]](https://github.com/traust-security/traust-core/blob/4fd2391039cc384721c2d281c4d27fe0bb407e46/src/traust_core/v1/services/insight.py/#L373-L382)</code>
<code>[src/traust_core/v1/repositories/traust/findings/decision.py\[177-187\]](https://github.com/traust-security/traust-core/blob/4fd2391039cc384721c2d281c4d27fe0bb407e46/src/traust_core/v1/repositories/traust/findings/decision.py/#L177-L187)</code>
<code>[perf/queries/test_findings.py\[658-660\]](https://github.com/traust-security/traust-core/blob/4fd2391039cc384721c2d281c4d27fe0bb407e46/perf/queries/test_findings.py/#L658-L660)</code>
<code>[README.md\[225-229\]](https://github.com/traust-security/traust-core/blob/4fd2391039cc384721c2d281c4d27fe0bb407e46/README.md/#L225-L229)</code>
></details>
<details>
><summary>Agent prompt</summary>
><br/>
>>The issue below was found during a code review. Follow the provided context and guidance below and implement a solution > >## Issue description >Existing databases do not receive the partial index required by the new per-read freshness predicate through this repository's initialization path. >## Fix Focus Areas >- src/traust_core/v1/repositories/traust/findings/decision.py[177-187] >- src/traust_core/v1/context/context.py[740-753] >- perf/queries/test_findings.py[658-660] >## Recommended Fix >Provide a reviewed additive index upgrade or an enforced deployment prerequisite for existing SQLite and PostgreSQL databases before activating this query. Verify the plan on an upgraded existing database, not only one created from the new contracts DDL. >
<code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
></details>
<hr/>
</details>
<details>
<summary> 2. Repeated builds can reuse an old publication <code>🐞 Bug</code> <code>≡ Correctness</code></summary>
<br/>
<details open>
><summary>Description</summary>
><br/>
><pre>
><b><i>record_builds()</i></b> uses <b><i>_clock.now()</i></b> as the only new per-observation value in the document, while
>artifact publication deduplicates identical payload digests. If two unchanged folds receive the same
>clock value, including with the supported <b><i>FixedClock</i></b>, the second call returns the earlier artifact
>row rather than recording a new observation.
></pre>
></details>
<details>
><summary>Code</summary>
><br/>
><code>[src/traust_core/v1/services/insight.py[398]](https://github.com/traust-security/traust-core/pull/442/files#diff-33baccc4b7229ac6a008e1deb9111adb23d99dc4414fb04187a3db0db1ce0f73R398-R398)</code>
>>+ "built_at": self._clock.now().isoformat(), >
></details>
<details>
><summary>Evidence</summary>
><br/>
><pre>
>The new field is sourced from the injected clock; <b><i>FixedClock</i></b> can return the same instant for both
>calls. The imported-artifact insert resolves a digest conflict by returning the existing row, and
>insight serving selects from that publication history. This is a code-inspection finding, not an
>executed reproduction.
></pre>
> <code>[src/traust_core/v1/services/insight.py\[391-405\]](https://github.com/traust-security/traust-core/blob/4fd2391039cc384721c2d281c4d27fe0bb407e46/src/traust_core/v1/services/insight.py/#L391-L405)</code>
<code>[src/traust_core/v1/providers/clock.py\[11-18\]](https://github.com/traust-security/traust-core/blob/4fd2391039cc384721c2d281c4d27fe0bb407e46/src/traust_core/v1/providers/clock.py/#L11-L18)</code>
<code>[src/traust_core/v1/repositories/traust/assessments/artifact.py\[78-108\]](https://github.com/traust-security/traust-core/blob/4fd2391039cc384721c2d281c4d27fe0bb407e46/src/traust_core/v1/repositories/traust/assessments/artifact.py/#L78-L108)</code>
<code>[src/traust_core/v1/services/insight.py\[405-416\]](https://github.com/traust-security/traust-core/blob/4fd2391039cc384721c2d281c4d27fe0bb407e46/src/traust_core/v1/services/insight.py/#L405-L416)</code>
></details>
<details>
><summary>Agent prompt</summary>
><br/>
>>The issue below was found during a code review. Follow the provided context and guidance below and implement a solution > >## Issue description >Two completed unchanged folds with the same clock value produce identical documents, so digest-based artifact deduplication reuses the first publication. >## Fix Focus Areas >- src/traust_core/v1/services/insight.py[394-405] >- src/traust_core/v1/repositories/traust/assessments/artifact.py[78-108] >- tests/v1/services/test_insight_builds.py[287-302] >## Recommended Fix >Give each completed census observation a distinct publication identity independent of clock precision, while preserving the required `built_at` value and the existing artifact contract. Add a regression that records unchanged builds twice at the same clock instant and checks for distinct artifact IDs and a latest publication. >
<code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
></details>
<hr/>
</details>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<!-- qodo-self-review-slot -->
<!-- qodo-context:start -->
<details><summary><strong>Context sources</strong></summary>
<div>Review mode: <code>Auto: ⚖️ Balanced</code>: Behavioral freshness, publication, query, and companion-contract changes require careful review.</div>
<!-- qodo-context:end -->
</details>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<!-- qodo-daily-tip:start -->
<details>
<summary><strong>Tip of the day</strong></summary>
<br/>
<pre>💡 Did you know, you can start a comment with 'qodo' or '@qodo' to chat about any finding</pre>
<a href="https://docs.qodo.ai/tips-and-tricks">More tips ↗</a> | <a href="https://app.qodo.ai/configurations?tab=display-preferences">Customize Qodo ↗</a> | <a href="https://docs.qodo.ai">Qodo docs ↗</a>
</details>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<!-- qodo-daily-tip:end -->
<!-- https://github.com/traust-security/traust-core/commit/4fd2391039cc384721c2d281c4d27fe0bb407e46 -->
<a href="https://www.qodo.ai"><img src="https://www.qodo.ai/wp-content/uploads/2025/03/qodo-logo.svg" width="80" alt="Qodo Logo"></a>
Verified against the #143 review thread and both Qodo passes — this is the matching producer/reader half, and it is honest about what it does not fix.
What the diff delivers, verified:
built_at on every completed observation (src/traust_core/v1/services/insight.py:398): the document now carries the observation clock, so the digest changes on every cadence run even when the folded views are byte-identical. The two new regressions pin the behavior — test_an_unchanged_cadence_observation_is_a_new_publication and test_returning_to_an_earlier_census_publishes_it_as_the_latest_build (A-B-A content yields three distinct artifact ids, and the final serve selects the third). That closes Qodo's #143 finding ("unchanged builds expire despite republishing"), resolved on the contracts side at 4c8f205.src/traust_core/v1/repositories/traust/findings/decision.py:176-190): actor IS NOT NULL AND substr(actor, 1, 8) <> 'machine:', kept literal so the prepared statement plans on idx_decision_nonmachine_recorded. test_legacy_actor_statements_advance_the_freshness_watermark covers user:, bare email spellings and case-variant Machine:; test_a_legacy_actor_invalidates_the_build_without_gaining_human_authority confirms the effect is invalidation only — after the cadence rebuild the stored build serves exactly the on-demand fold's model. The perf case still asserts the Index Only Scan on the new partial index.The two Qodo findings on this PR, adjudicated (advisory per the org's review policy; remediation stays with the author):
FixedClock in tests; production wires SystemClock (datetime.now(UTC), microsecond resolution) against a 10-minute cadence, and the A-B-A reuse additionally requires the clock to return to an earlier instant. The failure direction under a stuck clock is also conservative: the reused row keeps the older created_at, so the serving-age bound expires it sooner and the read folds on demand — extra folds, never staler content. No change.The commit-order limitation — max recording time is not commit order — stays open by design, tracked as #443 with the candidate designs (a WAL bound, a commit-sequenced counter, a snapshot-pinned read point) and their costs.
Merge state: required checks green (lint, test 3.11/3.12, postgres; api-check and ratchet also green), the head commit is signed and valid, and the pin 4c8f205 is #143's head — this branch's CI ran against exactly that revision. #143 itself is fully green and waits only on its code-owner review (contracts enforces one; the author cannot supply it). Merging this companion now lands the reader/producer half against the already-pinned, tested revision; the envelope follows the moment its review gate clears.
🤖 Generated with Crush
Assisted-by: Crush:rits/zai-org/glm-5-3
gurnben
chubirka
Companion consumer update for traust-contracts#143, pinned to 4c8f205. Core#434 has merged, so this is a new PR rather than rewriting that history.
Verification: affected service/repository regressions on SQLite and PostgreSQL passed after correcting new test fixtures; 22 REST tests passed; ruff check/format passed. A real prepared-query profile over 40,000 synthetic machine rows plus a legacy actor used Index Only Scan on the new index (0.004 ms). No full Core coverage or production deployment claimed.
Independent review finding (pre-existing, not fixed here): max recording-time is not commit-order. A delayed older transaction can commit during a fold without advancing the watermark; reproduced stored critical=1 versus fresh critical=0. Commit-safe invalidation needs a separate Core fix. This PR does not claim serializable snapshot freshness.
Changed tests: added unchanged/returning-publication regressions and legacy invalidation-without-authority coverage; retained canonical human and machine behavior. Updated the existing query performance case for the new index.