fix(crafter): use deterministic filename for AI config collector#2943
Draft
waveywaves wants to merge 1 commit intochainloop-dev:mainfrom
Draft
fix(crafter): use deterministic filename for AI config collector#2943waveywaves wants to merge 1 commit intochainloop-dev:mainfrom
waveywaves wants to merge 1 commit intochainloop-dev:mainfrom
Conversation
529538f to
486591a
Compare
Replace os.CreateTemp (which generates a random suffix) with a deterministic filename derived from the config's content hash. Root cause: although AddMaterialContractFree uses a deterministic map key (Materials[m.Name]), the Artifact struct embedded in each material gets its Name and Digest from fileStats(), which calls os.Stat(filepath).Name(). A random temp filename therefore produces a different Artifact.Name on every call — even when the JSON payload is byte-for-byte identical — causing the CAS to treat each retry as a new, distinct resource. Changes: - Extract aiConfigTempFilePath() helper for deterministic path generation - Use full ConfigHash (no truncation) to avoid collision risk - Write into a private os.MkdirTemp directory (symlink-safe) instead of the shared os.TempDir() - Add unit tests proving same-input-same-output and different-input- different-output invariants Fixes: chainloop-dev#2907 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Vibhav Bobade <vibhav.bobde@gmail.com>
486591a to
cb22854
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
os.CreateTemp(random suffix) with a deterministic filename derived from the fullConfigHash, eliminating duplicate CAS uploads on attestation retriesos.MkdirTempdirectory instead of the sharedos.TempDir()to prevent symlink attacksaiConfigTempFilePath()helper with documentation explaining the root causeRoot Cause Analysis
The reviewer questioned whether the filename is actually the cause of duplicates since
Materials[m.Name]already uses a deterministic key. The answer is yes — the map key is deterministic, but the Artifact struct inside the material is not.Here is the chain of causation:
uploadAgentConfig()callsos.CreateTemp("", "ai-agent-config-claude-*.json"), which produces a random filename like/tmp/ai-agent-config-claude-3847291.jsonAddMaterialContractFree()callsaddMaterial(), which callsuploadAndCraft()uploadAndCraft()callsfileStats(artifactPath), which doesos.Stat(filepath).Name()to get the basename of the temp fileArtifact.Namein the material proto (line 136 ofmaterials.go)fileStats()also computessha256(file_contents)which becomesArtifact.DigestBecause the JSON payload is identical across retries,
Artifact.Digeststays the same. ButArtifact.Namechanges every time (random temp suffix), so the CAS sees a different resource name on each retry. The map keyMaterials["ai-agent-config-claude"]is overwritten (good), but each retry still triggers a new CAS upload because the artifact metadata differs.The fix makes the filename deterministic by deriving it from
ConfigHash, soArtifact.Nameis stable across retries.Changes
pkg/attestation/crafter/collector_aiagentconfig.go:aiConfigTempFilePath()helper with detailed doc comment explaining the root causeos.CreateTemp+ manual write/close withos.MkdirTemp(private dir) +os.WriteFile(deterministic filename)ConfigHashinstead of truncated[:12]— no reason to truncate a sha256 hex stringdefer os.Remove(tmpFile.Name())in favor ofdefer os.RemoveAll(tmpDir)pkg/attestation/crafter/collector_aiagentconfig_test.go(new):Test Plan
go build ./pkg/attestation/crafter/passesgo test ./pkg/attestation/crafter/...— all 8 sub-packages passTestAIConfigTempFilePath_Deterministictests verify:Fixes #2907