GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — feat(core): watch MCP server events and wake the session by adamdotdevin · Pull Request #63178 · anomalyco/opencode Conversation
|
| StreamResult, | ||
| { | ||
| signal, | ||
| timeout: execution, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
events/stream is meant to stay open indefinitely, but it uses the tool-call execution timeout. For attached servers that's a fixed 10 minutes. For stdio and remote servers it's whatever timeout.execution is set to, and users often set a few minutes. When it expires, the SDK rejects with "Request timed out", and the watch stops and wakes the session with that error. A once: false watch, or a once watch on a slow job, dies after that long. Could the stream go without a request timeout, or treat a timeout like a clean close and reopen from the cursor?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good catch. events/stream now uses the longest timeout setTimeout allows (about 24.8 days) instead of the tool-call one, and there's a test that keeps a stream open past a 50 ms execution timeout. fwiw I left the capability declaration ungated: gating it means threading the flag through the MCP layer, connection and pool, and declaring it changes nothing until the tool opens a stream.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The fix in 6c29c70 works.
events/stream now passes STREAM_TIMEOUT = 2 ** 31 - 1 (client.ts:73, used at :510). In the SDK version core uses (@modelcontextprotocol/[email protected]), that value goes straight into a single setTimeout. That timer doesn't reset on progress, and no maxTotalTimeout applies unless the caller sets one, so nothing else cuts the stream off sooner. Past ~24.8 days the request still times out and the watch ends with an error instead of reopening. That limit is fine.pr/head, all 4 tests in test/mcp-events.test.ts pass. I changed the timeout back to execution and only "keeps a stream open past the server's execution timeout" failed, so the test does guard the fix.events/*. Nothing is sent and no notification is routed until the tool opens a stream, and only the tool is behind the flag.I didn't check remote streams over a real HTTP transport. A proxy or the server's idle timeout can still drop a long-lived SSE response. That shows up as the stream closing or failing, which the reopen-from-cursor path covers (up to three closes in a row with no event), so it isn't blocking.
I ran this in a temporary worktree at pr/head and removed it afterwards. I didn't touch repo/ or its uncommitted bun.lock change.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ) | ||
| yield* follow().pipe( | ||
| // A server that closes the stream itself, as when it restarts in place, is followed once more. | ||
| Stream.concat(Stream.suspend(follow)), |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This follows a server-closed stream only once. If the server closes it a second time (say it restarts twice), the stream ends cleanly, nothing reaches Effect.catch, and the watch stops without waking the session, even though the session was told not to poll. A clean end should either keep reopening (with a bound or backoff) or wake the session with a "watch stopped" message like the error path does.
Related: line 114 sets cursor = event.cursor even when an event has no cursor, which clears the caller's cursor. A later reopen then starts from "now" and can skip events. Keeping the previous value when event.cursor is undefined would avoid that.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixed. A clean close now reopens from the last cursor after 500 ms, and after 3 closes in a row with no event in between it stops and wakes the session with the same "watch stopped" message. An event without a cursor keeps the previous one now too.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I checked this in 6c29c70 and it's fixed.
follow() reopens after 500 ms using the last cursor. Every event sets closes back to 0, and when the count passes 3 the watch fails with "the server kept closing the stream". That goes through the existing Effect.catch, so the session gets the "watch stopped" message. One small detail: the check is ++closes > 3, so the server can close the stream 3 times and get 3 reopens, and the watch only stops on the 4th close in a row. The test asserts this behaviour too (the stop arrives as wakes[5]), so the code and the test agree. Only your description says 3 closes.if (event.cursor !== undefined) cursor = event.cursor). The reopen test checks it: the reopen after a cursor-less event still sends c2.events.stream is built on Stream.callback, so nothing is sent until the stream runs. That means the reopen request goes out after the 500 ms sleep, with the cursor as it stands then.events/stream off it (STREAM_TIMEOUT = 2**31-1). That looks right: before this, a long-lived watch would have been cut off by the tool-call timeout.I ran bun test test/mcp-events.test.ts against pr/head in a temporary worktree and all 4 tests passed. I didn't run the full mcp suite or typecheck. I didn't change anything in repo/; the bun.lock modification there was already present and I left it alone.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
events/stream no longer uses the tool-call execution timeout. A watch reopens a stream the server closes from the last cursor, and stops (waking the session) after three closes in a row without an event. An event without a cursor keeps the previous one.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Both earlier points are addressed. The stream no longer uses the execution timeout. A closed stream is now reopened from the last cursor and gives up, waking the session, after three closes in a row with no event in between. An event without a cursor keeps the previous one. I checked this with bun test test/mcp-events.test.ts (4/4 pass) and bun run typecheck:tsc in core (passes). The public API is unchanged since the last review: it adds the opencode_watch_mcp_event tool and the mcp_events flag, and declares the MCP events client extension, all backwards compatible.
One thing in the tests (inline): the reconnect case is now hard to follow and spends about 3 s in real 500 ms waits.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| // The server ends the stream itself, as when it restarts in place: follow it from the last cursor, until | ||
| // it has closed it three times in a row without an event. | ||
| for (const index of [0, 1, 2, 3, 4, 5]) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This loop is hard to follow: an event is sent at index 3, and the expected cursor flips on index < 3. Each reopen also waits the real 500 ms, so the test spends about 3 s sleeping. Could it check just the core cases: one reopen from the last cursor, the counter resetting after an event, and giving up after three closes in a row? Making the delay something the test can shorten, or using it.effect with TestClock, would keep it fast and less timing-dependent.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Split it out into its own it.effect test on TestClock that checks one reopen from the last cursor, the count resetting after an event, and the watch stopping on the third close in a row, which is now what the code does too.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I checked b29040640 and 6c29c70f0 on pr/head, and the change does what you describe.
Code: ++closes >= 3 fails the watch on the third close in a row (mcp-events.ts:113). closes = 0 in runForEach resets the count after each event. The comment above it now matches.
Test: the new it.effect test walks through the steps in order:
c1. An event with no cursor keeps the last one.c3, and the third stops the watch with "the server kept closing the stream".The 500 ms sleeps run on TestClock through advance. The leftover it.live test still covers replay, session deletion and termination, so nothing from the old loop was lost.
What I ran:
pr/head, bun run test test/mcp-events.test.ts passed 5 out of 5, five runs in a row, in about 1.2 s.> 3, and removing the closes = 0 reset.One small point you can ignore: when it fails, it fails at advance's 15 s deadline with "condition never became true", not on a specific expect.
I didn't run typecheck or the full test/mcp suite. I removed the worktree afterwards and didn't change repo/. The bun.lock modification already there is someone else's and I left it alone.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
A watch now stops on the third close in a row, matching its comment.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The test point is addressed. The reconnect case is now its own short test driven by TestClock, and the file runs in about a second. The >= 3 change matches the documented "three closes in a row" behaviour. The other two points were already fixed by the previous push. I ran bun test test/mcp-events.test.ts three times (5/5 each time) and bun run typecheck:tsc in core (passes). Public API: the regenerated client types and OpenAPI document now add the optional mcp_events boolean to the config flags. Together with the opencode_watch_mcp_event tool and the MCP events client extension, this is all backwards compatible. No new concerns.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Superseded: this landed in two co-authored PRs. #63254 adds the MCP Events client (with an |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.