fix(stage): persist every V2 model generation in tracked sessions

This commit is contained in:
2026-10-20 20:39:50 +01:00
parent 1b8d0f2a75
commit cb57da5dfc
7 changed files with 307 additions and 13 deletions
+34 -6
View File
@@ -266,6 +266,7 @@ func (c OpenCodeClient) sessionActive(ctx context.Context, sessionID string) (bo
}
func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) {
c.FormatAudit = ""
if c.Operations != nil {
c.Operations.Provider, c.Operations.Model = config.Provider, config.Model
}
@@ -287,7 +288,11 @@ func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, promp
}
}
// Fail before creating a session if the generation payload is too large.
body := generationBody("prompt", prompt, config, c.Backend)
promptKey := "prompt"
if c.Backend == "v2" {
promptKey = "text"
}
body := generationBody(promptKey, prompt, config, c.Backend)
encoded, err := json.Marshal(body)
if err != nil {
return "", err
@@ -315,13 +320,28 @@ func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, promp
return "", fmt.Errorf("OpenCode returned an invalid session ID")
}
var watch *budgetWatch
promptStarted := false
defer func() {
watch.stopWatch(ctx, &err)
defer watch.finish(ctx, &err)
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
defer cancel()
if err != nil {
if stopErr := c.InterruptAndConfirm(cleanup, session.Data.ID); stopErr != nil {
stopErr := c.InterruptAndConfirm(cleanup, session.Data.ID)
// Read after settlement so a late assistant turn remains available as
// failure evidence. Never replay a prompt to reconstruct missing turns.
if promptStarted && c.Backend == "v2" {
readCtx, readCancel := context.WithTimeout(context.WithoutCancel(ctx), 3*time.Second)
data, readErr := c.call(readCtx, "get", "/api/session/"+session.Data.ID+finalAssistantReadback, nil)
readCancel()
if readErr == nil && len(data) > 0 {
c.FormatAudit = RedactEvidence(string(data))
}
if watch == nil {
c.settleV2Usage(ctx, config.Budget, session.Data.ID)
}
}
if stopErr != nil {
err = fmt.Errorf("retained OpenCode session %s; interrupt failed: %w", session.Data.ID, errors.Join(err, stopErr, ErrStageCleanup))
return
}
@@ -351,10 +371,12 @@ func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, promp
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 {
// V2 /generate does not persist messages or update session usage. Every V2
// model call uses tracked prompting under the session's deny-all tool policy,
// including unbudgeted text-only stages, so its exact native turns survive.
if c.Backend == "v2" {
promptStarted = true
if _, err = c.call(ctx, "post", "/api/session/"+session.Data.ID+"/prompt", body); err != nil {
return "", err
}
if err = c.waitForSessionIdle(ctx, session.Data.ID); err != nil {
@@ -368,6 +390,12 @@ func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, promp
if err != nil {
return "", err
}
if !config.Budget.Enabled() {
// As with AgentClient, missing usage is not a generation failure when
// no limit is enabled (and a nil ledger needs no aggregate read).
c.settleV2Usage(ctx, config.Budget, session.Data.ID)
return text, nil
}
// 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)