MCPcopy Create free account
hub / github.com/Noumena-Network/code / writeRemoteEntriesToLocal

Function writeRemoteEntriesToLocal

src/services/teamMemorySync/index.ts:687–753  ·  view source on GitHub ↗

* 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

(
  entries: Record<string, string>,
)

Source from the content-addressed store, hash-verified

685 * Returns the number of files actually written.
686 */
687async function writeRemoteEntriesToLocal(
688 entries: Record<string, string>,
689): Promise<number> {
690 const results = await Promise.all(
691 Object.entries(entries).map(async ([relPath, content]) => {
692 let validatedPath: string
693 try {
694 validatedPath = await validateTeamMemKey(relPath)
695 } catch (e) {
696 if (e instanceof PathTraversalError) {
697 logForDebugging(`team-memory-sync: ${e.message}`, { level: 'warn' })
698 return false
699 }
700 throw e
701 }
702
703 const sizeBytes = Buffer.byteLength(content, 'utf8')
704 if (sizeBytes > MAX_FILE_SIZE_BYTES) {
705 logForDebugging(
706 `team-memory-sync: skipping oversized remote entry "${relPath}"`,
707 { level: 'info' },
708 )
709 return false
710 }
711
712 // Skip if on-disk content already matches. Handles the common case
713 // where pull returns unchanged entries (skipEtagCache path, first
714 // pull of a session with warm disk state from prior session).
715 try {
716 const existing = await readFile(validatedPath, 'utf8')
717 if (existing === content) {
718 return false
719 }
720 } catch (e) {
721 if (
722 isErrnoException(e) &&
723 e.code !== 'ENOENT' &&
724 e.code !== 'ENOTDIR'
725 ) {
726 logForDebugging(
727 `team-memory-sync: unexpected read error for "${relPath}": ${e.code}`,
728 { level: 'debug' },
729 )
730 }
731 // Fall through to write for ENOENT/ENOTDIR (file doesn't exist yet)
732 }
733
734 try {
735 const parentDir = validatedPath.substring(
736 0,
737 validatedPath.lastIndexOf(sep),
738 )
739 await mkdir(parentDir, { recursive: true })
740 await writeFile(validatedPath, content, 'utf8')
741 return true
742 } catch (e) {
743 logForDebugging(
744 `team-memory-sync: failed to write "${relPath}": ${e}`,

Callers 1

pullTeamMemoryFunction · 0.85

Calls 7

validateTeamMemKeyFunction · 0.85
readFileFunction · 0.85
isErrnoExceptionFunction · 0.85
mkdirFunction · 0.85
countFunction · 0.85
entriesMethod · 0.80
logForDebuggingFunction · 0.50

Tested by

no test coverage detected