Skip to content

Fix type inference for ActionState - #4220

Open
mbg wants to merge 1 commit into
mainfrom
mbg/fix-action-state-type-inference
Open

mbg wants to merge 1 commit into
mainfrom
mbg/fix-action-state-type-inference

Conversation

@mbg

@mbg mbg commented Oct 9, 2026

Copy link
Copy Markdown
Member

While working on something else, I noticed that type inference for ActionState was broken in some cases. The problem is that, when given a chance, TypeScript will simplify an ActionState<Fs> type as much as possible, which results in a concrete object type that doesn't mention Fs. That causes problems for functions which accept ActionState<Fs> values, but need to know the feature keys Fs such as the callee function from testing-utils.ts.

Assuming that we have a function test(action: ActionState<["Logger", "Env"]): boolean, then before this change the result of callee(test) would be a value of type CallableEnvBuilder<[], boolean, readonly (keyof FeatureState)[]>. That's no good since readonly (keyof FeatureState)[] satisfies any StateFeature[] constraint and doesn't tell us which features the function actually needs.

The fix here is a bit complex, but fundamentally focuses around adding a property to the result of ActionState that tracks Fs. That's straight-forward, but we also need to ensure that ActionState values which provide a superset of the required properties can be passed to functions which don't require all of it. That is accomplished by making the extra property a function and tracking the features Fs in its parameter to exploit the contravariance of function parameters.

Finally, we also need to account for Env implying ReadOnlyEnv. That's addressed by expanding "Env" to "Env" | "ReadOnlyEnv" in the tracked features.

Extending ActionState objects is also tricky because in e.g. the existing { ...action, features } constructions, features: FeatureEnablement and so the "Features" key is not mentioned. The extendActionState function works around that by using a reverse lookup (implemented in FeatureNameFor) to lookup the feature key for a given object.

Risk assessment

For internal use only. Please select the risk level of this change:

  • Low risk: Changes are fully under feature flags, or have been fully tested and validated in pre-production environments and are highly observable, or are documentation or test only.

Which use cases does this change impact?

Environments:

  • Testing/None - This change does not impact any CodeQL workflows in production.

How did/will you validate this change?

  • Unit tests - I am depending on unit test coverage (i.e. tests in .test.ts files).
  • End-to-end tests - I am depending on PR checks (i.e. tests in pr-checks).

If something goes wrong after this change is released, what are the mitigation and rollback strategies?

  • Development/testing only - This change cannot cause any failures in production.

How will you know if something goes wrong after this change is released?

  • Other - Please provide details.

Are there any special considerations for merging or releasing this change?

  • No special considerations - This change can be merged at any time.

Merge / deployment checklist

  • Confirm this change is backwards compatible with existing workflows.
  • Consider adding a changelog entry for this change.
  • Confirm the readme and docs have been updated if necessary.

@mbg
mbg requested a review from a team as a code owner October 9, 2026 13:29
Copilot AI balanced review requested due to automatic review settings October 9, 2026 13:29
@github-actions github-actions Bot added the size/S Should be easy to review label Oct 9, 2026

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.

Warning

  • Copilot's review of this pull request may be incomplete because some of the changed files are excluded by your Copilot content exclusion settings. See Excluding content from Copilot for details.

🟡 Changes recommended

The helper’s documented generic escape hatch is unusable, and the compile-time behavior lacks regression coverage.

2 open findings
What changed in this PR

This PR improves ActionState feature inference for testing utilities and state extension.

Changes:

  • Adds phantom feature tracking with Env implication handling.
  • Introduces extendActionState and updates three action entry points.
  • Updates the generated bundle.
File Description
src/​action-common.ts Adds feature tracking and state extension types.
src/​init-action.ts Uses the state extension helper.
src/​setup-codeql-action.ts Uses the state extension helper.
src/​start-proxy-action.ts Uses the state extension helper.
lib/​entry-points.js Generated bundle; excluded from review by policy.
Files excluded by content exclusion policy (1)
  • lib/entry-points.js

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/action-common.ts
Comment thread src/action-common.ts
Comment on lines +117 to +122
* `Fs[number]` returns the union of all features in `Fs`. We wrap it in `ImpliedFeatures`
* so that `Env` is expanded into `Env | ReadOnlyEnv`, allowing functions that expect the
* `ReadOnlyEnv` feature to be provided with an `ActionState` that has the `Env` feature
* without requiring this to be made explicit.
*/
readonly [stateFeatures]?: (ts: ImpliedFeatures<Fs[number]>) => void;

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Should be easy to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants