Class: Agent::Lock::Identity
- Inherits:
-
Object
- Object
- Agent::Lock::Identity
- Defined in:
- lib/agent/lock/identity.rb
Overview
Who is asking, and whether they are still around.
A lock is worthless unless the holder gives the same answer every time it is asked. The obvious answer, this process's own pid, is the wrong one: an agent harness runs each command in a shell of its own, so a lock acquired by one invocation could never be released by the next. The identity has to belong to the session, not to the process that happens to be speaking for it right now.
In order of preference:
AGENT_ID what a human or a harness set on purpose
session name what a library caller named its session (session_name:)
CLAUDE_SESSION_ID the session, which survives --resume
fingerprint the first ancestor process that is not a shell
The fingerprint is the fallback that needs explaining. Walking up from
this process, the shells are throwaway and the thing above them is not:
the claude or codex process driving the session, or the terminal a
human is typing in. Its pid and start time, hashed, are stable for as
long as that session lives and different for anybody else's.
Constant Summary collapse
- SHELLS =
Process names skipped while walking up to the session's own process: every agent command runs in a fresh shell, so a shell is never the session itself.
%w[sh bash zsh ksh csh tcsh fish dash].freeze
- MAX_HOPS =
How many ancestors the walk climbs before giving up, so a process tree of nothing but shells cannot loop it forever.
10
Class Method Summary collapse
-
.current ⇒ Identity
Deliberately not memoized.
-
.session_pid ⇒ Integer
The ancestry walk, on the other hand, cannot change while this process lives, and it costs two
pscalls per hop.
Instance Method Summary collapse
-
#evidence ⇒ Hash{Symbol => Object}
Evidence, not identity: enough to ask later whether the holder is still running.
-
#id ⇒ String
The holder name written into a lock.
-
#initialize(env: ENV, liveness_pid: nil, session_name: nil) ⇒ Identity
constructor
A new instance of Identity.
-
#parent_id ⇒ String?
The session that spawned this one: AGENT_PARENT_ID when a harness says so, and otherwise inferred.
-
#parent_source ⇒ Symbol?
Where #parent_id came from.
-
#source ⇒ Symbol
Where #id came from, so a session can check what it is being taken for before a lock is written under the wrong name.
Constructor Details
#initialize(env: ENV, liveness_pid: nil, session_name: nil) ⇒ Identity
Returns a new instance of Identity.
70 71 72 73 74 |
# File 'lib/agent/lock/identity.rb', line 70 def initialize(env: ENV, liveness_pid: nil, session_name: nil) @env = env @liveness_pid = liveness_pid @session_name = session_name end |
Class Method Details
.current ⇒ Identity
Deliberately not memoized. A forked CLI computes this once and dies, but the same code runs inside a long-lived process during the specs and inside any harness that loads the library, where a cached answer would outlive the environment it was computed from.
46 |
# File 'lib/agent/lock/identity.rb', line 46 def current = new |
.session_pid ⇒ Integer
The ancestry walk, on the other hand, cannot change while this
process lives, and it costs two ps calls per hop.
52 |
# File 'lib/agent/lock/identity.rb', line 52 def session_pid = @session_pid ||= yield |
Instance Method Details
#evidence ⇒ Hash{Symbol => Object}
Evidence, not identity: enough to ask later whether the holder is still running. The pid alone is not enough, since pids are reused, and the host matters because a lock store can be on a shared or synced volume.
136 137 138 139 |
# File 'lib/agent/lock/identity.rb', line 136 def evidence pid = @liveness_pid || (session_pid unless @session_name) { pid: pid, started_at: ProcessInfo.started_at(pid), host: Socket.gethostname } end |
#id ⇒ String
Returns the holder name written into a lock.
77 78 79 |
# File 'lib/agent/lock/identity.rb', line 77 def id @id ||= explicit || session || fingerprint end |
#parent_id ⇒ String?
The session that spawned this one: AGENT_PARENT_ID when a harness says so, and otherwise inferred.
The inference exists because Claude Code runs a sub-agent inside its parent's own process and sets nothing to tell them apart, so without it every sub-agent resolved to its parent's fingerprint and none of them could ever block another. A sub-agent's one distinguishing mark is the AGENT_ID it was told to use. When that differs from what this session would answer to without it, the session is, by elimination, the parent.
A holder can also say it has none: AGENT_PARENT_ID set, and empty. A top-level service that names itself is otherwise taken for a child of whatever terminal or agent started it, and a lock that session holds would not block it.
98 99 100 101 102 |
# File 'lib/agent/lock/identity.rb', line 98 def parent_id return @parent_id if defined?(@parent_id) @parent_id = parentless? ? nil : declared_parent || inferred_parent end |
#parent_source ⇒ Symbol?
Where #parent_id came from. An inferred parent is a guess a human may want to overrule with AGENT_PARENT_ID, so it is reported as one.
A parent declared absent is told apart from one there was nothing to infer: an AGENT_PARENT_ID exported empty by accident looks the same in a lock record, and this is where it can be seen.
124 125 126 127 128 129 |
# File 'lib/agent/lock/identity.rb', line 124 def parent_source return :none if parentless? return :explicit if declared_parent :inferred if parent_id end |
#source ⇒ Symbol
Where #id came from, so a session can check what it is being taken for before a lock is written under the wrong name.
108 109 110 111 112 113 |
# File 'lib/agent/lock/identity.rb', line 108 def source return :explicit if explicit return :session if session :fingerprint end |