Skip to content

feat: reuse empty artifact chat sessions - #742

Merged
ilyabo merged 2 commits into
mainfrom
codex/artifact-chat-session-reuse
Jun 21, 2026
Merged

feat: reuse empty artifact chat sessions#742
ilyabo merged 2 commits into
mainfrom
codex/artifact-chat-session-reuse

Conversation

@ilyabo

@ilyabo ilyabo commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Create an artifact-scoped empty chat automatically when CLI artifact creation commands create/select a new artifact.
  • Reuse the most recently opened empty, non-running chat already owned by the current artifact before creating another blank session.
  • Document and test the new reusable artifact AI session helper.

Why

New artifacts should be immediately ready for artifact-scoped chat, but repeated chat creation should not accumulate duplicate blank sessions. lastOpenedAt remains the recency signal; createdAt is left intact.

Validation

  • pnpm --filter @sqlrooms/artifacts test -- artifactAiSessions
  • pnpm --filter @sqlrooms/artifacts typecheck
  • pnpm --filter sqlrooms-cli-app typecheck
  • pnpm build
  • pre-push hook: knip, turbo typecheck, turbo test

Summary by CodeRabbit

  • New Features
    • Artifact creation now reuses the latest eligible “empty” artifact-scoped AI session (when it’s not running and has no draft text/messages), reducing unnecessary new sessions.
    • Initial artifact chat setup is now surface-aware, avoiding it on AI/MCP-related surfaces.
  • Documentation
    • Updated Artifact-Owned AI Sessions guidance, including how reusable empty sessions are identified and when new sessions are always created.
  • Tests
    • Added coverage for empty-session reuse, exclusion of specific sessions, and correct behavior when session content is missing or explicit options are provided.

@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@ilyabo, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 29 minutes and 59 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 644cd3af-86e8-4cc9-9978-e6b753bb3408

📥 Commits

Reviewing files that changed from the base of the PR and between c2e139b and 83687e2.

📒 Files selected for processing (1)
  • packages/artifacts/src/ai/artifactAiSessionHelpers.ts
📝 Walkthrough

Walkthrough

Adds getEmptyAiSessionIdForArtifact helper to find the most recently opened non-running empty session for an artifact. createArtifactScopedSession is updated to reuse such sessions when no explicit options are given. Dashboard artifact-creation commands now conditionally invoke createArtifactScopedSession based on invocation surface type, skipping session creation when invoked from 'ai' or 'mcp' surfaces.

Changes

Empty AI Session Reuse with Surface-Conditional Creation

Layer / File(s) Summary
Empty session helper types and implementation
packages/artifacts/src/ai/artifactAiSessionHelpers.ts, packages/artifacts/src/ai/index.ts
ArtifactAiSession gains optional prompt and uiMessages fields to assess emptiness; ArtifactAiSessionsForArtifactOptions gains excludeSessionIds iterable; new exported ArtifactAiSessionWithContent type; implements and exports getEmptyAiSessionIdForArtifact which returns the newest non-running, empty, artifact-owned session id or undefined.
Artifact-scoped session reuse in slice
packages/artifacts/src/ai/artifactAiSlice.ts
Imports the new helper and updates createArtifactScopedSession to detect when no explicit name/model options are provided, find an existing empty session via the helper, switch to it if found and differs from current, and skip new-session creation unless no reusable empty session exists or options are explicit.
Dashboard commands with conditional session creation
apps/sqlrooms-cli-ui/src/createDashboardCommands.ts
Adds shouldCreateInitialArtifactChat helper to gate session creation based on invocation surface type (skip creation if surface is 'ai' or 'mcp'). Updates both artifact-creation command execution handlers to accept full context and conditionally invoke createArtifactScopedSession after setting the current artifact.
Tests and documentation
packages/artifacts/__tests__/artifactAiSessions.test.ts, packages/artifacts/README.md
Adds unit tests for getEmptyAiSessionIdForArtifact (latest-empty selection, excludeSessionIds filtering, content field validation) and slice tests for reuse/current-session-skip/explicit-options scenarios. Updates README to document reuse semantics and lists the new helper.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • sqlrooms/sqlrooms#616: Modifies the same createArtifactCommand and createDashboardCreateArtifactCommand execution paths in createDashboardCommands.ts for artifact title generation during creation.

