Skip to content

refactor: propagate ctx into the four public constructors#146

Merged
robbyt merged 2 commits into
mainfrom
claude/review-repo-improvements-NDaxm
May 23, 2026
Merged

refactor: propagate ctx into the four public constructors#146
robbyt merged 2 commits into
mainfrom
claude/review-repo-improvements-NDaxm

Conversation

@robbyt
Copy link
Copy Markdown
Owner

@robbyt robbyt commented May 23, 2026

Summary

Finishes the work started in PR #144. The four public consumer constructors now take ctx as the first argument:

  • polyscript.New[E](ctx, src, opts...)
  • extism.FromExtismLoader(ctx, ldr, opts...)
  • risor.FromRisorLoader(ctx, ldr, opts...)
  • starlark.FromStarlarkLoader(ctx, ldr, opts...)

The three inline context.Background() injections in engines/{extism,risor,starlark}/new.go are gone. Cancellation now actually reaches the loader's I/O and the compiler's parse path.

deprecated.go

The twelve legacy FromXxx* constructors keep their signatures unchanged and inject context.Background() inline at each downstream call to From*Loader. They are slated for removal in #104; churning their signatures during the deprecation window adds caller pain without value.

New tests

  • engines/risor/new_test.go::TestFromRisorLoader_CompileCancelled — pre-cancelled ctx aborts the Risor parser. Asserts errors.Is(err, context.Canceled).
  • engines/extism/new_test.go::TestFromExtismLoader_LoaderCancelled — pre-cancelled ctx aborts the loader's GetReader. (Wazero's compile of the small embedded test module completes too fast to reliably demonstrate parser-level cancellation; the loader-IO cancellation path is the meaningful one for end-users anyway.)
  • polyscript_test.go::TestNew_CompileCancelled — top-level Risor cancellation via polyscript.New[polyscript.Risor].
  • polyscript_test.go::TestNew_HTTPLoaderCancelled — end-to-end demonstration: a stalling httptest.Server, a 50ms ctx deadline, an assertion that the fetch aborts near the deadline (rather than waiting for the 2s server stall) and returns context.DeadlineExceeded. This is the precise scenario [v1][breaking] Propagate ctx into polyscript.New[E] and the three FromXxxLoader constructors #145 set out to fix.

Files

  • Modify: polyscript.go, engines/{extism,risor,starlark}/new.go — add ctx as first arg
  • Modify: deprecated.go — inject context.Background() inline at each call
  • Modify: all 9 examples/*/main.go — thread ctx
  • Modify: ~8 _test.go files — pass t.Context() at call sites
  • Add: 4 new cancellation tests (per-engine + top-level + HTTP end-to-end)
  • Modify: CHANGELOG.md[Unreleased] > Changed

Net diff: 23 files changed, 258 insertions, 120 deletions.

Closes #145.

Test plan

  • go build ./... clean
  • go vet ./... clean
  • go test -race -count=1 ./... full suite green
  • grep -rn 'context\.Background()' --include='*.go' engines/{extism,risor,starlark}/new.go polyscript.go returns zero hits
  • grep -c 'context\.Background()' deprecated.go returns 12 (one per legacy constructor)
  • HTTP loader cancellation: stalling server aborts near the ctx deadline (~50ms) rather than waiting for the full 2s server stall
  • CI: SonarQube, dependency-review, Copilot review

https://claude.ai/code/session_01C61VEAmjxSnX5Xhbab8NvL


Generated by Claude Code

Finishes the work started in #144. The four public consumer constructors
now take ctx as the first argument:

- polyscript.New[E](ctx, src, opts...)
- extism.FromExtismLoader(ctx, ldr, opts...)
- risor.FromRisorLoader(ctx, ldr, opts...)
- starlark.FromStarlarkLoader(ctx, ldr, opts...)

The three inline context.Background() injections in engines/*/new.go go
away; cancellation now actually reaches the loader's I/O and the
compiler's parse path.

