-
Notifications
You must be signed in to change notification settings - Fork 303
fix(server): preserve sends and truthful presence across poll transitions #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -249,6 +249,17 @@ export async function serve({ | |
| const logEvent = verbose ? (line) => writeLog(`[lavish] ${line}`) : null; | ||
| let publicPort = port; | ||
|
|
||
| function finishFeedbackDelivery(key, result) { | ||
| if (result.status !== "feedback") return; | ||
| const chat = result.chat; | ||
| delete result.chat; | ||
| markFeedbackDelivered(key, activePolls, deliveredFeedback, events); | ||
| // A batch flagged `session_ended` is the last one this session will ever deliver, so no | ||
| // later poll or agent reply can retire the working state markFeedbackDelivered just set: | ||
| // release it here or presence reports an agent still working on a session that is over. | ||
| if (result.session_ended) clearFeedbackDelivery(key, activePolls, deliveredFeedback, events); | ||
| if (Array.isArray(chat)) events.emit("chat-sync", key, chat); | ||
| } | ||
| // Whiteboard sidecar files live next to state.json, keyed by session + diagram. | ||
| const whiteboardStateRoot = path.dirname(stateFile); | ||
|
|
||
|
|
@@ -376,17 +387,34 @@ export async function serve({ | |
| }); | ||
|
|
||
| app.get("/api/poll", async (req, res, next) => { | ||
| // `close` is subscribed before the first `await` and re-checked after the listeners are armed, | ||
| // because a client that disconnects while `takeFeedback` is in flight would otherwise arrive | ||
| // too late for its own cleanup: the handler marks the poll active afterwards and nothing left | ||
| // would clear it, leaving presence stuck on "listening" for an agent that is already gone. | ||
| let requestClosed = Boolean(req.destroyed); | ||
| let cleanupPoll = null; | ||
| const onRequestClose = () => { | ||
| requestClosed = true; | ||
| cleanupPoll?.(); | ||
| }; | ||
| const detachRequestClose = () => req.off("close", onRequestClose); | ||
| req.on("close", onRequestClose); | ||
| try { | ||
| const file = await canonicalFile(String(req.query.file || "")); | ||
| const key = sessionKey(file); | ||
| const timeoutMs = | ||
| req.query.timeoutMs === undefined ? null : Math.max(0, Math.min(Number(req.query.timeoutMs || 0), 2147483647)); | ||
| const immediate = await store.takeFeedback(key); | ||
| if (immediate.status !== "waiting") { | ||
| if (immediate.status === "feedback") markFeedbackDelivered(key, activePolls, deliveredFeedback, events); | ||
| finishFeedbackDelivery(key, immediate); | ||
| detachRequestClose(); | ||
| res.json(immediate); | ||
|
Comment on lines
+449
to
450
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the polling client disconnects after the closure check but before the immediate JSON response finishes, this branch marks the destructively taken batch as delivered and detaches the close listener before |
||
| return; | ||
| } | ||
| if (requestClosed || req.destroyed || res.writableEnded) { | ||
| detachRequestClose(); | ||
| return; | ||
| } | ||
| const streamHeartbeat = timeoutMs === null; | ||
| let heartbeat = null; | ||
| if (streamHeartbeat) { | ||
|
|
@@ -399,7 +427,7 @@ export async function serve({ | |
| } | ||
| setPollActive(key, activePolls, deliveredFeedback, events, true); | ||
| refreshIdleTimer(); | ||
| const timer = timeoutMs === null ? null : setTimeout(() => respond().catch(handleRespondError), timeoutMs); | ||
| let timer = null; | ||
| let cleaned = false; | ||
| let responding = false; | ||
| const cleanup = () => { | ||
|
|
@@ -411,13 +439,15 @@ export async function serve({ | |
| events.off("ended", onFeedback); | ||
| setPollActive(key, activePolls, deliveredFeedback, events, false); | ||
| refreshIdleTimer(); | ||
| cleanupPoll = null; | ||
| detachRequestClose(); | ||
| }; | ||
| const respond = async () => { | ||
| if (responding || res.writableEnded) return; | ||
| responding = true; | ||
| try { | ||
| const result = await store.takeFeedback(key); | ||
| if (result.status === "feedback") markFeedbackDelivered(key, activePolls, deliveredFeedback, events); | ||
| finishFeedbackDelivery(key, result); | ||
| if (streamHeartbeat) { | ||
| res.end(JSON.stringify(result)); | ||
| } else { | ||
|
|
@@ -443,8 +473,15 @@ export async function serve({ | |
| }; | ||
| events.on("feedback", onFeedback); | ||
| events.on("ended", onFeedback); | ||
| req.on("close", cleanup); | ||
| cleanupPoll = cleanup; | ||
| if (requestClosed || req.destroyed || res.writableEnded) { | ||
| cleanup(); | ||
| return; | ||
| } | ||
| timer = timeoutMs === null ? null : setTimeout(() => respond().catch(handleRespondError), timeoutMs); | ||
| } catch (error) { | ||
| cleanupPoll?.(); | ||
| detachRequestClose(); | ||
| next(error); | ||
| } | ||
| }); | ||
|
|
@@ -620,9 +657,9 @@ export async function serve({ | |
| } | ||
| events.emit("agent-reply", req.params.key, text); | ||
| // The reply concludes the delivered-feedback "working" state. Without this, a poll that | ||
| // drains feedback and then releases leaves presence stuck on "working" — the chrome keeps | ||
| // Send disabled — until some future poll happens to attach, even though the agent already | ||
| // answered. See "SSE agent-presence returns to waiting after an agent reply". | ||
| // drains feedback and then releases leaves presence stuck on "working" even after the agent | ||
| // answers. Human sends remain available while working because the server queues them for the | ||
| // next poll. See "SSE agent-presence returns to waiting after an agent reply". | ||
| clearFeedbackDelivery(req.params.key, activePolls, deliveredFeedback, events); | ||
| res.json({ status: "sent" }); | ||
| } catch (error) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.