agentmemory icon indicating copy to clipboard operation
agentmemory copied to clipboard

OpenCode plugin can record observations without ever creating the session

Open Wermeling opened this issue 1 month ago • 0 comments

Summary

With the OpenCode plugin, observations can be captured successfully while the corresponding session is never created in agentmemory. The result is:

  • /agentmemory/observe requests succeed
  • observations appear in server logs
  • memory_sessions does not show the new session
  • later summarize calls fail with Session not found for summarize

This showed up after fixing an unrelated infrastructure problem where agentmemory mcp had silently fallen back to local InMemoryKV because /agentmemory/livez was not reachable. Once the real server was running, the remaining bug was in the plugin/session-start path.

Environment

  • OpenCode 1.15.10
  • agentmemory 0.9.21
  • WSL2 / Linux
  • plugin: plugin/opencode/agentmemory-capture.ts

Observed behavior

The server receives plugin events for the new OpenCode session, for example:

  • prompt_submit
  • post_tool_use
  • assistant_message
  • session_status
  • session_diff
  • session_updated
  • patch_applied

But the session itself is missing from /agentmemory/sessions / memory_sessions.

Server log excerpt:

[agentmemory] info Observation captured {"obsId":"...","sessionId":"ses_...","hook":"prompt_submit","compress":"synthetic"}
[agentmemory] info Observation captured {"obsId":"...","sessionId":"ses_...","hook":"post_tool_use","compress":"synthetic"}
[agentmemory] warn Session not found for summarize {"sessionId":"ses_..."}

So the server is clearly receiving events for a session ID that was never registered via /agentmemory/session/start.

Expected behavior

Once the first event for a session arrives, that session should exist in agentmemory and be visible via memory_sessions.

Why this seems to happen

The plugin currently depends on session.created to call /agentmemory/session/start.

However, in practice it is possible for later hooks to arrive and be processed even though the session-start path did not successfully establish the session record.

That leaves the system in a half-working state:

  • observations continue to be captured
  • the session table remains empty for that session
  • summarize/end flows later fail because the session was never created

Repro outline

  1. Start a real agentmemory server so MCP is not using fallback mode.
  2. Configure OpenCode with:
    • MCP: agentmemory mcp
    • plugin: plugin/opencode/agentmemory-capture.ts
  3. Start a fresh OpenCode session.
  4. Send a few prompts / run a few tools.
  5. Check memory_sessions.

Observed:

  • new session not listed
  • but server logs show observations for a new ses_... session ID

Suggested fix

Make the plugin resilient to missing/late session.created handling by lazily bootstrapping the session on first event.

In other words:

  • before observe(...), ensure the session exists
  • if it does not, call /agentmemory/session/start using the current sessionId
  • then proceed with /observe

This makes the plugin robust even if session.created is missed, delayed, or shaped differently than expected.

A smaller but also useful improvement: make post() treat non-2xx responses as failures. Right now it can silently ignore HTTP errors because it only catches fetch exceptions.

Additional note

This is distinct from the agentmemory mcp fallback behavior. We also confirmed separately that if /agentmemory/livez is unavailable, the MCP bridge silently falls back to local InMemoryKV, which can mask server/plugin issues during debugging.

Wermeling avatar May 25 '26 08:05 Wermeling

I would accept a pr on this.

1337Core avatar Apr 17 '26 06:04 1337Core

actually in archive.md it was marked as subpar. @Axorax do you know why this was? from surface level it seems fine.

1337Core avatar Apr 18 '26 01:04 1337Core

actually in archive.md it was marked as subpar. @Axorax do you know why this was? from surface level it seems fine.

Because the site was mainly in Russian. I think it just got added there with Yandex. We can reconsider adding it.

Axorax avatar Apr 25 '26 11:04 Axorax

oh shoot your right my browser was autotranslating. i'm sure there are better ones

1337Core avatar Apr 28 '26 03:04 1337Core

Better alternatives available. Seems to be mainly Russian.

Axorax avatar May 29 '26 17:05 Axorax