1package client
2
3import (
4 "github.com/bnema/vev/internal/ports"
5 "github.com/bnema/vev/internal/protocol"
6)
7
8// outputApplyState is the client-side dependency chain for one attachment.
9// Cells and captured semantic context are admitted together and committed only
10// after the corresponding terminal transaction succeeds.
11type outputApplyState struct {
12 epoch uint64
13 state uint64
14 viewRevision uint64
15 initialized bool
16 context protocol.ViewContext
17}
18
19func (s outputApplyState) next(output protocol.Output) (outputApplyState, bool) {
20 if output.Epoch == 0 {
21 return outputApplyState{}, false
22 }
23 // Ordered side effects cross replay reset gates, but cannot advance or
24 // overwrite committed state. Handoff cleanup must not invent a destination.
25 if output.New == 0 {
26 if output.Base != 0 || output.Full || output.Context != nil {
27 return outputApplyState{}, false
28 }
29 return s, true
30 }
31 if output.Context == nil || output.Context.Validate() != nil || output.Context.Publication <= s.context.Publication {
32 return outputApplyState{}, false
33 }
34 if !s.initialized {
35 if output.Base != 0 || !output.Full {
36 return outputApplyState{}, false
37 }
38 } else {
39 if output.Epoch < s.epoch {
40 return outputApplyState{}, false
41 }
42 if output.Epoch == s.epoch {
43 if output.ViewRevision != s.viewRevision || output.Context.Route.Target.LifecycleID != s.context.Route.Target.LifecycleID {
44 return outputApplyState{}, false
45 }
46 if output.Full || output.Base != s.state || output.New != output.Base+1 {
47 return outputApplyState{}, false
48 }
49 } else if !output.Full || output.Base != 0 {
50 return outputApplyState{}, false
51 }
52 }
53 return outputApplyState{
54 epoch: output.Epoch, state: output.New, viewRevision: output.ViewRevision,
55 initialized: true, context: *output.Context,
56 }, true
57}
58
59// nextView returns accepted and reset-needed separately: old updates are
60// harmless, while missing/future dependencies use the existing coalesced reset
61// request rather than retaining an unbounded metadata reorder queue.
62func (s outputApplyState) nextView(update protocol.UIViewUpdate) (next outputApplyState, accepted, reset bool) {
63 if update.Context.Validate() != nil || update.Epoch == 0 || update.State == 0 || !s.initialized {
64 return s, false, true
65 }
66 if update.Epoch < s.epoch || update.Epoch == s.epoch && update.State < s.state || update.Context.Publication <= s.context.Publication {
67 return s, false, false
68 }
69 if update.Epoch != s.epoch || update.State != s.state || update.Context.Route.Target.LifecycleID != s.context.Route.Target.LifecycleID {
70 return s, false, true
71 }
72 s.context = update.Context
73 return s, true, false
74}
75
76// uiContext composes one publication context from the captured identity and a
77// presentation. It applies the published-context shape rule: only Attached
78// carries the session route, the focus, and the committed output boundary;
79// Picker and Connecting carry the handle and their status with every session
80// field zeroed, so an unattached capture can never present session metadata.
81func (s outputApplyState) uiContext(identity ports.UIContext, status ports.UIPresentationStatus) ports.UIContext {
82 if status != ports.UIStatusAttached {
83 return ports.UIContext{AttachmentHandle: identity.AttachmentHandle, Status: status}
84 }
85 return ports.UIContext{
86 AttachmentHandle: identity.AttachmentHandle, Generation: identity.Generation,
87 Route: s.context.Route, TabID: s.context.TabID, FocusedPaneID: s.context.FocusedPaneID,
88 OutputEpoch: s.epoch, OutputState: s.state, ViewRevision: s.viewRevision,
89 ViewPublication: s.context.Publication, Status: status,
90 }
91}