| 1 | // Compiled inside each historical tag, never against the current module. |
| 2 | package main |
| 3 | |
| 4 | import ( |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "reasonix/internal/agent" |
| 11 | "reasonix/internal/provider" |
| 12 | // CANONICAL_IMPORT |
| 13 | ) |
| 14 | |
| 15 | func must(err error) { if err != nil { panic(err) } } |
| 16 | func body(value any) []byte { b, err := json.Marshal(value); must(err); return b } |
| 17 | func save(path string, value any) { must(os.WriteFile(path, append(body(value), '\n'), 0600)) } |
| 18 | |
| 19 | func main() { |
| 20 | // CONTINUE_CALL |
| 21 | output := os.Args[1] |
| 22 | messages := []provider.Message{ |
| 23 | {ID:"system", Role:provider.RoleSystem, Content:"Synthetic historical system prompt"}, |
| 24 | {ID:"question", Role:provider.RoleUser, Content:"中文历史问题 # %20", Images:[]string{"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII="}}, |
| 25 | {ID:"call", Role:provider.RoleAssistant, Content:"Inspecting file", ReasoningContent:"Synthetic reasoning", ReasoningSignature:"synthetic-signature", ToolCalls:[]provider.ToolCall{{ID:"call-original",Name:"read_file",Arguments:`{"path":"中文 # %.txt"}`}}}, |
| 26 | {ID:"result",Role:provider.RoleTool,ToolCallID:"call-original",Name:"read_file",Content:strings.Repeat("large persisted tool result 中文\n",2048)}, |
| 27 | {ID:"answer",Role:provider.RoleAssistant,Content:"Historical final answer"}, |
| 28 | } |
| 29 | for n:=0;n<70;n++ { |
| 30 | messages=append(messages,provider.Message{ID:fmt.Sprintf("u-%d",n),Role:provider.RoleUser,Content:fmt.Sprintf("Question %d",n)},provider.Message{ID:fmt.Sprintf("a-%d",n),Role:provider.RoleAssistant,Content:fmt.Sprintf("Answer %d",n)}) |
| 31 | } |
| 32 | legacy := filepath.Join(output,"legacy") |
| 33 | must(os.MkdirAll(legacy,0700)) |
| 34 | for _,name:=range []string{"complete","branch","interrupted"} { |
| 35 | s:=agent.NewSession("") |
| 36 | selected:=messages |
| 37 | if name=="branch" { selected=append(append([]provider.Message{},messages[:5]...),provider.Message{ID:"branch-question",Role:provider.RoleUser,Content:"Independent branch work"}) } |
| 38 | for _,m:=range selected {s.Add(m)} |
| 39 | if name=="interrupted" {s.Add(provider.Message{ID:"partial",Role:provider.RoleAssistant,Content:"Partial visible response",LocalOnly:true})} |
| 40 | path:=filepath.Join(legacy,name+".jsonl") |
| 41 | must(s.Save(path)) |
| 42 | metadata:=agent.BranchMeta{ID:name,Scope:"global",TopicID:"topic-"+name,TopicTitle:"Historical "+name,CustomTitle:"User title "+name,Model:"fixture/model",ToolApprovalMode:"ask",Goal:"Retained goal"} |
| 43 | if name=="branch" { metadata.ParentID="complete";metadata.ForkTurn=1 } |
| 44 | must(agent.SaveBranchMeta(path,metadata)) |
| 45 | loaded,err:=agent.LoadSession(path);must(err) |
| 46 | save(filepath.Join(output,name+"-expected.json"),loaded.Snapshot()) |
| 47 | } |
| 48 | // CANONICAL_CALL |
| 49 | } |
| 50 | |
| 51 | // CANONICAL_START |
| 52 | func continueCanonical(directory, id string) { |
| 53 | ctx:=context.Background() |
| 54 | service,err:=session.NewService("desktop",session.NewFilesystemPersistence(filepath.Dir(directory)));must(err) |
| 55 | ref:=session.SessionRef{HostID:"desktop",SessionID:id} |
| 56 | snapshot,err:=service.Query().Snapshot(ctx,ref) |
| 57 | if errors.Is(err,session.ErrUnsupportedVersion) {fmt.Println(`{"status":"unsupported_version"}`);return};must(err) |
| 58 | // The old public query replays the history; a cold runtime's snapshot can |
| 59 | // deliberately omit history until hydration and is not a read result. |
| 60 | if len(snapshot.Projection.Messages)==0 {panic("new-version history was not readable")} |
| 61 | binding,err:=service.Open(ctx,ref);must(err) |
| 62 | _,err=binding.Runtime().Session().Append(ctx,session.Batch{OperationID:"continued-in-old-version",Events:[]session.Event{{Kind:"message/complete",Payload:body(map[string]any{"message":provider.Message{ID:"old-version-user",Role:provider.RoleUser,Content:"Continued in the actual old writer"}})}}});must(err) |
| 63 | must(binding.Release(ctx));must(service.Close(ctx,ref)) |
| 64 | fmt.Println(`{"status":"continued"}`) |
| 65 | } |
| 66 | |
| 67 | func writeCanonical(output string, messages []provider.Message) { |
| 68 | ctx:=context.Background() |
| 69 | for _,name:=range []string{"complete","compacted","interrupted"} { |
| 70 | id:="canonical-"+name |
| 71 | writer,err:=session.CreateStore(filepath.Join(output,"canonical",id),id);must(err) |
| 72 | events:=[]session.Event{{Kind:"turn/start"}} |
| 73 | for _,message:=range messages {events=append(events,session.Event{Kind:"message/complete",Payload:body(map[string]any{"message":message})})} |
| 74 | events=append(events,session.Event{Kind:"goal/state",Payload:json.RawMessage(`{"goal":"Retained canonical goal","status":"paused","futureGoal":{"keep":true}}`)}) |
| 75 | if name=="compacted" {events=append(events,session.Event{Kind:"compaction",Payload:body(map[string]any{"messages":[]provider.Message{{ID:"summary",Role:provider.RoleSystem,Content:"Persisted compaction summary"}},"trigger":"manual"})})} |
| 76 | if name!="interrupted" {events=append(events,session.Event{Kind:"turn/end",Payload:json.RawMessage(`{"status":"completed"}`)})} |
| 77 | _,err=writer.Append(ctx,session.Batch{OperationID:"historical-operation",TurnID:"historical-turn",Events:events});must(err) |
| 78 | must(writer.Close(ctx)) |
| 79 | } |
| 80 | save(filepath.Join(output,"canonical-expected.json"),messages) |
| 81 | // FORMAL_CALL |
| 82 | } |
| 83 | // CANONICAL_END |
| 84 |