protocol

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package protocol provides inter-agent protocol message handling.

This package defines protocol message types for Witness-Refinery communication and provides handlers for processing these messages.

Protocol Message Types:

  • MERGE_READY: Witness → Refinery (branch ready for merge)
  • MERGED: Refinery → Witness (merge succeeded, cleanup ok)
  • MERGE_FAILED: Refinery → Witness (merge failed, needs rework)
  • REWORK_REQUEST: Refinery → Witness (rebase needed)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractPolecat

func ExtractPolecat(subject string) string

ExtractPolecat extracts the polecat name from a protocol message subject. Subject format: "TYPE <polecat-name>"

func IsProtocolMessage

func IsProtocolMessage(subject string) bool

IsProtocolMessage returns true if the subject matches a known protocol type.

func NewMergeFailedMessage

func NewMergeFailedMessage(rig, polecat, branch, issue, targetBranch, failureType, errorMsg string) *mail.Message

NewMergeFailedMessage creates a MERGE_FAILED protocol message. Sent by Refinery to Witness when merge fails (tests, build, etc.).

func NewMergeReadyMessage

func NewMergeReadyMessage(rig, polecat, branch, issue string) *mail.Message

NewMergeReadyMessage creates a MERGE_READY protocol message. Sent by Witness to Refinery when a polecat's work is verified and ready.

func NewMergedMessage

func NewMergedMessage(rig, polecat, branch, issue, targetBranch, mergeCommit string) *mail.Message

NewMergedMessage creates a MERGED protocol message. Sent by Refinery to Witness when a branch is successfully merged.

func NewReworkRequestMessage

func NewReworkRequestMessage(rig, polecat, branch, issue, targetBranch string, conflictFiles []string) *mail.Message

NewReworkRequestMessage creates a REWORK_REQUEST protocol message. Sent by Refinery to Witness when a branch needs rebasing due to conflicts.

Types

type DefaultRefineryHandler

type DefaultRefineryHandler struct {
	// Rig is the name of the rig this refinery processes.
	Rig string

	// WorkDir is the working directory for operations.
	WorkDir string

	// Queue is the merge request queue.
	Queue *mrqueue.Queue

	// Router is used to send mail messages.
	Router *mail.Router

	// Output is where to write status messages.
	Output io.Writer
}

DefaultRefineryHandler provides the default implementation for Refinery protocol handlers. It receives MERGE_READY messages from the Witness and adds work to the merge queue.

func NewRefineryHandler

func NewRefineryHandler(rig, workDir string) *DefaultRefineryHandler

NewRefineryHandler creates a new DefaultRefineryHandler.

func (*DefaultRefineryHandler) HandleMergeReady

func (h *DefaultRefineryHandler) HandleMergeReady(payload *MergeReadyPayload) error

HandleMergeReady handles a MERGE_READY message from Witness. When a polecat's work is verified and ready, the Refinery: 1. Validates the merge request 2. Adds it to the merge queue 3. Acknowledges receipt

func (*DefaultRefineryHandler) NotifyMergeOutcome

func (h *DefaultRefineryHandler) NotifyMergeOutcome(polecat, branch, issue, targetBranch string, outcome MergeOutcome) error

NotifyMergeOutcome sends the appropriate protocol message based on the outcome.

func (*DefaultRefineryHandler) SendMergeFailed

func (h *DefaultRefineryHandler) SendMergeFailed(polecat, branch, issue, targetBranch, failureType, errorMsg string) error

SendMergeFailed sends a MERGE_FAILED message to the Witness. Called by the Refinery when a merge fails.

func (*DefaultRefineryHandler) SendMerged

func (h *DefaultRefineryHandler) SendMerged(polecat, branch, issue, targetBranch, mergeCommit string) error

SendMerged sends a MERGED message to the Witness. Called by the Refinery after successfully merging a branch.

func (*DefaultRefineryHandler) SendReworkRequest

func (h *DefaultRefineryHandler) SendReworkRequest(polecat, branch, issue, targetBranch string, conflictFiles []string) error

SendReworkRequest sends a REWORK_REQUEST message to the Witness. Called by the Refinery when a branch has conflicts.

func (*DefaultRefineryHandler) SetOutput

func (h *DefaultRefineryHandler) SetOutput(w io.Writer)

SetOutput sets the output writer for status messages.

type DefaultWitnessHandler

type DefaultWitnessHandler struct {
	// Rig is the name of the rig this witness manages.
	Rig string

	// WorkDir is the working directory for operations.
	WorkDir string

	// Router is used to send mail messages.
	Router *mail.Router

	// Output is where to write status messages.
	Output io.Writer
}

DefaultWitnessHandler provides the default implementation for Witness protocol handlers. It receives messages from the Refinery about merge outcomes and takes appropriate action.

func NewWitnessHandler

func NewWitnessHandler(rig, workDir string) *DefaultWitnessHandler

