GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(acp): add standard session list and resume (#914) · Twigpine/zero@c1937df

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs
    • HOW_ZERO_WORKS.md
  • internal
    • acp
      • agent.go
      • agent_test.go
      • jsonrpc.go
      • jsonrpc_test.go
      • translate.go
      • types.go
    • sessions
      • replay.go
      • store.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/HOW_ZERO_WORKS.md‎

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 numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ Conceptually, one requested tool goes through these phases:
302302
the model can inspect it and decide what to do next.
303303
6. **Render and persist** — the result is surfaced to the TUI, stream-JSON
304304
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.
307308

308309
Tool success and failure are both informative context. A successful `read_file`
309310
result gives the model file contents; a failed `edit_file` result gives the model
Collapse file

‎internal/acp/agent.go‎

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

‎internal/acp/agent_test.go‎

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

‎internal/acp/jsonrpc.go‎

Copy file name to clipboardExpand all lines: internal/acp/jsonrpc.go
+43-10Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ type Conn struct {
8989

9090
handlers map[string]HandlerFunc
9191
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{}
9299

93100
mu sync.Mutex
94101
nextID int64
@@ -106,11 +113,12 @@ type Conn struct {
106113
// lifetime.
107114
func NewConn(r io.Reader, w io.Writer) *Conn {
108115
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),
114122
}
115123
}
116124

@@ -303,11 +311,7 @@ func (c *Conn) handleLine(ctx context.Context, line []byte) {
303311
}(msg)
304312
case msg.isNotify():
305313
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)
311315
}
312316
default:
313317
// Malformed frame; reply only if we can identify a request id.
@@ -317,6 +321,35 @@ func (c *Conn) handleLine(ctx context.Context, line []byte) {
317321
}
318322
}
319323

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+
320353
func (c *Conn) dispatchRequest(ctx context.Context, msg rpcMessage) {
321354
fn := c.handlers[msg.Method]
322355
if fn == nil {
Collapse file

‎internal/acp/jsonrpc_test.go‎

Copy file name to clipboardExpand all lines: internal/acp/jsonrpc_test.go
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,86 @@ func TestConnNotification(t *testing.T) {
360360
}
361361
}
362362

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+
363443
func TestConnMethodNotFound(t *testing.T) {
364444
a, _, stop := connPair(t)
365445
defer stop()
Collapse file

‎internal/acp/translate.go‎

Copy file name to clipboardExpand all lines: internal/acp/translate.go
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ func agentMessageChunk(delta string) ContentChunk {
1919
return ContentChunk{SessionUpdate: UpdateAgentMessageChunk, Content: TextBlock(delta)}
2020
}
2121

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+
2230
func agentThoughtChunk(delta string) ContentChunk {
2331
return ContentChunk{SessionUpdate: UpdateAgentThoughtChunk, Content: TextBlock(delta)}
2432
}
Collapse file

‎internal/acp/types.go‎

Copy file name to clipboardExpand all lines: internal/acp/types.go
+54-5Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const (
1212
MethodAuthenticate = "authenticate"
1313
MethodSessionNew = "session/new"
1414
MethodSessionLoad = "session/load"
15+
MethodSessionList = "session/list"
16+
MethodSessionResume = "session/resume"
1517
MethodSessionPrompt = "session/prompt"
1618
MethodSessionCancel = "session/cancel" // notification
1719
MethodSessionUpdate = "session/update" // notification (agent -> client)
@@ -62,8 +64,16 @@ type PromptCapabilities struct {
6264
}
6365

6466
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"`
6777
}
6878

6979
type AuthMethod struct {
@@ -117,9 +127,14 @@ type McpServer struct {
117127
}
118128

119129
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"`
123138
}
124139

125140
type NewSessionResult struct {
@@ -140,6 +155,39 @@ type LoadSessionResult struct {
140155
Modes *SessionModeState `json:"modes,omitempty"`
141156
}
142157

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+
143191
// ---- prompt turn ----
144192

145193
type PromptParams struct {
@@ -174,6 +222,7 @@ type SessionNotification struct {
174222
// ContentBlock under "content"; the variant is set via SessionUpdate.
175223
type ContentChunk struct {
176224
SessionUpdate string `json:"sessionUpdate"`
225+
MessageID string `json:"messageId,omitempty"`
177226
Content ContentBlock `json:"content"`
178227
}
179228

Collapse file

‎internal/sessions/replay.go‎

Copy file name to clipboardExpand all lines: internal/sessions/replay.go
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,20 @@ func CompactionPayloadFromPlan(summary string, plan CompactionPlan) (CompactionP
226226
}
227227

228228
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)
230238
if err != nil {
231-
return nil, err
239+
return nil, present, err
232240
}
233-
return RehydrateEvents(events)
241+
rehydrated, err := RehydrateEvents(events)
242+
return rehydrated, present, err
234243
}
235244

236245
func (store *Store) ReadReplayEvents(sessionID string) ([]Event, error) {
Collapse file

‎internal/sessions/store.go‎

Copy file name to clipboardExpand all lines: internal/sessions/store.go
+14-5Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -813,15 +813,24 @@ func (store *Store) UpdateModel(sessionID string, modelID string) (Metadata, err
813813
}
814814

815815
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) {
816825
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)
818827
}
819828
data, err := os.ReadFile(store.eventsPath(sessionID))
820829
if err != nil {
821830
if errors.Is(err, os.ErrNotExist) {
822-
return []Event{}, nil
831+
return []Event{}, false, nil
823832
}
824-
return nil, fmt.Errorf("read zero session events: %w", err)
833+
return nil, false, fmt.Errorf("read zero session events: %w", err)
825834
}
826835
// A genuine torn tail is an INCOMPLETE final write — a crash mid-append leaves
827836
// 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) {
851860
if index == lastNonEmpty && tornTailPossible {
852861
break
853862
}
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)
855864
}
856865
events = append(events, event)
857866
}
858-
return events, nil
867+
return events, true, nil
859868
}
860869

861870
func (store *Store) timestamp() string {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.