Fix goal recovery

This commit is contained in:
ms2k 2026-10-22 22:05:33 +09:00
commit 996f586bc1

View file

@ -10,11 +10,12 @@ import (
)
type intentLog struct {
Path string `json:"path"`
Pos int64 `json:"pos"`
Fence int64 `json:"fence"`
Partial bool `json:"partial,omitempty"`
Sealed bool `json:"sealed,omitempty"`
Path string `json:"path"`
Pos int64 `json:"pos"`
Fence int64 `json:"fence"`
Partial bool `json:"partial,omitempty"`
Sealed bool `json:"sealed,omitempty"`
Calls map[string]struct{} `json:"calls,omitempty"`
}
func openIntent(path string) (*intentLog, error) {
@ -88,7 +89,7 @@ func (l *intentLog) read(end int64) (goalStatus, error) {
l.Pos = next
continue
}
status, match, err := parseIntent(line)
status, match, err := l.parse(line)
if err != nil {
return "", err
}
@ -99,13 +100,18 @@ func (l *intentLog) read(end int64) (goalStatus, error) {
}
}
func parseIntent(line []byte) (goalStatus, bool, error) {
func (l *intentLog) parse(line []byte) (goalStatus, bool, error) {
var record struct {
Type string `json:"type"`
Payload struct {
Type string `json:"type"`
Role string `json:"role"`
Content []struct {
Type string `json:"type"`
Role string `json:"role"`
Name string `json:"name"`
Namespace string `json:"namespace"`
CallID string `json:"call_id"`
Arguments json.RawMessage `json:"arguments"`
Output json.RawMessage `json:"output"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
@ -117,13 +123,44 @@ func parseIntent(line []byte) (goalStatus, bool, error) {
if err := json.Unmarshal(line, &record); err != nil {
return "", false, err
}
if record.Type != "response_item" {
return "", false, nil
}
p := record.Payload
if record.Type != "response_item" || p.Type != "message" || p.Role != "user" || len(p.Content) != 1 ||
switch p.Type {
case "function_call":
if p.Name == "update_goal" && (p.Namespace == "" || p.Namespace == "functions") && p.CallID != "" {
var args struct {
Status goalStatus `json:"status"`
}
var arguments string
if json.Unmarshal(p.Arguments, &arguments) == nil && json.Unmarshal([]byte(arguments), &args) == nil && args.Status == blocked {
if l.Calls == nil {
l.Calls = make(map[string]struct{})
}
l.Calls[p.CallID] = struct{}{}
}
}
return "", false, nil
case "function_call_output":
if _, ok := l.Calls[p.CallID]; !ok {
return "", false, nil
}
delete(l.Calls, p.CallID)
var output string
var result goalResult
if json.Unmarshal(p.Output, &output) == nil && json.Unmarshal([]byte(output), &result) == nil &&
result.Goal != nil && result.Goal.Status == blocked {
return blocked, true, nil
}
return "", false, nil
}
if p.Type != "message" || p.Role != "user" || len(p.Content) != 1 ||
p.Content[0].Type != "input_text" || len(p.Meta.Kinds) != 1 ||
p.Meta.Kinds[0] != "user.goal" && p.Meta.Kinds[0] != "user.goal.omitted" {
return "", false, nil
}
for _, line := range strings.Split(p.Content[0].Text, "\n") {
for line := range strings.SplitSeq(p.Content[0].Text, "\n") {
if line == "User cleared the goal." {
return "cleared", true, nil
}