Files
archie/internal/app/controlplane/workflow_settings.go
T
sam 64a4210b1f fix(controlplane): refresh a stale seeded resource and re-seed a removed one
A seed was consulted only for a resource kind the store did not hold, so a
document that changed in the build never reached a database an earlier
release had seeded. For workflow-definitions that is not a stale setting but
a total outage: the collection validates as one document, so a stored step
type this build no longer has fails every task's pin, whatever workflow the
task names. The seed's request ID was stable per kind and the store answers a
known request ID from resource_history, which outlives the resource, so the
documented recovery -- remove the resource and restart -- wrote nothing
either.

Seeds now carry one identity the reader can name, and a kind the store holds
is refreshed only when the seed itself wrote the newest revision: a
file-derived value stays the store's, and an operator's replacement is never
undone. The request ID is derived from the value, so an idempotent replay is
still one revision without making a second, changed document impossible. A
replay whose resource is gone re-creates it from the ledger's revision.

The shipped-vs-file-derived distinction is the seed's to make, so it is a
property of the definition (Defaults is set exactly for the kinds that ship a
document) rather than a check at each consumer.
2026-09-29 14:27:57 +10:00

88 lines
3.9 KiB
Go

package controlplane
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
const WorkflowExecutionSettingsKind = "workflow-execution-settings"
// defaultTaskRuntime bounds a task run when the stored settings predate the
// limit.
const defaultTaskRuntime = 4 * time.Hour
type executionSettingsDocument struct {
MaxModelToolSteps int `json:"max_model_tool_steps"`
MaxRuntimeSeconds int64 `json:"max_runtime_seconds" title:"Max seconds per agent call" doc:"Limit for one agent call inside a stage. 0 disables it."`
MaxConsecutiveGateFailures int `json:"max_consecutive_gate_failures"`
// MaxTaskRuntimeSeconds is a pointer so a document stored before the
// field existed reads as the default, while an explicit 0 disables it.
MaxTaskRuntimeSeconds *int64 `json:"max_task_runtime_seconds" title:"Max task runtime seconds" doc:"Limit for a whole task run; the task is parked when it passes. 0 disables it."`
}
// ImportWorkflowExecutionSettings preserves the focused bootstrap API for callers
// that do not own the complete legacy configuration document. It is the same
// producer rule as ImportConfig, at this narrower entry point: a kind the store
// holds is left alone, and a kind it does not hold is seeded under the one seed
// identity (see seedActor and seedRequestID).
func (s *Server) ImportWorkflowExecutionSettings(ctx context.Context, settings workflow.ExecutionSettings) (int64, error) {
resource, err := s.store.Resource(ctx, WorkflowExecutionSettingsKind)
if err == nil {
return resource.Version, nil
}
if !errors.Is(err, storecontract.ErrResourceNotFound) {
return 0, err
}
value, err := encodeSettings(settings)
if err != nil {
return 0, err
}
value, err = workflowDefinition().Decode(value)
if err != nil {
return 0, err
}
resource, err = s.store.PutResource(ctx, storecontract.ResourceWrite{Kind: WorkflowExecutionSettingsKind, Value: value, Actor: seedActor, Source: seedSource, RequestID: seedRequestID(WorkflowExecutionSettingsKind, value), ExpectedVersion: 0, At: time.Now().UTC()})
return resource.Version, err
}
func workflowDefinition() Definition {
return Definition{
Kind: WorkflowExecutionSettingsKind, Title: "Workflow execution settings", ApplyMode: "live", Document: executionSettingsDocument{},
Seed: func(cfg config.Config) any {
taskRuntime := time.Duration(cfg.Budgets.TaskWallClock)
if taskRuntime == 0 {
taskRuntime = defaultTaskRuntime
}
return executionSettingsDocument{cfg.Budgets.MaxSteps, int64(time.Duration(cfg.Budgets.WallClock) / time.Second), cfg.Budgets.GateMaxFailures, new(int64(taskRuntime / time.Second))}
},
Validate: func(input []byte) error { _, err := decodeSettings(input); return err },
}
}
func encodeSettings(s workflow.ExecutionSettings) ([]byte, error) {
return json.Marshal(executionSettingsDocument{s.MaxModelToolSteps, int64(s.MaxRuntime / time.Second), s.MaxConsecutiveGateFailures, new(int64(s.MaxTaskRuntime / time.Second))})
}
func decodeSettings(value []byte) (workflow.ExecutionSettings, error) {
var document executionSettingsDocument
if err := json.Unmarshal(value, &document); err != nil {
return workflow.ExecutionSettings{}, fmt.Errorf("%w: invalid JSON: %w", ErrValidation, err)
}
taskRuntime := defaultTaskRuntime
if document.MaxTaskRuntimeSeconds != nil {
taskRuntime = time.Duration(*document.MaxTaskRuntimeSeconds) * time.Second
}
settings := workflow.ExecutionSettings{MaxModelToolSteps: document.MaxModelToolSteps, MaxRuntime: time.Duration(document.MaxRuntimeSeconds) * time.Second, MaxConsecutiveGateFailures: document.MaxConsecutiveGateFailures, MaxTaskRuntime: taskRuntime}
if err := settings.Validate(); err != nil {
return workflow.ExecutionSettings{}, fmt.Errorf("%w: %w", ErrValidation, err)
}
return settings, nil
}