Skip to content

fix(opencode-plugin): correct session.messages response shape and update tests#993

Merged
nicoloboschi merged 1 commit intovectorize-io:mainfrom
apnea:fix/opencode-auto-retain-api-shape
Apr 13, 2026
Merged

fix(opencode-plugin): correct session.messages response shape and update tests#993
nicoloboschi merged 1 commit intovectorize-io:mainfrom
apnea:fix/opencode-auto-retain-api-shape

Conversation

@apnea
Copy link
Copy Markdown
Contributor

@apnea apnea commented Apr 11, 2026

Summary

Fix the opencode plugin's getSessionMessages function, which had two bugs that prevented auto-retain from functioning since it was introduced in #853.

The bugs

1. Wrong response shape (hooks.ts:131)

The code read msg.role but the opencode SDK returns messages wrapped as { info: { role }, parts }. This caused a runtime TypeError: Cannot read properties of undefined (reading 'role') every time getSessionMessages was called.

2. Mismatched TypeScript type for SDK call (hooks.ts OpencodeClient type)

The production code correctly calls opencodeClient.session.messages({ path: { id: sessionId } }), but the TypeScript type declaration for that function specified { sessionID: string } — a completely different signature. The type definition did not match the actual call. This was a type error that would have been caught at compile time with stricter type checking.

Impact

Auto-retain and pre-compaction retain were non-functional from the point the plugin was introduced. Every call to getSessionMessages threw a TypeError that was silently caught, returning an empty array. The handleSessionIdle and compaction hooks then exited early at the if (!messages.length) return check. The tools (retain/recall/reflect) and system transform hook were unaffected.

The original test mocks used the same incorrect { role } shape, so all tests passed despite the code not matching the real SDK contract.

Changes

  • hooks.ts: Fix OpencodeClient type to match the real SDK, read msg.info.role instead of msg.role
  • hooks.test.ts: Update all mock message shapes to { info: { role }, parts }

Testing

  • 89/89 tests pass in a clean Docker environment (node:20, fresh clone from remote)
  • Manually verified auto-retain fires correctly in a live opencode session

Copy link
Copy Markdown
Collaborator

@nicoloboschi nicoloboschi left a comment

Choose a reason for hiding this comment

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

we should support 1.3.x versions still

@apnea
Copy link
Copy Markdown
Contributor Author

apnea commented Apr 13, 2026

Hi @nicoloboschi , the plugin is built against @opencode-ai/sdk@1.3.13. I'm not sure what you mean.

@nicoloboschi
Copy link
Copy Markdown
Collaborator

Hi @nicoloboschi , the plugin is built against @opencode-ai/sdk@1.3.13. I'm not sure what you mean.

I thought opencode changed the message format but I was wrong, sorry, merging now

@nicoloboschi nicoloboschi merged commit fd87de9 into vectorize-io:main Apr 13, 2026
DK09876 added a commit that referenced this pull request Apr 14, 2026
PR #993 added hardcoded console.error calls throughout hooks.ts for
debugging the message parsing fix. These are not gated behind the debug
config flag, so they spam every user's TUI with red error text on every
event, message parse, and retain cycle.

Replace all console.error calls with debugLog(config, ...) so they only
appear when debug: true is set in plugin options.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
nicoloboschi pushed a commit that referenced this pull request Apr 14, 2026
PR #993 added hardcoded console.error calls throughout hooks.ts for
debugging the message parsing fix. These are not gated behind the debug
config flag, so they spam every user's TUI with red error text on every
event, message parse, and retain cycle.

Replace all console.error calls with debugLog(config, ...) so they only
appear when debug: true is set in plugin options.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
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