GeezerVison is showing github.com — feat(acp): add standard session list and resume (#914) · Twigpine/zero@c1937df
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/HOW_ZERO_WORKS.md +3-2Lines changed: 3 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -302,8 +302,9 @@ Conceptually, one requested tool goes through these phases: | 302 | 302 | the model can inspect it and decide what to do next. | 303 | 303 | 6. **Render and persist** — the result is surfaced to the TUI, stream-JSON | 304 | 304 | writer, or ACP client. TUI and exec record rich tool/session events for | 305 | | - resume/history; ACP currently persists conversational messages and streams | 306 | | - tool/permission updates to the client. | | 305 | + resume/history; ACP persists conversational messages and completed tool-call | | 306 | + starts/results for load replay. Live streaming details and permission | | 307 | + exchanges remain client notifications rather than durable history. | 307 | 308 |
| 308 | 309 | Tool success and failure are both informative context. A successful `read_file` | 309 | 310 | result gives the model file contents; a failed `edit_file` result gives the model | |
Collapse file Copy file name to clipboardExpand all lines: internal/acp/agent.go +545-53Lines changed: 545 additions & 53 deletions Large diffs are not rendered by default. Collapse file Copy file name to clipboardExpand all lines: internal/acp/agent_test.go +1,684-13Lines changed: 1684 additions & 13 deletions Large diffs are not rendered by default. Collapse file Copy file name to clipboardExpand all lines: internal/acp/jsonrpc.go +43-10Lines changed: 43 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -89,6 +89,13 @@ type Conn struct { | 89 | 89 |
| 90 | 90 | handlers map[string]HandlerFunc | 91 | 91 | notifiers map[string]NotifyFunc | | 92 | + // Notifications for one method are an ordered stream: session/update in | | 93 | + // particular is stateful, so dispatching every frame in an unrelated | | 94 | + // goroutine can make a later chunk overtake an earlier one. Different | | 95 | + // methods retain independent tails, keeping session/cancel responsive while | | 96 | + // an update handler is busy. | | 97 | + notifyMu sync.Mutex | | 98 | + notifyTails map[string]chan struct{} | 92 | 99 |
| 93 | 100 | mu sync.Mutex | 94 | 101 | nextID int64 | @@ -106,11 +113,12 @@ type Conn struct { | 106 | 113 | // lifetime. | 107 | 114 | func NewConn(r io.Reader, w io.Writer) *Conn { | 108 | 115 | return &Conn{ | 109 | | - rawReader: r, | 110 | | - w: w, | 111 | | - handlers: make(map[string]HandlerFunc), | 112 | | - notifiers: make(map[string]NotifyFunc), | 113 | | - pending: make(map[int64]chan rpcMessage), | | 116 | + rawReader: r, | | 117 | + w: w, | | 118 | + handlers: make(map[string]HandlerFunc), | | 119 | + notifiers: make(map[string]NotifyFunc), | | 120 | + notifyTails: make(map[string]chan struct{}), | | 121 | + pending: make(map[int64]chan rpcMessage), | 114 | 122 | } | 115 | 123 | } | 116 | 124 |
| @@ -303,11 +311,7 @@ func (c *Conn) handleLine(ctx context.Context, line []byte) { | 303 | 311 | }(msg) | 304 | 312 | case msg.isNotify(): | 305 | 313 | if fn := c.notifiers[msg.Method]; fn != nil { | 306 | | - c.wg.Add(1) | 307 | | - go func(m rpcMessage) { | 308 | | - defer c.wg.Done() | 309 | | - fn(ctx, m.Params) | 310 | | - }(msg) | | 314 | + c.dispatchNotification(ctx, msg, fn) | 311 | 315 | } | 312 | 316 | default: | 313 | 317 | // Malformed frame; reply only if we can identify a request id. | @@ -317,6 +321,35 @@ func (c *Conn) handleLine(ctx context.Context, line []byte) { | 317 | 321 | } | 318 | 322 | } | 319 | 323 |
| | 324 | +// dispatchNotification preserves wire order within one method without making | | 325 | +// unrelated notification methods wait for each other. The read loop installs | | 326 | +// each tail before starting its goroutine, so goroutine scheduling cannot | | 327 | +// reorder the chain it observes. | | 328 | +func (c *Conn) dispatchNotification(ctx context.Context, msg rpcMessage, fn NotifyFunc) { | | 329 | + c.notifyMu.Lock() | | 330 | + previous := c.notifyTails[msg.Method] | | 331 | + done := make(chan struct{}) | | 332 | + c.notifyTails[msg.Method] = done | | 333 | + c.notifyMu.Unlock() | | 334 | + | | 335 | + c.wg.Add(1) | | 336 | + go func() { | | 337 | + defer c.wg.Done() | | 338 | + defer func() { | | 339 | + close(done) | | 340 | + c.notifyMu.Lock() | | 341 | + if c.notifyTails[msg.Method] == done { | | 342 | + delete(c.notifyTails, msg.Method) | | 343 | + } | | 344 | + c.notifyMu.Unlock() | | 345 | + }() | | 346 | + if previous != nil { | | 347 | + <-previous | | 348 | + } | | 349 | + fn(ctx, msg.Params) | | 350 | + }() | | 351 | +} | | 352 | + | 320 | 353 | func (c *Conn) dispatchRequest(ctx context.Context, msg rpcMessage) { | 321 | 354 | fn := c.handlers[msg.Method] | 322 | 355 | if fn == nil { | |
Collapse file Copy file name to clipboardExpand all lines: internal/acp/jsonrpc_test.go +80Lines changed: 80 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -360,6 +360,86 @@ func TestConnNotification(t *testing.T) { | 360 | 360 | } | 361 | 361 | } | 362 | 362 |
| | 363 | +func TestConnNotificationsForOneMethodStayInWireOrder(t *testing.T) { | | 364 | + a, b, stop := connPair(t) | | 365 | + defer stop() | | 366 | + | | 367 | + firstStarted := make(chan struct{}) | | 368 | + releaseFirst := make(chan struct{}) | | 369 | + delivered := make(chan int, 2) | | 370 | + b.HandleNotify("update", func(_ context.Context, params json.RawMessage) { | | 371 | + var in struct{ Sequence int } | | 372 | + _ = json.Unmarshal(params, &in) | | 373 | + if in.Sequence == 1 { | | 374 | + close(firstStarted) | | 375 | + <-releaseFirst | | 376 | + } | | 377 | + delivered <- in.Sequence | | 378 | + }) | | 379 | + | | 380 | + if err := a.Notify("update", map[string]int{"Sequence": 1}); err != nil { | | 381 | + t.Fatalf("first notify: %v", err) | | 382 | + } | | 383 | + select { | | 384 | + case <-firstStarted: | | 385 | + case <-time.After(2 * time.Second): | | 386 | + t.Fatal("first notification did not start") | | 387 | + } | | 388 | + if err := a.Notify("update", map[string]int{"Sequence": 2}); err != nil { | | 389 | + t.Fatalf("second notify: %v", err) | | 390 | + } | | 391 | + select { | | 392 | + case sequence := <-delivered: | | 393 | + t.Fatalf("notification %d overtook the blocked first notification", sequence) | | 394 | + case <-time.After(25 * time.Millisecond): | | 395 | + } | | 396 | + close(releaseFirst) | | 397 | + for want := 1; want <= 2; want++ { | | 398 | + select { | | 399 | + case got := <-delivered: | | 400 | + if got != want { | | 401 | + t.Fatalf("notification order = ...,%d; want %d", got, want) | | 402 | + } | | 403 | + case <-time.After(2 * time.Second): | | 404 | + t.Fatalf("notification %d was not delivered", want) | | 405 | + } | | 406 | + } | | 407 | +} | | 408 | + | | 409 | +func TestConnDifferentNotificationMethodsStayConcurrent(t *testing.T) { | | 410 | + a, b, stop := connPair(t) | | 411 | + defer stop() | | 412 | + | | 413 | + updateStarted := make(chan struct{}) | | 414 | + releaseUpdate := make(chan struct{}) | | 415 | + cancelDelivered := make(chan struct{}) | | 416 | + b.HandleNotify("update", func(context.Context, json.RawMessage) { | | 417 | + close(updateStarted) | | 418 | + <-releaseUpdate | | 419 | + }) | | 420 | + b.HandleNotify("cancel", func(context.Context, json.RawMessage) { | | 421 | + close(cancelDelivered) | | 422 | + }) | | 423 | + | | 424 | + if err := a.Notify("update", nil); err != nil { | | 425 | + t.Fatalf("update notify: %v", err) | | 426 | + } | | 427 | + select { | | 428 | + case <-updateStarted: | | 429 | + case <-time.After(2 * time.Second): | | 430 | + t.Fatal("update notification did not start") | | 431 | + } | | 432 | + if err := a.Notify("cancel", nil); err != nil { | | 433 | + t.Fatalf("cancel notify: %v", err) | | 434 | + } | | 435 | + select { | | 436 | + case <-cancelDelivered: | | 437 | + case <-time.After(2 * time.Second): | | 438 | + t.Fatal("different notification method was blocked behind update") | | 439 | + } | | 440 | + close(releaseUpdate) | | 441 | +} | | 442 | + | 363 | 443 | func TestConnMethodNotFound(t *testing.T) { | 364 | 444 | a, _, stop := connPair(t) | 365 | 445 | defer stop() | |
Collapse file Copy file name to clipboardExpand all lines: internal/acp/translate.go +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,14 @@ func agentMessageChunk(delta string) ContentChunk { | 19 | 19 | return ContentChunk{SessionUpdate: UpdateAgentMessageChunk, Content: TextBlock(delta)} | 20 | 20 | } | 21 | 21 |
| | 22 | +func replayMessageChunk(role, messageID, text string) ContentChunk { | | 23 | + update := UpdateAgentMessageChunk | | 24 | + if role == "user" { | | 25 | + update = UpdateUserMessageChunk | | 26 | + } | | 27 | + return ContentChunk{SessionUpdate: update, MessageID: messageID, Content: TextBlock(text)} | | 28 | +} | | 29 | + | 22 | 30 | func agentThoughtChunk(delta string) ContentChunk { | 23 | 31 | return ContentChunk{SessionUpdate: UpdateAgentThoughtChunk, Content: TextBlock(delta)} | 24 | 32 | } | |
Collapse file Copy file name to clipboardExpand all lines: internal/acp/types.go +54-5Lines changed: 54 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,8 @@ const ( | 12 | 12 | MethodAuthenticate = "authenticate" | 13 | 13 | MethodSessionNew = "session/new" | 14 | 14 | MethodSessionLoad = "session/load" | | 15 | + MethodSessionList = "session/list" | | 16 | + MethodSessionResume = "session/resume" | 15 | 17 | MethodSessionPrompt = "session/prompt" | 16 | 18 | MethodSessionCancel = "session/cancel" // notification | 17 | 19 | MethodSessionUpdate = "session/update" // notification (agent -> client) | @@ -62,8 +64,16 @@ type PromptCapabilities struct { | 62 | 64 | } | 63 | 65 |
| 64 | 66 | type AgentCapabilities struct { | 65 | | - LoadSession bool `json:"loadSession"` | 66 | | - PromptCapabilities PromptCapabilities `json:"promptCapabilities"` | | 67 | + LoadSession bool `json:"loadSession"` | | 68 | + PromptCapabilities PromptCapabilities `json:"promptCapabilities"` | | 69 | + SessionCapabilities *SessionCapabilities `json:"sessionCapabilities,omitempty"` | | 70 | +} | | 71 | + | | 72 | +// Empty capability objects are presence flags in ACP v1. Pointers preserve | | 73 | +// the wire distinction between an advertised `{}` and an omitted capability. | | 74 | +type SessionCapabilities struct { | | 75 | + List *struct{} `json:"list,omitempty"` | | 76 | + Resume *struct{} `json:"resume,omitempty"` | 67 | 77 | } | 68 | 78 |
| 69 | 79 | type AuthMethod struct { | @@ -117,9 +127,14 @@ type McpServer struct { | 117 | 127 | } | 118 | 128 |
| 119 | 129 | type NewSessionParams struct { | 120 | | - Cwd string `json:"cwd"` | 121 | | - McpServers []McpServer `json:"mcpServers"` | 122 | | - AdditionalDirectories []string `json:"additionalDirectories,omitempty"` | | 130 | + Cwd string `json:"cwd"` | | 131 | + McpServers []McpServer `json:"mcpServers"` | | 132 | + // AdditionalDirectories is declared by the protocol and consumed nowhere in | | 133 | + // this repo yet. When it is wired up it must go through requestedWorkspace | | 134 | + // like Cwd does: these are client-supplied paths with no absoluteness rule of | | 135 | + // their own, which is the same shape as the resume-cwd hole that made | | 136 | + // {"sessionId":"known"} activate a session against this process's directory. | | 137 | + AdditionalDirectories []string `json:"additionalDirectories,omitempty"` | 123 | 138 | } | 124 | 139 |
| 125 | 140 | type NewSessionResult struct { | @@ -140,6 +155,39 @@ type LoadSessionResult struct { | 140 | 155 | Modes *SessionModeState `json:"modes,omitempty"` | 141 | 156 | } | 142 | 157 |
| | 158 | +// ListSessionsParams and the following types implement ACP v1 session/list. | | 159 | +// Transcript contents stay behind session/load; this method returns metadata | | 160 | +// only and leaves the optional pagination cursor opaque. | | 161 | +type ListSessionsParams struct { | | 162 | + Cwd string `json:"cwd,omitempty"` | | 163 | + Cursor string `json:"cursor,omitempty"` | | 164 | +} | | 165 | + | | 166 | +type SessionInfoMeta struct { | | 167 | + ModelID string `json:"modelId,omitempty"` | | 168 | + CreatedAt string `json:"createdAt,omitempty"` | | 169 | +} | | 170 | + | | 171 | +type SessionInfo struct { | | 172 | + SessionID string `json:"sessionId"` | | 173 | + Cwd string `json:"cwd"` | | 174 | + Title string `json:"title,omitempty"` | | 175 | + UpdatedAt string `json:"updatedAt,omitempty"` | | 176 | + Meta *SessionInfoMeta `json:"_meta,omitempty"` | | 177 | +} | | 178 | + | | 179 | +type ListSessionsResult struct { | | 180 | + Sessions []SessionInfo `json:"sessions"` | | 181 | + NextCursor string `json:"nextCursor,omitempty"` | | 182 | +} | | 183 | + | | 184 | +// session/resume takes session/load's wire shape, so its cwd is a plain string | | 185 | +// too and an omitted one decodes exactly like an empty one. Sharing the type is | | 186 | +// safe only because activatePersistedSession requires an absolute cwd from the | | 187 | +// request itself — nothing downstream may supply a default for either method. | | 188 | +type ResumeSessionParams = LoadSessionParams | | 189 | +type ResumeSessionResult = LoadSessionResult | | 190 | + | 143 | 191 | // ---- prompt turn ---- | 144 | 192 |
| 145 | 193 | type PromptParams struct { | @@ -174,6 +222,7 @@ type SessionNotification struct { | 174 | 222 | // ContentBlock under "content"; the variant is set via SessionUpdate. | 175 | 223 | type ContentChunk struct { | 176 | 224 | SessionUpdate string `json:"sessionUpdate"` | | 225 | + MessageID string `json:"messageId,omitempty"` | 177 | 226 | Content ContentBlock `json:"content"` | 178 | 227 | } | 179 | 228 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/sessions/replay.go +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -226,11 +226,20 @@ func CompactionPayloadFromPlan(summary string, plan CompactionPlan) (CompactionP | 226 | 226 | } | 227 | 227 |
| 228 | 228 | func (store *Store) ReadRehydratedEvents(sessionID string) ([]Event, error) { | 229 | | - events, err := store.ReadEvents(sessionID) | | 229 | + events, _, err := store.ReadRehydratedEventsWithPresence(sessionID) | | 230 | + return events, err | | 231 | +} | | 232 | + | | 233 | +// ReadRehydratedEventsWithPresence carries the underlying event-log presence | | 234 | +// through compaction projection without changing ReadRehydratedEvents' existing | | 235 | +// empty-on-missing contract. | | 236 | +func (store *Store) ReadRehydratedEventsWithPresence(sessionID string) ([]Event, bool, error) { | | 237 | + events, present, err := store.ReadEventsWithPresence(sessionID) | 230 | 238 | if err != nil { | 231 | | - return nil, err | | 239 | + return nil, present, err | 232 | 240 | } | 233 | | - return RehydrateEvents(events) | | 241 | + rehydrated, err := RehydrateEvents(events) | | 242 | + return rehydrated, present, err | 234 | 243 | } | 235 | 244 |
| 236 | 245 | func (store *Store) ReadReplayEvents(sessionID string) ([]Event, error) { | |
Collapse file Copy file name to clipboardExpand all lines: internal/sessions/store.go +14-5Lines changed: 14 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -813,15 +813,24 @@ func (store *Store) UpdateModel(sessionID string, modelID string) (Metadata, err | 813 | 813 | } | 814 | 814 |
| 815 | 815 | func (store *Store) ReadEvents(sessionID string) ([]Event, error) { | | 816 | + events, _, err := store.ReadEventsWithPresence(sessionID) | | 817 | + return events, err | | 818 | +} | | 819 | + | | 820 | +// ReadEventsWithPresence preserves the distinction between an intentionally | | 821 | +// empty event log and a missing one. Most readers accept both as empty history, | | 822 | +// but activation protocols that promise to restore previously populated | | 823 | +// context need the stronger signal. | | 824 | +func (store *Store) ReadEventsWithPresence(sessionID string) ([]Event, bool, error) { | 816 | 825 | if !ValidSessionID(sessionID) { | 817 | | - return nil, fmt.Errorf("invalid zero session id %q", sessionID) | | 826 | + return nil, false, fmt.Errorf("invalid zero session id %q", sessionID) | 818 | 827 | } | 819 | 828 | data, err := os.ReadFile(store.eventsPath(sessionID)) | 820 | 829 | if err != nil { | 821 | 830 | if errors.Is(err, os.ErrNotExist) { | 822 | | - return []Event{}, nil | | 831 | + return []Event{}, false, nil | 823 | 832 | } | 824 | | - return nil, fmt.Errorf("read zero session events: %w", err) | | 833 | + return nil, false, fmt.Errorf("read zero session events: %w", err) | 825 | 834 | } | 826 | 835 | // A genuine torn tail is an INCOMPLETE final write — a crash mid-append leaves | 827 | 836 | // the last line without its terminating newline. If the file ends with a | @@ -851,11 +860,11 @@ func (store *Store) ReadEvents(sessionID string) ([]Event, error) { | 851 | 860 | if index == lastNonEmpty && tornTailPossible { | 852 | 861 | break | 853 | 862 | } | 854 | | - return nil, fmt.Errorf("invalid json in zero session %s %s at line %d: %w", sessionID, EventsFile, index+1, err) | | 863 | + return nil, true, fmt.Errorf("invalid json in zero session %s %s at line %d: %w", sessionID, EventsFile, index+1, err) | 855 | 864 | } | 856 | 865 | events = append(events, event) | 857 | 866 | } | 858 | | - return events, nil | | 867 | + return events, true, nil | 859 | 868 | } | 860 | 869 |
| 861 | 870 | func (store *Store) timestamp() string { | |
|