Merge main into lane-retry: per-run usage recording alongside live run events
main brought pushed run events (emitRun/RunEventFrame across protocol, server and the Android client) and this branch brought per-run usage recording. Both touch internal/server/runner.go; git auto-merged cleanly and the result keeps both intents — each model step is stamped with its effective provider/model and budget-derived usage (stageUsage, overlayEffectiveModels) while run events are emitted (emitRun) and applied to the same step loop. No conflict markers, no side's work discarded. Post-merge checks: gofmt -l clean, go build ./..., go vet ./... and go test ./... all pass.
This commit is contained in:
@@ -47,6 +47,13 @@ type runner struct {
|
||||
// events pushes run state deltas to subscribed clients. When nil (tests that
|
||||
// build a runner directly) emits are dropped, so nothing else changes.
|
||||
events *runEventHub
|
||||
// identMu guards idents: the pipeline identity and stored title (see
|
||||
// runIdentity) that every emitted frame of a run carries, so a client with no
|
||||
// cached row renders a brand-new run without re-reading the list. Populated
|
||||
// where a run is created, decided or canceled; a run with no remembered
|
||||
// identity keeps blank identity fields and the client resyncs once instead.
|
||||
identMu sync.Mutex
|
||||
idents map[int64]runIdentity
|
||||
// exportSession writes a finished stage's session into its own artifacts
|
||||
// directory. It defaults to stage.ExportSession; a failure is best-effort and
|
||||
// never changes the stage's status, outcome, target or result validation.
|
||||
@@ -82,7 +89,7 @@ func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
|
||||
return nil, err
|
||||
}
|
||||
root := filepath.Join(s.Directory(), "artifacts")
|
||||
return &runner{ctx: ctx, store: s, artifactsRoot: root, active: map[int64]activeRun{}, attentionSent: map[int64]map[string]bool{}, exportSession: stage.ExportSession}, nil
|
||||
return &runner{ctx: ctx, store: s, artifactsRoot: root, active: map[int64]activeRun{}, idents: map[int64]runIdentity{}, attentionSent: map[int64]map[string]bool{}, exportSession: stage.ExportSession}, nil
|
||||
}
|
||||
|
||||
func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.RunDetail, error) {
|
||||
@@ -252,6 +259,18 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// The run row now exists: read it once for its resolved identity (pipeline
|
||||
// id/name, the store-derived title, the pinned version), so the queued frame
|
||||
// and every later frame carry what a client needs to render a brand-new run
|
||||
// without re-reading the list. A failed read only costs that seed: the run is
|
||||
// still launched below and its frames carry no identity, so the client falls
|
||||
// back to its one resync.
|
||||
detail, readErr := r.store.GetRun(r.ctx, id)
|
||||
if readErr != nil {
|
||||
detail = nil
|
||||
} else if detail != nil {
|
||||
r.rememberRunIdentity(id, identityOfRunSummary(detail.RunSummary))
|
||||
}
|
||||
if created {
|
||||
// Emit the created delta before launching: the queued frame reaches
|
||||
// subscribers before the running/step-started transition, so a client
|
||||
@@ -266,12 +285,14 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
|
||||
r.run(ctx, id, d, workspace, request)
|
||||
})
|
||||
}
|
||||
detail, err := r.store.GetRun(r.ctx, id)
|
||||
if detail != nil {
|
||||
_, detail.Executing = r.active[id]
|
||||
r.overlayEffectiveModels(detail)
|
||||
}
|
||||
return detail, err
|
||||
if readErr != nil {
|
||||
return nil, readErr
|
||||
}
|
||||
return detail, nil
|
||||
}
|
||||
|
||||
// launch registers an active run's cancellation seam and workspace, adds it to
|
||||
@@ -406,6 +427,7 @@ func (r *runner) cancel(id int64) error {
|
||||
r.forgetAttention(id)
|
||||
if r.events != nil {
|
||||
if detail, err := r.store.GetRun(r.ctx, id); err == nil && detail != nil {
|
||||
r.rememberRunIdentity(id, identityOfRunSummary(detail.RunSummary))
|
||||
r.emitRun(protocol.RunEventFrame{
|
||||
RunID: id,
|
||||
Workspace: detail.Workspace,
|
||||
@@ -450,6 +472,9 @@ func (r *runner) decide(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
return nil, err
|
||||
}
|
||||
if applied {
|
||||
// Re-seed the identity so the resumed frames (this one and the resumed
|
||||
// loop's) carry it even after a restart, when no creation was observed.
|
||||
r.rememberRunIdentity(detail.ID, identityOfRunSummary(detail.RunSummary))
|
||||
for i := range detail.Steps {
|
||||
if detail.Steps[i].Sequence == step.Sequence && detail.Steps[i].Stage == step.Stage {
|
||||
detail.Steps[i] = step
|
||||
@@ -518,6 +543,9 @@ func terminalFinish(target, stageKey, outcome, diagnostic string) (status, final
|
||||
// shared loop from the decision's target; no full-table scan and no extra
|
||||
// per-step query are introduced.
|
||||
func (r *runner) resume(ctx context.Context, id int64, d *protocol.RunDetail, workspace string, step protocol.RunStep) {
|
||||
// A resumed run's frames carry the same identity as its creation, so re-seed
|
||||
// it here too: resume is also reachable from a store read after a restart.
|
||||
r.rememberRunIdentity(id, identityOfRunSummary(d.RunSummary))
|
||||
// The decision may route straight to a terminal, which has no stage to run.
|
||||
if status, outcome, diagnostic, terminal := terminalFinish(step.Target, step.Stage, step.Outcome, step.Diagnostic); terminal {
|
||||
r.finishRunState(id, d.Name, workspace, status, outcome, diagnostic, completedSteps(d.Steps), step.Sequence, step.Stage, stage.Values(step.Output))
|
||||
@@ -615,6 +643,60 @@ func (s *runState) publishServerChangedFiles() {
|
||||
s.memory["input"] = entry
|
||||
}
|
||||
|
||||
// runIdentity is the pipeline identity and stored title a run's pushed frames
|
||||
// carry (RunEventFrame.PipelineID/Name/Title/Version). It is remembered where a
|
||||
// run is created, decided or canceled and stamped onto every frame that leaves
|
||||
// emitRun, so a client that has never seen the run can build its list row from
|
||||
// the frame without re-reading the whole list.
|
||||
type runIdentity struct {
|
||||
pipelineID int64
|
||||
name string
|
||||
title string
|
||||
version int
|
||||
}
|
||||
|
||||
// identityOfRunSummary is the one projection of a run row onto the identity its
|
||||
// pushed frames carry, so the frame and the list row cannot disagree.
|
||||
func identityOfRunSummary(s protocol.RunSummary) runIdentity {
|
||||
return runIdentity{pipelineID: s.PipelineID, name: s.Name, title: s.Title, version: s.Version}
|
||||
}
|
||||
|
||||
// rememberRunIdentity records the identity every later frame of run id carries.
|
||||
// It is safe from any goroutine and takes its own lock (never r.mu), so it is
|
||||
// callable from a path that already holds r.mu.
|
||||
func (r *runner) rememberRunIdentity(id int64, ident runIdentity) {
|
||||
if r.idents == nil {
|
||||
return
|
||||
}
|
||||
r.identMu.Lock()
|
||||
r.idents[id] = ident
|
||||
r.identMu.Unlock()
|
||||
}
|
||||
|
||||
// stampRunIdentity fills a frame's blank identity fields from the run's
|
||||
// remembered identity, leaving any the emit site set untouched. A run with no
|
||||
// remembered identity keeps them blank, so the client falls back to its resync.
|
||||
func (r *runner) stampRunIdentity(frame *protocol.RunEventFrame) {
|
||||
r.identMu.Lock()
|
||||
ident, ok := r.idents[frame.RunID]
|
||||
r.identMu.Unlock()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if frame.PipelineID == 0 {
|
||||
frame.PipelineID = ident.pipelineID
|
||||
}
|
||||
if frame.Name == "" {
|
||||
frame.Name = ident.name
|
||||
}
|
||||
if frame.Title == "" {
|
||||
frame.Title = ident.title
|
||||
}
|
||||
if frame.Version == 0 {
|
||||
frame.Version = ident.version
|
||||
}
|
||||
}
|
||||
|
||||
// emitRun pushes one run delta to the subscribed-client hub when one is wired,
|
||||
// and does nothing otherwise (a runner built directly in a test has no hub).
|
||||
// terminal marks a final transition, which the hub flushes without coalescing.
|
||||
@@ -622,6 +704,7 @@ func (r *runner) emitRun(frame protocol.RunEventFrame, terminal bool) {
|
||||
if r.events == nil {
|
||||
return
|
||||
}
|
||||
r.stampRunIdentity(&frame)
|
||||
r.events.emit(frame, terminal)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user