-
Notifications
You must be signed in to change notification settings - Fork 279
capture scenario where git hooks are overwritten during a running agent prompt #791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,116 @@ | ||||||||||||||||||||||||||||||||||||||||
| //go:build integration | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| package integration | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||||||||||||||
| "testing" | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| "github.com/entireio/cli/cmd/entire/cli/strategy" | ||||||||||||||||||||||||||||||||||||||||
| "github.com/stretchr/testify/assert" | ||||||||||||||||||||||||||||||||||||||||
| "github.com/stretchr/testify/require" | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // TestHookOverwrite_MidTurnWipe_NextPromptRecovers simulates the scenario from | ||||||||||||||||||||||||||||||||||||||||
| // https://github.com/entireio/cli/issues/784: | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // Flow: | ||||||||||||||||||||||||||||||||||||||||
| // 1. Prompt 1: agent creates files, commits via hooks → checkpoint trailer ✓ | ||||||||||||||||||||||||||||||||||||||||
| // 2. Mid-turn: third-party tool (husky/lefthook) overwrites git hooks | ||||||||||||||||||||||||||||||||||||||||
| // 3. Agent commits again (no hooks fire) → NO trailer | ||||||||||||||||||||||||||||||||||||||||
| // 4. Prompt 2 starts (user-prompt-submit) → EnsureSetup reinstalls hooks | ||||||||||||||||||||||||||||||||||||||||
| // 5. Agent commits via hooks → checkpoint trailer ✓ (hooks restored) | ||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||
| // The key insight: GitCommitWithShadowHooks invokes the binary directly (simulating | ||||||||||||||||||||||||||||||||||||||||
| // working hooks), while GitAdd+GitCommit uses go-git without hooks (simulating | ||||||||||||||||||||||||||||||||||||||||
| // overwritten hooks where `entire` is never called). | ||||||||||||||||||||||||||||||||||||||||
| func TestHookOverwrite_MidTurnWipe_NextPromptRecovers(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||
| t.Parallel() | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| env := NewFeatureBranchEnv(t) | ||||||||||||||||||||||||||||||||||||||||
| hooksDir := filepath.Join(env.RepoDir, ".git", "hooks") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| sess := env.NewSession() | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // === Prompt 1: normal flow, hooks work === | ||||||||||||||||||||||||||||||||||||||||
| err := env.SimulateUserPromptSubmitWithPromptAndTranscriptPath( | ||||||||||||||||||||||||||||||||||||||||
| sess.ID, "Create files A and B", sess.TranscriptPath) | ||||||||||||||||||||||||||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| env.WriteFile("fileA.go", "package main\n\nfunc A() {}\n") | ||||||||||||||||||||||||||||||||||||||||
| env.WriteFile("fileB.go", "package main\n\nfunc B() {}\n") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| sess.CreateTranscript("Create files A and B", []FileChange{ | ||||||||||||||||||||||||||||||||||||||||
| {Path: "fileA.go", Content: "package main\n\nfunc A() {}\n"}, | ||||||||||||||||||||||||||||||||||||||||
| {Path: "fileB.go", Content: "package main\n\nfunc B() {}\n"}, | ||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // First commit — hooks are intact, binary is invoked → trailer added | ||||||||||||||||||||||||||||||||||||||||
| env.GitCommitWithShadowHooks("Add file A", "fileA.go") | ||||||||||||||||||||||||||||||||||||||||
| cpID1 := env.GetCheckpointIDFromCommitMessage(env.GetHeadHash()) | ||||||||||||||||||||||||||||||||||||||||
| require.NotEmpty(t, cpID1, "first commit should have checkpoint trailer") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // === Simulate husky/lefthook overwriting hooks mid-turn === | ||||||||||||||||||||||||||||||||||||||||
| // This is what happens when an agent runs `npm install` and husky's | ||||||||||||||||||||||||||||||||||||||||
| // `prepare` lifecycle script reinstalls its own hooks. | ||||||||||||||||||||||||||||||||||||||||
| for _, hookName := range strategy.ManagedGitHookNames() { | ||||||||||||||||||||||||||||||||||||||||
| hookPath := filepath.Join(hooksDir, hookName) | ||||||||||||||||||||||||||||||||||||||||
| huskyContent := "#!/bin/sh\n# husky - do not edit\n. \"$(dirname \"$0\")/_/husky.sh\"\n" | ||||||||||||||||||||||||||||||||||||||||
| err := os.WriteFile(hookPath, []byte(huskyContent), 0o755) | ||||||||||||||||||||||||||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Verify hooks are overwritten | ||||||||||||||||||||||||||||||||||||||||
| require.False(t, strategy.IsGitHookInstalledInDir(t.Context(), env.RepoDir), | ||||||||||||||||||||||||||||||||||||||||
| "hooks should be detected as overwritten") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Second commit — hooks are gone, use plain go-git commit (no binary invoked). | ||||||||||||||||||||||||||||||||||||||||
| // This simulates what happens in the real world: git runs the husky hook script, | ||||||||||||||||||||||||||||||||||||||||
| // which doesn't call `entire`, so no trailer is added. | ||||||||||||||||||||||||||||||||||||||||
| env.GitAdd("fileB.go") | ||||||||||||||||||||||||||||||||||||||||
| env.GitCommit("Add file B") | ||||||||||||||||||||||||||||||||||||||||
| cpID2 := env.GetCheckpointIDFromCommitMessage(env.GetHeadHash()) | ||||||||||||||||||||||||||||||||||||||||
| assert.Empty(t, cpID2, | ||||||||||||||||||||||||||||||||||||||||
| "second commit should NOT have trailer (hooks were overwritten, entire never called)") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // End prompt 1 | ||||||||||||||||||||||||||||||||||||||||
| err = env.SimulateStop(sess.ID, sess.TranscriptPath) | ||||||||||||||||||||||||||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // === Prompt 2: EnsureSetup should reinstall hooks === | ||||||||||||||||||||||||||||||||||||||||
| sess2 := env.NewSession() | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| env.WriteFile("fileC.go", "package main\n\nfunc C() {}\n") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| sess2.CreateTranscript("Create file C", []FileChange{ | ||||||||||||||||||||||||||||||||||||||||
| {Path: "fileC.go", Content: "package main\n\nfunc C() {}\n"}, | ||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| err = env.SimulateUserPromptSubmitWithPromptAndTranscriptPath( | ||||||||||||||||||||||||||||||||||||||||
| sess2.ID, "Create file C", sess2.TranscriptPath) | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+82
to
+91
|
||||||||||||||||||||||||||||||||||||||||
| sess2 := env.NewSession() | |
| env.WriteFile("fileC.go", "package main\n\nfunc C() {}\n") | |
| sess2.CreateTranscript("Create file C", []FileChange{ | |
| {Path: "fileC.go", Content: "package main\n\nfunc C() {}\n"}, | |
| }) | |
| err = env.SimulateUserPromptSubmitWithPromptAndTranscriptPath( | |
| sess2.ID, "Create file C", sess2.TranscriptPath) | |
| env.WriteFile("fileC.go", "package main\n\nfunc C() {}\n") | |
| sess.CreateTranscript("Create file C", []FileChange{ | |
| {Path: "fileC.go", Content: "package main\n\nfunc C() {}\n"}, | |
| }) | |
| err = env.SimulateUserPromptSubmitWithPromptAndTranscriptPath( | |
| sess.ID, "Create file C", sess.TranscriptPath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says “git runs the husky hook script”, but the code path here (
env.GitCommit) uses go-git and intentionally does not execute any hook scripts. Tweaking the comment to reflect that this is simulating a commit whereentireis not invoked (because hooks are overwritten / bypassed) would make the test’s intent clearer and avoid confusion for future readers.