-
Notifications
You must be signed in to change notification settings - Fork 4
fix(sessions): 启动期用户消息可靠投递 #188
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
d0ba785
fix(sessions): reliably deliver startup user messages via event queue
jh0904 2151cf7
test(sessions): keep core startup delivery coverage only
jh0904 4029d81
refactor(sessions): simplify startup queue handoff after review
jh0904 112e515
fix(sessions): only queue startup messages for cloud environments
jh0904 6c573f5
fix(db): use stable tenant UUIDs for startup queue
jh0904 73b2d10
fix(sessions): reinject session history when activating code sessions
jh0904 7eef483
refactor(db): rename session startup window helper
arthur-zhang c79cbf5
refactor(db): simplify session event queue existence check
arthur-zhang 2c06800
refactor(db): simplify listSessionEventQueueIdentityRows
arthur-zhang ee3eee6
refactor(db): simplify ListSessionEventQueueItems event lookup
arthur-zhang e05b3c7
refactor(db): simplify delete session event queue query
arthur-zhang f55b5ab
Merge origin/main into codex/fix-session-event-reliable-delivery
jh0904 88033f5
fix(db): renumber session event queue migration after main UUID series
jh0904 3609ca3
revert: drop unrelated merge fixes from startup-delivery branch
jh0904 c5a63a4
refactor(db): simplify startup queue SQL and drop cloud filter docs
jh0904 1ccc6fc
Merge remote-tracking branch 'origin/main' into codex/fix-session-eve…
jh0904 080310a
refactor(sessions): move activation tx orchestration out of DB
jh0904 56afac5
refactor(sessions): clarify startup queue delivery and activation han…
jh0904 3fa3cbe
fix(sessions): atomically replay activation history
jh0904 c8c0ed0
Migrate session activation SQL to generated yourbatis mappers
arthur-zhang 5ea177c
refactor(db): migrate single code-session event append to yourbatis
jh0904 d3e211a
fix(ci): generate yourbatis mappers before Go typecheck
jh0904 f65c999
merge origin/main into session startup delivery branch
jh0904 c7c4c90
merge origin/main into session startup delivery branch
jh0904 8819d86
fix: reliably deliver startup session events
jh0904 4d76cdb
refactor(db): clarify code-session append naming and comments
jh0904 2b1bdee
fix(sessions): harden activation replay
jh0904 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # Session 启动期消息可靠投递 | ||
|
|
||
| ## 问题 | ||
|
|
||
| Runner 过去在 prepare 阶段读取一次 `session_events` 快照,随后才创建 sandbox 和 Code | ||
| Session。快照之后、Code Session 创建之前发送的消息虽然已经写入 `session_events`,但不会 | ||
| 进入 runtime 消费的 `code_session_inbound_events`。 | ||
|
|
||
| ## 设计 | ||
|
|
||
| `session_events` 是启动输入的唯一事实源,不增加临时 queue、watermark 或公开状态。 | ||
|
|
||
| Send Events 与 Code Session activation 都先锁同一条 Session 行: | ||
|
|
||
| - Send 通过 `DB.AppendSessionEvents` 锁 Session 并提交公开事件; | ||
| - activation 锁 Session 后读取完整公开历史,在同一事务中写 inbound 并将 Code Session 从 | ||
| `initializing` 切为 `active`。 | ||
|
|
||
| 因此只可能有两种顺序: | ||
|
|
||
| 1. Send 先提交,activation 随后读取到该事件并写入 inbound; | ||
| 2. activation 先提交,Send 随后由现有 active realtime 路径投递当前 batch。 | ||
|
|
||
| ```mermaid | ||
| sequenceDiagram | ||
| participant Client | ||
| participant Send as Send transaction | ||
| participant Session as sessions row | ||
| participant Events as session_events | ||
| participant Activate as Activation transaction | ||
| participant CS as code_sessions | ||
| participant Inbound as code_session_inbound_events | ||
|
|
||
| alt Send 先锁 Session | ||
| Send->>Session: SELECT FOR UPDATE | ||
| Activate->>Session: wait | ||
| Send->>Events: INSERT current batch | ||
| Send->>Send: COMMIT | ||
| Activate->>Session: acquire lock | ||
| Activate->>Events: read complete history | ||
| Activate->>Inbound: append forwardable events | ||
| Activate->>CS: initializing → active | ||
| Activate->>Activate: COMMIT | ||
| else Activate 先锁 Session | ||
| Activate->>Session: SELECT FOR UPDATE | ||
| Send->>Session: wait | ||
| Activate->>Events: read complete history | ||
| Activate->>Inbound: append forwardable events | ||
| Activate->>CS: initializing → active | ||
| Activate->>Activate: COMMIT | ||
| Send->>Session: acquire lock | ||
| Send->>Events: INSERT current batch | ||
| Send->>Inbound: active realtime delivery | ||
| end | ||
| ``` | ||
|
|
||
| ## 激活流程 | ||
|
|
||
| `Service.CreateManagedAgentCodeSession`: | ||
|
|
||
| 1. 创建 `initializing` Code Session; | ||
| 2. 写入 `initialize` inbound; | ||
| 3. 调用 `ActivateManagedAgentCodeSession`; | ||
| 4. activation 事务锁定 Session 和 initializing Code Session; | ||
| 5. 按 `created_at ASC, id ASC` 读取完整 `session_events`; | ||
| 6. 过滤并转换可转发事件,幂等写入 inbound; | ||
| 7. 将 Code Session 切为 `active` 并提交。 | ||
|
|
||
| 任一历史转换、inbound 写入或状态更新失败时,activation 事务整体回滚,Code Session 保持 | ||
| `initializing`。Deployment initial events 已经属于 `session_events`,无需单独交接路径。 | ||
|
|
||
| ## Realtime cutover | ||
|
|
||
| Send 提交公开事件后始终调用 `Service.QueuePublicSessionEvents`。该方法重新读取最新 Code | ||
| Session,只有 `status == active` 时才写 inbound;不存在或仍为 `initializing` 时直接返回。 | ||
|
|
||
| 如果 activation 恰好在公开事件提交后、realtime 检查前完成,同一事件可能同时出现在 | ||
| activation 历史和 realtime 尝试中;现有 inbound idempotency key 会保留一份,不会重复投递。 | ||
|
|
||
| ## 验收 | ||
|
|
||
| - Runner prepare 后、Code Session 创建前接受的消息最终进入 inbound; | ||
| - 启动期接受多条用户消息,activation 按公开历史顺序全部重放; | ||
| - activation 失败时不留下部分 inbound,也不切换为 active; | ||
| - activation 后的新 batch 只通过 realtime 路径追加; | ||
| - Deployment initial user messages 在 `initialize` 后按输入顺序进入 inbound。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.