Eliminate O(N×L) live-metadata JSON.stringify and sequential full-archive scans in cosAgentStorage
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Refactoring
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- javascript, nodejs
- Ambito
- backend, performance
Direzione di ricerca
Start in server/services/cos/cosAgentStorage.js: read inspect(), previewAgentStorage(), runAutomaticAgentStorage() and revalidate(), then trace the callers in server/services/cos/cos.js (around line 413) and server/routes/dataManager.js (line 29). Run server/services/cos/cosAgentStorage.test.js before and after. Done when live metadata is serialized once per scan, metadata.json is statted once per run, and the automatic path stops at the limit with exact interactive totals.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Audit slice: server/services/cos (re-verification of 2026-11-24 performance-efficiency slice) — Date: 2026-11-26
Scope: medium
Problem
The hourly cos-recording-maintenance job (server/services/cos/cos.js:413-420) calls runAutomaticAgentStorage(), and POST /api/data-manager/agent-storage/preview (server/routes/dataManager.js:29) calls previewAgentStorage(). In server/services/cos/cosAgentStorage.js:
O(N × L)repeatedJSON.stringifyof live agent metadata (cosAgentStorage.js:147): Insideinspect(), every archived agent evaluates:
Object.values(state.agents).some(agent => JSON.stringify(agent.metadata || {}).includes(id))
AcrossNarchived runs andLlive agents, a single scan performsN × LfullJSON.stringifyserializations of every live agent's metadata object.- Sequential full-archive filesystem traversal with redundant syscalls (
cosAgentStorage.js:122-169,:204-224):previewAgentStorage()awaitsinspect(date, id, ...)serially for every archived run on disk. Eachinspect()call performs 10–11 sequential syscalls:safeDirectory()(:122-128) re-runslstat(AGENTS_DIR)andlstat(join(AGENTS_DIR, date))for every single run even thoughlocators()just enumerated those directories withwithFileTypes: true.json(metadata.json)(:131) callslstat+readFileonmetadata.json, and line:137callslstat(join(dir, 'metadata.json'))a second time.- The independent
lstatcalls forraw.txt,raw.txt.gz,output.txt,prompt.txt, andraw-storage.json(:132-137) run sequentially instead of in parallel.
- Unbounded full-disk scans during hourly automatic maintenance (
cosAgentStorage.js:351-362):runAutomaticAgentStorage()only requestslimit: 25candidates and ignorestotals,reasons,rows, andmodels, yetpreviewAgentStorage()still scans every archived run on disk — including date buckets newer thanolderThanDaysand all runs after 25 eligible candidates are already found. When bothautoPurgeandautoCompressare enabled and no purge candidates exist,runAutomaticAgentStorage()runs the full sequential scan twice back-to-back (:356and:359). - Repeated
state.jsonreads during batch execution (cosAgentStorage.js:301-315):revalidate()callsinspect()withoutliveStatetwice per candidate (once before callingcompress/purge, and again insidewithStateLock), re-readingstate.jsonand re-stringifying all live agents' metadata up to 50 times per 25-item batch.
Impact
On installs with thousands of archived CoS runs, every hourly cos-recording-maintenance tick (and every Data Manager storage preview request) executes tens of thousands of sequential filesystem syscalls and hundreds of thousands of redundant JSON.stringify() calls on the main Node event loop, causing recurring CPU and disk I/O spikes.
Fix
Update server/services/cos/cosAgentStorage.js (and cover in server/services/cos/cosAgentStorage.test.js):
- Precompute live-agent reference haystack once per scan: Build a helper (e.g.
buildLiveReferenceContext(liveState)) that computesliveMetadataStrings = Object.values(state.agents).map(agent => JSON.stringify(agent.metadata || {}))once perpreviewAgentStorage()call (and once perrevalidate()call), passing it intoinspect()so line:147checksliveMetadataStrings.some(text => text.includes(id))inO(1)serialization cost. - Parallelize and deduplicate filesystem reads in
inspect()andpreviewAgentStorage():- Reuse the
lstatresult from readingmetadata.jsoninstead of statting it a second time on:137. - Parallelize the independent
lstat/readManifestcalls insideinspect()viaPromise.all. - When called from
previewAgentStorage(), skip re-stattingAGENTS_DIRandAGENTS_DIR/<date>on every agent (validate the parent directories once per bucket). - Process locator batches in
previewAgentStorage()with bounded concurrency (e.g.mapWithConcurrencywith concurrency 16, preserving deterministic date/id ordering forcandidatesandrows).
- Reuse the
- Add an early-stop + date-cutoff fast path for
runAutomaticAgentStorage():- Pass an internal option (e.g.
{ stopAtLimit: true }) fromrunAutomaticAgentStorage()topreviewAgentStorage()so automatic runs skip date buckets strictly newer thannow - (olderThanDays - 1) * DAY(leaving a 1-day UTC/local buffer so timezone/completion-time edge cases are never missed) and stop scanning oncecandidates.length >= limit. - Interactive
POST /api/data-manager/agent-storage/previewkeepsstopAtLimit: falsesototals,reasons,rows, andmodelsremain exact.
- Pass an internal option (e.g.
Acceptance Criteria
-
inspect()does not callJSON.stringify(agent.metadata || {})per archived run when scanning inpreviewAgentStorage(); live agent metadata is serialized at most once per scan. -
inspect()statsmetadata.jsonat most once per run and runs independent artifactlstat/manifest reads in parallel viaPromise.all. -
previewAgentStorage()processes archived runs with bounded concurrency while preserving deterministic ordering and exacttotals,reasons,rows, andmodelsfor interactive previews. -
runAutomaticAgentStorage()skips date buckets newer than the retention threshold (with a safe day buffer) and stops scanning oncelimiteligible candidates are collected. - Existing and new unit tests in
server/services/cos/cosAgentStorage.test.jspass, verifying eligibility, safety gates, live-reference detection, and early-stop behavior.
- Lingua principale
- JavaScript
- Stelle
- 38
- Fork
- 32
- Merge medio
- 14m
- PR unite (30g)
- 995
Preparare l'ambiente
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di atomantic/PortOS
-
area:testing bug effort:low model:light plan planner:gpt-6-2-sol tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
effort:low model:light performance performance-efficiency plan planner:gemini-4-neon source:performance-efficiency
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
I maintainer di solito rispondono entro 1 giorno
-
area:media effort:medium model:medium performance performance-efficiency plan planner:gemini-4-neon source:performance-efficiency
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
I maintainer di solito rispondono entro 1 giorno
-
data-integrity-safety data-safety effort:low model:light plan planner:gemini-4-neon source:data-integrity-safety
Difficoltà 3/5 1-2 giorni Idoneità per principianti 42/100
I maintainer di solito rispondono entro 1 giorno
-
bug effort:low model:light plan planner:gemini-4-neon source:ui-runtime-bugs ui-runtime-bugs
Difficoltà 3/5 Mezza giornata Idoneità per principianti 55/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di atomantic/PortOS
Issue simili
-
enhancement good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
Agent360dk/browser-mcp#124 ·
I maintainer di solito rispondono entro 2 giorni
-
area:site feature good first issue help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
pacocartones/free-llm-api-hub#829 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 1-3 ore Idoneità per principianti 78/100
Jason-Vaughan/TangleClaw#3270 ·
I maintainer di solito rispondono entro 1 giorno
-
Range with start greater than end crashes the server process (bytes=100-50)Forse già presa @draarivpatel-ui l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100