NewWitnessHandler creates a new DefaultWitnessHandler.

func (*DefaultWitnessHandler) HandleMergeFailed

func (h *DefaultWitnessHandler) HandleMergeFailed(payload *MergeFailedPayload) error

HandleMergeFailed handles a MERGE_FAILED message from Refinery. When a merge fails (tests, build, etc.), the Witness: 1. Logs the failure 2. Notifies the polecat about the failure and required fixes 3. Updates the polecat's state to indicate rework needed

func (*DefaultWitnessHandler) HandleMerged

func (h *DefaultWitnessHandler) HandleMerged(payload *MergedPayload) error

HandleMerged handles a MERGED message from Refinery. When a branch is successfully merged, the Witness: 1. Logs the success 2. Notifies the polecat of successful merge 3. Initiates polecat cleanup (nuke worktree)

func (*DefaultWitnessHandler) HandleReworkRequest

func (h *DefaultWitnessHandler) HandleReworkRequest(payload *ReworkRequestPayload) error

HandleReworkRequest handles a REWORK_REQUEST message from Refinery. When a branch has conflicts requiring rebase, the Witness: 1. Logs the conflict 2. Notifies the polecat with rebase instructions 3. Updates the polecat's state to indicate rebase needed

func (*DefaultWitnessHandler) SetOutput

func (h *DefaultWitnessHandler) SetOutput(w io.Writer)

SetOutput sets the output writer for status messages.

type Handler

type Handler func(msg *mail.Message) error

Handler processes a protocol message and returns an error if processing failed.

type HandlerRegistry

type HandlerRegistry struct {
	// contains filtered or unexported fields
}

HandlerRegistry maps message types to their handlers.

func NewHandlerRegistry

func NewHandlerRegistry() *HandlerRegistry

NewHandlerRegistry creates a new handler registry.

func WrapRefineryHandlers

func WrapRefineryHandlers(h RefineryHandler) *HandlerRegistry

WrapRefineryHandlers creates mail handlers from a RefineryHandler.

func WrapWitnessHandlers

func WrapWitnessHandlers(h WitnessHandler) *HandlerRegistry

WrapWitnessHandlers creates mail handlers from a WitnessHandler.

func (*HandlerRegistry) CanHandle

func (r *HandlerRegistry) CanHandle(msg *mail.Message) bool

CanHandle returns true if a handler is registered for the message's type.

func (*HandlerRegistry) Handle

func (r *HandlerRegistry) Handle(msg *mail.Message) error

Handle dispatches a message to the appropriate handler. Returns an error if no handler is registered for the message type.

func (*HandlerRegistry) ProcessProtocolMessage

func (r *HandlerRegistry) ProcessProtocolMessage(msg *mail.Message) (bool, error)

ProcessProtocolMessage processes a protocol message using the registry. It returns (true, nil) if the message was handled successfully, (true, error) if handling failed, or (false, nil) if not a protocol message.

func (*HandlerRegistry) Register

func (r *HandlerRegistry) Register(msgType MessageType, handler Handler)

Register adds a handler for a specific message type.

type MergeFailedPayload

type MergeFailedPayload struct {
	// Branch is the source branch that failed to merge.
	Branch string `json:"branch"`

	// Issue is the beads issue ID.
	Issue string `json:"issue"`

	// Polecat is the worker name.
	Polecat string `json:"polecat"`

	// Rig is the rig name.
	Rig string `json:"rig"`

	// FailedAt is when the failure occurred.
	FailedAt time.Time `json:"failed_at"`

	// FailureType categorizes the failure (tests, build, push, etc.).
	FailureType string `json:"failure_type"`

	// Error is the error message.
	Error string `json:"error"`

	// TargetBranch is the branch we tried to merge into.
	TargetBranch string `json:"target_branch"`
}

MergeFailedPayload contains the data for a MERGE_FAILED message. Sent by Refinery when merge fails due to tests, build, or other errors.

func ParseMergeFailedPayload

func ParseMergeFailedPayload(body string) *MergeFailedPayload

ParseMergeFailedPayload parses a MERGE_FAILED message body into a payload.

type MergeOutcome

type MergeOutcome struct {
	// Success indicates whether the merge was successful.
	Success bool

	// Conflict indicates the failure was due to conflicts (needs rebase).
	Conflict bool

	// FailureType categorizes the failure (e.g., "tests", "build").
	FailureType string

	// Error is the error message if the merge failed.
	Error string

	// MergeCommit is the SHA of the merge commit on success.
	MergeCommit string

	// ConflictFiles lists files with conflicts (if Conflict is true).
	ConflictFiles []string
}

NotifyMergeOutcome is a convenience method that sends the appropriate message based on the merge result.

type MergeReadyPayload

