grouprollaws: a cut session request never starts the plugin
When the observer cut the request (the engine does when it cannot journal the session id), Run still entered term.run, which started the plugin before it checked the context, so the remote command could run. Run now checks the context right after reporting the id; if cut, it ends the session by id and returns without launching. pty.run checks too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+41
−0
internal/grouprollaws/pty_unix.go+4 −0internal/grouprollaws/session.go+7 −0internal/grouprollaws/session_test.go+30 −0
Intent
Human
<task-notification> <task-id>bjpespqns</task-id> <tool-use-id>toolu_01MEZicryTPxMRTowY2wq36F</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/669506ff-23ec-45f1-9035-1d29593f2c37/tasks/bjpespqns.output</output-file> <status>completed</status> <summary>Background command "Wait for Luna and Astra" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/grouprollaws/pty_unix.go b/internal/grouprollaws/pty_unix.go| index 05c03ff15..95639a9b2 100644 |
| --- a/internal/grouprollaws/pty_unix.go |
| +++ b/internal/grouprollaws/pty_unix.go |
| @@ -124,6 +124,10 @@ func (o *owned) finish(kill bool) error { |
| // is never reaped earlier, even when it exits while a descendant still |
| // holds the terminal. |
| func (p *pty) run(ctx context.Context, path string, argv, env []string, end time.Time, report func(string)) ([]byte, error) { |
| + // A cut request starts no plugin. |
| + if err := ctx.Err(); err != nil { |
| + return nil, err |
| + } |
| slave := os.NewFile(uintptr(p.slave), "pty") |
| p.slave = -1 |
| proc, err := startOwned(&exec.Cmd{ |
diff --git a/internal/grouprollaws/session.go b/internal/grouprollaws/session.go| index 651db6aeb..ee101f4fe 100644 |
| --- a/internal/grouprollaws/session.go |
| +++ b/internal/grouprollaws/session.go |
| @@ -319,6 +319,13 @@ func (a *Adapter) Run(ctx context.Context, iid, verb string, args grouproll.BoxA |
| return none, malformed() |
| } |
| report(*out.SessionId) |
| + // The observer may have cut the request (the engine does when it |
| + // cannot journal the id). Then the plugin must not start: the remote |
| + // command runs only once the plugin attaches. End the session by id. |
| + if err := ctx.Err(); err != nil { |
| + _ = a.CancelSession(context.WithoutCancel(ctx), *out.SessionId) |
| + return none, err |
| + } |
| 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. |
diff --git a/internal/grouprollaws/session_test.go b/internal/grouprollaws/session_test.go| index df35567dc..b6be23c4f 100644 |
| --- a/internal/grouprollaws/session_test.go |
| +++ b/internal/grouprollaws/session_test.go |
| @@ -714,3 +714,33 @@ func TestSessionWithoutTokenOrStreamIsCancelled(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// Astra P1-1 (fix round 1): when the observer cuts the request (the |
| +// engine does when it cannot journal the session id), the plugin never |
| +// starts, so the remote command never runs; the session is ended by id |
| +// (one TerminateSession) and Run returns the context's error. |
| +func TestSessionObserverCancelNeverLaunchesPlugin(t *testing.T) { |
| + dir := withFakePlugin(t, fakeControl{Body: statusBody}) |
| + a, rec := sessionAdapter(t, "eu-west-1") |
| + c, cut := context.WithCancel(ctx) |
| + defer cut() |
| + var ids []string |
| + _, err := a.Run(c, testIID, "status", grouproll.BoxArgs{}, func(id string) { |
| + ids = append(ids, id) |
| + cut() |
| + }) |
| + if !errors.Is(err, context.Canceled) { |
| + t.Fatalf("Run = %v, want context.Canceled", err) |
| + } |
| + if !reflect.DeepEqual(ids, []string{testSession}) { |
| + t.Fatalf("sessions reported %q", ids) |
| + } |
| + if _, err := os.Stat(filepath.Join(dir, "fakeplugin-record.json")); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatal("the plugin started after the observer cut the request") |
| + } |
| + reqs := rec.requests() |
| + if len(reqs) != 2 || reqs[0].Action != "StartSession" || reqs[1].Action != "TerminateSession" || |
| + reqs[1].JSON["SessionId"] != testSession { |
| + t.Fatalf("requests %v, want StartSession then one TerminateSession", rec.actions()) |
| + } |
| +} |