Retain OpenCode sessions and verify fresh retry isolation
This commit is contained in:
+33
-10
@@ -21,14 +21,15 @@ const maxModelResponse = 1 << 20
|
||||
// OpenCodeClient reuses the selected OpenCode CLI and its authenticated accounts.
|
||||
// No API keys are copied into pipeline definitions, subprocess arguments, or logs.
|
||||
// A fresh location-scoped session avoids unrelated user conversation history and
|
||||
// is deleted after generation. Generation is text-only: no agent tools are run.
|
||||
// is retained for debugging. Generation is text-only: no agent tools are run.
|
||||
type OpenCodeClient struct {
|
||||
Directory string // empty uses the pipeline server's working directory
|
||||
Backend string
|
||||
Reference string
|
||||
FormatAudit string
|
||||
connection *opencode.Connection
|
||||
command func(context.Context, string, ...string) ([]byte, error)
|
||||
Directory string // empty uses the pipeline server's working directory
|
||||
Backend string
|
||||
Reference string
|
||||
FormatAudit string
|
||||
OnGenerationSession func(id, backend, reference string, active bool) error
|
||||
connection *opencode.Connection
|
||||
command func(context.Context, string, ...string) ([]byte, error)
|
||||
}
|
||||
|
||||
type openCodeError struct {
|
||||
@@ -182,11 +183,33 @@ func (c OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt
|
||||
defer func() {
|
||||
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
defer cancel()
|
||||
if _, cleanupErr := c.call(cleanup, "delete", "/api/session/"+session.Data.ID, nil); cleanupErr != nil {
|
||||
text = ""
|
||||
err = &openCodeError{operation: "session cleanup", cause: errors.Join(err, cleanupErr)}
|
||||
if err != nil {
|
||||
if stopErr := c.Interrupt(cleanup, session.Data.ID); stopErr != nil {
|
||||
err = fmt.Errorf("retained OpenCode session %s; interrupt failed: %w", session.Data.ID, errors.Join(err, stopErr))
|
||||
return
|
||||
}
|
||||
}
|
||||
if c.OnGenerationSession != nil {
|
||||
if persistErr := c.OnGenerationSession(session.Data.ID, c.Backend, c.Reference, false); persistErr != nil {
|
||||
text = ""
|
||||
err = errors.Join(err, persistErr)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("retained OpenCode session %s: %w", session.Data.ID, err)
|
||||
}
|
||||
}()
|
||||
if c.connection != nil && c.Backend == "v1" {
|
||||
c.Reference, err = c.connection.Reference()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
if c.OnGenerationSession != nil {
|
||||
if err = c.OnGenerationSession(session.Data.ID, c.Backend, c.Reference, true); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
response, err := c.call(ctx, "post", "/api/session/"+session.Data.ID+"/generate", body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
Reference in New Issue
Block a user