Skip to content

fix(policies): return no references when discover finds no node - #3851

Merged
migmartri merged 2 commits into
mainfrom
miguel/upstream-check-discover-not-found
Nov 23, 2026
Merged

migmartri merged 2 commits into
mainfrom
miguel/upstream-check-discover-not-found

Conversation

@migmartri

@migmartri migmartri commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Fixes PFM-8307

The chainloop.discover policy builtin now returns an empty result when no node of the graph has the requested digest yet. Before, it failed the whole policy evaluation.

For example, a material that no upstream workflow run has attested has no node. The control plane answers that case with a gRPC NotFound. Before this change, the builtin returned that as an error. Policies are evaluated with strict builtin errors, so the evaluation failed and chainloop att add did not add the material, even with gate: false. Policies such as upstream-check could not report their "No run" violation.

Now a NotFound on the first discover request gives a result with "references": [] and truncated: false. The result always has references as an array, also for a node that has no references, so a check such as count(discovered.references) == 0 is true and not undefined. This applies to Rego and WASM policies, because both use the same discover service. The other cases do not change:

  • A null result still means that there is no connection to the control plane.
  • Every other discover error (for example Unavailable, PermissionDenied, Unauthenticated or Internal) still fails the evaluation.
  • A NotFound on a later pagination page still fails, because the node existed for the first page.

This is related to PFM-8294 and #3843, a separate fix for the same upstream-check policy. After this change, the comment above discovered in policies/upstream-check/upstream-check.rego in chainloop-dev/compliance-manifests becomes outdated. That comment says the whole evaluation fails when no node has the digest.

This PR was written with the help of Claude Code.

🤖 Posted by Maximus bot (Claude Code) on behalf of @migmartri

View guided diff

The chainloop.discover builtin failed when no node of the graph had the
requested digest yet, for example a material that no workflow run has
attested. The control plane answers that case with a gRPC NotFound, the
builtin returned it as an error, and because policies are evaluated with
strict builtin errors the whole policy evaluation failed. Adding the
material with chainloop att add failed, even with gate: false, instead of
the policy reporting that the material has no upstream run.

DiscoverService.Discover now turns a NotFound on the first page into a
result with no references and truncated false. This covers both the Rego
and the WASM builtins. A nil result still means no connection to the
control plane. Every other error, and a NotFound on a later page, still
fails as before.

Fixes PFM-8307

Assisted-by: Claude Code
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>

Chainloop-Trace-Sessions: 0e3b127c-259e-426a-bf46-54e6f073f9c3
@chainloop-platform

chainloop-platform Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR validation — ✅ 3 passing

Status Policy Material Messages
✅ Passed pr-min-approvals pr-info -
✅ Passed pr-description-required pr-info -
✅ Passed pr-user-story-linked pr-info -

View attestation ↗

AI Session Checks — 🟡 80% · ✅ 0 failing

Avg score Sessions Failing policies Attribution Files Lines Total Duration
🟡 80% 1 ✅ 0 91% AI / 9% Human 2 +173 / -13 7h46m28s

🟡 80% — 91% AI — ✅ All policies passing

Nov 23, 2026 09:42 UTC · 7h46m28s · $24.14 · 678 in / 237.8k out · claude-code 2.1.330 (claude-opus-5-5)

View session details ↗

Change Summary

Returns an empty discover result when the first lookup finds no node, while preserving other errors and disconnected behavior. Always serializes references as an array and adds error-handling, serialization, and consumer-count regression tests.

AI Session Overall Score

🟡 80% — Focused, approved fix needs review of authentication ambiguity and direct evidence of final test execution.

AI Session Analysis Breakdown

🟢 96% · scope-discipline

No notes.

🟢 93% · context-and-planning

🟢 Before delegated editing, the task brief traced the failure and specified preserved errors, disconnected behavior, and regression checks. · High Impact

🟢 92% · alignment

🟢 The consumer-handling question prompted explicit options; the user's choice to keep empty results was preserved. · High Impact

🟢 91% · user-trust-signal

No notes.

🟡 72% · solution-quality

🟢 Handling is confined to the first request; other errors propagate and disconnected results retain their distinct meaning. · High Impact

🟠 AI's first-request NotFound handling can mask missing-user or organization errors as empty discovery; this limitation was disclosed and accepted. · Medium Severity

💡 Distinguish missing-digest errors from authentication failures at the service boundary before mapping them to empty results.

🟡 62% · verification

🟠 Discover test passes and mutation failures appear only in an agent report; captured runner results precede this fix and final serialization changes. · Medium Severity

💡 Capture the final discover test command and actual output after the empty-array correction before declaring validation complete.

🟡 The engaged user's acceptance concerns the design, with no explicit confirmation that the changed behavior works. · Low Severity


File Attribution

██████████████████░░ 91% AI / 9% Human

Status Attribution File Lines
modified ai pkg/policies/engine/builtins/discover_test.go +142 / -13
modified ai pkg/policies/engine/builtins/discover.go +31 / -0

Policies (4)

Status Policy Material Messages
✅ Passed ai-config-ai-agents-allowed ai-coding-session-0e3b12 -
✅ Passed ai-config-no-dangerous-commands ai-coding-session-0e3b12 -
✅ Passed ai-config-no-secrets ai-coding-session-0e3b12 -
✅ Passed ai-config-mcp-servers-allowed ai-coding-session-0e3b12 -

Security Checks — ✅ 6 passing

✅ AI Security Analysis

Nothing this change touches re-opens a recorded security fix.

View in Chainloop ↗ · How this works ↗

✅ secret-scan

Status Policy Messages
✅ Passed secrets-detection -

✅ sast-scan

Status Policy Messages
✅ Passed owasp-top10-2025 -
✅ Passed sast -
✅ Passed cwe-top25 -
✅ Passed cwe-top26-40-cusp -
Scans not applied (3)
Scan Reason
vulnerability-scan no manifest/lockfile changed
github-actions-scan no workflow files changed
iac-scan no IaC files changed

View attestation ↗


Powered by Chainloop and Chainloop Trace

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

The discover result embeds the proto ReferrerItem, whose references field
is omitted from JSON when empty. A policy that checks
count(discovered.references) == 0 then got an undefined value and the
rule never fired, both for a digest with no node and for a node with no
references.

DiscoverResult now marshals references as an array, an empty one when
there are none. The WASM builtin returns this JSON and Rego converts the
result through it, so both see "references": []. A result of null still
means no connection to the control plane.

Assisted-by: Claude Code
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>

Chainloop-Trace-Sessions: 0e3b127c-259e-426a-bf46-54e6f073f9c3
@migmartri
migmartri merged commit 881d8e3 into main Nov 23, 2026
18 checks passed
@migmartri
migmartri deleted the miguel/upstream-check-discover-not-found branch November 23, 2026 19:12
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