diff --git a/go.mod b/go.mod index 991a04ec..9d239de7 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/flanksource/captain -go 1.27.1 +go 1.27.4 require ( github.com/anthropics/anthropic-sdk-go v1.26.0 diff --git a/pkg/ai/provider/genkit/genkit.go b/pkg/ai/provider/genkit/genkit.go index 705d420a..86d486ca 100644 --- a/pkg/ai/provider/genkit/genkit.go +++ b/pkg/ai/provider/genkit/genkit.go @@ -258,6 +258,21 @@ func isSchemaMismatch(err error) bool { // chunks stay internal; their validated final value is carried by EventResult. func (p *Provider) ExecuteStream(ctx context.Context, req ai.Request) (<-chan ai.Event, error) { ch := make(chan ai.Event, 16) + emitCtx, cancelEmit := context.WithCancel(ctx) + var emitMu sync.Mutex + send := func(event ai.Event) error { + emitMu.Lock() + defer emitMu.Unlock() + if err := emitCtx.Err(); err != nil { + return err + } + select { + case ch <- event: + return nil + case <-emitCtx.Done(): + return emitCtx.Err() + } + } correlation := newToolEventCorrelation() cb := func(_ context.Context, chunk *gkai.ModelResponseChunk) error { events, err := chunkToEvents(chunk, p.cfg.Model.Name, correlation) @@ -268,10 +283,8 @@ func (p *Provider) ExecuteStream(ctx context.Context, req ai.Request) (<-chan ai if req.Prompt.HasSchema() && ev.Kind == ai.EventText { continue } - select { - case ch <- ev: - case <-ctx.Done(): - return ctx.Err() + if err := send(ev); err != nil { + return err } } return nil @@ -279,20 +292,23 @@ func (p *Provider) ExecuteStream(ctx context.Context, req ai.Request) (<-chan ai // emit lets in-process caller-tool execution publish tool_use/permission/ // tool_result events onto the same stream as the model's text chunks. emit := func(ev ai.Event) { - select { - case ch <- ev: - case <-ctx.Done(): - } + _ = send(ev) } turns := newTurnLog(p.provider, p.cfg.Model.Name, emit) opts, err := p.correlatedGenerateOptions(req, cb, emit, correlation, turns) if err != nil { + cancelEmit() return nil, err } go func() { - defer close(ch) resp, err := p.generate(ctx, opts) + // Genkit can return a tool error before sibling tools finish. Stop their + // callbacks and serialize the terminal event and closure with active sends. + cancelEmit() + emitMu.Lock() + defer emitMu.Unlock() + defer close(ch) usage := turns.total() costUSD := 0.0 if usage != nil { diff --git a/pkg/ai/provider/genkit/stream_lifecycle_ginkgo_test.go b/pkg/ai/provider/genkit/stream_lifecycle_ginkgo_test.go new file mode 100644 index 00000000..b3397275 --- /dev/null +++ b/pkg/ai/provider/genkit/stream_lifecycle_ginkgo_test.go @@ -0,0 +1,89 @@ +package genkit + +import ( + "context" + "sync" + "time" + + gkai "github.com/firebase/genkit/go/ai" + gk "github.com/firebase/genkit/go/genkit" + "github.com/flanksource/captain/pkg/ai" + "github.com/flanksource/captain/pkg/api" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Genkit stream lifecycle", func() { + It("closes a failed generation before a delayed sibling tool returns", func(ctx SpecContext) { + started, release, returned := make(chan struct{}), make(chan struct{}), make(chan struct{}) + releaseTool := sync.OnceFunc(func() { close(release) }) + DeferCleanup(releaseTool) + genkit := gk.Init(ctx) + modelRef := "test/failed-parallel-tools" + gk.DefineModel(genkit, modelRef, &gkai.ModelOptions{Supports: &gkai.ModelSupports{Tools: true, Multiturn: true}}, + func(modelCtx context.Context, _ *gkai.ModelRequest, stream gkai.ModelStreamCallback) (*gkai.ModelResponse, error) { + message := gkai.NewModelMessage( + gkai.NewToolRequestPart(&gkai.ToolRequest{Name: "lookup", Ref: "slow", Input: map[string]any{"city": "Cape Town"}}), + gkai.NewToolRequestPart(&gkai.ToolRequest{Name: "lookup", Ref: "invalid", Input: map[string]any{"city": 1}}), + ) + if err := stream(modelCtx, &gkai.ModelResponseChunk{Role: gkai.RoleModel, Content: message.Content}); err != nil { + return nil, err + } + return &gkai.ModelResponse{Message: message, FinishReason: gkai.FinishReasonStop}, nil + }) + provider := &Provider{ + cfg: ai.Config{Model: api.Model{Name: "failed-parallel-tools", Mode: api.ModeAPI}, Tools: []api.ToolDefinition{{ + Name: "lookup", DefaultPermission: api.ToolPolicyAllow, + InputSchema: map[string]any{"type": "object", "properties": map[string]any{"city": map[string]any{"type": "string"}}, "required": []string{"city"}}, + Handler: func(toolCtx context.Context, _ map[string]any) (any, error) { + defer close(returned) + close(started) + <-release + return nil, toolCtx.Err() + }, + }}}, + provider: ai.Anthropic, g: genkit, modelRef: modelRef, + } + stream, err := provider.ExecuteStream(ctx, api.Spec{Prompt: api.Prompt{User: "Look up both cities."}}) + Expect(err).NotTo(HaveOccurred()) + Eventually(started).Should(BeClosed()) + var terminal ai.Event + for event := range stream { + terminal = event + } + Expect(terminal.Kind).To(Equal(ai.EventError)) + Expect(terminal.Error).To(ContainSubstring("city")) + Expect(returned).NotTo(BeClosed()) + releaseTool() + Eventually(returned).Should(BeClosed()) + }, SpecTimeout(10*time.Second)) + + It("rejects callbacks after the event stream closes", func(ctx SpecContext) { + genkit := gk.Init(ctx) + modelRef := "test/late-stream-callback" + var callback gkai.ModelStreamCallback + gk.DefineModel(genkit, modelRef, &gkai.ModelOptions{}, + func(_ context.Context, _ *gkai.ModelRequest, stream gkai.ModelStreamCallback) (*gkai.ModelResponse, error) { + callback = stream + return &gkai.ModelResponse{Message: gkai.NewModelTextMessage("complete"), FinishReason: gkai.FinishReasonStop}, nil + }) + provider := &Provider{ + cfg: ai.Config{Model: api.Model{Name: "late-stream-callback", Mode: api.ModeAPI}}, + provider: ai.Anthropic, g: genkit, modelRef: modelRef, + } + stream, err := provider.ExecuteStream(ctx, api.Spec{Prompt: api.Prompt{User: "Return a response."}}) + Expect(err).NotTo(HaveOccurred()) + var events []ai.Event + for event := range stream { + events = append(events, event) + } + Expect(events[len(events)-1].Kind).To(Equal(ai.EventResult)) + Expect(callback).NotTo(BeNil()) + var lateErr error + Expect(func() { + lateErr = callback(ctx, &gkai.ModelResponseChunk{Role: gkai.RoleModel, Content: []*gkai.Part{gkai.NewTextPart("late")}}) + }).NotTo(Panic()) + Expect(lateErr).To(MatchError(context.Canceled)) + Expect(ctx.Err()).NotTo(HaveOccurred()) + }) +}) diff --git a/pkg/api/permission_capabilities.go b/pkg/api/permission_capabilities.go index bec5189b..72208490 100644 --- a/pkg/api/permission_capabilities.go +++ b/pkg/api/permission_capabilities.go @@ -333,6 +333,12 @@ func resources(mcpOff, skillsOn bool) map[ResourceKind]map[ResourceMode]Support } } +func apiResources() map[ResourceKind]map[ResourceMode]Support { + result := resources(false, false) + result[ResourceKindMCP][ResourceDisabled] = nativeNote("", "API runtimes do not load ambient MCP servers; caller tools remain governed by toolPolicy") + return result +} + // access builds the permissions.access row. full adds nothing anywhere, so it is // native everywhere a compiler exists; read-only is the cell that differs. func access(readOnly Support) map[AccessLevel]Support { @@ -466,38 +472,38 @@ var permissionCapabilities = map[Runtime]PermissionCapabilities{ RuntimeOf(Anthropic, ModeAPI): { Modes: unsupportedModes(), ToolPolicies: toolPolicies(noToolFilter(), callerTools(), noToolFilter()), - Resources: resources(false, false), + Resources: apiResources(), Access: apiAccess(), }, RuntimeOf(OpenAI, ModeAPI): { Modes: unsupportedModes(), ToolPolicies: toolPolicies(noToolFilter(), callerTools(), noToolFilter()), - Resources: resources(false, false), + Resources: apiResources(), Access: apiAccess(), }, RuntimeOf(Google, ModeAPI): { Modes: unsupportedModes(), ToolPolicies: toolPolicies(noToolFilter(), callerTools(), noToolFilter()), - Resources: resources(false, false), + Resources: apiResources(), Access: apiAccess(), }, RuntimeOf(DeepSeek, ModeAPI): { Modes: unsupportedModes(), ToolPolicies: toolPolicies(noToolFilter(), callerTools(), noToolFilter()), - Resources: resources(false, false), + Resources: apiResources(), Access: apiAccess(), }, RuntimeOf(Jev, ModeAPI): { Modes: unsupportedModes(), ToolPolicies: toolPolicies(noToolFilter(), noToolFilter(), noToolFilter()), - Resources: resources(false, false), + Resources: apiResources(), Access: apiAccess(), }, // bash runs a command, with no tools to filter and no resources to load. RuntimeOf(Bash, ModeAPI): { Modes: unsupportedModes(), ToolPolicies: toolPolicies(noToolFilter(), noToolFilter(), noToolFilter()), - Resources: resources(false, false), + Resources: apiResources(), }, // --- claude transports: the enum's native home. diff --git a/pkg/api/permission_capabilities_ginkgo_test.go b/pkg/api/permission_capabilities_ginkgo_test.go index 131d2d41..4826f3c7 100644 --- a/pkg/api/permission_capabilities_ginkgo_test.go +++ b/pkg/api/permission_capabilities_ginkgo_test.go @@ -226,10 +226,10 @@ var _ = Describe("PermissionCapabilities", func() { // The resource axis is asymmetric today and the table has to say so, because // the opposite direction is accepted and then dropped in both cases. Describe("resources", func() { - It("silences MCP only where a provider actually sends the empty server set", func() { + It("honours disabled MCP when ambient servers are absent or explicitly silenced", func() { for _, runtime := range api.AllRuntimes() { want := api.SupportUnsupported - if runtime.Mode != api.ModeAPI && runtime != (api.Runtime{Provider: "google", Mode: api.ModeCLI}) { + if runtime != (api.Runtime{Provider: "google", Mode: api.ModeCLI}) { want = api.SupportNative } Expect(api.PermissionCapabilitiesFor(runtime). diff --git a/pkg/api/resource_api_ginkgo_test.go b/pkg/api/resource_api_ginkgo_test.go new file mode 100644 index 00000000..2b248c41 --- /dev/null +++ b/pkg/api/resource_api_ginkgo_test.go @@ -0,0 +1,27 @@ +package api_test + +import ( + "github.com/flanksource/captain/pkg/api" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("API MCP resource policy", func() { + It("accepts disabled MCP without warnings on API runtimes", func() { + for _, runtime := range api.AllRuntimes() { + if runtime.Mode != api.ModeAPI { + continue + } + provider, ok := runtime.ModelProvider() + Expect(ok).To(BeTrue()) + warnings, err := api.ValidateRuntimeSpec(api.Spec{ + Model: api.Model{Provider: provider, Mode: api.ModeAPI}, + Permissions: api.Permissions{MCP: api.MCP{Disabled: true}}, + }) + Expect(err).NotTo(HaveOccurred(), runtime.String()) + Expect(warnings).To(BeEmpty(), runtime.String()) + Expect(api.PermissionCapabilitiesFor(runtime).ResourceSupport(api.ResourceKindMCP, api.ResourceEnabled).Kind). + To(Equal(api.SupportUnsupported), runtime.String()) + } + }) +}) diff --git a/pkg/cli/prompt_batch_run.go b/pkg/cli/prompt_batch_run.go index 56b885aa..0b490e3f 100644 --- a/pkg/cli/prompt_batch_run.go +++ b/pkg/cli/prompt_batch_run.go @@ -72,18 +72,30 @@ func launchAsyncBatch(ctx context.Context, launch promptLaunch) (PromptRunResult task.StartTask("finalize prompt batch", func(_ flanksourceContext.Context, t *task.Task) (PromptRunSummary, error) { succeeded, failed := 0, 0 + var failures error for i := range handles { summary, runErr := handles[i].GetResult() if runErr != nil || !summary.Success { failed++ + if runErr == nil { + runErr = fmt.Errorf("run %s did not succeed: %s", summary.RunID, summary.Error) + } + failures = errors.Join(failures, runErr) } else { succeeded++ } } reason := fmt.Sprintf("%d succeeded, %d failed", succeeded, failed) updatePromptSessionLifecycle(context.WithoutCancel(t.Context()), batch.ID, batchLifecycle(succeeded, failed), reason) + summary := PromptRunSummary{RunID: batch.ID.String(), SessionID: batch.ID.String(), Success: failed == 0} + if failures != nil { + summary.Error = failures.Error() + } + if launch.OnComplete != nil { + launch.OnComplete(summary) + } t.Success() - return PromptRunSummary{RunID: batch.ID.String(), Success: failed == 0}, nil + return summary, nil }, task.WithIdentity("prompt-batch-finalize-"+batch.ID.String()), task.WithSlots(0)) return result, nil } diff --git a/pkg/cli/prompt_completion_ginkgo_test.go b/pkg/cli/prompt_completion_ginkgo_test.go new file mode 100644 index 00000000..3899f103 --- /dev/null +++ b/pkg/cli/prompt_completion_ginkgo_test.go @@ -0,0 +1,35 @@ +package cli + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("prompt completion callbacks", func() { + DescribeTable("reports every terminal outcome exactly once outside the stream lock", + func(failed bool) { + stream := newRunStream() + stream.setRun(PromptRunFrame{RunID: "run-callback", SessionID: "shared-session", Model: "test-model"}) + var outcomes []PromptRunSummary + stream.onComplete = func(summary PromptRunSummary) { + stream.state() + outcomes = append(outcomes, summary) + } + if failed { + stream.fail("admission refused") + } else { + stream.complete(PromptRunSummary{Success: true}) + } + stream.fail("duplicate completion") + Expect(outcomes).To(HaveLen(1)) + Expect(outcomes[0].RunID).To(Equal("run-callback")) + Expect(outcomes[0].SessionID).To(Equal("shared-session")) + Expect(outcomes[0].Success).To(Equal(!failed)) + if failed { + Expect(outcomes[0].Error).To(Equal("admission refused")) + } + }, + Entry("success", false), + Entry("failure before a provider event", true), + ) +}) diff --git a/pkg/cli/prompt_invocation.go b/pkg/cli/prompt_invocation.go index 0fa433e0..2f5ab9f0 100644 --- a/pkg/cli/prompt_invocation.go +++ b/pkg/cli/prompt_invocation.go @@ -27,6 +27,13 @@ type PromptInvocation struct { Group *task.Group Name string OnEvent func(int, ai.Event) + // OnComplete receives the terminal outcome, including failures before the + // provider emits its first event. + OnComplete func(PromptRunSummary) + // SessionKey merges invocations into one conversation, including while its + // first provider session is still starting. Fresh explicitly replaces it. + SessionKey string + Fresh bool } // LaunchPrompt starts the prompt as an async run, the same run an HTTP @@ -47,7 +54,8 @@ func LaunchPrompt(ctx context.Context, invocation PromptInvocation) (PromptRunRe } logRuntimeWarnings(rendered.Resolution.Warnings) return launchRendered(ctx, promptLaunch{ - ID: invocation.Reference, Rendered: rendered, Group: invocation.Group, Name: invocation.Name, OnEvent: invocation.OnEvent, + ID: invocation.Reference, Rendered: rendered, Group: invocation.Group, Name: invocation.Name, OnEvent: invocation.OnEvent, OnComplete: invocation.OnComplete, + SessionKey: invocation.SessionKey, Fresh: invocation.Fresh, }) } diff --git a/pkg/cli/prompt_run.go b/pkg/cli/prompt_run.go index 1f6974e1..a003a3ad 100644 --- a/pkg/cli/prompt_run.go +++ b/pkg/cli/prompt_run.go @@ -68,12 +68,15 @@ func runPromptAction(ctx context.Context, id string, flags map[string]string) (P // id, whether it is a chat, and where its task goes (see PromptInvocation's // Group and Name). type promptLaunch struct { - ID string - Rendered PromptRenderResult - Chat bool - Group *task.Group - Name string - OnEvent func(int, ai.Event) + ID string + Rendered PromptRenderResult + Chat bool + Group *task.Group + Name string + OnEvent func(int, ai.Event) + OnComplete func(PromptRunSummary) + SessionKey string + Fresh bool } // runGroup is the group the launch's tasks join: the caller's, or a @@ -135,13 +138,28 @@ var executePromptRequestFunc = executePromptRequest // run handle (the serve/web-UI contract). func launchAsyncRun(launch promptLaunch) (PromptRunResult, error) { rendered, chat := launch.Rendered, launch.Chat + if launch.Fresh { + rendered.Input.SessionID, rendered.Config.SessionID = "", "" + launch.Rendered = rendered + } timeout, err := renderedTimeout(rendered) if err != nil { return PromptRunResult{}, err } runID := uuid.NewString() stream := promptRuns.create(runID) - stream.observer = launch.OnEvent + stream.resumeSession = rendered.Input.SessionID + var previous *runStream + if !chat { + previous = promptSessionQueue.enqueue(launch, stream) + } + stream.observer = func(iteration int, event ai.Event) { + promptSessionQueue.observeSession(stream, rendered, event) + if launch.OnEvent != nil { + launch.OnEvent(iteration, event) + } + } + stream.onComplete = launch.OnComplete capabilities := chatCapabilitiesFor(rendered.Provider, rendered.Mode) stream.setRun(PromptRunFrame{ RunID: runID, Status: "running", Chat: chat, Model: rendered.Model, @@ -149,6 +167,28 @@ func launchAsyncRun(launch promptLaunch) (PromptRunResult, error) { }) execute := func(_ flanksourceContext.Context, t *task.Task) (PromptRunSummary, error) { + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + stream.setCancel(cancel) + if err := continuePromptSession(ctx, previous, &rendered); err != nil { + return failRun(t, stream, err) + } + if err := ctx.Err(); err != nil { + return failRun(t, stream, err) + } + stream.mu.Lock() + stream.resumeSession = rendered.Input.SessionID + stream.mu.Unlock() + if previous != nil { + turn := task.StartTask("continue session", func(_ flanksourceContext.Context, child *task.Task) (PromptRunSummary, error) { + return runPromptStream(child, rendered, timeout, runID, stream, nil) + }, task.WithContext(ctx), task.WithGroup(task.GroupFromContext(t.Context())), task.WithModel(rendered.Model)) + summary, err := turn.GetResult() + if done, _, _ := stream.state(); err != nil && !done { + return failRun(t, stream, err) + } + return summary, err + } return runPromptStream(t, rendered, timeout, runID, stream, nil) } taskOptions := []task.Option{task.WithGroup(launch.runGroup(runID, "")), task.WithModel(rendered.Model), task.WithPrompt(rendered.Input.Prompt.User)} @@ -157,6 +197,9 @@ func launchAsyncRun(launch promptLaunch) (PromptRunResult, error) { promptChats.register(chatSession) chatSession.start(launch.taskName(""), taskOptions...) } else { + if previous != nil { + taskOptions = append(taskOptions, task.WithSlots(0)) + } task.StartTask(launch.taskName(""), execute, taskOptions...) } return PromptRunResult{ diff --git a/pkg/cli/prompt_run_stream.go b/pkg/cli/prompt_run_stream.go index 52700ba0..e4cb5edd 100644 --- a/pkg/cli/prompt_run_stream.go +++ b/pkg/cli/prompt_run_stream.go @@ -38,6 +38,11 @@ type runStream struct { errMsg string endedAt time.Time observer func(int, ai.Event) + onComplete func(PromptRunSummary) + finished chan struct{} + conversation *promptConversation + previous *runStream + resumeSession string // session is the summary of the session the run drives; see // prompt_run_stream_summary.go. session runSessionSummary @@ -45,6 +50,7 @@ type runStream struct { func newRunStream() *runStream { return &runStream{ + finished: make(chan struct{}), subs: map[chan session.Message]struct{}{}, eventSubs: map[chan runStreamEvent]struct{}{}, } @@ -149,8 +155,8 @@ func (s *runStream) fail(msg string) PromptRunSummary { func (s *runStream) finish(sum *PromptRunSummary, errMsg string) { s.mu.Lock() - defer s.mu.Unlock() if s.done { + s.mu.Unlock() return } s.done = true @@ -180,6 +186,10 @@ func (s *runStream) finish(sum *PromptRunSummary, errMsg string) { } } s.summary = sum + if sum != nil && sum.Success && s.resumeSession == "" { + s.resumeSession = sum.SessionID + } + close(s.finished) for ch := range s.subs { delete(s.subs, ch) close(ch) @@ -188,6 +198,11 @@ func (s *runStream) finish(sum *PromptRunSummary, errMsg string) { delete(s.eventSubs, ch) close(ch) } + callback := s.onComplete + s.mu.Unlock() + if callback != nil && sum != nil { + callback(*sum) + } } // setRunMetadata records the run's session id and model, and republishes the @@ -377,6 +392,7 @@ func prunePromptRuns(ctx context.Context, b *runBroker) { return case <-ticker.C: b.prune(15 * time.Minute) + promptSessionQueue.prune() } } } diff --git a/pkg/cli/prompt_session_queue.go b/pkg/cli/prompt_session_queue.go new file mode 100644 index 00000000..0105f6d5 --- /dev/null +++ b/pkg/cli/prompt_session_queue.go @@ -0,0 +1,107 @@ +package cli + +import ( + "context" + "fmt" + "sync" + + "github.com/flanksource/captain/pkg/ai" +) + +// sessionRunQueue admits one turn at a time into a shared provider session. +// Every turn keeps its own tools, permissions, callbacks and outcome. A fresh +// request replaces the conversation for subsequent turns without interrupting +// turns already delivered to the previous conversation. +type sessionRunQueue struct { + mu sync.Mutex + tails map[string]*promptConversation +} + +type promptConversation struct{ tail *runStream } + +var promptSessionQueue = sessionRunQueue{tails: map[string]*promptConversation{}} + +func (q *sessionRunQueue) enqueue(launch promptLaunch, stream *runStream) *runStream { + key := launch.SessionKey + if key == "" && launch.Rendered.Input.SessionID != "" { + key = launch.Rendered.Provider + "/" + launch.Rendered.Mode + "/" + launch.Rendered.Input.SessionID + } + if key == "" { + return nil + } + q.mu.Lock() + defer q.mu.Unlock() + conversation := q.tails[key] + if conversation == nil && launch.Rendered.Input.SessionID != "" { + conversation = q.tails[launch.Rendered.Provider+"/"+launch.Rendered.Mode+"/"+launch.Rendered.Input.SessionID] + } + if conversation == nil || launch.Fresh { + conversation = &promptConversation{} + } + previous := conversation.tail + stream.previous = previous + conversation.tail, stream.conversation = stream, conversation + q.tails[key] = conversation + return previous +} + +func continuePromptSession(ctx context.Context, previous *runStream, rendered *PromptRenderResult) error { + if previous == nil { + return nil + } + var cause string + for previous != nil { + select { + case <-ctx.Done(): + return ctx.Err() + case <-previous.finished: + } + previous.mu.Lock() + id := previous.resumeSession + cause = previous.errMsg + previous.mu.Unlock() + if id != "" { + rendered.Input.SessionID, rendered.Config.SessionID = id, id + return nil + } + previous = previous.previous + } + return fmt.Errorf("shared session did not start: %s", cause) +} + +// observeSession also registers the provider identity, so an explicit resume +// joins the conversation already running under a host's SessionKey. +func (q *sessionRunQueue) observeSession(stream *runStream, rendered PromptRenderResult, event ai.Event) { + if event.SessionID == "" { + return + } + stream.mu.Lock() + stream.resumeSession = event.SessionID + stream.mu.Unlock() + key := rendered.Provider + "/" + rendered.Mode + "/" + event.SessionID + q.mu.Lock() + defer q.mu.Unlock() + if stream.conversation != nil { + q.tails[key] = stream.conversation + } else if _, exists := q.tails[key]; !exists { + stream.conversation = &promptConversation{tail: stream} + q.tails[key] = stream.conversation + } +} + +func (q *sessionRunQueue) prune() { + q.mu.Lock() + defer q.mu.Unlock() + for key, conversation := range q.tails { + finished := true + for stream := conversation.tail; stream != nil; stream = stream.previous { + if done, _ := stream.terminalAt(); !done { + finished = false + break + } + } + if finished { + delete(q.tails, key) + } + } +} diff --git a/pkg/cli/prompt_session_queue_ginkgo_test.go b/pkg/cli/prompt_session_queue_ginkgo_test.go new file mode 100644 index 00000000..0e6661e9 --- /dev/null +++ b/pkg/cli/prompt_session_queue_ginkgo_test.go @@ -0,0 +1,50 @@ +package cli + +import ( + "context" + + "github.com/flanksource/captain/pkg/ai" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("shared session admission", func() { + It("joins an explicit resume and later grouped comments to the same conversation", func() { + queue := sessionRunQueue{tails: map[string]*promptConversation{}} + launch := promptLaunch{SessionKey: "shared", Rendered: PromptRenderResult{Provider: "anthropic", Mode: "agent"}} + first, second, third := newRunStream(), newRunStream(), newRunStream() + Expect(queue.enqueue(launch, first)).To(BeNil()) + queue.observeSession(first, launch.Rendered, ai.Event{SessionID: "provider-session"}) + resume := launch + resume.SessionKey, resume.Rendered.Input.SessionID = "", "provider-session" + Expect(queue.enqueue(resume, second)).To(BeIdenticalTo(first)) + Expect(queue.enqueue(launch, third)).To(BeIdenticalTo(second)) + }) + + It("keeps the conversation when a queued turn is cancelled before it starts", func() { + first, cancelled := newRunStream(), newRunStream() + cancelled.previous = first + cancelled.fail("stopped") + first.complete(PromptRunSummary{Success: true, SessionID: "provider-session"}) + rendered := PromptRenderResult{} + Expect(continuePromptSession(context.Background(), cancelled, &rendered)).To(Succeed()) + Expect(rendered.Input.SessionID).To(Equal("provider-session")) + Expect(rendered.Config.SessionID).To(Equal("provider-session")) + }) + + It("does not resume a Captain admission identity when the provider never started", func() { + first := newRunStream() + first.setRun(PromptRunFrame{SessionID: "captain-admission"}) + first.fail("admission refused") + rendered := PromptRenderResult{} + Expect(continuePromptSession(context.Background(), first, &rendered)).To(MatchError("shared session did not start: admission refused")) + Expect(rendered.Input.SessionID).To(BeEmpty()) + }) + + It("cancels a queued turn without waiting for the active turn", func() { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + rendered := PromptRenderResult{} + Expect(continuePromptSession(ctx, newRunStream(), &rendered)).To(MatchError(context.Canceled)) + }) +}) diff --git a/pkg/cli/prompt_shared_session_ginkgo_test.go b/pkg/cli/prompt_shared_session_ginkgo_test.go new file mode 100644 index 00000000..29a80abe --- /dev/null +++ b/pkg/cli/prompt_shared_session_ginkgo_test.go @@ -0,0 +1,116 @@ +package cli + +import ( + "context" + "errors" + "time" + + "github.com/flanksource/captain/pkg/api" + "github.com/flanksource/captain/pkg/promptrun" + "github.com/flanksource/captain/pkg/sandbox/substraterun" + "github.com/google/uuid" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +type sharedSessionSandbox struct { + wholeRunSandbox + started chan api.Spec + release chan struct{} + session string +} + +func (s *sharedSessionSandbox) RunRemote(ctx context.Context, in promptrun.Input, started func(api.Spec) error) (promptrun.Result, error) { + req := in.Resolved.Spec + if err := started(req); err != nil { + return promptrun.Result{}, err + } + s.started <- req + if req.Prompt.User == "refused comment" { + return promptrun.Result{}, errors.New("execution refused") + } + if req.Prompt.User == "first comment" { + select { + case <-ctx.Done(): + return promptrun.Result{}, ctx.Err() + case <-s.release: + } + } + return promptrun.Result{SessionID: s.session, Model: req.Model.Name, Response: &api.Response{Text: "done"}, Passed: true}, nil +} + +var _ = Describe("shared prompt sessions", func() { + var sandbox *sharedSessionSandbox + var rendered PromptRenderResult + var key string + BeforeEach(func() { + openCLIRecordDB() + sandbox = &sharedSessionSandbox{started: make(chan api.Spec, 4), release: make(chan struct{}), session: uuid.NewString()} + api.RegisterSandbox(api.SandboxSubstrate, func(api.SandboxConfig) (api.Sandbox, error) { return sandbox, nil }) + DeferCleanup(func() { api.RegisterSandbox(api.SandboxSubstrate, substraterun.Substrate) }) + DeferCleanup(func() { + select { + case <-sandbox.release: + default: + close(sandbox.release) + } + }) + model := api.Model{Name: "claude-sonnet-5", Provider: api.Anthropic, Mode: api.ModeCLI} + rendered = testRenderedPrompt(model) + rendered.Input.SetCwd(GinkgoT().TempDir()) + rendered.Input.Budget.Timeout = "10s" + rendered.Input.Sandbox = &api.SandboxRef{Mode: api.SandboxSubstrate, Backend: "test-shared"} + rendered.Config.SandboxSelection = &api.SandboxConfig{Kind: api.SandboxSubstrate, Name: "test-shared"} + rendered.Input.Prompt.User = "first comment" + key = uuid.NewString() + }) + + It("merges comments arriving during startup or execution into one conversation", func(ctx SpecContext) { + first, err := launchAsyncRun(promptLaunch{Rendered: rendered, SessionKey: key}) + Expect(err).NotTo(HaveOccurred()) + DeferCleanup(promptRuns.remove, first.RunID) + Eventually(sandbox.started).Should(Receive(HaveField("Prompt.User", "first comment"))) + rendered.Input.Prompt.User = "second comment" + second, err := launchAsyncRun(promptLaunch{Rendered: rendered, SessionKey: key}) + Expect(err).NotTo(HaveOccurred()) + DeferCleanup(promptRuns.remove, second.RunID) + Consistently(sandbox.started, 100*time.Millisecond).ShouldNot(Receive()) + close(sandbox.release) + Eventually(sandbox.started).Should(Receive(And(HaveField("SessionID", sandbox.session), HaveField("Prompt.User", "second comment")))) + for _, run := range []PromptRunResult{first, second} { + summary, err := AwaitPromptRun(ctx, run.RunID) + Expect(err).NotTo(HaveOccurred()) + Expect(summary.Success).To(BeTrue()) + Expect(summary.SessionID).To(Equal(sandbox.session)) + } + }) + + It("starts a separate conversation when fresh is explicit, even while the old one is active", func(ctx SpecContext) { + first, err := launchAsyncRun(promptLaunch{Rendered: rendered, SessionKey: key}) + Expect(err).NotTo(HaveOccurred()) + DeferCleanup(promptRuns.remove, first.RunID) + Eventually(sandbox.started).Should(Receive()) + rendered.Input.Prompt.User = "fresh comment" + rendered.Input.SessionID, rendered.Config.SessionID = sandbox.session, sandbox.session + fresh, err := launchAsyncRun(promptLaunch{Rendered: rendered, SessionKey: key, Fresh: true}) + Expect(err).NotTo(HaveOccurred()) + DeferCleanup(promptRuns.remove, fresh.RunID) + Eventually(sandbox.started).Should(Receive(HaveField("SessionID", ""))) + _, err = AwaitPromptRun(ctx, fresh.RunID) + Expect(err).NotTo(HaveOccurred()) + close(sandbox.release) + _, err = AwaitPromptRun(ctx, first.RunID) + Expect(err).NotTo(HaveOccurred()) + }) + + It("propagates a pre-provider execution failure through the launch callback", func(ctx SpecContext) { + rendered.Input.Prompt.User = "refused comment" + completed := make(chan PromptRunSummary, 1) + run, err := launchAsyncRun(promptLaunch{Rendered: rendered, SessionKey: key, OnComplete: func(summary PromptRunSummary) { completed <- summary }}) + Expect(err).NotTo(HaveOccurred()) + DeferCleanup(promptRuns.remove, run.RunID) + _, err = AwaitPromptRun(ctx, run.RunID) + Expect(err).To(MatchError(ContainSubstring("execution refused"))) + Eventually(completed).Should(Receive(And(HaveField("Success", false), HaveField("Error", ContainSubstring("execution refused"))))) + }) +}) diff --git a/pkg/cli/session_current.go b/pkg/cli/session_current.go new file mode 100644 index 00000000..7b0c6512 --- /dev/null +++ b/pkg/cli/session_current.go @@ -0,0 +1,77 @@ +package cli + +import ( + "context" + "fmt" + "os" + + clickyprocess "github.com/flanksource/clicky/process" +) + +type currentLiveRuntime struct { + getenv func(string) string + // ancestors are this process's ancestor PIDs, nearest first. + ancestors func(ctx context.Context) ([]int, error) + live func(ctx context.Context) ([]SessionRecord, error) +} + +// CurrentLiveSession is the live agent session this process runs inside, as +// RunSessionLive lists it: the nearest ancestor process that is a live +// session, else the live session the environment's session marker names. It +// is nil when the process runs inside no agent, and an error when a marker +// names a session that is not live. +func CurrentLiveSession(ctx context.Context) (*SessionRecord, error) { + return currentLiveSession(ctx, currentLiveRuntime{ + getenv: os.Getenv, + ancestors: func(ctx context.Context) ([]int, error) { + snapshot, err := clickyprocess.Discover(ctx, clickyprocess.SnapshotOptions{}) + if err != nil { + return nil, err + } + var pids []int + for _, process := range snapshot.Ancestors(os.Getpid()) { + pids = append(pids, process.PID) + } + return pids, nil + }, + live: func(ctx context.Context) ([]SessionRecord, error) { + result, err := RunSessionLive(ctx, SessionLiveOptions{Source: "all", All: true, Full: true}) + return result.Sessions, err + }, + }) +} + +func currentLiveSession(ctx context.Context, runtime currentLiveRuntime) (*SessionRecord, error) { + marker := detectEnvironmentSession(runtime.getenv) + if marker == nil { + return nil, nil + } + records, err := runtime.live(ctx) + if err != nil { + return nil, fmt.Errorf("listing live sessions: %w", err) + } + ancestors, err := runtime.ancestors(ctx) + if err != nil { + return nil, fmt.Errorf("listing this process's ancestors: %w", err) + } + byPID := map[int]SessionRecord{} + for _, record := range records { + if record.Live != nil && record.Live.PID > 0 { + byPID[record.Live.PID] = record + } + } + for _, pid := range ancestors { + if record, ok := byPID[pid]; ok { + return &record, nil + } + } + for _, record := range records { + if record.Live != nil && marker.SessionID != "" && record.ID == marker.SessionID { + return &record, nil + } + } + if marker.SessionID == "" { + return nil, fmt.Errorf("%s marks a %s session, but no ancestor of this process is a live session", marker.Marker, marker.Source) + } + return nil, fmt.Errorf("%s=%s names no live %s session", marker.Marker, marker.SessionID, marker.Source) +} diff --git a/pkg/cli/session_current_ginkgo_test.go b/pkg/cli/session_current_ginkgo_test.go new file mode 100644 index 00000000..7f0deb24 --- /dev/null +++ b/pkg/cli/session_current_ginkgo_test.go @@ -0,0 +1,53 @@ +package cli + +import ( + "context" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("CurrentLiveSession", func() { + const ( + shellPID = 4100 + claudePID = 4000 + codexPID = 3000 + ) + claude := SessionRecord{ID: "claude-live", Source: "claude", Live: &SessionLiveWire{PID: claudePID}} + codex := SessionRecord{ID: "codex-live", Source: "codex", Live: &SessionLiveWire{PID: codexPID}} + ended := SessionRecord{ID: "claude-ended", Source: "claude"} + + current := func(env map[string]string, ancestors []int) (*SessionRecord, error) { + return currentLiveSession(context.Background(), currentLiveRuntime{ + getenv: func(key string) string { return env[key] }, + ancestors: func(context.Context) ([]int, error) { return ancestors, nil }, + live: func(context.Context) ([]SessionRecord, error) { + return []SessionRecord{codex, claude, ended}, nil + }, + }) + } + + DescribeTable("resolves the session this process runs inside", + func(env map[string]string, ancestors []int, expected *SessionRecord) { + Expect(current(env, ancestors)).To(Equal(expected)) + }, + Entry("nearest live ancestor over an inherited outer marker", + map[string]string{"CODEX_THREAD_ID": "codex-live", "CLAUDECODE": "1"}, []int{shellPID, claudePID, codexPID}, &claude), + Entry("bare provider marker via the ancestor agent", + map[string]string{"CLAUDECODE": "1"}, []int{shellPID, claudePID}, &claude), + Entry("marker id when no ancestor is visible", + map[string]string{"CLAUDE_CODE_SESSION_ID": "claude-live"}, []int{shellPID}, &claude), + Entry("no agent marker", map[string]string{}, []int{shellPID, claudePID}, nil), + ) + + DescribeTable("fails when the marked session is not live", + func(env map[string]string, message string) { + _, err := current(env, []int{shellPID}) + Expect(err).To(MatchError(message)) + }, + Entry("ended session id", map[string]string{"CLAUDE_CODE_SESSION_ID": "claude-ended"}, + "CLAUDE_CODE_SESSION_ID=claude-ended names no live claude session"), + Entry("bare marker without a live ancestor", map[string]string{"CLAUDECODE": "1"}, + "CLAUDECODE marks a claude session, but no ancestor of this process is a live session"), + ) +})