Class: Agent::Lock::Identity

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV, liveness_pid: nil, session_name: nil) ⇒ Identity

Returns a new instance of Identity.

Parameters:

  • env (Hash) (defaults to: ENV)
  • liveness_pid (Integer, nil) (defaults to: nil) —

    the process a lock lives and dies with. nil is the session's own process, found by walking up from the parent past the shells, which is right for the CLI, where the parent is the agent's throwaway shell. An in-process caller passes its own pid: walking up from a library host finds the host's parent, which outlives a crashed host and keeps its locks from ever being reaped. It is evidence only. The name a lock is signed with, and the parent inferred from it, still come from the session, so a script an agent runs is the same holder as that agent's alock.

  • session_name (String, nil) (defaults to: nil) —

    the session's name, used as given in place of CLAUDE_SESSION_ID and the fingerprint: a plan id, a job id. A session known by name alone is tied to no process, so its evidence carries no pid and its locks are trusted for the stale window from their last write: see Record#expired?



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.

Returns:



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.

Returns:

  • (Integer)


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.

Returns:

  • (Hash{Symbol => Object})


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.

Returns:

  • (String) —

    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.

Returns:

  • (String, nil)


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.

Returns:

  • (Symbol, nil) —

    :explicit, :inferred, :none when the holder said it has no parent, or nil when there is none to infer



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.

Returns:

  • (Symbol) —

    :explicit, :session or :fingerprint



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