Skip to content

Add workspace integrity guard - #75

Closed
Alajmah wants to merge 0 commit into
mainfrom
lane64-audit-guard
Closed

Alajmah wants to merge 0 commit into
mainfrom
lane64-audit-guard

Conversation

@Alajmah

@Alajmah Alajmah commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a small workspace integrity guard around patch application.

Changes

  • Adds a helper for saving and restoring a patch target.
  • Applies the helper in approval and direct apply paths.
  • Adds focused workspace integrity tests.

Closes #64.

Validation

CI should run on this PR.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ab1fd304ae

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/sovereign_cortex/patch_restore.py Outdated
Comment on lines +23 to +24
except Exception:
_restore_patch_target(snapshot)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Restore the staged index entry after a failed commit

GitAudit.commit_paths() runs git add before attempting the commit, so when the commit fails—for example, because a pre-commit hook rejects it—this handler restores only the working-tree file while leaving the failed patch staged. The approval remains pending, but a later unrelated git commit can silently record that patch; snapshot and restore the target's prior index state as part of this rollback.

Useful? React with 👍 / 👎.

Comment thread src/sovereign_cortex/patch_restore.py Outdated
Comment on lines +30 to +32
existed = path.exists()
content = path.read_text(encoding="utf-8") if existed else None
return PatchTargetSnapshot(path=path, existed=existed, content=content)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the target's exact bytes in the snapshot

When an existing target uses CRLF line endings, read_text() performs universal-newline conversion and the later write_text() restoration writes normalized platform line endings. On Linux, a failed commit therefore changes the supposedly restored file from CRLF to LF; use byte-oriented snapshot and restoration so rollback reproduces the original content exactly.

Useful? React with 👍 / 👎.

Comment thread src/sovereign_cortex/patch_restore.py Outdated
Comment on lines +21 to +24
try:
yield
except Exception:
_restore_patch_target(snapshot)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restore the target when patch application is interrupted

If the user interrupts patch application or the Git commit with Ctrl-C, KeyboardInterrupt bypasses this except Exception block, leaving the applied file—and potentially its staged entry—in place even though the command never completed. Cleanup context managers should restore on BaseException (and then re-raise) so cancellation receives the same integrity guarantee as ordinary failures.

Useful? React with 👍 / 👎.

@Alajmah Alajmah closed this Jun 9, 2026
@Alajmah
Alajmah force-pushed the lane64-audit-guard branch from ab1fd30 to 19415de Compare June 9, 2026 00:36
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.

Protect vault writes from Git audit failure

1 participant