Skip to content

[LSP] Document explicit compiler pool scopes - #678

Merged
lagergren merged 3 commits into
masterfrom
errs/i2-ambient-pools-20261008
Oct 9, 2026
Merged

lagergren merged 3 commits into
masterfrom
errs/i2-ambient-pools-20261008

Conversation

@lagergren

@lagergren lagergren commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Why?

Compiler hosts need the existing working-pool scope contract documented at the API boundary. A constant's owning pool is not necessarily the working pool, and dispatched work must establish its own context.

How?

Document the existing getCurrentPool() and withPool(pool) behavior: nullable current context, operation-wide try-with-resources scopes, same-thread restoration, and explicit scopes on worker threads.

Implications

This is documentation-only: no production behavior or API changes, and no new tests. It is not a production prerequisite for the compiler adapter.

Changes

  • javatools/src/main/java/org/xvm/asm/ConstantPool.java: Javadoc only, +11/-1 lines.

Following Gene's review, both added Java scope-test classes have been removed in 6d3395101. The same removal is mirrored on the lsp mono-code branch; its tests of actual adapter compilation, partial analysis, and caller-pool restoration remain with that consumer.

Validation

./gradlew spotlessCheck --console=plain --quiet passed on the documentation-only revision. The final diff was inspected to confirm that it contains only Javadoc; no new functional test run is claimed for this revision.

The earlier fallback-free downstream acceptance remains evidence for the adapter's compatibility: 1,785 adapter cases and all 82 packaged compiler-server protocol cases passed, with two existing disabled adapter tests. The six actual adapter boundary cases also passed. Those results and exact revisions are recorded in the earlier review response; they are not additional files or behavior in this PR.

@lagergren

Copy link
Copy Markdown
Contributor Author

Design rationale: retain compiler context, support ordinary host calls

ConstantPool.getCurrentPool() reads thread-local context. Compiler and runtime scopes normally bind that context, but a debugger watch, a Java embedding host, or a focused test can legitimately inspect an existing object on an unbound thread. One observed failure was in the inspection path itself: MethodBody.isOp() and toString() could dereference a null ambient pool while trying to describe a method.

Why the ambient pool must still win

An object's owner and the compiler's current working pool are not interchangeable. Cross-pool operations deliberately bind context so that well-known constants and type operations resolve in that context. Replacing every read with getConstantPool() would remove the null dereference but could return the wrong answer during compilation.

The rule is therefore currentOr(owner): return the bound pool when present, otherwise the owner supplied by the caller. Constant.poolInUse() provides that rule for constants; methods use the pool of their identity, properties use their property identity, and the interpreter connector uses its container. The helper performs a lookup only: it never pushes context, changes the thread-local, extends a scope, or manufactures a global default. If a caller supplies a null fallback on an unbound thread, currentOr still returns null.

Boundaries reviewers should preserve

This is a targeted audit of readers that have a meaningful owner. It is not a blanket replacement of every ambient-pool access. Existing withPool scopes and their restoration behavior remain intact. It also does not make shared compiler/container instances safe for arbitrary concurrent use; the fallback solves missing context for these calls, not ownership or synchronization throughout the compiler.

There is no dependency on I1 or the later listener/embedding API changes. The independent regression uses the current getNormalizedType() API on master. No compatibility wrapper for the older canonical-type method is introduced.

Validation performed

Eleven tests executed with zero failures, errors, or skips: three ConstantPoolAmbientTest cases, two MethodBodyAmbientPoolTest cases, one ConstantPoolDiagnosticsTest case, and five FloatingConstantPoolTest cases. The same invocation built and installed all 24 XDK modules and passed spotlessCheck.

./gradlew :javatools:test \
  --tests org.xvm.asm.ConstantPoolAmbientTest \
  --tests org.xvm.asm.constants.MethodBodyAmbientPoolTest \
  --tests org.xvm.asm.ConstantPoolDiagnosticsTest \
  --tests org.xvm.asm.FloatingConstantPoolTest \
  spotlessCheck :xdk:installDist \
  --rerun-tasks --no-build-cache --console=plain --quiet

