Versioned before code: cmd/gc/session_beads.go at b78058917 . These selected excerpts are abridged for reading; every omission is marked in the source.
How to read this file
Follow the execution boundary
Click any line section to open its explanation. The complete file stays visible, and the selected explanation opens directly beneath the code it describes.
// Abridged from cmd/gc/session_beads.go for reading; not the exact source.
// Long doc comments and //nolint directives are removed, and every omission
// is marked. Identifiers and log strings match the original.
// Source revision: b78058917bc65846db89e1c3b25dc17269822483.
package main
func closeBead(
store beads.Store,
id string,
reason string,
now time.Time,
stderr io.Writer,
) bool {
if stderr == nil {
stderr = io.Discard
}
// Skip the write when the bead is already closed: three reconciler paths
// reach closeBead, and each would otherwise write a different terminal
// state, flapping metadata on a closed bead. This Get result is reused as
// the snapshot below, and a failed Get is non-fatal because the next
// reconciler tick is the idempotent fallback.Lines 1–23
Close only from persisted session facts
- What this section does
- Introduces the versioned source and begins the best-effort close path with a promise that a later reconciler tick can try again.
- Why it matters for Temporal
- Retry belonged to a future controller scan. There was no execution record naming which command failed or when it should resume.
- Best-effort close
- Future scan
- No execution history
snapshot, snapshotErr := store.Get(id)
if snapshotErr == nil && snapshot.Status == "closed" {
return false
}
if reason == string(session.StateFailedCreate) {
return closeFailedCreateBead(
sessionFrontDoor(store),
id,
now,
stderr,
)
}
if setMetaBatch(
sessionFrontDoor(store),
id,
session.ClosePatch(now, reason),
stderr,
) != nil {
return false
}
if err := sessionFrontDoor(store).CloseWithoutReason(id); err != nil {
fmt.Fprintf(stderr, "session beads: closing %s: %v\n", id, err)
return false
}
// Cascade external-messaging cleanup, or a pool respawn leaves zombie
// memberships and the successor never re-binds.
cancelStateAssignedToRetiredSessionBead(store, id, now, stderr)
if snapshotErr == nil {
releaseWorkFromClosedSessionBead(store, snapshot, stderr)
}
return true
}Lines 24–55
Commit metadata, close, then release
- What this section does
- Reads a snapshot, writes close metadata, closes the session record, cleans related state, and only then asks the release helper to find its work.
- Why it matters for Temporal
- Each successful write creates another crash boundary. A later scan sees the writes but not the coordinator's lost call stack.
- Ordered writes
- Crash windows
- Idempotent guard
// Clears the assignee on every non-closed work bead assigned to the given
// session bead and resets in_progress work to open. Best-effort: errors are
// logged but never fail the caller, because releaseOrphanedPoolAssignments on
// the next reconcile tick is the idempotent fallback.
func releaseWorkFromClosedSessionBead(
store beads.Store,
sessionBead beads.Bead,
stderr io.Writer,
) {
if store == nil {
return
}
if stderr == nil {
stderr = io.Discard
}
seenAssignees := make(map[string]struct{}, 3)
addAssignee := func(val string) {
val = strings.TrimSpace(val)
if val == "" {
return
}
seenAssignees[val] = struct{}{}
}
// Any of the bead ID, session_name, configured named identity, alias, or
// alias history may appear as a work bead's assignee.
for _, id := range sessionBeadAssigneeIdentities(sessionBead) {
addAssignee(id)
}Lines 56–85
Reconstruct every identity the session may have used
- What this section does
- Builds a set of bead, session, configured-name, and alias identities that might appear as an assignee.
- Why it matters for Temporal
- Recovery has to infer ownership from several mutable identifiers because no single durable execution owns the handoff.
- Identity reconstruction
- Assignee aliases
- Mutable facts
seenWork := make(map[string]struct{})
wa := workAssignmentForStore(beads.WorkStore{Store: store})
for assignee := range seenAssignees {
for _, status := range []string{"in_progress", "open"} {
work, err := wa.OpenAssignedToBasic(assignee, status)
if err != nil {
fmt.Fprintf(
stderr,
"session beads: listing work assigned to closing session %s (%s): %v\n",
sessionBead.ID,
assignee,
err,
)
continue
}
for _, item := range work {
if session.IsSessionBeadOrRepairable(item) {
continue
}
if _, dup := seenWork[item.ID]; dup {
continue
}
seenWork[item.ID] = struct{}{}Lines 86–109
Search open and in-progress work
- What this section does
- Queries every reconstructed assignee in two statuses, skips session records, and deduplicates work found through overlapping identities.
- Why it matters for Temporal
- The repair cost grows with the number of representations that can describe the same logical operation.
- Multi-query scan
- Deduplication
- Status inference
// The owning session is closing, so the work is fully
// detached. ReleaseWorkBead clears the assignee and stale
// session-affinity metadata and resets in_progress to open.
// The close-release path passes no run_target fallback.
if err := wa.ReleaseWorkBead(item, ""); err != nil {
fmt.Fprintf(
stderr,
"session beads: releasing work %s from closing session %s: %v\n",
item.ID,
sessionBead.ID,
err,
)
}
}
}
}
}Lines 110–127
Reopen work without restoring its route
- What this section does
- Clears the dead assignee and resets in-progress work to open, passing an empty route fallback in this historical revision.
- Why it matters for Temporal
- This exact path could leave completed, pushed work open but undiscoverable. A later production fix had to add another recovery contract.
- Empty route
- Stranded handoff
- Historical failure