Skip to content

refactor(assistant): split runtime concerns - #63

Merged
omarluq merged 3 commits into
mainfrom
refactor/assistant-runtime-split
May 30, 2026
Merged

refactor(assistant): split runtime concerns#63
omarluq merged 3 commits into
mainfrom
refactor/assistant-runtime-split

Conversation

@omarluq

@omarluq omarluq commented May 30, 2026

Copy link
Copy Markdown
Owner

Summary

  • split assistant runtime helpers by concern
  • move session resolution, persistence, slash commands, model execution, skills, and context helpers out of runtime.go
  • keep behavior unchanged while reducing runtime.go size

Validation

  • mise exec -- go test ./internal/assistant
  • mise exec -- task ci

@coderabbitai

coderabbitai Bot commented May 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ed07850b-0ba2-4fd8-878f-a5ffb0808fd7

📥 Commits

Reviewing files that changed from the base of the PR and between 0f401d9 and 5025fe0.

📒 Files selected for processing (14)
  • internal/assistant/client_test.go
  • internal/assistant/context_usage.go
  • internal/assistant/lifecycle.go
  • internal/assistant/lifecycle_diagnostics.go
  • internal/assistant/provider_hooks.go
  • internal/assistant/retry.go
  • internal/assistant/tool_schema.go
  • internal/database/entry_metadata.go
  • internal/database/validation_test.go
  • internal/extension/lifecycle.go
  • internal/extension/sources.go
  • internal/terminal/token_usage.go
  • internal/tool/bash.go
  • internal/tool/edit_diff.go
💤 Files with no reviewable changes (2)
  • internal/database/validation_test.go
  • internal/assistant/client_test.go
✅ Files skipped from review due to trivial changes (4)
  • internal/terminal/token_usage.go
  • internal/assistant/lifecycle_diagnostics.go
  • internal/assistant/provider_hooks.go
  • internal/extension/lifecycle.go

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Slash-command support for listing/executing skills and built-in tools, with readable tool output.
    • Session controls: resume latest, explicit session selection, and clearer parent/creation behavior.
    • Streaming/persistent partial responses so intermediate “thinking” and tool results are stored.
  • Refactor

    • Assistant runtime reorganized for improved model orchestration, caching/retry handling, context loading, and command flow.

Walkthrough

This PR modularizes the assistant runtime by extracting session resolution, context loading, model orchestration (with caching and retry), streaming partial-progress persistence, slash-command dispatch, and skill read tooling into dedicated files.

Changes

Assistant Runtime Refactoring

