| 1 | package event |
| 2 | |
| 3 | import "time" |
| 4 | |
| 5 | // Event represents a single event in the klaus event log. |
| 6 | type Event struct { |
| 7 | Timestamp string `json:"timestamp"` // RFC3339 |
| 8 | RunID string `json:"run_id"` // Run that produced the event |
| 9 | Type string `json:"type"` // e.g. "agent:completed" |
| 10 | Data map[string]interface{} `json:"data,omitempty"` // Type-specific payload |
| 11 | } |
| 12 | |
| 13 | // Supported event types. |
| 14 | const ( |
| 15 | ConsultStarted = "consult:started" |
| 16 | ConsultCompleted = "consult:completed" |
| 17 | ReviewStarted = "review:started" |
| 18 | ReviewCompleted = "review:completed" |
| 19 | AgentStarted = "agent:started" |
| 20 | AgentCompleted = "agent:completed" |
| 21 | AgentPRCreated = "agent:pr-created" |
| 22 | AgentPaused = "agent:paused" |
| 23 | AgentResumed = "agent:resumed" |
| 24 | AgentCIPassed = "agent:ci-passed" |
| 25 | AgentCIFailed = "agent:ci-failed" |
| 26 | AgentNeedsAttention = "agent:needs-attention" |
| 27 | PRAwaitingApproval = "pr:awaiting-approval" |
| 28 | PRApproved = "pr:approved" |
| 29 | PRMerged = "pr:merged" |
| 30 | // PRApprovalChanged signals that an external decision about a PR's |
| 31 | // merge-readiness has changed (e.g. `klaus approve` flipped an internal |
| 32 | // approval flag). It is an invalidation signal — the dashboard reacts by |
| 33 | // re-fetching GitHub status and re-evaluating the pipeline FSM, just as |
| 34 | // it does for a real webhook. The name is deliberately backend-agnostic |
| 35 | // so the same signal can be reused once klaus supports non-GitHub |
| 36 | // merge-readiness sources. |
| 37 | PRApprovalChanged = "pr:approval-changed" |
| 38 | // PipelineStalled signals that the pipeline gave up on a PR after |
| 39 | // exhausting its retry budget (fix, rebase, review-fix or auto-merge |
| 40 | // attempts). It is emitted once per entry into the stalled stage, so a |
| 41 | // coordinator watching the channel gets one loud notification instead of |
| 42 | // a per-poll stream, and knows the PR now needs a human. |
| 43 | PipelineStalled = "pipeline:stalled" |
| 44 | // AgentBranchPushed signals that a run finished without a PR but with its |
| 45 | // branch on origin — the normal finish in a direct-push repo, where the |
| 46 | // operator integrates the branch by hand. It carries repo, branch and sha. |
| 47 | AgentBranchPushed = "agent:branch-pushed" |
| 48 | // AgentSharedGitChanged signals that finalize found a file its run's |
| 49 | // clone shares across worktrees (its git config, info/attributes or a |
| 50 | // hook) changed since launch. It carries dir, the clone's common git |
| 51 | // dir, and files, the changed paths relative to it. |
| 52 | AgentSharedGitChanged = "agent:shared-git-changed" |
| 53 | // AgentChangeReady signals that finalize marked a pushed branch's head |
| 54 | // ready on the valley its repo delivers to, as a version of the run's |
| 55 | // change. It follows agent:branch-pushed and carries repo, change_id, |
| 56 | // handle, version, title and head. `klaus ready` emits it too, with |
| 57 | // via "klaus ready", when it marks a draft ready. |
| 58 | AgentChangeReady = "agent:change-ready" |
| 59 | // AgentChangeRecorded signals that finalize recorded, for a run without |
| 60 | // a PR, the change or branch the run pushed in place of the ones launch |
| 61 | // recorded, as when an agent made a new change instead of continuing |
| 62 | // the one it was launched on; or that the run pushed heads of more than |
| 63 | // one change, and finalize recorded the one its final head belongs to. |
| 64 | // It comes before agent:completed and carries change_id, branch and |
| 65 | // head as recorded, launch_change_id and launch_branch, and, when the |
| 66 | // run pushed more than one change, pushed: each head's change_id, |
| 67 | // branch and head. |
| 68 | AgentChangeRecorded = "agent:change-recorded" |
| 69 | // AgentChangeDraft signals that finalize recorded a pushed branch's head |
| 70 | // as a version of the run's change on its valley but left the change a |
| 71 | // draft awaiting review (ready_at_finalize off): `klaus ready` marks it |
| 72 | // ready, emitting agent:change-ready. It follows agent:branch-pushed and |
| 73 | // carries what agent:change-ready does. |
| 74 | AgentChangeDraft = "agent:change-draft" |
| 75 | // ChangeRestackDispatched signals that klaus launched a run to resolve |
| 76 | // a restack conflict the valley reported on a change klaus owns (klaus |
| 77 | // restack-conflicts, which the dashboard runs). It goes out under the |
| 78 | // new run's ID and carries repo, change_id, handle, version, head, |
| 79 | // target_tip, report (the report commit), attempt and max_attempts. |
| 80 | ChangeRestackDispatched = "change:restack-dispatched" |
| 81 | // ChangeRestackStalled signals that klaus stopped short of resolving a |
| 82 | // change's restack conflict: its attempts are spent, it couldn't |
| 83 | // return the change to draft, or the launch failed. It goes out under |
| 84 | // the change ID, once per conflict, and carries what |
| 85 | // change:restack-dispatched does, with reason in place of the attempt. |
| 86 | ChangeRestackStalled = "change:restack-stalled" |
| 87 | // The change:* types below report a valley change's life as the |
| 88 | // valley lists it, as pr:approved and pr:merged report a PR's: the |
| 89 | // dashboard's integration check reads the listing and emits each once, |
| 90 | // through the change ledger (pipeline.ChangeLedger), for the changes |
| 91 | // of the session's runs and tracked changes. A change the ledger hasn't |
| 92 | // seen before is recorded as it stands, with no event. Each goes out |
| 93 | // under the change's handle and carries repo, handle, change_id, |
| 94 | // version, head and title: those of the version it concerns. |
| 95 | // |
| 96 | // ChangeApproved signals that a ready version of a change has an |
| 97 | // integration request: the operator approved it. |
| 98 | ChangeApproved = "change:approved" |
| 99 | // ChangeIntegrated signals that a change landed. It carries |
| 100 | // landed_commit too. |
| 101 | ChangeIntegrated = "change:integrated" |
| 102 | // ChangeStale, ChangeRejected and ChangeFailed signal the integrator's |
| 103 | // decision on the request for a change's active version. Each goes out |
| 104 | // again only for a new version or a new decision. |
| 105 | ChangeStale = "change:stale" |
| 106 | ChangeRejected = "change:rejected" |
| 107 | ChangeFailed = "change:failed" |
| 108 | // ChangeWithdrawn signals that a change was withdrawn. |
| 109 | ChangeWithdrawn = "change:withdrawn" |
| 110 | // ChangeNewVersion signals a version of a change that neither this |
| 111 | // session's finalize nor its klaus ready recorded: the valley's |
| 112 | // restacker's, or a human's. It carries uploader too. |
| 113 | ChangeNewVersion = "change:new-version" |
| 114 | // ChangeChecksFailed and ChangeChecksPassed signal that the host's |
| 115 | // check worker failed, or passed, the required checks of a ready |
| 116 | // change's active version, as agent:ci-failed and agent:ci-passed do |
| 117 | // a PR's CI. Each goes out once per version, and again only when the |
| 118 | // checks of that version change their verdict. They carry failed: |
| 119 | // the names of the failed checks. |
| 120 | ChangeChecksFailed = "change:checks-failed" |
| 121 | ChangeChecksPassed = "change:checks-passed" |
| 122 | // PhaseStarted and PhaseCompleted bracket a phase of a run (Phase* |
| 123 | // below). phase:completed carries the phase's started_at, ended_at and |
| 124 | // duration_ms, so a reader needn't pair it with its start. |
| 125 | PhaseStarted = "phase:started" |
| 126 | PhaseCompleted = "phase:completed" |
| 127 | // CheckStarted and CheckCompleted bracket a check an agent ran where |
| 128 | // klaus can see it: `klaus _pre-review`, or a build, test or lint |
| 129 | // command in the agent's stream (Check* kinds below). check:completed |
| 130 | // carries duration_ms and, when known, success. A stream check whose |
| 131 | // command line also ran _pre-review carries includes_pre_review; a |
| 132 | // sandbox run's _pre-review check, which reached the run through the |
| 133 | // agent's stream, carries via: stream. |
| 134 | CheckStarted = "check:started" |
| 135 | CheckCompleted = "check:completed" |
| 136 | ) |
| 137 | |
| 138 | // The phases of a run. Setup is launch's work before the agent starts (fetch, |
| 139 | // worktree, hooks, pane); it has no phase:started, since a launch that fails |
| 140 | // records no run. Agent is the agent's own work, from its pane starting to |
| 141 | // finalize starting. Finalize is _finalize's work, up to killing the pane. |
| 142 | const ( |
| 143 | PhaseSetup = "setup" |
| 144 | PhaseAgent = "agent" |
| 145 | PhaseFinalize = "finalize" |
| 146 | ) |
| 147 | |
| 148 | // The kinds of check:started and check:completed. |
| 149 | const ( |
| 150 | CheckPreReview = "pre-review" |
| 151 | CheckBuild = "build" |
| 152 | CheckTest = "test" |
| 153 | CheckLint = "lint" |
| 154 | ) |
| 155 | |
| 156 | // Dispatch reasons: why a run was launched, recorded on the run |
| 157 | // (dispatch_reason) and on its events. `klaus launch --reason` names any |
| 158 | // other, such as a timer's "docs-timer". |
| 159 | const ( |
| 160 | DispatchLaunch = "launch" // an operator or coordinator launch of new work |
| 161 | DispatchPRFollowUp = "pr-follow-up" // klaus launch --pr |
| 162 | DispatchChangeFollowUp = "change-follow-up" // klaus launch --change |
| 163 | DispatchResume = "resume" // klaus launch --resume-from, without --pr or --change |
| 164 | DispatchBudgetResume = "budget-resume" // --pr on a budget-paused PR |
| 165 | DispatchCIFix = "ci-fix" // the pipeline, for failing CI |
| 166 | DispatchReviewFix = "review-fix" // the pipeline, for changes requested |
| 167 | DispatchReviewCommentFix = "review-comment-fix" // the pipeline, for a trusted reviewer's comments |
| 168 | DispatchConflictFix = "conflict-fix" // the pipeline, for merge conflicts |
| 169 | DispatchRestackConflict = "restack-conflict" // klaus restack-conflicts, for a valley change's restack conflict |
| 170 | DispatchChecksFailed = "checks-failed" // klaus failed-checks, for a ready valley change's failed checks |
| 171 | ) |
| 172 | |
| 173 | // BudgetPausedLabel is the GitHub label applied to PRs whose agents have |
| 174 | // paused due to budget exhaustion. Klaus uses the label as the persistence |
| 175 | // signal for the paused state; the draft PR + label is the canonical record. |
| 176 | const BudgetPausedLabel = "klaus:budget-paused" |
| 177 | |
| 178 | // New creates an Event with the current timestamp. |
| 179 | func New(runID, eventType string, data map[string]interface{}) Event { |
| 180 | return Event{ |
| 181 | Timestamp: time.Now().UTC().Format(time.RFC3339), |
| 182 | RunID: runID, |
| 183 | Type: eventType, |
| 184 | Data: data, |
| 185 | } |
| 186 | } |