fix(plugin): per-session project detection from session.created directory#749
fix(plugin): per-session project detection from session.created directory#749Acharnite wants to merge 2 commits into
Conversation
…tory Two changes: 1. Use ctx.project?.worktree (full path) instead of ctx.project?.id (short name) as the plugin-init project detection fallback. 2. Resolve project per-session from session.created event's info?.directory field, falling back to the init-time projectPath. This ensures that when OpenCode runs from a workspace root (e.g. /home/user/projects/) serving multiple sub-projects, each session is correctly tagged with its own project directory (e.g. /home/user/projects/my-app) instead of the shared workspace root.
|
@Acharnite is attempting to deploy a commit to the rohitg00's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe plugin changes projectPath sourcing to use the project's worktree at init, and session.created now computes a per-session project path from the session directory (falling back to the init-time projectPath) used for both project and cwd in the /session/start request. ChangesProject path sourcing and session initialization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 `@plugin/opencode/agentmemory-capture.ts`:
- Line 191: The assignment to sessionProject uses a type assertion
(info?.directory as string) without runtime checks, so if info?.directory is
non-string or empty the backend will reject /session/start; replace this with
explicit validation: check if info?.directory is a non-empty string before using
it and otherwise fall back to projectPath or throw/return a clear error; update
the logic around sessionProject (where sessionProject is set) to validate typeof
info?.directory === "string" && info.directory.trim().length > 0 and only then
use it, otherwise use projectPath or raise a descriptive error to fail fast.
🪄 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: 0de8058a-1c91-445e-8c7d-71d710b3eae2
📒 Files selected for processing (1)
plugin/opencode/agentmemory-capture.ts
CodeRabbitAI review feedback: - Replace (info?.directory as string) type assertion with proper runtime typeof + length validation to avoid 400 errors from backend - Add JSDoc comments to AgentmemoryCapturePlugin and postJson - Add inline comment explaining validation logic Refs: PR rohitg00#749 CodeRabbitAI review
Problem
When OpenCode runs as a web service from a workspace root directory (e.g.,
/home/user/projects/) serving multiple sub-projects, the agentmemory-capture plugin'sprojectPathis initialized once at plugin load time. Every session receives the same project string — the workspace root — regardless of which sub-project the user is actually working in.This causes memory cross-contamination: sessions in
project-aandproject-bboth get tagged withproject: "/home/user/projects/", making project-scoped queries (mem::searchwithprojectfilter) useless and mixing memories across unrelated projects.Solution
Two changes to
plugin/opencode/agentmemory-capture.ts:1. Better plugin-init project detection (line 171)
The OpenCode plugin context already has
ctx.project?.worktree(full filesystem path) andctx.directory— use them before falling back toprocess.cwd():ctx.project?.idreturns a short name (e.g.,my-app) whilectx.project?.worktreereturns the full path (e.g.,/home/user/projects/my-app). Full paths are unique, short names are not.2. Per-session project from
session.createdevent (line 191, 197-198)The OpenCode bus publishes
session.createdevents that include adirectoryfield with the specific project path. Extract it per-session instead of using the global init-time value:This is a 3-line change (1 new, 2 modified) with a clean fallback: if
info?.directoryis not available (e.g., CLI mode), it uses the init-timeprojectPath.Testing
Verified on OpenCode 1.15.12 running as a systemd web service with
WorkingDirectory=/home/kiffer/projects/and multiple sub-projects. Before the fix,agentmemory-error.logshowed all sessions withproject: "/home/kiffer/projects". After the fix, sessions are correctly tagged with their specific project directory (e.g.,project: "/home/kiffer/projects/kodehold").Related issues
Summary by CodeRabbit