Skip to content

Simplify challenge sampling to use full file content#301

Merged
filinvadim merged 6 commits into
developfrom
claude/tender-dijkstra-rbgr0c
Jun 21, 2026
Merged

Simplify challenge sampling to use full file content#301
filinvadim merged 6 commits into
developfrom
claude/tender-dijkstra-rbgr0c

Conversation

@filinvadim

Copy link
Copy Markdown
Member

Summary

Refactored the challenge sampling mechanism to read entire file contents instead of extracting random lines and substrings. This simplifies the sampling logic and reduces the number of error cases.

Key Changes

  • Removed line-based sampling: eliminated getRandomLine() and readLines() functions that filtered and parsed files line-by-line
  • Simplified SampleLocation.FileStack from storing 4 values (file index, line index, left/right bounds) to just 1 (file index)
  • Updated generateSample() to read full file content via fs.ReadFile() instead of extracting random substrings
  • Removed unused error types: ErrEmptySampleLine, ErrNoNonEmptySampleLines, ErrInvalidSubstringBounds
  • Updated findSample() to retrieve and return complete file content
  • Changed directory/file selection to treat subdirectories and files equally in the sampling pool
  • Removed bufio import (no longer needed)
  • Updated ErrInvalidStackSize message to reflect new stack structure

Implementation Details

  • Files are now sampled as complete units rather than extracting specific line ranges
  • The sampling algorithm now iterates through a combined pool of subdirectories and files, giving equal probability to both
  • Validation logic simplified: only checks that FileStack has at least 1 element and that the file index is in bounds

https://claude.ai/code/session_019AKujXgp59pHmUDM4uaiTF

claude and others added 2 commits June 21, 2026 18:46
… samples

generateSample descended into the first non-empty subdirectory and only fell back to the current directory's files when every subtree failed, so files outside leaf directories were almost never sampled. Treat each subdirectory and each file as equal candidates at every level so coverage spans the whole tree.

Replace the random line substring (which could be a single near-worthless character) with the whole file's contents as the sample. FileStack now carries just the file index.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019AKujXgp59pHmUDM4uaiTF
@codecov-commenter

codecov-commenter commented Jun 21, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
security/challenge.go 88.00% 2 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

claude added 2 commits June 21, 2026 18:58
Add deterministic tests on an in-memory tree (fstest.MapFS) for the two fixed bugs: every sample is a whole file (not a substring), files at every directory depth are sampled (not just leaves), the generate/resolve round-trip holds, the challenge equals SHA256(wholeFile + nonce), and the index/empty-stack/empty-codebase error paths.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019AKujXgp59pHmUDM4uaiTF
Consolidate the whole-file/all-level traversal tests into the existing challenge_test.go instead of a separate file.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019AKujXgp59pHmUDM4uaiTF
io/fs paths (embed.FS, fstest.MapFS) are always slash-separated. filepath.Join produces backslashes on Windows, so traversal below the top level failed and only root files were sampled - breaking sampling on Windows nodes and cross-platform challenges. Switch to path.Join.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019AKujXgp59pHmUDM4uaiTF

Copilot AI 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.

Pull request overview

This PR refactors the security challenge sampling/resolution flow to sample entire file contents (instead of random line substrings), simplifying SampleLocation.FileStack and removing now-unneeded line/substring logic and error cases.

Changes:

  • Simplified sampling to read whole file contents via fs.ReadFile, and updated SampleLocation.FileStack to only store a file index.
  • Removed line-based helpers/errors and updated stack-size validation/error messaging accordingly.
  • Added targeted tests to lock in the new whole-file sampling and round-trip behavior; bumped the repo version.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
version Bumps application/version identifier.
security/challenge.go Implements whole-file sampling, updates location stack structure, and simplifies resolution accordingly.
security/challenge_test.go Adds regression/contract tests for whole-file sampling and Generate/Resolve round-trips.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread security/challenge.go Outdated
findSample resolves a SampleLocation that arrives over the wire (JSON) from a peer; a negative file_stack/dir_stack value passed the upper-bound-only check and panicked on slice indexing. Guard both lower bounds and return ErrSampleIndexOutOfBounds. Addresses a PR review comment.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019AKujXgp59pHmUDM4uaiTF
@filinvadim filinvadim merged commit 27c500d into develop Jun 21, 2026
6 checks passed
@filinvadim filinvadim deleted the claude/tender-dijkstra-rbgr0c branch June 21, 2026 19:17
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.

4 participants