Files
orchestra/internal/operations/workphase.go
T
kami 7f12c7fc37 v3 workflow: intent, phases, review, submission, enforcement, burn-in
The v3 stack, previously an uncommitted working tree, plus this session's two
units and the burn-in instrument. This commit is the burn-in build identity:
coordinator and worker must both report this revision before a task is created.

Workflow (earlier sessions, uncommitted until now): human decision events and
reduction, source cursors and reconcile-before-launch, turn-boundary
reconciliation, internal/agentctx as the single renderer, ace-fca phases with
sealed artifacts, the trajectory gate, bounded grilling, independent review,
task pr enforcement, and human review reflection.

Capability restrictions at the agent boundary: an authz.Agent surface at
GatedWrite may ask and may not act. It also fixes two bugs the unit exposed --
gated surfaces could not reach the two endpoints written for them, and
RequestHumanDecision would block an unowned task while rejecting a question
from the session that did own it.

Turn-boundary reconcile-failure escalation: a streak of consecutive failures
asks the session to hand off, fenced on the lease epoch, with reconcile_failure
as a real handoff reason. The worker was dropping the coordinator's verdict on
the floor; it now acts on it.

Burn-in: herdr.WriteLaunchContext dumps the exact agentctx.Build result to
<worktree>/.orchestra/launch.md at every launch, local and federated. BURNIN.md
is the runbook. deploy/build.sh stamps both binaries from one commit.

go build, go vet and go test ./... pass, 20 packages.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 18:31:20 +04:00

97 lines
3.4 KiB
Go

package operations
import (
"encoding/json"
"fmt"
"orchestra/internal/authz"
"orchestra/internal/domain"
"orchestra/internal/registry"
"orchestra/internal/store"
"orchestra/internal/workphase"
)
// AdvanceWorkPhase moves a task to the next phase on its project's declared
// path and seals the artifact the phase produced.
//
// Only Orchestra changes phase. An agent that believes the phase should
// change says so through the approval surface, and this is what acts on that
// belief. The artifact is validated before the transition is recorded, so a
// phase can never be left with an artifact the next phase cannot read.
//
// Review is the end of the path. Its only move is back to implement, because
// a review that passes ends the task through the lifecycle, not the phase.
func AdvanceWorkPhase(s *store.Store, project registry.Project, taskID string, artifact []byte) (domain.Event, error) {
return advanceWorkPhase(s, project, taskID, artifact, nil)
}
// advanceWorkPhase carries extra payload fields a specific transition needs,
// such as the commit a review phase is entered against.
func advanceWorkPhase(s *store.Store, project registry.Project, taskID string, artifact []byte, extra map[string]any) (domain.Event, error) {
t, ok := s.Task(taskID)
if !ok {
return domain.Event{}, domain.ErrNotFound
}
next, ok := project.NextPhase(t.WorkPhase)
if !ok {
return domain.Event{}, fmt.Errorf("%w: work phase %q is the end of project %s's path", domain.ErrInvalid, current(t), project.ID)
}
// The gate sits between the sealed artifact and the next phase, so the
// human confirms a direction that is already written down.
if project.GateRequired(current(t), next) {
switch {
case trajectoryGateOpen(s, taskID):
cleared, err := clearTrajectoryGate(s, t)
if err != nil {
return domain.Event{}, err
}
t = cleared
case t.State == domain.StateBlocked && t.BlockReason == domain.BlockReasonTrajectoryGate:
// Already waiting. Re-raising would spam the human and reset the
// position the open check depends on.
return domain.Event{}, fmt.Errorf("%w (task %s, %s to %s)", ErrTrajectoryGate, taskID, current(t), next)
default:
// The artifact is not sealed yet, so the packet reads the proposal
// from the bytes in hand. The caller retries this same advance with
// the same artifact once the human has answered.
return domain.Event{}, raiseTrajectoryGate(s, t, current(t), next, artifact)
}
}
payload := map[string]any{"phase": string(next), "from": string(current(t))}
for k, v := range extra {
payload[k] = v
}
if len(artifact) > 0 {
// Validate against the phase being left, which is the phase that
// produced this artifact.
switch current(t) {
case domain.WorkPhaseResearch:
if _, err := workphase.DecodeResearch(artifact); err != nil {
return domain.Event{}, err
}
case domain.WorkPhasePlan:
if _, err := workphase.DecodePlan(artifact); err != nil {
return domain.Event{}, err
}
}
ref, err := s.PutArtifact(artifact)
if err != nil {
return domain.Event{}, err
}
payload["artifact_ref"] = ref
}
b, err := json.Marshal(payload)
if err != nil {
return domain.Event{}, err
}
e := domain.Event{ID: domain.NewID(), Type: domain.EventWorkPhaseChanged, TaskID: taskID, Version: t.Version + 1, Payload: b, Surface: string(authz.System)}
return e, s.Append(e)
}
func current(t domain.Task) domain.WorkPhase {
if t.WorkPhase == "" {
return domain.WorkPhaseFrame
}
return t.WorkPhase
}