carlos / platform Public

grouprollaws: cancel a session whose StartSession answer cannot attach

A StartSession answer with a session id but no token or stream URL left
the session open: the plugin never ran, and the error was not a timeout,
so the engine did not cancel it. Run now sends TerminateSession for it
before returning the error.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 8b1d94aefddbd151ada858e388e834e179740097 parent 0c50aaf
2 files changed, +34 −0
  • internal/grouprollaws/session.go +3 −0
  • internal/grouprollaws/session_test.go +31 −0
diff --git a/internal/grouprollaws/session.go b/internal/grouprollaws/session.go
index 377de5b13..651db6aeb 100644
--- a/internal/grouprollaws/session.go
+++ b/internal/grouprollaws/session.go
@@ -320,6 +320,9 @@ func (a *Adapter) Run(ctx context.Context, iid, verb string, args grouproll.BoxA
}
report(*out.SessionId)
if out.TokenValue == nil || out.StreamUrl == nil {
+ // No plugin can attach to it, so nothing will end it: cancel the
+ // session in place. A failed cancel leaves it to idle out.
+ _ = a.CancelSession(context.WithoutCancel(ctx), *out.SessionId)
return none, malformed()
}
responseJSON, err := compactJSON(startResponse{SessionId: *out.SessionId,
diff --git a/internal/grouprollaws/session_test.go b/internal/grouprollaws/session_test.go
index 6a52dfd5b..df35567dc 100644
--- a/internal/grouprollaws/session_test.go
+++ b/internal/grouprollaws/session_test.go
@@ -683,3 +683,34 @@ func contextWithCancelAfter(d time.Duration) (context.Context, context.CancelFun
time.AfterFunc(d, cancel)
return c, cancel
}
+
+// Fable P3-3: a StartSession answer with a session id but no token or
+// stream URL cannot be attached to, so the plugin never runs; the session
+// it opened is cancelled at once (TerminateSession) before the error.
+func TestSessionWithoutTokenOrStreamIsCancelled(t *testing.T) {
+ for name, answer := range map[string]string{
+ "no token": `{"SessionId":"` + testSession + `","StreamUrl":"` + testStream + `"}`,
+ "no stream": `{"SessionId":"` + testSession + `","TokenValue":"` + testToken + `"}`,
+ } {
+ t.Run(name, func(t *testing.T) {
+ dir := withFakePlugin(t, fakeControl{Body: statusBody})
+ a, rec := sessionAdapter(t, "eu-west-1")
+ rec.responses["StartSession"] = []canned{{status: 200, body: answer}}
+ var o observer
+ _, err := a.Run(ctx, testIID, "status", grouproll.BoxArgs{}, o.seen)
+ if u := uncertainKey(t, err); u.Key != "api" {
+ t.Fatalf("error %#v, want Uncertain api", u)
+ }
+ if !reflect.DeepEqual(o.ids, []string{testSession}) {
+ t.Fatalf("sessions reported %q", o.ids)
+ }
+ reqs := rec.requests()
+ if len(reqs) != 2 || reqs[1].Action != "TerminateSession" || reqs[1].JSON["SessionId"] != testSession {
+ t.Fatalf("requests %v, want StartSession then TerminateSession", rec.actions())
+ }
+ if _, err := os.Stat(filepath.Join(dir, "fakeplugin-record.json")); !errors.Is(err, os.ErrNotExist) {
+ t.Fatal("the plugin ran without a token")
+ }
+ })
+ }
+}