GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — harness/turn.go at main · majorcontext/harness

Expand file tree

/

turn.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
192 lines (179 loc) · 6.15 KB
/

turn.go

Copy path

File metadata and controls

192 lines (179 loc) · 6.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package harness
import (
"context"
"errors"
"fmt"
"iter"
"github.com/majorcontext/harness/internal/eventlog"
"github.com/majorcontext/harness/protocol"
)
// Turn follows the outcome of one input. It holds no goroutine; Events and
// Wait read the log of the session, and any number of Turns may follow one
// input. It is safe for concurrent use.
type Turn struct {
s *Session
adm protocol.Admitted
}
// TurnResult is the outcome of an input.
type TurnResult struct {
InputID string
// TurnID is empty for a withdrawn input and for a slash command.
TurnID string
// StopReason is completed, interrupted, failed, or awaiting_input.
StopReason protocol.StopReason
// Cause says why the turn stopped early; it is empty otherwise.
Cause protocol.Cause
// Error is the masked failure text.
Error string
ErrorClass protocol.ErrorClass
// Usage sums the model calls of the turn that the log holds from the
// admission of the input on.
Usage protocol.Usage
// Command is the final status of a typed slash command.
Command string
// Withdrawn says that no turn took the input.
Withdrawn bool
}
// Admitted returns the receipt of the input.
func (t *Turn) Admitted() protocol.Admitted { return t.adm }
// Events yields the events from the admission of the input through the end of
// the turn that took it, or through the withdrawal of the input, or through
// the final status of the command. It yields the ephemeral frames of the
// running turn too. When the session stops first, it ends with Session.Err.
func (t *Turn) Events(ctx context.Context) iter.Seq2[protocol.Event, error] {
return func(yield func(protocol.Event, error) bool) {
if _, err := t.follow(ctx, func(e protocol.Event) bool { return yield(e, nil) }); err != nil {
yield(protocol.Event{}, err)
}
}
}
// Wait returns when the turn that took the input ends, the input is
// withdrawn, or the command finishes. It fails with Session.Err when the
// session stops first, and with the error of ctx.
func (t *Turn) Wait(ctx context.Context) (TurnResult, error) {
return t.follow(ctx, func(protocol.Event) bool { return true })
}
// Steer admits in as a steer input that joins this turn. It fails with
// ErrTurnMismatch when the turn does not run, also when no turn has taken the
// input yet and when the input is a command.
func (t *Turn) Steer(ctx context.Context, in protocol.Input, opts ...PromptOption) (*Turn, error) {
if in.Delivery == protocol.DeliveryQueue || in.ExpectedTurnID != "" {
return nil, fmt.Errorf("%w: Steer sets the delivery and the expected turn", ErrInvalidRequest)
}
turnID, _, err := t.fate(ctx)
if err != nil {
return nil, err
}
if turnID == "" {
return nil, fmt.Errorf("%w: input %s has no running turn", ErrTurnMismatch, t.adm.InputID)
}
in.Delivery, in.ExpectedTurnID = protocol.DeliverySteer, turnID
return t.s.Prompt(ctx, in, opts...)
}
// Cancel withdraws the input while it is queued, or interrupts the turn that
// took it. It returns after the turn has ended. A turn that has ended, an
// input that is withdrawn, and a command are no error.
func (t *Turn) Cancel(ctx context.Context) error {
if t.adm.Command != "" {
return nil
}
for range 2 {
turnID, withdrawn, err := t.fate(ctx)
if err != nil || withdrawn {
return err
}
if turnID != "" {
err := t.s.Interrupt(ctx, protocol.Interrupt{TurnID: turnID})
if errors.Is(err, ErrTurnMismatch) {
return nil
}
return err
}
if err := t.s.Withdraw(ctx, t.adm.InputID); err != nil {
return err
}
}
return nil
}
// fate returns the turn that took the input, and whether it was withdrawn.
func (t *Turn) fate(ctx context.Context) (turnID string, withdrawn bool, err error) {
err = t.s.a.Read(ctx, func(st *eventlog.State) { turnID, withdrawn = st.InputFate(t.adm.InputID) })
return turnID, withdrawn, err
}
// follow reads the events of the session from the admission of the input and
// passes each one to emit. It returns the outcome when it is final, with no
// error, also when emit returns false.
func (t *Turn) follow(ctx context.Context, emit func(protocol.Event) bool) (TurnResult, error) {
return t.followEvents(t.s.Events(ctx, t.adm.Seq-1), emit, false)
}
func (t *Turn) followEvents(events iter.Seq2[protocol.Event, error], emit func(protocol.Event) bool, ownTurn bool) (TurnResult, error) {
res := TurnResult{InputID: t.adm.InputID}
if t.adm.Seq == 0 || t.adm.InputID == "" {
return res, fmt.Errorf("%w: the receipt names no input", ErrInvalidRequest)
}
var usage protocol.Usage
admitted := false
for e, err := range events {
if err != nil {
return res, err
}
if e.Ephemeral && !admitted {
continue
}
admitted = true
done, err := step(e, &res, &usage)
if err != nil {
return res, err
}
if ownTurn && res.TurnID == "" && (e.Ephemeral || e.Seq != t.adm.Seq) && !done {
continue
}
if !emit(e) || done {
return res, nil
}
}
return res, nil
}
// step folds e into r, the outcome of the input, and reports whether r is final.
func step(e protocol.Event, r *TurnResult, usage *protocol.Usage) (done bool, err error) {
switch e.Kind {
case protocol.KindCommandRecorded, protocol.KindInputPromoted, protocol.KindInputWithdrawn,
protocol.KindTurnStarted, protocol.KindContextMeasured, protocol.KindCompactionApplied, protocol.KindTurnEnded:
default:
return false, nil
}
p, err := e.Payload()
if err != nil {
return false, err
}
switch p := p.(type) {
case *protocol.CommandRecorded:
if p.InputID == r.InputID && p.Status != protocol.CommandAccepted {
r.Command, done = p.Status, true
}
case *protocol.InputPromoted:
if p.InputID == r.InputID {
r.TurnID = p.TurnID
}
case *protocol.InputWithdrawn:
if p.InputID == r.InputID {
r.Withdrawn, done = true, true
}
case *protocol.TurnStarted:
*usage = protocol.Usage{}
for _, id := range p.InputIDs {
if id == r.InputID {
r.TurnID = p.TurnID
}
}
case *protocol.ContextMeasured:
*usage = usage.Add(p.Usage)
case *protocol.CompactionApplied:
*usage = usage.Add(p.Usage)
case *protocol.TurnEnded:
if r.TurnID != "" && p.TurnID == r.TurnID {
r.StopReason, r.Cause, r.Error, r.ErrorClass, r.Usage, done = p.StopReason, p.Cause, p.Error, p.ErrorClass, *usage, true
}
}
return done, nil
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.