From 839b35f310b6cc66602a7c0bb6850733738a3a9e Mon Sep 17 00:00:00 2001 From: Mikhail Date: Sat, 29 Aug 2026 11:47:05 +0300 Subject: [PATCH] GEN2 M4 remediation: durable recovery and governed evolution --- adapters/postgres/work_repository.go | 115 ++++++++++++++++++ contracts/work/governed_evolution.go | 39 ++++++ contracts/work/long_running.go | 16 +++ contracts/work/perspective.go | 8 +- database/migrations/050_work_long_running.sql | 22 ++++ modules/epistemics/domain/outcome.go | 35 ++++++ modules/epistemics/domain/outcome_test.go | 21 ++++ modules/work/domain/governed_evolution.go | 76 ++++++++++++ .../work/domain/governed_evolution_test.go | 77 ++++++++++++ modules/work/domain/perspective.go | 2 +- modules/work/domain/perspective_test.go | 2 +- runtime/supervisor/durable_store.go | 94 ++++++++++++++ runtime/supervisor/supervisor_test.go | 32 +++++ 13 files changed, 536 insertions(+), 3 deletions(-) create mode 100644 contracts/work/governed_evolution.go create mode 100644 database/migrations/050_work_long_running.sql create mode 100644 modules/epistemics/domain/outcome.go create mode 100644 modules/epistemics/domain/outcome_test.go create mode 100644 modules/work/domain/governed_evolution.go create mode 100644 modules/work/domain/governed_evolution_test.go create mode 100644 runtime/supervisor/durable_store.go diff --git a/adapters/postgres/work_repository.go b/adapters/postgres/work_repository.go index 53659ed..7de7367 100644 --- a/adapters/postgres/work_repository.go +++ b/adapters/postgres/work_repository.go @@ -25,6 +25,121 @@ var ( ErrDevelopmentCandidateConflict = errors.New("development candidate conflict") ) +func (r *WorkRepository) SaveInvestigation(ctx context.Context, value work.Investigation, expectedRevision int64) error { + if !value.Valid() || expectedRevision < 0 { + return errors.New("invalid investigation revision") + } + attempts := make([]string, 0, len(value.AttemptRefs)) + for _, item := range value.AttemptRefs { + attempts = append(attempts, string(item)) + } + hypotheses := make([]string, 0, len(value.HypothesisRefs)) + for _, item := range value.HypothesisRefs { + hypotheses = append(hypotheses, string(item)) + } + findings := make([]string, 0, len(value.FindingRefs)) + for _, item := range value.FindingRefs { + findings = append(findings, string(item)) + } + blockers := append([]string(nil), value.Blockers...) + checkpoints := make([]string, 0, len(value.CheckpointRefs)) + for _, item := range value.CheckpointRefs { + checkpoints = append(checkpoints, string(item)) + } + if expectedRevision == 0 { + result, err := r.pool.Exec(ctx, `INSERT INTO work.long_running_work(investigation_id,objective,hypothesis_refs,attempt_refs,finding_refs,blockers,checkpoint_refs,next_step,state,revision,time_budget_millis,cost_budget_micros,max_attempts,recorded_at_unix_millis) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) ON CONFLICT(investigation_id) DO NOTHING`, value.ID, value.Objective, hypotheses, attempts, findings, blockers, checkpoints, value.NextStep, value.State, value.Revision, value.TimeBudgetMillis, value.CostBudgetMicros, value.MaxAttempts, value.RecordedAtUnixMillis) + if err != nil { + return err + } + if result.RowsAffected() != 1 { + return errors.New("investigation already exists") + } + return nil + } + result, err := r.pool.Exec(ctx, `UPDATE work.long_running_work SET objective=$2,hypothesis_refs=$3,attempt_refs=$4,finding_refs=$5,blockers=$6,checkpoint_refs=$7,next_step=$8,state=$9,revision=$10,time_budget_millis=$11,cost_budget_micros=$12,max_attempts=$13,recorded_at_unix_millis=$14 WHERE investigation_id=$1 AND revision=$15`, value.ID, value.Objective, hypotheses, attempts, findings, blockers, checkpoints, value.NextStep, value.State, value.Revision, value.TimeBudgetMillis, value.CostBudgetMicros, value.MaxAttempts, value.RecordedAtUnixMillis, expectedRevision) + if err != nil { + return err + } + if result.RowsAffected() != 1 { + return errors.New("investigation revision conflict") + } + return nil +} + +func (r *WorkRepository) LoadInvestigation(ctx context.Context, id work.LongRunningWorkID) (work.Investigation, error) { + var value work.Investigation + var hypotheses, attempts, findings, blockers, checkpoints []string + err := r.pool.QueryRow(ctx, `SELECT investigation_id,objective,hypothesis_refs,attempt_refs,finding_refs,blockers,checkpoint_refs,next_step,state,revision,time_budget_millis,cost_budget_micros,max_attempts,recorded_at_unix_millis FROM work.long_running_work WHERE investigation_id=$1`, id).Scan(&value.ID, &value.Objective, &hypotheses, &attempts, &findings, &blockers, &checkpoints, &value.NextStep, &value.State, &value.Revision, &value.TimeBudgetMillis, &value.CostBudgetMicros, &value.MaxAttempts, &value.RecordedAtUnixMillis) + if err != nil { + return work.Investigation{}, fmt.Errorf("load investigation: %w", err) + } + for _, item := range hypotheses { + value.HypothesisRefs = append(value.HypothesisRefs, work.EvidenceRef(item)) + } + for _, item := range attempts { + value.AttemptRefs = append(value.AttemptRefs, work.AgentRunID(item)) + } + for _, item := range findings { + value.FindingRefs = append(value.FindingRefs, work.EvidenceRef(item)) + } + value.Blockers = append(value.Blockers, blockers...) + for _, item := range checkpoints { + value.CheckpointRefs = append(value.CheckpointRefs, work.AgentCheckpointID(item)) + } + return value, nil +} + +// SaveInvestigationCheckpoint atomically records the checkpoint and marks its +// attempts verified. The unique key makes retries idempotent. +func (r *WorkRepository) SaveInvestigationCheckpoint(ctx context.Context, checkpoint work.InvestigationCheckpoint) error { + if !checkpoint.Valid() { + return errors.New("invalid investigation checkpoint") + } + tx, err := r.pool.Begin(ctx) + if err != nil { + return err + } + defer tx.Rollback(ctx) + if _, err = tx.Exec(ctx, `INSERT INTO work.investigation_checkpoints(checkpoint_id,investigation_id,completed_attempt_refs,completed_finding_refs,next_step,recorded_at_unix_millis) VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT(checkpoint_id) DO NOTHING`, checkpoint.ID, checkpoint.InvestigationID, stringIDs(checkpoint.CompletedAttemptRefs), stringEvidence(checkpoint.CompletedFindingRefs), checkpoint.NextStep, checkpoint.RecordedAtUnixMillis); err != nil { + return err + } + for _, attempt := range checkpoint.CompletedAttemptRefs { + if _, err = tx.Exec(ctx, `INSERT INTO work.investigation_attempts(investigation_id,attempt_ref,verified_at_unix_millis) VALUES($1,$2,$3) ON CONFLICT(investigation_id,attempt_ref) DO NOTHING`, checkpoint.InvestigationID, attempt, checkpoint.RecordedAtUnixMillis); err != nil { + return err + } + } + return tx.Commit(ctx) +} + +func (r *WorkRepository) UnverifiedInvestigationAttempts(ctx context.Context, id work.LongRunningWorkID, candidates []work.AgentRunID) ([]work.AgentRunID, error) { + result := make([]work.AgentRunID, 0, len(candidates)) + for _, candidate := range candidates { + var exists bool + if err := r.pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM work.investigation_attempts WHERE investigation_id=$1 AND attempt_ref=$2)`, id, candidate).Scan(&exists); err != nil { + return nil, err + } + if !exists { + result = append(result, candidate) + } + } + return result, nil +} + +func stringIDs(values []work.AgentRunID) []string { + result := make([]string, 0, len(values)) + for _, value := range values { + result = append(result, string(value)) + } + return result +} +func stringEvidence(values []work.EvidenceRef) []string { + result := make([]string, 0, len(values)) + for _, value := range values { + result = append(result, string(value)) + } + return result +} + func OpenWork(ctx context.Context, config ContinuityConfig) (*WorkRepository, error) { pool, err := openPool(ctx, config) if err != nil { diff --git a/contracts/work/governed_evolution.go b/contracts/work/governed_evolution.go new file mode 100644 index 0000000..7833845 --- /dev/null +++ b/contracts/work/governed_evolution.go @@ -0,0 +1,39 @@ +package work + +type EvolutionRunStage string + +const ( + EvolutionNeedRecorded EvolutionRunStage = "IMPROVEMENT_NEED" + EvolutionCandidateBuilt EvolutionRunStage = "CANDIDATE" + EvolutionIndependentlyEvaluated EvolutionRunStage = "INDEPENDENT_EVALUATION" + EvolutionSandboxed EvolutionRunStage = "SANDBOX_BUILD" + EvolutionBenchmarked EvolutionRunStage = "BENCHMARK" + EvolutionCanaried EvolutionRunStage = "CANARY" + EvolutionPromoted EvolutionRunStage = "PROMOTION" + EvolutionRolledBack EvolutionRunStage = "ROLLBACK" +) + +type GovernedEvolutionResult struct { + NeedRef string + CandidateRef string + EvaluationRef string + Stages []EvolutionRunStage + EvidenceRefs []EvidenceRef + LearningLedgerOutcomeRef string + Promoted bool + RollbackRef string +} + +// GovernedEvolutionOps are explicit gates. The caller supplies existing +// development executor/supervisor operations; this interface grants no new +// authority and has no autonomous promotion implementation. +type GovernedEvolutionOps struct { + CreateCandidate func() (EvolutionCandidate, error) + Evaluate func(EvolutionCandidate) (EvolutionEvaluation, error) + SandboxBuild func(EvolutionCandidate) (EvidenceRef, error) + Benchmark func(EvolutionCandidate) (EvidenceRef, bool, error) + Canary func(EvolutionCandidate) (EvidenceRef, bool, error) + Promote func(EvolutionCandidate, EvolutionEvaluation) (string, error) + Rollback func(EvolutionCandidate) (string, error) + LearningLedgerOutcome func(GovernedEvolutionResult) (string, error) +} diff --git a/contracts/work/long_running.go b/contracts/work/long_running.go index 369b76c..a6744bc 100644 --- a/contracts/work/long_running.go +++ b/contracts/work/long_running.go @@ -30,6 +30,22 @@ type Investigation struct { RecordedAtUnixMillis int64 } +// InvestigationCheckpoint is the durable boundary between attempts. A +// checkpoint is written only after an attempt has been verified, so a worker +// restart can resume at NextStep without replaying that attempt. +type InvestigationCheckpoint struct { + ID AgentCheckpointID + InvestigationID LongRunningWorkID + CompletedAttemptRefs []AgentRunID + CompletedFindingRefs []EvidenceRef + NextStep string + RecordedAtUnixMillis int64 +} + +func (c InvestigationCheckpoint) Valid() bool { + return c.ID != "" && c.InvestigationID != "" && c.NextStep != "" && c.RecordedAtUnixMillis > 0 +} + func (w Investigation) Valid() bool { return w.ID != "" && w.Objective != "" && w.State != "" && w.Revision > 0 && w.TimeBudgetMillis >= 0 && w.CostBudgetMicros >= 0 && w.MaxAttempts >= 0 && w.RecordedAtUnixMillis > 0 } diff --git a/contracts/work/perspective.go b/contracts/work/perspective.go index bc4de0f..dc4762f 100644 --- a/contracts/work/perspective.go +++ b/contracts/work/perspective.go @@ -4,7 +4,10 @@ type PerspectiveID string type PerspectiveSpec struct { ID PerspectiveID ObjectiveLens string + FocusRef string RetrievalProfileRef string + SkillRefs []string + ToolRefs []string EvaluationCriteria []string CapabilityRequirements []string ExpectedOutputShape string @@ -13,6 +16,9 @@ type PerspectiveSpec struct { type SpecialistAssignment struct { Perspective PerspectiveSpec SubtaskRef string + FocusRef string + SkillRefs []string + ToolRefs []string EvidenceRefs []EvidenceRef } type TeamDecision struct { @@ -22,5 +28,5 @@ type TeamDecision struct { } func (p PerspectiveSpec) Valid() bool { - return p.ID != "" && p.ObjectiveLens != "" && len(p.EvaluationCriteria) > 0 && p.ExpectedOutputShape != "" + return p.ID != "" && p.ObjectiveLens != "" && p.FocusRef != "" && len(p.SkillRefs) > 0 && len(p.ToolRefs) > 0 && len(p.EvaluationCriteria) > 0 && p.ExpectedOutputShape != "" } diff --git a/database/migrations/050_work_long_running.sql b/database/migrations/050_work_long_running.sql new file mode 100644 index 0000000..69888e7 --- /dev/null +++ b/database/migrations/050_work_long_running.sql @@ -0,0 +1,22 @@ +BEGIN; +SET ROLE work_role; +CREATE TABLE IF NOT EXISTS work.long_running_work ( + investigation_id TEXT PRIMARY KEY, objective TEXT NOT NULL, + hypothesis_refs TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[], attempt_refs TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[], + finding_refs TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[], blockers TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[], + checkpoint_refs TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[], next_step TEXT NOT NULL, state TEXT NOT NULL, + revision BIGINT NOT NULL, time_budget_millis BIGINT NOT NULL, cost_budget_micros BIGINT NOT NULL, + max_attempts INTEGER NOT NULL, recorded_at_unix_millis BIGINT NOT NULL, + CHECK (investigation_id <> ''), CHECK (objective <> ''), CHECK (next_step <> ''), CHECK (revision > 0), CHECK (time_budget_millis >= 0), CHECK (cost_budget_micros >= 0), CHECK (max_attempts >= 0), CHECK (recorded_at_unix_millis > 0) +); +CREATE TABLE IF NOT EXISTS work.investigation_checkpoints ( + checkpoint_id TEXT PRIMARY KEY, investigation_id TEXT NOT NULL, completed_attempt_refs TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[], + completed_finding_refs TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[], next_step TEXT NOT NULL, recorded_at_unix_millis BIGINT NOT NULL, + CHECK (checkpoint_id <> ''), CHECK (investigation_id <> ''), CHECK (next_step <> ''), CHECK (recorded_at_unix_millis > 0) +); +CREATE TABLE IF NOT EXISTS work.investigation_attempts ( + investigation_id TEXT NOT NULL, attempt_ref TEXT NOT NULL, verified_at_unix_millis BIGINT NOT NULL, + PRIMARY KEY (investigation_id, attempt_ref), CHECK (investigation_id <> ''), CHECK (attempt_ref <> ''), CHECK (verified_at_unix_millis > 0) +); +RESET ROLE; +COMMIT; diff --git a/modules/epistemics/domain/outcome.go b/modules/epistemics/domain/outcome.go new file mode 100644 index 0000000..e66e866 --- /dev/null +++ b/modules/epistemics/domain/outcome.go @@ -0,0 +1,35 @@ +package domain + +import ( + "errors" + "git.cin.su/m/Assistant/contracts/epistemics" + "git.cin.su/m/Assistant/platform/ids" +) + +var ErrInvalidOutcomeAssessment = errors.New("invalid world outcome assessment") + +// AssessWorldOutcome keeps Commit's execution fact separate from Epistemics' +// observation. No observation means UNKNOWN; a transport receipt alone never +// becomes success. +func AssessWorldOutcome(principalID, attemptRef, expectedEffect string, receiptRefs []string, observationRefs []epistemics.ObservationID, claimRefs []epistemics.ClaimID, evidenceRefs []epistemics.EvidenceRef, observedState epistemics.WorldOutcomeState, confidence, assessedAt int64) (epistemics.WorldOutcomeAssessment, error) { + if principalID == "" || attemptRef == "" || expectedEffect == "" || assessedAt < 1 || confidence < 0 || confidence > 1000 { + return epistemics.WorldOutcomeAssessment{}, ErrInvalidOutcomeAssessment + } + state := epistemics.OutcomeUnknownState + if len(observationRefs) == 0 { + state = epistemics.OutcomeUnknownState + } else if observedState == epistemics.OutcomeVerifiedSuccess || observedState == epistemics.OutcomeVerifiedPartial || observedState == epistemics.OutcomeVerifiedFailure || observedState == epistemics.OutcomeConflicted { + state = observedState + } else { + state = epistemics.OutcomeVerifiedPartial + } + id, err := ids.New() + if err != nil { + return epistemics.WorldOutcomeAssessment{}, err + } + value := epistemics.WorldOutcomeAssessment{ID: string(id), PrincipalID: principalID, ActionAttemptRef: attemptRef, ExecutionReceiptRefs: append([]string(nil), receiptRefs...), ExpectedEffectRef: expectedEffect, ObservationRefs: append([]epistemics.ObservationID(nil), observationRefs...), ClaimRefs: append([]epistemics.ClaimID(nil), claimRefs...), State: state, ConfidenceMillis: confidence, EvidenceRefs: append([]epistemics.EvidenceRef(nil), evidenceRefs...), AssessedAt: assessedAt} + if !value.Valid() { + return epistemics.WorldOutcomeAssessment{}, ErrInvalidOutcomeAssessment + } + return value, nil +} diff --git a/modules/epistemics/domain/outcome_test.go b/modules/epistemics/domain/outcome_test.go new file mode 100644 index 0000000..72f0e60 --- /dev/null +++ b/modules/epistemics/domain/outcome_test.go @@ -0,0 +1,21 @@ +package domain_test + +import ( + "git.cin.su/m/Assistant/contracts/epistemics" + "git.cin.su/m/Assistant/modules/epistemics/domain" + "testing" +) + +func TestExecutionReceiptWithoutObservationRemainsUnknown(t *testing.T) { + value, err := domain.AssessWorldOutcome("principal", "attempt", "effect", []string{"receipt"}, nil, nil, nil, epistemics.OutcomeVerifiedSuccess, 1000, 1) + if err != nil || value.State != epistemics.OutcomeUnknownState { + t.Fatalf("value=%+v err=%v", value, err) + } +} + +func TestObservedOutcomeCanBeVerifiedSeparately(t *testing.T) { + value, err := domain.AssessWorldOutcome("principal", "attempt", "effect", []string{"receipt"}, []epistemics.ObservationID{"observation"}, []epistemics.ClaimID{"claim"}, []epistemics.EvidenceRef{"evidence"}, epistemics.OutcomeVerifiedSuccess, 900, 1) + if err != nil || value.State != epistemics.OutcomeVerifiedSuccess || len(value.EvidenceRefs) != 1 { + t.Fatalf("value=%+v err=%v", value, err) + } +} diff --git a/modules/work/domain/governed_evolution.go b/modules/work/domain/governed_evolution.go new file mode 100644 index 0000000..5ebfb15 --- /dev/null +++ b/modules/work/domain/governed_evolution.go @@ -0,0 +1,76 @@ +package domain + +import ( + "errors" + "git.cin.su/m/Assistant/contracts/work" +) + +var ErrGovernedEvolution = errors.New("governed evolution gate failed") + +func RunGovernedEvolution(needRef string, ops work.GovernedEvolutionOps) (work.GovernedEvolutionResult, error) { + result := work.GovernedEvolutionResult{NeedRef: needRef, Stages: []work.EvolutionRunStage{work.EvolutionNeedRecorded}} + if needRef == "" || ops.CreateCandidate == nil || ops.Evaluate == nil || ops.SandboxBuild == nil || ops.Benchmark == nil || ops.Promote == nil || ops.Rollback == nil || ops.LearningLedgerOutcome == nil { + return result, ErrGovernedEvolution + } + candidate, err := ops.CreateCandidate() + if err != nil { + return result, err + } + if candidate.ID == "" || candidate.ProposalRef == "" { + return result, ErrGovernedEvolution + } + result.CandidateRef = candidate.ID + result.Stages = append(result.Stages, work.EvolutionCandidateBuilt) + evaluation, err := ops.Evaluate(candidate) + if err != nil || !evaluation.ValidFor(candidate) { + return result, ErrGovernedEvolution + } + result.EvaluationRef = evaluation.CandidateRef + result.Stages = append(result.Stages, work.EvolutionIndependentlyEvaluated) + sandboxEvidence, err := ops.SandboxBuild(candidate) + if err != nil || sandboxEvidence == "" { + return result, ErrGovernedEvolution + } + candidate.SandboxEvidenceRefs = append(candidate.SandboxEvidenceRefs, sandboxEvidence) + result.EvidenceRefs = append(result.EvidenceRefs, sandboxEvidence) + result.Stages = append(result.Stages, work.EvolutionSandboxed) + benchmarkEvidence, passed, err := ops.Benchmark(candidate) + if err != nil || !passed || benchmarkEvidence == "" { + result = rollbackEvolution(result, candidate, ops) + return result, ErrGovernedEvolution + } + candidate.BenchmarkEvidenceRefs = append(candidate.BenchmarkEvidenceRefs, benchmarkEvidence) + result.EvidenceRefs = append(result.EvidenceRefs, benchmarkEvidence) + result.Stages = append(result.Stages, work.EvolutionBenchmarked) + if ops.Canary != nil { + canaryEvidence, passed, canaryErr := ops.Canary(candidate) + if canaryErr != nil || !passed || canaryEvidence == "" { + result = rollbackEvolution(result, candidate, ops) + return result, ErrGovernedEvolution + } + result.EvidenceRefs = append(result.EvidenceRefs, canaryEvidence) + result.Stages = append(result.Stages, work.EvolutionCanaried) + } + promotionRef, err := ops.Promote(candidate, evaluation) + if err != nil || promotionRef == "" { + result = rollbackEvolution(result, candidate, ops) + return result, ErrGovernedEvolution + } + result.Promoted = true + result.Stages = append(result.Stages, work.EvolutionPromoted) + result.LearningLedgerOutcomeRef, err = ops.LearningLedgerOutcome(result) + if err != nil || result.LearningLedgerOutcomeRef == "" { + return result, ErrGovernedEvolution + } + return result, nil +} + +func rollbackEvolution(result work.GovernedEvolutionResult, candidate work.EvolutionCandidate, ops work.GovernedEvolutionOps) work.GovernedEvolutionResult { + ref, err := ops.Rollback(candidate) + if err == nil && ref != "" { + result.RollbackRef = ref + result.Stages = append(result.Stages, work.EvolutionRolledBack) + result.LearningLedgerOutcomeRef, _ = ops.LearningLedgerOutcome(result) + } + return result +} diff --git a/modules/work/domain/governed_evolution_test.go b/modules/work/domain/governed_evolution_test.go new file mode 100644 index 0000000..bafb99f --- /dev/null +++ b/modules/work/domain/governed_evolution_test.go @@ -0,0 +1,77 @@ +package domain_test + +import ( + "git.cin.su/m/Assistant/contracts/work" + "git.cin.su/m/Assistant/modules/work/domain" + "testing" +) + +func TestGovernedEvolutionExecutesGatesAndLedgerOutcome(t *testing.T) { + calls := []string{} + result, err := domain.RunGovernedEvolution("need-1", work.GovernedEvolutionOps{ + CreateCandidate: func() (work.EvolutionCandidate, error) { + calls = append(calls, "candidate") + return work.EvolutionCandidate{ID: "candidate-1", ProposalRef: "proposal-1", CreatorIdentity: "creator", RollbackRef: "rollback-plan"}, nil + }, + Evaluate: func(c work.EvolutionCandidate) (work.EvolutionEvaluation, error) { + calls = append(calls, "evaluate") + return work.EvolutionEvaluation{CandidateRef: c.ID, EvaluatorIdentity: "independent", Approved: true, MustNotWorsenPassed: true, EvidenceRefs: []work.EvidenceRef{"eval"}, RecordedAtUnixMillis: 1}, nil + }, + SandboxBuild: func(work.EvolutionCandidate) (work.EvidenceRef, error) { + calls = append(calls, "sandbox-build") + return "sandbox", nil + }, + Benchmark: func(work.EvolutionCandidate) (work.EvidenceRef, bool, error) { + calls = append(calls, "benchmark") + return "benchmark", true, nil + }, + Canary: func(work.EvolutionCandidate) (work.EvidenceRef, bool, error) { + calls = append(calls, "canary") + return "canary", true, nil + }, + Promote: func(work.EvolutionCandidate, work.EvolutionEvaluation) (string, error) { + calls = append(calls, "promote") + return "promotion", nil + }, + Rollback: func(work.EvolutionCandidate) (string, error) { + calls = append(calls, "rollback") + return "rollback", nil + }, + LearningLedgerOutcome: func(work.GovernedEvolutionResult) (string, error) { + calls = append(calls, "ledger") + return "ledger-outcome", nil + }, + }) + if err != nil || !result.Promoted || result.LearningLedgerOutcomeRef != "ledger-outcome" { + t.Fatalf("result=%+v err=%v", result, err) + } + want := []string{"candidate", "evaluate", "sandbox-build", "benchmark", "canary", "promote", "ledger"} + if len(calls) != len(want) { + t.Fatalf("calls=%v", calls) + } + for i := range want { + if calls[i] != want[i] { + t.Fatalf("calls=%v", calls) + } + } +} + +func TestGovernedEvolutionRollsBackOnMustNotWorsenFailure(t *testing.T) { + rolledBack := false + result, err := domain.RunGovernedEvolution("need-1", work.GovernedEvolutionOps{ + CreateCandidate: func() (work.EvolutionCandidate, error) { + return work.EvolutionCandidate{ID: "candidate-1", ProposalRef: "proposal-1", CreatorIdentity: "creator", RollbackRef: "rollback-plan"}, nil + }, + Evaluate: func(c work.EvolutionCandidate) (work.EvolutionEvaluation, error) { + return work.EvolutionEvaluation{CandidateRef: c.ID, EvaluatorIdentity: "independent", Approved: true, MustNotWorsenPassed: true, EvidenceRefs: []work.EvidenceRef{"eval"}, RecordedAtUnixMillis: 1}, nil + }, + SandboxBuild: func(work.EvolutionCandidate) (work.EvidenceRef, error) { return "sandbox", nil }, Benchmark: func(work.EvolutionCandidate) (work.EvidenceRef, bool, error) { return "benchmark", false, nil }, + Promote: func(work.EvolutionCandidate, work.EvolutionEvaluation) (string, error) { + t.Fatal("promotion bypassed failed benchmark") + return "", nil + }, Rollback: func(work.EvolutionCandidate) (string, error) { rolledBack = true; return "rollback-evidence", nil }, LearningLedgerOutcome: func(r work.GovernedEvolutionResult) (string, error) { return "ledger-rollback", nil }, + }) + if err == nil || !rolledBack || result.RollbackRef != "rollback-evidence" || result.Promoted { + t.Fatalf("result=%+v err=%v", result, err) + } +} diff --git a/modules/work/domain/perspective.go b/modules/work/domain/perspective.go index e6f1486..3a90a84 100644 --- a/modules/work/domain/perspective.go +++ b/modules/work/domain/perspective.go @@ -11,7 +11,7 @@ func AssembleSpecialists(p work.PerspectiveSpec, subtasks []string) work.TeamDec } assignments := make([]work.SpecialistAssignment, 0, len(subtasks)) for _, s := range subtasks { - assignments = append(assignments, work.SpecialistAssignment{Perspective: p, SubtaskRef: s}) + assignments = append(assignments, work.SpecialistAssignment{Perspective: p, SubtaskRef: s, FocusRef: p.FocusRef, SkillRefs: append([]string(nil), p.SkillRefs...), ToolRefs: append([]string(nil), p.ToolRefs...)}) } return work.TeamDecision{UseTeam: true, Reason: "independent subtasks justify bounded perspectives", Assignments: assignments} } diff --git a/modules/work/domain/perspective_test.go b/modules/work/domain/perspective_test.go index 05c5061..472f325 100644 --- a/modules/work/domain/perspective_test.go +++ b/modules/work/domain/perspective_test.go @@ -7,7 +7,7 @@ import ( ) func TestPerspectiveTeamIsBoundedByIndependentSubtasks(t *testing.T) { - p := work.PerspectiveSpec{ID: "architect", ObjectiveLens: "architecture", EvaluationCriteria: []string{"correctness"}, ExpectedOutputShape: "finding"} + p := work.PerspectiveSpec{ID: "architect", ObjectiveLens: "architecture", FocusRef: "focus-1", SkillRefs: []string{"skill-1"}, ToolRefs: []string{"tool-1"}, EvaluationCriteria: []string{"correctness"}, ExpectedOutputShape: "finding"} if d := domain.AssembleSpecialists(p, []string{"a"}); d.UseTeam { t.Fatal("team for one subtask") } diff --git a/runtime/supervisor/durable_store.go b/runtime/supervisor/durable_store.go new file mode 100644 index 0000000..f98c4e1 --- /dev/null +++ b/runtime/supervisor/durable_store.go @@ -0,0 +1,94 @@ +package supervisor + +import ( + "encoding/json" + "errors" + "os" + "path/filepath" +) + +// DurableStore is supervisor-owned external control-plane state. It is not a +// candidate Assistant process-local recovery cache. +type DurableStore struct{ path string } + +func NewDurableStore(path string) (*DurableStore, error) { + if path == "" || !filepath.IsAbs(path) { + return nil, errors.New("supervisor state path must be absolute") + } + return &DurableStore{path: path}, nil +} + +func (s *DurableStore) Save(value Lifeboat) error { + if s == nil || s.path == "" { + return errors.New("supervisor state store is unavailable") + } + payload, err := json.Marshal(value) + if err != nil { + return err + } + if err := os.MkdirAll(filepath.Dir(s.path), 0700); err != nil { + return err + } + temporary, err := os.CreateTemp(filepath.Dir(s.path), ".lifeboat-*.tmp") + if err != nil { + return err + } + temporaryName := temporary.Name() + defer os.Remove(temporaryName) + if err = temporary.Chmod(0600); err == nil { + _, err = temporary.Write(payload) + } + if closeErr := temporary.Close(); err == nil { + err = closeErr + } + if err != nil { + return err + } + return os.Rename(temporaryName, s.path) +} + +func (s *DurableStore) Load() (Lifeboat, error) { + if s == nil || s.path == "" { + return Lifeboat{}, errors.New("supervisor state store is unavailable") + } + payload, err := os.ReadFile(s.path) + if err != nil { + return Lifeboat{}, err + } + var value Lifeboat + if err := json.Unmarshal(payload, &value); err != nil { + return Lifeboat{}, err + } + return value, nil +} + +// PersistentLifeboat reloads before every transition and commits after it. +type PersistentLifeboat struct{ store *DurableStore } + +func NewPersistentLifeboat(store *DurableStore) *PersistentLifeboat { + return &PersistentLifeboat{store: store} +} +func (p *PersistentLifeboat) transition(fn func(*Lifeboat) bool) (bool, error) { + value, err := p.store.Load() + if err != nil && !os.IsNotExist(err) { + return false, err + } + ok := fn(&value) + if !ok { + return false, nil + } + if err := p.store.Save(value); err != nil { + return false, err + } + return true, nil +} +func (p *PersistentLifeboat) Start(candidate Release) (bool, error) { + return p.transition(func(value *Lifeboat) bool { return value.Start(candidate) }) +} +func (p *PersistentLifeboat) Record(observation Observation) (bool, error) { + return p.transition(func(value *Lifeboat) bool { return value.Record(observation) }) +} +func (p *PersistentLifeboat) Rollback() (bool, error) { + return p.transition(func(value *Lifeboat) bool { return value.Rollback() }) +} +func (p *PersistentLifeboat) Snapshot() (Lifeboat, error) { return p.store.Load() } diff --git a/runtime/supervisor/supervisor_test.go b/runtime/supervisor/supervisor_test.go index 6aa8704..7fa4ee6 100644 --- a/runtime/supervisor/supervisor_test.go +++ b/runtime/supervisor/supervisor_test.go @@ -2,6 +2,7 @@ package supervisor_test import ( "git.cin.su/m/Assistant/runtime/supervisor" + "path/filepath" "testing" ) @@ -15,3 +16,34 @@ func TestLifeboatRollsBackCandidateToLastKnownGood(t *testing.T) { t.Fatalf("%#v", l) } } + +func TestPersistentLifeboatRestoresLastKnownGoodAfterCandidateFailure(t *testing.T) { + store, err := supervisor.NewDurableStore(filepath.Join(t.TempDir(), "control", "lifeboat.json")) + if err != nil { + t.Fatal(err) + } + first := supervisor.NewPersistentLifeboat(store) + if ok, err := first.Start(supervisor.Release{ID: "good", Version: "1"}); err != nil || !ok { + t.Fatalf("start good: %v %v", ok, err) + } + if ok, err := first.Record(supervisor.Observation{ReleaseID: "good", Ready: true, ObservedAtUnixMillis: 1}); err != nil || !ok { + t.Fatalf("record good: %v %v", ok, err) + } + if ok, err := first.Start(supervisor.Release{ID: "candidate", Version: "2"}); err != nil || !ok { + t.Fatalf("start candidate: %v %v", ok, err) + } + if ok, err := first.Record(supervisor.Observation{ReleaseID: "candidate", Ready: false, Reason: "candidate crashed", ObservedAtUnixMillis: 2}); err != nil || !ok { + t.Fatalf("record failure: %v %v", ok, err) + } + second := supervisor.NewPersistentLifeboat(store) + if ok, err := second.Rollback(); err != nil || !ok { + t.Fatalf("rollback after restart: %v %v", ok, err) + } + state, err := second.Snapshot() + if err != nil { + t.Fatal(err) + } + if state.Current.ID != "good" || state.Current.State != supervisor.ReleaseRolledBack || state.LastKnownGood.ID != "good" { + t.Fatalf("state=%+v", state) + } +}