forked from sanbuphy/learn-coding-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.ts
More file actions
278 lines (265 loc) · 10.4 KB
/
paths.ts
File metadata and controls
278 lines (265 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import memoize from 'lodash-es/memoize.js'
import { homedir } from 'os'
import { isAbsolute, join, normalize, sep } from 'path'
import {
getIsNonInteractiveSession,
getProjectRoot,
} from '../bootstrap/state.js'
import { getFeatureValue_CACHED_MAY_BE_STALE } from '../services/analytics/growthbook.js'
import {
getClaudeConfigHomeDir,
isEnvDefinedFalsy,
isEnvTruthy,
} from '../utils/envUtils.js'
import { findCanonicalGitRoot } from '../utils/git.js'
import { sanitizePath } from '../utils/path.js'
import {
getInitialSettings,
getSettingsForSource,
} from '../utils/settings/settings.js'
/**
* Whether auto-memory features are enabled (memdir, agent memory, past session search).
* Enabled by default. Priority chain (first defined wins):
* 1. CLAUDE_CODE_DISABLE_AUTO_MEMORY env var (1/true → OFF, 0/false → ON)
* 2. CLAUDE_CODE_SIMPLE (--bare) → OFF
* 3. CCR without persistent storage → OFF (no CLAUDE_CODE_REMOTE_MEMORY_DIR)
* 4. autoMemoryEnabled in settings.json (supports project-level opt-out)
* 5. Default: enabled
*/
export function isAutoMemoryEnabled(): boolean {
const envVal = process.env.CLAUDE_CODE_DISABLE_AUTO_MEMORY
if (isEnvTruthy(envVal)) {
return false
}
if (isEnvDefinedFalsy(envVal)) {
return true
}
// --bare / SIMPLE: prompts.ts already drops the memory section from the
// system prompt via its SIMPLE early-return; this gate stops the other half
// (extractMemories turn-end fork, autoDream, /remember, /dream, team sync).
if (isEnvTruthy(process.env.CLAUDE_CODE_SIMPLE)) {
return false
}
if (
isEnvTruthy(process.env.CLAUDE_CODE_REMOTE) &&
!process.env.CLAUDE_CODE_REMOTE_MEMORY_DIR
) {
return false
}
const settings = getInitialSettings()
if (settings.autoMemoryEnabled !== undefined) {
return settings.autoMemoryEnabled
}
return true
}
/**
* Whether the extract-memories background agent will run this session.
*
* The main agent's prompt always has full save instructions regardless of
* this gate — when the main agent writes memories, the background agent
* skips that range (hasMemoryWritesSince in extractMemories.ts); when it
* doesn't, the background agent catches anything missed.
*
* Callers must also gate on feature('EXTRACT_MEMORIES') — that check cannot
* live inside this helper because feature() only tree-shakes when used
* directly in an `if` condition.
*/
export function isExtractModeActive(): boolean {
if (!getFeatureValue_CACHED_MAY_BE_STALE('tengu_passport_quail', false)) {
return false
}
return (
!getIsNonInteractiveSession() ||
getFeatureValue_CACHED_MAY_BE_STALE('tengu_slate_thimble', false)
)
}
/**
* Returns the base directory for persistent memory storage.
* Resolution order:
* 1. CLAUDE_CODE_REMOTE_MEMORY_DIR env var (explicit override, set in CCR)
* 2. ~/.claude (default config home)
*/
export function getMemoryBaseDir(): string {
if (process.env.CLAUDE_CODE_REMOTE_MEMORY_DIR) {
return process.env.CLAUDE_CODE_REMOTE_MEMORY_DIR
}
return getClaudeConfigHomeDir()
}
const AUTO_MEM_DIRNAME = 'memory'
const AUTO_MEM_ENTRYPOINT_NAME = 'MEMORY.md'
/**
* Normalize and validate a candidate auto-memory directory path.
*
* SECURITY: Rejects paths that would be dangerous as a read-allowlist root
* or that normalize() doesn't fully resolve:
* - relative (!isAbsolute): "../foo" — would be interpreted relative to CWD
* - root/near-root (length < 3): "/" → "" after strip; "/a" too short
* - Windows drive-root (C: regex): "C:\" → "C:" after strip
* - UNC paths (\\server\share): network paths — opaque trust boundary
* - null byte: survives normalize(), can truncate in syscalls
*
* Returns the normalized path with exactly one trailing separator,
* or undefined if the path is unset/empty/rejected.
*/
function validateMemoryPath(
raw: string | undefined,
expandTilde: boolean,
): string | undefined {
if (!raw) {
return undefined
}
let candidate = raw
// Settings.json paths support ~/ expansion (user-friendly). The env var
// override does not (it's set programmatically by Cowork/SDK, which should
// always pass absolute paths). Bare "~", "~/", "~/.", "~/..", etc. are NOT
// expanded — they would make isAutoMemPath() match all of $HOME or its
// parent (same class of danger as "/" or "C:\").
if (
expandTilde &&
(candidate.startsWith('~/') || candidate.startsWith('~\\'))
) {
const rest = candidate.slice(2)
// Reject trivial remainders that would expand to $HOME or an ancestor.
// normalize('') = '.', normalize('.') = '.', normalize('foo/..') = '.',
// normalize('..') = '..', normalize('foo/../..') = '..'
const restNorm = normalize(rest || '.')
if (restNorm === '.' || restNorm === '..') {
return undefined
}
candidate = join(homedir(), rest)
}
// normalize() may preserve a trailing separator; strip before adding
// exactly one to match the trailing-sep contract of getAutoMemPath()
const normalized = normalize(candidate).replace(/[/\\]+$/, '')
if (
!isAbsolute(normalized) ||
normalized.length < 3 ||
/^[A-Za-z]:$/.test(normalized) ||
normalized.startsWith('\\\\') ||
normalized.startsWith('//') ||
normalized.includes('\0')
) {
return undefined
}
return (normalized + sep).normalize('NFC')
}
/**
* Direct override for the full auto-memory directory path via env var.
* When set, getAutoMemPath()/getAutoMemEntrypoint() return this path directly
* instead of computing `{base}/projects/{sanitized-cwd}/memory/`.
*
* Used by Cowork to redirect memory to a space-scoped mount where the
* per-session cwd (which contains the VM process name) would otherwise
* produce a different project-key for every session.
*/
function getAutoMemPathOverride(): string | undefined {
return validateMemoryPath(
process.env.CLAUDE_COWORK_MEMORY_PATH_OVERRIDE,
false,
)
}
/**
* Settings.json override for the full auto-memory directory path.
* Supports ~/ expansion for user convenience.
*
* SECURITY: projectSettings (.claude/settings.json committed to the repo) is
* intentionally excluded — a malicious repo could otherwise set
* autoMemoryDirectory: "~/.ssh" and gain silent write access to sensitive
* directories via the filesystem.ts write carve-out (which fires when
* isAutoMemPath() matches and hasAutoMemPathOverride() is false). This follows
* the same pattern as hasSkipDangerousModePermissionPrompt() etc.
*/
function getAutoMemPathSetting(): string | undefined {
const dir =
getSettingsForSource('policySettings')?.autoMemoryDirectory ??
getSettingsForSource('flagSettings')?.autoMemoryDirectory ??
getSettingsForSource('localSettings')?.autoMemoryDirectory ??
getSettingsForSource('userSettings')?.autoMemoryDirectory
return validateMemoryPath(dir, true)
}
/**
* Check if CLAUDE_COWORK_MEMORY_PATH_OVERRIDE is set to a valid override.
* Use this as a signal that the SDK caller has explicitly opted into
* the auto-memory mechanics — e.g. to decide whether to inject the
* memory prompt when a custom system prompt replaces the default.
*/
export function hasAutoMemPathOverride(): boolean {
return getAutoMemPathOverride() !== undefined
}
/**
* Returns the canonical git repo root if available, otherwise falls back to
* the stable project root. Uses findCanonicalGitRoot so all worktrees of the
* same repo share one auto-memory directory (anthropics/claude-code#24382).
*/
function getAutoMemBase(): string {
return findCanonicalGitRoot(getProjectRoot()) ?? getProjectRoot()
}
/**
* Returns the auto-memory directory path.
*
* Resolution order:
* 1. CLAUDE_COWORK_MEMORY_PATH_OVERRIDE env var (full-path override, used by Cowork)
* 2. autoMemoryDirectory in settings.json (trusted sources only: policy/local/user)
* 3. <memoryBase>/projects/<sanitized-git-root>/memory/
* where memoryBase is resolved by getMemoryBaseDir()
*
* Memoized: render-path callers (collapseReadSearchGroups → isAutoManagedMemoryFile)
* fire per tool-use message per Messages re-render; each miss costs
* getSettingsForSource × 4 → parseSettingsFile (realpathSync + readFileSync).
* Keyed on projectRoot so tests that change its mock mid-block recompute;
* env vars / settings.json / CLAUDE_CONFIG_DIR are session-stable in
* production and covered by per-test cache.clear.
*/
export const getAutoMemPath = memoize(
(): string => {
const override = getAutoMemPathOverride() ?? getAutoMemPathSetting()
if (override) {
return override
}
const projectsDir = join(getMemoryBaseDir(), 'projects')
return (
join(projectsDir, sanitizePath(getAutoMemBase()), AUTO_MEM_DIRNAME) + sep
).normalize('NFC')
},
() => getProjectRoot(),
)
/**
* Returns the daily log file path for the given date (defaults to today).
* Shape: <autoMemPath>/logs/YYYY/MM/YYYY-MM-DD.md
*
* Used by assistant mode (feature('KAIROS')): rather than maintaining
* MEMORY.md as a live index, the agent appends to a date-named log file
* as it works. A separate nightly /dream skill distills these logs into
* topic files + MEMORY.md.
*/
export function getAutoMemDailyLogPath(date: Date = new Date()): string {
const yyyy = date.getFullYear().toString()
const mm = (date.getMonth() + 1).toString().padStart(2, '0')
const dd = date.getDate().toString().padStart(2, '0')
return join(getAutoMemPath(), 'logs', yyyy, mm, `${yyyy}-${mm}-${dd}.md`)
}
/**
* Returns the auto-memory entrypoint (MEMORY.md inside the auto-memory dir).
* Follows the same resolution order as getAutoMemPath().
*/
export function getAutoMemEntrypoint(): string {
return join(getAutoMemPath(), AUTO_MEM_ENTRYPOINT_NAME)
}
/**
* Check if an absolute path is within the auto-memory directory.
*
* When CLAUDE_COWORK_MEMORY_PATH_OVERRIDE is set, this matches against the
* env-var override directory. Note that a true return here does NOT imply
* write permission in that case — the filesystem.ts write carve-out is gated
* on !hasAutoMemPathOverride() (it exists to bypass DANGEROUS_DIRECTORIES).
*
* The settings.json autoMemoryDirectory DOES get the write carve-out: it's the
* user's explicit choice from a trusted settings source (projectSettings is
* excluded — see getAutoMemPathSetting), and hasAutoMemPathOverride() remains
* false for it.
*/
export function isAutoMemPath(absolutePath: string): boolean {
// SECURITY: Normalize to prevent path traversal bypasses via .. segments
const normalizedPath = normalize(absolutePath)
return normalizedPath.startsWith(getAutoMemPath())
}