Session Control and Long Tasks: Stopping, Retrying, Branching, and Subagents
Turn one-shot question and answer into a session people can actually work in: stop mid-run, retry after failure, edit and resend the last turn, branch from any point. Then handle progress for long tasks, subagent visualization, and how to resume a cut stream without producing duplicate messages.
Today's Goals
- Implement stopping with AbortController, and say what the server still has to do after the client aborts
- Implement edit-and-resend and session branching, and explain why a branch tree fits agent sessions better than linear history
- Handle resumption after a cut stream, and explain why deduplication keys on message id rather than content
Everything so far has lived within a single run. Today we handle the relationships between runs, which is what separates a session from a one-shot question. When you finish, scroll back up and tick the three goals off.
Plain-Language Walkthrough
Stopping: cutting the interpreter's mic while the venue stays powered
If the organizers call a halt, the interpreter mutes immediately. But that only means you stop hearing them — the speaker is still talking, the recorders are still rolling, the venue is still burning electricity.
AbortController is that mute button.
const controller = new AbortController()
await fetch('/api/agent', { signal: controller.signal, ... })
// user clicks stop
controller.abort()abort() closes the client's side of the connection. The server's run keeps going, and tokens keep burning.
This matters directly for cost: on a three-minute run where the user stops at ten seconds, an abort-only implementation still pays for the remaining two minutes and fifty seconds.
Interview questions
The user clicks stop. Is calling AbortController enough on the frontend? What does the server still need to do?用户点了停止,前端调用中止控制器就够了吗?服务端还需要做什么?
Common in ChinaCommon overseasIntermediate#abort-controller#cost-control#streaming🔒 Unlock this chapter to see the answer keys.
Should conversation history be an array or a tree? How do edit-and-resend and branching affect that choice?会话历史用数组还是用树?编辑重发和分支这两个需求会怎么影响你的选择?
Common in ChinaCommon overseasIntermediate#data-structure#session-management#ux🔒 Unlock this chapter to see the answer keys.
A stream breaks midway and the client reconnects to resume. How do you prevent duplicate messages?流断在一半,前端重连续播,怎么保证不出现重复的消息?
Common in ChinaCommon overseasDeep dive#resumption#deduplication#streaming🔒 Unlock this chapter to see the answer keys.
An agent runs a three-minute task. How do you present progress in the UI?Agent 跑一个三分钟的长任务,界面上你会怎么表现进度?
Common in ChinaCommon overseasIntermediate#long-running-tasks#progress-ui#ux🔒 Unlock this chapter to see the answer keys.