Layer / File(s) Summary
Runtime core cleanup
internal/assistant/runtime.go
Removes inlined orchestration functions and helper types, retaining only the Runtime type, exported constructors/getters, stream/event/request/response structs, and small helpers; updates imports accordingly.
Session resolution orchestration
internal/assistant/runtime_session.go
Implements resolveSession dispatcher and strategies to load requested sessions, resume latest (or create new), or create a new prompt session; returns session entity and lifecycle event name and enforces input conflict rules.
Context message loading and filtering
internal/assistant/runtime_context.go
Loads session context messages, filters to model-facing roles (user/assistant only), and generates a base system prompt including CWD guidance; wraps load errors with oops codes.
Model orchestration and completion with caching
internal/assistant/runtime_model.go
Adds respond with slash-command short-circuit, response caching, modelResponse orchestration (model selection, auth, context usage emission, tool registry), completion request construction, and completeWithRetry with provider-attempt tracking and retry event emission.
Streaming partial progress and persistence
internal/assistant/runtime_persist.go
Accumulates streamed assistant/thinking/tool-result blocks, forwards events, merges adjacent text blocks, persists intermediate progress and formatted tool-result messages on failure, and provides retry/reset logic.
Skill activation and read-tool infrastructure
internal/assistant/runtime_skills.go
Loads activated skills via read tool (truncated to 2000 lines), emits StreamEventSkillLoaded events with ToolEvent payloads, and builds activation and match metadata payloads.
Slash command dispatch and execution
internal/assistant/runtime_slash.go
Parses slash commands, dispatches skill and tool subcommands to runtime handlers, routes unknown commands to extensions, and wraps execution failures with oops errors.
Stdlib/util refactors and test tweaks
internal/* (maps/slices/strings changes), internal/assistant/* tests
Replaces manual map-copy/merge loops and reverse-iteration with maps.Copy/slices.Backward, uses strings.SplitSeq/strings.CutPrefix, and removes an unnecessary loop-variable capture in tests.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • omarluq/librecode#43: Related to tool registry and request-aware tool schema/registry APIs used by the new model/tool orchestration.
  • omarluq/librecode#9: Related config shape changes for extensions referenced by the runtime refactor.
  • omarluq/librecode#30: Overlaps provider lifecycle and completion retry/event plumbing that was moved into runtime_model.go.

Poem

🐰 Once one grand file, now split with care,
Seven modules share the prompt's fare—
Sessions resolve and context flows clean,
Models retry and persistence keeps sheen,
Skills and slash commands dance on the scene.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: splitting assistant runtime concerns into separate files to improve code organization and maintainability.
Description check ✅ Passed The description is directly related to the changeset, outlining the refactoring goals (splitting runtime helpers by concern), listing specific actions taken (moving session resolution, persistence, slash commands, model execution, skills, and context), and documenting validation steps performed.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/assistant-runtime-split

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

@codecov-commenter

codecov-commenter commented May 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.60035% with 157 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.31%. Comparing base (45722ea) to head (5025fe0).

Files with missing lines Patch % Lines
internal/assistant/runtime_session.go 37.50% 41 Missing and 9 partials ⚠️
internal/assistant/runtime_model.go 75.30% 34 Missing and 7 partials ⚠️
internal/assistant/runtime_slash.go 54.54% 27 Missing and 8 partials ⚠️
internal/assistant/runtime_persist.go 86.86% 10 Missing and 8 partials ⚠️
internal/assistant/runtime_skills.go 86.36% 5 Missing and 1 partial ⚠️
internal/assistant/runtime_context.go 86.48% 3 Missing and 2 partials ⚠️
internal/assistant/retry.go 93.33% 0 Missing and 1 partial ⚠️
internal/tool/bash.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #63      +/-   ##
==========================================
- Coverage   62.32%   62.31%   -0.01%     
==========================================
  Files         178      184       +6     
  Lines       17824    17818       -6     
==========================================
- Hits        11108    11103       -5     
+ Misses       5619     5618       -1     
  Partials     1097     1097              
Flag Coverage Δ
unittests 62.31% <72.60%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

@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

🧹 Nitpick comments (2)
internal/assistant/runtime_model.go (1)

118-146: ⚖️ Poor tradeoff

Consider bundling modelCompletionRequest parameters.

SonarCloud flags this constructor as having 9 parameters (limit 7). Several of these already travel together (e.g. selectedModel, auth, usage, messages, systemPrompt could be passed via the existing contextResult/a small params struct), which would clear the gate and reduce positional-arg mistakes. Optional given this is a behavior-preserving refactor.

🤖 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 `@internal/assistant/runtime_model.go` around lines 118 - 146, Replace the long
parameter list of Runtime.modelCompletionRequest by introducing a small params
struct (e.g., ModelCompletionParams or reuse the existing contextResult) that
bundles related values like SelectedModel *model.Model, Auth model.RequestAuth,
Messages []database.MessageEntity, SystemPrompt string, Usage model.TokenUsage,
SessionID string, CWD string, Registry *tool.Registry and OnEvent
func(StreamEvent); change modelCompletionRequest to accept that single params
struct (plus the runtime receiver), map the struct fields to the
CompletionRequest as before, and update all call sites to construct and pass the
new params struct so behavior remains identical while reducing the parameter
count and preventing positional-arg mistakes.
internal/assistant/runtime_skills.go (1)

44-46: 💤 Low value

Consider making this a package constant.

The function always returns the same value. Unless there are plans to make this configurable, consider defining it as a constant for clarity.

♻️ Optional refactor
+const maxActiveSkillReadLines = 2000
+
-func maxActiveSkillReadLines() int {
-	return 2000
-}

Then update the caller on line 20:

-	limit := maxActiveSkillReadLines()
+	limit := maxActiveSkillReadLines
🤖 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 `@internal/assistant/runtime_skills.go` around lines 44 - 46, Replace the
trivial function maxActiveSkillReadLines() with a package-level constant (e.g.,
MaxActiveSkillReadLines = 2000) and update all call sites (including the caller
referencing maxActiveSkillReadLines) to use that constant instead of calling the
function; keep the constant name exported or unexported to match intended
visibility and adjust any tests or references to maxActiveSkillReadLines
accordingly.
🤖 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 `@internal/assistant/runtime_slash.go`:
- Around line 24-26: Replace the plain fmt.Errorf in the empty slash command
check with a samber/oops error: locate the if block that checks commandName ==
"" in runtime_slash.go and return an oops error created via
oops.In("assistant").Code("empty_slash_command").New("assistant: empty slash
command") (or use .Wrapf(err, "...") if you are wrapping an existing error),
ensuring the package consistently uses oops for contextual errors.
- Around line 86-88: Replace the plain fmt.Errorf return with an oops error:
change the return that currently does fmt.Errorf("assistant: skill %q not
found", name) to use samber/oops, e.g.
oops.In("assistant").Code("skill_not_found").Newf("skill %q not found", name)
(or Wrapf(nil, ...) if your codebase prefers Wrapf); update the import to
include "github.com/samber/oops" if not already present and ensure the returned
error uses this oops instance instead of fmt.Errorf.

---

Nitpick comments:
In `@internal/assistant/runtime_model.go`:
- Around line 118-146: Replace the long parameter list of
Runtime.modelCompletionRequest by introducing a small params struct (e.g.,
ModelCompletionParams or reuse the existing contextResult) that bundles related
values like SelectedModel *model.Model, Auth model.RequestAuth, Messages
[]database.MessageEntity, SystemPrompt string, Usage model.TokenUsage, SessionID
string, CWD string, Registry *tool.Registry and OnEvent func(StreamEvent);
change modelCompletionRequest to accept that single params struct (plus the
runtime receiver), map the struct fields to the CompletionRequest as before, and
update all call sites to construct and pass the new params struct so behavior
remains identical while reducing the parameter count and preventing
positional-arg mistakes.

In `@internal/assistant/runtime_skills.go`:
- Around line 44-46: Replace the trivial function maxActiveSkillReadLines() with
a package-level constant (e.g., MaxActiveSkillReadLines = 2000) and update all
call sites (including the caller referencing maxActiveSkillReadLines) to use
that constant instead of calling the function; keep the constant name exported
or unexported to match intended visibility and adjust any tests or references to
maxActiveSkillReadLines accordingly.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b6faa6b3-ab85-43d7-8a01-97aaaa5f0d92

📥 Commits

Reviewing files that changed from the base of the PR and between 45722ea and a6ec3d2.

📒 Files selected for processing (7)
  • internal/assistant/runtime.go
  • internal/assistant/runtime_context.go
  • internal/assistant/runtime_model.go
  • internal/assistant/runtime_persist.go
  • internal/assistant/runtime_session.go
  • internal/assistant/runtime_skills.go
  • internal/assistant/runtime_slash.go
💤 Files with no reviewable changes (1)
  • internal/assistant/runtime.go

Comment thread internal/assistant/runtime_slash.go
Comment thread internal/assistant/runtime_slash.go
coderabbitai[bot]
coderabbitai Bot previously approved these changes May 30, 2026
@sonarqubecloud

Copy link
Copy Markdown

@omarluq
omarluq merged commit fa16085 into main May 30, 2026
13 checks passed
@omarluq
omarluq deleted the refactor/assistant-runtime-split branch May 30, 2026 19:22
@coderabbitai coderabbitai Bot mentioned this pull request Jun 21, 2026
This was referenced Jul 14, 2026
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