diff --git a/.cursor-plugin/plugin.json b/.cursor-plugin/plugin.json index b46de0240c..de35a75d6d 100644 --- a/.cursor-plugin/plugin.json +++ b/.cursor-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "agentmemory", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "description": "Persistent memory for Cursor: auto-captures sessions, prompts, and tool use via hooks, recalls with hybrid BM25 + vector + graph search, and exposes 55 memory tools over MCP. Keyless by default, runs entirely on your machine.", "author": { "name": "Rohit Ghumare", diff --git a/CHANGELOG.md b/CHANGELOG.md index 27f72c74d1..339194fd65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.9.33-daniel.75 — Additive memory-pressure recovery + +- Preserve the existing available-memory normal/recovery paths; Linux PSI adds a separately qualified recovery path rather than vetoing previously allowed work. +- Keep independent legacy and PSI observation windows, invalidate interrupted legacy evidence at every activation read, and reject malformed PSI fields instead of treating them as zero. +- Preserve critical protection, recovery-attempt history, other pause holds, and final durability/activation checks. Existing application data and capture originals are not rewritten by this policy change. +- Correct the `.74` candidate's additional PSI veto. The `.75` package passed isolated tests and installed-package save/search/normal-shutdown verification, and was subsequently deployed. Live observation does not guarantee safety against future allocation bursts. +- Policy and limitations: `docs/customizations/MEMORY-PRESSURE-BUDGET.md`. + ## 0.9.32-daniel.68 — Sharded graph retrieval index - Store new graph retrieval builds in per-build hash shards to stay below the native file namespace size limit. @@ -22,6 +30,8 @@ ## [Unreleased] +## [0.9.33-daniel.75] — 2026-11-14 +## [0.9.33-daniel.74] — 2026-11-14 ## [0.9.33-daniel.73] — 2026-11-14 - Stage existing SDK capture queue reference replacements in a private, quota-counted queue workspace, avoiding permanent ext4 directory growth from random temporary names in large queues. Preserve the existing 5 MiB admission and 8 KiB transition budgets, crash files, source originals, and conservative refusal when legacy bootstrap cannot fit. Source fix only; installed services are unchanged. See `docs/customizations/SDK-SPOOL-TRANSITION-WORKSPACE.md`. - Make Ubuntu CI subprocess fixtures use the active Node executable. Validate finite capture deadlines under contention without weakening exact-once ACK/original preservation checks, and wait for real browser fixture completion instead of a fixed virtual-time DOM dump. Test-only changes; no operating service changes. @@ -690,6 +700,8 @@ Wave release closing several breaking regressions reported against v0.9.26, plus - `/agentmemory:forget` skill still calls `memory_governance_delete` which only touches `KV.memories` and never observations ([#833](https://github.com/rohitg00/agentmemory/issues/833)). Skill rewrite + new `memory_forget` MCP tool tracked separately. - `crypto.randomUUID()` global-only on Node <19 ([#715](https://github.com/rohitg00/agentmemory/issues/715)). Drop-in import fix tracked. +[0.9.33-daniel.75]: https://github.com/rohitg00/agentmemory/compare/v0.9.33-daniel.74...v0.9.33-daniel.75 +[0.9.33-daniel.74]: https://github.com/rohitg00/agentmemory/compare/v0.9.33-daniel.73...v0.9.33-daniel.74 [0.9.33-daniel.73]: https://github.com/rohitg00/agentmemory/compare/v0.9.33-daniel.72...v0.9.33-daniel.73 [0.9.33-daniel.72]: https://github.com/rohitg00/agentmemory/compare/v0.9.33-daniel.71...v0.9.33-daniel.72 [0.9.33-daniel.71]: https://github.com/rohitg00/agentmemory/compare/v0.9.33-daniel.70...v0.9.33-daniel.71 diff --git a/docs/customizations/MEMORY-PRESSURE-BUDGET.md b/docs/customizations/MEMORY-PRESSURE-BUDGET.md new file mode 100644 index 0000000000..95117c3fb5 --- /dev/null +++ b/docs/customizations/MEMORY-PRESSURE-BUDGET.md @@ -0,0 +1,73 @@ +# Additive memory-pressure recovery + +Status: released as `0.9.33-daniel.75` after isolated tests and installed-package +verification. Existing data and capture originals are not migrated by this policy. +No host pressure experiment was performed. Earlier release artifacts are unchanged. + +## Why + +Available-memory percentages alone can pause work even when measured stall pressure +is low. The additional path permits bounded nonzero stalls without claiming that +past host observations predict future concurrent-pipeline allocation bursts. + +## Policy + +Retain the original critical threshold `max(0.5 GiB, total * 5%)` and all critical +API refusal behavior. With valid Linux PSI only, permit an additional recovery +path after 120 seconds of fresh samples above +`max(3 GiB, critical + 2 GiB)`. Strictly greater is required at the floor. + +The proposed operational stall budget is some <=1%, full <=0.1%, about 1400/146 + times the observed whole-window ratios. These are candidate budgets, not proven +universal safety limits. Use counter deltas / actual monotonic elapsed time, plus +current avg10 and avg60 within the same budgets. Microstalls do not reset a window. +A large/sustained pressure reading or floor violation resets only the added +pressure window. The original policy remains authoritative: available >=18% +(or its existing reserve floor) is normal regardless of PSI, and available >22% +(or its existing recovery floor) permits recovery after 120 fresh seconds even +when PSI is high. PSI is an additional recovery opportunity below the original +reserve, never a veto on either legacy path. Once qualified, healthy pressure +prevents repeated pauses solely at 18%. + +The .74 deployed candidate incorrectly added such a veto. A read-only trace +showed 10.51–11.00 decimal GB available with full stall 0.4615% over 120 seconds, +blocking legacy recovery. The .75 candidate removes the veto without increasing +the PSI budgets or bypassing critical, freshness, attempts or storage protection. +The original recovery timer and the PSI window are independent: a 119-second +quiet interval below the old recovery threshold plus one second above it with +high PSI cannot combine into an approved 120-second recovery. Either complete +path must independently qualify again during activation. + +Missing/malformed PSI, unsafe or regressing counters, non-monotonic elapsed time, +and sampling gaps over 10 seconds disable the added path; use the original 18/22% +policy and rebuild the pressure window. Swap usage is not a zero-swap gate. +Sample counters are validated individually: full delta can microscopically exceed +some delta because kernel reads are not a simultaneous snapshot. + +Existing wall-clock freshness checks, 120-second stable interval, three attempts, +backoff, activation-time rechecks, persisted manual/deployment/storage holds, +durability fences, and failed-activation restoration of the paused control state +are retained. This is not automatic software rollback: operational code rollback +requires separate approval, and data is never rewound. Recovery does not clear +other holds. + +## Limits and replacement + +A 5-second sampling cadence cannot prevent an allocation burst between samples. +No PSI threshold guarantees safe simultaneous pipeline startup. Final activation +checks reject pressure/floor changes before reopening admission, but post-start +burst behavior needs separately authorized operational observation. Upstream can +replace this customization only after equivalent fresh-window, critical protection, +hold preservation, and failed-activation control-state restoration tests pass +without losing data or retry history. + +## Verification + +Deterministic synthetic samples only, with injected monotonic time; no real memory +pressure induced. The focused budget tests cover boundaries, microstalls, missing +PSI, rollover, stale gaps, clock regression, current spikes, actual elapsed time, +compound holds, and post-fence pressure return. Existing protection/durability, +boot, build-profile, and fallback artifact suites are included. The full repository suite and exact-package start/save/search/normal-shutdown +checks also passed before deployment. Actual deployment retained existing capture +records; detailed operational evidence stays outside the public source tree. +Ongoing workload behavior and future allocation bursts still require observation. diff --git a/integrations/hermes/plugin.yaml b/integrations/hermes/plugin.yaml index 1b8e55890d..e42437977e 100644 --- a/integrations/hermes/plugin.yaml +++ b/integrations/hermes/plugin.yaml @@ -1,5 +1,5 @@ name: agentmemory -version: 0.9.33-daniel.73 +version: 0.9.33-daniel.75 description: "Persistent memory for Hermes Agent with profile-scoped capture, recall, and native memory tools." author: "Rohit Ghumare" homepage: "https://github.com/rohitg00/agentmemory" diff --git a/integrations/omp/package.json b/integrations/omp/package.json index 96503114a8..5ca1f6e163 100644 --- a/integrations/omp/package.json +++ b/integrations/omp/package.json @@ -2,7 +2,7 @@ "name": "agentmemory-omp-extension", "private": true, "type": "module", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "omp": { "name": "agentmemory", "description": "Persistent cross-session memory for omp \u2014 captures sessions, tools, approvals, and compaction; injects smart-search recall.", diff --git a/integrations/openclaw/openclaw.plugin.json b/integrations/openclaw/openclaw.plugin.json index 2a725d1245..8278602ad0 100644 --- a/integrations/openclaw/openclaw.plugin.json +++ b/integrations/openclaw/openclaw.plugin.json @@ -3,7 +3,7 @@ "kind": "memory", "name": "agentmemory", "description": "Persistent cross-session memory for OpenClaw via agentmemory.", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "configSchema": { "type": "object", "additionalProperties": false, diff --git a/integrations/openclaw/package.json b/integrations/openclaw/package.json index c8d172bd41..0adc98a207 100644 --- a/integrations/openclaw/package.json +++ b/integrations/openclaw/package.json @@ -1,6 +1,6 @@ { "name": "agentmemory", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "type": "module", "openclaw": { "extensions": [ diff --git a/integrations/openclaw/plugin.yaml b/integrations/openclaw/plugin.yaml index b02e89e7e9..594e290941 100644 --- a/integrations/openclaw/plugin.yaml +++ b/integrations/openclaw/plugin.yaml @@ -1,5 +1,5 @@ name: agentmemory -version: 0.9.33-daniel.73 +version: 0.9.33-daniel.75 description: "Persistent cross-session memory for OpenClaw via agentmemory." author: "Rohit Ghumare" homepage: "https://github.com/rohitg00/agentmemory" diff --git a/package.json b/package.json index 6f3406a070..91645790f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@agentmemory/agentmemory", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "description": "Persistent memory for AI coding agents, powered by iii-engine's three primitives", "type": "module", "main": "dist/index.mjs", diff --git a/packages/mcp/package.json b/packages/mcp/package.json index 10fcad23ba..6f84c53b96 100644 --- a/packages/mcp/package.json +++ b/packages/mcp/package.json @@ -1,6 +1,6 @@ { "name": "@agentmemory/mcp", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "description": "Standalone MCP server for agentmemory — thin shim that re-exposes @agentmemory/agentmemory's MCP entrypoint", "type": "module", "bin": { @@ -28,7 +28,7 @@ "homepage": "https://github.com/rohitg00/agentmemory#readme", "bugs": "https://github.com/rohitg00/agentmemory/issues", "dependencies": { - "@agentmemory/agentmemory": "0.9.33-daniel.73" + "@agentmemory/agentmemory": "0.9.33-daniel.75" }, "publishConfig": { "access": "public", diff --git a/plugin/.claude-plugin/plugin.json b/plugin/.claude-plugin/plugin.json index c4c91bb45e..17e5986d86 100644 --- a/plugin/.claude-plugin/plugin.json +++ b/plugin/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "agentmemory", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "description": "Persistent memory for AI coding agents -- captures tool usage, compresses via LLM, injects context into future sessions. 12 hooks, 55 MCP tools, 17 skills, real-time viewer.", "author": { "name": "Rohit Ghumare", diff --git a/plugin/.codex-plugin/plugin.json b/plugin/.codex-plugin/plugin.json index b69f03b19f..14f803d3a0 100644 --- a/plugin/.codex-plugin/plugin.json +++ b/plugin/.codex-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "agentmemory", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "description": "Local persistent memory for coding agents. Recall decisions, resume sessions, learn from corrections, and manage memory through 55 MCP tools and 17 skills. Optional capture through 6 Codex hooks.", "author": { "name": "Rohit Ghumare", diff --git a/plugin/.devin-plugin/plugin.json b/plugin/.devin-plugin/plugin.json index 7d25963140..85d6873d2b 100644 --- a/plugin/.devin-plugin/plugin.json +++ b/plugin/.devin-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "agentmemory", "displayName": "agentmemory", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "description": "Persistent memory for Devin CLI and Devin Desktop. Captures every session automatically, recalls with hybrid BM25 + vector + graph search, and exposes 55 memory tools over MCP. Keyless by default, runs on your machine. Not supported in cloud sessions.", "logo": "assets/icon.svg", "author": { @@ -23,7 +23,7 @@ "command": "npx", "args": [ "-y", - "@agentmemory/mcp@0.9.33-daniel.73" + "@agentmemory/mcp@0.9.33-daniel.75" ], "env": { "AGENTMEMORY_URL": "${AGENTMEMORY_URL:-http://localhost:3111}" diff --git a/plugin/opencode/plugin.json b/plugin/opencode/plugin.json index 6d7e6595c9..779fb88ccf 100644 --- a/plugin/opencode/plugin.json +++ b/plugin/opencode/plugin.json @@ -1,6 +1,6 @@ { "name": "agentmemory-capture", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "description": "OpenCode plugin for agentmemory — full Claude Code hook parity: session lifecycle (create/idle/status/compacted/update/diff/delete/error), messages & prompts (chat.message, message.updated user+assistant, message.removed), tool lifecycle (ToolPart states with timing), part tracking (subtask, step-finish, reasoning, file, patch, compaction, agent, retry), file enrichment pipeline, permissions, task tracking (w/ priority), commands, config & model tracking. 22 hooks, 2 slash commands.", "author": { "name": "Rohit Ghumare", diff --git a/plugin/plugin.json b/plugin/plugin.json index 2cc3e3f815..f0e0b0b316 100644 --- a/plugin/plugin.json +++ b/plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "agentmemory", - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "description": "Persistent memory for AI coding agents -- captures tool usage, compresses via LLM, injects context into future sessions. 12 hooks, 55 MCP tools, 17 skills, real-time viewer.", "author": { "name": "Rohit Ghumare", diff --git a/plugin/scripts/plugin-bridge.mjs b/plugin/scripts/plugin-bridge.mjs index 788aa540fb..c32a9f6cbb 100644 --- a/plugin/scripts/plugin-bridge.mjs +++ b/plugin/scripts/plugin-bridge.mjs @@ -3,7 +3,7 @@ import { readFileSync, realpathSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; //#region src/version.ts -const VERSION = "0.9.33-daniel.73"; +const VERSION = "0.9.33-daniel.75"; //#endregion //#region src/secret-store.ts const SECRET_KEY = "AGENTMEMORY_SECRET"; diff --git a/scripts/docs-sync.state.json b/scripts/docs-sync.state.json index 4aaa9fb0b7..6f96f78b57 100644 --- a/scripts/docs-sync.state.json +++ b/scripts/docs-sync.state.json @@ -1,5 +1,5 @@ { - "version": "0.9.33-daniel.73", + "version": "0.9.33-daniel.75", "facts": { "mcpTools": [ "55" diff --git a/scripts/personal-release/verify-recovery-artifact.mjs b/scripts/personal-release/verify-recovery-artifact.mjs index 0cda8a2520..b8e42738e5 100644 --- a/scripts/personal-release/verify-recovery-artifact.mjs +++ b/scripts/personal-release/verify-recovery-artifact.mjs @@ -6,21 +6,28 @@ import {resolve} from 'node:path'; import {fileURLToPath} from 'node:url'; import {createHash} from 'node:crypto'; const ensure=(condition,code)=>{if(!condition)throw Error(code);}; -const selectedNames=new Set(['GiB','SAFE_APIS','MEMORY_RECOVERY_ENABLED']); +const selectedNames=new Set(['GiB','SAFE_APIS','MEMORY_RECOVERY_ENABLED','PRESSURE_BUDGET']); function isolatedGuard(bundle){ const file=ts.createSourceFile('reviewed-recovery-bundle.mjs',bundle,ts.ScriptTarget.Latest,true,ts.ScriptKind.JS); ensure(file.parseDiagnostics.length===0,'compiled_guard_shape_unsupported'); const functions=file.statements.filter(s=>ts.isFunctionDeclaration(s)&&s.name?.text==='createMemoryPressureGuard'); ensure(functions.length===1&&functions[0].body,'compiled_guard_shape_unsupported'); + const helpers=['validPressure','pressureWindow'].flatMap(name=>{ + const matches=file.statements.filter(s=>ts.isFunctionDeclaration(s)&&s.name?.text===name); + ensure(matches.length<=1,'compiled_guard_shape_unsupported'); + return matches.map(s=>s.getText(file).replace(/^export\s+/,'')); + }); const constants=new Map(); for(const statement of file.statements){ if(!ts.isVariableStatement(statement))continue; for(const declaration of statement.declarationList.declarations){ if(!ts.isIdentifier(declaration.name)||!selectedNames.has(declaration.name.text))continue; - const name=declaration.name.text,value=declaration.initializer; + const name=declaration.name.text,initializer=declaration.initializer; + const value=name==='PRESSURE_BUDGET'&&initializer&&ts.isCallExpression(initializer)&&initializer.expression.getText(file)==='Object.freeze'&&initializer.arguments.length===1?initializer.arguments[0]:initializer; ensure(!constants.has(name)&&value,'compiled_guard_shape_unsupported'); let valid=false; if(name==='MEMORY_RECOVERY_ENABLED')valid=value.kind===ts.SyntaxKind.TrueKeyword||value.kind===ts.SyntaxKind.FalseKeyword; + if(name==='PRESSURE_BUDGET')valid=ts.isObjectLiteralExpression(value)&&value.properties.length===2&&value.properties.every(p=>ts.isPropertyAssignment(p)&&ts.isIdentifier(p.name)&&ts.isNumericLiteral(p.initializer)&&((p.name.text==='some'&&Number(p.initializer.text)===1)||(p.name.text==='full'&&Number(p.initializer.text)===0.1)))&&new Set(value.properties.map(p=>p.name.text)).size===2; if(name==='GiB')valid=/^1024\s*\*\*\s*3$/.test(value.getText(file))||ts.isNumericLiteral(value)&&Number(value.text)===1024**3; if(name==='SAFE_APIS')valid=ts.isNewExpression(value)&&ts.isIdentifier(value.expression)&&value.expression.text==='Set'&&value.arguments?.length===1&&ts.isArrayLiteralExpression(value.arguments[0])&&value.arguments[0].elements.every(v=>ts.isStringLiteral(v)); ensure(valid,'compiled_guard_shape_unsupported');constants.set(name,`const ${name}=${value.getText(file)};`); @@ -28,7 +35,7 @@ function isolatedGuard(bundle){ } const context=createContext({setInterval(){throw Error('unexpected_scheduled_work');},clearInterval(){}},{codeGeneration:{strings:false,wasm:false}}); const body=functions[0].getText(file).replace(/^export\s+/,''); - return new Script([...constants.values(),body,'createMemoryPressureGuard;'].join('\n')).runInContext(context,{timeout:1000}); + return new Script([...constants.values(),...helpers,body,'createMemoryPressureGuard;'].join('\n')).runInContext(context,{timeout:1000}); } export async function verifyRecoveryBundle({bundle,manifest,expectedVersion}={}){ const profile=manifest?.agentmemoryCompatibility; @@ -38,7 +45,7 @@ export async function verifyRecoveryBundle({bundle,manifest,expectedVersion}={}) let now=1000,available=8*1024**3,reads=0,resumes=0,pauses=0,releasePause; const pausePending=new Promise(r=>{releasePause=r;}); const control={snapshot:()=>({status:'paused',pausedRequested:true,holds:['memory'],recovery:{attempts:2,maxAttempts:3,nextAttemptAt:0}}),change(action){if(action==='resume'){resumes++;return Promise.resolve(this.snapshot());}pauses++;return pausePending;}}; - const guard=createGuard({control,schedule:false,clock:()=>now,readSample:async()=>{reads++;return{total:16*1024**3,available};}}); + const guard=createGuard({control,schedule:false,clock:()=>now,monotonicClock:()=>now,readSample:async()=>{reads++;return{total:16*1024**3,available};}}); try{ ensure(guard.snapshot().automaticMemoryRecovery===false,'compiled_recovery_not_disabled'); for(let i=0;i<80;i++){now+=5000;await guard.refresh();await guard.drain();} diff --git a/src/functions/export-import.ts b/src/functions/export-import.ts index 42d1f9c2e5..d165d6372a 100644 --- a/src/functions/export-import.ts +++ b/src/functions/export-import.ts @@ -315,7 +315,7 @@ export function registerExportImportFunction(sdk: IIIClient, state: StateKV): vo const strategy = data.strategy || "merge"; const importData = data.exportData; - const supportedVersions = new Set(["0.3.0", "0.4.0", "0.5.0", "0.6.0", "0.6.1", "0.7.0", "0.7.2", "0.7.3", "0.7.4", "0.7.5", "0.7.6", "0.7.7", "0.7.9", "0.8.0", "0.8.1", "0.8.2", "0.8.3", "0.8.4", "0.8.5", "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10", "0.8.11", "0.8.12", "0.8.13", "0.9.0", "0.9.1", "0.9.2", "0.9.3", "0.9.4", "0.9.5", "0.9.6", "0.9.7", "0.9.8", "0.9.9", "0.9.10", "0.9.11", "0.9.12", "0.9.13", "0.9.14", "0.9.15", "0.9.16", "0.9.17", "0.9.18", "0.9.19", "0.9.20", "0.9.21", "0.9.22", "0.9.23", "0.9.24", "0.9.25", "0.9.26", "0.9.27", "0.9.28", "0.9.29", "0.9.30", "0.9.31", "0.9.31-daniel.1", "0.9.31-daniel.33", "0.9.31-daniel.34", "0.9.31-daniel.35", "0.9.31-daniel.36", "0.9.31-daniel.37", "0.9.31-daniel.38", "0.9.31-daniel.39", "0.9.31-daniel.40", "0.9.31-daniel.41", "0.9.31-daniel.42", "0.9.31-daniel.43", "0.9.31-daniel.44", "0.9.31-daniel.45", "0.9.31-daniel.46", "0.9.31-daniel.47", "0.9.31-daniel.48", "0.9.31-daniel.49", "0.9.31-daniel.50", "0.9.31-daniel.51", "0.9.31-daniel.52", "0.9.31-daniel.53", "0.9.31-daniel.54", "0.9.32-daniel.55", "0.9.32-daniel.57", "0.9.32-daniel.58", "0.9.32-daniel.59", "0.9.32-daniel.60", "0.9.32-daniel.61", "0.9.32-daniel.62", "0.9.32-daniel.63", "0.9.32-daniel.64", "0.9.32-daniel.65", "0.9.32-daniel.66", "0.9.32-daniel.67", "0.9.32-daniel.68", "0.9.33-daniel.69", "0.9.33-daniel.70", "0.9.33-daniel.71", "0.9.33-daniel.72", "0.9.33-daniel.73", "0.9.30-daniel.1", "0.9.30-daniel.10", "0.9.30-daniel.11", "0.9.30-daniel.15", "0.9.30-daniel.16", "0.9.30-daniel.18", "0.9.30-daniel.19", "0.9.30-daniel.2", "0.9.30-daniel.20", "0.9.30-daniel.21", "0.9.30-daniel.22", "0.9.30-daniel.23", "0.9.30-daniel.24", "0.9.30-daniel.25", "0.9.30-daniel.26", "0.9.30-daniel.27", "0.9.30-daniel.28", "0.9.30-daniel.3", "0.9.30-daniel.4", "0.9.30-daniel.5", "0.9.30-daniel.6", "0.9.30-daniel.7", "0.9.30-daniel.8", "0.9.30-daniel.9", "0.9.30-daniel.29", "0.9.30-daniel.30", "0.9.30-daniel.31", "0.9.30-daniel.32"]); + const supportedVersions = new Set(["0.3.0", "0.4.0", "0.5.0", "0.6.0", "0.6.1", "0.7.0", "0.7.2", "0.7.3", "0.7.4", "0.7.5", "0.7.6", "0.7.7", "0.7.9", "0.8.0", "0.8.1", "0.8.2", "0.8.3", "0.8.4", "0.8.5", "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10", "0.8.11", "0.8.12", "0.8.13", "0.9.0", "0.9.1", "0.9.2", "0.9.3", "0.9.4", "0.9.5", "0.9.6", "0.9.7", "0.9.8", "0.9.9", "0.9.10", "0.9.11", "0.9.12", "0.9.13", "0.9.14", "0.9.15", "0.9.16", "0.9.17", "0.9.18", "0.9.19", "0.9.20", "0.9.21", "0.9.22", "0.9.23", "0.9.24", "0.9.25", "0.9.26", "0.9.27", "0.9.28", "0.9.29", "0.9.30", "0.9.31", "0.9.31-daniel.1", "0.9.31-daniel.33", "0.9.31-daniel.34", "0.9.31-daniel.35", "0.9.31-daniel.36", "0.9.31-daniel.37", "0.9.31-daniel.38", "0.9.31-daniel.39", "0.9.31-daniel.40", "0.9.31-daniel.41", "0.9.31-daniel.42", "0.9.31-daniel.43", "0.9.31-daniel.44", "0.9.31-daniel.45", "0.9.31-daniel.46", "0.9.31-daniel.47", "0.9.31-daniel.48", "0.9.31-daniel.49", "0.9.31-daniel.50", "0.9.31-daniel.51", "0.9.31-daniel.52", "0.9.31-daniel.53", "0.9.31-daniel.54", "0.9.32-daniel.55", "0.9.32-daniel.57", "0.9.32-daniel.58", "0.9.32-daniel.59", "0.9.32-daniel.60", "0.9.32-daniel.61", "0.9.32-daniel.62", "0.9.32-daniel.63", "0.9.32-daniel.64", "0.9.32-daniel.65", "0.9.32-daniel.66", "0.9.32-daniel.67", "0.9.32-daniel.68", "0.9.33-daniel.69", "0.9.33-daniel.70", "0.9.33-daniel.71", "0.9.33-daniel.72", "0.9.33-daniel.73", "0.9.33-daniel.74", "0.9.33-daniel.75", "0.9.30-daniel.1", "0.9.30-daniel.10", "0.9.30-daniel.11", "0.9.30-daniel.15", "0.9.30-daniel.16", "0.9.30-daniel.18", "0.9.30-daniel.19", "0.9.30-daniel.2", "0.9.30-daniel.20", "0.9.30-daniel.21", "0.9.30-daniel.22", "0.9.30-daniel.23", "0.9.30-daniel.24", "0.9.30-daniel.25", "0.9.30-daniel.26", "0.9.30-daniel.27", "0.9.30-daniel.28", "0.9.30-daniel.3", "0.9.30-daniel.4", "0.9.30-daniel.5", "0.9.30-daniel.6", "0.9.30-daniel.7", "0.9.30-daniel.8", "0.9.30-daniel.9", "0.9.30-daniel.29", "0.9.30-daniel.30", "0.9.30-daniel.31", "0.9.30-daniel.32"]); if (!supportedVersions.has(importData.version)) { return { success: false, diff --git a/src/ops/memory-pressure.d.mts b/src/ops/memory-pressure.d.mts index 099b69ac9f..4e05d59039 100644 --- a/src/ops/memory-pressure.d.mts +++ b/src/ops/memory-pressure.d.mts @@ -1,8 +1,10 @@ import type { IIIClient } from "iii-sdk"; import type { HttpRequest } from "@iii-dev/helpers/http"; import type { BackgroundControl } from "./background-control.mjs"; -export function readHostMemory():Promise<{total:number;available:number}|null>; -export function createMemoryPressureGuard(options:{control:BackgroundControl;readSample?:typeof readHostMemory;schedule?:boolean;clock?:()=>number}):{ +export interface MemoryPressureSample {some:{avg10:number;avg60:number;total:number};full:{avg10:number;avg60:number;total:number};} +export function parseMemoryPressure(text:string):MemoryPressureSample|null; +export function readHostMemory():Promise<{total:number;available:number;pressure?:MemoryPressureSample|null}|null>; +export function createMemoryPressureGuard(options:{control:BackgroundControl;readSample?:typeof readHostMemory;schedule?:boolean;clock?:()=>number;monotonicClock?:()=>number}):{ refresh():Promise;snapshot():{level:string;[key:string]:unknown}; install(sdk:IIIClient,auth:(req:HttpRequest)=>unknown):void; start():void;stop():Promise;drain():Promise; diff --git a/src/ops/memory-pressure.mjs b/src/ops/memory-pressure.mjs index 6af2f1ba49..42ee1c334e 100644 --- a/src/ops/memory-pressure.mjs +++ b/src/ops/memory-pressure.mjs @@ -3,26 +3,74 @@ export const MEMORY_RECOVERY_ENABLED = true; const GiB=1024**3; const SAFE_APIS=new Set(['api::liveness','api::health','api::config-flags','api::background-control-get','api::background-control-set']); +// Candidate operational stall budgets, not a guarantee against allocation bursts. +const PRESSURE_BUDGET=Object.freeze({some:1,full:0.1}); +export function parseMemoryPressure(text){ + const result={}; + for(const name of ['some','full']){ + const lines=text.split('\n').filter(line=>line.startsWith(name+' ')); + if(lines.length!==1)return null; + const fields={}; + for(const token of lines[0].trim().split(/\s+/).slice(1)){ + const match=/^(avg10|avg60|avg300|total)=([0-9]+(?:\.[0-9]+)?)$/.exec(token); + if(!match||Object.hasOwn(fields,match[1])||(match[1]==='total'&&!/^[0-9]+$/.test(match[2])))return null; + fields[match[1]]=Number(match[2]); + if(match[1]!=='total'&&(!Number.isFinite(fields[match[1]])||fields[match[1]]>100))return null; + } + result[name]={avg10:fields.avg10,avg60:fields.avg60,total:fields.total}; + } + return validPressure(result)?result:null; +} +function validPressure(p){ + return ['some','full'].every(name=>p?.[name]&&['avg10','avg60'].every(key=>Number.isFinite(p[name][key])&&p[name][key]>=0&&p[name][key]<=100)&&Number.isSafeInteger(p[name].total)&&p[name].total>=0); +} +function pressureWindow(){ + let points=[]; + return (p,now,available,floor)=>{ + const last=points.at(-1); + if(!validPressure(p)||!Number.isFinite(now)||(last&&(now10000||['some','full'].some(name=>p[name].totalfloor&&['some','full'].every(name=>p[name].avg10<=PRESSURE_BUDGET[name]&&p[name].avg60<=PRESSURE_BUDGET[name]); + if(!current){points=[];return {quiet:false,ready:false};} + if(last?.now===now){ + if(point.some!==last.some||point.full!==last.full){points=[];return null;} + }else points.push(point); + while(points.length>1&&points[1].now<=now-120000)points.shift(); + const first=points[0],elapsed=now-first.now; + const quiet=elapsed===0||['some','full'].every(name=>(point[name]-first[name])/(elapsed*1000)*100<=PRESSURE_BUDGET[name]); + if(!quiet){points=[];return {quiet:false,ready:false};} + return {quiet:true,ready:elapsed>=120000}; + }; +} + export async function readHostMemory(){ if(process.platform!=='linux')return null; const text=await readFile('/proc/meminfo','utf8'); const bytes=name=>Number(text.match(new RegExp('^'+name+':\\s+(\\d+)\\s+kB','m'))?.[1])*1024; - return{total:bytes('MemTotal'),available:bytes('MemAvailable')}; + let pressure=null;try{pressure=parseMemoryPressure(await readFile('/proc/pressure/memory','utf8'));}catch{} + return{total:bytes('MemTotal'),available:bytes('MemAvailable'),pressure}; } -export function createMemoryPressureGuard({control,readSample=readHostMemory,schedule=true,clock=Date.now}={}){ +export function createMemoryPressureGuard({control,readSample=readHostMemory,schedule=true,clock=Date.now,monotonicClock=()=>performance.now()}={}){ let state={automaticMemoryRecovery:MEMORY_RECOVERY_ENABLED,level:'unknown',totalBytes:null,availableBytes:null,reserveBytes:null,criticalBytes:null,checkedAt:null,rejectedRequests:0,recoveryStatus:'observing',stableSince:null},timer=null,active=null,pendingRecovery=null,pendingPause=null,stopped=false; let lastSampleAt=null,stableSince=null,sampleSequence=0; + const observePressure=pressureWindow(); async function readFresh(){ const sequence=++sampleSequence,before=clock(),m=await readSample(),now=clock(); if(sequence!==sampleSequence||!m||!Number.isFinite(m.total)||m.total<=0||!Number.isFinite(m.available)||m.available<0||m.available>m.total||!Number.isFinite(now)||now10000)throw Error('memory_sample_unavailable'); const reserve=Math.max(2*GiB,m.total*.18),critical=Math.max(GiB/2,m.total*.05),recoveryBytes=Math.max(3*GiB,m.total*.22); - state={...state,level:m.availablerecoveryBytes}; + const pressureFloor=Math.max(3*GiB,critical+2*GiB),pressure=observePressure(m.pressure,monotonicClock(),m.available,pressureFloor); + const oldRecovered=m.available>recoveryBytes; + if(!oldRecovered)stableSince=null; + const level=m.available=reserve||(pressure?.quiet===true&&pressure.ready)?'normal':'high'; + state={...state,level,stableSince,pressureFloorBytes:pressureFloor,pressureBudget:PRESSURE_BUDGET,pressureWindowReady:pressure?.ready??false,pressureFallback:pressure===null,totalBytes:m.total,availableBytes:m.available,reserveBytes:reserve,criticalBytes:critical,recoveryBytes,sampleError:false,checkedAt:now}; + return{now,recovered:oldRecovered,pressureRecovered:pressure?.quiet===true&&pressure.ready}; } async function sample(){ try{ - const {now,recovered}=await readFresh(); + const {now,recovered,pressureRecovered}=await readFresh(); if(lastSampleAt===null||now10000||!recovered)stableSince=null; lastSampleAt=now; if(!recovered)stableSince=null;else if(stableSince===null)stableSince=now; @@ -42,17 +90,17 @@ export function createMemoryPressureGuard({control,readSample=readHostMemory,sch if(c.recovery.attempts>=c.recovery.maxAttempts){state.recoveryStatus='attempt_limit';return;} if(now{ let checked; - try{checked=await readFresh();}catch{stableSince=null;lastSampleAt=null;state={...state,level:['high','critical'].includes(state.level)?state.level:'unknown',sampleError:true,stableSince:null};return false;} - if(stableSince===null||checked.now-stableSince<120000||!checked.recovered||checked.now10000||stopped){stableSince=null;state.stableSince=null;return false;} + try{checked=await readFresh();}catch{observePressure(null,0,0,0);stableSince=null;lastSampleAt=null;state={...state,level:['high','critical'].includes(state.level)?state.level:'unknown',sampleError:true,stableSince:null};return false;} + if(!(checked.pressureRecovered||(stableSince!==null&&checked.now-stableSince>=120000&&checked.recovered))||checked.now10000||stopped){stableSince=null;state.stableSince=null;return false;} lastSampleAt=checked.now;return true; }}).then(()=>{ state.recoveryStatus=['high','critical'].includes(state.level)?'pressure':control.snapshot().pausedRequested?'held':'recovered'; },()=>{state.recoveryStatus='held';}).finally(()=>{pendingRecovery=null;stableSince=null;state.stableSince=null;}); - }catch{stableSince=null;lastSampleAt=null;state={...state,level:['high','critical'].includes(state.level)?state.level:'unknown',sampleError:true,recoveryStatus:'sample_error',stableSince:null,checkedAt:clock()};} + }catch{observePressure(null,0,0,0);stableSince=null;lastSampleAt=null;state={...state,level:['high','critical'].includes(state.level)?state.level:'unknown',sampleError:true,recoveryStatus:'sample_error',stableSince:null,checkedAt:clock()};} } function refresh(){if(stopped)return Promise.resolve();if(active)return active;active=sample().finally(()=>{active=null;});return active;} async function drain(){while(active||pendingRecovery||pendingPause)await Promise.all([active,pendingRecovery,pendingPause]);} diff --git a/src/types.ts b/src/types.ts index f1ec9d3a88..d2e1d305d7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -421,7 +421,7 @@ export interface ExportCollectionPagination { } export interface ExportData { - version: "0.3.0" | "0.4.0" | "0.5.0" | "0.6.0" | "0.6.1" | "0.7.0" | "0.7.2" | "0.7.3" | "0.7.4" | "0.7.5" | "0.7.6" | "0.7.7" | "0.7.9" | "0.8.0" | "0.8.1" | "0.8.2" | "0.8.3" | "0.8.4" | "0.8.5" | "0.8.6" | "0.8.7" | "0.8.8" | "0.8.9" | "0.8.10" | "0.8.11" | "0.8.12" | "0.8.13" | "0.9.0" | "0.9.1" | "0.9.2" | "0.9.3" | "0.9.4" | "0.9.5" | "0.9.6" | "0.9.7" | "0.9.8" | "0.9.9" | "0.9.10" | "0.9.11" | "0.9.12" | "0.9.13" | "0.9.14" | "0.9.15" | "0.9.16" | "0.9.17" | "0.9.18" | "0.9.19" | "0.9.20" | "0.9.21" | "0.9.22" | "0.9.23" | "0.9.24" | "0.9.25" | "0.9.26" | "0.9.27" | "0.9.28" | "0.9.29" | "0.9.30" | "0.9.30-daniel.1" | "0.9.30-daniel.2" | "0.9.30-daniel.3" | "0.9.30-daniel.4" | "0.9.30-daniel.5" | "0.9.30-daniel.6" | "0.9.30-daniel.7" | "0.9.30-daniel.8" | "0.9.30-daniel.9" | "0.9.30-daniel.10" | "0.9.30-daniel.11" | "0.9.30-daniel.15" | "0.9.30-daniel.16" | "0.9.30-daniel.18" | "0.9.30-daniel.19" | "0.9.30-daniel.20" | "0.9.30-daniel.21" | "0.9.30-daniel.22" | "0.9.30-daniel.23" | "0.9.30-daniel.24" | "0.9.30-daniel.25" | "0.9.30-daniel.26" | "0.9.30-daniel.27" | "0.9.30-daniel.28" | "0.9.31" | "0.9.31-daniel.1" | "0.9.31-daniel.33" | "0.9.31-daniel.34" | "0.9.31-daniel.35" | "0.9.31-daniel.36" | "0.9.31-daniel.37" | "0.9.31-daniel.38" | "0.9.31-daniel.39" | "0.9.31-daniel.40" | "0.9.31-daniel.41" | "0.9.31-daniel.42" | "0.9.31-daniel.43" | "0.9.31-daniel.44" | "0.9.31-daniel.45" | "0.9.31-daniel.46" | "0.9.31-daniel.47" | "0.9.31-daniel.48" | "0.9.31-daniel.49" | "0.9.31-daniel.50" | "0.9.31-daniel.51" | "0.9.31-daniel.52" | "0.9.31-daniel.53" | "0.9.31-daniel.54" | "0.9.32-daniel.55" | "0.9.32-daniel.57" | "0.9.32-daniel.58" | "0.9.32-daniel.59" | "0.9.32-daniel.60" | "0.9.32-daniel.61" | "0.9.32-daniel.62" | "0.9.32-daniel.63" | "0.9.32-daniel.64" | "0.9.32-daniel.65" | "0.9.32-daniel.66" | "0.9.32-daniel.67" | "0.9.32-daniel.68" | "0.9.33-daniel.69" | "0.9.33-daniel.70" | "0.9.33-daniel.71" | "0.9.33-daniel.72" | "0.9.33-daniel.73" | "0.9.30-daniel.29" | "0.9.30-daniel.30" | "0.9.30-daniel.31" | "0.9.30-daniel.32"; + version: "0.3.0" | "0.4.0" | "0.5.0" | "0.6.0" | "0.6.1" | "0.7.0" | "0.7.2" | "0.7.3" | "0.7.4" | "0.7.5" | "0.7.6" | "0.7.7" | "0.7.9" | "0.8.0" | "0.8.1" | "0.8.2" | "0.8.3" | "0.8.4" | "0.8.5" | "0.8.6" | "0.8.7" | "0.8.8" | "0.8.9" | "0.8.10" | "0.8.11" | "0.8.12" | "0.8.13" | "0.9.0" | "0.9.1" | "0.9.2" | "0.9.3" | "0.9.4" | "0.9.5" | "0.9.6" | "0.9.7" | "0.9.8" | "0.9.9" | "0.9.10" | "0.9.11" | "0.9.12" | "0.9.13" | "0.9.14" | "0.9.15" | "0.9.16" | "0.9.17" | "0.9.18" | "0.9.19" | "0.9.20" | "0.9.21" | "0.9.22" | "0.9.23" | "0.9.24" | "0.9.25" | "0.9.26" | "0.9.27" | "0.9.28" | "0.9.29" | "0.9.30" | "0.9.30-daniel.1" | "0.9.30-daniel.2" | "0.9.30-daniel.3" | "0.9.30-daniel.4" | "0.9.30-daniel.5" | "0.9.30-daniel.6" | "0.9.30-daniel.7" | "0.9.30-daniel.8" | "0.9.30-daniel.9" | "0.9.30-daniel.10" | "0.9.30-daniel.11" | "0.9.30-daniel.15" | "0.9.30-daniel.16" | "0.9.30-daniel.18" | "0.9.30-daniel.19" | "0.9.30-daniel.20" | "0.9.30-daniel.21" | "0.9.30-daniel.22" | "0.9.30-daniel.23" | "0.9.30-daniel.24" | "0.9.30-daniel.25" | "0.9.30-daniel.26" | "0.9.30-daniel.27" | "0.9.30-daniel.28" | "0.9.31" | "0.9.31-daniel.1" | "0.9.31-daniel.33" | "0.9.31-daniel.34" | "0.9.31-daniel.35" | "0.9.31-daniel.36" | "0.9.31-daniel.37" | "0.9.31-daniel.38" | "0.9.31-daniel.39" | "0.9.31-daniel.40" | "0.9.31-daniel.41" | "0.9.31-daniel.42" | "0.9.31-daniel.43" | "0.9.31-daniel.44" | "0.9.31-daniel.45" | "0.9.31-daniel.46" | "0.9.31-daniel.47" | "0.9.31-daniel.48" | "0.9.31-daniel.49" | "0.9.31-daniel.50" | "0.9.31-daniel.51" | "0.9.31-daniel.52" | "0.9.31-daniel.53" | "0.9.31-daniel.54" | "0.9.32-daniel.55" | "0.9.32-daniel.57" | "0.9.32-daniel.58" | "0.9.32-daniel.59" | "0.9.32-daniel.60" | "0.9.32-daniel.61" | "0.9.32-daniel.62" | "0.9.32-daniel.63" | "0.9.32-daniel.64" | "0.9.32-daniel.65" | "0.9.32-daniel.66" | "0.9.32-daniel.67" | "0.9.32-daniel.68" | "0.9.33-daniel.69" | "0.9.33-daniel.70" | "0.9.33-daniel.71" | "0.9.33-daniel.72" | "0.9.33-daniel.73" | "0.9.33-daniel.74" | "0.9.33-daniel.75" | "0.9.30-daniel.29" | "0.9.30-daniel.30" | "0.9.30-daniel.31" | "0.9.30-daniel.32"; exportedAt: string; sessions: Session[]; observations: Record; diff --git a/src/version.ts b/src/version.ts index dee9c44668..baae89d89b 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1,2 +1,2 @@ -export const VERSION = "0.9.33-daniel.73"; +export const VERSION = "0.9.33-daniel.75"; export const III_PINNED_VERSION = "0.25.1"; diff --git a/test/memory-pressure-budget.test.ts b/test/memory-pressure-budget.test.ts new file mode 100644 index 0000000000..1c83311212 --- /dev/null +++ b/test/memory-pressure-budget.test.ts @@ -0,0 +1,112 @@ +import {expect,it} from 'vitest'; +import {createBackgroundControl} from '../src/ops/background-control.mjs'; +import {createMemoryPressureGuard,parseMemoryPressure} from '../src/ops/memory-pressure.mjs'; +const GiB=1024**3; +async function fixture(hold?:string){ + let now=1000,mono=1000,record:any=hold?{version:2,paused:true,resumePending:false,holds:['memory',hold],recovery:{attempts:0,nextAttemptAt:0}}:null; + const m:any={total:30*GiB,available:5*GiB,pressure:{some:{avg10:0,avg60:0,total:1000},full:{avg10:0,avg60:0,total:1000}}}; + const kv:any={get:async()=>record,set:async(_:any,__:any,v:any)=>record=structuredClone(v),durability:{wait:async()=>({synced:true,mutationFailures:0}),waitForKeys:async()=>{}}}; + const control=createBackgroundControl({clock:()=>now});await control.init(kv);await control.change('pause',{source:'memory'}); + const guard=createMemoryPressureGuard({control,schedule:false,clock:()=>now,monotonicClock:()=>mono,readSample:async()=>structuredClone(m)}); + async function tick(ms=5000){now+=ms;mono+=ms;await guard.refresh();await guard.drain();} + return {m,kv,control,guard,tick,record:()=>record,mono:(v:number)=>mono=v,async window(){await tick(0);for(let i=0;i<24;i++){m.pressure.some.total+=40;m.pressure.full.total+=39;await tick();}}}; +} +it('admits bounded microstalls after 120 seconds below the old reserve without changing critical',async()=>{ + const f=await fixture();await f.tick(0);for(let i=0;i<23;i++){f.m.pressure.some.total+=40;f.m.pressure.full.total+=39;await f.tick();} + expect(f.control.isPaused()).toBe(true);await f.tick();expect(f.control.isPaused()).toBe(false); + expect(f.guard.snapshot()).toMatchObject({level:'normal',criticalBytes:1.5*GiB}); + await f.tick();expect(f.control.isPaused()).toBe(false); +}); +it.each(['missing','nan','regression','gap','floor','spike','budget','clock'])('does not use a broken pressure window: %s',async risk=>{ + const f=await fixture();await f.tick(0);for(let i=0;i<23;i++)await f.tick(); + if(risk==='missing')delete f.m.pressure; + if(risk==='nan')f.m.pressure.some.total=NaN; + if(risk==='regression')f.m.pressure.full.total=0; + if(risk==='floor')f.m.available=3.5*GiB; + if(risk==='spike')f.m.pressure.some.avg10=1.01; + if(risk==='budget')f.m.pressure.full.total+=121000; + if(risk==='clock')f.mono(0); + await f.tick(risk==='gap'?10001:5000);expect(f.control.isPaused()).toBe(true); +}); +it('falls back to original recovery when PSI is absent',async()=>{const f=await fixture();delete f.m.pressure;f.m.available=7*GiB;await f.tick(0);for(let i=0;i<24;i++)await f.tick();expect(f.control.isPaused()).toBe(false);}); +it.each(['spike','critical','missing'])('rechecks %s after the final storage fence',async risk=>{ + const f=await fixture();await f.tick(0);for(let i=0;i<23;i++)await f.tick(); + f.kv.durability.waitForKeys=async()=>{if(!f.record().paused){if(risk==='spike')f.m.pressure.full.avg60=.101;if(risk==='critical')f.m.available=GiB;if(risk==='missing')delete f.m.pressure;}}; + await f.tick();expect(f.control.snapshot().recovery.attempts).toBe(1);expect(f.control.snapshot().holds).toContain('memory');expect(f.control.isPaused()).toBe(true); +}); +it.each(['manual','deployment','storage_error'])('preserves %s hold under pressure recovery',async hold=>{const f=await fixture(hold);await f.window();expect(f.control.snapshot().holds).toContain(hold);expect(f.control.isPaused()).toBe(true);}); + +it('accepts exact budgets but does not recover before actual monotonic 120 seconds',async()=>{ + const f=await fixture();f.m.pressure.some.avg10=1;f.m.pressure.some.avg60=1;f.m.pressure.full.avg10=.1;f.m.pressure.full.avg60=.1; + await f.tick(0);for(let i=0;i<24;i++){f.m.pressure.some.total+=50000;f.m.pressure.full.total+=5000;await f.tick();} + expect(f.control.isPaused()).toBe(false); + const g=await fixture();await g.tick(0);for(let i=0;i<23;i++)await g.tick();g.mono(115000);await g.tick();expect(g.control.isPaused()).toBe(true); +}); +it('restarts observation after a rollover and admits only a new full window',async()=>{ + const f=await fixture();await f.tick(0);for(let i=0;i<23;i++)await f.tick();f.m.pressure.some.total=0;await f.tick();expect(f.control.isPaused()).toBe(true); + await f.tick();for(let i=0;i<23;i++)await f.tick();expect(f.control.isPaused()).toBe(true);await f.tick();expect(f.control.isPaused()).toBe(false); +}); +it('pauses a recovered service on new pressure and immediately keeps critical protection',async()=>{ + const f=await fixture();await f.window();f.m.pressure.full.avg10=.101;await f.tick();expect(f.control.snapshot().holds).toContain('memory'); + f.m.available=GiB;await f.tick();expect(f.guard.snapshot().level).toBe('critical'); +}); +it('requires a fresh activation certificate after a sampling gap',async()=>{ + const f=await fixture();await f.tick(0);for(let i=0;i<23;i++)await f.tick(); + f.kv.durability.waitForKeys=async()=>{if(!f.record().paused)f.mono(200000);};await f.tick();expect(f.control.isPaused()).toBe(true);expect(f.control.snapshot().holds).toContain('memory'); +}); +it('parses Linux pressure without inventing zero pressure on malformed or partial data',()=>{ + const line='some avg10=0.01 avg60=0.02 avg300=0.03 total=1000\nfull avg10=0 avg60=0 avg300=0 total=1001'; + expect(parseMemoryPressure(line)).toMatchObject({some:{total:1000},full:{total:1001}}); + for(const text of ['',line.split('\n')[0],line.replace('total=1000','total=-1'),line.replace('avg10=0.01','avg10=NaN'),line.replace('total=1000','total=9007199254740992')])expect(parseMemoryPressure(text)).toBeNull(); +}); + +it.each(['floor','critical','pressure'])('revokes normal resume admission after recovered %s risk',async risk=>{ + const f=await fixture();await f.window();const handlers=new Map();const sdk:any={registerFunction:(id:string,fn:any)=>handlers.set(id,fn)}; + f.guard.install(sdk,()=>null);sdk.registerFunction('api::background-control-set',async()=>({status_code:202}));sdk.registerFunction('api::synthetic-write',async()=>({status_code:200})); + const resume=()=>handlers.get('api::background-control-set')({body:{action:'resume'}}); + expect((await resume()).status_code).toBe(202); + if(risk==='floor')f.m.available=3.5*GiB;if(risk==='critical')f.m.available=GiB;if(risk==='pressure')f.m.pressure.some.avg60=1.01; + await f.tick();expect((await resume()).status_code).toBe(503);expect(f.control.snapshot().holds).toContain('memory'); + expect((await handlers.get('api::synthetic-write')({})).status_code).toBe(risk==='critical'?503:200); +}); +it.each([ + 'some avg10= avg60= total=\nfull avg10= avg60= total=', + 'some avg10=0x1 avg60=0 total=1\nfull avg10=0 avg60=0 total=1', + 'some avg10=Infinity avg60=0 total=1\nfull avg10=0 avg60=0 total=1', + 'some avg10=0 avg10=1 avg60=0 total=1\nfull avg10=0 avg60=0 total=1', + 'some avg10=0 avg60=0 total=1 total=2\nfull avg10=0 avg60=0 total=1', + 'some avg10= 0 avg60=0 total=1\nfull avg10=0 avg60=0 total=1', + 'some avg10=0 avg60=0 total=1\nsome avg10=0 avg60=0 total=1\nfull avg10=0 avg60=0 total=1', +])('rejects malformed PSI lexical fields instead of treating them as zero: %s',text=>{ + expect(parseMemoryPressure(text)).toBeNull(); +}); +it('preserves legacy 22 percent recovery after 120 seconds even with high PSI',async()=>{ + const f=await fixture();f.m.available=7*GiB;f.m.pressure.some.avg10=10;f.m.pressure.some.avg60=10;f.m.pressure.full.avg10=5;f.m.pressure.full.avg60=5; + await f.tick(0);for(let i=0;i<23;i++)await f.tick();expect(f.control.isPaused()).toBe(true); + await f.tick();expect(f.control.isPaused()).toBe(false);expect(f.guard.snapshot().level).toBe('normal'); +}); +it('does not add a pressure veto to running legacy-normal memory above 18 percent',async()=>{ + const f=await fixture();await f.window();f.m.available=6*GiB;f.m.pressure.full.avg10=10; + await f.tick();expect(f.control.isPaused()).toBe(false);expect(f.guard.snapshot().level).toBe('normal'); +}); +it('still refuses below-reserve recovery when the added PSI path has sustained pressure',async()=>{ + const f=await fixture();f.m.pressure.full.avg60=1;await f.window();expect(f.control.isPaused()).toBe(true);expect(f.guard.snapshot().level).toBe('high'); +}); +it('never combines 119 seconds of quiet below legacy recovery with one high-PSI legacy second',async()=>{ + const f=await fixture();await f.tick(0);for(let i=0;i<23;i++)await f.tick();await f.tick(4000); + f.m.available=7*GiB;f.m.pressure.full.avg10=10;await f.tick(1000); + expect(f.control.isPaused()).toBe(true);expect(f.control.snapshot().recovery.attempts).toBe(0); + for(let i=0;i<23;i++)await f.tick();expect(f.control.isPaused()).toBe(true);await f.tick();expect(f.control.isPaused()).toBe(false); +}); +it('does not switch an expired PSI certificate to a fresh legacy path at the final fence',async()=>{ + const f=await fixture();await f.tick(0);for(let i=0;i<23;i++)await f.tick(); + f.kv.durability.waitForKeys=async()=>{if(!f.record().paused){f.m.available=7*GiB;f.m.pressure.full.avg10=10;}}; + await f.tick();expect(f.control.snapshot().recovery.attempts).toBe(1);expect(f.control.isPaused()).toBe(true);expect(f.control.snapshot().holds).toContain('memory'); +}); +it('invalidates legacy history at every activation sample even while PSI temporarily qualifies',async()=>{ + const f=await fixture();f.m.available=7*GiB;await f.tick(0);for(let i=0;i<23;i++)await f.tick(); + let sawDip=false,sawReturn=false; + f.kv.durability.wait=async()=>{f.m.available=5*GiB;sawDip=true;return {synced:true,mutationFailures:0};}; + f.kv.durability.waitForKeys=async()=>{if(!f.record().paused){f.m.available=7*GiB;f.m.pressure.full.avg10=10;sawReturn=true;}}; + await f.tick();expect(sawDip).toBe(true);expect(sawReturn).toBe(true);expect(f.control.isPaused()).toBe(true);expect(f.control.snapshot().holds).toContain('memory'); +});