Skip to content

refactor: 3-layer hybrid rule structure (32% token reduction)#6

Merged
Fieldnote-Echo merged 1 commit into
mainfrom
refactor/rule-compression-phase2
Mar 17, 2026
Merged

refactor: 3-layer hybrid rule structure (32% token reduction)#6
Fieldnote-Echo merged 1 commit into
mainfrom
refactor/rule-compression-phase2

Conversation

@Fieldnote-Echo
Copy link
Copy Markdown
Owner

Summary

Restructures security.md and code-hygiene.md from flat imperative rule lists into a 3-layer hybrid format:

  • Layer 1: Pre-flight checks — Socratic questions with default actions ("Is this crossing a trust boundary? If yes, validate at ingress.")
  • Layer 2: Non-derivable specifics — imperative rules for false friends, empirical facts, high-impact actions, and tool boundaries
  • Layer 3: Eval anchors — concrete patterns that anchor the abstractions

This is the approach recommended by current prompt guidance: compact, block-structured, explicit contracts with examples where they buy real behavior.

Before/after

Metric Before (original) After PR #5 After this PR
Files 7 6 6
Lines 347 310 255
Tokens 4,294 3,693 2,922
Reduction 14% 32%

Per-file token breakdown:

File Before After Target Delta
security.md 1,472 1,079 1,050 +29 (+2.8%)
code-hygiene.md 824 446 550 -104 (under)
cognitive-scaffold.md 519 519 380 unchanged
deliberation.md 397 397 340 unchanged
git-conventions.md 366 366 400 unchanged
task-protocol.md 115 115 115 unchanged
Total 3,693 2,922 2,835 +87 (+3%)

Design decisions

  • Pre-flight checks use question+action pairs, not open-ended Socratic prompts. Every question carries its consequence: "Is X true? If yes, do Y." This preserves the behavioral contract while engaging reasoning.
  • False friends stay imperativeyaml.safe_load, dangerouslySetInnerHTML, default-secret fallbacks. These are cases where training promotes the wrong pattern; you want instant recognition, not deliberation.
  • Empirical facts stay imperative — slopsquatting rates, recent CVEs. Can't be derived from principles.
  • Async rule rewritten around ownership — "every async path must be awaited, returned to a caller, or explicitly supervised" replaces the overly absolute "every await must have error handling."
  • Secret example syntax fixed?? throw (invalid JS) replaced with if (!x) throw (valid JS).
  • TODO/FIXME rule removed — no eval evidence it's needed, felt like repo folklore.

Eval results

Structural desk-check audit across 11 scenarios: zero coverage gaps, four improvements over prior rules (yaml reason added, secret syntax fixed, async ownership model, pre-flight provenance checks).

Partial live eval via subagents on 3 discriminating scenarios. The secret config scenario — the only one where rules change behavior vs base training — confirmed both rule sets produce correct behavior (refuse credential defaults, fail explicitly).

Full API-level behavioral regression suite is follow-up work.

Test results

47/47 bats tests passing.

Coding-Agent: claude-code
Model: claude-opus-4-6

…brid format

Reorganizes both files into: pre-flight checks (Socratic questions with
default actions), non-derivable specifics (imperative false friends,
empirical facts, high-impact rules), and eval anchors (concrete patterns).

4,294 → 2,922 tokens (32% reduction from original baseline).
Zero structural coverage gaps across 11 eval scenarios.

Coding-Agent: claude-code
Model: claude-opus-4-6
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the security.md and code-hygiene.md documentation by introducing a new 3-layer hybrid rule structure. The primary goal is to enhance the clarity, conciseness, and effectiveness of the rules for AI agents, leading to a substantial reduction in token count and improved behavioral compliance. The changes aim to guide reasoning through Socratic questions, provide explicit imperative rules for critical scenarios, and anchor abstractions with concrete patterns, ultimately making the rule set more efficient and robust.