Suggested reviewers

  • lixun910
  • dmitriy-kostianetskiy

🐇 Hop hop, no more fresh starts each time,
An empty session waits—reuse it, sublime!
But AI calls get skipped (oh my, oh me!),
Only user-made artifacts spawn chat spree.
Efficient rabbit saves sessions with glee! 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 71.43% 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: reuse empty artifact chat sessions' directly and accurately summarizes the main change in the pull request, which implements functionality to reuse empty artifact-scoped chat sessions instead of creating duplicates.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/artifact-chat-session-reuse

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@ilyabo ilyabo changed the title [codex] reuse empty artifact chat sessions feat: reuse empty artifact chat sessions Jun 20, 2026
@ilyabo
ilyabo marked this pull request as ready for review June 20, 2026 11:23

@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: 29c820edba

ℹ️ 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 on lines +283 to +287
if (emptySessionId) {
if (get().ai.config.currentSessionId !== emptySessionId) {
get().ai.switchSession(emptySessionId);
}
return emptySessionId;

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 Avoid no-oping New session on renamed empty chats

When the current artifact's empty session has been renamed, the CLI still enables the New session button because AssistantChatContainer only disables empty sessions with default-generated names, but this branch finds that same empty session and returns it instead of creating another one. In that scenario clicking New session appears to do nothing and prevents starting a separate blank chat until the user sends a message or deletes/modifies the draft; either disable the caller for all empty sessions or avoid reusing the current renamed draft.

Useful? React with 👍 / 👎.

@ilyabo
ilyabo force-pushed the codex/artifact-chat-session-reuse branch from 29c820e to a369d63 Compare June 20, 2026 11:36

@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: a369d63995

ℹ️ 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 on lines +287 to +291
if (emptySessionId) {
if (currentSessionId !== emptySessionId) {
get().ai.switchSession(emptySessionId);
}
return emptySessionId;

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 Preserve requested session options when reusing

When createArtifactScopedSession is called with name, modelProvider, or model and the artifact already has another empty chat, this branch switches to that old chat and returns before createSession(name, modelProvider, model) runs. Callers of this exported API can therefore request a specifically named/modelled session but silently get an existing draft with the previous name/model instead; skip reuse or update the reused draft when explicit options are supplied.

Useful? React with 👍 / 👎.

@ilyabo
ilyabo force-pushed the codex/artifact-chat-session-reuse branch from a369d63 to 2070608 Compare June 20, 2026 11:43

@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: 20706086cf

ℹ️ 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".

state.pivot.ensurePivot(artifactId, {title: uniqueTitle});
}
state.artifacts.setCurrentArtifact(artifactId);
state.artifactAi.createArtifactScopedSession();

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 Avoid switching chats during AI-created artifacts

The CLI app registers these commands in the default AI command tool path (apps/sqlrooms-cli-ui/src/store.ts:714), and that tool invokes them with surface: 'ai' (packages/ai/src/tools/commandTools.ts:158). When a running assistant uses a create-artifact command, this new side effect creates/switches to a blank session for the new artifact while the original session keeps streaming in the old artifact's history, so the user loses the visible in-progress answer; keep this initial-chat creation in the tabstrip/UI caller or gate it away from AI/MCP command executions. The identical call in the dashboard-specific branch below has the same issue.

Useful? React with 👍 / 👎.

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

🧹 Nitpick comments (1)
packages/artifacts/src/ai/artifactAiSessionHelpers.ts (1)

118-154: 💤 Low value

Consider adding complete TSDoc with parameter and return documentation.

The exported function has a basic comment but is missing @param and @returns tags. As per coding guidelines, exported functions should have TSDoc.

Consider expanding to:

/**
 * Returns the most recently opened empty AI session explicitly owned by
 * `artifactId`.
 *
 * An empty session is one that is not running, has no UI messages, and has
 * no prompt text (ignoring whitespace).
 *
 * `@param` options - Session selection options
 * `@param` options.sessions - Available sessions to search
 * `@param` options.aiSessionArtifacts - Session-to-artifact ownership mapping
 * `@param` options.artifactId - Target artifact ID
 * `@param` options.excludeSessionIds - Session IDs to skip during selection
 * `@returns` The ID of the most recently opened empty session, or undefined if none found
 */
🤖 Prompt for 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.

In `@packages/artifacts/src/ai/artifactAiSessionHelpers.ts` around lines 118 -
154, The exported function `getEmptyAiSessionIdForArtifact` has only a basic
comment describing its return value but is missing complete TSDoc documentation.
Add `@param` tags documenting each property of the destructured options
parameter (sessions, aiSessionArtifacts, artifactId, excludeSessionIds) and a
`@returns` tag that documents the return type and what it represents. Also
expand the description comment to clarify what constitutes an empty session.

Source: Coding guidelines

🤖 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.

Nitpick comments:
In `@packages/artifacts/src/ai/artifactAiSessionHelpers.ts`:
- Around line 118-154: The exported function `getEmptyAiSessionIdForArtifact`
has only a basic comment describing its return value but is missing complete
TSDoc documentation. Add `@param` tags documenting each property of the
destructured options parameter (sessions, aiSessionArtifacts, artifactId,
excludeSessionIds) and a `@returns` tag that documents the return type and what
it represents. Also expand the description comment to clarify what constitutes
an empty session.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ac26fb5c-93f7-4cc9-a51c-2ee6bd5b82f9

📥 Commits

Reviewing files that changed from the base of the PR and between 571cae1 and 2070608.

📒 Files selected for processing (6)
  • apps/sqlrooms-cli-ui/src/createDashboardCommands.ts
  • packages/artifacts/README.md
  • packages/artifacts/__tests__/artifactAiSessions.test.ts
  • packages/artifacts/src/ai/artifactAiSessionHelpers.ts
  • packages/artifacts/src/ai/artifactAiSlice.ts
  • packages/artifacts/src/ai/index.ts

@ilyabo
ilyabo force-pushed the codex/artifact-chat-session-reuse branch from 2070608 to af01a24 Compare June 20, 2026 12:11

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

Actionable comments posted: 1

🤖 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 `@apps/sqlrooms-cli-ui/src/createDashboardCommands.ts`:
- Line 149: The destructuring assignment in the
DashboardCreateArtifactCommandInput block does not handle the case where input
is undefined, which will cause a runtime error. Add a null check or provide a
fallback value (such as an empty object) before destructuring the title and
layoutType properties from input to ensure the code safely handles the case when
input is omitted or undefined.
🪄 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

Run ID: 1f4510ea-6b15-49ab-9104-4f1b1071217d

📥 Commits

Reviewing files that changed from the base of the PR and between 2070608 and af01a24.

📒 Files selected for processing (6)
  • apps/sqlrooms-cli-ui/src/createDashboardCommands.ts
  • packages/artifacts/README.md
  • packages/artifacts/__tests__/artifactAiSessions.test.ts
  • packages/artifacts/src/ai/artifactAiSessionHelpers.ts
  • packages/artifacts/src/ai/artifactAiSlice.ts
  • packages/artifacts/src/ai/index.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/artifacts/README.md
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/artifacts/tests/artifactAiSessions.test.ts
  • packages/artifacts/src/ai/artifactAiSessionHelpers.ts
  • packages/artifacts/src/ai/index.ts
  • packages/artifacts/src/ai/artifactAiSlice.ts

Comment thread apps/sqlrooms-cli-ui/src/createDashboardCommands.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: af01a24f4c

ℹ️ 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 on lines +144 to +145
(session.uiMessages?.length ?? 0) === 0 &&
(session.prompt ?? '').trim().length === 0

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 Require message fields for empty-session selection

When callers use the newly exported getEmptyAiSessionIdForArtifact with the public ArtifactAiSession shape, prompt and uiMessages are optional, but this check defaults missing values to empty. An external caller can therefore pass a valid minimal non-running session summary that actually represents a used chat and have it selected as a reusable empty draft; require these fields for this helper or treat missing values as non-empty.

Useful? React with 👍 / 👎.

@ilyabo
ilyabo force-pushed the codex/artifact-chat-session-reuse branch from af01a24 to c2e139b Compare June 20, 2026 14:48
@ilyabo
ilyabo merged commit bd26a35 into main Jun 21, 2026
3 checks passed
@ilyabo
ilyabo deleted the codex/artifact-chat-session-reuse branch June 21, 2026 09:15
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