From b5d60fae209d7b371feba1deda7bb665affcfdb4 Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Mon, 12 Oct 2026 16:04:47 +0100 Subject: [PATCH 01/10] agent simulate: read simulator flow steps from scenarios.yaml Read the top-level `steps` map and the per-scenario `start` and `max_duration` keys, and send them in the ScenarioGroup. Each step is decoded with protojson, so field names, enum names, durations, and unknown-key errors match the server's parser. Check only clear user errors locally: a scenario must set exactly one of `start` and `instructions`, and `start` must name a step or a built-in. The server validates the rest at CreateSimulationRun. Bump github.com/livekit/protocol to b440b893 for the new fields. Addresses AGT-3725 --- cmd/lk/simulate.go | 89 ++++++++++- cmd/lk/simulate_scenario_ids_test.go | 222 +++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 +- 4 files changed, 312 insertions(+), 5 deletions(-) diff --git a/cmd/lk/simulate.go b/cmd/lk/simulate.go index 97b59ca89..e90bce242 100644 --- a/cmd/lk/simulate.go +++ b/cmd/lk/simulate.go @@ -43,7 +43,9 @@ import ( "github.com/livekit/protocol/utils" lksdk "github.com/livekit/server-sdk-go/v2" "github.com/livekit/server-sdk-go/v2/pkg/cloudagents" + "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/durationpb" ) func init() { @@ -352,14 +354,32 @@ func scenarioGroupToYAML(group *livekit.ScenarioGroup) ([]byte, error) { if f.ID == "" { f.ID = utils.NewGuid(utils.ScenarioGroupPrefix) } + for name, step := range group.GetSteps() { + v, err := protoToYAMLValue(step) + if err != nil { + return nil, fmt.Errorf("failed to encode step %q: %w", name, err) + } + if f.Steps == nil { + f.Steps = map[string]any{} + } + f.Steps[name] = v + } for _, s := range group.GetScenarios() { ys := yamlScenario{ ID: s.GetId(), Label: s.GetLabel(), Instructions: s.GetInstructions(), + Start: s.GetStart(), AgentExpectations: s.GetAgentExpectations(), Tags: s.GetTags(), } + if s.GetMaxDuration() != nil { + v, err := protoToYAMLValue(s.GetMaxDuration()) + if err != nil { + return nil, fmt.Errorf("failed to encode max_duration for scenario %q: %w", s.GetLabel(), err) + } + ys.MaxDuration, _ = v.(string) + } if ys.ID == "" { ys.ID = utils.NewGuid(utils.ScenarioPrefix) } @@ -376,17 +396,21 @@ func scenarioGroupToYAML(group *livekit.ScenarioGroup) ([]byte, error) { } // scenariosFile mirrors a scenarios.yaml; `userdata` is a nested mapping here -// and JSON-encoded into the proto's string field. +// and JSON-encoded into the proto's string field. Each `steps` entry is a +// SimulatorStep in protojson form. type scenariosFile struct { ID string `yaml:"id"` Name string `yaml:"name"` + Steps map[string]any `yaml:"steps,omitempty"` Scenarios []yamlScenario `yaml:"scenarios"` } type yamlScenario struct { ID string `yaml:"id"` Label string `yaml:"label"` - Instructions string `yaml:"instructions"` + Instructions string `yaml:"instructions,omitempty"` + Start string `yaml:"start,omitempty"` + MaxDuration string `yaml:"max_duration,omitempty"` AgentExpectations string `yaml:"agent_expectations"` Tags map[string]string `yaml:"tags"` Userdata map[string]any `yaml:"userdata"` @@ -454,6 +478,21 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { return nil, fmt.Errorf("scenarios file has no id") } group := &livekit.ScenarioGroup{Id: f.ID, Name: f.Name} + names := make([]string, 0, len(f.Steps)) + for name := range f.Steps { + names = append(names, name) + } + sort.Strings(names) + for _, name := range names { + step := &livekit.SimulatorStep{} + if err := yamlValueToProto(f.Steps[name], step); err != nil { + return nil, fmt.Errorf("step %q: %w", name, err) + } + if group.Steps == nil { + group.Steps = map[string]*livekit.SimulatorStep{} + } + group.Steps[name] = step + } seen := map[string]bool{} for _, s := range f.Scenarios { if s.ID == "" { @@ -463,6 +502,19 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { return nil, fmt.Errorf("duplicate scenario id %q", s.ID) } seen[s.ID] = true + if (s.Start == "") == (s.Instructions == "") { + return nil, fmt.Errorf("scenario %q: set exactly one of `start` and `instructions`", s.Label) + } + if s.Start != "" && group.Steps[s.Start] == nil && !builtinSimulatorSteps[s.Start] { + return nil, fmt.Errorf("scenario %q: start %q is not a step", s.Label, s.Start) + } + var maxDuration *durationpb.Duration + if s.MaxDuration != "" { + maxDuration = &durationpb.Duration{} + if err := yamlValueToProto(s.MaxDuration, maxDuration); err != nil { + return nil, fmt.Errorf("scenario %q: max_duration: %w", s.Label, err) + } + } var userdata string if len(s.Userdata) > 0 { b, err := json.Marshal(s.Userdata) @@ -475,6 +527,8 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { Id: s.ID, Label: s.Label, Instructions: s.Instructions, + Start: s.Start, + MaxDuration: maxDuration, AgentExpectations: s.AgentExpectations, Tags: s.Tags, Userdata: userdata, @@ -483,6 +537,37 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { return group, nil } +// builtinSimulatorSteps are the steps the server provides; a scenario may start at one. +var builtinSimulatorSteps = map[string]bool{ + "livekit/hangup": true, + "livekit/voicemail": true, + "livekit/unavailable": true, +} + +// yamlValueToProto decodes a YAML value through its JSON form with protojson, +// so field names, enum names, durations, and unknown-key errors match the +// server's parser. +func yamlValueToProto(v any, m proto.Message) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + return protojson.Unmarshal(b, m) +} + +// protoToYAMLValue is the inverse of yamlValueToProto. +func protoToYAMLValue(m proto.Message) (any, error) { + b, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(m) + if err != nil { + return nil, err + } + var v any + if err := json.Unmarshal(b, &v); err != nil { + return nil, err + } + return v, nil +} + func generateAgentName() string { const chars = "abcdefghijklmnopqrstuvwxyz0123456789" b := make([]byte, 8) diff --git a/cmd/lk/simulate_scenario_ids_test.go b/cmd/lk/simulate_scenario_ids_test.go index 73f8b1142..fa0323f9f 100644 --- a/cmd/lk/simulate_scenario_ids_test.go +++ b/cmd/lk/simulate_scenario_ids_test.go @@ -20,9 +20,12 @@ import ( "regexp" "strings" "testing" + "time" "github.com/livekit/protocol/livekit" "github.com/stretchr/testify/require" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/durationpb" "gopkg.in/yaml.v3" ) @@ -197,3 +200,222 @@ func TestScenarioGroupToYAMLFillsIDs(t *testing.T) { require.True(t, strings.HasPrefix(loaded.GetScenarios()[0].GetId(), "SCN_")) require.Equal(t, "fixed", loaded.GetScenarios()[1].GetId()) } + +func TestLoadScenarioGroupSimulatorFlow(t *testing.T) { + const steps = ` +steps: + menu: + say: Press 1 for sales. + speaker: MACHINE + routes: + - match: "1" + to: sales + timeout: + after: 8s + to: livekit/hangup + sales: + instructions: You are a sales rep. + voicemail: + beep: false +` + cases := []struct { + name string + yaml string + want *livekit.ScenarioGroup + wantErr []string + }{ + { + name: "steps and start", + yaml: ` +id: g1 +name: n` + steps + ` +scenarios: + - id: s1 + label: call + start: menu + max_duration: 60s +`, + want: &livekit.ScenarioGroup{ + Id: "g1", + Name: "n", + Steps: map[string]*livekit.SimulatorStep{ + "menu": { + Say: "Press 1 for sales.", + Speaker: livekit.SimulatorStep_MACHINE, + Routes: []*livekit.SimulatorStep_Route{{Match: "1", To: "sales"}}, + Timeout: &livekit.SimulatorStep_Timeout{After: durationpb.New(8 * time.Second), To: "livekit/hangup"}, + }, + "sales": { + Instructions: "You are a sales rep.", + Voicemail: &livekit.SimulatorStep_Voicemail{Beep: proto.Bool(false)}, + }, + }, + Scenarios: []*livekit.Scenario{{ + Id: "s1", + Label: "call", + Start: "menu", + MaxDuration: durationpb.New(60 * time.Second), + }}, + }, + }, + { + name: "built-in start", + yaml: ` +id: g1 +name: n +scenarios: + - id: s1 + label: call + start: livekit/unavailable +`, + want: &livekit.ScenarioGroup{ + Id: "g1", + Name: "n", + Scenarios: []*livekit.Scenario{{Id: "s1", Label: "call", Start: "livekit/unavailable"}}, + }, + }, + { + name: "instructions only", + yaml: ` +id: g1 +name: n +scenarios: + - id: s1 + label: order + instructions: order a burger + agent_expectations: confirms the order +`, + want: &livekit.ScenarioGroup{ + Id: "g1", + Name: "n", + Scenarios: []*livekit.Scenario{{ + Id: "s1", + Label: "order", + Instructions: "order a burger", + AgentExpectations: "confirms the order", + }}, + }, + }, + { + name: "unknown step key", + yaml: ` +id: g1 +name: n +steps: + menu: + sya: typo +scenarios: + - id: s1 + label: call + start: menu +`, + wantErr: []string{`step "menu"`, "sya"}, + }, + { + name: "start and instructions", + yaml: ` +id: g1 +name: n` + steps + ` +scenarios: + - id: s1 + label: call + start: menu + instructions: hi +`, + wantErr: []string{`scenario "call"`, "exactly one of `start` and `instructions`"}, + }, + { + name: "unknown start", + yaml: ` +id: g1 +name: n` + steps + ` +scenarios: + - id: s1 + label: call + start: nowhere +`, + wantErr: []string{`scenario "call"`, `start "nowhere"`}, + }, + { + name: "max_duration without unit", + yaml: ` +id: g1 +name: n` + steps + ` +scenarios: + - id: s1 + label: call + start: menu + max_duration: 60 +`, + wantErr: []string{`scenario "call"`, "max_duration"}, + }, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + group, err := loadScenarioGroup(writeScenariosFile(t, c.yaml)) + if c.wantErr != nil { + for _, e := range c.wantErr { + require.ErrorContains(t, err, e) + } + return + } + require.NoError(t, err) + require.True(t, proto.Equal(c.want, group), "got %v", group) + }) + } +} + +func TestScenarioGroupToYAMLRoundTripsFlow(t *testing.T) { + path := writeScenariosFile(t, ` +id: g1 +name: n +steps: + menu: + say: Press 1. + speaker: MACHINE + max_visits: + count: 2 + to: livekit/hangup + voicemail: + beep: false + silence_timeout: 1.5s +scenarios: + - id: s1 + label: call + start: menu + max_duration: 60s +`) + group, err := loadScenarioGroup(path) + require.NoError(t, err) + out, err := scenarioGroupToYAML(group) + require.NoError(t, err) + again, err := loadScenarioGroup(writeScenariosFile(t, string(out))) + require.NoError(t, err) + require.True(t, proto.Equal(group, again), "got %v\nyaml:\n%s", again, out) +} + +func TestInsertScenarioIDsKeepsSteps(t *testing.T) { + in := `name: n +steps: + menu: + say: hi # greet +scenarios: + - label: call + start: menu +` + out, added, err := insertScenarioIDs([]byte(in)) + require.NoError(t, err) + require.Equal(t, 2, added) + ids := regexp.MustCompile(`(SCNG?)_\w+`) + want := `id: SCNG_x +name: n +steps: + menu: + say: hi # greet +scenarios: + - id: SCN_x + label: call + start: menu +` + require.Equal(t, want, ids.ReplaceAllString(string(out), "${1}_x")) +} diff --git a/go.mod b/go.mod index 71cfb20e4..35d334b8d 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 github.com/joho/godotenv v1.5.1 github.com/klauspost/compress v1.20.1 - github.com/livekit/protocol v1.52.2-0.20261008051848-8c1250755780 + github.com/livekit/protocol v1.52.2-0.20261012145918-b440b89363b0 github.com/livekit/server-sdk-go/v2 v2.19.0 github.com/mattn/go-isatty v0.0.22 github.com/moby/moby/client v0.4.1 diff --git a/go.sum b/go.sum index 255293c62..39493bc7a 100644 --- a/go.sum +++ b/go.sum @@ -364,8 +364,8 @@ github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5AT github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095 h1:BcliKAXoMhl/nWmzQweQ5kmh4Qqagxl4s3Z5pvM/7AY= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095/go.mod h1:o8CFmAdrVwzJNOCsQCLUzXRjokkufNshnQHOe4fRaqU= -github.com/livekit/protocol v1.52.2-0.20261008051848-8c1250755780 h1:7T5JGMTDLcbI/emnTcrspygdWlVepIdHYA54um4v6Qo= -github.com/livekit/protocol v1.52.2-0.20261008051848-8c1250755780/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4= +github.com/livekit/protocol v1.52.2-0.20261012145918-b440b89363b0 h1:9vgVVkqvoMqY9gDTeY7sjc43OsgCaQXFi/sKrawi5nU= +github.com/livekit/protocol v1.52.2-0.20261012145918-b440b89363b0/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4= github.com/livekit/psrpc v0.8.2 h1:7tbkhBJoYlK5Csfkdv1XYbZjyAQeaEZgupvIXNCv7Dc= github.com/livekit/psrpc v0.8.2/go.mod h1:6KnU8vULoizX0Er9NNfeKJDV8ug87j3fuo19M0bhj6Q= github.com/livekit/server-sdk-go/v2 v2.19.0 h1:fZ0zmeC/5V+rXiI/MeC05EjwTfXmpaGkr/DXLG3EzgA= From 8fd64f1b6204734c5ffcf2db6c7fd88805290565 Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Tue, 13 Oct 2026 15:03:54 +0100 Subject: [PATCH 02/10] agent simulate: rename simulator flow steps to nodes Follows the protocol rename: scenarios.yaml takes a top-level `nodes` map, and errors say node. A node can no longer be a voicemail box; scenarios reach one through the built-in livekit/voicemail. --- cmd/lk/simulate.go | 42 ++++++++++++------------- cmd/lk/simulate_scenario_ids_test.go | 47 ++++++++++++++-------------- go.mod | 2 +- go.sum | 2 ++ 4 files changed, 47 insertions(+), 46 deletions(-) diff --git a/cmd/lk/simulate.go b/cmd/lk/simulate.go index e90bce242..2376a3ad0 100644 --- a/cmd/lk/simulate.go +++ b/cmd/lk/simulate.go @@ -354,15 +354,15 @@ func scenarioGroupToYAML(group *livekit.ScenarioGroup) ([]byte, error) { if f.ID == "" { f.ID = utils.NewGuid(utils.ScenarioGroupPrefix) } - for name, step := range group.GetSteps() { - v, err := protoToYAMLValue(step) + for name, node := range group.GetNodes() { + v, err := protoToYAMLValue(node) if err != nil { - return nil, fmt.Errorf("failed to encode step %q: %w", name, err) + return nil, fmt.Errorf("failed to encode node %q: %w", name, err) } - if f.Steps == nil { - f.Steps = map[string]any{} + if f.Nodes == nil { + f.Nodes = map[string]any{} } - f.Steps[name] = v + f.Nodes[name] = v } for _, s := range group.GetScenarios() { ys := yamlScenario{ @@ -396,12 +396,12 @@ func scenarioGroupToYAML(group *livekit.ScenarioGroup) ([]byte, error) { } // scenariosFile mirrors a scenarios.yaml; `userdata` is a nested mapping here -// and JSON-encoded into the proto's string field. Each `steps` entry is a -// SimulatorStep in protojson form. +// and JSON-encoded into the proto's string field. Each `nodes` entry is a +// SimulatorNode in protojson form. type scenariosFile struct { ID string `yaml:"id"` Name string `yaml:"name"` - Steps map[string]any `yaml:"steps,omitempty"` + Nodes map[string]any `yaml:"nodes,omitempty"` Scenarios []yamlScenario `yaml:"scenarios"` } @@ -478,20 +478,20 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { return nil, fmt.Errorf("scenarios file has no id") } group := &livekit.ScenarioGroup{Id: f.ID, Name: f.Name} - names := make([]string, 0, len(f.Steps)) - for name := range f.Steps { + names := make([]string, 0, len(f.Nodes)) + for name := range f.Nodes { names = append(names, name) } sort.Strings(names) for _, name := range names { - step := &livekit.SimulatorStep{} - if err := yamlValueToProto(f.Steps[name], step); err != nil { - return nil, fmt.Errorf("step %q: %w", name, err) + node := &livekit.SimulatorNode{} + if err := yamlValueToProto(f.Nodes[name], node); err != nil { + return nil, fmt.Errorf("node %q: %w", name, err) } - if group.Steps == nil { - group.Steps = map[string]*livekit.SimulatorStep{} + if group.Nodes == nil { + group.Nodes = map[string]*livekit.SimulatorNode{} } - group.Steps[name] = step + group.Nodes[name] = node } seen := map[string]bool{} for _, s := range f.Scenarios { @@ -505,8 +505,8 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { if (s.Start == "") == (s.Instructions == "") { return nil, fmt.Errorf("scenario %q: set exactly one of `start` and `instructions`", s.Label) } - if s.Start != "" && group.Steps[s.Start] == nil && !builtinSimulatorSteps[s.Start] { - return nil, fmt.Errorf("scenario %q: start %q is not a step", s.Label, s.Start) + if s.Start != "" && group.Nodes[s.Start] == nil && !builtinSimulatorNodes[s.Start] { + return nil, fmt.Errorf("scenario %q: start %q is not a node", s.Label, s.Start) } var maxDuration *durationpb.Duration if s.MaxDuration != "" { @@ -537,8 +537,8 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { return group, nil } -// builtinSimulatorSteps are the steps the server provides; a scenario may start at one. -var builtinSimulatorSteps = map[string]bool{ +// builtinSimulatorNodes are the nodes the server provides; a scenario may start at one. +var builtinSimulatorNodes = map[string]bool{ "livekit/hangup": true, "livekit/voicemail": true, "livekit/unavailable": true, diff --git a/cmd/lk/simulate_scenario_ids_test.go b/cmd/lk/simulate_scenario_ids_test.go index fa0323f9f..307146e2a 100644 --- a/cmd/lk/simulate_scenario_ids_test.go +++ b/cmd/lk/simulate_scenario_ids_test.go @@ -202,8 +202,8 @@ func TestScenarioGroupToYAMLFillsIDs(t *testing.T) { } func TestLoadScenarioGroupSimulatorFlow(t *testing.T) { - const steps = ` -steps: + const nodes = ` +nodes: menu: say: Press 1 for sales. speaker: MACHINE @@ -215,8 +215,7 @@ steps: to: livekit/hangup sales: instructions: You are a sales rep. - voicemail: - beep: false + expectation: Asks about the order. ` cases := []struct { name string @@ -225,10 +224,10 @@ steps: wantErr []string }{ { - name: "steps and start", + name: "nodes and start", yaml: ` id: g1 -name: n` + steps + ` +name: n` + nodes + ` scenarios: - id: s1 label: call @@ -238,16 +237,16 @@ scenarios: want: &livekit.ScenarioGroup{ Id: "g1", Name: "n", - Steps: map[string]*livekit.SimulatorStep{ + Nodes: map[string]*livekit.SimulatorNode{ "menu": { Say: "Press 1 for sales.", - Speaker: livekit.SimulatorStep_MACHINE, - Routes: []*livekit.SimulatorStep_Route{{Match: "1", To: "sales"}}, - Timeout: &livekit.SimulatorStep_Timeout{After: durationpb.New(8 * time.Second), To: "livekit/hangup"}, + Speaker: livekit.SimulatorNode_MACHINE, + Routes: []*livekit.SimulatorNode_Route{{Match: "1", To: "sales"}}, + Timeout: &livekit.SimulatorNode_Timeout{After: durationpb.New(8 * time.Second), To: "livekit/hangup"}, }, "sales": { Instructions: "You are a sales rep.", - Voicemail: &livekit.SimulatorStep_Voicemail{Beep: proto.Bool(false)}, + Expectation: "Asks about the order.", }, }, Scenarios: []*livekit.Scenario{{ @@ -297,11 +296,11 @@ scenarios: }, }, { - name: "unknown step key", + name: "unknown node key", yaml: ` id: g1 name: n -steps: +nodes: menu: sya: typo scenarios: @@ -309,13 +308,13 @@ scenarios: label: call start: menu `, - wantErr: []string{`step "menu"`, "sya"}, + wantErr: []string{`node "menu"`, "sya"}, }, { name: "start and instructions", yaml: ` id: g1 -name: n` + steps + ` +name: n` + nodes + ` scenarios: - id: s1 label: call @@ -328,7 +327,7 @@ scenarios: name: "unknown start", yaml: ` id: g1 -name: n` + steps + ` +name: n` + nodes + ` scenarios: - id: s1 label: call @@ -340,7 +339,7 @@ scenarios: name: "max_duration without unit", yaml: ` id: g1 -name: n` + steps + ` +name: n` + nodes + ` scenarios: - id: s1 label: call @@ -369,16 +368,16 @@ func TestScenarioGroupToYAMLRoundTripsFlow(t *testing.T) { path := writeScenariosFile(t, ` id: g1 name: n -steps: +nodes: menu: say: Press 1. speaker: MACHINE max_visits: count: 2 to: livekit/hangup - voicemail: - beep: false - silence_timeout: 1.5s + timeout: + after: 1.5s + to: livekit/voicemail scenarios: - id: s1 label: call @@ -394,9 +393,9 @@ scenarios: require.True(t, proto.Equal(group, again), "got %v\nyaml:\n%s", again, out) } -func TestInsertScenarioIDsKeepsSteps(t *testing.T) { +func TestInsertScenarioIDsKeepsNodes(t *testing.T) { in := `name: n -steps: +nodes: menu: say: hi # greet scenarios: @@ -409,7 +408,7 @@ scenarios: ids := regexp.MustCompile(`(SCNG?)_\w+`) want := `id: SCNG_x name: n -steps: +nodes: menu: say: hi # greet scenarios: diff --git a/go.mod b/go.mod index 35d334b8d..d8b253200 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 github.com/joho/godotenv v1.5.1 github.com/klauspost/compress v1.20.1 - github.com/livekit/protocol v1.52.2-0.20261012145918-b440b89363b0 + github.com/livekit/protocol v1.52.2-0.20261013140121-6cffd8387872 github.com/livekit/server-sdk-go/v2 v2.19.0 github.com/mattn/go-isatty v0.0.22 github.com/moby/moby/client v0.4.1 diff --git a/go.sum b/go.sum index 39493bc7a..0c1d3c150 100644 --- a/go.sum +++ b/go.sum @@ -366,6 +366,8 @@ github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095 h1:Bcli github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095/go.mod h1:o8CFmAdrVwzJNOCsQCLUzXRjokkufNshnQHOe4fRaqU= github.com/livekit/protocol v1.52.2-0.20261012145918-b440b89363b0 h1:9vgVVkqvoMqY9gDTeY7sjc43OsgCaQXFi/sKrawi5nU= github.com/livekit/protocol v1.52.2-0.20261012145918-b440b89363b0/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4= +github.com/livekit/protocol v1.52.2-0.20261013140121-6cffd8387872 h1:h27C5QKbZko55gFrvj4zFFYOlXHH8V0dSzif9PqJ+qM= +github.com/livekit/protocol v1.52.2-0.20261013140121-6cffd8387872/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4= github.com/livekit/psrpc v0.8.2 h1:7tbkhBJoYlK5Csfkdv1XYbZjyAQeaEZgupvIXNCv7Dc= github.com/livekit/psrpc v0.8.2/go.mod h1:6KnU8vULoizX0Er9NNfeKJDV8ug87j3fuo19M0bhj6Q= github.com/livekit/server-sdk-go/v2 v2.19.0 h1:fZ0zmeC/5V+rXiI/MeC05EjwTfXmpaGkr/DXLG3EzgA= From 51104265870405f30a2b73ad987711624d983ddc Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Tue, 20 Oct 2026 15:10:41 +0100 Subject: [PATCH 03/10] agent simulate: drop max_visits from simulator flow nodes Menus now repeat at most three times inside the simulator, so the schema no longer has max_visits. --- cmd/lk/simulate_scenario_ids_test.go | 6 +++--- go.mod | 2 +- go.sum | 6 ++---- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/cmd/lk/simulate_scenario_ids_test.go b/cmd/lk/simulate_scenario_ids_test.go index 307146e2a..8704609b3 100644 --- a/cmd/lk/simulate_scenario_ids_test.go +++ b/cmd/lk/simulate_scenario_ids_test.go @@ -372,9 +372,9 @@ nodes: menu: say: Press 1. speaker: MACHINE - max_visits: - count: 2 - to: livekit/hangup + routes: + - match: "1" + to: livekit/hangup timeout: after: 1.5s to: livekit/voicemail diff --git a/go.mod b/go.mod index d8b253200..841709bf0 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 github.com/joho/godotenv v1.5.1 github.com/klauspost/compress v1.20.1 - github.com/livekit/protocol v1.52.2-0.20261013140121-6cffd8387872 + github.com/livekit/protocol v1.52.2-0.20261020140226-e747784b3b51 github.com/livekit/server-sdk-go/v2 v2.19.0 github.com/mattn/go-isatty v0.0.22 github.com/moby/moby/client v0.4.1 diff --git a/go.sum b/go.sum index 0c1d3c150..01a1a0adc 100644 --- a/go.sum +++ b/go.sum @@ -364,10 +364,8 @@ github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5AT github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095 h1:BcliKAXoMhl/nWmzQweQ5kmh4Qqagxl4s3Z5pvM/7AY= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095/go.mod h1:o8CFmAdrVwzJNOCsQCLUzXRjokkufNshnQHOe4fRaqU= -github.com/livekit/protocol v1.52.2-0.20261012145918-b440b89363b0 h1:9vgVVkqvoMqY9gDTeY7sjc43OsgCaQXFi/sKrawi5nU= -github.com/livekit/protocol v1.52.2-0.20261012145918-b440b89363b0/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4= -github.com/livekit/protocol v1.52.2-0.20261013140121-6cffd8387872 h1:h27C5QKbZko55gFrvj4zFFYOlXHH8V0dSzif9PqJ+qM= -github.com/livekit/protocol v1.52.2-0.20261013140121-6cffd8387872/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4= +github.com/livekit/protocol v1.52.2-0.20261020140226-e747784b3b51 h1:fuI+SDujw0elgfFn3sdyt9qIDpU61sKnXSb3MLtQJic= +github.com/livekit/protocol v1.52.2-0.20261020140226-e747784b3b51/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4= github.com/livekit/psrpc v0.8.2 h1:7tbkhBJoYlK5Csfkdv1XYbZjyAQeaEZgupvIXNCv7Dc= github.com/livekit/psrpc v0.8.2/go.mod h1:6KnU8vULoizX0Er9NNfeKJDV8ug87j3fuo19M0bhj6Q= github.com/livekit/server-sdk-go/v2 v2.19.0 h1:fZ0zmeC/5V+rXiI/MeC05EjwTfXmpaGkr/DXLG3EzgA= From ac77dcab6b1f58adace148b18b03e0e5daf2373f Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Wed, 21 Oct 2026 11:58:18 +0100 Subject: [PATCH 04/10] agent simulate: drop max_duration from scenarios.yaml --- cmd/lk/simulate.go | 17 ----------------- cmd/lk/simulate_scenario_ids_test.go | 22 +++------------------- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 6 insertions(+), 39 deletions(-) diff --git a/cmd/lk/simulate.go b/cmd/lk/simulate.go index 2376a3ad0..4090eb5dc 100644 --- a/cmd/lk/simulate.go +++ b/cmd/lk/simulate.go @@ -45,7 +45,6 @@ import ( "github.com/livekit/server-sdk-go/v2/pkg/cloudagents" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/types/known/durationpb" ) func init() { @@ -373,13 +372,6 @@ func scenarioGroupToYAML(group *livekit.ScenarioGroup) ([]byte, error) { AgentExpectations: s.GetAgentExpectations(), Tags: s.GetTags(), } - if s.GetMaxDuration() != nil { - v, err := protoToYAMLValue(s.GetMaxDuration()) - if err != nil { - return nil, fmt.Errorf("failed to encode max_duration for scenario %q: %w", s.GetLabel(), err) - } - ys.MaxDuration, _ = v.(string) - } if ys.ID == "" { ys.ID = utils.NewGuid(utils.ScenarioPrefix) } @@ -410,7 +402,6 @@ type yamlScenario struct { Label string `yaml:"label"` Instructions string `yaml:"instructions,omitempty"` Start string `yaml:"start,omitempty"` - MaxDuration string `yaml:"max_duration,omitempty"` AgentExpectations string `yaml:"agent_expectations"` Tags map[string]string `yaml:"tags"` Userdata map[string]any `yaml:"userdata"` @@ -508,13 +499,6 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { if s.Start != "" && group.Nodes[s.Start] == nil && !builtinSimulatorNodes[s.Start] { return nil, fmt.Errorf("scenario %q: start %q is not a node", s.Label, s.Start) } - var maxDuration *durationpb.Duration - if s.MaxDuration != "" { - maxDuration = &durationpb.Duration{} - if err := yamlValueToProto(s.MaxDuration, maxDuration); err != nil { - return nil, fmt.Errorf("scenario %q: max_duration: %w", s.Label, err) - } - } var userdata string if len(s.Userdata) > 0 { b, err := json.Marshal(s.Userdata) @@ -528,7 +512,6 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { Label: s.Label, Instructions: s.Instructions, Start: s.Start, - MaxDuration: maxDuration, AgentExpectations: s.AgentExpectations, Tags: s.Tags, Userdata: userdata, diff --git a/cmd/lk/simulate_scenario_ids_test.go b/cmd/lk/simulate_scenario_ids_test.go index 8704609b3..d50b99831 100644 --- a/cmd/lk/simulate_scenario_ids_test.go +++ b/cmd/lk/simulate_scenario_ids_test.go @@ -232,7 +232,6 @@ scenarios: - id: s1 label: call start: menu - max_duration: 60s `, want: &livekit.ScenarioGroup{ Id: "g1", @@ -250,10 +249,9 @@ scenarios: }, }, Scenarios: []*livekit.Scenario{{ - Id: "s1", - Label: "call", - Start: "menu", - MaxDuration: durationpb.New(60 * time.Second), + Id: "s1", + Label: "call", + Start: "menu", }}, }, }, @@ -335,19 +333,6 @@ scenarios: `, wantErr: []string{`scenario "call"`, `start "nowhere"`}, }, - { - name: "max_duration without unit", - yaml: ` -id: g1 -name: n` + nodes + ` -scenarios: - - id: s1 - label: call - start: menu - max_duration: 60 -`, - wantErr: []string{`scenario "call"`, "max_duration"}, - }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { @@ -382,7 +367,6 @@ scenarios: - id: s1 label: call start: menu - max_duration: 60s `) group, err := loadScenarioGroup(path) require.NoError(t, err) diff --git a/go.mod b/go.mod index 841709bf0..0da469e9a 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 github.com/joho/godotenv v1.5.1 github.com/klauspost/compress v1.20.1 - github.com/livekit/protocol v1.52.2-0.20261020140226-e747784b3b51 + github.com/livekit/protocol v1.53.1-0.20261021105631-cc82b7991668 github.com/livekit/server-sdk-go/v2 v2.19.0 github.com/mattn/go-isatty v0.0.22 github.com/moby/moby/client v0.4.1 diff --git a/go.sum b/go.sum index 01a1a0adc..88816cd9c 100644 --- a/go.sum +++ b/go.sum @@ -364,8 +364,8 @@ github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5AT github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095 h1:BcliKAXoMhl/nWmzQweQ5kmh4Qqagxl4s3Z5pvM/7AY= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095/go.mod h1:o8CFmAdrVwzJNOCsQCLUzXRjokkufNshnQHOe4fRaqU= -github.com/livekit/protocol v1.52.2-0.20261020140226-e747784b3b51 h1:fuI+SDujw0elgfFn3sdyt9qIDpU61sKnXSb3MLtQJic= -github.com/livekit/protocol v1.52.2-0.20261020140226-e747784b3b51/go.mod h1:k7v95/DNhMkslMeBxdQ+lQ6gKc8hMNGYB7t0+6sEGU4= +github.com/livekit/protocol v1.53.1-0.20261021105631-cc82b7991668 h1:mHob82bvXkNlaEFaOMQehcM0aDJ6gvZ4JYpfCkkQJko= +github.com/livekit/protocol v1.53.1-0.20261021105631-cc82b7991668/go.mod h1:kUTAI2TrTngGOiWP6MTAOkJpru6YYGd0MP9wzlDATy4= github.com/livekit/psrpc v0.8.2 h1:7tbkhBJoYlK5Csfkdv1XYbZjyAQeaEZgupvIXNCv7Dc= github.com/livekit/psrpc v0.8.2/go.mod h1:6KnU8vULoizX0Er9NNfeKJDV8ug87j3fuo19M0bhj6Q= github.com/livekit/server-sdk-go/v2 v2.19.0 h1:fZ0zmeC/5V+rXiI/MeC05EjwTfXmpaGkr/DXLG3EzgA= From 4bdff164449ee7edf33343dcec7d6792a69ce78d Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Wed, 21 Oct 2026 12:17:57 +0100 Subject: [PATCH 05/10] agent simulate: pin protocol to the flow schema head --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 0da469e9a..95ebf0394 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 github.com/joho/godotenv v1.5.1 github.com/klauspost/compress v1.20.1 - github.com/livekit/protocol v1.53.1-0.20261021105631-cc82b7991668 + github.com/livekit/protocol v1.53.1-0.20261021111358-8f0255c23cd7 github.com/livekit/server-sdk-go/v2 v2.19.0 github.com/mattn/go-isatty v0.0.22 github.com/moby/moby/client v0.4.1 diff --git a/go.sum b/go.sum index 88816cd9c..e6eabefcd 100644 --- a/go.sum +++ b/go.sum @@ -364,8 +364,8 @@ github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5AT github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095 h1:BcliKAXoMhl/nWmzQweQ5kmh4Qqagxl4s3Z5pvM/7AY= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095/go.mod h1:o8CFmAdrVwzJNOCsQCLUzXRjokkufNshnQHOe4fRaqU= -github.com/livekit/protocol v1.53.1-0.20261021105631-cc82b7991668 h1:mHob82bvXkNlaEFaOMQehcM0aDJ6gvZ4JYpfCkkQJko= -github.com/livekit/protocol v1.53.1-0.20261021105631-cc82b7991668/go.mod h1:kUTAI2TrTngGOiWP6MTAOkJpru6YYGd0MP9wzlDATy4= +github.com/livekit/protocol v1.53.1-0.20261021111358-8f0255c23cd7 h1:Ysee11wxSCqtFF9GJbl6hFlW88hBLZuvKierCjtdWzI= +github.com/livekit/protocol v1.53.1-0.20261021111358-8f0255c23cd7/go.mod h1:kUTAI2TrTngGOiWP6MTAOkJpru6YYGd0MP9wzlDATy4= github.com/livekit/psrpc v0.8.2 h1:7tbkhBJoYlK5Csfkdv1XYbZjyAQeaEZgupvIXNCv7Dc= github.com/livekit/psrpc v0.8.2/go.mod h1:6KnU8vULoizX0Er9NNfeKJDV8ug87j3fuo19M0bhj6Q= github.com/livekit/server-sdk-go/v2 v2.19.0 h1:fZ0zmeC/5V+rXiI/MeC05EjwTfXmpaGkr/DXLG3EzgA= From 62f05ec27b6fed5ce3c4f5e29d7a46c096246749 Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Wed, 21 Oct 2026 12:30:26 +0100 Subject: [PATCH 06/10] agent simulate: drop node expectation --- cmd/lk/simulate_scenario_ids_test.go | 2 -- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/cmd/lk/simulate_scenario_ids_test.go b/cmd/lk/simulate_scenario_ids_test.go index d50b99831..dac4a8014 100644 --- a/cmd/lk/simulate_scenario_ids_test.go +++ b/cmd/lk/simulate_scenario_ids_test.go @@ -215,7 +215,6 @@ nodes: to: livekit/hangup sales: instructions: You are a sales rep. - expectation: Asks about the order. ` cases := []struct { name string @@ -245,7 +244,6 @@ scenarios: }, "sales": { Instructions: "You are a sales rep.", - Expectation: "Asks about the order.", }, }, Scenarios: []*livekit.Scenario{{ diff --git a/go.mod b/go.mod index 95ebf0394..7279a333a 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 github.com/joho/godotenv v1.5.1 github.com/klauspost/compress v1.20.1 - github.com/livekit/protocol v1.53.1-0.20261021111358-8f0255c23cd7 + github.com/livekit/protocol v1.53.1-0.20261021112906-7b7ed8c98658 github.com/livekit/server-sdk-go/v2 v2.19.0 github.com/mattn/go-isatty v0.0.22 github.com/moby/moby/client v0.4.1 diff --git a/go.sum b/go.sum index e6eabefcd..1a5e9b334 100644 --- a/go.sum +++ b/go.sum @@ -364,8 +364,8 @@ github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5AT github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095 h1:BcliKAXoMhl/nWmzQweQ5kmh4Qqagxl4s3Z5pvM/7AY= github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095/go.mod h1:o8CFmAdrVwzJNOCsQCLUzXRjokkufNshnQHOe4fRaqU= -github.com/livekit/protocol v1.53.1-0.20261021111358-8f0255c23cd7 h1:Ysee11wxSCqtFF9GJbl6hFlW88hBLZuvKierCjtdWzI= -github.com/livekit/protocol v1.53.1-0.20261021111358-8f0255c23cd7/go.mod h1:kUTAI2TrTngGOiWP6MTAOkJpru6YYGd0MP9wzlDATy4= +github.com/livekit/protocol v1.53.1-0.20261021112906-7b7ed8c98658 h1:eGv4bMT1XXAF67kUJoqq3743358SbqENzvD3XxIhqtA= +github.com/livekit/protocol v1.53.1-0.20261021112906-7b7ed8c98658/go.mod h1:kUTAI2TrTngGOiWP6MTAOkJpru6YYGd0MP9wzlDATy4= github.com/livekit/psrpc v0.8.2 h1:7tbkhBJoYlK5Csfkdv1XYbZjyAQeaEZgupvIXNCv7Dc= github.com/livekit/psrpc v0.8.2/go.mod h1:6KnU8vULoizX0Er9NNfeKJDV8ug87j3fuo19M0bhj6Q= github.com/livekit/server-sdk-go/v2 v2.19.0 h1:fZ0zmeC/5V+rXiI/MeC05EjwTfXmpaGkr/DXLG3EzgA= From 80f194a168bdae2a00cf5b39af1dd10a7d5c2b65 Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Wed, 21 Oct 2026 15:47:42 +0100 Subject: [PATCH 07/10] agent simulate: a scenario without start or instructions loads, as on main --- cmd/lk/simulate.go | 4 ++-- cmd/lk/simulate_scenario_ids_test.go | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/lk/simulate.go b/cmd/lk/simulate.go index 4090eb5dc..4f131e70b 100644 --- a/cmd/lk/simulate.go +++ b/cmd/lk/simulate.go @@ -493,8 +493,8 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { return nil, fmt.Errorf("duplicate scenario id %q", s.ID) } seen[s.ID] = true - if (s.Start == "") == (s.Instructions == "") { - return nil, fmt.Errorf("scenario %q: set exactly one of `start` and `instructions`", s.Label) + if s.Start != "" && s.Instructions != "" { + return nil, fmt.Errorf("scenario %q: set `start` or `instructions`, not both", s.Label) } if s.Start != "" && group.Nodes[s.Start] == nil && !builtinSimulatorNodes[s.Start] { return nil, fmt.Errorf("scenario %q: start %q is not a node", s.Label, s.Start) diff --git a/cmd/lk/simulate_scenario_ids_test.go b/cmd/lk/simulate_scenario_ids_test.go index dac4a8014..0d161d18e 100644 --- a/cmd/lk/simulate_scenario_ids_test.go +++ b/cmd/lk/simulate_scenario_ids_test.go @@ -317,7 +317,7 @@ scenarios: start: menu instructions: hi `, - wantErr: []string{`scenario "call"`, "exactly one of `start` and `instructions`"}, + wantErr: []string{`scenario "call"`, "`start` or `instructions`, not both"}, }, { name: "unknown start", @@ -400,3 +400,15 @@ scenarios: ` require.Equal(t, want, ids.ReplaceAllString(string(out), "${1}_x")) } + +func TestScenarioWithoutStartOrInstructionsLoads(t *testing.T) { + group, err := loadScenarioGroup(writeScenariosFile(t, ` +id: g1 +name: n +scenarios: + - id: s1 + label: call +`)) + require.NoError(t, err) + require.Len(t, group.GetScenarios(), 1) +} From 52823995ad457f6de0c612393123535c80892a1e Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Tue, 17 Nov 2026 14:48:48 +0000 Subject: [PATCH 08/10] agent simulate: check simulator flows before the run starts Read nodes from the YAML tree so every value keeps its source text, and reject keys with no value. Check every start, then, and to that a scenario can reach, leave livekit/ names to the server, and reject a scenario with neither start nor instructions when the file has nodes. --- cmd/lk/simulate.go | 70 +++------- cmd/lk/simulate_flow.go | 200 +++++++++++++++++++++++++++ cmd/lk/simulate_scenario_ids_test.go | 156 ++++++++++++++++++++- 3 files changed, 371 insertions(+), 55 deletions(-) create mode 100644 cmd/lk/simulate_flow.go diff --git a/cmd/lk/simulate.go b/cmd/lk/simulate.go index ebfe54ffe..2c085a13f 100644 --- a/cmd/lk/simulate.go +++ b/cmd/lk/simulate.go @@ -44,7 +44,6 @@ import ( "github.com/livekit/protocol/utils" lksdk "github.com/livekit/server-sdk-go/v2" "github.com/livekit/server-sdk-go/v2/pkg/cloudagents" - "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" ) @@ -382,12 +381,12 @@ func scenarioGroupToYAML(group *livekit.ScenarioGroup) ([]byte, error) { f.ID = utils.NewGuid(utils.ScenarioGroupPrefix) } for name, node := range group.GetNodes() { - v, err := protoToYAMLValue(node) + v, err := protoToYAML(node) if err != nil { return nil, fmt.Errorf("failed to encode node %q: %w", name, err) } if f.Nodes == nil { - f.Nodes = map[string]any{} + f.Nodes = map[string]yaml.Node{} } f.Nodes[name] = v } @@ -417,12 +416,12 @@ func scenarioGroupToYAML(group *livekit.ScenarioGroup) ([]byte, error) { // scenariosFile mirrors a scenarios.yaml; `userdata` is a nested mapping here // and JSON-encoded into the proto's string field. Each `nodes` entry is a -// SimulatorNode in protojson form. +// SimulatorNode in protojson form (see simulate_flow.go). type scenariosFile struct { - ID string `yaml:"id"` - Name string `yaml:"name"` - Nodes map[string]any `yaml:"nodes,omitempty"` - Scenarios []yamlScenario `yaml:"scenarios"` + ID string `yaml:"id"` + Name string `yaml:"name"` + Nodes map[string]yaml.Node `yaml:"nodes,omitempty"` + Scenarios []yamlScenario `yaml:"scenarios"` } type yamlScenario struct { @@ -503,21 +502,11 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { return nil, fmt.Errorf("scenarios file has no id") } group := &livekit.ScenarioGroup{Id: f.ID, Name: f.Name} - names := make([]string, 0, len(f.Nodes)) - for name := range f.Nodes { - names = append(names, name) - } - sort.Strings(names) - for _, name := range names { - node := &livekit.SimulatorNode{} - if err := yamlValueToProto(f.Nodes[name], node); err != nil { - return nil, fmt.Errorf("node %q: %w", name, err) - } - if group.Nodes == nil { - group.Nodes = map[string]*livekit.SimulatorNode{} - } - group.Nodes[name] = node + nodes, err := decodeNodes(f.Nodes) + if err != nil { + return nil, err } + group.Nodes = nodes seen := map[string]bool{} for _, s := range f.Scenarios { if s.ID == "" { @@ -530,8 +519,9 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { if s.Start != "" && s.Instructions != "" { return nil, fmt.Errorf("scenario %q: set `start` or `instructions`, not both", s.Label) } - if s.Start != "" && group.Nodes[s.Start] == nil && !builtinSimulatorNodes[s.Start] { - return nil, fmt.Errorf("scenario %q: start %q is not a node", s.Label, s.Start) + // with nodes in the file, a scenario with neither is most likely a misspelled `start` + if s.Start == "" && s.Instructions == "" && len(group.Nodes) > 0 { + return nil, fmt.Errorf("scenario %q: set `start` or `instructions`", s.Label) } var userdata string if len(s.Userdata) > 0 { @@ -551,38 +541,10 @@ func loadScenarioGroup(path string) (*livekit.ScenarioGroup, error) { Userdata: userdata, }) } - return group, nil -} - -// builtinSimulatorNodes are the nodes the server provides; a scenario may start at one. -var builtinSimulatorNodes = map[string]bool{ - "livekit/hangup": true, - "livekit/voicemail": true, - "livekit/unavailable": true, -} - -// yamlValueToProto decodes a YAML value through its JSON form with protojson, -// so field names, enum names, durations, and unknown-key errors match the -// server's parser. -func yamlValueToProto(v any, m proto.Message) error { - b, err := json.Marshal(v) - if err != nil { - return err - } - return protojson.Unmarshal(b, m) -} - -// protoToYAMLValue is the inverse of yamlValueToProto. -func protoToYAMLValue(m proto.Message) (any, error) { - b, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(m) - if err != nil { - return nil, err - } - var v any - if err := json.Unmarshal(b, &v); err != nil { + if err := checkFlows(group); err != nil { return nil, err } - return v, nil + return group, nil } func generateAgentName() string { diff --git a/cmd/lk/simulate_flow.go b/cmd/lk/simulate_flow.go new file mode 100644 index 000000000..e32690137 --- /dev/null +++ b/cmd/lk/simulate_flow.go @@ -0,0 +1,200 @@ +// Copyright 2026 LiveKit, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "encoding/json" + "errors" + "fmt" + "regexp" + "sort" + "strings" + "time" + + "gopkg.in/yaml.v3" + + "github.com/livekit/protocol/livekit" + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" +) + +// builtinNodePrefix marks the nodes the server provides, such as +// livekit/hangup. Which built-ins exist is for the server to say. +const builtinNodePrefix = "livekit/" + +// maxNodeTimeout is the documented limit of SimulatorNode.Timeout.after. +const maxNodeTimeout = 20 * time.Second + +// decodeNodes reads the `nodes` of a scenarios.yaml. +func decodeNodes(nodes map[string]yaml.Node) (map[string]*livekit.SimulatorNode, error) { + if len(nodes) == 0 { + return nil, nil + } + names := make([]string, 0, len(nodes)) + for name := range nodes { + names = append(names, name) + } + sort.Strings(names) + out := make(map[string]*livekit.SimulatorNode, len(nodes)) + for _, name := range names { + if strings.HasPrefix(name, builtinNodePrefix) { + return nil, fmt.Errorf("node %q: the %s prefix is reserved for built-in nodes", name, builtinNodePrefix) + } + value := nodes[name] + node := &livekit.SimulatorNode{} + if err := yamlToProto(&value, node); err != nil { + return nil, fmt.Errorf("node %q (line %d): %w", name, value.Line, err) + } + out[name] = node + } + return out, nil +} + +// jsonPosition is where protojson saw an error in the JSON it was given, +// which is not a place in the user's file. +var jsonPosition = regexp.MustCompile(`\(line \d+:\d+\):\s*`) + +// yamlToProto decodes YAML into m through protojson, so field names, enum +// names, and durations follow the server's parser. Every scalar goes in as its +// source text, because every leaf of a SimulatorNode is a string, an enum, or +// a duration: otherwise YAML reads `2026-01-01` as a date and `0123` as an +// octal number. +func yamlToProto(n *yaml.Node, m proto.Message) error { + v, err := yamlSourceValue(n) + if err != nil { + return err + } + b, err := json.Marshal(v) + if err != nil { + return err + } + if err := protojson.Unmarshal(b, m); err != nil { + return errors.New(jsonPosition.ReplaceAllString(err.Error(), "")) + } + return nil +} + +func yamlSourceValue(n *yaml.Node) (any, error) { + switch n.Kind { + case yaml.DocumentNode: + if len(n.Content) == 0 { + return nil, fmt.Errorf("line %d: empty document", n.Line) + } + return yamlSourceValue(n.Content[0]) + case yaml.AliasNode: + return yamlSourceValue(n.Alias) + case yaml.MappingNode: + out := make(map[string]any, len(n.Content)/2) + for i := 0; i+1 < len(n.Content); i += 2 { + key, value := n.Content[i], n.Content[i+1] + if _, ok := out[key.Value]; ok { + return nil, fmt.Errorf("line %d: duplicate key %q", key.Line, key.Value) + } + if isYAMLNull(value) { + return nil, fmt.Errorf("line %d: %s has no value (quote a value that starts with #)", key.Line, key.Value) + } + v, err := yamlSourceValue(value) + if err != nil { + return nil, err + } + out[key.Value] = v + } + return out, nil + case yaml.SequenceNode: + out := make([]any, 0, len(n.Content)) + for _, item := range n.Content { + if isYAMLNull(item) { + return nil, fmt.Errorf("line %d: empty list item", item.Line) + } + v, err := yamlSourceValue(item) + if err != nil { + return nil, err + } + out = append(out, v) + } + return out, nil + case yaml.ScalarNode: + return n.Value, nil + } + return nil, fmt.Errorf("line %d: unsupported YAML", n.Line) +} + +func isYAMLNull(n *yaml.Node) bool { + return n.Kind == yaml.ScalarNode && n.ShortTag() == "!!null" +} + +// protoToYAML is the inverse of yamlToProto. +func protoToYAML(m proto.Message) (yaml.Node, error) { + var n yaml.Node + b, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(m) + if err != nil { + return n, err + } + var v any + if err := json.Unmarshal(b, &v); err != nil { + return n, err + } + err = n.Encode(v) + return n, err +} + +// checkFlows reports what the server would reject in the nodes a scenario can +// reach, so a bad file fails before the agent starts. Nodes no scenario +// reaches are not checked, as on the server. +func checkFlows(group *livekit.ScenarioGroup) error { + var errs []error + var pending []string + follow := func(where, field, to string) { + switch { + case to == "": + errs = append(errs, fmt.Errorf("%s: %s names no node", where, field)) + case strings.HasPrefix(to, builtinNodePrefix): + // a built-in: the server knows which exist + case group.GetNodes()[to] == nil: + errs = append(errs, fmt.Errorf("%s: %s %q is not a node", where, field, to)) + default: + pending = append(pending, to) + } + } + for _, s := range group.GetScenarios() { + if s.GetStart() != "" { + follow(fmt.Sprintf("scenario %q", s.GetLabel()), "start", s.GetStart()) + } + } + checked := map[string]bool{} + for len(pending) > 0 { + name := pending[0] + pending = pending[1:] + if checked[name] { + continue + } + checked[name] = true + node := group.GetNodes()[name] + where := fmt.Sprintf("node %q", name) + if node.GetThen() != "" { + follow(where, "then", node.GetThen()) + } + for i, route := range node.GetRoutes() { + follow(where, fmt.Sprintf("routes[%d].to", i), route.GetTo()) + } + if timeout := node.GetTimeout(); timeout != nil { + follow(where, "timeout.to", timeout.GetTo()) + if after := timeout.GetAfter().AsDuration(); after > maxNodeTimeout { + errs = append(errs, fmt.Errorf("%s: timeout.after is %s, more than %s", where, after, maxNodeTimeout)) + } + } + } + return errors.Join(errs...) +} diff --git a/cmd/lk/simulate_scenario_ids_test.go b/cmd/lk/simulate_scenario_ids_test.go index 0d161d18e..cb48defde 100644 --- a/cmd/lk/simulate_scenario_ids_test.go +++ b/cmd/lk/simulate_scenario_ids_test.go @@ -304,7 +304,7 @@ scenarios: label: call start: menu `, - wantErr: []string{`node "menu"`, "sya"}, + wantErr: []string{`node "menu" (line 6)`, "sya"}, }, { name: "start and instructions", @@ -331,6 +331,154 @@ scenarios: `, wantErr: []string{`scenario "call"`, `start "nowhere"`}, }, + { + name: "misspelled start", + yaml: ` +id: g1 +name: n` + nodes + ` +scenarios: + - id: s1 + label: call + strat: menu +`, + wantErr: []string{`scenario "call": set ` + "`start` or `instructions`"}, + }, + { + name: "scalars keep their source text", + yaml: ` +id: g1 +name: n +nodes: + menu: + say: 2026-01-01 + routes: + - match: 0123 + to: livekit/hangup + - match: 1 + to: livekit/hangup + - match: yes + to: livekit/hangup +scenarios: + - id: s1 + label: call + start: menu +`, + want: &livekit.ScenarioGroup{ + Id: "g1", + Name: "n", + Nodes: map[string]*livekit.SimulatorNode{ + "menu": { + Say: "2026-01-01", + Routes: []*livekit.SimulatorNode_Route{ + {Match: "0123", To: "livekit/hangup"}, + {Match: "1", To: "livekit/hangup"}, + {Match: "yes", To: "livekit/hangup"}, + }, + }, + }, + Scenarios: []*livekit.Scenario{{Id: "s1", Label: "call", Start: "menu"}}, + }, + }, + { + name: "unquoted pound", + yaml: ` +id: g1 +name: n +nodes: + menu: + say: Enter your number. + routes: + - match: # + to: livekit/hangup +scenarios: + - id: s1 + label: call + start: menu +`, + wantErr: []string{`node "menu"`, "line 8: match has no value"}, + }, + { + name: "empty then", + yaml: ` +id: g1 +name: n +nodes: + menu: + say: hi + then: +scenarios: + - id: s1 + label: call + start: menu +`, + wantErr: []string{`node "menu"`, "then has no value"}, + }, + { + name: "dangling targets", + yaml: ` +id: g1 +name: n +nodes: + menu: + say: hi + routes: + - match: "1" + to: nowhere + - match: "2" + timeout: + after: 30s + to: next + next: + say: bye + then: gone +scenarios: + - id: s1 + label: call + start: menu +`, + wantErr: []string{ + `node "menu": routes[0].to "nowhere" is not a node`, + `node "menu": routes[1].to names no node`, + `node "menu": timeout.after is 30s, more than 20s`, + `node "next": then "gone" is not a node`, + }, + }, + { + name: "unreached nodes and unknown built-ins pass", + yaml: ` +id: g1 +name: n +nodes: + orphan: + say: hi + then: nowhere +scenarios: + - id: s1 + label: call + start: livekit/busy +`, + want: &livekit.ScenarioGroup{ + Id: "g1", + Name: "n", + Nodes: map[string]*livekit.SimulatorNode{"orphan": {Say: "hi", Then: "nowhere"}}, + Scenarios: []*livekit.Scenario{{Id: "s1", Label: "call", Start: "livekit/busy"}}, + }, + }, + { + name: "reserved node name", + yaml: ` +id: g1 +name: n +nodes: + livekit/hangup: + say: bye +scenarios: + - id: s1 + label: call + start: livekit/hangup +`, + wantErr: []string{`node "livekit/hangup": the livekit/ prefix is reserved`}, + }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { @@ -339,6 +487,7 @@ scenarios: for _, e := range c.wantErr { require.ErrorContains(t, err, e) } + require.NotRegexp(t, `\(line \d+:\d+\)`, err.Error(), "a position in the JSON, not the YAML") return } require.NoError(t, err) @@ -358,9 +507,14 @@ nodes: routes: - match: "1" to: livekit/hangup + - match: "#" + to: livekit/hangup timeout: after: 1.5s to: livekit/voicemail + date: + say: "2026-01-01" + then: livekit/hangup scenarios: - id: s1 label: call From 8f65684a6a2aeadaf01dd851c845ada09274d58e Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Tue, 17 Nov 2026 16:52:14 +0000 Subject: [PATCH 09/10] agent simulate: read simulator nodes against their schema Walk each node's YAML with its proto descriptor. An anchor that contains itself fails instead of overflowing the stack, merge keys expand, an enum takes its number as well as its name, a null behind an alias is rejected, explicit tags other than str and int are rejected, and unknown fields name their YAML line. Reject a timeout.after of zero or less. --- cmd/lk/simulate_flow.go | 273 ++++++++++++++++++++++----- cmd/lk/simulate_scenario_ids_test.go | 142 +++++++++++++- 2 files changed, 362 insertions(+), 53 deletions(-) diff --git a/cmd/lk/simulate_flow.go b/cmd/lk/simulate_flow.go index e32690137..322dc4b97 100644 --- a/cmd/lk/simulate_flow.go +++ b/cmd/lk/simulate_flow.go @@ -15,6 +15,7 @@ package main import ( + "bytes" "encoding/json" "errors" "fmt" @@ -28,6 +29,7 @@ import ( "github.com/livekit/protocol/livekit" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" ) // builtinNodePrefix marks the nodes the server provides, such as @@ -49,86 +51,250 @@ func decodeNodes(nodes map[string]yaml.Node) (map[string]*livekit.SimulatorNode, sort.Strings(names) out := make(map[string]*livekit.SimulatorNode, len(nodes)) for _, name := range names { - if strings.HasPrefix(name, builtinNodePrefix) { - return nil, fmt.Errorf("node %q: the %s prefix is reserved for built-in nodes", name, builtinNodePrefix) - } value := nodes[name] - node := &livekit.SimulatorNode{} - if err := yamlToProto(&value, node); err != nil { - return nil, fmt.Errorf("node %q (line %d): %w", name, value.Line, err) + node, err := decodeNode(name, &value) + if err != nil { + return nil, err } out[name] = node } return out, nil } +// decodeNode reads one entry of a scenarios.yaml's `nodes`. +func decodeNode(name string, value *yaml.Node) (*livekit.SimulatorNode, error) { + if strings.HasPrefix(name, builtinNodePrefix) { + return nil, fmt.Errorf("node %q: the %s prefix is reserved for built-in nodes", name, builtinNodePrefix) + } + node := &livekit.SimulatorNode{} + if err := yamlToProto(value, node); err != nil { + return nil, fmt.Errorf("node %q: %w", name, err) + } + return node, nil +} + // jsonPosition is where protojson saw an error in the JSON it was given, // which is not a place in the user's file. var jsonPosition = regexp.MustCompile(`\(line \d+:\d+\):\s*`) // yamlToProto decodes YAML into m through protojson, so field names, enum -// names, and durations follow the server's parser. Every scalar goes in as its -// source text, because every leaf of a SimulatorNode is a string, an enum, or -// a duration: otherwise YAML reads `2026-01-01` as a date and `0123` as an -// octal number. +// names, and durations follow the server's parser. A scalar goes in as its +// source text, except an integer for an enum field: otherwise YAML reads +// `2026-01-01` as a date and `0123` as an octal number. Errors name the line +// in the YAML. func yamlToProto(n *yaml.Node, m proto.Message) error { - v, err := yamlSourceValue(n) + d := yamlDecoder{open: map[yamlPlace]bool{}} + v, err := d.message(n, m.ProtoReflect().Descriptor()) if err != nil { return err } - b, err := json.Marshal(v) - if err != nil { + var b bytes.Buffer + enc := json.NewEncoder(&b) + enc.SetEscapeHTML(false) + if err := enc.Encode(v); err != nil { return err } - if err := protojson.Unmarshal(b, m); err != nil { - return errors.New(jsonPosition.ReplaceAllString(err.Error(), "")) + if err := protojson.Unmarshal(b.Bytes(), m); err != nil { + return fmt.Errorf("line %d: %s", n.Line, jsonPosition.ReplaceAllString(err.Error(), "")) } return nil } -func yamlSourceValue(n *yaml.Node) (any, error) { - switch n.Kind { - case yaml.DocumentNode: - if len(n.Content) == 0 { - return nil, fmt.Errorf("line %d: empty document", n.Line) - } - return yamlSourceValue(n.Content[0]) - case yaml.AliasNode: - return yamlSourceValue(n.Alias) - case yaml.MappingNode: - out := make(map[string]any, len(n.Content)/2) - for i := 0; i+1 < len(n.Content); i += 2 { - key, value := n.Content[i], n.Content[i+1] - if _, ok := out[key.Value]; ok { - return nil, fmt.Errorf("line %d: duplicate key %q", key.Line, key.Value) - } - if isYAMLNull(value) { - return nil, fmt.Errorf("line %d: %s has no value (quote a value that starts with #)", key.Line, key.Value) - } - v, err := yamlSourceValue(value) +// yamlDecoder turns YAML into the JSON form of a message. +type yamlDecoder struct { + // the mappings and sequences being read, so an anchor that contains itself + // fails instead of recursing forever; keyed by place, since a node can be + // a copy of the one an alias points to + open map[yamlPlace]bool +} + +type yamlPlace struct{ line, column int } + +func placeOf(n *yaml.Node) yamlPlace { return yamlPlace{n.Line, n.Column} } + +func (d *yamlDecoder) message(n *yaml.Node, md protoreflect.MessageDescriptor) (any, error) { + n, err := d.resolve(n) + if err != nil { + return nil, err + } + if isYAMLNull(n) { + return nil, fmt.Errorf("line %d: no fields", n.Line) + } + if n.Kind != yaml.MappingNode { + return nil, fmt.Errorf("line %d: expected a mapping", n.Line) + } + if err := d.enter(n); err != nil { + return nil, err + } + defer d.leave(n) + pairs, err := d.pairs(n) + if err != nil { + return nil, err + } + out := make(map[string]any, len(pairs)) + for _, pair := range pairs { + key, value := pair[0], pair[1] + fd := md.Fields().ByJSONName(key.Value) + if fd == nil { + fd = md.Fields().ByTextName(key.Value) + } + if fd == nil { + return nil, fmt.Errorf("line %d: unknown field %q", key.Line, key.Value) + } + v, err := d.field(key, value, fd) + if err != nil { + return nil, err + } + out[key.Value] = v + } + return out, nil +} + +func (d *yamlDecoder) field(key, n *yaml.Node, fd protoreflect.FieldDescriptor) (any, error) { + n, err := d.resolve(n) + if err != nil { + return nil, err + } + if isYAMLNull(n) { + return nil, fmt.Errorf("line %d: %s has no value (quote a value that starts with #)", key.Line, key.Value) + } + if !fd.IsList() { + return d.single(n, fd) + } + if n.Kind != yaml.SequenceNode { + return nil, fmt.Errorf("line %d: %s takes a list", n.Line, key.Value) + } + if err := d.enter(n); err != nil { + return nil, err + } + defer d.leave(n) + out := make([]any, 0, len(n.Content)) + for _, item := range n.Content { + item, err := d.resolve(item) + if err != nil { + return nil, err + } + if isYAMLNull(item) { + return nil, fmt.Errorf("line %d: empty item in %s", item.Line, key.Value) + } + v, err := d.single(item, fd) + if err != nil { + return nil, err + } + out = append(out, v) + } + return out, nil +} + +// single is one value of fd: a nested message, or a scalar. +func (d *yamlDecoder) single(n *yaml.Node, fd protoreflect.FieldDescriptor) (any, error) { + if fd.Kind() == protoreflect.MessageKind && n.Kind == yaml.MappingNode { + return d.message(n, fd.Message()) + } + // every other value is a scalar, a duration included + if n.Kind != yaml.ScalarNode { + return nil, fmt.Errorf("line %d: %s takes a single value", n.Line, fd.TextName()) + } + if n.Style&yaml.TaggedStyle != 0 && n.ShortTag() != "!!str" && n.ShortTag() != "!!int" { + return nil, fmt.Errorf("line %d: the %s tag is not supported", n.Line, n.ShortTag()) + } + if fd.Kind() == protoreflect.EnumKind && n.ShortTag() == "!!int" { + return json.Number(n.Value), nil // protojson takes an enum's number as well as its name + } + return n.Value, nil +} + +// pairs is a mapping's keys and values, with `<<` merge keys expanded. A key +// the mapping sets itself wins over a merged one, and an earlier merge wins +// over a later one, as YAML defines. +func (d *yamlDecoder) pairs(n *yaml.Node) ([][2]*yaml.Node, error) { + var own, merged [][2]*yaml.Node + seen := map[string]bool{} + for i := 0; i+1 < len(n.Content); i += 2 { + key, value := n.Content[i], n.Content[i+1] + if key.ShortTag() == "!!merge" { + sources, err := d.mergeSources(value) if err != nil { return nil, err } - out[key.Value] = v - } - return out, nil - case yaml.SequenceNode: - out := make([]any, 0, len(n.Content)) - for _, item := range n.Content { - if isYAMLNull(item) { - return nil, fmt.Errorf("line %d: empty list item", item.Line) + for _, source := range sources { + if err := d.enter(source); err != nil { + return nil, err + } + inner, err := d.pairs(source) + d.leave(source) + if err != nil { + return nil, err + } + merged = append(merged, inner...) } - v, err := yamlSourceValue(item) - if err != nil { - return nil, err + continue + } + if seen[key.Value] { + return nil, fmt.Errorf("line %d: duplicate key %q", key.Line, key.Value) + } + seen[key.Value] = true + own = append(own, [2]*yaml.Node{key, value}) + } + for _, pair := range merged { + if !seen[pair[0].Value] { + seen[pair[0].Value] = true + own = append(own, pair) + } + } + return own, nil +} + +func (d *yamlDecoder) mergeSources(n *yaml.Node) ([]*yaml.Node, error) { + n, err := d.resolve(n) + if err != nil { + return nil, err + } + items := []*yaml.Node{n} + if n.Kind == yaml.SequenceNode { + items = n.Content + } + sources := make([]*yaml.Node, 0, len(items)) + for _, item := range items { + item, err := d.resolve(item) + if err != nil { + return nil, err + } + if item.Kind != yaml.MappingNode { + return nil, fmt.Errorf("line %d: `<<` merges only mappings", item.Line) + } + sources = append(sources, item) + } + return sources, nil +} + +// resolve follows aliases and document wrappers to the node they stand for. +func (d *yamlDecoder) resolve(n *yaml.Node) (*yaml.Node, error) { + for { + switch n.Kind { + case yaml.AliasNode: + n = n.Alias + case yaml.DocumentNode: + if len(n.Content) == 0 { + return nil, fmt.Errorf("line %d: empty document", n.Line) } - out = append(out, v) + n = n.Content[0] + default: + return n, nil } - return out, nil - case yaml.ScalarNode: - return n.Value, nil } - return nil, fmt.Errorf("line %d: unsupported YAML", n.Line) +} + +func (d *yamlDecoder) enter(n *yaml.Node) error { + if d.open[placeOf(n)] { + return fmt.Errorf("line %d: anchor %q contains itself", n.Line, n.Anchor) + } + d.open[placeOf(n)] = true + return nil +} + +func (d *yamlDecoder) leave(n *yaml.Node) { + delete(d.open, placeOf(n)) } func isYAMLNull(n *yaml.Node) bool { @@ -191,7 +357,10 @@ func checkFlows(group *livekit.ScenarioGroup) error { } if timeout := node.GetTimeout(); timeout != nil { follow(where, "timeout.to", timeout.GetTo()) - if after := timeout.GetAfter().AsDuration(); after > maxNodeTimeout { + switch after := timeout.GetAfter().AsDuration(); { + case after <= 0: + errs = append(errs, fmt.Errorf("%s: timeout.after must be more than 0s", where)) + case after > maxNodeTimeout: errs = append(errs, fmt.Errorf("%s: timeout.after is %s, more than %s", where, after, maxNodeTimeout)) } } diff --git a/cmd/lk/simulate_scenario_ids_test.go b/cmd/lk/simulate_scenario_ids_test.go index cb48defde..45ee42e0d 100644 --- a/cmd/lk/simulate_scenario_ids_test.go +++ b/cmd/lk/simulate_scenario_ids_test.go @@ -304,7 +304,7 @@ scenarios: label: call start: menu `, - wantErr: []string{`node "menu" (line 6)`, "sya"}, + wantErr: []string{`node "menu": line 6: unknown field "sya"`}, }, { name: "start and instructions", @@ -464,6 +464,146 @@ scenarios: Scenarios: []*livekit.Scenario{{Id: "s1", Label: "call", Start: "livekit/busy"}}, }, }, + { + name: "an anchor that contains itself", + yaml: ` +id: g1 +name: n +nodes: + menu: &menu + say: hi + routes: [*menu] +scenarios: + - id: s1 + label: call + start: menu +`, + wantErr: []string{`node "menu": line 5: anchor "menu" contains itself`}, + }, + { + name: "an anchor used as a string field", + yaml: ` +id: g1 +name: n +nodes: + loop: &loop { say: hi, then: *loop } +scenarios: + - id: s1 + label: call + start: loop +`, + wantErr: []string{`node "loop": line 5: then takes a single value`}, + }, + { + name: "merge keys, numeric enums, and aliases", + yaml: ` +id: g1 +name: n +defaults: &machine + speaker: MACHINE + timeout: { after: 8s, to: livekit/hangup } +nodes: + menu: + <<: *machine + say: For Sam, press 1. + timeout: { after: 5s, to: menu } + greeting: + <<: [*machine] + say: Hello. + speaker: 0 +scenarios: + - id: s1 + label: call + start: menu + - id: s2 + label: greeting + start: greeting +`, + want: &livekit.ScenarioGroup{ + Id: "g1", + Name: "n", + Nodes: map[string]*livekit.SimulatorNode{ + "menu": { + Say: "For Sam, press 1.", + Speaker: livekit.SimulatorNode_MACHINE, + Timeout: &livekit.SimulatorNode_Timeout{After: durationpb.New(5 * time.Second), To: "menu"}, + }, + "greeting": { + Say: "Hello.", + Speaker: livekit.SimulatorNode_PERSON, + Timeout: &livekit.SimulatorNode_Timeout{After: durationpb.New(8 * time.Second), To: "livekit/hangup"}, + }, + }, + Scenarios: []*livekit.Scenario{ + {Id: "s1", Label: "call", Start: "menu"}, + {Id: "s2", Label: "greeting", Start: "greeting"}, + }, + }, + }, + { + name: "a null behind an alias", + yaml: ` +id: g1 +name: n +none: &none ~ +nodes: + menu: { say: hi, then: *none } +scenarios: + - id: s1 + label: call + start: menu +`, + wantErr: []string{`node "menu": line 6: then has no value`}, + }, + { + name: "an explicit tag", + yaml: ` +id: g1 +name: n +nodes: + menu: { say: !!binary aGk=, then: livekit/hangup } +scenarios: + - id: s1 + label: call + start: menu +`, + wantErr: []string{`node "menu": line 5: the !!binary tag is not supported`}, + }, + { + name: "an empty node", + yaml: ` +id: g1 +name: n +nodes: + menu: +scenarios: + - id: s1 + label: call + start: menu +`, + wantErr: []string{`node "menu": line 5: no fields`}, + }, + { + name: "a timeout of zero", + yaml: ` +id: g1 +name: n +nodes: + menu: { say: hi, timeout: { after: 0s, to: livekit/hangup } } + hold: { say: hold, timeout: { to: livekit/hangup } } +scenarios: + - id: s1 + label: call + start: menu + - id: s2 + label: hold + start: hold +`, + wantErr: []string{ + `node "menu": timeout.after must be more than 0s`, + `node "hold": timeout.after must be more than 0s`, + }, + }, { name: "reserved node name", yaml: ` From eceba22d77dd38db7eef5bdf0efe85f4d213f4da Mon Sep 17 00:00:00 2001 From: Chenghao Mou Date: Tue, 17 Nov 2026 16:58:00 +0000 Subject: [PATCH 10/10] agent simulate: name test anchor keys with the x- prefix --- cmd/lk/simulate_scenario_ids_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/lk/simulate_scenario_ids_test.go b/cmd/lk/simulate_scenario_ids_test.go index 45ee42e0d..5eb2ed1f2 100644 --- a/cmd/lk/simulate_scenario_ids_test.go +++ b/cmd/lk/simulate_scenario_ids_test.go @@ -499,7 +499,7 @@ scenarios: yaml: ` id: g1 name: n -defaults: &machine +x-defaults: &machine speaker: MACHINE timeout: { after: 8s, to: livekit/hangup } nodes: @@ -545,7 +545,7 @@ scenarios: yaml: ` id: g1 name: n -none: &none ~ +x-none: &none ~ nodes: menu: { say: hi, then: *none } scenarios: