MCPcopy Create free account
hub / github.com/freecodexyz/free-code / saveConfigWithLock

Function saveConfigWithLock

src/utils/config.ts:1173–1349  ·  view source on GitHub ↗

* Returns true if a write was performed; false if the write was skipped * (no changes, or auth-loss guard tripped). Callers use this to decide * whether to invalidate the cache -- invalidating after a skipped write * destroys the good cached state the auth-loss guard depends on.

(
  file: string,
  createDefault: () => A,
  mergeFn: (current: A) => A,
)

Source from the content-addressed store, hash-verified

1171 * destroys the good cached state the auth-loss guard depends on.
1172 */
1173function saveConfigWithLock<A extends object>(
1174 file: string,
1175 createDefault: () => A,
1176 mergeFn: (current: A) => A,
1177): boolean {
1178 const defaultConfig = createDefault()
1179 const dir = dirname(file)
1180 const fs = getFsImplementation()
1181
1182 // Ensure directory exists (mkdirSync is already recursive in FsOperations)
1183 fs.mkdirSync(dir)
1184
1185 let release
1186 try {
1187 const lockFilePath = `${file}.lock`
1188 const startTime = Date.now()
1189 release = lockfile.lockSync(file, {
1190 lockfilePath: lockFilePath,
1191 onCompromised: err => {
1192 // Default onCompromised throws from a setTimeout callback, which
1193 // becomes an unhandled exception. Log instead -- the lock being
1194 // stolen (e.g. after a 10s event-loop stall) is recoverable.
1195 logForDebugging(`Config lock compromised: ${err}`, { level: 'error' })
1196 },
1197 })
1198 const lockTime = Date.now() - startTime
1199 if (lockTime > 100) {
1200 logForDebugging(
1201 'Lock acquisition took longer than expected - another Claude instance may be running',
1202 )
1203 logEvent('tengu_config_lock_contention', {
1204 lock_time_ms: lockTime,
1205 })
1206 }
1207
1208 // Check for stale write - file changed since we last read it
1209 // Only check for global config file since lastReadFileStats tracks that specific file
1210 if (lastReadFileStats && file === getGlobalClaudeFile()) {
1211 try {
1212 const currentStats = fs.statSync(file)
1213 if (
1214 currentStats.mtimeMs !== lastReadFileStats.mtime ||
1215 currentStats.size !== lastReadFileStats.size
1216 ) {
1217 logEvent('tengu_config_stale_write', {
1218 read_mtime: lastReadFileStats.mtime,
1219 write_mtime: currentStats.mtimeMs,
1220 read_size: lastReadFileStats.size,
1221 write_size: currentStats.size,
1222 })
1223 }
1224 } catch (e) {
1225 const code = getErrnoCode(e)
1226 if (code !== 'ENOENT') {
1227 throw e
1228 }
1229 // File doesn't exist yet, no stale check needed
1230 }

Callers 2

saveGlobalConfigFunction · 0.85
saveCurrentProjectConfigFunction · 0.85

Calls 10

getFsImplementationFunction · 0.85
logForDebuggingFunction · 0.85
logEventFunction · 0.85
getErrnoCodeFunction · 0.85
wouldLoseAuthStateFunction · 0.85
jsonStringifyFunction · 0.85
getConfigBackupDirFunction · 0.85
getConfigFunction · 0.70
releaseFunction · 0.50

Tested by

no test coverage detected