MCPcopy
hub / github.com/codeaashu/claude-code / registerSession

Function registerSession

src/utils/concurrentSessions.ts:59–109  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

57 * Errors logged to debug, never thrown.
58 */
59export async function registerSession(): Promise<boolean> {
60 if (getAgentId() != null) return false
61
62 const kind: SessionKind = envSessionKind() ?? 'interactive'
63 const dir = getSessionsDir()
64 const pidFile = join(dir, `${process.pid}.json`)
65
66 registerCleanup(async () => {
67 try {
68 await unlink(pidFile)
69 } catch {
70 // ENOENT is fine (already deleted or never written)
71 }
72 })
73
74 try {
75 await mkdir(dir, { recursive: true, mode: 0o700 })
76 await chmod(dir, 0o700)
77 await writeFile(
78 pidFile,
79 jsonStringify({
80 pid: process.pid,
81 sessionId: getSessionId(),
82 cwd: getOriginalCwd(),
83 startedAt: Date.now(),
84 kind,
85 entrypoint: process.env.CLAUDE_CODE_ENTRYPOINT,
86 ...(feature('UDS_INBOX')
87 ? { messagingSocketPath: process.env.CLAUDE_CODE_MESSAGING_SOCKET }
88 : {}),
89 ...(feature('BG_SESSIONS')
90 ? {
91 name: process.env.CLAUDE_CODE_SESSION_NAME,
92 logPath: process.env.CLAUDE_CODE_SESSION_LOG,
93 agent: process.env.CLAUDE_CODE_AGENT,
94 }
95 : {}),
96 }),
97 )
98 // --resume / /resume mutates getSessionId() via switchSession. Without
99 // this, the PID file's sessionId goes stale and `claude ps` sparkline
100 // reads the wrong transcript.
101 onSessionSwitch(id => {
102 void updatePidFile({ sessionId: id })
103 })
104 return true
105 } catch (e) {
106 logForDebugging(`[concurrentSessions] register failed: ${errorMessage(e)}`)
107 return false
108 }
109}
110
111/**
112 * Update this session's name in its PID registry file so ListPeers

Callers 1

runFunction · 0.85

Calls 13

getAgentIdFunction · 0.85
envSessionKindFunction · 0.85
getSessionsDirFunction · 0.85
registerCleanupFunction · 0.85
unlinkFunction · 0.85
mkdirFunction · 0.85
jsonStringifyFunction · 0.85
getSessionIdFunction · 0.85
getOriginalCwdFunction · 0.85
featureFunction · 0.85
updatePidFileFunction · 0.85
logForDebuggingFunction · 0.85

Tested by

no test coverage detected