type MergeReadyPayload struct {
	// Branch is the polecat's work branch (e.g., "polecat/Toast/gt-abc").
	Branch string `json:"branch"`

	// Issue is the beads issue ID the polecat completed.
	Issue string `json:"issue"`

	// Polecat is the worker name.
	Polecat string `json:"polecat"`

	// Rig is the rig name containing the polecat.
	Rig string `json:"rig"`

	// Verified contains verification notes.
	Verified string `json:"verified,omitempty"`

	// Timestamp is when the message was created.
	Timestamp time.Time `json:"timestamp"`
}

MergeReadyPayload contains the data for a MERGE_READY message. Sent by Witness after verifying polecat work is complete.

func ParseMergeReadyPayload

func ParseMergeReadyPayload(body string) *MergeReadyPayload

ParseMergeReadyPayload parses a MERGE_READY message body into a payload.

type MergedPayload

type MergedPayload struct {
	// Branch is the source branch that was merged.
	Branch string `json:"branch"`

	// Issue is the beads issue ID.
	Issue string `json:"issue"`

	// Polecat is the worker name.
	Polecat string `json:"polecat"`

	// Rig is the rig name.
	Rig string `json:"rig"`

	// MergedAt is when the merge completed.
	MergedAt time.Time `json:"merged_at"`

	// MergeCommit is the SHA of the merge commit.
	MergeCommit string `json:"merge_commit,omitempty"`

	// TargetBranch is the branch merged into (e.g., "main").
	TargetBranch string `json:"target_branch"`
}

MergedPayload contains the data for a MERGED message. Sent by Refinery after successful merge to target branch.

func ParseMergedPayload

func ParseMergedPayload(body string) *MergedPayload

ParseMergedPayload parses a MERGED message body into a payload.

type MessageType

type MessageType string

MessageType identifies the protocol message type.

const (
	// TypeMergeReady is sent from Witness to Refinery when a polecat's work
	// is verified and ready for merge queue processing.
	// Subject format: "MERGE_READY <polecat-name>"
	TypeMergeReady MessageType = "MERGE_READY"

	// TypeMerged is sent from Refinery to Witness when a branch has been
	// successfully merged to the target branch.
	// Subject format: "MERGED <polecat-name>"
	TypeMerged MessageType = "MERGED"

	// TypeMergeFailed is sent from Refinery to Witness when a merge attempt
	// failed (tests, build, or other non-conflict error).
	// Subject format: "MERGE_FAILED <polecat-name>"
	TypeMergeFailed MessageType = "MERGE_FAILED"

	// TypeReworkRequest is sent from Refinery to Witness when a polecat's
	// branch needs rebasing due to conflicts with the target branch.
	// Subject format: "REWORK_REQUEST <polecat-name>"
	TypeReworkRequest MessageType = "REWORK_REQUEST"
)

func ParseMessageType

func ParseMessageType(subject string) MessageType

ParseMessageType extracts the protocol message type from a mail subject. Returns empty string if subject doesn't match a known protocol type.

type RefineryHandler

type RefineryHandler interface {
	// HandleMergeReady is called when a polecat's work is verified and ready.
	HandleMergeReady(payload *MergeReadyPayload) error
}

RefineryHandler defines the interface for Refinery protocol handlers. The Refinery receives messages from Witness about ready branches.

type ReworkRequestPayload

type ReworkRequestPayload struct {
	// Branch is the source branch that needs rebasing.
	Branch string `json:"branch"`

	// Issue is the beads issue ID.
	Issue string `json:"issue"`

	// Polecat is the worker name.
	Polecat string `json:"polecat"`

	// Rig is the rig name.
	Rig string `json:"rig"`

	// RequestedAt is when the rework was requested.
	RequestedAt time.Time `json:"requested_at"`

	// TargetBranch is the branch to rebase onto.
	TargetBranch string `json:"target_branch"`

	// ConflictFiles lists files with conflicts (if known).
	ConflictFiles []string `json:"conflict_files,omitempty"`

	// Instructions provides specific rebase instructions.
	Instructions string `json:"instructions,omitempty"`
}

ReworkRequestPayload contains the data for a REWORK_REQUEST message. Sent by Refinery when a polecat's branch has conflicts requiring rebase.

func ParseReworkRequestPayload

func ParseReworkRequestPayload(body string) *ReworkRequestPayload

ParseReworkRequestPayload parses a REWORK_REQUEST message body into a payload.

type WitnessHandler

type WitnessHandler interface {
	// HandleMerged is called when a branch was successfully merged.
	HandleMerged(payload *MergedPayload) error

	// HandleMergeFailed is called when a merge attempt failed.
	HandleMergeFailed(payload *MergeFailedPayload) error

	// HandleReworkRequest is called when a branch needs rebasing.
	HandleReworkRequest(payload *ReworkRequestPayload) error
}

WitnessHandler defines the interface for Witness protocol handlers. The Witness receives messages from Refinery about merge status.

Jump to

Keyboard shortcuts

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