Documentation
¶
Overview ¶
Package brainsync makes 2nd Brain one memory across the user's own linked computers (ADR-118; 03-contracts.md § 2nd Brain across computers).
A link runs one way (ADR-067): the computer that linked the others — the controller — reaches them, never the reverse. So the controller is the brain's home and drives every exchange: on each round it sends a reader what changed in the home's brain since the reader last took it in, and takes back what the reader's sessions learned, in one request through the existing end-to-end encrypted channel (POST /v1/brain/sync). Every computer then answers recall, the handoff and the MCP tools from its own database — the home's memories included — so the 150 ms recall budget is never spent on a network, and a reader whose home is asleep still knows everything up to the last round, and says how old that is.
What a reader learns while its home is offline waits in its brain_changes after the cursor the home last took (the queue is the log itself) and goes on the next round; nothing is lost and nothing is taken twice.
Index ¶
Constants ¶
const ( // Interval is how often the home starts a round with each computer. Interval = 3 * time.Minute // FirstRound is how long after start the first round waits, so a // daemon's start is not slowed by it. FirstRound = 30 * time.Second // Stale is how long a reader goes without hearing from its home before // it says the home is offline: three rounds missed and a little more. Stale = 10 * time.Minute // RoundWait bounds one request through the link. RoundWait = 45 * time.Second // MaxPages bounds the pages one round moves each way, so a first round // with a big brain is spread over a few rounds rather than one long one. MaxPages = 25 )
Timing of the rounds.
const Path = "/v1/brain/sync"
Path is the route a reader answers on, for a computer allowed there only.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Home ¶
type Home struct {
Store *store.Store
Link Link
On func() bool
Scope func() store.BrainSyncScope
Name func() string
// Changed is told when a round changed the brain here.
Changed func()
Now func() time.Time
Log *slog.Logger
// contains filtered or unexported fields
}
Home is the side that drives rounds: the computer that linked the others.
func (*Home) Round ¶
Round is one exchange with computer m: what the home learned goes there, what m learned comes here, a page at a time, until both are level or MaxPages each way were moved.
func (*Home) States ¶
func (h *Home) States() []MachineState
States is every linked computer's state, in the link's order.
type HomeChoice ¶
HomeChoice is a computer that reaches this one.
type HomeState ¶
type HomeState struct {
Device string `json:"device"`
Name string `json:"name"`
// LastHeard is when the home last reached this computer, unix ms; 0 for
// never.
LastHeard int64 `json:"last_heard,omitempty"`
// Stale says the home has not been heard from for Stale: answers here
// come from this computer's copy, as of LastHeard.
Stale bool `json:"stale"`
// Memories counts the memories that came from it.
Memories int `json:"memories"`
// Picked says the user chose it; false is the default, the first
// computer allowed here.
Picked bool `json:"picked"`
}
HomeState is this computer's home as a reader sees it.
type Link ¶
type Link interface {
List() []peers.Status
Do(id string, req *http.Request) (*http.Response, error)
}
Link is the channel to the computers this one reaches (peers.Manager).
type MachineState ¶
type MachineState struct {
Machine string `json:"machine"`
Name string `json:"name"`
// Peer is its 2nd Brain id, once it answered.
Peer string `json:"peer,omitempty"`
// State: "sharing", "off" (its switch is off), "other_home" (it takes
// another computer as its home; HomeName says which), "old" (its
// Caprock predates this), "offline", "error".
State string `json:"state"`
HomeName string `json:"home_name,omitempty"`
Error string `json:"error,omitempty"`
LastOK int64 `json:"last_ok,omitempty"`
LastTry int64 `json:"last_try,omitempty"`
// Sent and Taken count the memories that went there and came here in
// this daemon's life.
Sent int `json:"sent"`
Taken int `json:"taken"`
}
MachineState is how sharing with one linked computer stands, for the 2nd Brain screen and Settings → Computers.
type Peer ¶
Peer is a brain as it names itself to another: its 2nd Brain id and the computer's name.
type Reader ¶
type Reader struct {
Store *store.Store
// On is the switch on this computer.
On func() bool
// Home is the device id of the computer this one takes as its home, and
// its name; "" when none reaches it.
Home func() (device, name string)
// Scope is what never leaves this computer.
Scope func() store.BrainSyncScope
// Name is this computer's name.
Name func() string
// Changed is told when a batch changed the brain here.
Changed func()
Now func() time.Time
}
Reader is the side that answers: every computer some other one links.
type Request ¶
type Request struct {
From Peer `json:"from"`
// After is how far the home has taken in the reader's changes: the
// reader sends what changed after it.
After int64 `json:"after"`
// Batch is the home's changes after what the reader said it received.
Batch *store.BrainSyncBatch `json:"batch,omitempty"`
}
Request is POST /v1/brain/sync's body, from the home to a reader.
type Response ¶
type Response struct {
Self Peer `json:"self"`
// On is the reader's own switch, One 2nd Brain across my computers.
On bool `json:"on"`
// Home says the reader takes the caller as its brain's home; when not,
// HomeName names the computer it does take, and nothing was exchanged.
Home bool `json:"home"`
HomeName string `json:"home_name,omitempty"`
// Received is how far the reader has taken in the home's changes.
Received int64 `json:"received"`
// Batch is the reader's changes after Request.After.
Batch *store.BrainSyncBatch `json:"batch,omitempty"`
}
Response is the reader's answer.
type Status ¶
type Status struct {
On bool `json:"on"`
Self Peer `json:"self"`
// Home is this computer's home, when another computer reaches it.
Home *HomeState `json:"home,omitempty"`
// Homes are the computers that reach this one, any of which may be the
// home; the picker shows them when there is more than one.
Homes []HomeChoice `json:"homes"`
// Readers are the computers this one links: the home's side.
Readers []MachineState `json:"readers"`
}
Status is GET /v1/brain/sync: how this computer shares its brain.