Skip to content

[codex] Add exit-aware lexical cleanup#240

Open
briancavalier wants to merge 6 commits into
mainfrom
codex/exit-aware-lexical-finalizers
Open

[codex] Add exit-aware lexical cleanup#240
briancavalier wants to merge 6 commits into
mainfrom
codex/exit-aware-lexical-finalizers

Conversation

@briancavalier

@briancavalier briancavalier commented Jun 19, 2026

Copy link
Copy Markdown
Owner

Summary

  • Extend bracket and finalizing so cleanup can inspect lexical region exits.
  • Add a public RegionExit type for scope-less lexical exits, while preserving existing scoped Exit for Finally-related APIs.
  • Preserve the existing one-argument bracket release and finalizing(Fx) code paths for compatibility.

Notes

This is a prototype PR. Scope-owned finalizers still receive Exit, where scoped control exits have a resolved concrete scope. Exit-aware lexical cleanup now receives RegionExit, so pre-boundary observations of returnFrom, abort, and interruptFrom do not expose raw or unresolved scope tokens.

For external task interruption before a typed exit is available, lexical cleanup receives { type: 'interrupted' } or { type: 'interrupted', reason }.

Callback cleanup exits are captured and combined with the primary lexical exit before resuming, matching the existing finalizing(Fx) cleanup-exit behavior. That preserves primary failures across cleanup returnFrom/abort/interruptFrom exits and transactional rollback decisions.

Validation

  • corepack pnpm build
  • corepack pnpm typecheck
  • corepack pnpm lint
  • corepack pnpm test

@briancavalier briancavalier marked this pull request as ready for review June 19, 2026 13:21

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f5b02ce85c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/Fx.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c5304405b0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/Fx.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 88628654c2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/Fx.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e88c7fdef2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/Fx.ts
if (exit === undefined) return undefined as A
released = true
if (isSuccessExit(exit)) {
yield* andFinally(r, toRegionExit(exit))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Resume successful bracket cleanup exits outside the mask

When the bracket body succeeds but this release callback exits via a scoped control effect such as returnFrom or abort to an enclosing withScope, the effect is yielded while the uninterruptibleMask token is still active. ScopeBoundary consumes that control effect without resuming this iterator, so the mask-end in the outer finally never runs and run throws Interrupt mask invariant failed instead of returning the cleanup exit. The failure path avoids this by capturing cleanup with returnExit and resuming it through restore(...); the successful-body path needs equivalent handling for cleanup exits.

Useful? React with 👍 / 👎.

Comment thread src/Fx.ts
if (exit === undefined) return undefined as A
finalized = true
if (isSuccessExit(exit)) {
yield* finally_(toRegionExit(exit))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Resume successful finalizer exits outside the mask

When the protected program succeeds and an exit-aware cleanup returns a scoped control exit, e.g. ok('body').pipe(finalizing(() => returnFrom(S, 'cleanup')), withScope(S), run), this line yields that effect while the surrounding uninterruptibleMask is still active. The matching scope handler consumes the control effect without resuming this iterator, so the mask cleanup is skipped and run fails with Interrupt mask invariant failed; the non-success branch already avoids this by capturing the cleanup exit and re-emitting it through restore(...).

Useful? React with 👍 / 👎.

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