forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
1256 lines (1166 loc) · 43.1 KB
/
Copy pathindex.ts
File metadata and controls
1256 lines (1166 loc) · 43.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Team Memory Sync Service
*
* Syncs team memory files between the local filesystem and the server API.
* Team memory is scoped per-repo (identified by git remote hash) and shared
* across all authenticated org members.
*
* API contract (anthropic/anthropic#250711 + #283027):
* GET /api/claude_code/team_memory?repo={owner/repo} → TeamMemoryData (includes entryChecksums)
* GET /api/claude_code/team_memory?repo={owner/repo}&view=hashes → metadata + entryChecksums only (no entry bodies)
* PUT /api/claude_code/team_memory?repo={owner/repo} → upload entries (upsert semantics)
* 404 = no data exists yet
*
* Sync semantics:
* - Pull overwrites local files with server content (server wins per-key).
* - Push uploads only keys whose content hash differs from serverChecksums
* (delta upload). Server uses upsert: keys not in the PUT are preserved.
* - File deletions do NOT propagate: deleting a local file won't remove it
* from the server, and the next pull will restore it locally.
*
* State management:
* All mutable state (ETag tracking, watcher suppression) lives in a
* SyncState object created by the caller and threaded through every call.
* This avoids module-level mutable state and gives tests natural isolation.
*/
import axios from 'axios'
import { createHash } from 'crypto'
import { mkdir, readdir, readFile, stat, writeFile } from 'fs/promises'
import { join, relative, sep } from 'path'
import {
CLAUDE_AI_INFERENCE_SCOPE,
CLAUDE_AI_PROFILE_SCOPE,
getOauthConfig,
OAUTH_BETA_HEADER,
} from '../../constants/oauth.js'
import {
getTeamMemPath,
PathTraversalError,
validateTeamMemKey,
} from '../../memdir/teamMemPaths.js'
import { count } from '../../utils/array.js'
import {
checkAndRefreshOAuthTokenIfNeeded,
getClaudeAIOAuthTokens,
} from '../../utils/auth.js'
import { logForDebugging } from '../../utils/debug.js'
import { classifyAxiosError } from '../../utils/errors.js'
import { getGithubRepo } from '../../utils/git.js'
import {
getAPIProvider,
isFirstPartyAnthropicBaseUrl,
} from '../../utils/model/providers.js'
import { sleep } from '../../utils/sleep.js'
import { jsonStringify } from '../../utils/slowOperations.js'
import { getClaudeCodeUserAgent } from '../../utils/userAgent.js'
import { logEvent } from '../analytics/index.js'
import type { AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS } from '../analytics/metadata.js'
import { getRetryDelay } from '../api/withRetry.js'
import { scanForSecrets } from './secretScanner.js'
import {
type SkippedSecretFile,
TeamMemoryDataSchema,
type TeamMemoryHashesResult,
type TeamMemorySyncFetchResult,
type TeamMemorySyncPushResult,
type TeamMemorySyncUploadResult,
TeamMemoryTooManyEntriesSchema,
} from './types.js'
const TEAM_MEMORY_SYNC_TIMEOUT_MS = 30_000
// Per-entry size cap — server default from anthropic/anthropic#293258.
// Pre-filtering oversized entries saves bandwidth: the structured 413 for
// this case doesn't give us anything to learn (one file is just too big).
const MAX_FILE_SIZE_BYTES = 250_000
// No client-side DEFAULT_MAX_ENTRIES: the server's entry-count cap is
// GB-tunable per-org (claude_code_team_memory_limits), so any compile-time
// constant here will drift. We only truncate after learning the effective
// limit from a structured 413's extra_details.max_entries.
// Gateway body-size cap. The API gateway rejects PUT bodies over ~256-512KB
// with an unstructured (HTML) 413 before the request reaches the app server —
// distinguishable from the app's structured entry-count 413 only by latency
// (~750ms gateway vs ~2.3s app on comparable payloads). #21969 removed the
// client entry-count cap; cold pushes from heavy users then sent 300KB-1.4MB
// bodies and hit this. 200KB leaves headroom under the observed threshold
// and keeps a single-entry-at-MAX_FILE_SIZE_BYTES solo batch (~250KB) just
// under the real gateway limit. Batches larger than this are split into
// sequential PUTs — server upsert-merge semantics make that safe.
const MAX_PUT_BODY_BYTES = 200_000
const MAX_RETRIES = 3
const MAX_CONFLICT_RETRIES = 2
// ─── Sync state ─────────────────────────────────────────────
/**
* Mutable state for the team memory sync service.
* Created once per session by the watcher and passed to all sync functions.
* Tests create a fresh instance per test for isolation.
*/
export type SyncState = {
/** Last known server checksum (ETag) for conditional requests. */
lastKnownChecksum: string | null
/**
* Per-key content hash (`sha256:<hex>`) of what we believe the server
* currently holds. Populated from server-provided entryChecksums on pull
* and from local hashes on successful push. Used to compute the delta on
* push — only keys whose local hash differs are uploaded.
*/
serverChecksums: Map<string, string>
/**
* Server-enforced max_entries cap, learned from a structured 413 response
* (anthropic/anthropic#293258 adds error_code + extra_details.max_entries).
* Stays null until a 413 is observed — the server's cap is GB-tunable
* per-org so there is no correct client-side default. While null,
* readLocalTeamMemory sends everything and lets the server be
* authoritative (it rejects atomically).
*/
serverMaxEntries: number | null
}
export function createSyncState(): SyncState {
return {
lastKnownChecksum: null,
serverChecksums: new Map(),
serverMaxEntries: null,
}
}
/**
* Compute `sha256:<hex>` over the UTF-8 bytes of the given content.
* Format matches the server's entryChecksums values (anthropic/anthropic#283027)
* so local-vs-server comparison works by direct string equality.
*/
export function hashContent(content: string): string {
return 'sha256:' + createHash('sha256').update(content, 'utf8').digest('hex')
}
/**
* Type guard narrowing an unknown error to a Node.js errno-style exception.
* Uses `in` narrowing so no `as` cast is needed at call sites.
*/
function isErrnoException(e: unknown): e is NodeJS.ErrnoException {
return e instanceof Error && 'code' in e && typeof e.code === 'string'
}
// ─── Auth & endpoint ─────────────────────────────────────────
/**
* Check if user is authenticated with first-party OAuth (required for team memory sync).
*/
function isUsingOAuth(): boolean {
if (getAPIProvider() !== 'firstParty' || !isFirstPartyAnthropicBaseUrl()) {
return false
}
const tokens = getClaudeAIOAuthTokens()
return Boolean(
tokens?.accessToken &&
tokens.scopes?.includes(CLAUDE_AI_INFERENCE_SCOPE) &&
tokens.scopes.includes(CLAUDE_AI_PROFILE_SCOPE),
)
}
function getTeamMemorySyncEndpoint(repoSlug: string): string {
const baseUrl =
process.env.TEAM_MEMORY_SYNC_URL || getOauthConfig().BASE_API_URL
return `${baseUrl}/api/claude_code/team_memory?repo=${encodeURIComponent(repoSlug)}`
}
function getAuthHeaders(): {
headers?: Record<string, string>
error?: string
} {
const oauthTokens = getClaudeAIOAuthTokens()
if (oauthTokens?.accessToken) {
return {
headers: {
Authorization: `Bearer ${oauthTokens.accessToken}`,
'anthropic-beta': OAUTH_BETA_HEADER,
'User-Agent': getClaudeCodeUserAgent(),
},
}
}
return { error: 'No OAuth token available for team memory sync' }
}
// ─── Fetch (pull) ────────────────────────────────────────────
async function fetchTeamMemoryOnce(
state: SyncState,
repoSlug: string,
etag?: string | null,
): Promise<TeamMemorySyncFetchResult> {
try {
await checkAndRefreshOAuthTokenIfNeeded()
const auth = getAuthHeaders()
if (auth.error) {
return {
success: false,
error: auth.error,
skipRetry: true,
errorType: 'auth',
}
}
const headers: Record<string, string> = { ...auth.headers }
if (etag) {
headers['If-None-Match'] = `"${etag.replace(/"/g, '')}"`
}
const endpoint = getTeamMemorySyncEndpoint(repoSlug)
const response = await axios.get(endpoint, {
headers,
timeout: TEAM_MEMORY_SYNC_TIMEOUT_MS,
validateStatus: status =>
status === 200 || status === 304 || status === 404,
})
if (response.status === 304) {
logForDebugging('team-memory-sync: not modified (304)', {
level: 'debug',
})
return { success: true, notModified: true, checksum: etag ?? undefined }
}
if (response.status === 404) {
logForDebugging('team-memory-sync: no remote data (404)', {
level: 'debug',
})
state.lastKnownChecksum = null
return { success: true, isEmpty: true }
}
const parsed = TeamMemoryDataSchema().safeParse(response.data)
if (!parsed.success) {
logForDebugging('team-memory-sync: invalid response format', {
level: 'warn',
})
return {
success: false,
error: 'Invalid team memory response format',
skipRetry: true,
errorType: 'parse',
}
}
// Extract checksum from response data or ETag header
const responseChecksum =
parsed.data.checksum ||
response.headers['etag']?.replace(/^"|"$/g, '') ||
undefined
if (responseChecksum) {
state.lastKnownChecksum = responseChecksum
}
logForDebugging(
`team-memory-sync: fetched successfully (checksum: ${responseChecksum ?? 'none'})`,
{ level: 'debug' },
)
return {
success: true,
data: parsed.data,
isEmpty: false,
checksum: responseChecksum,
}
} catch (error) {
const { kind, status, message } = classifyAxiosError(error)
const body = axios.isAxiosError(error)
? JSON.stringify(error.response?.data ?? '')
: ''
if (kind !== 'other') {
logForDebugging(`team-memory-sync: fetch error ${status}: ${body}`, {
level: 'warn',
})
}
switch (kind) {
case 'auth':
return {
success: false,
error: `Not authorized for team memory sync: ${body}`,
skipRetry: true,
errorType: 'auth',
httpStatus: status,
}
case 'timeout':
return {
success: false,
error: 'Team memory sync request timeout',
errorType: 'timeout',
}
case 'network':
return {
success: false,
error: 'Cannot connect to server',
errorType: 'network',
}
default:
return {
success: false,
error: message,
errorType: 'unknown',
httpStatus: status,
}
}
}
}
/**
* Fetch only per-key checksums + metadata (no entry bodies).
* Used for cheap serverChecksums refresh during 412 conflict resolution — avoids
* downloading ~300KB of content just to learn which keys changed.
* Requires anthropic/anthropic#283027 deployed; on failure the caller fails the
* push and the watcher retries on the next edit.
*/
async function fetchTeamMemoryHashes(
state: SyncState,
repoSlug: string,
): Promise<TeamMemoryHashesResult> {
try {
await checkAndRefreshOAuthTokenIfNeeded()
const auth = getAuthHeaders()
if (auth.error) {
return { success: false, error: auth.error, errorType: 'auth' }
}
const endpoint = getTeamMemorySyncEndpoint(repoSlug) + '&view=hashes'
const response = await axios.get(endpoint, {
headers: auth.headers,
timeout: TEAM_MEMORY_SYNC_TIMEOUT_MS,
validateStatus: status => status === 200 || status === 404,
})
if (response.status === 404) {
state.lastKnownChecksum = null
return { success: true, entryChecksums: {} }
}
const checksum =
response.data?.checksum || response.headers['etag']?.replace(/^"|"$/g, '')
const entryChecksums = response.data?.entryChecksums
// Requires anthropic/anthropic#283027. If entryChecksums is missing,
// treat as a probe failure — caller fails the push; watcher retries.
if (!entryChecksums || typeof entryChecksums !== 'object') {
return {
success: false,
error:
'Server did not return entryChecksums (?view=hashes unsupported)',
errorType: 'parse',
}
}
if (checksum) {
state.lastKnownChecksum = checksum
}
return {
success: true,
version: response.data?.version,
checksum,
entryChecksums,
}
} catch (error) {
const { kind, status, message } = classifyAxiosError(error)
switch (kind) {
case 'auth':
return {
success: false,
error: 'Not authorized',
errorType: 'auth',
httpStatus: status,
}
case 'timeout':
return { success: false, error: 'Timeout', errorType: 'timeout' }
case 'network':
return { success: false, error: 'Network error', errorType: 'network' }
default:
return {
success: false,
error: message,
errorType: 'unknown',
httpStatus: status,
}
}
}
}
async function fetchTeamMemory(
state: SyncState,
repoSlug: string,
etag?: string | null,
): Promise<TeamMemorySyncFetchResult> {
let lastResult: TeamMemorySyncFetchResult | null = null
for (let attempt = 1; attempt <= MAX_RETRIES + 1; attempt++) {
lastResult = await fetchTeamMemoryOnce(state, repoSlug, etag)
if (lastResult.success || lastResult.skipRetry) {
return lastResult
}
if (attempt > MAX_RETRIES) {
return lastResult
}
const delayMs = getRetryDelay(attempt)
logForDebugging(`team-memory-sync: retry ${attempt}/${MAX_RETRIES}`, {
level: 'debug',
})
await sleep(delayMs)
}
return lastResult!
}
// ─── Upload (push) ───────────────────────────────────────────
/**
* Split a delta into PUT-sized batches under MAX_PUT_BODY_BYTES each.
*
* Greedy bin-packing over sorted keys — sorting gives deterministic batches
* across calls, which matters for ETag stability if the conflict loop retries
* after a partial commit. The byte count is the full serialized body
* including JSON overhead, so what we measure is what axios sends.
*
* A single entry exceeding MAX_PUT_BODY_BYTES goes into its own solo batch
* (MAX_FILE_SIZE_BYTES=250K already caps individual files; a ~250K solo body
* is above our soft cap but below the gateway's observed real threshold).
*/
export function batchDeltaByBytes(
delta: Record<string, string>,
): Array<Record<string, string>> {
const keys = Object.keys(delta).sort()
if (keys.length === 0) return []
// Fixed overhead for `{"entries":{}}` — each entry then adds its marginal
// bytes. jsonStringify (≡ JSON.stringify under the hood) on the raw
// strings handles escaping so the count matches what axios serializes.
const EMPTY_BODY_BYTES = Buffer.byteLength('{"entries":{}}', 'utf8')
const entryBytes = (k: string, v: string): number =>
Buffer.byteLength(jsonStringify(k), 'utf8') +
Buffer.byteLength(jsonStringify(v), 'utf8') +
2 // colon + comma (comma over-counts by 1 on the last entry; harmless slack)
const batches: Array<Record<string, string>> = []
let current: Record<string, string> = {}
let currentBytes = EMPTY_BODY_BYTES
for (const key of keys) {
const added = entryBytes(key, delta[key]!)
if (
currentBytes + added > MAX_PUT_BODY_BYTES &&
Object.keys(current).length > 0
) {
batches.push(current)
current = {}
currentBytes = EMPTY_BODY_BYTES
}
current[key] = delta[key]!
currentBytes += added
}
batches.push(current)
return batches
}
async function uploadTeamMemory(
state: SyncState,
repoSlug: string,
entries: Record<string, string>,
ifMatchChecksum?: string | null,
): Promise<TeamMemorySyncUploadResult> {
try {
await checkAndRefreshOAuthTokenIfNeeded()
const auth = getAuthHeaders()
if (auth.error) {
return { success: false, error: auth.error, errorType: 'auth' }
}
const headers: Record<string, string> = {
...auth.headers,
'Content-Type': 'application/json',
}
if (ifMatchChecksum) {
headers['If-Match'] = `"${ifMatchChecksum.replace(/"/g, '')}"`
}
const endpoint = getTeamMemorySyncEndpoint(repoSlug)
const response = await axios.put(
endpoint,
{ entries },
{
headers,
timeout: TEAM_MEMORY_SYNC_TIMEOUT_MS,
validateStatus: status => status === 200 || status === 412,
},
)
if (response.status === 412) {
logForDebugging('team-memory-sync: conflict (412 Precondition Failed)', {
level: 'info',
})
return { success: false, conflict: true, error: 'ETag mismatch' }
}
const responseChecksum = response.data?.checksum
if (responseChecksum) {
state.lastKnownChecksum = responseChecksum
}
logForDebugging(
`team-memory-sync: uploaded ${Object.keys(entries).length} entries (checksum: ${responseChecksum ?? 'none'})`,
{ level: 'debug' },
)
return {
success: true,
checksum: responseChecksum,
lastModified: response.data?.lastModified,
}
} catch (error) {
const body = axios.isAxiosError(error)
? JSON.stringify(error.response?.data ?? '')
: ''
logForDebugging(
`team-memory-sync: upload failed: ${error instanceof Error ? error.message : ''} ${body}`,
{ level: 'warn' },
)
const { kind, status: httpStatus, message } = classifyAxiosError(error)
const errorType = kind === 'http' || kind === 'other' ? 'unknown' : kind
let serverErrorCode: 'team_memory_too_many_entries' | undefined
let serverMaxEntries: number | undefined
let serverReceivedEntries: number | undefined
// Parse structured 413 (anthropic/anthropic#293258). The server's
// RequestTooLargeException includes error_code + extra_details with
// the effective max_entries (may be GB-tuned per-org). Cache it so
// the next push trims to the right value.
if (httpStatus === 413 && axios.isAxiosError(error)) {
const parsed = TeamMemoryTooManyEntriesSchema().safeParse(
error.response?.data,
)
if (parsed.success) {
serverErrorCode = parsed.data.error.details.error_code
serverMaxEntries = parsed.data.error.details.max_entries
serverReceivedEntries = parsed.data.error.details.received_entries
}
}
return {
success: false,
error: message,
errorType,
httpStatus,
...(serverErrorCode !== undefined && { serverErrorCode }),
...(serverMaxEntries !== undefined && { serverMaxEntries }),
...(serverReceivedEntries !== undefined && { serverReceivedEntries }),
}
}
}
// ─── Local file operations ───────────────────────────────────
/**
* Read all team memory files from the local directory into a flat key-value map.
* Keys are relative paths from the team memory directory.
* Empty files are included (content will be empty string).
*
* PSR M22174: Each file is scanned for credentials before inclusion
* using patterns from gitleaks. Files containing secrets are SKIPPED
* (not uploaded) and collected in skippedSecrets so the caller can
* warn the user.
*/
async function readLocalTeamMemory(maxEntries: number | null): Promise<{
entries: Record<string, string>
skippedSecrets: SkippedSecretFile[]
}> {
const teamDir = getTeamMemPath()
const entries: Record<string, string> = {}
const skippedSecrets: SkippedSecretFile[] = []
async function walkDir(dir: string): Promise<void> {
try {
const dirEntries = await readdir(dir, { withFileTypes: true })
await Promise.all(
dirEntries.map(async entry => {
const fullPath = join(dir, entry.name)
if (entry.isDirectory()) {
await walkDir(fullPath)
} else if (entry.isFile()) {
try {
const stats = await stat(fullPath)
if (stats.size > MAX_FILE_SIZE_BYTES) {
logForDebugging(
`team-memory-sync: skipping oversized file ${entry.name} (${stats.size} > ${MAX_FILE_SIZE_BYTES} bytes)`,
{ level: 'info' },
)
return
}
const content = await readFile(fullPath, 'utf8')
const relPath = relative(teamDir, fullPath).replaceAll('\\', '/')
// PSR M22174: scan for secrets BEFORE adding to the upload
// payload. If a secret is detected, skip this file entirely
// so it never leaves the machine.
const secretMatches = scanForSecrets(content)
if (secretMatches.length > 0) {
// Report only the first match per file — one secret is
// enough to skip the file and we don't want to log more
// than necessary about credential locations.
const firstMatch = secretMatches[0]!
skippedSecrets.push({
path: relPath,
ruleId: firstMatch.ruleId,
label: firstMatch.label,
})
logForDebugging(
`team-memory-sync: skipping "${relPath}" — detected ${firstMatch.label}`,
{ level: 'warn' },
)
return
}
entries[relPath] = content
} catch {
// Skip unreadable files
}
}
}),
)
} catch (e) {
if (isErrnoException(e)) {
if (e.code !== 'ENOENT' && e.code !== 'EACCES' && e.code !== 'EPERM') {
throw e
}
} else {
throw e
}
}
}
await walkDir(teamDir)
// Truncate only if we've LEARNED a cap from the server (via a structured
// 413's extra_details.max_entries — anthropic/anthropic#293258). The
// server's entry-count cap is GB-tunable per-org via
// claude_code_team_memory_limits; we have no way to know it in advance.
// Before the first 413 we send everything and let the server be
// authoritative. The server validates total stored entries after merge
// (not PUT body count) and rejects atomically — nothing is written on 413.
//
// Sorting before truncation is what makes delta computation work: without
// it, the parallel walk above picks a different N-of-M subset each push
// (Promise.all resolves in completion order), serverChecksums misses keys,
// and the "delta" balloons to near-full snapshot. With deterministic
// truncation, the same N keys are compared against the same server state.
//
// When disk has more files than the learned cap, alphabetically-last ones
// consistently never sync. When the merged (server + delta) count exceeds
// the cap we still fail — recovering requires soft_delete_keys.
const keys = Object.keys(entries).sort()
if (maxEntries !== null && keys.length > maxEntries) {
const dropped = keys.slice(maxEntries)
logForDebugging(
`team-memory-sync: ${keys.length} local entries exceeds server cap of ${maxEntries}; ${dropped.length} file(s) will NOT sync: ${dropped.join(', ')}. Consider consolidating or removing some team memory files.`,
{ level: 'warn' },
)
logEvent('tengu_team_mem_entries_capped', {
total_entries: keys.length,
dropped_count: dropped.length,
max_entries: maxEntries,
})
const truncated: Record<string, string> = {}
for (const key of keys.slice(0, maxEntries)) {
truncated[key] = entries[key]!
}
return { entries: truncated, skippedSecrets }
}
return { entries, skippedSecrets }
}
/**
* Write remote team memory entries to the local directory.
* Validates every path against the team memory directory boundary.
* Skips entries whose on-disk content already matches, so unchanged
* files keep their mtime and don't spuriously invalidate the
* getMemoryFiles cache or trigger watcher events.
*
* Parallel: each entry is processed independently (validate + read-compare
* + mkdir + write). Concurrent mkdir on a shared parent is safe with
* recursive: true (EEXIST is swallowed). The initial pull is the long
* pole in startTeamMemoryWatcher — p99 was ~22s serial at 50 entries.
*
* Returns the number of files actually written.
*/
async function writeRemoteEntriesToLocal(
entries: Record<string, string>,
): Promise<number> {
const results = await Promise.all(
Object.entries(entries).map(async ([relPath, content]) => {
let validatedPath: string
try {
validatedPath = await validateTeamMemKey(relPath)
} catch (e) {
if (e instanceof PathTraversalError) {
logForDebugging(`team-memory-sync: ${e.message}`, { level: 'warn' })
return false
}
throw e
}
const sizeBytes = Buffer.byteLength(content, 'utf8')
if (sizeBytes > MAX_FILE_SIZE_BYTES) {
logForDebugging(
`team-memory-sync: skipping oversized remote entry "${relPath}"`,
{ level: 'info' },
)
return false
}
// Skip if on-disk content already matches. Handles the common case
// where pull returns unchanged entries (skipEtagCache path, first
// pull of a session with warm disk state from prior session).
try {
const existing = await readFile(validatedPath, 'utf8')
if (existing === content) {
return false
}
} catch (e) {
if (
isErrnoException(e) &&
e.code !== 'ENOENT' &&
e.code !== 'ENOTDIR'
) {
logForDebugging(
`team-memory-sync: unexpected read error for "${relPath}": ${e.code}`,
{ level: 'debug' },
)
}
// Fall through to write for ENOENT/ENOTDIR (file doesn't exist yet)
}
try {
const parentDir = validatedPath.substring(
0,
validatedPath.lastIndexOf(sep),
)
await mkdir(parentDir, { recursive: true })
await writeFile(validatedPath, content, 'utf8')
return true
} catch (e) {
logForDebugging(
`team-memory-sync: failed to write "${relPath}": ${e}`,
{ level: 'warn' },
)
return false
}
}),
)
return count(results, Boolean)
}
// ─── Public API ──────────────────────────────────────────────
/**
* Check if team memory sync is available (requires first-party OAuth).
*/
export function isTeamMemorySyncAvailable(): boolean {
return isUsingOAuth()
}
/**
* Pull team memory from the server and write to local directory.
* Returns true if any files were updated.
*/
export async function pullTeamMemory(
state: SyncState,
options?: { skipEtagCache?: boolean },
): Promise<{
success: boolean
filesWritten: number
/** Number of entries the server returned, regardless of whether they were written to disk. */
entryCount: number
notModified?: boolean
error?: string
}> {
const skipEtagCache = options?.skipEtagCache ?? false
const startTime = Date.now()
if (!isUsingOAuth()) {
logPull(startTime, { success: false, errorType: 'no_oauth' })
return {
success: false,
filesWritten: 0,
entryCount: 0,
error: 'OAuth not available',
}
}
const repoSlug = await getGithubRepo()
if (!repoSlug) {
logPull(startTime, { success: false, errorType: 'no_repo' })
return {
success: false,
filesWritten: 0,
entryCount: 0,
error: 'No git remote found',
}
}
const etag = skipEtagCache ? null : state.lastKnownChecksum
const result = await fetchTeamMemory(state, repoSlug, etag)
if (!result.success) {
logPull(startTime, {
success: false,
errorType: result.errorType,
status: result.httpStatus,
})
return {
success: false,
filesWritten: 0,
entryCount: 0,
error: result.error,
}
}
if (result.notModified) {
logPull(startTime, { success: true, notModified: true })
return { success: true, filesWritten: 0, entryCount: 0, notModified: true }
}
if (result.isEmpty || !result.data) {
// Server has no data — clear stale serverChecksums so the next push
// doesn't skip entries it thinks the server already has.
state.serverChecksums.clear()
logPull(startTime, { success: true })
return { success: true, filesWritten: 0, entryCount: 0 }
}
const entries = result.data.content.entries
const responseChecksums = result.data.content.entryChecksums
// Refresh serverChecksums from server-provided per-key hashes.
// Requires anthropic/anthropic#283027 — if the response lacks entryChecksums
// (pre-deploy server), serverChecksums stays empty and the next push uploads
// everything; it self-corrects on push success.
state.serverChecksums.clear()
if (responseChecksums) {
for (const [key, hash] of Object.entries(responseChecksums)) {
state.serverChecksums.set(key, hash)
}
} else {
logForDebugging(
'team-memory-sync: server response missing entryChecksums (pre-#283027 deploy) — next push will be full, not delta',
{ level: 'debug' },
)
}
const filesWritten = await writeRemoteEntriesToLocal(entries)
if (filesWritten > 0) {
const { clearMemoryFileCaches } = await import('../../utils/claudemd.js')
clearMemoryFileCaches()
}
logForDebugging(`team-memory-sync: pulled ${filesWritten} files`, {
level: 'info',
})
logPull(startTime, { success: true, filesWritten })
return {
success: true,
filesWritten,
entryCount: Object.keys(entries).length,
}
}
/**
* Push local team memory files to the server with optimistic locking.
*
* Uses delta upload: only keys whose local content hash differs from
* serverChecksums are included in the PUT. On 412 conflict, probes
* GET ?view=hashes to refresh serverChecksums, recomputes the delta
* (naturally excluding keys where a teammate's push matches ours),
* and retries. No merge, no disk writes — server-only new keys from
* a teammate's concurrent push propagate on the next pull.
*
* Local-wins-on-conflict is the opposite of syncTeamMemory's pull-first
* semantics. This is intentional: pushTeamMemory is triggered by a local edit,
* and that edit must not be silently discarded just because a teammate pushed
* in the meantime. Content-level merge (same key, both changed) is not
* attempted — the local version simply overwrites the server version for that
* key, and the server's edit to that key is lost. This is the lesser evil:
* the local user is actively editing and can re-incorporate the teammate's
* changes, whereas silently discarding the local edit loses work the user
* just did with no recourse.
*/
export async function pushTeamMemory(
state: SyncState,
): Promise<TeamMemorySyncPushResult> {
const startTime = Date.now()
let conflictRetries = 0
if (!isUsingOAuth()) {
logPush(startTime, { success: false, errorType: 'no_oauth' })
return {
success: false,
filesUploaded: 0,
error: 'OAuth not available',
errorType: 'no_oauth',
}
}
const repoSlug = await getGithubRepo()
if (!repoSlug) {
logPush(startTime, { success: false, errorType: 'no_repo' })
return {
success: false,
filesUploaded: 0,
error: 'No git remote found',
errorType: 'no_repo',
}
}
// Read local entries once at the start. Conflict resolution does NOT re-read
// from disk — the delta computation against a refreshed serverChecksums naturally
// excludes server-origin content, so the user's local edit cannot be clobbered.
// Secret scanning (PSR M22174) happens here once — files with detected
// secrets are excluded from the upload set.
const localRead = await readLocalTeamMemory(state.serverMaxEntries)
const entries = localRead.entries
const skippedSecrets = localRead.skippedSecrets
if (skippedSecrets.length > 0) {
// Log a user-visible warning listing which files were skipped and why.
// Don't block the push — just exclude those files. The secret VALUE is
// never logged, only the type label.
const summary = skippedSecrets
.map(s => `"${s.path}" (${s.label})`)
.join(', ')
logForDebugging(
`team-memory-sync: ${skippedSecrets.length} file(s) skipped due to detected secrets: ${summary}. Remove the secret(s) to enable sync for these files.`,
{ level: 'warn' },
)
logEvent('tengu_team_mem_secret_skipped', {
file_count: skippedSecrets.length,
// Only log gitleaks rule IDs (not values, not paths — paths could
// leak repo structure). Comma-joined for compact single-field analytics.
rule_ids: skippedSecrets
.map(s => s.ruleId)
.join(
',',
) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
})
}
// Hash each local entry once. The loop recomputes the delta each iteration
// (serverChecksums may change after a 412 probe) but local hashes are stable.
const localHashes = new Map<string, string>()
for (const [key, content] of Object.entries(entries)) {
localHashes.set(key, hashContent(content))
}
let sawConflict = false
for (
let conflictAttempt = 0;
conflictAttempt <= MAX_CONFLICT_RETRIES;
conflictAttempt++
) {
// Delta: only upload keys whose content hash differs from what we believe
// the server holds. On first push after a fresh pull, this is exactly the
// user's local edits. After a 412 probe, matching hashes are excluded —
// server-origin content from a teammate's concurrent push is naturally
// dropped from the delta, so we never re-upload it.
const delta: Record<string, string> = {}
for (const [key, localHash] of localHashes) {
if (state.serverChecksums.get(key) !== localHash) {
delta[key] = entries[key]!
}
}
const deltaCount = Object.keys(delta).length
if (deltaCount === 0) {
// Nothing to upload. This is the expected fast path after a fresh pull
// with no local edits, and also the convergence point after a 412 where
// the teammate's push was a strict superset of ours.
logPush(startTime, {
success: true,
conflict: sawConflict,
conflictRetries,
})
return {
success: true,
filesUploaded: 0,
...(skippedSecrets.length > 0 && { skippedSecrets }),
}
}
// Split the delta into PUT-sized batches to stay under the gateway's
// body-size limit. Typical deltas (1-3 edited files) land in one batch;
// cold pushes with many files are where this earns its keep. Each batch
// is a complete PUT that upserts its keys independently — if batch N
// fails, batches 1..N-1 are already committed server-side. Updating
// serverChecksums after each success means the outer conflict-loop retry
// naturally resumes from the uncommitted tail (those keys still differ).
// state.lastKnownChecksum is updated inside uploadTeamMemory on each
// 200, so the ETag chain threads through the batches automatically.
const batches = batchDeltaByBytes(delta)
let filesUploaded = 0