solopup-memory 0.0.0-stage → 0.21.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +453 -0
- package/COMPATIBILITY.md +155 -0
- package/LICENSE +21 -0
- package/README.en.md +62 -0
- package/README.md +105 -2
- package/RELEASE.md +43 -0
- package/SECURITY.md +73 -0
- package/accept.mjs +18 -0
- package/belief-revision.mjs +48 -0
- package/candidates.mjs +355 -0
- package/capture-diagnostics.mjs +77 -0
- package/capture-evidence.mjs +49 -0
- package/capture-inputs.mjs +171 -0
- package/capture-jobs.mjs +195 -0
- package/capture-llm.mjs +299 -0
- package/capture-projections.mjs +7 -0
- package/capture-revision.mjs +13 -0
- package/capture-source-aliases.mjs +20 -0
- package/capture-submission.mjs +75 -0
- package/capture-validation-contract.mjs +30 -0
- package/capture-validation-job.mjs +43 -0
- package/capture-validation.mjs +112 -0
- package/capture.mjs +109 -0
- package/context-history.mjs +123 -0
- package/control-schema.mjs +18 -0
- package/cordis.patch.yml +3 -0
- package/corpus/capture-contract-v25.json +59 -0
- package/corpus/external-labeled.json +187 -0
- package/corpus/holdout-v2.json +217 -0
- package/corpus/holdout-v3.json +145 -0
- package/corpus/injections.json +699 -0
- package/corpus/legacy-self-excluding-dev36.json +7 -0
- package/corpus/legacy-summary-v15.json +12 -0
- package/episodes.mjs +130 -0
- package/eval/mechanical-table.mjs +470 -0
- package/eval/paired-analysis.mjs +47 -0
- package/eval/run-eval.mjs +493 -0
- package/field-lineage.mjs +62 -0
- package/fixture-host.mjs +96 -0
- package/host-services.mjs +8 -0
- package/icon.svg +1 -0
- package/index.mjs +2931 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/managed-access.mjs +182 -0
- package/managed-activation.mjs +36 -0
- package/managed-hooks.mjs +110 -0
- package/managed-logs.mjs +34 -0
- package/managed-policy.mjs +296 -0
- package/meaning.mjs +117 -0
- package/memory-policy.mjs +18 -0
- package/memory-protocol.mjs +21 -0
- package/memory-rule-policy.mjs +11 -0
- package/memory-scope-policy.mjs +7 -0
- package/native-write-validation.mjs +45 -0
- package/package.json +206 -4
- package/plugin-config.mjs +34 -0
- package/project-stores.mjs +222 -0
- package/publication.mjs +92 -0
- package/restoration-lock.mjs +9 -0
- package/restore.mjs +173 -0
- package/retrieval.mjs +43 -0
- package/revision-admission.mjs +6 -0
- package/runtime-startup.mjs +36 -0
- package/scripts/backup.mjs +15 -0
- package/scripts/candidates.mjs +46 -0
- package/scripts/capture-jobs.mjs +41 -0
- package/scripts/control.mjs +25 -0
- package/scripts/patch.mjs +152 -0
- package/scripts/probe-env.mjs +8 -0
- package/scripts/probe-load.mjs +88 -0
- package/scripts/probe-package.mjs +104 -0
- package/scripts/projects.mjs +19 -0
- package/scripts/verify.mjs +81 -0
- package/security.mjs +489 -0
- package/service-readiness.mjs +34 -0
- package/snapshots.mjs +76 -0
- package/source-identity.mjs +29 -0
- package/source-record.mjs +59 -0
- package/source-retirement.mjs +76 -0
- package/store-lock-guard.mjs +4 -0
- package/test-adversarial.mjs +331 -0
- package/test-belief-revision.mjs +166 -0
- package/test-candidates.mjs +147 -0
- package/test-capture-diagnostics.mjs +98 -0
- package/test-capture-field-lineage.mjs +63 -0
- package/test-capture-inputs.mjs +271 -0
- package/test-capture-jobs.mjs +301 -0
- package/test-capture-projections.mjs +78 -0
- package/test-capture-revision.mjs +77 -0
- package/test-capture-submission.mjs +68 -0
- package/test-capture-validation-flow.mjs +47 -0
- package/test-capture-validation-job.mjs +76 -0
- package/test-capture-validation-lineage.mjs +40 -0
- package/test-capture-validation.mjs +84 -0
- package/test-capture.mjs +808 -0
- package/test-compaction-compat.mjs +307 -0
- package/test-compat.mjs +126 -0
- package/test-concurrency.mjs +204 -0
- package/test-conflict.mjs +133 -0
- package/test-context-history.mjs +105 -0
- package/test-control-schema.mjs +26 -0
- package/test-episodes.mjs +157 -0
- package/test-explicit-memory.mjs +43 -0
- package/test-field-lineage.mjs +105 -0
- package/test-history.mjs +305 -0
- package/test-inline-summary.mjs +52 -0
- package/test-legacy-source-identity.mjs +46 -0
- package/test-links.mjs +245 -0
- package/test-managed-access.mjs +114 -0
- package/test-managed-activation.mjs +93 -0
- package/test-meaning.mjs +178 -0
- package/test-native-service-lifecycle.mjs +25 -0
- package/test-native-write-validation.mjs +154 -0
- package/test-packaging.mjs +102 -0
- package/test-paired-analysis.mjs +26 -0
- package/test-patch.mjs +218 -0
- package/test-plugin-contract.mjs +118 -0
- package/test-project-stores.mjs +228 -0
- package/test-publication.mjs +145 -0
- package/test-restore.mjs +127 -0
- package/test-retrieval.mjs +75 -0
- package/test-revision-admission.mjs +23 -0
- package/test-rule-admission.mjs +52 -0
- package/test-runtime-startup.mjs +68 -0
- package/test-schema.mjs +187 -0
- package/test-security.mjs +448 -0
- package/test-service-readiness.mjs +50 -0
- package/test-smell.mjs +201 -0
- package/test-snapshots.mjs +58 -0
- package/test-source-completeness.mjs +44 -0
- package/test-source-key.mjs +17 -0
- package/test-source-retirement.mjs +83 -0
- package/test-status-both.mjs +42 -0
- package/test-supersede.mjs +216 -0
- package/test-text-admission.mjs +82 -0
- package/test-tool-inputs.mjs +13 -0
- package/test-tool-observations.mjs +124 -0
- package/test-tool-write-guard.mjs +23 -0
- package/test-tool-write-lifecycle.mjs +52 -0
- package/test-usability.mjs +588 -0
- package/test-validation-source-refs.mjs +27 -0
- package/test-validation-transition-context.mjs +41 -0
- package/test-withdrawal-archives.mjs +156 -0
- package/test-withdrawal.mjs +167 -0
- package/test.mjs +515 -0
- package/tool-inputs.mjs +68 -0
- package/tool-observations.mjs +115 -0
- package/validation-transition-context.mjs +30 -0
- package/version.mjs +5 -0
- package/withdrawal.mjs +155 -0
- package/worker-write.mjs +13 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"createdBy": "frozen dev36 synthetic writer; read compatibility only",
|
|
3
|
+
"files": {
|
|
4
|
+
"MEMORY.md": "# 记忆索引\n\n## user\n\n- [user] 合成旧数据:探索性草稿先确认试验目标 — 索引摘要\n\n",
|
|
5
|
+
".candidates.json": "{\n \"schema\": 3,\n \"revision\": 1,\n \"records\": [\n {\n \"id\": \"9e7ee4facdff697cf01d59d1d29d19cb0a7b52fa36efc70c5326f86f95a52178\",\n \"type\": \"user\",\n \"scope\": \"global\",\n \"summary\": \"合成旧数据:探索性草稿先确认试验目标\",\n \"meaning\": {\n \"kind\": \"preference\",\n \"conditions\": [\n {\n \"dimension\": \"document_kind\",\n \"value\": \"exploratory_draft\"\n }\n ],\n \"except\": [\n {\n \"dimension\": \"document_kind\",\n \"value\": \"exploratory_draft\"\n }\n ],\n \"alternatives\": [],\n \"falsifiedBy\": []\n },\n \"sessionId\": \"synthetic-seed-d91840cf-f814-4dd0-83b1-4d37cb1928fe\",\n \"evidence\": [\n {\n \"messageId\": \"seed-root\",\n \"hash\": \"3f75b18940b8777d2e71c0d12bf4cec2efad3f85852cda11be47fb5dcd39ef4e\",\n \"quote\": \"Synthetic explicit fixture seed 合成旧数据:探索性草稿先确认试验目标\"\n }\n ],\n \"sources\": [\n \"0d1f1f39cee75ec30dc6525f831e5da6329e125f43b10bbf14d5b584b494bdbb\"\n ],\n \"indexRevision\": \"a4f0266dc8cba5a3c6f478c33259954392deca56de7e7da63dd026e79abaadb2\",\n \"origin\": \"tool-context\",\n \"callHash\": \"be5522d9cd7a8805e8494e151252abb2c3bf47e9a57bd7b8f6383be1b0916586\",\n \"indexLine\": \"- [user] 合成旧数据:探索性草稿先确认试验目标 — 索引摘要\",\n \"status\": \"recorded\",\n \"createdAt\": \"2026-10-30T23:50:12.220Z\"\n }\n ],\n \"retired\": [],\n \"revoked\": [],\n \"rejectedIds\": [],\n \"withdrawnLines\": [],\n \"withdrawnMemoryIds\": [],\n \"withdrawnCandidateIds\": [],\n \"withdrawnBlocks\": [],\n \"managedWithdrawnSources\": []\n}\n"
|
|
6
|
+
}
|
|
7
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"sourceVersion": "0.15.0",
|
|
3
|
+
"candidateId": "33fd1aff300b73b9077435a026e3312cddfd27c0de259fea2b523bd5895ff329",
|
|
4
|
+
"memoryId": "64cdc5fe",
|
|
5
|
+
"files": {
|
|
6
|
+
".audit.jsonl": "{\"at\":\"2026-10-29T02:12:59.405Z\",\"pid\":98430,\"op\":\"write\",\"outcome\":\"ok\",\"type\":\"user\",\"bytes\":89,\"withDetail\":false,\"line\":\"- [user] 合成旧摘要:答复固定先给结论 — user.md\",\"sessionId\":\"legacy-summary-source\",\"via\":\"local-candidate-review\",\"scope\":\"project\",\"evidence\":[{\"messageId\":\"legacy-summary-message\",\"hash\":\"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\",\"quote\":\"本合成测试约定答复先给结论\"}]}\n",
|
|
7
|
+
".candidates.json": "{\n \"schema\": 1,\n \"revision\": 2,\n \"records\": [\n {\n \"id\": \"33fd1aff300b73b9077435a026e3312cddfd27c0de259fea2b523bd5895ff329\",\n \"type\": \"user\",\n \"scope\": \"project\",\n \"summary\": \"合成旧摘要:答复固定先给结论\",\n \"sessionId\": \"legacy-summary-source\",\n \"evidence\": [\n {\n \"messageId\": \"legacy-summary-message\",\n \"hash\": \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\",\n \"quote\": \"本合成测试约定答复先给结论\"\n }\n ],\n \"sources\": [\n \"3124b9ca789eaa09bfebffced1bfee875935bf26063759d34ea950c07c1ed46b\"\n ],\n \"indexRevision\": \"a4f0266dc8cba5a3c6f478c33259954392deca56de7e7da63dd026e79abaadb2\",\n \"status\": \"adopted\",\n \"createdAt\": \"2026-10-29T02:12:59.345Z\",\n \"adoption\": {\n \"memoryId\": \"64cdc5fe\",\n \"reviewedIndex\": \"a4f0266dc8cba5a3c6f478c33259954392deca56de7e7da63dd026e79abaadb2\",\n \"at\": \"2026-10-29T02:12:59.360Z\"\n }\n }\n ],\n \"revoked\": [],\n \"rejectedIds\": [],\n \"withdrawnLines\": [],\n \"withdrawnMemoryIds\": [],\n \"withdrawnCandidateIds\": [],\n \"withdrawnBlocks\": [],\n \"managedWithdrawnSources\": []\n}\n",
|
|
8
|
+
".history.jsonl": "{\"at\":\"2026-10-29T02:12:59.402Z\",\"pid\":98430,\"op\":\"insert\",\"id\":\"64cdc5fe\",\"line\":\"- [user] 合成旧摘要:答复固定先给结论 — user.md\",\"type\":\"user\",\"related\":[],\"sessionId\":\"legacy-summary-source\"}\n",
|
|
9
|
+
"MEMORY.md": "# 记忆索引\n\n## user\n\n- [user] 合成旧摘要:答复固定先给结论 — user.md\n\n"
|
|
10
|
+
},
|
|
11
|
+
"sourceArchiveSha256": "7e956a3dd7b7123c2af3d866dc8acd929c6958285e9721ec7d680c021696e16f"
|
|
12
|
+
}
|
package/episodes.mjs
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/** Bounded record-time history, published with the current projection. Never auto-injected. */
|
|
2
|
+
import {createHash} from 'node:crypto'
|
|
3
|
+
import {join} from 'node:path'
|
|
4
|
+
import {lstatSync} from 'node:fs'
|
|
5
|
+
import {readSafely,normalizeText,escapeFraming,entryId} from './security.mjs'
|
|
6
|
+
import {proposal,toolProposal} from './source-record.mjs'
|
|
7
|
+
|
|
8
|
+
export const EPISODE_FILE='.episodes.json',EPISODE_BYTES=2*1024*1024,EPISODE_LIMIT=1024
|
|
9
|
+
const sha=value=>createHash('sha256').update(JSON.stringify(value)).digest('hex')
|
|
10
|
+
const hex=value=>typeof value==='string'&&/^[a-f0-9]{64}$/.test(value)
|
|
11
|
+
const iso=value=>typeof value==='string'&&/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(value)&&Number.isFinite(Date.parse(value))&&new Date(value).toISOString()===value
|
|
12
|
+
const fail=code=>{const error=new Error('memory-history-'+code);error.code=error.message;throw error}
|
|
13
|
+
const sourced=source=>['tool-context','quoted-extraction'].includes(source?.origin)
|
|
14
|
+
const sourceLine=source=>source.indexLine??`- [${source.type}] ${source.summary} — ${source.detail===undefined?'索引摘要':source.type+'.md'}`
|
|
15
|
+
const lines=text=>new Set(String(text??'').split(/\r?\n/).map(line=>normalizeText(line).trim()).filter(line=>line.startsWith('- [')))
|
|
16
|
+
function sourceProof(input,strict=false){
|
|
17
|
+
if(!input||!iso(input.createdAt))fail('invalid-source-time')
|
|
18
|
+
const result=sourced(input)?toolProposal(input):input.origin==='reviewed-adoption'?{...proposal(input),origin:'reviewed-adoption'}:fail('invalid-source-origin')
|
|
19
|
+
if(result.id!==input.id)fail('invalid-source-identity')
|
|
20
|
+
const canonical={...result,createdAt:input.createdAt}
|
|
21
|
+
if(strict&&JSON.stringify(input)!==JSON.stringify(canonical))fail('corrupt-source-proof')
|
|
22
|
+
return canonical
|
|
23
|
+
}
|
|
24
|
+
function sealEntry(entry){const {checksum,...body}=entry;return{...body,checksum:sha(body)}}
|
|
25
|
+
function entry(input){
|
|
26
|
+
if(!input||!Number.isSafeInteger(input.sequence)||input.sequence<1||!iso(input.recordedAt)||!['published','observed-existing'].includes(input.observation))fail('corrupt-entry')
|
|
27
|
+
const source=sourceProof(input.source,true),line=sourceLine(source)
|
|
28
|
+
if(input.line!==line||input.id!==sha([source.id,input.sequence,input.observation])||input.scope!==source.scope)fail('corrupt-entry-identity')
|
|
29
|
+
const result={id:input.id,sequence:input.sequence,recordedAt:input.recordedAt,observation:input.observation,scope:source.scope,line,source}
|
|
30
|
+
if(input.closed!==undefined){
|
|
31
|
+
const c=input.closed
|
|
32
|
+
if(!c||!Number.isSafeInteger(c.sequence)||c.sequence<input.sequence||!iso(c.at)||c.at<input.recordedAt||!['superseded','forgotten','expired','compacted','over-cap','untracked-removal'].includes(c.reason)||c.by!==undefined&&!hex(c.by)||c.uncertain!==undefined&&c.uncertain!==true||Object.keys(c).some(key=>!['sequence','at','reason','by','uncertain'].includes(key)))fail('corrupt-closure')
|
|
33
|
+
result.closed={sequence:c.sequence,at:c.at,reason:c.reason,...c.by===undefined?{}:{by:c.by},...c.uncertain?{uncertain:true}:{}}
|
|
34
|
+
}
|
|
35
|
+
if(Object.keys(input).sort().join('|')!==[...Object.keys(result),'checksum'].sort().join('|')||input.checksum!==sha(result))fail('corrupt-entry-checksum')
|
|
36
|
+
return{...result,checksum:input.checksum}
|
|
37
|
+
}
|
|
38
|
+
function empty(){return{schema:1,revision:0,latestSequence:0,startedAt:null,lastRecordedAt:null,droppedEntries:0,droppedThroughSequence:0,droppedThroughAt:null,untrackedTransitions:0,entries:[]}}
|
|
39
|
+
export function episodeSnapshot(dir){
|
|
40
|
+
try{const stat=lstatSync(join(dir,EPISODE_FILE));if(!stat.isFile()||stat.isSymbolicLink()||stat.nlink!==1)fail('unsafe-file')}catch(error){if(error.code!=='ENOENT')throw error}
|
|
41
|
+
const raw=readSafely(join(dir,EPISODE_FILE),{maxBytes:EPISODE_BYTES})
|
|
42
|
+
if(raw===undefined)return{exists:false,state:empty(),fingerprint:sha(null),lineNumbers:new Map()}
|
|
43
|
+
let parsed;try{parsed=JSON.parse(raw)}catch{fail('corrupt-json')}
|
|
44
|
+
if(!parsed||typeof parsed!=='object'||Array.isArray(parsed))fail('corrupt-state')
|
|
45
|
+
const {checksum,...state}=parsed;if(!hex(checksum)||checksum!==sha(state))fail('corrupt-state-checksum')
|
|
46
|
+
const keys=Object.keys(empty()).sort().join('|')
|
|
47
|
+
if(Object.keys(state).sort().join('|')!==keys||![1,2].includes(state.schema)||!['revision','latestSequence','droppedEntries','droppedThroughSequence','untrackedTransitions'].every(k=>Number.isSafeInteger(state[k])&&state[k]>=0)||state.droppedThroughSequence>state.latestSequence||!Array.isArray(state.entries)||state.entries.length>EPISODE_LIMIT||state.startedAt!==null&&!iso(state.startedAt)||state.lastRecordedAt!==null&&!iso(state.lastRecordedAt)||state.droppedThroughAt!==null&&!iso(state.droppedThroughAt)||state.latestSequence>0&&(state.startedAt===null||state.lastRecordedAt===null)||state.droppedEntries>0&&state.droppedThroughAt===null)fail('corrupt-state')
|
|
48
|
+
if(state.schema===1&&state.entries.some(row=>row?.source?.fieldLineage!==undefined))fail('field-format-needs-schema2')
|
|
49
|
+
state.entries=state.entries.map(entry);const seen=new Set(),sources=new Set()
|
|
50
|
+
for(const row of state.entries){if(seen.has(row.sequence)||sources.has(row.source.id)||row.sequence>state.latestSequence||row.closed?.sequence>state.latestSequence||state.lastRecordedAt===null||row.recordedAt>state.lastRecordedAt||row.closed?.at>state.lastRecordedAt||state.startedAt===null||row.recordedAt<state.startedAt)fail('corrupt-sequence');seen.add(row.sequence);sources.add(row.source.id)}
|
|
51
|
+
const identities=new Set(state.entries.map(row=>row.id)),lineNumbers=new Map();let offset=0,line=1
|
|
52
|
+
for(const match of raw.matchAll(/"id"\s*:\s*"([a-f0-9]{64})"/g)){line+=raw.slice(offset,match.index).split('\n').length-1;offset=match.index;if(identities.has(match[1])){if(lineNumbers.has(match[1]))fail('ambiguous-entry-location');lineNumbers.set(match[1],line)}}
|
|
53
|
+
if(lineNumbers.size!==state.entries.length)fail('missing-entry-location')
|
|
54
|
+
return{exists:true,state,fingerprint:sha(raw),lineNumbers}
|
|
55
|
+
}
|
|
56
|
+
function serialized(state){return JSON.stringify({...state,checksum:sha(state)},null,2)+'\n'}
|
|
57
|
+
function bounded(state){
|
|
58
|
+
while(state.entries.length>EPISODE_LIMIT||Buffer.byteLength(serialized(state))>EPISODE_BYTES){
|
|
59
|
+
const at=state.entries.findIndex(row=>row.closed!==undefined)
|
|
60
|
+
if(at<0)fail('active-capacity')
|
|
61
|
+
const [removed]=state.entries.splice(at,1);state.droppedEntries++;state.droppedThroughSequence=Math.max(state.droppedThroughSequence,removed.closed.sequence);state.droppedThroughAt=state.droppedThroughAt&&state.droppedThroughAt>removed.closed.at?state.droppedThroughAt:removed.closed.at
|
|
62
|
+
}
|
|
63
|
+
return{name:EPISODE_FILE,text:serialized(state)}
|
|
64
|
+
}
|
|
65
|
+
/** Caller holds publication ownership. One logical sequence per observed transition. */
|
|
66
|
+
export function prepareEpisodes(dir,{before,after,published,baseline=[],revoked=[],reason='forgotten',at=new Date().toISOString()}){
|
|
67
|
+
const snap=episodeSnapshot(dir)
|
|
68
|
+
if(!snap.exists&&!published&&!baseline.length)return undefined
|
|
69
|
+
if(!iso(at))fail('invalid-time')
|
|
70
|
+
const state=snap.state,old=lines(before),next=lines(after),removed=[...old].filter(line=>!next.has(line)),recordedAt=state.lastRecordedAt&&state.lastRecordedAt>at?state.lastRecordedAt:at
|
|
71
|
+
const canonical=published?sourceProof(published.source):undefined
|
|
72
|
+
if(canonical&&sourceLine(canonical)!==published.line)fail('publication-line-mismatch')
|
|
73
|
+
if(canonical?.sources.some(root=>revoked.includes(root)))fail('publication-source-withdrawn')
|
|
74
|
+
const countBefore=state.entries.length;state.entries=state.entries.filter(row=>!row.source.sources.some(root=>revoked.includes(root)))
|
|
75
|
+
const live=state.entries.filter(row=>!row.closed)
|
|
76
|
+
const orphaned=live.filter(row=>!old.has(row.line))
|
|
77
|
+
state.untrackedTransitions+=orphaned.length
|
|
78
|
+
// Observe a traceable pre-upgrade value only at this transition, never invent
|
|
79
|
+
// an earlier interval from a ledger timestamp or old best-effort log.
|
|
80
|
+
for(const line of removed){
|
|
81
|
+
if(state.entries.some(row=>!row.closed&&row.line===line))continue
|
|
82
|
+
const records=baseline.filter(r=>['recorded','adopted'].includes(r.status)&&sourceLine({...r,origin:sourced(r)?r.origin:'reviewed-adoption'})===line)
|
|
83
|
+
if(records.length!==1){state.untrackedTransitions++;continue}
|
|
84
|
+
const source=sourceProof({...records[0],origin:sourced(records[0])?records[0].origin:'reviewed-adoption'}),sequence=++state.latestSequence
|
|
85
|
+
state.entries.push(sealEntry({id:sha([source.id,sequence,'observed-existing']),sequence,recordedAt,observation:'observed-existing',scope:source.scope,line,source}))
|
|
86
|
+
}
|
|
87
|
+
const changed=removed.length>0||canonical!==undefined||orphaned.length>0||countBefore!==state.entries.length
|
|
88
|
+
if(!changed)return undefined
|
|
89
|
+
const sequence=++state.latestSequence
|
|
90
|
+
for(let i=0;i<state.entries.length;i++){
|
|
91
|
+
const row=state.entries[i];if(row.closed||!removed.includes(row.line)&&!orphaned.some(old=>old.id===row.id))continue
|
|
92
|
+
const unknown=orphaned.some(old=>old.id===row.id)
|
|
93
|
+
state.entries[i]=sealEntry({...row,closed:{sequence,at:recordedAt,reason:unknown?'untracked-removal':reason,...canonical&&!unknown?{by:canonical.id}:{},...unknown?{uncertain:true}:{}}})
|
|
94
|
+
}
|
|
95
|
+
if(canonical){
|
|
96
|
+
if(state.entries.some(row=>row.source.id===canonical.id))fail('duplicate-publication')
|
|
97
|
+
state.entries.push(sealEntry({id:sha([canonical.id,sequence,'published']),sequence,recordedAt,observation:'published',scope:canonical.scope,line:published.line,source:canonical}))
|
|
98
|
+
}
|
|
99
|
+
if(state.entries.some(row=>row.source.fieldLineage))state.schema=2
|
|
100
|
+
state.startedAt??=recordedAt;state.lastRecordedAt=recordedAt;state.revision++
|
|
101
|
+
return bounded(state)
|
|
102
|
+
}
|
|
103
|
+
/** Whole tracked historical entries depend on every contributing source root. */
|
|
104
|
+
export function purgeEpisodes(dir,source){
|
|
105
|
+
if(!hex(source))fail('invalid-source-key')
|
|
106
|
+
const snap=episodeSnapshot(dir);if(!snap.exists)return undefined
|
|
107
|
+
const kept=snap.state.entries.filter(row=>!row.source.sources.includes(source))
|
|
108
|
+
if(kept.length===snap.state.entries.length)return undefined
|
|
109
|
+
snap.state.entries=kept;snap.state.revision++
|
|
110
|
+
return bounded(snap.state)
|
|
111
|
+
}
|
|
112
|
+
export function historyRows(dir,{scope,index,revoked=[],atSequence,asOf,expiredAt=()=>false}={}){
|
|
113
|
+
if(atSequence!==undefined&&(!Number.isSafeInteger(atSequence)||atSequence<1)||asOf!==undefined&&!iso(asOf)||atSequence!==undefined&&asOf!==undefined)fail('invalid-cutoff')
|
|
114
|
+
const snap=episodeSnapshot(dir),s=snap.state,current=lines(index)
|
|
115
|
+
if(s.entries.some(row=>scope!==undefined&&row.scope!==scope))fail('scope-mismatch')
|
|
116
|
+
if(atSequence!==undefined&&atSequence>s.latestSequence)fail('unknown-future-sequence')
|
|
117
|
+
const allowed=s.entries.filter(row=>!row.source.sources.some(root=>revoked.includes(root)))
|
|
118
|
+
const untrackedCurrentRows=[...current].filter(line=>!allowed.some(row=>!row.closed&&row.line===line)).length
|
|
119
|
+
const unreconciled=allowed.filter(row=>!row.closed&&!current.has(row.line))
|
|
120
|
+
const cutoff=atSequence??s.latestSequence
|
|
121
|
+
const times=s.entries.flatMap(row=>[...(row.sequence<=cutoff?[row.recordedAt]:[]),...(row.closed&&row.closed.sequence<=cutoff?[row.closed.at]:[])])
|
|
122
|
+
const cutoffTime=asOf??(atSequence!==undefined?times.sort().at(-1):new Date().toISOString())
|
|
123
|
+
const selected=allowed.filter(row=>(atSequence!==undefined?row.sequence<=atSequence&&(!row.closed||row.closed.sequence>atSequence):asOf!==undefined?row.recordedAt<=asOf&&(!row.closed||row.closed.at>asOf):true)&&(!(atSequence!==undefined||asOf!==undefined)||!expiredAt(row.line,Date.parse(cutoffTime))))
|
|
124
|
+
const covered=atSequence!==undefined?cutoff>s.droppedThroughSequence:asOf!==undefined?(s.startedAt!==null&&asOf>=s.startedAt&&(!s.droppedThroughAt||asOf>s.droppedThroughAt)):s.droppedEntries===0
|
|
125
|
+
const coverage={exists:snap.exists,scope:scope??'unspecified',startedAt:s.startedAt,latestSequence:s.latestSequence,droppedEntries:s.droppedEntries,droppedThroughSequence:s.droppedThroughSequence,droppedThroughAt:s.droppedThroughAt,untrackedCurrentRows,untrackedTransitions:s.untrackedTransitions,unreconciledEntries:unreconciled.length,withdrawalBarriersPresent:revoked.length>0,complete:snap.exists&&covered&&untrackedCurrentRows===0&&s.untrackedTransitions===0&&unreconciled.length===0}
|
|
126
|
+
return{coverage,rows:selected.map(row=>({row,line:snap.lineNumbers.get(row.id),history:{sequence:row.sequence,recordedAt:row.recordedAt,observation:row.observation,state:unreconciled.some(old=>old.id===row.id)?'unreconciled':row.closed?.uncertain?'uncertain-interval':atSequence!==undefined||asOf!==undefined?'active-at-cutoff':row.closed?row.closed.reason:expiredAt(row.line,Date.parse(cutoffTime))?'expired':'current',sourceSessionId:row.source.sessionId,sourceMessageIds:row.source.evidence.map(e=>e.messageId),...row.closed?{closedAt:row.closed.at,closedAtSequence:row.closed.sequence}:{}}}))}
|
|
127
|
+
}
|
|
128
|
+
const nullableTime={oneOf:[{type:'string'},{type:'null'}]}
|
|
129
|
+
export const HISTORY_SCHEMA={type:'object',additionalProperties:false,required:['sequence','recordedAt','observation','state','sourceSessionId','sourceMessageIds'],properties:{sequence:{type:'integer'},recordedAt:{type:'string'},observation:{type:'string',enum:['published','observed-existing']},state:{type:'string'},sourceSessionId:{type:'string'},sourceMessageIds:{type:'array',items:{type:'string'}},closedAt:{type:'string'},closedAtSequence:{type:'integer'}}}
|
|
130
|
+
export const COVERAGE_SCHEMA={type:'array',items:{type:'object',additionalProperties:false,required:['exists','scope','startedAt','latestSequence','droppedEntries','droppedThroughSequence','droppedThroughAt','untrackedCurrentRows','untrackedTransitions','unreconciledEntries','withdrawalBarriersPresent','complete'],properties:{exists:{type:'boolean'},scope:{type:'string'},startedAt:nullableTime,latestSequence:{type:'integer'},droppedEntries:{type:'integer'},droppedThroughSequence:{type:'integer'},droppedThroughAt:nullableTime,untrackedCurrentRows:{type:'integer'},untrackedTransitions:{type:'integer'},unreconciledEntries:{type:'integer'},withdrawalBarriersPresent:{type:'boolean'},complete:{type:'boolean'}}}}
|
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 机械指标表:只测「不需要模型」的那几行。
|
|
4
|
+
*
|
|
5
|
+
* node eval/mechanical-table.mjs [--json]
|
|
6
|
+
*
|
|
7
|
+
* 对齐 LongMemEval 那套评估表里可以离线算的部分:
|
|
8
|
+
* 上下文税、检索可见性(H@1 的近似)、重复率、冲突率、过期残留、写入闸误记。
|
|
9
|
+
* 测不了的行(捕获召回、五种能力、编造率)在表里标 N/A 并写明原因——
|
|
10
|
+
* 空着比编一个数诚实。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { readFileSync, mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
14
|
+
import { join, dirname } from 'node:path'
|
|
15
|
+
import { tmpdir } from 'node:os'
|
|
16
|
+
import { fileURLToPath } from 'node:url'
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
apply,
|
|
20
|
+
checkIndexBudget,
|
|
21
|
+
pruneStore,
|
|
22
|
+
indexLine,
|
|
23
|
+
searchStore,
|
|
24
|
+
similarity,
|
|
25
|
+
statusOf,
|
|
26
|
+
storeDirFor,
|
|
27
|
+
writeMemory,
|
|
28
|
+
forgetMemory,
|
|
29
|
+
} from '../index.mjs'
|
|
30
|
+
|
|
31
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
32
|
+
const LIMIT_BYTES = 25 * 1024
|
|
33
|
+
const INSTRUCTION_BUDGET = 64 * 1024
|
|
34
|
+
|
|
35
|
+
function sandbox() {
|
|
36
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-mech-'))
|
|
37
|
+
const home = join(root, 'home')
|
|
38
|
+
const cwd = join(root, 'proj')
|
|
39
|
+
mkdirSync(home, { recursive: true })
|
|
40
|
+
mkdirSync(cwd, { recursive: true })
|
|
41
|
+
const prev = process.env.DSH_HOME
|
|
42
|
+
process.env.DSH_HOME = home
|
|
43
|
+
return {
|
|
44
|
+
cwd,
|
|
45
|
+
cleanup() {
|
|
46
|
+
if (prev === undefined) delete process.env.DSH_HOME
|
|
47
|
+
else process.env.DSH_HOME = prev
|
|
48
|
+
rmSync(root, { recursive: true, force: true })
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function mount(cwd) {
|
|
54
|
+
const captured = { cwd, tools: [] }
|
|
55
|
+
apply({
|
|
56
|
+
systemPrompt: { context: (e) => (captured.context = e) },
|
|
57
|
+
workingDirectory: { get: () => captured.cwd },
|
|
58
|
+
tools: { register: (definition) => captured.tools.push(definition) },
|
|
59
|
+
on() {},
|
|
60
|
+
})
|
|
61
|
+
return captured
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const render = (captured) => captured.context.text({ agent: { session: {} } })
|
|
65
|
+
|
|
66
|
+
// ── 1. 上下文税 ────────────────────────────────────────────────────────────
|
|
67
|
+
function contextTax() {
|
|
68
|
+
const rows = []
|
|
69
|
+
for (const size of [10, 50, 150, 200, 300, 500]) {
|
|
70
|
+
const box = sandbox()
|
|
71
|
+
try {
|
|
72
|
+
const dir = storeDirFor('global', box.cwd)
|
|
73
|
+
mkdirSync(dir, { recursive: true })
|
|
74
|
+
const lines = []
|
|
75
|
+
for (let i = 0; i < size - 1; i += 1) lines.push(`- [user] 条目 ${i} 的一句话摘要 — user.md`)
|
|
76
|
+
lines.splice(Math.floor(lines.length / 2), 0, '- [reference] 目标条目 TARGET — reference.md')
|
|
77
|
+
writeFileSync(join(dir, 'MEMORY.md'), `# 索引\n\n## user\n\n${lines.join('\n')}\n`, 'utf8')
|
|
78
|
+
const text = render(mount(box.cwd))
|
|
79
|
+
const bytes = Buffer.byteLength(text, 'utf8')
|
|
80
|
+
rows.push({
|
|
81
|
+
size,
|
|
82
|
+
bytes,
|
|
83
|
+
pctOfIndexCap: Number(((bytes / LIMIT_BYTES) * 100).toFixed(1)),
|
|
84
|
+
pctOfInstructionBudget: Number(((bytes / INSTRUCTION_BUDGET) * 100).toFixed(1)),
|
|
85
|
+
})
|
|
86
|
+
} finally {
|
|
87
|
+
box.cleanup()
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return rows
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ── 2. 位置可见性(H@1 的机械近似)────────────────────────────────────────
|
|
94
|
+
function positionVisibility() {
|
|
95
|
+
const size = 300
|
|
96
|
+
const positions = [1, 50, 100, 150, 200, 201, 250, 300]
|
|
97
|
+
const results = []
|
|
98
|
+
for (const position of positions) {
|
|
99
|
+
const box = sandbox()
|
|
100
|
+
try {
|
|
101
|
+
const dir = storeDirFor('global', box.cwd)
|
|
102
|
+
mkdirSync(dir, { recursive: true })
|
|
103
|
+
const lines = []
|
|
104
|
+
for (let i = 0; i < size; i += 1) {
|
|
105
|
+
lines.push(i + 1 === position ? '- [reference] 目标条目 TARGET — reference.md' : `- [user] 条目 ${i} — user.md`)
|
|
106
|
+
}
|
|
107
|
+
writeFileSync(join(dir, 'MEMORY.md'), `# 索引\n\n## user\n\n${lines.join('\n')}\n`, 'utf8')
|
|
108
|
+
const text = render(mount(box.cwd))
|
|
109
|
+
results.push({ position, visible: text.includes('TARGET') })
|
|
110
|
+
} finally {
|
|
111
|
+
box.cleanup()
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return results
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ── 3. 重复率:按相似度分档看闸门行为 ──────────────────────────────────────
|
|
118
|
+
function duplicateByBand() {
|
|
119
|
+
const base = 'E01 全链路已跑完,下一步 S3 场景卡'
|
|
120
|
+
const candidates = [
|
|
121
|
+
{ label: '完全相同', text: base },
|
|
122
|
+
{ label: '仅差句号', text: `${base}。` },
|
|
123
|
+
{ label: '改一个词', text: 'E01 全链路已跑完,接下来 S3 场景卡' },
|
|
124
|
+
{ label: '换说法', text: '第一步先把场景卡做完再进稿子' },
|
|
125
|
+
]
|
|
126
|
+
const rows = []
|
|
127
|
+
for (const candidate of candidates) {
|
|
128
|
+
const box = sandbox()
|
|
129
|
+
try {
|
|
130
|
+
const dir = storeDirFor('global', box.cwd)
|
|
131
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', base) })
|
|
132
|
+
let outcome
|
|
133
|
+
try {
|
|
134
|
+
const r = writeMemory({ type: 'project', dir, line: indexLine('project', candidate.text) })
|
|
135
|
+
outcome = r.duplicate ? 'duplicate(幂等,不入库)' : r.warning ? 'warning(入库但提示)' : 'accepted(当作新条目)'
|
|
136
|
+
} catch (error) {
|
|
137
|
+
outcome = /相似/.test(error.message) ? 'rejected(相似度闸)' : `error:${error.message.slice(0, 20)}`
|
|
138
|
+
}
|
|
139
|
+
rows.push({
|
|
140
|
+
label: candidate.label,
|
|
141
|
+
similarity: Number(similarity(base, candidate.text).toFixed(3)),
|
|
142
|
+
outcome,
|
|
143
|
+
})
|
|
144
|
+
} finally {
|
|
145
|
+
box.cleanup()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return rows
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ── 4. 冲突率:同一事实两个值 ──────────────────────────────────────────────
|
|
152
|
+
function conflictRate() {
|
|
153
|
+
const cases = [
|
|
154
|
+
['集数', '剧本定为 26 集', '剧本定为 24 集'],
|
|
155
|
+
['场次', 'E01 是 16 场', 'E01 是 12 场'],
|
|
156
|
+
['模型', '写手用 deepseek-flash', '写手用 gemini-4-argon'],
|
|
157
|
+
]
|
|
158
|
+
const rows = []
|
|
159
|
+
for (const [name, a, b] of cases) {
|
|
160
|
+
const box = sandbox()
|
|
161
|
+
try {
|
|
162
|
+
const dir = storeDirFor('global', box.cwd)
|
|
163
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', a) })
|
|
164
|
+
let outcome
|
|
165
|
+
try {
|
|
166
|
+
const r = writeMemory({ type: 'project', dir, line: indexLine('project', b) })
|
|
167
|
+
outcome = r.duplicate ? 'duplicate' : /冲突/.test(r.warning ?? '') ? '⚠ 冲突警告' : r.warning ? 'warning' : 'accepted(未察觉)'
|
|
168
|
+
} catch (error) {
|
|
169
|
+
outcome = /相似/.test(error.message) ? 'rejected(疑似同一条)' : 'error'
|
|
170
|
+
}
|
|
171
|
+
const index = readFileSync(join(dir, 'MEMORY.md'), 'utf8')
|
|
172
|
+
rows.push({
|
|
173
|
+
name,
|
|
174
|
+
similarity: Number(similarity(a, b).toFixed(3)),
|
|
175
|
+
outcome,
|
|
176
|
+
bothPresent: index.includes(a) && index.includes(b),
|
|
177
|
+
})
|
|
178
|
+
} finally {
|
|
179
|
+
box.cleanup()
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return rows
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ── 5. 过期与取代的残留 ────────────────────────────────────────────────────
|
|
186
|
+
function staleResidual() {
|
|
187
|
+
const box = sandbox()
|
|
188
|
+
try {
|
|
189
|
+
const dir = storeDirFor('global', box.cwd)
|
|
190
|
+
// 取代:旧值应完全消失
|
|
191
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '剧本定为 26 集') })
|
|
192
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '剧本定为 24 集'), supersedes: '26 集' })
|
|
193
|
+
const afterSupersede = render(mount(box.cwd))
|
|
194
|
+
|
|
195
|
+
// 过期:到期条目不应出现
|
|
196
|
+
const yesterday = new Date(Date.now() - 2 * 86400000)
|
|
197
|
+
const stamp = `${yesterday.getFullYear()}-${String(yesterday.getMonth() + 1).padStart(2, '0')}-${String(yesterday.getDate()).padStart(2, '0')}`
|
|
198
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '限期条目 EXPIRE-ME', stamp) })
|
|
199
|
+
const afterExpiry = render(mount(box.cwd))
|
|
200
|
+
|
|
201
|
+
// 删除:删掉的不能残留
|
|
202
|
+
const forgetTarget = indexLine('project', '临时条目 DELETE-ME')
|
|
203
|
+
writeMemory({ type: 'project', dir, line: forgetTarget })
|
|
204
|
+
forgetMemory(dir, 'DELETE-ME')
|
|
205
|
+
const afterForget = render(mount(box.cwd))
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
supersedeOldGone: !afterSupersede.includes('26 集'),
|
|
209
|
+
supersedeNewPresent: afterSupersede.includes('24 集'),
|
|
210
|
+
expiredHidden: !afterExpiry.includes('EXPIRE-ME'),
|
|
211
|
+
expiryNotice: afterExpiry.includes('已隐藏'),
|
|
212
|
+
deletedGone: !afterForget.includes('DELETE-ME'),
|
|
213
|
+
}
|
|
214
|
+
} finally {
|
|
215
|
+
box.cleanup()
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ── 6. 写入闸的误记率(闸门行为,不是模型的判断)───────────────────────────
|
|
220
|
+
async function gateRejections() {
|
|
221
|
+
// 走真实工具入口:参数校验在 resolveTarget 里,直接调 writeMemory 会绕过它
|
|
222
|
+
const box = sandbox()
|
|
223
|
+
try {
|
|
224
|
+
const captured = mount(box.cwd)
|
|
225
|
+
const tool = captured.tools.find((item) => item.name === 'memory_write')
|
|
226
|
+
const exec = { agent: { session: { id: 'mech' }, options: { model: 'mech' } } }
|
|
227
|
+
const traps = [
|
|
228
|
+
['凭据(sk-…)', { type: 'user', summary: `sk-${'a'.repeat(32)}`, scope: 'global' }],
|
|
229
|
+
['超长摘要(260 字)', { type: 'user', summary: 'x'.repeat(260), scope: 'global' }],
|
|
230
|
+
['空摘要', { type: 'user', summary: ' ', scope: 'global' }],
|
|
231
|
+
['非法 type', { type: 'nope', summary: 'x', scope: 'global' }],
|
|
232
|
+
]
|
|
233
|
+
const results = []
|
|
234
|
+
for (const [name, args] of traps) {
|
|
235
|
+
try {
|
|
236
|
+
await tool.execute(args, exec)
|
|
237
|
+
results.push({ name, caught: false })
|
|
238
|
+
} catch {
|
|
239
|
+
results.push({ name, caught: true })
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return results
|
|
243
|
+
} finally {
|
|
244
|
+
box.cleanup()
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// ── 7. 写入死路率(U1)────────────────────────────────────────────────────
|
|
249
|
+
function deadEnds() {
|
|
250
|
+
const box = sandbox()
|
|
251
|
+
try {
|
|
252
|
+
const dir = storeDirFor('global', box.cwd)
|
|
253
|
+
mkdirSync(dir, { recursive: true })
|
|
254
|
+
const lines = ['# 索引', '', '## project', '']
|
|
255
|
+
for (let i = 0; i < 400; i += 1) lines.push(`- [project] 占位${String.fromCharCode(97 + (i % 26))}${i} — project.md`)
|
|
256
|
+
writeFileSync(join(dir, 'MEMORY.md'), `${lines.join('\n')}\n`, 'utf8')
|
|
257
|
+
|
|
258
|
+
let rejected = 0
|
|
259
|
+
let recovered = 0
|
|
260
|
+
try {
|
|
261
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '满库时的新条目') })
|
|
262
|
+
} catch (error) {
|
|
263
|
+
rejected += 1
|
|
264
|
+
// 报错里必须给出可执行的下一步
|
|
265
|
+
const actionable = /memory_prune/.test(error.message)
|
|
266
|
+
if (actionable) {
|
|
267
|
+
pruneStore(dir, { apply: true })
|
|
268
|
+
try {
|
|
269
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '满库时的新条目') })
|
|
270
|
+
recovered = 1
|
|
271
|
+
} catch {
|
|
272
|
+
recovered = 0
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return { rejected, recovered }
|
|
277
|
+
} finally {
|
|
278
|
+
box.cleanup()
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// ── 8. 截断召回率(U2)────────────────────────────────────────────────────
|
|
283
|
+
function truncationRecall() {
|
|
284
|
+
const box = sandbox()
|
|
285
|
+
try {
|
|
286
|
+
const dir = storeDirFor('global', box.cwd)
|
|
287
|
+
mkdirSync(dir, { recursive: true })
|
|
288
|
+
const lines = ['# 索引', '', '## project', '']
|
|
289
|
+
for (let i = 0; i < 400; i += 1) {
|
|
290
|
+
lines.push(i === 5 ? '- [project] 被截断的稀有事实 ZEBRA — project.md' : `- [project] 条目${i} — project.md`)
|
|
291
|
+
}
|
|
292
|
+
writeFileSync(join(dir, 'MEMORY.md'), `${lines.join('\n')}\n`, 'utf8')
|
|
293
|
+
|
|
294
|
+
const injected = render(mount(box.cwd))
|
|
295
|
+
const visibleInContext = injected.includes('ZEBRA')
|
|
296
|
+
const found = searchStore(dir, 'ZEBRA').hits.length
|
|
297
|
+
return { visibleInContext, foundBySearch: found }
|
|
298
|
+
} finally {
|
|
299
|
+
box.cleanup()
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// ── 9. 压缩效果(U3)───────────────────────────────────────────────────────
|
|
304
|
+
function compactionEffect() {
|
|
305
|
+
const box = sandbox()
|
|
306
|
+
try {
|
|
307
|
+
const dir = storeDirFor('global', box.cwd)
|
|
308
|
+
mkdirSync(dir, { recursive: true })
|
|
309
|
+
const lines = ['# 索引', '', '## project', '']
|
|
310
|
+
for (let i = 0; i < 340; i += 1) {
|
|
311
|
+
const suffix = i < 40 ? ' (exp:2020-01-01)' : ''
|
|
312
|
+
lines.push(`- [project] 占位${String.fromCharCode(97 + (i % 26))}${i} — project.md${suffix}`)
|
|
313
|
+
}
|
|
314
|
+
lines.push('- [project] 剧本定为26集 — project.md')
|
|
315
|
+
writeFileSync(join(dir, 'MEMORY.md'), `${lines.join('\n')}\n`, 'utf8')
|
|
316
|
+
|
|
317
|
+
const before = statusOf(dir)
|
|
318
|
+
const plan = pruneStore(dir, { apply: true })
|
|
319
|
+
const after = statusOf(dir)
|
|
320
|
+
return {
|
|
321
|
+
beforeLines: before.index.lines,
|
|
322
|
+
afterLines: after.index.lines,
|
|
323
|
+
expired: plan.removed.expired,
|
|
324
|
+
overCap: plan.removed.overCap,
|
|
325
|
+
underLimit: after.index.lines <= 300,
|
|
326
|
+
}
|
|
327
|
+
} finally {
|
|
328
|
+
box.cleanup()
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const report = {
|
|
333
|
+
at: new Date().toISOString(),
|
|
334
|
+
contextTax: contextTax(),
|
|
335
|
+
positionVisibility: positionVisibility(),
|
|
336
|
+
duplicateByBand: duplicateByBand(),
|
|
337
|
+
conflictRate: conflictRate(),
|
|
338
|
+
staleResidual: staleResidual(),
|
|
339
|
+
gateRejections: await gateRejections(),
|
|
340
|
+
deadEnds: deadEnds(),
|
|
341
|
+
truncationRecall: truncationRecall(),
|
|
342
|
+
compactionEffect: compactionEffect(),
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (process.argv.includes('--json')) {
|
|
346
|
+
console.log(JSON.stringify(report, null, 2))
|
|
347
|
+
process.exit(0)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// 门禁:只卡可用性三条(U1/U2/U3),不卡"数字好不好看"
|
|
351
|
+
if (process.argv.includes('--check')) {
|
|
352
|
+
const checks = []
|
|
353
|
+
const deadEndFree = report.deadEnds.rejected === 0 || report.deadEnds.recovered === 1
|
|
354
|
+
checks.push(['U1 满库压缩后恢复可写', deadEndFree])
|
|
355
|
+
checks.push(['U2 截断条目可检索', report.truncationRecall.foundBySearch > 0])
|
|
356
|
+
checks.push(['U3 压缩后不超上限', report.compactionEffect.underLimit])
|
|
357
|
+
let failed = 0
|
|
358
|
+
for (const [label, ok] of checks) {
|
|
359
|
+
if (!ok) failed += 1
|
|
360
|
+
console.log(`[${ok ? 'PASS' : 'FAIL'}] ${label}`)
|
|
361
|
+
}
|
|
362
|
+
process.exit(failed === 0 ? 0 : 1)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const lines = []
|
|
366
|
+
lines.push(`# 机械指标表(对照评估表里可离线算的行)`)
|
|
367
|
+
lines.push('')
|
|
368
|
+
lines.push(`生成时间:${report.at}`)
|
|
369
|
+
lines.push('')
|
|
370
|
+
lines.push('## 1. 上下文税')
|
|
371
|
+
lines.push('')
|
|
372
|
+
lines.push('| 库规模 | 注入字节 | 占索引上限 25 KB | 占 DSH 指令预算 64 KiB |')
|
|
373
|
+
lines.push('|---|---|---|---|')
|
|
374
|
+
for (const row of report.contextTax) {
|
|
375
|
+
lines.push(`| ${row.size} 条 | ${row.bytes} B | ${row.pctOfIndexCap}% | ${row.pctOfInstructionBudget}% |`)
|
|
376
|
+
}
|
|
377
|
+
lines.push('')
|
|
378
|
+
lines.push('## 2. 位置可见性(H@1 的机械近似:目标是否出现在注入里)')
|
|
379
|
+
lines.push('')
|
|
380
|
+
lines.push('| 目标位置(共 300 条) | 出现在注入里 |')
|
|
381
|
+
lines.push('|---|---|')
|
|
382
|
+
for (const row of report.positionVisibility) {
|
|
383
|
+
lines.push(`| 第 ${row.position} 条 | ${row.visible ? '是' : '否'} |`)
|
|
384
|
+
}
|
|
385
|
+
lines.push('')
|
|
386
|
+
lines.push('## 3. 重复率(按相似度分档看闸门行为)')
|
|
387
|
+
lines.push('')
|
|
388
|
+
lines.push('| 情形 | 相似度 | 结果 |')
|
|
389
|
+
lines.push('|---|---|---|')
|
|
390
|
+
for (const row of report.duplicateByBand) lines.push(`| ${row.label} | ${row.similarity} | ${row.outcome} |`)
|
|
391
|
+
lines.push('')
|
|
392
|
+
lines.push('## 4. 冲突率(同一事实两个值)')
|
|
393
|
+
lines.push('')
|
|
394
|
+
lines.push('| 事实 | 相似度 | 结果 | 两条都入库 |')
|
|
395
|
+
lines.push('|---|---|---|---|')
|
|
396
|
+
for (const row of report.conflictRate) {
|
|
397
|
+
lines.push(`| ${row.name} | ${row.similarity} | ${row.outcome} | ${row.bothPresent ? '是' : '否'} |`)
|
|
398
|
+
}
|
|
399
|
+
lines.push('')
|
|
400
|
+
lines.push('## 5. 过期与取代的残留')
|
|
401
|
+
lines.push('')
|
|
402
|
+
const residual = report.staleResidual
|
|
403
|
+
lines.push(`| 检查 | 结果 |`)
|
|
404
|
+
lines.push('|---|---|')
|
|
405
|
+
lines.push(`| 取代后旧值消失 | ${residual.supersedeOldGone ? '是' : '否'} |`)
|
|
406
|
+
lines.push(`| 取代后新值在场 | ${residual.supersedeNewPresent ? '是' : '否'} |`)
|
|
407
|
+
lines.push(`| 过期条目被隐藏 | ${residual.expiredHidden ? '是' : '否'} |`)
|
|
408
|
+
lines.push(`| 过期有提示 | ${residual.expiryNotice ? '是' : '否'} |`)
|
|
409
|
+
lines.push(`| 删除后无残留 | ${residual.deletedGone ? '是' : '否'} |`)
|
|
410
|
+
lines.push('')
|
|
411
|
+
lines.push('## 6. 写入闸(这是闸门行为,不是模型判断)')
|
|
412
|
+
lines.push('')
|
|
413
|
+
lines.push('| 陷阱 | 是否被拦 |')
|
|
414
|
+
lines.push('|---|---|')
|
|
415
|
+
for (const row of report.gateRejections) lines.push(`| ${row.name} | ${row.caught ? '是' : '否'} |`)
|
|
416
|
+
lines.push('')
|
|
417
|
+
lines.push('## 7. 可用性(U1–U3)')
|
|
418
|
+
lines.push('')
|
|
419
|
+
lines.push('| 指标 | 实测 | 验收线 |')
|
|
420
|
+
lines.push('|---|---|---|')
|
|
421
|
+
lines.push(`| 写入死路率 | ${report.deadEnds.rejected === 0 ? 0 : ((report.deadEnds.rejected - report.deadEnds.recovered) / report.deadEnds.rejected) * 100}% | 0% |`)
|
|
422
|
+
lines.push(`| 满库一次压缩后恢复可写 | ${report.deadEnds.recovered === 1 ? '是' : '否'} | 是 |`)
|
|
423
|
+
lines.push(`| 截断条目可检索 | ${report.truncationRecall.foundBySearch > 0 ? '是' : '否'}(注入中${report.truncationRecall.visibleInContext ? '可见' : '不可见'}) | 是 |`)
|
|
424
|
+
lines.push(`| 压缩后索引行数 | ${report.compactionEffect.beforeLines} → ${report.compactionEffect.afterLines} | ≤300 |`)
|
|
425
|
+
lines.push('')
|
|
426
|
+
lines.push('## 8. 这张表覆盖不到的行(需要活体 agent)')
|
|
427
|
+
lines.push('')
|
|
428
|
+
lines.push('| 评估表的行 | 为什么测不了 |')
|
|
429
|
+
lines.push('|---|---|')
|
|
430
|
+
lines.push('| 捕获召回率 | 要模型自己决定「什么值得记」,插件只提供写入闸 |')
|
|
431
|
+
lines.push('| 五种能力(提取/跨会话/时间/更新/拒答) | 每题都要新会话里由模型作答 |')
|
|
432
|
+
lines.push('| 编造率 / 错误自信率 | 同上,且判分需要盲评 |')
|
|
433
|
+
lines.push('| 检索首次命中率(真正意义上的) | 需要数「第几次工具调用命中」,不是「是否在注入里」 |')
|
|
434
|
+
lines.push('| 净收益(时间) | 需要真实任务计时 |')
|
|
435
|
+
|
|
436
|
+
// ── 结论:从数据里算出来,不手写数字 ──
|
|
437
|
+
const visiblePositions = report.positionVisibility.filter((row) => row.visible).map((row) => row.position)
|
|
438
|
+
const maxVisible = visiblePositions.length > 0 ? Math.max(...visiblePositions) : 0
|
|
439
|
+
const hiddenPositions = report.positionVisibility.filter((row) => !row.visible).map((row) => row.position)
|
|
440
|
+
const conflictLeaked = report.conflictRate.filter((row) => row.bothPresent).length
|
|
441
|
+
const paraphrase = report.duplicateByBand.find((row) => row.label === '改一个词')
|
|
442
|
+
const verbatim = report.duplicateByBand.find((row) => row.label === '完全相同')
|
|
443
|
+
|
|
444
|
+
lines.push('## 9. 这张表暴露的问题')
|
|
445
|
+
lines.push('')
|
|
446
|
+
// 方向从数据里判断,不写死:隐藏位置全在可见位置之前 = 保最新;之后 = 保最旧
|
|
447
|
+
const direction =
|
|
448
|
+
hiddenPositions.length === 0
|
|
449
|
+
? '没有条目被丢弃'
|
|
450
|
+
: Math.max(...hiddenPositions) < Math.min(...visiblePositions)
|
|
451
|
+
? '丢弃的是**最早的**(保最新)'
|
|
452
|
+
: Math.min(...hiddenPositions) > Math.max(...visiblePositions)
|
|
453
|
+
? '丢弃的是**最新的**(保最旧)'
|
|
454
|
+
: '丢弃的是**中间段**'
|
|
455
|
+
lines.push(`1. **有效容量 ${maxVisible} 条**(第 ${hiddenPositions.join('、')} 条不可见)。丢弃方向:${direction}。`)
|
|
456
|
+
const warned = report.conflictRate.filter((row) => /冲突/.test(row.outcome)).length
|
|
457
|
+
lines.push(
|
|
458
|
+
`2. **冲突会被提示,但不会被拦。** ${report.conflictRate.length} 组「同一事实两个值」里 ${warned} 组给出了警告,` +
|
|
459
|
+
`${conflictLeaked} 组两条都入库。这是刻意的:文本规则分不清「改口」和「枚举」,拦截会误伤后者,` +
|
|
460
|
+
`所以把判断交给看到上下文的人。`,
|
|
461
|
+
)
|
|
462
|
+
lines.push(`3. **同义改写会被当成新条目。** 逐字重复能挡住(${verbatim.outcome}),但相似度 ${paraphrase.similarity} 的改写直接入库——索引会被同一件事的多种说法慢慢稀释。`)
|
|
463
|
+
lines.push('')
|
|
464
|
+
lines.push('这三条都不是"安全漏洞",是**质量上限**:机械指标能测出来,说明它们可修;')
|
|
465
|
+
lines.push('修之前,任何基于这张表的横向比较都会把这三个缺陷一起带进去。')
|
|
466
|
+
lines.push('')
|
|
467
|
+
|
|
468
|
+
const markdown = `${lines.join('\n')}\n`
|
|
469
|
+
writeFileSync(join(HERE, 'mechanical-table.md'), markdown, 'utf8')
|
|
470
|
+
console.log(markdown)
|