deprecated.go keeps its twelve legacy FromXxx* signatures unchanged and
injects context.Background() inline at each downstream call. They are
slated for removal in #104; churning their signatures during the
deprecation window adds caller pain without value.

New cancellation tests:
- engines/risor/new_test.go: TestFromRisorLoader_CompileCancelled —
  pre-cancelled ctx aborts the Risor parser
- engines/extism/new_test.go: TestFromExtismLoader_LoaderCancelled —
  pre-cancelled ctx aborts the loader's GetReader (the wazero compile
  of the small embedded test module completes too fast to demonstrate
  parser-level cancellation)
- polyscript_test.go: TestNew_CompileCancelled (top-level Risor cancel)
  and TestNew_HTTPLoaderCancelled (end-to-end: slow httptest server,
  ctx deadline, verify the fetch aborts near the deadline rather than
  waiting for the full server stall)

Closes #145.

https://claude.ai/code/session_01C61VEAmjxSnX5Xhbab8NvL
Copilot AI review requested due to automatic review settings May 23, 2026 21:55
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 23, 2026

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

CI gci lint flagged formatting on the new ctx-injected From*Loader
calls. gofumpt prefers each arg on its own line when wrapping; the
mechanical sed I used to inject t.Context() left some calls with
two args on the wrap line.

https://claude.ai/code/session_01C61VEAmjxSnX5Xhbab8NvL
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR completes the context-threading refactor by adding context.Context as the first parameter to the four public consumer constructors so cancellation can reach loader I/O (e.g., HTTP fetch) and engine compile/parse paths.

Changes:

  • Add ctx context.Context as the first argument to polyscript.New[E] and the three engine-level From*Loader constructors.
  • Remove inline context.Background() injection from engine constructors; keep deprecated constructors unchanged by injecting context.Background() at call sites.
  • Add/adjust tests and examples to pass t.Context() / context.Background() and add cancellation-focused test coverage.

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
readme_test.go Updates README-backed tests to pass t.Context() into polyscript.New.
polyscript.go Changes New[E] signature to accept ctx and threads it into engine construction.
polyscript_test.go Updates call sites and adds end-to-end cancellation tests (compile + HTTP loader).
polyscript_options_test.go Updates option/constructor tests to pass t.Context().
polyscript_extism_test.go Updates Extism construction tests to pass t.Context().
examples/simple/{risor,starlark,extism}/main.go Threads a context into polyscript.New in examples.
examples/multiple-instantiation/{risor,starlark,extism}/main.go Threads a context into polyscript.New in examples.
examples/data-prep/{risor,starlark,extism}/main.go Threads a context into polyscript.New in examples.
engines/starlark/new.go Adds ctx to FromStarlarkLoader and forwards it into script.NewExecutableUnit.
engines/starlark/new_test.go Updates Starlark engine constructor tests to pass t.Context().
engines/risor/new.go Adds ctx to FromRisorLoader and forwards it into script.NewExecutableUnit.
engines/risor/new_test.go Updates Risor engine constructor tests and adds cancellation test.
engines/extism/new.go Adds ctx to FromExtismLoader and forwards it into script.NewExecutableUnit.
engines/extism/new_test.go Updates Extism engine constructor tests and adds loader-cancellation test.
engines/integration_test.go Updates integration tests to pass t.Context() into engine constructors.
deprecated.go Keeps deprecated constructors’ signatures and injects context.Background() at downstream calls.
CHANGELOG.md Documents the breaking change and the new cancellation behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread polyscript.go
Comment thread engines/extism/new.go
Comment thread engines/risor/new.go
Comment thread engines/starlark/new.go
@robbyt robbyt merged commit 5c7d9d9 into main May 23, 2026
3 checks passed
@robbyt robbyt deleted the claude/review-repo-improvements-NDaxm branch May 23, 2026 22:40
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.

[v1][breaking] Propagate ctx into polyscript.New[E] and the three FromXxxLoader constructors

3 participants