-
Notifications
You must be signed in to change notification settings - Fork 445
Expose memory stores to on.steps in pre-activation for deterministic dispatch gating
#44015
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 2 commits
1812bc3
ee0df32
ab61304
315c559
d6faece
30c3789
85aabb5
b8514e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ func (c *Compiler) buildPreActivationJob(data *WorkflowData, needsPermissionChec | |
| steps = c.buildPreActivationSkipIfQuerySteps(data, steps, skipIfToken) | ||
| steps = c.buildPreActivationSkipIfCheckFailingStep(data, steps) | ||
| steps = c.buildPreActivationRolesBotsCmdSteps(data, steps) | ||
| steps = c.buildPreActivationMemoryRestoreSteps(data, steps) | ||
| steps, onStepIDs, err := c.injectPreActivationOnSteps(data, steps, customSteps) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
@@ -230,6 +231,37 @@ func (c *Compiler) buildPreActivationRolesBotsCmdSteps(data *WorkflowData, steps | |
| return steps | ||
| } | ||
|
|
||
| // buildPreActivationMemoryRestoreSteps restores memory stores before on.steps run in pre-activation. | ||
| // This is a read-only surface: it restores/loads memory data but does not emit write-back or commit steps. | ||
| func (c *Compiler) buildPreActivationMemoryRestoreSteps(data *WorkflowData, steps []string) []string { | ||
| if len(data.OnSteps) == 0 { | ||
| return steps | ||
| } | ||
|
|
||
| var memorySteps strings.Builder | ||
|
|
||
| generateCacheMemorySteps(&memorySteps, data) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug:
This can corrupt the cache for the actual agent job (a pre-activation run that fails mid-step still saves a partial/stale cache) and wastes cache quota. Suggested fix: Set @copilot please address this. |
||
| generateRepoMemorySteps(&memorySteps, data) | ||
|
|
||
|
|
||
| if data.SafeOutputs != nil && data.SafeOutputs.CommentMemory != nil { | ||
| memorySteps.WriteString(" - name: Prepare comment memory files\n") | ||
| fmt.Fprintf(&memorySteps, " uses: %s\n", getCachedActionPin("actions/github-script", data)) | ||
| memorySteps.WriteString(" with:\n") | ||
| fmt.Fprintf(&memorySteps, " github-token: %s\n", getEffectiveSafeOutputGitHubToken(data.SafeOutputs.CommentMemory.GitHubToken)) | ||
| memorySteps.WriteString(" script: |\n") | ||
| memorySteps.WriteString(" const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs');\n") | ||
| memorySteps.WriteString(" setupGlobals(core, github, context, exec, io, getOctokit);\n") | ||
| memorySteps.WriteString(" const { main } = require('${{ runner.temp }}/gh-aw/actions/setup_comment_memory_files.cjs');\n") | ||
| memorySteps.WriteString(" await main();\n") | ||
| } | ||
|
|
||
| if memorySteps.Len() > 0 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/codebase-design] All three memory types are accumulated into a single π‘ SuggestionConsider appending each memory type as a separate @copilot please address this. |
||
| steps = append(steps, memorySteps.String()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maintainability: entire multi-step content appended as one slice element, inconsistent with the rest of the codebase. All other step-building helpers in this file (e.g., Suggested fix: Follow the existing pattern β call helpers directly on the @copilot please address this. |
||
| } | ||
|
|
||
| return steps | ||
| } | ||
|
|
||
| func (c *Compiler) appendPreActivationSkipRolesStep(data *WorkflowData, steps []string) []string { | ||
| steps = append(steps, " - name: Check skip-roles\n") | ||
| steps = append(steps, fmt.Sprintf(" id: %s\n", constants.CheckSkipRolesStepID)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -225,6 +225,85 @@ Test on.steps are appended after built-in checks | |
| t.Error("Expected membership check step to appear before on.steps gate step in pre_activation job") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("on_steps_with_memory_stores_restored_in_pre_activation", func(t *testing.T) { | ||
| workflowContent := `--- | ||
| on: | ||
| workflow_dispatch: null | ||
| roles: all | ||
| steps: | ||
| - name: Gate check | ||
| id: gate | ||
| run: echo "checking..." | ||
| tools: | ||
| cache-memory: true | ||
| repo-memory: true | ||
| comment-memory: true | ||
| engine: copilot | ||
| --- | ||
|
|
||
| Test on.steps with memory stores restored in pre-activation | ||
| ` | ||
| workflowFile := filepath.Join(tmpDir, "test-on-steps-memory-pre-activation.md") | ||
| if err := os.WriteFile(workflowFile, []byte(workflowContent), 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| err := compiler.CompileWorkflow(workflowFile) | ||
| if err != nil { | ||
| t.Fatalf("CompileWorkflow() returned error: %v", err) | ||
| } | ||
|
|
||
| lockFile := stringutil.MarkdownToLockFile(workflowFile) | ||
| lockContent, err := os.ReadFile(lockFile) | ||
| if err != nil { | ||
| t.Fatalf("Failed to read lock file: %v", err) | ||
| } | ||
| lockContentStr := string(lockContent) | ||
| var preActivationSectionBuilder strings.Builder | ||
| inPreActivation := false | ||
| for line := range strings.SplitSeq(lockContentStr, "\n") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The π‘ Consider a more robust extractorUse a known sibling job name to bound the search, or parse the full YAML with @copilot please address this. |
||
| if strings.HasPrefix(line, " pre_activation:") { | ||
| inPreActivation = true | ||
| } | ||
| if inPreActivation { | ||
| // Next top-level job (2 spaces, not deeper indentation) ends pre_activation section. | ||
| if strings.HasPrefix(line, " ") && | ||
| !strings.HasPrefix(line, " ") && | ||
| strings.HasSuffix(line, ":") && | ||
| !strings.HasPrefix(line, " pre_activation:") { | ||
| break | ||
| } | ||
| preActivationSectionBuilder.WriteString(line) | ||
| preActivationSectionBuilder.WriteByte('\n') | ||
| } | ||
| } | ||
| preActivationSection := preActivationSectionBuilder.String() | ||
| if preActivationSection == "" { | ||
| t.Fatalf("Expected pre_activation section in lock file. Lock file:\n%s", lockContentStr) | ||
| } | ||
|
|
||
| // Memory stores should be restored before on.steps run in pre-activation. | ||
| assert.Contains(t, preActivationSection, "Restore cache-memory file share data") | ||
| assert.Contains(t, preActivationSection, "Clone repo-memory branch (default)") | ||
| assert.Contains(t, preActivationSection, "Prepare comment memory files") | ||
| assert.Contains(t, preActivationSection, " id: gate") | ||
|
Comment on lines
+289
to
+293
|
||
|
|
||
| cacheRestoreIdx := strings.Index(preActivationSection, "Restore cache-memory file share data") | ||
| repoRestoreIdx := strings.Index(preActivationSection, "Clone repo-memory branch (default)") | ||
| commentMemoryIdx := strings.Index(preActivationSection, "Prepare comment memory files") | ||
| gateStepIdx := strings.Index(preActivationSection, " id: gate") | ||
| if cacheRestoreIdx == -1 || repoRestoreIdx == -1 || commentMemoryIdx == -1 || gateStepIdx == -1 { | ||
| t.Fatalf("Expected memory restore steps and gate step in pre_activation section. Section:\n%s", preActivationSection) | ||
| } | ||
| assert.Less(t, cacheRestoreIdx, gateStepIdx, "cache-memory should be restored before on.steps") | ||
| assert.Less(t, repoRestoreIdx, gateStepIdx, "repo-memory should be restored before on.steps") | ||
| assert.Less(t, commentMemoryIdx, gateStepIdx, "comment-memory should be restored before on.steps") | ||
|
|
||
| // Pre-activation must not include write-back/commit steps. | ||
| assert.NotContains(t, preActivationSection, "Commit cache-memory changes") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The write-back absence checks ( π‘ Add the missing assertion// Verify cache action is restore-only (not auto-saving)
assert.NotContains(t, preActivationSection, "actions/cache@",
"pre_activation should use actions/cache/restore, not actions/cache")
assert.Contains(t, preActivationSection, "actions/cache/restore@",
"pre_activation cache step should be restore-only")This directly tests the read-only property the PR description promises. @copilot please address this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test coverage: no case exercises the The test asserts that write-back steps ( Suggested addition: add an assertion like: assert.Contains(t, preActivationSection, "actions/cache/restore")
assert.NotContains(t, preActivationSection, "uses: actions/cache@")(or the pinned variants returned by @copilot please address this. |
||
| assert.NotContains(t, preActivationSection, "Push repo-memory changes") | ||
| }) | ||
| } | ||
|
|
||
| // TestExtractOnSteps tests the extractOnSteps function directly | ||
|
|
||
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.
[/codebase-design]
generateCacheMemoryStepsis not purely read-only β whencache.RestoreOnlyis false and threat detection is disabled, it emitsactions/cache(notactions/cache/restore), which auto-saves via its post-action. This violates the stated read-only contract for pre-activation.π‘ Suggested fix
Force restore-only when calling from the pre-activation context. Options:
generateCacheMemoryRestoreOnlyStepsthat always usesactions/cache/restore.RestoreOnly: truebefore delegating togenerateCacheMemorySteps.The test should also assert that
actions/cache/restore(notactions/cache) appears in the pre_activation section to catch a regression here.@copilot please address this.