fix: サブエージェント稼働中に result が届いても Completed に倒さない - #46
Merged
Merged
Conversation
Task ツールがバックグラウンド実行されると tool_result が即返り本体ターンが 続行するため、サブエージェント稼働中にトップレベルの result/success が先に 届く。これをそのまま completed 判定していたため、実際には作業継続中なのに バッジが「Completed」へ倒れる不具合があった。 - system/task_started / task_notification で稼働中タスク集合(activeTaskIds)を追跡 - result/success 受信時にタスクが残っていれば deferredResult に保留し running を維持 - 最後のタスクが settle した時点で保留結果を使って completed を確定(completeWith) - skip_transcript の雑務タスクはゲート対象外。late notification は failed を復活させない - 実データ採取のため spike に subagent シナリオを追加し、fixture でテスト SDK メッセージ形状は scripts/spike.ts の subagent シナリオで実採取し、 __fixtures__/session-subagent.jsonl として回帰テストに固定した。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
背景 / 不具合
作業がまだ完了していない(サブエージェントに委譲して継続中の)セッションが、
Completedと判定されてしまう不具合の修正。Task ツールのサブエージェントがバックグラウンド実行されると、その
tool_result("running in the background")が即座に返り本体ターンが続行するため、サブエージェント稼働中にトップレベルのresult/successが先に届く。従来のsdk-parse.tsはこのresult/successを無条件にcompletedへマッピングしていたため、実際には作業継続中なのにバッジが「Completed」に倒れていた。原因の裏取り(実データ)
CLAUDE.md の「SDK メッセージの形を想定で書かない」に従い、
scripts/spike.tsにsubagentシナリオを追加して実際の SDK ストリームを採取した。判明した事実:resultを出さない(result/successは最後にトップレベル1件だけ)。parent_tool_use_id(= Task の tool_use id)付きで届く(トップレベルはnull)。system/task_started→task_progress→task_notification(status: completed|failed|stopped)。result/successがtask_notificationより先に届きうる。修正内容
sdk-parse.tsで稼働中のサブエージェント/Task を追跡し、完了判定をゲートする:system/task_started/system/task_notificationで稼働中タスク集合activeTaskIdsを追跡。result/success受信時にタスクが残っていればcompletedにせず、結果をdeferredResultに保留してrunningを維持。task_notificationで settle し集合が空になった時点で、保留結果を使ってcompletedを確定(completeWith)。skip_transcriptの雑務タスクはゲート対象外。failed/aborted後の遅延task_notificationはセッションをcompletedに復活させない(status === 'running'ガード)。activeTaskIds/deferredResultはいずれも transient で永続しない。テスト計画
npm test(738 passed / core・utils カバレッジ 80% 以上、sdk-parse.ts97.2%)npm run typechecknpm run lint(Biome)__fixtures__/session-subagent.jsonl(フォアグラウンド settle → 正常completed)の回帰テストskip_transcript無視/failedを遅延 notification で復活させない/同一 id の重複task_startedを dedup補足
scripts/spike.ts subagentシナリオで再採取可能。docs/ARCHITECTURE.md/docs/TECH_NOTES.md)も更新済み。🤖 Generated with Claude Code