The focused tests distinguish absent context from an intentionally bound alternate pool. The full module build checks that normal compilation still completes. A normalized binary-output comparison was not repeated on this master base, so the acceptance claim is successful tests and compilation, not proven byte-for-byte equivalence.

Review and validation boundary

Tested commit: 6c6720fa4. Independent base: f442aced6. The recorded validation ran locally on October 8, 2026. Master has since advanced with the Gradle 9.8.1 upgrade (#676); that commit does not overlap these 12 changed files. The validation above describes the pinned branch, not the newer merge result, which still needs current CI.

This is I2 in the foundation series. I1 addresses diagnostic identity; I2 addresses missing ambient constant-pool context. Both target master independently, and neither requires the other. I3 remains unpublished.

@lagergren

Copy link
Copy Markdown
Contributor Author

I should add that I have several valid reproducers in the LSP that proves that this is a problem, and I believe this is the architecturally correct way to fix it.

I have also run extensive testing on all existing scenarior to make sure they are still sound and working.

@lagergren

Copy link
Copy Markdown
Contributor Author

As usual, this can also be backed out as a single self contained commit if something about this doesn't work quite right. When I have the compiler LSP server in you can satisfy that the ownership relations are corrected by this PR by looking at before and after logs. I am very sure I haven't compromised anything else.

@lagergren
lagergren force-pushed the errs/i2-ambient-pools-20261008 branch from 6c6720f to dd136d3 Compare October 8, 2026 20:15
@lagergren

Copy link
Copy Markdown
Contributor Author

(And this has to do with the slighty more esoteric modes of compilation that we need to use in the LSP - but rest assure that nothing else gets the wrong pool, defaults to a pool it didn't default o before or has any changed lookup semantics. I have even run fuzzing on this to make sure that the existing functionality is 1-1 preserved)

@ggleyzer

ggleyzer commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

I'm not sure what the failure scenario is, but I believe this is an incorrect way to go.

Our design assumption is that any entrance into the compiler world needs to start with this preamble:

        try (var _ = ConstantPool.withPool(pool)) {
            // whatever you do here will always have ConstantPool.getCurrentPool();
        }

My only explanation is that the offending code is not following this.

@ggleyzer ggleyzer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Our design assumption is that any entrance into the compiler world needs to start with this preamble:

    try (var _ = ConstantPool.withPool(pool)) {
        // whatever you do here will always have ConstantPool.getCurrentPool();
    }

My only explanation is that the offending code is not following this.

@ggleyzer

ggleyzer commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

BTW, some of the places you changed are probably old not quite correct code; I will verify/fix

@lagergren lagergren changed the title [LSP-PREREQUISITE] Use owning constant pools when no ambient pool is bound [LSP] Document and test explicit compiler pool scopes Oct 8, 2026
@lagergren

Copy link
Copy Markdown
Contributor Author

Gene, you are right about the boundary contract. I have withdrawn the owner-fallback design rather than asking you to accept it as an LSP prerequisite.

The historical reproducer was an inspection/assertion-formatting operation outside a pool scope. The broader replacement of eighteen reads was preventative, not eighteen separately demonstrated compiler-adapter defects. Preserving an already bound pool made that proposal compatible with the exercised compilation paths, but did not establish that its fallback behavior was necessary. My earlier rationale overstated that conclusion.

currentOr(owner) also only chooses the result of one lookup; it does not establish a working-pool scope for nested operations. Your operation-wide try (var _ = ConstantPool.withPool(pool)) contract is the appropriate boundary. The working pool may intentionally differ from a constant's owner, and scopes must be established on the executing thread and restore the caller's context.

The correction commit removes currentOr, poolInUse and every changed reader, including the connector change. The final PR diff is three files: Javadoc for the existing scope API and two regression classes. There is no production-behavior change or added API relative to the base. The tests cover unbound entry/exit, nested restoration after an exception, worker-thread isolation/no leak to a later task, a real cross-pool range operation, and scoped MethodBody inspection.

I checked the actual downstream consumer before making this change. The current compiler adapter already wraps semantic extraction, dependency capture and partial semantic inspection in explicit pool scopes, then exposes detached facts to request threads. In an isolated checkout of the full working branch I removed all of I2's production fallbacks and ran the entire adapter-package suite: 1,785 executed tests passed, zero failures/errors; two existing Tree-sitter navigation placeholders were disabled. This includes the compiler navigation/refactoring/partial-analysis suites, concurrent detached-snapshot queries and the 120-cycle retention workload. I also inspected the packaged server bytecode to verify that neither fallback helper was present.

Six additional downstream cases passed for successful compilation plus snapshot/artifact extraction, failed compilation, and partial member inspection, each from an unbound caller and an unrelated bound caller. They check semantic results and restoration of the caller pool. The six revised Java scope tests also passed without skips. No adapter production workaround or change was needed for these results.

Your follow-up about auditing older call sites remains separate: this PR now leaves those implementations exactly as on the base. Any actual missing scope found there should be fixed at its operation boundary with its own reproducer.

The packaged compiler-server protocol suite then passed all 82 cases, with zero failures/errors/skips. Root and LSP formatting checks passed. The fallback-free acceptance used the working LSP tree at 18ee5f69e plus the now-committed local integration correction 2fada9216; the independent PR correction is a6850a9c8. The latter leaves only the three documentation/test files in the final PR diff.

Reproduction commands for the full downstream checkout:

./gradlew :lang:lsp-server:test --tests 'org.xvm.lsp.adapter.*' \
  --rerun-tasks --no-build-cache \
  -PincludeBuildLang=true -PincludeBuildAttachLang=true --console=plain --quiet

./gradlew :lang:lsp-server:test --tests org.xvm.lsp.adapter.CompilerPoolScopeTest \
  :lang:lsp-server:compilerStdioTest \
  :javatools:test --tests org.xvm.asm.ConstantPoolAmbientTest \
  --tests org.xvm.asm.constants.MethodBodyAmbientPoolTest \
  spotlessCheck :lang:lsp-server:spotlessCheck \
  --rerun-tasks --no-build-cache \
  -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler \
  --console=plain --quiet

I have updated the PR title and description to supersede the original fallback rationale. Please re-review the reduced scope; I am leaving your changes-requested review in place.

@lagergren
lagergren requested a review from ggleyzer October 8, 2026 20:54

@ggleyzer ggleyzer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The new doc is good; the tests have no value IMHO

@lagergren lagergren changed the title [LSP] Document and test explicit compiler pool scopes [LSP] Document explicit compiler pool scopes Oct 9, 2026
@lagergren
lagergren requested a review from ggleyzer October 9, 2026 09:55
Preserve the bound compiler pool when present and use each constant's owning pool on unbound host threads. Adapt the independent regression to the current normalized-type API.

Re-extracted on master f442ace. Eleven focused tests pass without skips, all 24 XDK modules build and install, and SpotlessCheck passes.
Withdraw the currentOr and poolInUse changes in response to review. Document the existing host boundary contract and exercise scope restoration, executor isolation, cross-pool operations, and scoped method inspection without changing production behavior.
@lagergren
lagergren force-pushed the errs/i2-ambient-pools-20261008 branch from 6d33951 to d8652a5 Compare October 9, 2026 12:31
@lagergren
lagergren merged commit f6948b2 into master Oct 9, 2026
2 checks passed
@lagergren
lagergren deleted the errs/i2-ambient-pools-20261008 branch October 9, 2026 12:32
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.

2 participants