Improve workflow UI, retain stage diffs, and add portable pipeline transfers
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
This commit is contained in:
@@ -143,6 +143,11 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
|
||||
return "", err
|
||||
}
|
||||
c.Transcript = string(data)
|
||||
return completedAgentResult(data)
|
||||
}
|
||||
|
||||
func completedAgentResult(data []byte) (string, error) {
|
||||
text := ""
|
||||
var messages struct {
|
||||
Data []struct {
|
||||
Finish string `json:"finish"`
|
||||
|
||||
@@ -138,7 +138,7 @@ func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Resu
|
||||
if len(tools) > 0 {
|
||||
return Result{}, fmt.Errorf("tool-enabled stages require an audited agent client")
|
||||
}
|
||||
client = OpenCodeClient{}
|
||||
client = &OpenCodeClient{}
|
||||
}
|
||||
if spec.Config.Budget.Enabled() {
|
||||
capable, ok := client.(interface{ BudgetCapable() bool })
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestLiveModelStage(t *testing.T) {
|
||||
def.Blocks["prompt"] = "Compute the sum of the input integers. Return success with the numeric sum in the output. Do not use tools."
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
result, err := (ModelStage{Client: OpenCodeClient{Directory: directory}}).Run(ctx, def, Values{"a": 2, "b": 3})
|
||||
result, err := (ModelStage{Client: &OpenCodeClient{Directory: directory}}).Run(ctx, def, Values{"a": 2, "b": 3})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ func safeCommandDiagnostic(err error, data []byte) string {
|
||||
return detail
|
||||
}
|
||||
|
||||
func (c OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) {
|
||||
func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) {
|
||||
if c.Operations != nil {
|
||||
c.Operations.Provider, c.Operations.Model = config.Provider, config.Model
|
||||
}
|
||||
@@ -240,6 +240,50 @@ func (c OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// V2 /generate returns text only and does not persist messages or update
|
||||
// session usage. Budgeted calls must use tracked, deny-all prompting instead.
|
||||
if c.Backend == "v2" && config.Budget.Enabled() {
|
||||
if _, err = c.call(ctx, "post", "/api/session/"+session.Data.ID+"/prompt", generationBody("text", prompt, config, c.Backend)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err = c.call(ctx, "post", "/api/experimental/session/"+session.Data.ID+"/wait", nil); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var data []byte
|
||||
data, err = c.call(ctx, "get", "/api/session/"+session.Data.ID+"/message?order=desc&limit=100&type=assistant", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
c.FormatAudit = string(data)
|
||||
text, err = completedAgentResult(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// A completed tracked response must have accounted tokens; session-zero
|
||||
// placeholders are not proof of a free, zero-token provider request.
|
||||
usageCtx, usageCancel := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second)
|
||||
usage, usageErr := c.call(usageCtx, "get", "/api/session/"+session.Data.ID, nil)
|
||||
usageCancel()
|
||||
if usageErr != nil {
|
||||
return "", fmt.Errorf("%w: %v", ErrBudgetAccounting, usageErr)
|
||||
}
|
||||
if usageErr = config.Budget.readUsage(session.Data.ID, usage, false, true); usageErr != nil {
|
||||
return "", usageErr
|
||||
}
|
||||
if config.Budget.Snapshot().LimitTokens > 0 {
|
||||
var response struct {
|
||||
Data struct{ reportedUsage } `json:"data"`
|
||||
}
|
||||
if json.Unmarshal(usage, &response) != nil {
|
||||
return "", ErrBudgetAccounting
|
||||
}
|
||||
u := response.Data.Tokens
|
||||
if u == nil || u.Input == nil || u.Output == nil || u.Reasoning == nil || u.Cache == nil || u.Cache.Read == nil || u.Cache.Write == nil || *u.Input+*u.Output+*u.Reasoning+*u.Cache.Read+*u.Cache.Write == 0 {
|
||||
return "", fmt.Errorf("%w: completed V2 response reported zero tokens", ErrBudgetAccounting)
|
||||
}
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
response, err := c.call(ctx, "post", "/api/session/"+session.Data.ID+"/generate", body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBudgetedV2UsesTrackedDenyAllPrompt(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
limit, tokens int64
|
||||
want error
|
||||
}{{"within limit", 10, 3, nil}, {"exhausted", 1, 3, ErrBudgetExceeded}, {"zero placeholder", 10, 0, ErrBudgetAccounting}} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
budget := NewBudget(.25, test.limit)
|
||||
budget.pollInterval = time.Hour
|
||||
prompted := false
|
||||
interrupts := 0
|
||||
messages := 0
|
||||
client := &OpenCodeClient{Backend: "v2", command: func(_ context.Context, _ string, args ...string) ([]byte, error) {
|
||||
path := args[2]
|
||||
switch {
|
||||
case path == "/api/session":
|
||||
var body struct {
|
||||
Permissions []map[string]string `json:"permissions"`
|
||||
}
|
||||
for i, arg := range args {
|
||||
if arg == "--data" {
|
||||
if err := json.Unmarshal([]byte(args[i+1]), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(body.Permissions) != 1 || body.Permissions[0]["effect"] != "deny" || body.Permissions[0]["action"] != "*" {
|
||||
t.Fatal("text stage can admit tools", body)
|
||||
}
|
||||
return []byte(`{"data":{"id":"ses_tracked"}}`), nil
|
||||
case strings.HasSuffix(path, "/generate"):
|
||||
t.Fatal("unaccounted /generate used")
|
||||
return nil, nil
|
||||
case strings.HasSuffix(path, "/prompt"):
|
||||
prompted = true
|
||||
return []byte(`{}`), nil
|
||||
case strings.HasSuffix(path, "/wait"):
|
||||
return []byte(`{}`), nil
|
||||
case strings.Contains(path, "/message?"):
|
||||
messages++
|
||||
return []byte(`{"data":[{"finish":"stop","content":[{"type":"text","text":"{\"outcome\":\"success\",\"output\":{\"summary\":\"done\"}}"}]}]}`), nil
|
||||
case strings.HasSuffix(path, "/interrupt"):
|
||||
interrupts++
|
||||
return []byte(`{}`), nil
|
||||
case path == "/api/session/ses_tracked":
|
||||
tokens := int64(0)
|
||||
cost := 0.0
|
||||
if prompted {
|
||||
tokens = test.tokens
|
||||
cost = .001
|
||||
}
|
||||
return usageResponse("ses_tracked", cost, tokens), nil
|
||||
default:
|
||||
t.Fatal("unexpected path", path)
|
||||
return nil, nil
|
||||
}
|
||||
}}
|
||||
def := modelDefinition()
|
||||
def.Configs["budget_tokens"] = strconv.FormatInt(test.limit, 10)
|
||||
def.Configs["budget_usd"] = "0.25"
|
||||
def.Configs["retries"] = "0"
|
||||
result, err := (ModelStage{Client: client}).Run(WithBudget(context.Background(), budget), def, nil)
|
||||
if test.want != nil {
|
||||
if !errors.Is(err, test.want) || interrupts != 1 {
|
||||
t.Fatal("budget failure/cleanup missing", err, interrupts)
|
||||
}
|
||||
} else if err != nil || result.Output["summary"] != "done" {
|
||||
t.Fatal(result, err)
|
||||
}
|
||||
if !prompted || messages != 1 || !strings.Contains(client.FormatAudit, "done") {
|
||||
t.Fatal("tracked evidence missing", prompted, messages, client.FormatAudit)
|
||||
}
|
||||
if budget.Snapshot().UsedTokens != test.tokens {
|
||||
t.Fatal("accounting double counted", budget.Snapshot())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user