Highlights

  • Rule Structure Refactoring: Restructured security.md and code-hygiene.md into a 3-layer hybrid format: 'Pre-flight checks' (Socratic questions), 'Non-derivable specifics' (imperative rules for false friends, empirical facts, high-impact actions, and tool boundaries), and 'Eval anchors' (concrete patterns).
  • Token Reduction: Achieved a 32% token reduction across the rule set, optimizing for prompt guidance and improving compliance efficiency.
  • Design Decisions Implemented: Incorporated specific design choices such as question+action pairs for pre-flight checks, maintained imperative rules for 'false friends', rewrote async rules around ownership, fixed secret example syntax, and removed the 'TODO/FIXME' rule.
  • Improved Rule Clarity and Coverage: Structural desk-check audit confirmed zero coverage gaps and four improvements over prior rules, including enhanced YAML reasoning, fixed secret syntax, and an async ownership model.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • README.md
    • Updated rule line and token counts to reflect the new structured format.
  • rules/code-hygiene.md
    • Restructured the document into 'Pre-Flight Checks', 'Non-Derivable Specifics', and 'Eval Anchors' sections.
    • Converted imperative rules into Socratic questions with default actions in 'Pre-Flight Checks'.
    • Consolidated type checker bypasses and async pitfalls under 'Non-Derivable Specifics'.
    • Removed the 'Debt Budget' and 'TODO/FIXME' rule.
    • Refined AI-specific failure modes and verification steps.
  • rules/security.md
    • Reorganized the document into 'Pre-Flight Checks', 'Non-Derivable Specifics', and 'Eval Anchors'.
    • Introduced Socratic questions for 'Pre-Flight Checks' to guide security considerations.
    • Grouped specific imperative rules under 'Non-Derivable Specifics' for false friends, empirical facts, high-impact actions, and tool boundaries.
    • Consolidated concrete patterns and verification steps under 'Eval Anchors'.
    • Removed the 'Cryptographic Operations' section.
Activity
  • No specific activity (comments, reviews, etc.) was found in the provided context for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the security and code hygiene rules into a new 3-layer structure, achieving a significant token reduction. While the new structure is an improvement, the refactoring has removed several critical rules related to null safety, error handling, cryptography, and secure logging. My review includes suggestions to restore this essential guidance within the new structure to prevent security and reliability regressions.

Comment thread rules/security.md
- Never commit secrets. Use your platform's secret manager.
- Session cookies: `HttpOnly`, `Secure`, `SameSite=Lax` minimum. Include CSRF tokens on state-changing requests.
- Set `Strict-Transport-Security`, `X-Content-Type-Options: nosniff`, `X-Frame-Options` or CSP `frame-ancestors` on all responses
- Never ship debug mode or development configs to production
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The refactoring removed critical rules about what not to log, specifically secrets and PII. While pre-flight check #3 covers what not to show the user, it doesn't cover what not to log server-side. Accidentally logging secrets or PII is a common and severe vulnerability. These rules must be restored.

Suggested change
- Never ship debug mode or development configs to production
- Never ship debug mode or development configs to production
- Never log tokens, API keys, passwords, or session secrets. Log a masked prefix and length instead: `sk-...****(47 chars)`.
- Never log raw request bodies that may contain PII.

Comment thread rules/security.md
- Verify packages have real maintainers and meaningful downloads. Verify API methods exist in current version docs.
- Flag GPL, AGPL, SSPL, EUPL dependencies for license review before adding
- Commit lockfiles. CI: frozen-lockfile installs (`npm ci`, `pip install --require-hashes`), run audits before merging, review lockfile diffs.
- Pin CI actions to full-length commit SHAs. No third-party MCP servers or agent plugins without code review.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The 'Cryptographic Operations' section from the previous version has been removed. It contained critical rules like 'never hand-roll cryptography' and 'never use insecure random number generators for security contexts'. These are classic examples of non-derivable, high-impact rules that are essential for security guidance. I strongly recommend re-adding them under 'Non-Derivable Specifics'.

