planstore

package
v0.7.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const RepoContextSchemaVersion = 1

RepoContextSchemaVersion is the current schema version for RepoContext JSON documents persisted on a plan. Bump when the shape changes; readers should tolerate older versions or ignore unknown fields.

Variables

View Source
var ErrNotFound = errors.New("plan not found")

Functions

func InitSchema added in v0.5.0

func InitSchema(ctx context.Context, exec libdbexec.Exec) error

InitSchema creates the plans and plan_steps tables if they do not exist.

NOTE: In production the schema is applied by runtimetypes.SchemaSQLite / runtimetypes.Schema (loaded in internal/contenoxcli/db_util.go and similar call sites). This function exists for unit tests that spin up an in-memory planstore without the full runtime. Keep the two schema shapes in sync whenever a column is added — see runtimetypes/schema_sqlite.sql for the canonical DDL.

func ValidOutcomes added in v0.7.0

func ValidOutcomes() map[string]struct{}

ValidOutcomes locks the outcome enum. Kept as a function (not a package var) to avoid data-race concerns if a caller ever mutates a returned map.

Types

type FailureClass added in v0.7.0

type FailureClass string

FailureClass tags WHY a step failed, so callers (e.g. plan next --auto) can decide whether to auto-replan, retry transparently, or surface to the user.

FailureClassEmpty is the default for unknown / legacy rows. FailureClassLogic is the catch-all for non-transient model/tool failures (e.g. the model said "I can't do X"). FailureClassCapacity means the chain hit a context-length or token-budget wall; the plan can usefully be replanned into smaller steps. FailureClassTransient is reserved for retries-exhausted transient HTTP errors (rate-limit / 5xx / timeout).

const (
	FailureClassEmpty     FailureClass = ""
	FailureClassLogic     FailureClass = "logic"
	FailureClassCapacity  FailureClass = "capacity"
	FailureClassTransient FailureClass = "transient"
)

type KeyFileNote added in v0.7.0

type KeyFileNote struct {
	Path string `json:"path"`
	Role string `json:"role,omitempty"`
}

KeyFileNote attaches a short role description to a notable path, e.g. {Path: "internal/contenoxcli/cli.go", Role: "CLI entrypoint"}.

type Plan

