feat(runs): the in-process agent loop, checkpointed as run steps, and the investigate workflow (#1977, #1889)
The agent model is an organisation setting in the new governance module. Every model and tool call is a journal step, so a killed run resumes without repeating either. Agents are offered connector lookups and read-only actions only, until judgement exists.
This commit is contained in:
16 files changed
+795
-23
No files matched your search
@@ -8,6 +8,8 @@ import (
|
||||
json "encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
@@ -79,18 +81,43 @@ type Invocation struct {
|
||||
}
|
||||
|
||||
type action struct {
|
||||
effect Effect
|
||||
idempotent bool
|
||||
input *jsonschema.Resolved
|
||||
effect Effect
|
||||
idempotent bool
|
||||
input *jsonschema.Resolved
|
||||
description string
|
||||
schema jsontext.Value
|
||||
}
|
||||
|
||||
type lookup struct {
|
||||
input *jsonschema.Resolved
|
||||
description string
|
||||
schema jsontext.Value
|
||||
}
|
||||
|
||||
// Declaration is one lookup or action a connector declared, as an agent is
|
||||
// offered it.
|
||||
type Declaration struct {
|
||||
Connector string `json:"connector"`
|
||||
Kind string `json:"kind"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Schema jsontext.Value `json:"schema"`
|
||||
ReadOnly bool `json:"read_only"`
|
||||
}
|
||||
|
||||
// Declaration kinds.
|
||||
const (
|
||||
KindLookup = "lookup"
|
||||
KindAction = "action"
|
||||
)
|
||||
|
||||
// Connector is a running connector and what it declared.
|
||||
type Connector struct {
|
||||
name string
|
||||
client pb.ConnectorServiceClient
|
||||
events map[string]*jsonschema.Resolved
|
||||
actions map[string]action
|
||||
lookups map[string]*jsonschema.Resolved
|
||||
lookups map[string]lookup
|
||||
}
|
||||
|
||||
// open configures a connector and reads its declaration.
|
||||
@@ -109,7 +136,7 @@ func open(ctx context.Context, name string, client pb.ConnectorServiceClient, se
|
||||
name: name, client: client,
|
||||
events: map[string]*jsonschema.Resolved{},
|
||||
actions: map[string]action{},
|
||||
lookups: map[string]*jsonschema.Resolved{},
|
||||
lookups: map[string]lookup{},
|
||||
}
|
||||
return c, c.load(desc)
|
||||
}
|
||||
@@ -127,18 +154,39 @@ func (c *Connector) load(desc *pb.DescribeResponse) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("connector %q: action %q: %w", c.name, a.GetName(), err)
|
||||
}
|
||||
c.actions[a.GetName()] = action{effect: effectFrom(a.GetEffect()), idempotent: a.GetIdempotent(), input: s}
|
||||
c.actions[a.GetName()] = action{
|
||||
effect: effectFrom(a.GetEffect()), idempotent: a.GetIdempotent(), input: s,
|
||||
description: a.GetDescription(), schema: schemaOrEmpty(a.GetInputSchema()),
|
||||
}
|
||||
}
|
||||
for _, l := range desc.GetLookups() {
|
||||
s, err := compile(l.GetInputSchema())
|
||||
if err != nil {
|
||||
return fmt.Errorf("connector %q: lookup %q: %w", c.name, l.GetName(), err)
|
||||
}
|
||||
c.lookups[l.GetName()] = s
|
||||
c.lookups[l.GetName()] = lookup{input: s, description: l.GetDescription(), schema: schemaOrEmpty(l.GetInputSchema())}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Declarations lists the connector's lookups and actions in name order.
|
||||
func (c *Connector) Declarations() []Declaration {
|
||||
out := make([]Declaration, 0, len(c.lookups)+len(c.actions))
|
||||
for _, name := range slices.Sorted(maps.Keys(c.lookups)) {
|
||||
l := c.lookups[name]
|
||||
out = append(out, Declaration{
|
||||
Connector: c.name, Kind: KindLookup, Name: name, Description: l.description, Schema: l.schema, ReadOnly: true,
|
||||
})
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(c.actions)) {
|
||||
a := c.actions[name]
|
||||
out = append(out, Declaration{
|
||||
Connector: c.name, Kind: KindAction, Name: name, Description: a.description, Schema: a.schema, ReadOnly: a.effect.ReadOnly,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Name is the connector's accepted name.
|
||||
func (c *Connector) Name() string { return c.name }
|
||||
|
||||
@@ -213,11 +261,11 @@ func (c *Connector) Invoke[T any](ctx context.Context, inv Invocation) (T, error
|
||||
// false when the connector found nothing, which callers report as a finding.
|
||||
func (c *Connector) Lookup[T any](ctx context.Context, name string, input jsontext.Value) (T, bool, error) {
|
||||
var zero T
|
||||
schema, ok := c.lookups[name]
|
||||
l, ok := c.lookups[name]
|
||||
if !ok {
|
||||
return zero, false, fmt.Errorf("connector %q: lookup %q: %w", c.name, name, ErrUnknownName)
|
||||
}
|
||||
if err := validate(schema, input); err != nil {
|
||||
if err := validate(l.input, input); err != nil {
|
||||
return zero, false, fmt.Errorf("connector %q: lookup %q: %w", c.name, name, err)
|
||||
}
|
||||
resp, err := c.client.Lookup(ctx, &pb.LookupRequest{Lookup: name, Input: input})
|
||||
@@ -256,6 +304,13 @@ func effectFrom(e *pb.Effect) Effect {
|
||||
return Effect{ReadOnly: e.GetReadOnly(), Reversibility: Reversibility(e.GetReversibility()), Summary: e.GetSummary()}
|
||||
}
|
||||
|
||||
func schemaOrEmpty(raw []byte) jsontext.Value {
|
||||
if len(raw) == 0 {
|
||||
return jsontext.Value(`{}`)
|
||||
}
|
||||
return jsontext.Value(raw)
|
||||
}
|
||||
|
||||
func compile(raw []byte) (*jsonschema.Resolved, error) {
|
||||
if len(raw) == 0 {
|
||||
raw = []byte(`{}`)
|
||||
|
||||
@@ -6,8 +6,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -64,6 +66,18 @@ func (m *Module) Get(name string) (*Connector, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// Declarations lists every running connector's lookups and actions, ordered
|
||||
// by connector and then by name.
|
||||
func (m *Module) Declarations() []Declaration {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
var out []Declaration
|
||||
for _, name := range slices.Sorted(maps.Keys(m.running)) {
|
||||
out = append(out, m.running[name].Declarations()...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Ready reports connectors that failed to start or whose process has exited.
|
||||
func (m *Module) Ready(context.Context) error {
|
||||
m.mu.RLock()
|
||||
|
||||
Reference in new issue
Block a user