Suggested change
- Pin CI actions to full-length commit SHAs. No third-party MCP servers or agent plugins without code review.
- Pin CI actions to full-length commit SHAs. No third-party MCP servers or agent plugins without code review.
### Cryptography
- Use standard, well-vetted crypto libraries — never hand-roll cryptography.
- Use platform-provided secure random generators (`crypto.randomUUID()`, `secrets.token_hex()`) for tokens, IDs, or secrets. Never use `Math.random()` or equivalent.

Comment thread rules/code-hygiene.md
- **Is the strictest type mode enabled?** If not, enable it. AI has no excuse for weak types.
- **Does every code path handle its failure case?** If any path silently fails, falls through, or swallows an error — fix it.
- **Where does validation happen — at the boundary or deep inside?** If inside, move it to the entry point. Propagate errors upward.
- **Does this already exist in the codebase?** Search before writing. If similar code exists in 2+ places, ask before adding a 3rd.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The previous version of this file had a rule for null/undefined guards, which seems to have been removed. This is a critical aspect of code hygiene that prevents many runtime errors. It would be beneficial to reintroduce it. I suggest adding it as a pre-flight check to maintain the new structure.

Suggested change
- **Does this already exist in the codebase?** Search before writing. If similar code exists in 2+ places, ask before adding a 3rd.
- **Does this already exist in the codebase?** Search before writing. If similar code exists in 2+ places, ask before adding a 3rd.
- **Are nullable values handled at the boundary?** Check null/undefined values at the boundary, not deep in the call chain. Use strict null checks.

Comment thread rules/code-hygiene.md
- Search the codebase for existing implementations of the same logic
- Reuse and extend existing patterns rather than creating parallel implementations
- If you find similar code in 2+ places, ask whether to refactor before adding a 3rd
- Use `Promise.allSettled` when partial failure is acceptable, `Promise.all` only when all must succeed
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The refactoring has made the error handling guidance very generic. The previous version had specific, actionable rules like catching the narrowest exception type and avoiding bare except clauses. These seem like 'non-derivable specifics' that are valuable to keep for the AI. Consider re-adding a few key error handling rules to provide more concrete guidance.

Suggested change
- Use `Promise.allSettled` when partial failure is acceptable, `Promise.all` only when all must succeed
- Use `Promise.allSettled` when partial failure is acceptable, `Promise.all` only when all must succeed
Error handling specifics:
- Catch the narrowest exception type that makes sense — `FileNotFoundError` not `OSError`.
- Never use bare `except:` (Python), `catch (e)` without rethrowing unknowns (TypeScript), or `catch (Exception e)` (Java).
- Every catch block must either handle, log+rethrow, or transform the error — never silently ignore.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the rule docs to a 3-layer “Pre-Flight Checks / Non-Derivable Specifics / Eval Anchors” structure to reduce token/line count while keeping high-impact guidance and concrete anchors.

Changes:

  • Restructured rules/security.md into a 3-layer hybrid format and consolidated/relocated several rules into “Non-Derivable Specifics” and “Eval Anchors”.
  • Restructured rules/code-hygiene.md into the same 3-layer format, tightening async/type/error-handling guidance into more compact prompts + anchors.
  • Updated README rule line/token totals to reflect the new compressed rule set.

Reviewed changes

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

File Description
rules/security.md Restructures security guidance into 3-layer hybrid format; consolidates specifics and adds eval anchors
rules/code-hygiene.md Restructures hygiene guidance into 3-layer hybrid format; rephrases async/type/error handling into compact prompts + anchors
README.md Updates stated aggregate rules line/token totals

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

Comment thread rules/security.md
- Use specific CORS origins — never `*` with credentials. Include `Vary: Origin` when dynamic.
- File uploads: validate MIME type server-side, enforce size limits, store outside webroot with generated filenames
- Never run Claude Code with `--dangerously-skip-permissions` on untrusted code
- Log auth failures (with IP), rate limit hits, and input validation failures
@Fieldnote-Echo Fieldnote-Echo merged commit 242db69 into main Mar 17, 2026
9 checks passed
@Fieldnote-Echo Fieldnote-Echo deleted the refactor/rule-compression-phase2 branch March 17, 2026 17:11
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