type Plan struct {
	ID        string     `json:"id"`
	Name      string     `json:"name"`
	Goal      string     `json:"goal"`
	Status    PlanStatus `json:"status"`
	SessionID string     `json:"session_id"`
	// CompiledChainJSON is the full [plancompile.Compile] output JSON for incremental Next (optional).
	CompiledChainJSON string `json:"compiled_chain_json,omitempty"`
	// CompiledChainID is the TaskChainDefinition.ID used when compiling (for invalidation).
	CompiledChainID string `json:"compiled_chain_id,omitempty"`
	// CompileExecutorChainID is the executor chain ID used when compiling (for invalidation).
	CompileExecutorChainID string `json:"compile_executor_chain_id,omitempty"`
	// RepoContextJSON is a [RepoContext] document produced by the explorer chain
	// and rendered into every step's seed prompt as {{var:repo_context}}.
	// Empty string means the plan has no explored context (current default).
	RepoContextJSON string    `json:"repo_context_json,omitempty"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
}

Plan maps to the plans table.

type PlanStatus

type PlanStatus string

PlanStatus represents the current state of a plan.

const (
	PlanStatusActive    PlanStatus = "active"
	PlanStatusCompleted PlanStatus = "completed"
	PlanStatusArchived  PlanStatus = "archived"
)

type PlanStep

type PlanStep struct {
	ID              string     `json:"id"`
	PlanID          string     `json:"plan_id"`
	Ordinal         int        `json:"ordinal"`
	Description     string     `json:"description"`
	Status          StepStatus `json:"status"`
	ExecutionResult string     `json:"execution_result"`
	ExecutedAt      time.Time  `json:"executed_at"` // Zero time if not executed

	// Summary is a JSON document (schema: outcome/summary/artifacts/handover_for_next/caveats)
	// produced by the summarizer chain and persisted by the plan_summary persist hook.
	// Empty string means NULL (legacy row or summarizer fell through to fallback).
	Summary string `json:"summary,omitempty"`
	// ChatHistoryJSON is the raw taskengine.ChatHistory from the executor subgraph,
	// preserved for debugging and for Retry prior-attempt context.
	ChatHistoryJSON string `json:"chat_history_json,omitempty"`
	// SummaryError is the raw summarizer output + parse/validation error when
	// both the first summarizer attempt and its repair attempt failed validation.
	// Populated only on fallback-path persistence.
	SummaryError string `json:"summary_error,omitempty"`
	// LastFailureSummary is the Summary (or ExecutionResult fallback) from the
	// previous failed attempt of this step, moved here by Retry. Lets the next
	// attempt's summarizer see why the prior try failed.
	LastFailureSummary string `json:"last_failure_summary,omitempty"`
	// FailureClass classifies the most recent failure so callers can pick a
	// recovery strategy (retry / replan / surface). Empty when the step is not
	// failed or when the failure was not classified.
	FailureClass FailureClass `json:"failure_class,omitempty"`
}

PlanStep maps to the plan_steps table.

type RepoContext added in v0.7.0

type RepoContext struct {
	SchemaVersion int           `json:"schema_version"`
	RepoRoot      string        `json:"repo_root,omitempty"`
	Languages     []string      `json:"languages,omitempty"`
	EntryPoints   []string      `json:"entry_points,omitempty"`
	BuildCommands []string      `json:"build_commands,omitempty"`
	TestCommands  []string      `json:"test_commands,omitempty"`
	Conventions   []string      `json:"conventions,omitempty"`
	KeyFiles      []KeyFileNote `json:"key_files,omitempty"`
	Caveats       []string      `json:"caveats,omitempty"`
	GeneratedAt   time.Time     `json:"generated_at,omitempty"`
}

RepoContext is a typed snapshot of a workspace's high-signal facts produced by the explorer chain (chain-plan-explorer.json) and persisted on Plan.RepoContextJSON. It is rendered into the seed prompt as {{var:repo_context}} so each plan step starts with concrete file paths, build / test commands, and conventions instead of cold exploration.

Fields are intentionally generic — the explorer model fills only what it can verify with read-only tools (filesystem grep / read). Any field may be empty; consumers must not rely on any particular field being populated.

type StepStatus

type StepStatus string

StepStatus represents the current state of a plan step.

const (
	StepStatusPending   StepStatus = "pending"
	StepStatusRunning   StepStatus = "running"
	StepStatusCompleted StepStatus = "completed"
	StepStatusFailed    StepStatus = "failed"
	StepStatusSkipped   StepStatus = "skipped"
)

type Store

type Store interface {
	// Plan operations
	CreatePlan(ctx context.Context, plan *Plan) error
	GetPlanByID(ctx context.Context, id string) (*Plan, error)
	GetPlanByName(ctx context.Context, name string) (*Plan, error)
	// GetActivePlan returns the most recently updated active plan, or ErrNotFound.
	GetActivePlan(ctx context.Context) (*Plan, error)
	ListPlans(ctx context.Context) ([]*Plan, error)
	DeletePlan(ctx context.Context, id string) error
	UpdatePlanStatus(ctx context.Context, planID string, status PlanStatus) error
	// UpdatePlanCompiledChain persists compile output for the plan (empty json clears compile cache).
	UpdatePlanCompiledChain(ctx context.Context, planID string, compiledChainJSON, compiledChainID, executorChainID string) error
	// UpdatePlanRepoContext persists the RepoContext JSON for the plan.
	// Pass empty string to clear it. Compile cache is NOT invalidated by this
	// call; the seed-prompt template var is read at run time.
	UpdatePlanRepoContext(ctx context.Context, planID string, repoContextJSON string) error

	// Step operations
	CreatePlanSteps(ctx context.Context, steps ...*PlanStep) error
	ListPlanSteps(ctx context.Context, planID string) ([]*PlanStep, error)
	UpdatePlanStepStatus(ctx context.Context, stepID string, status StepStatus, result string) error
	DeletePendingPlanSteps(ctx context.Context, planID string) error
	// ClaimNextPendingStep atomically marks the next pending step as running
	// and returns it. Returns ErrNotFound when no pending step exists.
	ClaimNextPendingStep(ctx context.Context, planID string) (*PlanStep, error)

	// UpdatePlanStepSummary persists a validated summary JSON + raw chat history for the step.
	// Called by the plan_summary persist hook on successful validation.
	UpdatePlanStepSummary(ctx context.Context, stepID string, summaryJSON, chatHistoryJSON string) error
	// UpdatePlanStepSummaryFailure persists the raw summarizer output + parse/validation error
	// and updates ExecutionResult to a fallback string so the next step still has context.
	// Called by the plan_summary fallback hook when validation failed twice.
	UpdatePlanStepSummaryFailure(ctx context.Context, stepID string, rawOutput, errMsg, fallbackExecResult string) error
	// MoveSummaryToLastFailure atomically copies the current Summary (or ExecutionResult
	// fallback) into LastFailureSummary and clears Summary/ChatHistoryJSON/SummaryError.
	// Called by Retry so the re-run's summarizer can see the prior failed attempt's context.
	MoveSummaryToLastFailure(ctx context.Context, stepID string) error
	// SetPlanStepFailureClass records the [FailureClass] of the most recent
	// failure on a step. Pass [FailureClassEmpty] to clear it (e.g. on retry).
	SetPlanStepFailureClass(ctx context.Context, stepID string, class FailureClass) error

	// Bulk operations for efficiency
	// DeleteFinishedPlans removes all completed/archived plans; returns count.
	DeleteFinishedPlans(ctx context.Context) (int, error)
	// ArchiveActivePlans sets all active plans to archived status.
	ArchiveActivePlans(ctx context.Context) error
}

Store defines the data access interface for plans and steps.

func New

func New(exec libdbexec.Exec) Store

New creates a new plan store instance.

type SummaryArtifact added in v0.7.0

type SummaryArtifact struct {
	Kind string `json:"kind"`
	Ref  string `json:"ref"`
	Note string `json:"note"`
}

SummaryArtifact is one entry in SummaryDoc.Artifacts. All fields optional individually; a well-formed entry typically has Kind and Ref.

type SummaryDoc added in v0.7.0

type SummaryDoc struct {
	Outcome         string            `json:"outcome"`
	Summary         string            `json:"summary"`
	Artifacts       []SummaryArtifact `json:"artifacts"`
	HandoverForNext string            `json:"handover_for_next"`
	Caveats         []string          `json:"caveats"`
}

SummaryDoc is the typed contract emitted by the summarizer chain and persisted in PlanStep.Summary (as JSON). Required fields: Outcome (enum), Summary, HandoverForNext. Artifacts and Caveats are optional (may be empty arrays).

This contract is shared between:

  • localhooks.PlanSummaryHook (validates + writes it on success, rejects invalid output so the DAG's repair branch can try again).
  • planservice.NewPlanStepMacroVars (reads the prior step's Summary to populate {{var:previous_output}} / {{var:previous_handover}} / {{var:previous_artifacts}} / {{var:previous_caveats}} for the next step's seed prompt).

Kept in planstore — not in planservice or localhooks — to avoid either side owning the contract the other depends on. Adding a required field here is a breaking change (requires migration of persisted rows); adding an optional field is safe.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL