Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
// Package issueworkflow owns project workflows (MUL-7420).
//
// A workflow is a whole pipeline: it selects and orders statuses from the
// workspace's shared issue_status catalog, and says who each column hands an
// issue to. Every workflow lists todo, done and cancelled; backlog and
// blocked are optional; only their positions are fixed (backlog, todo first;
// blocked, done, cancelled last). Statuses stay shared and peer-level, so
// issue.status keeps holding a catalog key.
//
// When an issue of a project enters a status through an ordinary issue write,
// it is assigned to the workflow's handler for that status and, for an agent,
// its run starts with the step's instructions. A relative handler (the project
// lead) resolves per project, so projects can share a workflow.
//
// A project without a workflow uses the workspace Default workflow. Default
// is a stored row once created; before that it is the built-in statuses.
// Once stored, Default limits status writes like any workflow, on its
// projects and on issues outside any project alike (MUL-8250). Issues already
// on statuses Default does not list keep them until someone moves them.
package issueworkflow
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"unicode/utf8"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/multica-ai/multica/server/internal/issuestatus"
db "github.com/multica-ai/multica/server/pkg/db/generated"
)
// Handler types a project can hand an issue to when it enters a status.
const (
HandlerNone = "none"
HandlerAgent = "agent"
HandlerSquad = "squad"
HandlerMember = "member"
HandlerProjectLead = "project_lead"
HandlerCreator = "creator"
)
const (
MaxSteps = 50
MaxNameRunes = 64
MaxDescriptionRunes = 256
MaxInstructionsRunes = 8000
DefaultName = "Default"
)
// StartStatus is where a new issue lands unless its creator picks a status.
const StartStatus = issuestatus.Todo
// These statuses sit in fixed places around a workflow's own steps. Backlog
// and blocked are optional; the rest every workflow lists.
var (
fixedHead = []string{issuestatus.Backlog, issuestatus.Todo}
fixedTail = []string{issuestatus.Blocked, issuestatus.Done, issuestatus.Cancelled}
)
// IsRequired reports whether every workflow must list the status: new issues
// start on todo, and done and cancelled are how work ends.
func IsRequired(key string) bool {
switch key {
case issuestatus.Todo, issuestatus.Done, issuestatus.Cancelled:
return true
}
return false
}
// IsFixed reports whether the status has a fixed place when listed.
func IsFixed(key string) bool {
for _, k := range fixedHead {
if k == key {
return true
}
}
for _, k := range fixedTail {
if k == key {
return true
}
}
return false
}
// AcceptsHandoff reports whether entering the status can hand an issue off.
// Parking, finishing and cancelling an issue never reassign it.
func AcceptsHandoff(key string) bool {
switch key {
case issuestatus.Backlog, issuestatus.Done, issuestatus.Cancelled:
return false
}
return true
}
// Arrange puts a workflow's steps in board order: the fixed statuses it
// lists in their places around its own steps, with the required ones added
// when missing. A repeated fixed status keeps its first step.
func Arrange(steps []Step) []Step {
fixed := map[string]Step{}
middle := make([]Step, 0, len(steps))
for _, s := range steps {
if !IsFixed(s.StatusKey) {
middle = append(middle, s)
continue
}
if _, ok := fixed[s.StatusKey]; !ok {
fixed[s.StatusKey] = s
}
}
place := func(out []Step, keys []string) []Step {
for _, key := range keys {
if s, ok := fixed[key]; ok {
out = append(out, s)
} else if IsRequired(key) {
out = append(out, Step{StatusKey: key, Handler: Handler{Type: HandlerNone}})
}
}
return out
}
out := place(make([]Step, 0, len(steps)+len(fixedHead)+len(fixedTail)), fixedHead)
out = append(out, middle...)
return place(out, fixedTail)
}
// ArrangeKeys is Arrange for bare status keys.
func ArrangeKeys(keys []string) []string {
return keysOf(Arrange(stepsFor(keys)))
}
func keysOf(steps []Step) []string {
keys := make([]string, len(steps))
for i, s := range steps {
keys[i] = s.StatusKey
}
return keys
}
// BuiltInDefaultKeys is Default before a workspace stores its own.
func BuiltInDefaultKeys() []string {
return ArrangeKeys([]string{issuestatus.Backlog, issuestatus.InProgress, issuestatus.InReview, issuestatus.Blocked})
}
// Handler names who receives an issue when it enters a step. ID is set only
// for the concrete types (agent, squad, member).
type Handler struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
}
// Step is one status of a workflow with its handoff.
type Step struct {
StatusKey string `json:"status_key"`
Handler Handler `json:"handler"`
Instructions string `json:"instructions"`
NextStatusKey string `json:"next_status_key,omitempty"`
BackStatusKey string `json:"back_status_key,omitempty"`
}
// Definition is a decoded workflow.
type Definition struct {
ID pgtype.UUID
Name string
IsDefault bool
Steps []Step
}
// HandsOff reports whether entering the step reassigns the issue.
func (s Step) HandsOff() bool {
switch s.Handler.Type {
case HandlerAgent, HandlerSquad, HandlerMember, HandlerProjectLead, HandlerCreator:
return true
}
return false
}
// storedStep is a step as the workflow row stores it; a step without a
// handoff is just its status key.
type storedStep struct {
StatusKey string `json:"status_key"`
Handler *Handler `json:"handler,omitempty"`
Instructions string `json:"instructions,omitempty"`
NextStatusKey string `json:"next_status_key,omitempty"`
BackStatusKey string `json:"back_status_key,omitempty"`
}
// Decode parses a stored workflow row.
func Decode(row db.IssueWorkflow) (Definition, error) {
steps, err := DecodeSteps(row.Steps)
if err != nil {
return Definition{}, err
}
return Definition{ID: row.ID, Name: row.Name, IsDefault: row.IsDefault, Steps: steps}, nil
}
// DecodeSteps parses a stored steps array, normalizing a missing handler to
// "none".
func DecodeSteps(raw []byte) ([]Step, error) {
if len(raw) == 0 {
return []Step{}, nil
}
var stored []storedStep
if err := json.Unmarshal(raw, &stored); err != nil {
return nil, fmt.Errorf("decode workflow steps: %w", err)
}
steps := make([]Step, len(stored))
for i, s := range stored {
steps[i] = Step{
StatusKey: s.StatusKey,
Handler: Handler{Type: HandlerNone},
Instructions: s.Instructions,
NextStatusKey: s.NextStatusKey,
BackStatusKey: s.BackStatusKey,
}
if s.Handler != nil && s.Handler.Type != "" {
steps[i].Handler = *s.Handler
}
}
return steps, nil
}
// DecodeKeys parses a stored steps array into its status keys, in order.
func DecodeKeys(raw []byte) ([]string, error) {
steps, err := DecodeSteps(raw)
if err != nil {
return nil, err
}
return keysOf(steps), nil
}
// EncodeSteps serializes a workflow's steps for storage.
func EncodeSteps(steps []Step) ([]byte, error) {
stored := make([]storedStep, len(steps))
for i, s := range steps {
stored[i] = storedStep{StatusKey: s.StatusKey, NextStatusKey: s.NextStatusKey, BackStatusKey: s.BackStatusKey}
if s.HandsOff() {
h := s.Handler
stored[i].Handler = &h
stored[i].Instructions = s.Instructions
}
}
return json.Marshal(stored)
}
// EncodeKeys serializes statuses without handoffs for storage.
func EncodeKeys(keys []string) ([]byte, error) {
return EncodeSteps(stepsFor(keys))
}
func stepsFor(keys []string) []Step {
steps := make([]Step, len(keys))
for i, key := range keys {
steps[i] = Step{StatusKey: key, Handler: Handler{Type: HandlerNone}}
}
return steps
}
// Limits reports whether the workflow restricts the statuses its issues may
// enter. Only a Default the workspace has not stored yet does not: it stands
// for "the built-in statuses" before the workspace ever used workflows, and
// limiting by it would strand custom statuses that workspace already uses.
func (d Definition) Limits() bool {
return !d.IsDefault || d.ID.Valid
}
// Has reports whether the workflow lists the status key.
func (d Definition) Has(key string) bool {
_, ok := d.Step(key)
return ok
}
// Step returns the step for a status key.
func (d Definition) Step(key string) (Step, bool) {
for _, s := range d.Steps {
if s.StatusKey == key {
return s, true
}
}
return Step{}, false
}
// StatusKeys lists the workflow's statuses in board order.
func (d Definition) StatusKeys() []string {
return keysOf(d.Steps)
}
// HandsOffAnywhere reports whether any step hands an issue off.
func (d Definition) HandsOffAnywhere() bool {
for _, s := range d.Steps {
if s.HandsOff() {
return true
}
}
return false
}
// NextStatus is where a step's handler moves the issue when done: the step's
// own choice, or else the next status on the board that is not blocked.
func (d Definition) NextStatus(key string) string {
return d.nextStatus(key, nil)
}
// nextStatus is NextStatus that also passes over closed statuses on the
// board: custom statuses sit before blocked / done / cancelled, so a custom
// closed one such as wont_fix would otherwise be where finished work goes
// (MUL-9913). effective resolves a key to its canonical behavior; nil leaves
// every key as itself. A step's own choice is kept as configured.
func (d Definition) nextStatus(key string, effective func(string) string) string {
step, ok := d.Step(key)
if !ok {
return ""
}
if step.NextStatusKey != "" {
return step.NextStatusKey
}
for i, s := range d.Steps {
if s.StatusKey != key {
continue
}
for _, next := range d.Steps[i+1:] {
if next.StatusKey != issuestatus.Blocked && effectiveOf(effective, next.StatusKey) != issuestatus.Cancelled {
return next.StatusKey
}
}
}
return ""
}
func effectiveOf(effective func(string) string, key string) string {
if effective == nil {
return key
}
return effective(key)
}
// handlerlessSpan is the stretch of work an assignee owns from a step that
// has no handler: entering such steps reassigns nothing and starts no run, so
// the same assignee carries the issue until it reaches a step that hands off
// (or the end of the board).
type handlerlessSpan struct {
// ok is set when the current step has no handler and the span applies.
ok bool
// start is the status that marks the work as started — in_progress when
// the span passes through it — or empty.
start string
// deliver is the status to set once all of the work is done.
deliver string
// until is where the span ends: the next step that hands off, done or
// cancelled, or empty when the board ends first.
until string
}
// handlerlessSpan walks from key along NextStatus over the steps without a
// handler. When the walk reaches a step that hands off, delivering moves the
// issue there. Otherwise it reaches the end of the board, and delivering sets
// the last status before done other than in_progress, leaving done to a
// human as without a workflow; only when no such status exists does it set
// what follows (done). A custom done or closed status ends the walk the way
// done does; effective resolves it as nextStatus does.
func (d Definition) handlerlessSpan(key string, effective func(string) string) handlerlessSpan {
current, ok := d.Step(key)
if !ok || current.HandsOff() {
return handlerlessSpan{}
}
switch key {
case issuestatus.Blocked, issuestatus.Done, issuestatus.Cancelled:
return handlerlessSpan{}
}
var span handlerlessSpan
span.ok = true
var passed []string
seen := map[string]bool{key: true}
for at := key; ; {
next := d.nextStatus(at, effective)
if next == "" || seen[next] {
break
}
seen[next] = true
step, _ := d.Step(next)
if behavior := effectiveOf(effective, next); step.HandsOff() || behavior == issuestatus.Done || behavior == issuestatus.Cancelled {
span.until = next
break
}
passed = append(passed, next)
at = next
}
for _, k := range passed {
if k == issuestatus.InProgress {
span.start = k
}
}
if target, ok := d.Step(span.until); ok && target.HandsOff() {
span.deliver = span.until
return span
}
for i := len(passed) - 1; i >= 0; i-- {
if passed[i] != issuestatus.InProgress {
span.deliver = passed[i]
return span
}
}
// Nothing but done lies ahead. From a step where work starts, done is the
// only way out; from a delivery status such as in_review the work is
// already handed in, and done stays a human's call as without a workflow.
switch key {
case issuestatus.Backlog, issuestatus.Todo, issuestatus.InProgress:
default:
return span
}
span.deliver = span.until
return span
}
// CatalogEntry is the slice of an issue_status row validation needs.
type CatalogEntry struct {
Key string
Name string
Category string
Archived bool
}
// Normalize trims free text and handoffs and puts the steps in board order.
func Normalize(name, description string, steps []Step) (string, string, []Step) {
normalized := make([]Step, len(steps))
for i, s := range steps {
normalized[i] = NormalizeHandoff(s)
}
return strings.TrimSpace(name), strings.TrimSpace(description), Arrange(normalized)
}
// ValidationError is a user-correctable problem with a workflow definition.
type ValidationError struct{ Message string }
func (e *ValidationError) Error() string { return e.Message }
func invalid(format string, args ...any) error {
return &ValidationError{Message: fmt.Sprintf(format, args...)}
}
// Validate checks a normalized workflow and its handoffs against the status
// catalog. The Default workflow keeps its reserved name. Handler ids are
// checked by the caller, which owns the workspace lookups.
func Validate(name, description string, steps []Step, isDefault bool, catalog map[string]CatalogEntry) error {
if !isDefault {
if name == "" || utf8.RuneCountInString(name) > MaxNameRunes {
return invalid("name must be 1-%d characters", MaxNameRunes)
}
if strings.EqualFold(name, DefaultName) {
return invalid("%q is reserved for the workspace default workflow", DefaultName)
}
}
if utf8.RuneCountInString(description) > MaxDescriptionRunes {
return invalid("description must be at most %d characters", MaxDescriptionRunes)
}
if len(steps) > MaxSteps {
return invalid("a workflow can have at most %d statuses", MaxSteps)
}
seen := make(map[string]bool, len(steps))
for _, key := range keysOf(steps) {
entry, ok := catalog[key]
if !ok && !issuestatus.IsBuiltIn(key) {
return invalid("status %q does not exist", key)
}
if ok && entry.Archived {
return invalid("status %q is archived", key)
}
if seen[key] {
return invalid("status %q appears more than once", key)
}
seen[key] = true
}
def := Definition{Steps: steps}
for _, s := range steps {
if err := ValidateHandoff(s, def); err != nil {
return err
}
}
return nil
}
// NormalizeHandoff trims a handoff and drops fields its handler type does
// not use.
func NormalizeHandoff(s Step) Step {
s.StatusKey = strings.TrimSpace(s.StatusKey)
s.Handler.Type = strings.TrimSpace(s.Handler.Type)
s.Handler.ID = strings.TrimSpace(s.Handler.ID)
if s.Handler.Type == "" {
s.Handler.Type = HandlerNone
}
switch s.Handler.Type {
case HandlerAgent, HandlerSquad, HandlerMember:
default:
// Only the concrete handler types carry an id; a stale one left
// by a type switch in the form would otherwise be stored.
s.Handler.ID = ""
}
s.Instructions = strings.TrimSpace(s.Instructions)
if s.Handler.Type == HandlerNone {
s.Instructions = ""
}
s.NextStatusKey = strings.TrimSpace(s.NextStatusKey)
s.BackStatusKey = strings.TrimSpace(s.BackStatusKey)
return s
}
// ValidateHandoff checks one normalized handoff against its workflow.
// Handler ids are checked by the caller, which owns the workspace lookups.
func ValidateHandoff(s Step, def Definition) error {
if !def.Has(s.StatusKey) {
return invalid("status %q is not part of this workflow", s.StatusKey)
}
switch s.Handler.Type {
case HandlerNone, HandlerProjectLead:
case HandlerAgent, HandlerSquad, HandlerMember:
if s.Handler.ID == "" {
return invalid("choose a %s to hand off to", s.Handler.Type)
}
case HandlerCreator:
return invalid("hand off to an agent, a squad, a member or the project lead")
default:
return invalid("unknown handler type %q", s.Handler.Type)
}
if s.HandsOff() && !AcceptsHandoff(s.StatusKey) {
return invalid("issues entering %q cannot be handed off", s.StatusKey)
}
if utf8.RuneCountInString(s.Instructions) > MaxInstructionsRunes {
return invalid("instructions must be at most %d characters", MaxInstructionsRunes)
}
for _, ref := range []string{s.NextStatusKey, s.BackStatusKey} {
if ref != "" && (ref == s.StatusKey || !def.Has(ref)) {
return invalid("%q is not another status of this workflow", ref)
}
}
return nil
}
// Querier is the read surface workflow resolution needs.
type Querier interface {
GetIssueWorkflow(ctx context.Context, arg db.GetIssueWorkflowParams) (db.IssueWorkflow, error)
GetDefaultIssueWorkflow(ctx context.Context, workspaceID pgtype.UUID) (db.IssueWorkflow, error)
GetProjectInWorkspace(ctx context.Context, arg db.GetProjectInWorkspaceParams) (db.Project, error)
}
// DefaultFor loads the workspace Default workflow, or the built-in one when
// the workspace has not stored its own yet.
func DefaultFor(ctx context.Context, q Querier, workspaceID pgtype.UUID) (Definition, error) {
row, err := q.GetDefaultIssueWorkflow(ctx, workspaceID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return Definition{Name: DefaultName, IsDefault: true, Steps: stepsFor(BuiltInDefaultKeys())}, nil
}
return Definition{}, err
}
return Decode(row)
}
// ForProject loads the workflow a project uses. def is nil only for an issue
// outside any project.
func ForProject(ctx context.Context, q Querier, workspaceID, projectID pgtype.UUID) (*Definition, db.Project, error) {
if !projectID.Valid {
return nil, db.Project{}, nil
}
project, err := q.GetProjectInWorkspace(ctx, db.GetProjectInWorkspaceParams{ID: projectID, WorkspaceID: workspaceID})
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, db.Project{}, nil
}
return nil, db.Project{}, err
}
def, err := ForProjectRow(ctx, q, project)
return def, project, err
}
// OutsideProject loads the workflow that limits the statuses of an issue
// outside any project: the workspace's stored Default, or nil before the
// workspace stores one. Nothing hands off outside a project, so it is only
// for checking statuses; ForProject stays nil there. (MUL-8250)
func OutsideProject(ctx context.Context, q Querier, workspaceID pgtype.UUID) (*Definition, error) {
row, err := q.GetDefaultIssueWorkflow(ctx, workspaceID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, err
}
def, err := Decode(row)
if err != nil {
return nil, err
}
return &def, nil
}
// ForProjectRow loads the workflow of an already-loaded project. A dangling
// workflow reference falls back to Default.
func ForProjectRow(ctx context.Context, q Querier, project db.Project) (*Definition, error) {
var def Definition
var err error
if project.WorkflowID.Valid {
var row db.IssueWorkflow
row, err = q.GetIssueWorkflow(ctx, db.GetIssueWorkflowParams{ID: project.WorkflowID, WorkspaceID: project.WorkspaceID})
if err == nil {
def, err = Decode(row)
return &def, err
}
if !errors.Is(err, pgx.ErrNoRows) {
return nil, err
}
}
def, err = DefaultFor(ctx, q, project.WorkspaceID)
if err != nil {
return nil, err
}
return &def, nil
}
// ResolveHandler turns a step's handler into a concrete assignee for one
// issue. ok is false when the step does not hand off, or when a relative
// handler (project lead, creator) has nobody to resolve to.
func ResolveHandler(step Step, project db.Project, issueCreatorType string, issueCreatorID pgtype.UUID) (assigneeType string, assigneeID pgtype.UUID, ok bool) {
switch step.Handler.Type {
case HandlerAgent, HandlerSquad, HandlerMember:
var id pgtype.UUID
if err := id.Scan(step.Handler.ID); err != nil || !id.Valid {
return "", pgtype.UUID{}, false
}
return step.Handler.Type, id, true
case HandlerProjectLead:
if !project.LeadType.Valid || !project.LeadID.Valid {
return "", pgtype.UUID{}, false
}
return project.LeadType.String, project.LeadID, true
case HandlerCreator:
if issueCreatorType == "" || !issueCreatorID.Valid {
return "", pgtype.UUID{}, false
}
return issueCreatorType, issueCreatorID, true
}
return "", pgtype.UUID{}, false
}
// MoveTarget picks the status an issue keeps when it moves into a project
// using def: its current status when the workflow lists it, otherwise the
// first step of the same lifecycle category, otherwise the start status.
func MoveTarget(def Definition, currentKey string, categoryOf func(string) string) string {
if def.Has(currentKey) {
return currentKey
}
category := categoryOf(currentKey)
for _, s := range def.Steps {
if category != "" && categoryOf(s.StatusKey) == category {
return s.StatusKey
}
}
return StartStatus
}
// NotInWorkflowError explains a status write the project's workflow rejects.
// Its message lists the allowed keys because agents read it verbatim.
type NotInWorkflowError struct {
StatusKey string
WorkflowName string
Allowed []string
// OutsideProject marks an issue outside any project, which takes the
// statuses of the workspace Default.
OutsideProject bool
}
func (e *NotInWorkflowError) Error() string {
if e.OutsideProject {
return fmt.Sprintf("status %q is not part of the workflow %q that issues outside a project use; use one of: %s",
e.StatusKey, e.WorkflowName, strings.Join(e.Allowed, ", "))
}
return fmt.Sprintf("status %q is not part of this project's workflow %q; use one of: %s",
e.StatusKey, e.WorkflowName, strings.Join(e.Allowed, ", "))
}
// StepMovedError refuses an agent's status change on an issue that has left
// the step the agent's run works on: someone else moved it while the run
// worked, so the run's decision no longer applies, and applying it would undo
// that move.
type StepMovedError struct {
IssueIdentifier string
// Step is the status the run works on; Current is where the issue is now.
Step, StepName string
Current, CurrentName string
// MovedBy names whoever last changed the status; empty when unknown.
MovedBy string
}
func (e *StepMovedError) Error() string {
mover := e.MovedBy
if mover == "" {
mover = "someone else"
}
return fmt.Sprintf("%s is no longer at %s: %s moved it to %s while this run was working, so this status change was not applied. "+
"Do not change the status again in this run; leave a comment if something still needs attention.",
e.IssueIdentifier, statusLabel(e.StepName, e.Step), mover, statusLabel(e.CurrentName, e.Current))
}
// statusLabel names a status for an agent: its display name with the key the
// CLI takes, or the bare key when the two are the same.
func statusLabel(name, key string) string {
if name == "" || name == key {
return fmt.Sprintf("%q", key)
}
return fmt.Sprintf("%q (%s)", name, key)
}
// CheckStatus rejects a status the workflow does not list. Callers skip it
// when a write keeps the issue's current status, so an issue already on a
// status its workflow no longer lists keeps working.
func CheckStatus(def *Definition, key string) error {
if def == nil || !def.Limits() || def.Has(key) {
return nil
}
return &NotInWorkflowError{StatusKey: key, WorkflowName: def.Name, Allowed: def.StatusKeys()}
}
// CheckStatusOutsideProject is CheckStatus for an issue outside any project,
// against OutsideProject's workflow.
func CheckStatusOutsideProject(def *Definition, key string) error {
err := CheckStatus(def, key)
var notIn *NotInWorkflowError
if errors.As(err, ¬In) {
notIn.OutsideProject = true
}
return err
}
// BriefInput carries what the handoff brief names.
type BriefInput struct {
Workflow Definition
ProjectName string
StatusKey string
IssueIdentifier string
// BoundStep is the task's effective step when its binding must be respected.
// A different StatusKey renders a continuation brief without status moves.
// Empty preserves the normal handoff, preview and unbound-run brief.
BoundStep string
// MovedBy names whoever last changed the status, when known.
MovedBy string
// StatusName resolves a catalog key to its display name.
StatusName func(key string) string
// EffectiveStatus resolves a catalog key to its canonical behavior. With
// no resolver, the key itself determines the behavior.
EffectiveStatus func(key string) string
// StatusCategory resolves a catalog key to its lifecycle category, and
// StatusDescription to the catalog's description of a custom status. The
// status map names both; without a resolver a custom status shows neither.
StatusCategory func(key string) string
StatusDescription func(key string) string
// HandlerName describes who a step hands off to, e.g. "Sentinel" or
// "Lin (project lead)". Empty means the step does not hand off.
HandlerName func(step Step) string
// Observer renders the brief for an agent working on the issue without
// being its assignee — mentioned in a comment, say: the statuses, and to
// leave the status to AssigneeName. The zero value is the assignee's
// brief, with the step's instructions and the commands that finish it.
Observer bool
// AssigneeName names the issue's assignee in an observer's brief; empty
// means unassigned.
AssigneeName string
// LegacyRuntimeRules renders for a daemon whose runtime brief still
// states the built-in in_progress / in_review rules (one without
// protocol.DaemonCapabilityWorkflowStatusRulesV1): the brief then says it
// replaces them, rules out the built-in moves, and carries the exit check.
// A current daemon drops those rules and states the exit check itself, so
// the zero value — also what the editor and timeline show — leaves these
// lines out rather than forbid what nothing asks for (MUL-8145).
LegacyRuntimeRules bool
// StepCommands renders the per-step command list — Step done, Needs
// changes — for a daemon whose runtime brief still points at those
// commands (one without protocol.DaemonCapabilityWorkflowStatusMapV1).
// The zero value renders the status map, whose rules the runtime brief
// states (MUL-9913). LegacyRuntimeRules implies the command list.
StepCommands bool
}
// Brief renders what an agent reads about the issue's project workflow: the
// workflow's statuses, who each step hands off to, and — for the assignee —
// the current step's instructions. A current daemon gets the status map
// (statusMapBrief) and states the rules for choosing from it; StepCommands
// renders the per-step commands an older daemon's runtime brief points at
// instead, and LegacyRuntimeRules adds the lines that override the built-in
// rules a still older one states.
//
// It is the one rendering of a workflow for agents: the claim carries it into
// every run's prompt, and the editor and timeline show the same text. Names
// and instructions are user-authored, so control characters are dropped and
// names are kept to one line.
func Brief(in BriefInput) string {
if !in.StepCommands && !in.LegacyRuntimeRules {
return statusMapBrief(in)
}
step, _ := in.Workflow.Step(in.StatusKey)
stepMoved := in.BoundStep != "" && in.BoundStep != in.StatusKey
terminal := in.atTerminal()
// Nothing on the board follows a terminal status, so its only moves are
// the ones its author configured (MUL-9746).
terminalMoves := terminal && (step.NextStatusKey != "" || step.BackStatusKey != "")
name := in.statusName
var b strings.Builder
writeBriefTitle(&b, in)
// Each move names who the target step hands off to, so the list needs no
// preamble about handoffs.
handsOffTo := func(key string) string {
if who := in.handlerOf(key); who != "" {
return " — hands off to " + who
}
return ""
}
// An older daemon still states the built-in rules itself, so its brief
// keeps the per-step wording rather than add a second in_progress rule.
var span handlerlessSpan
if !stepMoved && !in.Observer && !in.LegacyRuntimeRules && !terminal {
span = in.Workflow.handlerlessSpan(in.StatusKey, in.EffectiveStatus)
}
if in.LegacyRuntimeRules && !stepMoved {
b.WriteString("This workflow replaces the status rules in your runtime instructions: use only the status keys below")
if !in.Workflow.Has(issuestatus.InReview) {
fmt.Fprintf(&b, ", never `%s`", issuestatus.InReview)
}
b.WriteString(".\n")
}
if stepMoved {
writeStepMovedIntro(&b, in)
} else if in.Observer {
writeObserverIntro(&b, in)
} else if span.ok {
// A step without a handler is a progress marker, not a stage: naming
// it "the step you handle" made agents split one task across columns
// and stop after the first half (MUL-8231).
fmt.Fprintf(&b, "You are handling %s. Its %q step has no handler, so the work the issue asks for is yours ", in.IssueIdentifier, name(in.StatusKey))
if target, ok := in.Workflow.Step(span.until); ok && target.HandsOff() {
fmt.Fprintf(&b, "until it reaches the %q step%s.\n", name(span.until), handsOffTo(span.until))
} else {
b.WriteString("to finish.\n")
}
} else {
if terminal && !terminalMoves {
writeTerminalIntro(&b, in)
} else {
fmt.Fprintf(&b, "You are handling the %q step of %s.\n", name(in.StatusKey), in.IssueIdentifier)
}
writeStepInstructions(&b, step)
}
b.WriteString("\nStatuses in this workflow:\n")
for _, s := range in.Workflow.Steps {
// Built-in statuses have no stored name; their key reads on its own.
fmt.Fprintf(&b, "- `%s`", s.StatusKey)
if n := name(s.StatusKey); n != s.StatusKey {
fmt.Fprintf(&b, " %s", n)
}
if in.HandlerName != nil {
if who := briefLine(in.HandlerName(s)); who != "" {
fmt.Fprintf(&b, " — hands off to %s", who)
}
}
if s.StatusKey == in.StatusKey {
b.WriteString(" ← current")
}
b.WriteString("\n")
}
if stepMoved {
return b.String()
}
if in.Observer {
b.WriteString("\nMoving the issue into a step that hands off reassigns it to that step's handler and starts their run.\n")
return b.String()
}
if terminal && !terminalMoves {
return b.String()
}
b.WriteString("\nYour moves:\n")
switch {
case span.ok:
if span.start != "" {
fmt.Fprintf(&b, "Starting work → multica issue status %s %s\n", in.IssueIdentifier, span.start)
}
if span.deliver != "" {
fmt.Fprintf(&b, "Step done → multica issue status %s %s%s\n", in.IssueIdentifier, span.deliver, handsOffTo(span.deliver))
} else {
fmt.Fprintf(&b, "Step done → leave the issue at `%s`; `done` is set by a human\n", in.StatusKey)
}
default:
next := step.NextStatusKey
if !terminal {
next = in.Workflow.nextStatus(in.StatusKey, in.EffectiveStatus)
}
if next != "" {
fmt.Fprintf(&b, "Step done → multica issue status %s %s%s\n", in.IssueIdentifier, next, handsOffTo(next))
}
}
if step.BackStatusKey != "" {
fmt.Fprintf(&b, "Needs changes → multica issue status %s %s%s\n", in.IssueIdentifier, step.BackStatusKey, handsOffTo(step.BackStatusKey))
}
switch {
case in.Workflow.Has(issuestatus.Blocked) && !terminal:
fmt.Fprintf(&b, "Can't proceed → multica issue status %s blocked, then comment why\n", in.IssueIdentifier)
case step.BackStatusKey != "":
b.WriteString("Can't proceed → comment why, then run Needs changes\n")
default:
b.WriteString("Can't proceed → comment why and leave the status as it is\n")
}
if span.ok {
b.WriteString("Steps without a handler hand nothing off and start no run: finish all of the issue's work in this run before Step done.\n")
}
if in.LegacyRuntimeRules {
// An older daemon's brief still says to set in_progress first and
// in_review on delivery, and checks the status on exit in those terms.
b.WriteString("These commands are the only status moves for your step: set no status to mark that you started, and none to mark delivery — no separate `in_progress` or `in_review`.\n")
b.WriteString("Before your run ends, either run one of them or leave the issue at this step on purpose because its work continues. The system does not advance the status when your run ends.\n")
}
b.WriteString("When the issue enters a step you handle, you get a run automatically — do not add a wakeup for that. Wakeups for anything else (a time, a PR, another issue, a step someone else handles) are fine.\n")
return b.String()
}
func writeBriefTitle(b *strings.Builder, in BriefInput) {
title := briefLine(in.Workflow.Name)
if project := briefLine(in.ProjectName); project != "" {
title = fmt.Sprintf("%s (%s)", title, project)
}
fmt.Fprintf(b, "This project's workflow — %s\n", title)
}
func writeStepMovedIntro(b *strings.Builder, in BriefInput) {
mover := briefLine(in.MovedBy)
if mover == "" {
mover = "someone else"
}
fmt.Fprintf(b, "This run remains bound to %s, but %s is now at %s (last moved by %s).\n",
statusLabel(in.statusName(in.BoundStep), in.BoundStep), in.IssueIdentifier, statusLabel(in.statusName(in.StatusKey), in.StatusKey), mover)
b.WriteString("Do not change the issue's status in this run, including when starting or finishing work; this overrides the status rules in your runtime instructions. Do not retry a refused status change. Leave a comment if something needs attention. A member can review the current state and explicitly rerun the work.\n")
}
func writeObserverIntro(b *strings.Builder, in BriefInput) {
assignee := "is unassigned"
if who := briefLine(in.AssigneeName); who != "" {
assignee = "is assigned to " + who
}
fmt.Fprintf(b, "%s is at the %q step and %s. You are not its assignee: leave the status to them unless you are explicitly asked to change it.\n",
in.IssueIdentifier, in.statusName(in.StatusKey), assignee)
}
func writeTerminalIntro(b *strings.Builder, in BriefInput) {
fmt.Fprintf(b, "%s has reached the terminal status %s. Leave the issue at this status unless a member explicitly asks you to reopen it.\n",
in.IssueIdentifier, statusLabel(in.statusName(in.StatusKey), in.StatusKey))
}
func writeStepInstructions(b *strings.Builder, step Step) {
instructions := briefText(step.Instructions)
if instructions == "" {
return
}
b.WriteString("\nStep instructions:\n")
for _, line := range strings.Split(instructions, "\n") {
b.WriteString("> ")
b.WriteString(line)
b.WriteString("\n")
}
}
// statusName is a status's display name, or its key when it has none —
// built-in statuses have no stored name, and their key reads on its own.
func (in BriefInput) statusName(key string) string {
if in.StatusName == nil {
return key
}
if n := briefLine(in.StatusName(key)); n != "" {
return n
}
return key
}
// handlerOf names who entering key hands off to, or "" when it hands off to
// no one.
func (in BriefInput) handlerOf(key string) string {
target, ok := in.Workflow.Step(key)
if !ok || in.HandlerName == nil {
return ""
}
return briefLine(in.HandlerName(target))
}
func (in BriefInput) effective(key string) string {
return effectiveOf(in.EffectiveStatus, key)
}
func (in BriefInput) atTerminal() bool {
behavior := in.effective(in.StatusKey)
return behavior == issuestatus.Done || behavior == issuestatus.Cancelled
}
// briefLine keeps a user-authored name on one line of a brief: line breaks
// and tabs become single spaces and other control characters are dropped.
func briefLine(s string) string {
return strings.Join(strings.Fields(briefText(s)), " ")
}
// briefText drops the control characters of user-authored text, keeping its
// line breaks, so it cannot reshape the brief around it.
func briefText(s string) string {
s = strings.ReplaceAll(s, "\r\n", "\n")
s = strings.Map(func(r rune) rune {
if r == '\n' || r == '\t' {
return r
}
if r < 0x20 || r == 0x7f || (r >= 0x80 && r < 0xa0) || r == '\u2028' || r == '\u2029' {
return -1
}
return r
}, s)
return strings.TrimSpace(s)
}