MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / writeArmStateFile

Function writeArmStateFile

internal/cli/session_arm_state.go:216–286  ·  view source on GitHub ↗

writeArmStateFile writes (or idempotently overwrites) this session's arm-state file, mode 0600, with the given disarmed value, and returns the path. It refuses only when there is no key to write under at all — no messaging socket AND no working directory — because a file with neither could never be

(disarmed bool)

Source from the content-addressed store, hash-verified

214// no messaging socket AND no working directory — because a file with
215// neither could never be matched to a session or a repo by a reader.
216func writeArmStateFile(disarmed bool) (path string, err error) {
217 socket, cwd := currentSessionArmKey()
218 if socket == "" && cwd == "" {
219 return "", fmt.Errorf("cannot record arm state: no messaging socket and no working directory to key the session on")
220 }
221 p, err := armStatePath(socket, cwd)
222 if err != nil {
223 return "", err
224 }
225 // Carry the first-connect flag forward across arm/disarm rewrites so
226 // toggling consent within a session doesn't offer the session-start
227 // playbook again. Only a LIVE prior file counts — a dead-owner one is a
228 // stale session and its first-connect state is irrelevant.
229 booted := false
230 if prev, _, rerr := readArmState(); rerr == nil && prev != nil && armStateOwnerAlive(prev) {
231 booted = prev.Booted
232 }
233 st := ArmState{
234 Armed: !disarmed,
235 Disarmed: disarmed,
236 Booted: booted,
237 PID: os.Getpid(),
238 Socket: socket,
239 Cwd: cwd,
240 StartedAt: time.Now().UTC().Format(time.RFC3339),
241 }
242 if !disarmed {
243 var generation [32]byte
244 if _, err := rand.Read(generation[:]); err != nil {
245 return "", fmt.Errorf("generate arm generation: %w", err)
246 }
247 st.Generation = hex.EncodeToString(generation[:])
248 }
249 if socket != "" {
250 // Bind the file to this specific socket instance — its mtime plus,
251 // where available, its inode+device — not just the path, so a
252 // reused path (or a lingering stale node) can't revive it.
253 if info, statErr := os.Stat(socket); statErr == nil {
254 st.SocketMtimeUnixNano = info.ModTime().UnixNano()
255 if ino, dev, ok := statIdentity(info); ok {
256 st.SocketIno, st.SocketDev = ino, dev
257 st.MountNamespace = namespaceStamp(mountNamespaceFn)
258 }
259 }
260 } else {
261 // Headless: the owner is the session process, not this command
262 // (BUG-3227). os.Getpid() here is the exiting `pad session arm`,
263 // which read as dead on the very next read, so every headless arm
264 // and disarm was reaped on arrival. CaptureSessionOwner resolves
265 // the same owner the session registry records, with its start
266 // token for the pid-reuse defence.
267 owner, oerr := CaptureSessionOwner()
268 if oerr != nil {
269 return "", fmt.Errorf("cannot record arm state: %w", oerr)
270 }
271 st.PID, st.ProcStart, st.PIDSource, st.PIDNamespace = owner.PID, owner.ProcStart, owner.PIDSource, owner.PIDNamespace
272 }
273 data, err := json.MarshalIndent(st, "", " ")

Callers 2

WriteArmStateFunction · 0.85
WriteDisarmStateFunction · 0.85

Calls 12

currentSessionArmKeyFunction · 0.85
armStatePathFunction · 0.85
readArmStateFunction · 0.85
armStateOwnerAliveFunction · 0.85
namespaceStampFunction · 0.85
CaptureSessionOwnerFunction · 0.85
atomicWriteFileFunction · 0.85
FormatMethod · 0.80
statIdentityFunction · 0.70
NowMethod · 0.65
StatMethod · 0.65
ReadMethod · 0.45

Tested by

no test coverage detected