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,115 @@
|
|
|
1
|
+
/** Host-observed execution output. A checked quote is not a verified world fact. */
|
|
2
|
+
import {createHash} from 'node:crypto'
|
|
3
|
+
import {normalizeText,escapeFraming,findSecrets} from './security.mjs'
|
|
4
|
+
const hash=value=>createHash('sha256').update(JSON.stringify(value)).digest('hex')
|
|
5
|
+
const hex=value=>typeof value==='string'&&/^[a-f0-9]{64}$/.test(value)
|
|
6
|
+
const id=value=>typeof value==='string'&&value.length>0&&value.length<=300&&!/[\u0000-\u001f]/.test(value)
|
|
7
|
+
const seq=value=>Number.isSafeInteger(value)&&value>0
|
|
8
|
+
const plain=value=>value!==null&&typeof value==='object'&&!Array.isArray(value)&&[Object.prototype,null].includes(Object.getPrototypeOf(value))
|
|
9
|
+
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
|
|
10
|
+
const fail=reason=>{const error=Error('memory-observation-'+reason);error.code=error.message;throw error}
|
|
11
|
+
const toolName=value=>typeof value==='string'&&/^[A-Za-z_][A-Za-z0-9_.:/-]{0,199}$/.test(value)&&!value.startsWith('memory_')
|
|
12
|
+
const keys=['schema','kind','sessionId','toolName','callId','callSeq','resultSeq','resultMessageId','argumentsHash','outputHash','quote','outcome','observedAt']
|
|
13
|
+
export function normalizeObservationCertificate(input,sessionId){
|
|
14
|
+
if(!plain(input)||Object.keys(input).sort().join('|')!==[...keys].sort().join('|')||input.schema!==1||input.kind!=='native-tool-result'||!id(input.sessionId)||input.sessionId!==sessionId||!toolName(input.toolName)||!id(input.callId)||!seq(input.callSeq)||!seq(input.resultSeq)||input.callSeq>=input.resultSeq||!id(input.resultMessageId)||!hex(input.argumentsHash)||!hex(input.outputHash)||!['succeeded','error'].includes(input.outcome)||!iso(input.observedAt))fail('invalid-certificate')
|
|
15
|
+
const quote=typeof input.quote==='string'?escapeFraming(normalizeText(input.quote).trim()):''
|
|
16
|
+
if(!quote||quote.length>500||findSecrets(quote).length||quote!==input.quote)fail('invalid-quote')
|
|
17
|
+
return Object.fromEntries(keys.map(key=>[key,input[key]]))
|
|
18
|
+
}
|
|
19
|
+
/** Control roots include both execution events, not only the request to save a memory. */
|
|
20
|
+
export function observationRoots(certificate){return[certificate.callSeq,certificate.resultSeq].map(at=>hash([certificate.sessionId,certificate.sessionId+':'+at]))}
|
|
21
|
+
/** Identical arguments and output do not become new evidence on every read. */
|
|
22
|
+
export function observationIdentity(certificate){return hash([certificate.toolName,certificate.argumentsHash,certificate.outputHash])}
|
|
23
|
+
export function argumentFingerprint(input){
|
|
24
|
+
let value=input
|
|
25
|
+
if(typeof input==='string'){try{value=JSON.parse(input)}catch{return hash(['raw-arguments',input])}}
|
|
26
|
+
let nodes=0
|
|
27
|
+
const canonical=(value,depth=0)=>{
|
|
28
|
+
if(++nodes>2000||depth>12)fail('arguments-too-complex')
|
|
29
|
+
if(value===null||['string','boolean'].includes(typeof value)||typeof value==='number'&&Number.isFinite(value))return value
|
|
30
|
+
if(Array.isArray(value)){if(value.length>256)fail('arguments-too-complex');return value.map(item=>canonical(item,depth+1))}
|
|
31
|
+
if(plain(value)){if(Object.keys(value).length>128)fail('arguments-too-complex');return Object.fromEntries(Object.keys(value).sort().map(key=>[key,canonical(value[key],depth+1)]))}
|
|
32
|
+
fail('invalid-arguments')
|
|
33
|
+
}
|
|
34
|
+
return hash(['json-arguments',canonical(value)])
|
|
35
|
+
}
|
|
36
|
+
function textOf(message){
|
|
37
|
+
if(!Array.isArray(message?.content)||!message.content.length||message.content.length>32||message.content.some(block=>block?.type!=='text'||typeof block.text!=='string'))return undefined
|
|
38
|
+
const text=message.content.map(block=>block.text).join('\n')
|
|
39
|
+
if(Buffer.byteLength(text)>16384||findSecrets(normalizeText(text)).length)return undefined
|
|
40
|
+
return normalizeText(text)
|
|
41
|
+
}
|
|
42
|
+
function matches(message,result){return message?.role==='tool'&&message.source?.kind==='tool'&&message.source.callId===result.call.callId&&message.toolCallId===result.call.callId&&message.id===result.messageId&&message.isError===result.isError&&hash(message.content)===result.outputHash}
|
|
43
|
+
export function createObservationTracker(ownerForCall){
|
|
44
|
+
const states=new WeakMap(),sessions=new Map()
|
|
45
|
+
let active=true
|
|
46
|
+
function stateFor(session){let state=states.get(session);if(!state){state={calls:new Map(),results:new Map(),visible:new Map(),position:null};states.set(session,state)}return state}
|
|
47
|
+
function preStep(input){const session=input?.agent?.session;if(!session||!id(session.id)||!seq(input.turn)||!seq(input.step))return;const state=stateFor(session);state.position={turn:input.turn,step:input.step};state.visible.clear();sessions.set(session.id,new WeakRef(session));while(sessions.size>256)sessions.delete(sessions.keys().next().value)}
|
|
48
|
+
function event(session,event){
|
|
49
|
+
if(!session||!id(session.id)||!seq(event?.seq))return
|
|
50
|
+
const state=stateFor(session),data=event.data
|
|
51
|
+
if(event.surfaceOp&&typeof event.surfaceOp==='object'){
|
|
52
|
+
const op=event.surfaceOp
|
|
53
|
+
for(const result of state.results.values())if(op.op==='replace'&&result.seq>=op.startSeq&&result.seq<=op.endSeq)result.invalid=true
|
|
54
|
+
}
|
|
55
|
+
if(event.type==='tool/call'){
|
|
56
|
+
if(!id(data?.callId)||!seq(data.turn)||!seq(data.step))return
|
|
57
|
+
if(state.calls.has(data.callId)){state.calls.get(data.callId).ambiguous=true;return}
|
|
58
|
+
let owner,argumentsHash
|
|
59
|
+
try{const raw=JSON.stringify(data.arguments);if(typeof raw!=='string'||Buffer.byteLength(raw)>32768||findSecrets(normalizeText(raw)).length)throw Error();argumentsHash=argumentFingerprint(data.arguments);owner=ownerForCall(session,data.callId)}catch{}
|
|
60
|
+
state.calls.set(data.callId,{callId:data.callId,name:data.name,seq:event.seq,turn:data.turn,step:data.step,owner,argumentsHash})
|
|
61
|
+
while(state.calls.size>256)state.calls.delete(state.calls.keys().next().value)
|
|
62
|
+
}else if(event.type==='tool/result'){
|
|
63
|
+
const message=data?.message,call=state.calls.get(message?.toolCallId)
|
|
64
|
+
if(!call)return
|
|
65
|
+
if(state.results.has(call.callId)){state.results.get(call.callId).invalid=true;return}
|
|
66
|
+
const text=textOf(message),valid=event.surfaceOp==='append'&&Array.isArray(event.sourceEventSeqs)&&event.sourceEventSeqs.length===1&&event.sourceEventSeqs[0]===call.seq&&call.seq<event.seq&&data.turn===call.turn&&data.step===call.step&&message.role==='tool'&&message.source?.kind==='tool'&&message.source.callId===call.callId&&id(message.id)&&[true,false,undefined].includes(message.isError)&&(!data.error||message.isError===true)&&text!==undefined
|
|
67
|
+
state.results.set(call.callId,{call,seq:event.seq,messageId:message?.id,outputHash:hash(message?.content??null),isError:message?.isError,text,observedAt:new Date().toISOString(),invalid:!valid})
|
|
68
|
+
while(state.results.size>64)state.results.delete(state.results.keys().next().value)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function request(request){
|
|
72
|
+
if(!Array.isArray(request?.tools)||!request.tools.some(tool=>tool?.name==='memory_write'))return
|
|
73
|
+
const session=sessions.get(request.sessionId)?.deref(),state=session&&states.get(session)
|
|
74
|
+
if(!state)return
|
|
75
|
+
state.visible.clear()
|
|
76
|
+
if(request.signal?.aborted||!Array.isArray(request.messages)||request.messages.length>4096)return
|
|
77
|
+
for(const result of state.results.values())if(!result.invalid&&request.messages.filter(message=>matches(message,result)).length===1)state.visible.set(result.call.callId,state.position)
|
|
78
|
+
}
|
|
79
|
+
function forExecution(exec,reference,currentOwner){
|
|
80
|
+
exec?.signal?.throwIfAborted()
|
|
81
|
+
if(!active)fail('disposed')
|
|
82
|
+
if(!plain(reference)||Object.keys(reference).sort().join('|')!=='callId|quote'||!id(reference.callId)||typeof reference.quote!=='string')fail('invalid-reference')
|
|
83
|
+
const session=exec?.agent?.session,state=session&&states.get(session),current=state?.calls.get(exec?.rootCallId??exec?.callId),result=state?.results.get(reference.callId),seen=state?.visible.get(reference.callId)
|
|
84
|
+
if(!currentOwner||!current||!result||result.invalid||result.call.ambiguous||!result.call.owner||!result.call.argumentsHash||!toolName(result.call.name)||result.seq>=current.seq||!seen||seen.turn!==current.turn||seen.step!==current.step||typeof session.deriveMessages!=='function')fail('source-unavailable')
|
|
85
|
+
const currentMessages=session.deriveMessages();if(!Array.isArray(currentMessages)||currentMessages.length>4096||currentMessages.filter(message=>matches(message,result)).length!==1)fail('surface-changed')
|
|
86
|
+
const quote=normalizeText(reference.quote).trim();if(!quote||quote.length>500||!result.text.includes(quote)||findSecrets(quote).length)fail('quote-not-in-result')
|
|
87
|
+
const certificate=normalizeObservationCertificate({schema:1,kind:'native-tool-result',sessionId:session.id,toolName:result.call.name,callId:result.call.callId,callSeq:result.call.seq,resultSeq:result.seq,resultMessageId:result.messageId,argumentsHash:result.call.argumentsHash,outputHash:result.outputHash,quote:escapeFraming(quote),outcome:result.isError===true?'error':'succeeded',observedAt:result.observedAt},session.id)
|
|
88
|
+
return{...currentOwner,evidence:result.call.owner.evidence,observation:certificate,observationContextHash:result.call.owner.contextCwdHash}
|
|
89
|
+
}
|
|
90
|
+
function availableForExecution(exec){
|
|
91
|
+
exec?.signal?.throwIfAborted()
|
|
92
|
+
if(!active)fail('disposed')
|
|
93
|
+
const session=exec?.agent?.session,state=session&&states.get(session),current=state?.calls.get(exec?.rootCallId??exec?.callId)
|
|
94
|
+
if(!state||!current||typeof session.deriveMessages!=='function')fail('source-unavailable')
|
|
95
|
+
const messages=session.deriveMessages();if(!Array.isArray(messages)||messages.length>4096)fail('surface-changed')
|
|
96
|
+
const eligible=[...state.results.values()].filter(result=>{
|
|
97
|
+
const seen=state.visible.get(result.call.callId)
|
|
98
|
+
return !result.invalid&&!result.call.ambiguous&&result.call.owner&&result.call.argumentsHash&&toolName(result.call.name)&&result.seq<current.seq&&seen?.turn===current.turn&&seen.step===current.step&&messages.filter(message=>matches(message,result)).length===1
|
|
99
|
+
}).sort((a,b)=>b.seq-a.seq)
|
|
100
|
+
const items=[];let bytes=0
|
|
101
|
+
for(const result of eligible.slice(0,8)){const item={callId:result.call.callId,toolName:result.call.name,outcome:result.isError===true?'error':'succeeded',quotePreview:escapeFraming(result.text.slice(0,100)),observedAt:result.observedAt};const size=Buffer.byteLength(JSON.stringify(item));if(bytes+size>2400)break;items.push(item);bytes+=size}
|
|
102
|
+
return{items,truncated:eligible.length>items.length,note:'仅本会话当前可引用的原生输出;复制callId与实际引用,不猜ID。成功不证明世界事实;错误结果只能作诊断。'}
|
|
103
|
+
}
|
|
104
|
+
function disposeSession(session){states.delete(session);if(sessions.get(session.id)?.deref()===session)sessions.delete(session.id)}
|
|
105
|
+
function dispose(){active=false;sessions.clear()}
|
|
106
|
+
return{preStep,event,request,forExecution,availableForExecution,disposeSession,dispose}
|
|
107
|
+
}
|
|
108
|
+
export function observationView(record){
|
|
109
|
+
if(!record.observation)return undefined
|
|
110
|
+
const proof=normalizeObservationCertificate(record.observation,record.sessionId)
|
|
111
|
+
return{kind:proof.kind,toolName:proof.toolName,callId:proof.callId,resultEventId:proof.sessionId+':'+proof.resultSeq,observedAt:proof.observedAt,outcome:proof.outcome,quote:proof.quote,outputHash:proof.outputHash,quoteChecked:true,currentWorldStateVerified:false,continuouslyRevalidated:false}
|
|
112
|
+
}
|
|
113
|
+
export const OBSERVATION_REFERENCE_SCHEMA={type:'object',additionalProperties:false,required:['callId','quote'],properties:{callId:{type:'string',description:'先调用memory_status(includeObservations=true)取得并逐字复制真实callId;模型不应猜测传输层ID。仅本会话已见外部工具结果,不是消息ID,不能引用memory工具。'},quote:{type:'string',description:'从该工具实际文本输出逐字摘录,最多500字。插件核对原生调用、结果和可见内容,不接受模型自填验证状态。'}}}
|
|
114
|
+
export const OBSERVATION_LIST_SCHEMA={type:'object',additionalProperties:false,required:['items','truncated','note'],properties:{items:{type:'array',items:{type:'object',additionalProperties:false,required:['callId','toolName','outcome','quotePreview','observedAt'],properties:{callId:{type:'string'},toolName:{type:'string'},outcome:{type:'string',enum:['succeeded','error']},quotePreview:{type:'string'},observedAt:{type:'string'}}}},truncated:{type:'boolean'},note:{type:'string'}}}
|
|
115
|
+
export const OBSERVATION_VIEW_SCHEMA={type:'object',additionalProperties:false,required:['kind','toolName','callId','resultEventId','observedAt','outcome','quote','outputHash','quoteChecked','currentWorldStateVerified','continuouslyRevalidated'],properties:{kind:{type:'string',enum:['native-tool-result']},toolName:{type:'string'},callId:{type:'string'},resultEventId:{type:'string'},observedAt:{type:'string'},outcome:{type:'string',enum:['succeeded','error']},quote:{type:'string'},outputHash:{type:'string'},quoteChecked:{type:'boolean'},currentWorldStateVerified:{type:'boolean'},continuouslyRevalidated:{type:'boolean'}}}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** Bounded code-owned current projections for a no-rewrite validator. Not new owner evidence. */
|
|
2
|
+
import {normalizeMeaning} from './meaning.mjs'
|
|
3
|
+
import {rankLines} from './retrieval.mjs'
|
|
4
|
+
const fail=()=>{throw Error('memory-validation-context-invalid')}
|
|
5
|
+
export function validationTransitionContext(target,records,{maxBytes=4096,maxRecords=4}={}){
|
|
6
|
+
if(!target||!['global','project'].includes(target.scope)||typeof target.summary!=='string'||!Array.isArray(records)||records.length>512||!Number.isSafeInteger(maxBytes)||maxBytes<128||maxBytes>12288||!Number.isSafeInteger(maxRecords)||maxRecords<1||maxRecords>8)fail()
|
|
7
|
+
const ids=new Set(),owned=records.filter(r=>r?.scope===target.scope&&['recorded','adopted'].includes(r.status)).map(r=>{
|
|
8
|
+
if(typeof r.id!=='string'||!/^[a-f0-9]{64}$/.test(r.id)||ids.has(r.id)||typeof r.summary!=='string')fail();ids.add(r.id)
|
|
9
|
+
const line=r.indexLine??`- [${r.type}] ${r.summary} — ${r.detail===undefined?'索引摘要':r.type+'.md'}${r.expires===undefined?'':` (exp:${r.expires})`}`
|
|
10
|
+
return structuredClone({recordId:r.id,identityKind:r.legacyIndexOnly===true?'index-line':'source-record',sourceLinked:r.legacyIndexOnly!==true,indexLine:line,summary:r.summary,type:r.type,scope:r.scope,meaning:r.meaning??null,expires:r.expires??null})
|
|
11
|
+
})
|
|
12
|
+
const oldLine=typeof target.supersedes==='string'?target.supersedes.replace(/^\[(?:global|project)\] /,''):undefined
|
|
13
|
+
const exact=oldLine===undefined?[]:owned.filter(r=>r.indexLine===oldLine||r.indexLine.includes(oldLine))
|
|
14
|
+
if(exact.length>1)fail()
|
|
15
|
+
const ranked=rankLines(target.summary,owned.map(r=>({file:r.recordId,line:1,text:r.summary})))
|
|
16
|
+
const byId=new Map(owned.map(r=>[r.recordId,r])),order=[...exact.map(r=>r.recordId),...ranked.map(r=>r.file),...owned.toReversed().map(r=>r.recordId)]
|
|
17
|
+
const sameMetadata=row=>row.sourceLinked&&target.meaning!==undefined&&row.meaning!==null&&row.type===target.type&&row.expires===(target.expires??null)&&JSON.stringify(normalizeMeaning(row.meaning))===JSON.stringify(normalizeMeaning(target.meaning))
|
|
18
|
+
const effect=rows=>({proposed:oldLine===undefined?'append':'replace',existingRowsRemain:oldLine===undefined?'all':'all-except-designated-target',sameMetadataRecordIds:rows.filter(sameMetadata).map(row=>row.recordId)})
|
|
19
|
+
const envelope={operation:effect([]),records:[],omittedRecords:owned.length,scope:target.scope,targetShown:oldLine===undefined?null:false,boundary:'Existing indexed interpretations for transition checking only; sourceLinked=false is unclassified legacy index data with no verified source metadata. Not new owner evidence, world truth or execution authority.'}
|
|
20
|
+
const seen=new Set()
|
|
21
|
+
for(const id of order){
|
|
22
|
+
if(seen.has(id))continue;seen.add(id)
|
|
23
|
+
if(envelope.records.length>=maxRecords)break
|
|
24
|
+
const row=byId.get(id),rows=[...envelope.records,row],next={...envelope,operation:effect(rows),records:rows,omittedRecords:owned.length-envelope.records.length-1,targetShown:oldLine===undefined?null:exact.some(t=>t.recordId===id)||envelope.targetShown}
|
|
25
|
+
if(Buffer.byteLength(JSON.stringify(next))>maxBytes)continue
|
|
26
|
+
Object.assign(envelope,next)
|
|
27
|
+
}
|
|
28
|
+
if(Buffer.byteLength(JSON.stringify(envelope))>maxBytes)throw Error('memory-validation-context-budget')
|
|
29
|
+
return envelope
|
|
30
|
+
}
|
package/version.mjs
ADDED
package/withdrawal.mjs
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import {withdrawalSelector} from './source-identity.mjs'
|
|
2
|
+
import {readdirSync} from 'node:fs'
|
|
3
|
+
import { createHash } from 'node:crypto'
|
|
4
|
+
/** Build a withdrawal plan for traceable adopted records and audit-linked summary-only captures. */
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { readSafely, normalizeText, entryId } from './security.mjs'
|
|
7
|
+
import { contentHash } from './publication.mjs'
|
|
8
|
+
import { readManagedLogs } from './managed-logs.mjs'
|
|
9
|
+
import { removeRetiredBlock } from './source-retirement.mjs'
|
|
10
|
+
import {purgeEpisodes} from './episodes.mjs'
|
|
11
|
+
|
|
12
|
+
export const normalizedLineHash = (line) => contentHash(normalizeText(line).trim())
|
|
13
|
+
export const candidateDetail = (id, detail) => `[candidate:${id}]\n${detail}`
|
|
14
|
+
const hasSourceOrigin = record => ['tool-context', 'quoted-extraction'].includes(record.origin)
|
|
15
|
+
|
|
16
|
+
export function buildWithdrawal(dir, affected, source, formatLine, independent = []) {
|
|
17
|
+
const retired = affected.filter(record => record.status === 'retired')
|
|
18
|
+
const adopted = affected.filter((r) => r.status === 'adopted' || hasSourceOrigin(r) && ['recorded', 'superseded'].includes(r.status))
|
|
19
|
+
const hasDetail = record => record.detail !== undefined && normalizeText(String(record.detail)).trim().length > 0
|
|
20
|
+
const records = adopted.map((r) => ({ candidateId: r.id, line: hasSourceOrigin(r) ? r.indexLine : formatLine(r.type, r.summary, undefined, hasDetail(r)), type: r.type, detail: r.detail, allowMissingIndex: hasSourceOrigin(r) }))
|
|
21
|
+
const effectiveHash = other => other.status === 'retired' ? other.lineHash : normalizedLineHash(hasSourceOrigin(other) ? other.indexLine : formatLine(other.type, other.summary, undefined, hasDetail(other)))
|
|
22
|
+
if ([...records.map(record => normalizedLineHash(record.line)), ...retired.map(record => record.lineHash)].some(hash => independent.some(other => effectiveHash(other) === hash))) throw new Error('withdrawal-shared-effective-memory')
|
|
23
|
+
const logs = readManagedLogs(dir).map(({ name, raw }) => {
|
|
24
|
+
const rows = raw === undefined ? [] : raw.split('\n').filter((l) => l.trim()).map((l) => {
|
|
25
|
+
try { return JSON.parse(l) } catch { throw new Error('withdrawal-corrupt-log') }
|
|
26
|
+
})
|
|
27
|
+
return { name, raw, rows }
|
|
28
|
+
})
|
|
29
|
+
const sourceHash = source.sourceKey ?? createHash('sha256').update(JSON.stringify([source.sessionId, source.messageId])).digest('hex')
|
|
30
|
+
if(source.sourceKey&&source.sessionId!==undefined)source=withdrawalSelector(source.sourceKey,source)
|
|
31
|
+
const matchesSource = (row) => typeof row?.sessionId === 'string' && Array.isArray(row.evidence) && row.evidence.some(e => typeof e?.messageId === 'string' && createHash('sha256').update(JSON.stringify([row.sessionId, e.messageId])).digest('hex') === sourceHash)
|
|
32
|
+
for (const { rows } of logs) for (const row of rows) {
|
|
33
|
+
if (row?.op === 'write' && row.outcome === 'ok' && (source.sessionId!==undefined ? row.sessionId===source.sessionId : source.sourceKey ? typeof row.sessionId==='string' : false) && typeof row.line === 'string' && (!Array.isArray(row.evidence) || !row.evidence.length)) throw new Error('withdrawal-missing-source-lineage')
|
|
34
|
+
if (row?.op !== 'write' || row.outcome !== 'ok' || !matchesSource(row) || typeof row.line !== 'string') continue
|
|
35
|
+
if (records.some((r) => r.line === row.line) || retired.some(record => record.lineHash === normalizedLineHash(row.line))) continue
|
|
36
|
+
if (row.withDetail) throw new Error('withdrawal-untracked-detail')
|
|
37
|
+
if (!['user', 'feedback', 'project', 'reference'].includes(row.type)) throw new Error('withdrawal-invalid-audit-type')
|
|
38
|
+
records.push({ line: row.line, type: row.type })
|
|
39
|
+
}
|
|
40
|
+
const index = readSafely(join(dir, 'MEMORY.md'))
|
|
41
|
+
const indexLines = (index ?? '').split('\n')
|
|
42
|
+
const lineHashes = new Set([...records.map((r) => normalizedLineHash(r.line)), ...retired.map(record => record.lineHash)])
|
|
43
|
+
const ids = new Set([...records.map((r) => entryId(r.line)), ...retired.map(record => record.memoryId)])
|
|
44
|
+
const candidateIds = new Set(affected.map((r) => r.id))
|
|
45
|
+
for (const r of records) {
|
|
46
|
+
if (!r.allowMissingIndex && !indexLines.some((line) => normalizeText(line).trim() === normalizeText(r.line).trim())) throw new Error('withdrawal-effective-line-diverged')
|
|
47
|
+
}
|
|
48
|
+
for (const record of [...records.filter(record => record.allowMissingIndex && record.detail !== undefined), ...retired.filter(record => record.block)]) {
|
|
49
|
+
if (readdirSync(dir).some(name => name.startsWith(record.type + '.archive-'))) throw new Error('withdrawal-unreviewed-topic-archive')
|
|
50
|
+
}
|
|
51
|
+
const writes = []
|
|
52
|
+
const historical=purgeEpisodes(dir,sourceHash);if(historical)writes.push(historical)
|
|
53
|
+
if (index !== undefined && records.length + retired.length > 0) writes.push({ name: 'MEMORY.md', text: indexLines.filter((line) => !lineHashes.has(normalizedLineHash(line))).join('\n') })
|
|
54
|
+
const topics = new Map()
|
|
55
|
+
for (const r of records) {
|
|
56
|
+
if (r.detail === undefined) continue
|
|
57
|
+
const name = `${r.type}.md`
|
|
58
|
+
const current = topics.has(name) ? topics.get(name) : readSafely(join(dir, name))
|
|
59
|
+
const indexPresent = indexLines.some(line => normalizeText(line).trim() === normalizeText(r.line).trim())
|
|
60
|
+
if (current === undefined && r.allowMissingIndex && !indexPresent) continue
|
|
61
|
+
if (current === undefined || !r.candidateId) throw new Error('withdrawal-untracked-detail')
|
|
62
|
+
const block = candidateDetail(r.candidateId, r.detail)
|
|
63
|
+
const start = current.indexOf(block)
|
|
64
|
+
if (start < 0 && r.allowMissingIndex && !indexPresent && !current.includes(`[candidate:${r.candidateId}]`) && !current.includes(r.detail)) continue
|
|
65
|
+
if (start < 0 || current.indexOf(block, start + block.length) >= 0) throw new Error('withdrawal-untracked-detail')
|
|
66
|
+
const header = current.lastIndexOf('\n- ', start)
|
|
67
|
+
if (header < 0 || !/^\n- \d{4}-\d\d-\d\d|$/.test(current.slice(header, start)) || current[start + block.length] !== '\n') throw new Error('withdrawal-untracked-detail')
|
|
68
|
+
topics.set(name, current.slice(0, header) + '\n' + current.slice(start + block.length + 1))
|
|
69
|
+
}
|
|
70
|
+
for (const record of retired) {
|
|
71
|
+
if (!record.block) continue
|
|
72
|
+
const name = record.type + '.md', current = topics.has(name) ? topics.get(name) : readSafely(join(dir, name))
|
|
73
|
+
if (current === undefined) continue
|
|
74
|
+
const next = removeRetiredBlock(current, record)
|
|
75
|
+
if (next !== current) topics.set(name, next)
|
|
76
|
+
}
|
|
77
|
+
for (const [name, text] of topics) writes.push({ name, text })
|
|
78
|
+
function scrub(value) {
|
|
79
|
+
if (Array.isArray(value)) return value.filter((v) => !(v && typeof v === 'object' && ids.has(v.id))).map(scrub)
|
|
80
|
+
if (value && typeof value === 'object') {
|
|
81
|
+
const next = Object.create(null)
|
|
82
|
+
for (const [key, item] of Object.entries(value)) {
|
|
83
|
+
if (typeof item === 'string' && (lineHashes.has(normalizedLineHash(item)) || key === 'summary' && affected.some((r) => r.summary === item))) continue
|
|
84
|
+
next[key] = scrub(item)
|
|
85
|
+
}
|
|
86
|
+
return next
|
|
87
|
+
}
|
|
88
|
+
return value
|
|
89
|
+
}
|
|
90
|
+
for (const log of logs) {
|
|
91
|
+
if (log.raw === undefined) continue
|
|
92
|
+
let changed = false
|
|
93
|
+
const rows = log.rows.map((row) => {
|
|
94
|
+
if (!row || typeof row !== 'object' || Array.isArray(row)) throw new Error('withdrawal-corrupt-log')
|
|
95
|
+
if (ids.has(row.id) && typeof row.line === 'string' && !lineHashes.has(normalizedLineHash(row.line))) throw new Error('withdrawal-id-collision')
|
|
96
|
+
if (ids.has(row.id) || lineHashes.has(normalizedLineHash(String(row.line ?? ''))) || candidateIds.has(row.candidateId) || matchesSource(row)) {
|
|
97
|
+
changed = true
|
|
98
|
+
return { op: 'source-withdrawn', ...(typeof row.id === 'string' ? { id: row.id } : {}) }
|
|
99
|
+
}
|
|
100
|
+
const next = scrub(row)
|
|
101
|
+
if (JSON.stringify(next) !== JSON.stringify(row)) changed = true
|
|
102
|
+
return next
|
|
103
|
+
})
|
|
104
|
+
if (changed) writes.push({ name: log.name, text: rows.map((r) => JSON.stringify(r)).join('\n') + (rows.length ? '\n' : '') })
|
|
105
|
+
}
|
|
106
|
+
return { writes, lineHashes: [...lineHashes], memoryIds: [...ids], candidateIds: [...candidateIds],
|
|
107
|
+
blocks: [...records.filter((r) => r.candidateId && r.detail !== undefined).map((r) => ({ id: r.candidateId, length: candidateDetail(r.candidateId, r.detail).length, hash: contentHash(candidateDetail(r.candidateId, r.detail)) })), ...retired.filter(record => record.block).map(record => record.block)], effectiveRemoved: records.length + retired.length }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Suppress exact withdrawn lines and marked detail blocks if an old file is restored. */
|
|
111
|
+
export function filterWithdrawn(text, policy, isIndex = false) {
|
|
112
|
+
let filtered = text
|
|
113
|
+
for (const id of policy.candidateIds) {
|
|
114
|
+
const marker = `[candidate:${id}]\n`
|
|
115
|
+
let start = filtered.indexOf(marker)
|
|
116
|
+
while (start >= 0) {
|
|
117
|
+
const header = filtered.lastIndexOf('\n- ', start)
|
|
118
|
+
const block = policy.blocks.find((b) => b.id === id)
|
|
119
|
+
const length = block?.length
|
|
120
|
+
if (header < 0 || !/^\n- \d{4}-\d\d-\d\d|$/.test(filtered.slice(header, start)) || length === undefined || contentHash(filtered.slice(start, start + length)) !== block.hash) {
|
|
121
|
+
if (policy.strict === true) throw new Error('withdrawal-unverifiable-block')
|
|
122
|
+
start = filtered.indexOf(marker, start + marker.length)
|
|
123
|
+
continue
|
|
124
|
+
}
|
|
125
|
+
// Known original block length prevents embedded dated lines from leaking its tail.
|
|
126
|
+
filtered = filtered.slice(0, header) + filtered.slice(start + length)
|
|
127
|
+
start = filtered.indexOf(marker)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return filtered.split('\n').filter((line) => !policy.lineHashes.includes(normalizedLineHash(line))).join('\n')
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Scrub recognizable restored projections without recreating deleted evidence bodies. */
|
|
134
|
+
export function prepareRestoredWithdrawalCleanup(dir,policy,source){
|
|
135
|
+
const strict={...policy,strict:true},writes=[],ids=new Set(policy.memoryIds),candidateIds=new Set(policy.candidateIds),lines=new Set(policy.lineHashes)
|
|
136
|
+
const historical=purgeEpisodes(dir,source);if(historical)writes.push(historical)
|
|
137
|
+
for(const name of ['MEMORY.md','user.md','feedback.md','project.md','reference.md']){
|
|
138
|
+
const body=readSafely(join(dir,name));if(body===undefined)continue
|
|
139
|
+
const next=filterWithdrawn(body,strict,name==='MEMORY.md');if(next!==body)writes.push({name,text:next})
|
|
140
|
+
}
|
|
141
|
+
const matches=row=>typeof row?.sessionId==='string'&&Array.isArray(row.evidence)&&row.evidence.some(e=>typeof e?.messageId==='string'&&createHash('sha256').update(JSON.stringify([row.sessionId,e.messageId])).digest('hex')===source)
|
|
142
|
+
function scrub(value){
|
|
143
|
+
if(Array.isArray(value))return value.filter(item=>!item||typeof item!=='object'||!ids.has(item.id)).map(scrub)
|
|
144
|
+
if(value&&typeof value==='object'){const result={};for(const [key,item]of Object.entries(value)){if(typeof item==='string'&&lines.has(normalizedLineHash(item)))continue;result[key]=scrub(item)}return result}
|
|
145
|
+
return value
|
|
146
|
+
}
|
|
147
|
+
for(const {name,raw}of readManagedLogs(dir)){
|
|
148
|
+
if(raw===undefined)continue
|
|
149
|
+
const rows=raw.split('\n').filter(line=>line.trim()).map(line=>{try{return JSON.parse(line)}catch{throw Error('withdrawal-corrupt-log')}})
|
|
150
|
+
let changed=false
|
|
151
|
+
const after=rows.map(row=>{if(!row||typeof row!=='object'||Array.isArray(row))throw Error('withdrawal-corrupt-log');if(ids.has(row.id)||candidateIds.has(row.candidateId)||typeof row.line==='string'&&lines.has(normalizedLineHash(row.line))||matches(row)){changed=true;return{op:'source-withdrawn',...(typeof row.id==='string'?{id:row.id}:{})}}const next=scrub(row);if(JSON.stringify(next)!==JSON.stringify(row))changed=true;return next})
|
|
152
|
+
if(changed)writes.push({name,text:after.map(row=>JSON.stringify(row)).join('\n')+(after.length?'\n':'')})
|
|
153
|
+
}
|
|
154
|
+
return writes
|
|
155
|
+
}
|
package/worker-write.mjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** 并发 worker:向同一个库写 N 条。由 test-concurrency.mjs 以多进程方式拉起。 */
|
|
2
|
+
import { writeMemory } from './index.mjs'
|
|
3
|
+
import { retryBusyWrite } from './capture-jobs.mjs'
|
|
4
|
+
|
|
5
|
+
const [dir, prefix, countText] = process.argv.slice(2)
|
|
6
|
+
const count = Number(countText)
|
|
7
|
+
|
|
8
|
+
let attempts = 0
|
|
9
|
+
for (let i = 0; i < count; i += 1) await retryBusyWrite(
|
|
10
|
+
() => writeMemory({ type: 'user', dir, line: `- [user] ${prefix}-${i} — user.md` }),
|
|
11
|
+
{ beforeAttempt: () => { attempts += 1 } },
|
|
12
|
+
)
|
|
13
|
+
console.log(JSON.stringify({ writes: count, retries: attempts - count }))
|