pi-agent: Fix final integrated review
This commit is contained in:
+63
-70
@@ -42,6 +42,34 @@ const (
|
||||
// exactly one of running, confirmation, succeeded, failed, or closed.
|
||||
type OperationState interface {
|
||||
operationState()
|
||||
decode() DecodedOperationUpdate
|
||||
}
|
||||
|
||||
// OperationUpdateKind is the decoded variant of one operation stream update.
|
||||
// Consumers decode once and handle the payload without repeating type switches.
|
||||
type OperationUpdateKind uint8
|
||||
|
||||
const (
|
||||
OperationRunning OperationUpdateKind = iota + 1
|
||||
OperationConfirmation
|
||||
OperationSucceeded
|
||||
OperationFailed
|
||||
OperationClosed
|
||||
)
|
||||
|
||||
// DecodedOperationUpdate is the complete behavior-bearing projection of an
|
||||
// OperationUpdate variant. Fields that do not apply to Kind retain zero values.
|
||||
type DecodedOperationUpdate struct {
|
||||
Kind OperationUpdateKind
|
||||
Status OperationStatus
|
||||
Progress []TargetProgress
|
||||
Confirmation *Confirmation
|
||||
Err error
|
||||
FirstFailedTargetID string
|
||||
}
|
||||
|
||||
func (u DecodedOperationUpdate) IsTerminal() bool {
|
||||
return u.Kind != OperationRunning
|
||||
}
|
||||
|
||||
type operationRunning struct {
|
||||
@@ -50,10 +78,18 @@ type operationRunning struct {
|
||||
}
|
||||
|
||||
func (operationRunning) operationState() {}
|
||||
func (state operationRunning) decode() DecodedOperationUpdate {
|
||||
return DecodedOperationUpdate{Kind: OperationRunning, Status: state.status, Progress: cloneProgress(state.progress)}
|
||||
}
|
||||
|
||||
type operationConfirmation struct{ request Confirmation }
|
||||
|
||||
func (operationConfirmation) operationState() {}
|
||||
func (state operationConfirmation) decode() DecodedOperationUpdate {
|
||||
request := state.request
|
||||
request.Details = append([]string(nil), request.Details...)
|
||||
return DecodedOperationUpdate{Kind: OperationConfirmation, Confirmation: &request}
|
||||
}
|
||||
|
||||
type operationSucceeded struct {
|
||||
status OperationStatus
|
||||
@@ -61,6 +97,9 @@ type operationSucceeded struct {
|
||||
}
|
||||
|
||||
func (operationSucceeded) operationState() {}
|
||||
func (state operationSucceeded) decode() DecodedOperationUpdate {
|
||||
return DecodedOperationUpdate{Kind: OperationSucceeded, Status: state.status, Progress: cloneProgress(state.progress)}
|
||||
}
|
||||
|
||||
type operationFailed struct {
|
||||
status OperationStatus
|
||||
@@ -70,10 +109,19 @@ type operationFailed struct {
|
||||
}
|
||||
|
||||
func (operationFailed) operationState() {}
|
||||
func (state operationFailed) decode() DecodedOperationUpdate {
|
||||
return DecodedOperationUpdate{
|
||||
Kind: OperationFailed, Status: state.status, Progress: cloneProgress(state.progress),
|
||||
Err: state.err, FirstFailedTargetID: state.firstFailedTargetID,
|
||||
}
|
||||
}
|
||||
|
||||
type operationClosed struct{ status OperationStatus }
|
||||
|
||||
func (operationClosed) operationState() {}
|
||||
func (state operationClosed) decode() DecodedOperationUpdate {
|
||||
return DecodedOperationUpdate{Kind: OperationClosed, Status: state.status}
|
||||
}
|
||||
|
||||
// OperationUpdate is the common progressive contract used by interactive
|
||||
// detection, Target mutation, and Active Profile transition operations. Build
|
||||
@@ -116,79 +164,24 @@ func cloneProgress(progress []TargetProgress) []TargetProgress {
|
||||
return append([]TargetProgress(nil), progress...)
|
||||
}
|
||||
|
||||
func (u OperationUpdate) Status() OperationStatus {
|
||||
switch state := u.State.(type) {
|
||||
case operationRunning:
|
||||
return state.status
|
||||
case operationSucceeded:
|
||||
return state.status
|
||||
case operationFailed:
|
||||
return state.status
|
||||
case operationClosed:
|
||||
return state.status
|
||||
default:
|
||||
return OperationStatus{}
|
||||
// Decode centralizes the closed-set state decoding for all consumers.
|
||||
func (u OperationUpdate) Decode() DecodedOperationUpdate {
|
||||
if u.State == nil {
|
||||
return DecodedOperationUpdate{}
|
||||
}
|
||||
return u.State.decode()
|
||||
}
|
||||
|
||||
func (u OperationUpdate) Progress() []TargetProgress {
|
||||
switch state := u.State.(type) {
|
||||
case operationRunning:
|
||||
return cloneProgress(state.progress)
|
||||
case operationSucceeded:
|
||||
return cloneProgress(state.progress)
|
||||
case operationFailed:
|
||||
return cloneProgress(state.progress)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u OperationUpdate) Confirmation() *Confirmation {
|
||||
state, ok := u.State.(operationConfirmation)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
request := state.request
|
||||
request.Details = append([]string(nil), request.Details...)
|
||||
return &request
|
||||
}
|
||||
|
||||
func (u OperationUpdate) Err() error {
|
||||
if state, ok := u.State.(operationFailed); ok {
|
||||
return state.err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u OperationUpdate) FirstFailedTargetID() string {
|
||||
if state, ok := u.State.(operationFailed); ok {
|
||||
return state.firstFailedTargetID
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (u OperationUpdate) IsRunning() bool {
|
||||
_, ok := u.State.(operationRunning)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (u OperationUpdate) IsConfirmation() bool {
|
||||
_, ok := u.State.(operationConfirmation)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (u OperationUpdate) IsSucceeded() bool {
|
||||
_, ok := u.State.(operationSucceeded)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (u OperationUpdate) IsFailed() bool {
|
||||
_, ok := u.State.(operationFailed)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (u OperationUpdate) IsTerminal() bool { return !u.IsRunning() }
|
||||
func (u OperationUpdate) Status() OperationStatus { return u.Decode().Status }
|
||||
func (u OperationUpdate) Progress() []TargetProgress { return u.Decode().Progress }
|
||||
func (u OperationUpdate) Confirmation() *Confirmation { return u.Decode().Confirmation }
|
||||
func (u OperationUpdate) Err() error { return u.Decode().Err }
|
||||
func (u OperationUpdate) FirstFailedTargetID() string { return u.Decode().FirstFailedTargetID }
|
||||
func (u OperationUpdate) IsRunning() bool { return u.Decode().Kind == OperationRunning }
|
||||
func (u OperationUpdate) IsConfirmation() bool { return u.Decode().Kind == OperationConfirmation }
|
||||
func (u OperationUpdate) IsSucceeded() bool { return u.Decode().Kind == OperationSucceeded }
|
||||
func (u OperationUpdate) IsFailed() bool { return u.Decode().Kind == OperationFailed }
|
||||
func (u OperationUpdate) IsTerminal() bool { return u.Decode().IsTerminal() }
|
||||
|
||||
// OperationStream exposes the useful initial frame synchronously and all
|
||||
// subsequent structured changes through Updates. Updates closes after a
|
||||
|
||||
Reference in New Issue
Block a user