Skip to content

feat(interp): support context-aware function bridges#138

Merged
siyul-park merged 3 commits into
mainfrom
codex/context-aware-unmarshal
Jul 12, 2026
Merged

feat(interp): support context-aware function bridges#138
siyul-park merged 3 commits into
mainfrom
codex/context-aware-unmarshal

Conversation

@siyul-park

@siyul-park siyul-park commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Changes Made

  • Treat an exact leading context.Context as host-only in marshaled Go functions and exported host-object methods.
  • Pass caller contexts through unmarshaled VM function, closure, and live-function-reference wrappers.
  • Preserve background fallback, non-leading context conversion, error behavior, and callable ownership.
  • Document symmetric context behavior.

Related Issues

  • None.

Additional Information

Validated with:

  • go vet ./interp
  • go test -count=1 -race -run '^TestInterpreter_(Marshal|Unmarshal)$' ./interp\n- go test -count=1 -race ./interp\n- go test -count=1 -race ./...

Summary by CodeRabbit

  • New Features
    • Go functions exposed to the VM can receive the active execution context.
    • Cancellation and deadlines are now honored for VM-to-Go calls.
    • If no context is provided, calls default to context.Background().
    • Only a first context.Context parameter is treated specially; other positions are passed through normally.
  • Documentation
    • Clarified host integration rules for the special first-parameter context.Context behavior.
  • Tests
    • Expanded marshal/unmarshal coverage to verify context identity propagation and defaulting behavior.

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c9560bfb-5d4b-4c58-8aaf-5db1f828db28

📥 Commits

Reviewing files that changed from the base of the PR and between 4f65a30 and fabf106.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • go.mod
  • internal/cmd/geninterp/generate.go
  • interp/threaded.go

📝 Walkthrough

Walkthrough

The function bridge treats an exact first context.Context parameter as host-only, propagates active contexts into interpreter execution, defaults nil contexts to context.Background(), and tests cancellation, identity, closures, host methods, and non-first context parameters. Generated interpreter output now uses golang.org/x/tools/imports.

Changes

Context-aware function marshaling

Layer / File(s) Summary
Context parameter contract
interp/marshal.go, docs/host-integration.md
Function compilation omits an exact first context.Context from VM signatures, while other context positions remain VM arguments; documentation describes the behavior.
Marshaled host invocation
interp/interp.go, interp/marshal.go, interp/interp_test.go
Marshaled functions and host-object methods receive the active interpreter context, use context.Background() for nil values, and pass the context into interpreter execution.
Unmarshaled VM invocation
interp/marshal.go, interp/interp_test.go
Unmarshaled functions and closures extract optional contexts, offset argument conversion, preserve context identity, and propagate cancellation.
Generated interpreter output
internal/cmd/geninterp/generate.go, interp/threaded.go, go.mod
Generated source registers instr and types imports explicitly and is processed with golang.org/x/tools/imports; related module requirements and import ordering are updated.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant GoCaller
  participant GoWrapper
  participant Interpreter
  participant VMFunction
  GoCaller->>GoWrapper: call with optional context
  GoWrapper->>Interpreter: invoke with context or context.Background()
  Interpreter->>VMFunction: execute VM function
  VMFunction-->>Interpreter: return result or cancellation
  Interpreter-->>GoWrapper: return execution result
  GoWrapper-->>GoCaller: return Go value or error
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding context-aware function bridges in the interpreter.
Description check ✅ Passed The description follows the required template and covers changes made, related issues, and additional validation details.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/context-aware-unmarshal
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch codex/context-aware-unmarshal

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.44.1)
interp/threaded.go

ast-grep timed out on this file


Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@interp/interp_test.go`:
- Around line 2448-2467: Check and assert the error returned by setup.Close() in
the “function receives active context” test, and apply the same handling to the
other setup.Close() call at the referenced location. Use the test’s existing
require-based error assertion style.
- Around line 2588-2609: Update the context.WithValue call in the “VM function
context identity” test to use the file’s existing contextKey type instead of an
anonymous struct{} key, preserving the current context value and identity
assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 908096b4-780b-42a7-856e-526ab8e705ca

📥 Commits

Reviewing files that changed from the base of the PR and between 7564898 and bb42e3f.

📒 Files selected for processing (4)
  • docs/host-integration.md
  • interp/interp.go
  • interp/interp_test.go
  • interp/marshal.go

Comment thread interp/interp_test.go
Comment thread interp/interp_test.go
@codecov

codecov Bot commented Jul 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.37500% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 27.61%. Comparing base (b97a4fc) to head (fabf106).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
interp/marshal.go 81.48% 2 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #138       +/-   ##
===========================================
- Coverage   59.39%   27.61%   -31.79%     
===========================================
  Files          82       82               
  Lines       54215    59959     +5744     
===========================================
- Hits        32200    16555    -15645     
- Misses      13029    42252    +29223     
+ Partials     8986     1152     -7834     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@siyul-park
siyul-park merged commit b87e746 into main Jul 12, 2026
6 of 7 checks passed
@siyul-park
siyul-park deleted the codex/context-aware-unmarshal branch July 12, 2026 04:05
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.

1 participant