-
Notifications
You must be signed in to change notification settings - Fork 1
Better stop flow for AI bridge #101
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
Open
batuhan
wants to merge
7
commits into
main
Choose a base branch
from
batuhan/stop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9c8e01d
Add user stop handling and stop metadata
batuhan a691f3c
Refactor queue checks, abort text, and streaming errors
batuhan 5d7e0b2
Make stop metadata atomic and thread-safe
batuhan ed72a84
Centralize ack removal and simplify queue logic
batuhan 02c907a
Update pending_queue.go
batuhan a242d26
Propagate context to queue/stop ops and refine stop handling
batuhan bf332af
Fix pending queue locking and lastItem updates
batuhan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| package ai | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "maunium.net/go/mautrix/bridgev2" | ||
| "maunium.net/go/mautrix/bridgev2/database" | ||
| "maunium.net/go/mautrix/id" | ||
|
|
||
| bridgesdk "github.com/beeper/agentremote/sdk" | ||
| ) | ||
|
|
||
| func TestResolveUserStopPlanRoomWideWithoutReply(t *testing.T) { | ||
| oc := &AIClient{} | ||
| portal := &bridgev2.Portal{Portal: &database.Portal{MXID: "!room:test"}} | ||
| req := userStopRequest{Portal: portal, RequestedVia: "command"} | ||
|
|
||
| plan := oc.resolveUserStopPlan(req) | ||
| if plan.Kind != stopPlanKindRoomWide { | ||
| t.Fatalf("expected room-wide stop, got %#v", plan) | ||
| } | ||
| if plan.TargetKind != "all" || plan.Scope != "room" { | ||
| t.Fatalf("unexpected room-wide stop plan: %#v", plan) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveUserStopPlanMatchesActiveReplyTargets(t *testing.T) { | ||
| roomID := id.RoomID("!room:test") | ||
| oc := &AIClient{ | ||
| activeRoomRuns: map[id.RoomID]*roomRunState{ | ||
| roomID: { | ||
| sourceEvent: id.EventID("$user"), | ||
| initialEvent: id.EventID("$assistant"), | ||
| }, | ||
| }, | ||
| } | ||
| portal := &bridgev2.Portal{Portal: &database.Portal{MXID: roomID}} | ||
|
|
||
| placeholderPlan := oc.resolveUserStopPlan(userStopRequest{ | ||
| Portal: portal, | ||
| ReplyTo: id.EventID("$assistant"), | ||
| }) | ||
| if placeholderPlan.Kind != stopPlanKindActive || placeholderPlan.TargetKind != "placeholder_event" { | ||
| t.Fatalf("expected placeholder-targeted active stop, got %#v", placeholderPlan) | ||
| } | ||
|
|
||
| sourcePlan := oc.resolveUserStopPlan(userStopRequest{ | ||
| Portal: portal, | ||
| ReplyTo: id.EventID("$user"), | ||
| }) | ||
| if sourcePlan.Kind != stopPlanKindActive || sourcePlan.TargetKind != "source_event" { | ||
| t.Fatalf("expected source-targeted active stop, got %#v", sourcePlan) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveUserStopPlanSpeculativelyReturnsQueued(t *testing.T) { | ||
| oc := &AIClient{} | ||
| portal := &bridgev2.Portal{Portal: &database.Portal{MXID: "!room:test"}} | ||
|
|
||
| plan := oc.resolveUserStopPlan(userStopRequest{ | ||
| Portal: portal, | ||
| ReplyTo: id.EventID("$unknown"), | ||
| }) | ||
| if plan.Kind != stopPlanKindQueued || plan.TargetKind != "source_event" { | ||
| t.Fatalf("expected speculative queued stop plan, got %#v", plan) | ||
| } | ||
| } | ||
|
|
||
| func TestExecuteUserStopPlanFallsBackToNoMatch(t *testing.T) { | ||
| oc := &AIClient{} | ||
| portal := &bridgev2.Portal{Portal: &database.Portal{MXID: "!room:test"}} | ||
|
|
||
| result := oc.executeUserStopPlan(context.Background(), userStopRequest{ | ||
| Portal: portal, | ||
| }, userStopPlan{ | ||
| Kind: stopPlanKindQueued, | ||
| Scope: "turn", | ||
| TargetKind: "source_event", | ||
| TargetEventID: id.EventID("$nonexistent"), | ||
| }) | ||
| if result.Plan.Kind != stopPlanKindNoMatch { | ||
| t.Fatalf("expected no-match fallback, got %#v", result.Plan) | ||
| } | ||
| if result.QueuedStopped != 0 { | ||
| t.Fatalf("expected zero queued stopped, got %d", result.QueuedStopped) | ||
| } | ||
| } | ||
|
|
||
| func TestExecuteUserStopPlanRemovesOnlyTargetedQueuedTurn(t *testing.T) { | ||
| roomID := id.RoomID("!room:test") | ||
| oc := &AIClient{ | ||
| pendingQueues: map[id.RoomID]*pendingQueue{ | ||
| roomID: { | ||
| items: []pendingQueueItem{ | ||
| {pending: pendingMessage{SourceEventID: id.EventID("$one")}}, | ||
| {pending: pendingMessage{SourceEventID: id.EventID("$two")}}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| portal := &bridgev2.Portal{Portal: &database.Portal{MXID: roomID}} | ||
|
|
||
| result := oc.executeUserStopPlan(context.Background(), userStopRequest{ | ||
| Portal: portal, | ||
| }, userStopPlan{ | ||
| Kind: stopPlanKindQueued, | ||
| Scope: "turn", | ||
| TargetKind: "source_event", | ||
| TargetEventID: id.EventID("$one"), | ||
| }) | ||
| if result.QueuedStopped != 1 { | ||
| t.Fatalf("expected one queued turn to stop, got %#v", result) | ||
| } | ||
| snapshot := oc.getQueueSnapshot(roomID) | ||
| if snapshot == nil || len(snapshot.items) != 1 { | ||
| t.Fatalf("expected one queued item to remain, got %#v", snapshot) | ||
| } | ||
| if got := snapshot.items[0].pending.sourceEventID(); got != id.EventID("$two") { | ||
| t.Fatalf("expected remaining queued event $two, got %q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestExecuteUserStopPlanActiveNoOpFallsBackToNoMatch(t *testing.T) { | ||
| roomID := id.RoomID("!room:test") | ||
| oc := &AIClient{ | ||
| activeRoomRuns: map[id.RoomID]*roomRunState{ | ||
| roomID: { | ||
| sourceEvent: id.EventID("$user"), | ||
| }, | ||
| }, | ||
| } | ||
| portal := &bridgev2.Portal{Portal: &database.Portal{MXID: roomID}} | ||
|
|
||
| result := oc.executeUserStopPlan(context.Background(), userStopRequest{ | ||
| Portal: portal, | ||
| ReplyTo: id.EventID("$user"), | ||
| }, userStopPlan{ | ||
| Kind: stopPlanKindActive, | ||
| Scope: "turn", | ||
| TargetKind: "source_event", | ||
| TargetEventID: id.EventID("$user"), | ||
| }) | ||
| if result.Plan.Kind != stopPlanKindNoMatch { | ||
| t.Fatalf("expected no-match fallback for no-op active stop, got %#v", result.Plan) | ||
| } | ||
| if result.ActiveStopped { | ||
| t.Fatalf("expected active stop to report false, got %#v", result) | ||
| } | ||
| } | ||
|
|
||
| func TestBuildStreamUIMessageIncludesStopMetadata(t *testing.T) { | ||
| oc := &AIClient{} | ||
| conv := bridgesdk.NewConversation[*AIClient, *Config](context.Background(), nil, nil, bridgev2.EventSender{}, nil, nil) | ||
| turn := conv.StartTurn(context.Background(), nil, &bridgesdk.SourceRef{EventID: "$user", SenderID: "@user:test"}) | ||
| turn.SetID("turn-stop") | ||
| state := &streamingState{ | ||
| turn: turn, | ||
| finishReason: "stop", | ||
| responseID: "resp_123", | ||
| completedAtMs: 1, | ||
| } | ||
| state.stop.Store(&assistantStopMetadata{ | ||
| Reason: "user_stop", | ||
| Scope: "turn", | ||
| TargetKind: "source_event", | ||
| TargetEventID: "$user", | ||
| RequestedByEventID: "$stop", | ||
| RequestedVia: "command", | ||
| }) | ||
|
|
||
| ui := oc.buildStreamUIMessage(state, nil, nil) | ||
| metadata, ok := ui["metadata"].(map[string]any) | ||
| if !ok { | ||
| t.Fatalf("expected metadata map, got %T", ui["metadata"]) | ||
| } | ||
| stop, ok := metadata["stop"].(map[string]any) | ||
| if !ok { | ||
| t.Fatalf("expected nested stop metadata, got %#v", metadata["stop"]) | ||
| } | ||
| if stop["reason"] != "user_stop" || stop["requested_via"] != "command" { | ||
| t.Fatalf("unexpected stop metadata: %#v", stop) | ||
| } | ||
| if metadata["response_status"] != "cancelled" { | ||
| t.Fatalf("expected cancelled response status for stopped turn, got %#v", metadata["response_status"]) | ||
| } | ||
| } |
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.