Reconcile goal recovery with human intent and model state transitions

This commit is contained in:
ms2k 2026-10-18 19:21:26 +09:00
commit 548eb69211

View file

@ -21,13 +21,23 @@ func sameGoal(a, b *goal) bool {
return a != nil && b != nil && a.Objective == b.Objective && a.CreatedAt == b.CreatedAt
}
func sameState(a, b *goal) bool {
return sameGoal(a, b) && a.Status == b.Status && a.UpdatedAt == b.UpdatedAt
}
type watch struct {
Goal *goal `json:"goal,omitempty"`
Turn turn `json:"turn"`
Armed bool `json:"armed"`
Failures int `json:"failures"`
Due time.Time `json:"due"`
Goal *goal `json:"goal,omitempty"`
Turn turn `json:"turn"`
Armed bool `json:"armed"`
Known bool `json:"known"`
Pending bool `json:"pending,omitempty"`
Hold *goal `json:"hold,omitempty"`
Source *intentLog `json:"source,omitempty"`
Failures int `json:"failures"`
Due time.Time `json:"due"`
revision uint64
echo *goal
seed *goal
}
func (w *watch) stop() {
@ -37,11 +47,31 @@ func (w *watch) stop() {
func (w *watch) update(g *goal) {
w.revision++
if !sameGoal(w.Goal, g) {
w.stop()
w.Failures = 0
if w.Hold != nil {
if !sameGoal(w.Hold, g) {
w.Hold = nil
} else if g.Status == active && g.Status == w.Hold.Status && !w.Pending && w.echo == nil {
w.Hold = nil
if g.Status == active {
w.Armed = true
}
} else {
if g.Status == w.Hold.Status {
w.Goal = g
}
w.stop()
return
}
}
previous := w.Goal
same := sameGoal(previous, g)
if !same {
w.stop()
w.Known = false
w.Failures = 0
} else if w.Armed || previous.Status == active {
w.Known = true
}
w.Goal = g
if g == nil {
w.stop()
@ -49,48 +79,89 @@ func (w *watch) update(g *goal) {
}
switch g.Status {
case active:
if previous != nil && previous.Status != active {
w.stop()
}
if w.Turn.Status == "inProgress" {
w.Known = true
if !same || previous.Status != active {
w.Due = time.Time{}
w.Armed = true
} else if w.Turn.Status == "inProgress" {
w.Armed = true
}
case blocked:
if !w.Due.IsZero() && previous != nil && previous.UpdatedAt != g.UpdatedAt {
w.stop()
if !same && w.Pending {
w.Hold = g
}
default:
w.stop()
w.Hold = g
}
}
func (w *watch) notice(g *goal) {
if w.seed != nil {
w.seed = nil
return
}
if sameState(w.echo, g) {
w.echo = nil
return
}
if w.Hold != nil && sameGoal(w.Hold, g) {
w.revision++
if g.Status == blocked || g.Status == active {
return
}
w.Goal, w.Hold = g, g
w.stop()
return
}
if g != nil && g.Status == active && sameGoal(w.Goal, g) {
w.revision++
return
}
w.update(g)
}
func (w *watch) observe(t turn) {
if t.ID == "" {
return
}
w.revision++
if t.ID != w.Turn.ID {
w.stop()
w.Turn = t
w.Armed = t.Status == "inProgress" && w.Goal != nil && w.Goal.Status == active
return
fresh := t.ID != w.Turn.ID
if w.Known && (fresh || w.Turn.Status == "inProgress") && t.Status == "failed" &&
t.Error != nil && t.Error.Info.retryable() {
w.Armed = true
}
if fresh {
w.Due = time.Time{}
if w.Goal != nil && t.Status == "inProgress" && w.Goal.Status == active {
w.Armed = true
}
}
w.Turn = t
switch t.Status {
case "completed":
w.stop()
w.Due = time.Time{}
w.Failures = 0
case "interrupted":
w.stop()
if w.Pending && w.Goal != nil {
desired := *w.Goal
desired.Status = paused
w.Goal, w.Hold = &desired, &desired
}
case "failed":
if t.Error == nil || !t.Error.Info.retryable() {
w.stop()
}
}
if w.Hold != nil {
w.stop()
}
}
func (w *watch) eligible() bool {
return w.Armed && w.Goal != nil && w.Goal.Status == blocked &&
return w.Hold == nil && w.Armed && w.Goal != nil && w.Goal.Status == blocked &&
w.Turn.Status == "failed" && w.Turn.Error != nil && w.Turn.Error.Info.retryable()
}