Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 25 additions & 9 deletions pkg/ai/provider/genkit/genkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -268,31 +283,32 @@ 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
}
// 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 {
Expand Down
89 changes: 89 additions & 0 deletions pkg/ai/provider/genkit/stream_lifecycle_ginkgo_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
})
18 changes: 12 additions & 6 deletions pkg/api/permission_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/permission_capabilities_ginkgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
27 changes: 27 additions & 0 deletions pkg/api/resource_api_ginkgo_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
})
})
14 changes: 13 additions & 1 deletion pkg/cli/prompt_batch_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
35 changes: 35 additions & 0 deletions pkg/cli/prompt_completion_ginkgo_test.go
Original file line number Diff line number Diff line change
@@ -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),
)
})
10 changes: 9 additions & 1 deletion pkg/cli/prompt_invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
})
}

Expand Down
Loading
Loading