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,156 @@
|
|
|
1
|
+
import { test } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { mkdtempSync, readFileSync, writeFileSync, readdirSync, rmSync, symlinkSync, linkSync, existsSync, utimesSync } from 'node:fs'
|
|
4
|
+
import { spawnSync } from 'node:child_process'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { tmpdir } from 'node:os'
|
|
7
|
+
import { stageCandidate, listCandidates } from './candidates.mjs'
|
|
8
|
+
import { adoptCandidate, withdrawManagedSource, recoverMemoryPublication, storeRevision, searchStore, writeMemory, indexLine } from './index.mjs'
|
|
9
|
+
import { commitPublication } from './publication.mjs'
|
|
10
|
+
import { withLock } from './security.mjs'
|
|
11
|
+
import { MANAGED_LOG_BYTES, MANAGED_LOG_TOTAL_BYTES, isManagedLogName } from './managed-logs.mjs'
|
|
12
|
+
|
|
13
|
+
function box(run) {
|
|
14
|
+
const dir = mkdtempSync(join(tmpdir(), 'dsh-archive-withdrawal-'))
|
|
15
|
+
try { return run(dir) } finally { rmSync(dir, { recursive: true, force: true }) }
|
|
16
|
+
}
|
|
17
|
+
function seed(dir, type = 'user', sessionId = 'archive-source') {
|
|
18
|
+
const item = stageCandidate(dir, { type, scope: 'project', summary: `Archive synthetic ${type} durable choice`, detail: `Synthetic ${type} detail from withdrawn evidence.`, sessionId,
|
|
19
|
+
indexRevision: storeRevision(dir), evidence: [{ messageId: 'message-1', hash: 'b'.repeat(64), quote: 'Synthetic source evidence for archive withdrawal.' }] })
|
|
20
|
+
const view = adoptCandidate(dir, item.id)
|
|
21
|
+
adoptCandidate(dir, item.id, { apply: true, reviewedIndex: view.reviewedIndex, candidateRevision: view.candidateRevision })
|
|
22
|
+
}
|
|
23
|
+
function apply(dir, extra = {}) {
|
|
24
|
+
const preview = withdrawManagedSource(dir, 'archive-source', 'message-1')
|
|
25
|
+
return withdrawManagedSource(dir, 'archive-source', 'message-1', { apply: true, reviewedIndex: preview.reviewedIndex, candidateRevision: preview.candidateRevision, ...extra })
|
|
26
|
+
}
|
|
27
|
+
const snapshot = dir => new Map(readdirSync(dir).map(name => [name, readFileSync(join(dir, name), 'utf8')]))
|
|
28
|
+
|
|
29
|
+
test('more than one rotated log is scrubbed with current logs and preserves unrelated rows', () => box(dir => {
|
|
30
|
+
seed(dir)
|
|
31
|
+
const independent = JSON.stringify({ op: 'unrelated', id: 'independent-row', note: 'Keep the other source intact.' }) + '\n'
|
|
32
|
+
for (const base of ['.audit.jsonl', '.history.jsonl']) for (const suffix of [1, 2, 9, 16]) writeFileSync(join(dir, `${base}.${suffix}`), readFileSync(join(dir, base), 'utf8') + independent)
|
|
33
|
+
const before = snapshot(dir)
|
|
34
|
+
const preview = withdrawManagedSource(dir, 'archive-source', 'message-1')
|
|
35
|
+
assert.ok(preview.files.includes('.history.jsonl.16'))
|
|
36
|
+
for (const [name, raw] of before) assert.equal(readFileSync(join(dir, name), 'utf8'), raw, name)
|
|
37
|
+
assert.equal(apply(dir).effectiveRemoved, 1)
|
|
38
|
+
for (const base of ['.audit.jsonl', '.history.jsonl']) for (const suffix of [1, 2, 9, 16]) {
|
|
39
|
+
const raw = readFileSync(join(dir, `${base}.${suffix}`), 'utf8')
|
|
40
|
+
assert.doesNotMatch(raw, /durable choice|withdrawn evidence|Synthetic source evidence/)
|
|
41
|
+
assert.match(raw, /Keep the other source intact/)
|
|
42
|
+
}
|
|
43
|
+
assert.equal(searchStore(dir, 'durable choice').hits.length, 0)
|
|
44
|
+
}))
|
|
45
|
+
|
|
46
|
+
test('audit lineage in a later archive can withdraw a summary after current logs rotated away', () => box(dir => {
|
|
47
|
+
writeMemory({ dir, type: 'user', line: indexLine('user', 'Summary only recorded in later archive', undefined, false), provenance: { sessionId: 'archive-source', evidence: [{ messageId: 'message-1', hash: 'b'.repeat(64), quote: 'Synthetic source evidence for archive withdrawal.' }] } })
|
|
48
|
+
writeFileSync(join(dir, '.audit.jsonl.7'), readFileSync(join(dir, '.audit.jsonl')))
|
|
49
|
+
writeFileSync(join(dir, '.audit.jsonl'), '')
|
|
50
|
+
assert.equal(apply(dir).effectiveRemoved, 1)
|
|
51
|
+
assert.equal(searchStore(dir, 'Summary only').hits.length, 0)
|
|
52
|
+
assert.doesNotMatch(readFileSync(join(dir, '.audit.jsonl.7'), 'utf8'), /Summary only|Synthetic source evidence/)
|
|
53
|
+
}))
|
|
54
|
+
|
|
55
|
+
test('all four types, history ledger and thirty-two archives fit one recoverable forty-one-file withdrawal', () => box(dir => {
|
|
56
|
+
for (const type of ['user', 'feedback', 'project', 'reference']) seed(dir, type)
|
|
57
|
+
for (const base of ['.audit.jsonl', '.history.jsonl']) for (let suffix = 1; suffix <= 16; suffix++) writeFileSync(join(dir, `${base}.${suffix}`), readFileSync(join(dir, base)))
|
|
58
|
+
const preview = withdrawManagedSource(dir, 'archive-source', 'message-1')
|
|
59
|
+
assert.equal(preview.files.length, 40) // Candidate state is added at commit.
|
|
60
|
+
assert.ok(preview.files.includes('.episodes.json'))
|
|
61
|
+
assert.equal(apply(dir).effectiveRemoved, 4)
|
|
62
|
+
for (const name of readdirSync(dir)) assert.doesNotMatch(readFileSync(join(dir, name), 'utf8'), /durable choice|withdrawn evidence|Synthetic source evidence/, name)
|
|
63
|
+
assert.equal(listCandidates(dir, { status: 'withdrawn' }).total, 4)
|
|
64
|
+
assert.equal(searchStore(dir,'durable choice',{view:'history'}).hits.length,0)
|
|
65
|
+
}))
|
|
66
|
+
|
|
67
|
+
for (const name of ['.audit.jsonl.17', '.history.jsonl.0', '.history.jsonl.02', '.audit.jsonl.gz']) test(`unsupported archive ${name} refuses before changing authoritative files`, () => box(dir => {
|
|
68
|
+
seed(dir)
|
|
69
|
+
writeFileSync(join(dir, name), 'synthetic unclassified archive\n')
|
|
70
|
+
const before = snapshot(dir)
|
|
71
|
+
assert.throws(() => withdrawManagedSource(dir, 'archive-source', 'message-1'), /unsupported-log-archive/)
|
|
72
|
+
for (const [file, raw] of before) assert.equal(readFileSync(join(dir, file), 'utf8'), raw)
|
|
73
|
+
assert.equal(listCandidates(dir, { status: 'adopted' }).total, 1)
|
|
74
|
+
}))
|
|
75
|
+
|
|
76
|
+
for (const kind of ['symlink', 'hardlink']) test(`${kind} in numeric archives is rejected without changing the target`, () => box(dir => {
|
|
77
|
+
seed(dir)
|
|
78
|
+
const target = join(dir, 'unrelated.txt')
|
|
79
|
+
const untouched = JSON.stringify({ op: 'unrelated', note: 'unrelated synthetic text' }) + '\n'
|
|
80
|
+
writeFileSync(target, untouched)
|
|
81
|
+
if (kind === 'symlink') symlinkSync(target, join(dir, '.audit.jsonl.2'))
|
|
82
|
+
else linkSync(target, join(dir, '.audit.jsonl.2'))
|
|
83
|
+
assert.throws(() => withdrawManagedSource(dir, 'archive-source', 'message-1'), kind === 'hardlink' ? /linked-log/ : undefined)
|
|
84
|
+
assert.equal(readFileSync(target, 'utf8'), untouched)
|
|
85
|
+
assert.equal(listCandidates(dir, { status: 'adopted' }).total, 1)
|
|
86
|
+
}))
|
|
87
|
+
|
|
88
|
+
test('corrupt older archive refuses before partially clearing other source copies', () => box(dir => {
|
|
89
|
+
seed(dir)
|
|
90
|
+
writeFileSync(join(dir, '.history.jsonl.3'), '{incomplete')
|
|
91
|
+
const before = snapshot(dir)
|
|
92
|
+
assert.throws(() => withdrawManagedSource(dir, 'archive-source', 'message-1'), /corrupt-log/)
|
|
93
|
+
for (const [name, raw] of before) assert.equal(readFileSync(join(dir, name), 'utf8'), raw)
|
|
94
|
+
}))
|
|
95
|
+
|
|
96
|
+
test('per-file and combined archive read limits refuse with original data preserved', () => box(dir => {
|
|
97
|
+
seed(dir)
|
|
98
|
+
const before = readFileSync(join(dir, 'MEMORY.md'), 'utf8')
|
|
99
|
+
writeFileSync(join(dir, '.audit.jsonl.2'), 'x'.repeat(MANAGED_LOG_BYTES + 1))
|
|
100
|
+
assert.throws(() => withdrawManagedSource(dir, 'archive-source', 'message-1'))
|
|
101
|
+
rmSync(join(dir, '.audit.jsonl.2'))
|
|
102
|
+
const raw = JSON.stringify({ op: 'unrelated', note: 'x'.repeat(1024 * 1024 - 100) }) + '\n'
|
|
103
|
+
const count = Math.floor(MANAGED_LOG_TOTAL_BYTES / Buffer.byteLength(raw)) + 1
|
|
104
|
+
for (let i = 0; i < count; i++) writeFileSync(join(dir, `${i < 16 ? '.audit' : '.history'}.jsonl.${i % 16 + 1}`), raw)
|
|
105
|
+
assert.throws(() => withdrawManagedSource(dir, 'archive-source', 'message-1'), /log-budget-exceeded/)
|
|
106
|
+
assert.equal(readFileSync(join(dir, 'MEMORY.md'), 'utf8'), before)
|
|
107
|
+
assert.equal(listCandidates(dir, { status: 'adopted' }).total, 1)
|
|
108
|
+
}))
|
|
109
|
+
|
|
110
|
+
for (const phase of ['journal', 8]) test(`archive withdrawal interrupted at ${phase} blocks reads and recovers remaining archives`, () => box(dir => {
|
|
111
|
+
seed(dir)
|
|
112
|
+
for (let i = 1; i <= 16; i++) writeFileSync(join(dir, `.audit.jsonl.${i}`), readFileSync(join(dir, '.audit.jsonl')))
|
|
113
|
+
assert.throws(() => apply(dir, { publicationFault: event => { if (event.phase === phase || event.phase === 'file' && event.index === phase) throw new Error('controlled-interruption') } }), /controlled-interruption/)
|
|
114
|
+
assert.throws(() => searchStore(dir, 'durable choice'), /needs-recovery/)
|
|
115
|
+
assert.equal(recoverMemoryPublication(dir, { apply: true }).applied, true)
|
|
116
|
+
for (let i = 1; i <= 16; i++) assert.doesNotMatch(readFileSync(join(dir, `.audit.jsonl.${i}`), 'utf8'), /durable choice|Synthetic source evidence/)
|
|
117
|
+
assert.equal(searchStore(dir, 'durable choice').hits.length, 0)
|
|
118
|
+
}))
|
|
119
|
+
|
|
120
|
+
test('restored earlier log cannot reintroduce withdrawn current-view records', () => box(dir => {
|
|
121
|
+
seed(dir)
|
|
122
|
+
const index = readFileSync(join(dir, 'MEMORY.md')), audit = readFileSync(join(dir, '.audit.jsonl'))
|
|
123
|
+
writeFileSync(join(dir, '.audit.jsonl.2'), audit)
|
|
124
|
+
apply(dir)
|
|
125
|
+
writeFileSync(join(dir, '.audit.jsonl.2'), audit)
|
|
126
|
+
writeFileSync(join(dir, 'MEMORY.md'), index)
|
|
127
|
+
assert.equal(searchStore(dir, 'durable choice').hits.length, 0)
|
|
128
|
+
}))
|
|
129
|
+
|
|
130
|
+
test('archive publication targets remain a closed filename family', () => box(dir => {
|
|
131
|
+
for (const name of ['../.audit.jsonl.2', '.history.jsonl.17', '.audit.jsonl.01', '.audit.jsonl.2/escape', 'other.jsonl.2']) {
|
|
132
|
+
assert.equal(isManagedLogName(name), false)
|
|
133
|
+
assert.throws(() => withLock(dir, () => commitPublication(dir, [{ name, text: '{}' }])), /invalid-writes/)
|
|
134
|
+
assert.equal(existsSync(join(dir, '.publication.json')), false)
|
|
135
|
+
}
|
|
136
|
+
}))
|
|
137
|
+
|
|
138
|
+
test('forty-one-file withdrawal survives actual SIGKILL and finishes history/archive cleanup after lock expiry', () => box(dir => {
|
|
139
|
+
for (const type of ['user', 'feedback', 'project', 'reference']) seed(dir, type)
|
|
140
|
+
for (const base of ['.audit.jsonl', '.history.jsonl']) for (let suffix = 1; suffix <= 16; suffix++) writeFileSync(join(dir, `${base}.${suffix}`), readFileSync(join(dir, base)))
|
|
141
|
+
const preview = withdrawManagedSource(dir, 'archive-source', 'message-1')
|
|
142
|
+
const options = { apply: true, reviewedIndex: preview.reviewedIndex, candidateRevision: preview.candidateRevision }
|
|
143
|
+
const worker = `import {withdrawManagedSource} from ${JSON.stringify(new URL('./index.mjs', import.meta.url).href)};withdrawManagedSource(process.argv[1],'archive-source','message-1',{...JSON.parse(process.argv[2]),publicationFault:event=>{if(event.phase==='file'&&event.index===34)process.kill(process.pid,'SIGKILL')}})`
|
|
144
|
+
const stopped = spawnSync(process.execPath, ['--input-type=module', '-e', worker, dir, JSON.stringify(options)], { encoding: 'utf8', timeout: 15000 })
|
|
145
|
+
assert.equal(stopped.signal, 'SIGKILL', stopped.stderr)
|
|
146
|
+
const journal=JSON.parse(readFileSync(join(dir, '.publication.json')))
|
|
147
|
+
assert.equal(journal.files.length, 41)
|
|
148
|
+
assert.ok(journal.files.some(file=>file.name==='.episodes.json'))
|
|
149
|
+
assert.throws(() => searchStore(dir, 'durable choice'), /needs-recovery/)
|
|
150
|
+
const expired = new Date(Date.now() - 20000)
|
|
151
|
+
utimesSync(join(dir, '.write.lock'), expired, expired)
|
|
152
|
+
assert.equal(recoverMemoryPublication(dir, { apply: true }).applied, true)
|
|
153
|
+
assert.equal(listCandidates(dir, { status: 'withdrawn' }).total, 4)
|
|
154
|
+
assert.equal(searchStore(dir,'durable choice',{view:'history'}).hits.length,0)
|
|
155
|
+
for (const name of readdirSync(dir)) assert.doesNotMatch(readFileSync(join(dir, name), 'utf8'), /durable choice|withdrawn evidence|Synthetic source evidence/, name)
|
|
156
|
+
}))
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import {realpathSync} from 'node:fs'
|
|
2
|
+
import { test } from 'node:test'
|
|
3
|
+
import assert from 'node:assert/strict'
|
|
4
|
+
import { mkdtempSync, readFileSync, writeFileSync, rmSync, readdirSync, cpSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { tmpdir } from 'node:os'
|
|
7
|
+
import { stageCandidate, listCandidates, pruneCandidates, withdrawSource } from './candidates.mjs'
|
|
8
|
+
import { adoptCandidate, withdrawManagedSource, recoverMemoryPublication, writeMemory, storeRevision, indexLine, searchStore, lookupLinks, statusOf, forgetMemory, apply } from './index.mjs'
|
|
9
|
+
|
|
10
|
+
function box(run) { const dir = realpathSync(mkdtempSync(join(tmpdir(), 'dsh-withdrawal-'))); try { return run(dir) } finally { rmSync(dir, { recursive: true, force: true }) } }
|
|
11
|
+
function adopted(dir, detail = '唯一细节:只用于室内对白镜头。', source = 'source-1') {
|
|
12
|
+
const payload = { type: 'user', scope: 'project', summary: '唯一判断:柔和低反差光线', detail, sessionId: source,
|
|
13
|
+
indexRevision: storeRevision(dir), evidence: [{ messageId: 'message-1', hash: 'b'.repeat(64), quote: '这个项目的夜戏采用低反差光线' }] }
|
|
14
|
+
const item = stageCandidate(dir, payload)
|
|
15
|
+
const view = adoptCandidate(dir, item.id)
|
|
16
|
+
const result = adoptCandidate(dir, item.id, { apply: true, reviewedIndex: view.reviewedIndex, candidateRevision: view.candidateRevision })
|
|
17
|
+
return { payload, id: item.id, memoryId: result.memoryId }
|
|
18
|
+
}
|
|
19
|
+
function withdrawalOptions(dir, source = 'source-1') {
|
|
20
|
+
const preview = withdrawManagedSource(dir, source, 'message-1')
|
|
21
|
+
return { apply: true, reviewedIndex: preview.reviewedIndex, candidateRevision: preview.candidateRevision }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
test('来源撤回默认预览,不改候选、主题或索引', () => box((dir) => {
|
|
25
|
+
adopted(dir)
|
|
26
|
+
const before = new Map(readdirSync(dir).map((name) => [name, readFileSync(join(dir, name), 'utf8')]))
|
|
27
|
+
const view = withdrawManagedSource(dir, 'source-1', 'message-1')
|
|
28
|
+
assert.equal(view.effectiveRemoved, 1)
|
|
29
|
+
assert.equal(view.applied, false)
|
|
30
|
+
for (const [name, raw] of before) assert.equal(readFileSync(join(dir, name), 'utf8'), raw)
|
|
31
|
+
}))
|
|
32
|
+
test('撤回清除受管索引、标记细节、候选原文与审计历史正文,保留无关内容', () => box((dir) => {
|
|
33
|
+
const item = adopted(dir)
|
|
34
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '其他有效决定保持使用横屏构图'), detail: '另一项细节保持有效。' })
|
|
35
|
+
const result = withdrawManagedSource(dir, 'source-1', 'message-1', withdrawalOptions(dir))
|
|
36
|
+
assert.equal(result.effectiveRemoved, 1)
|
|
37
|
+
for (const name of ['MEMORY.md', 'user.md', '.candidates.json', '.audit.jsonl', '.history.jsonl']) {
|
|
38
|
+
const raw = readFileSync(join(dir, name), 'utf8')
|
|
39
|
+
assert.ok(!raw.includes(item.payload.summary), name)
|
|
40
|
+
assert.ok(!raw.includes(item.payload.detail), name)
|
|
41
|
+
assert.ok(!raw.includes(item.payload.evidence[0].quote), name)
|
|
42
|
+
}
|
|
43
|
+
assert.match(readFileSync(join(dir, 'MEMORY.md'), 'utf8'), /其他有效决定/)
|
|
44
|
+
assert.equal(listCandidates(dir, { status: 'withdrawn' }).total, 1)
|
|
45
|
+
assert.equal(lookupLinks(dir, { id: item.memoryId }).found, false)
|
|
46
|
+
}))
|
|
47
|
+
test('撤回后恢复旧索引、主题和历史,检索/关系/注入仍隐藏撤回内容', () => box((dir) => {
|
|
48
|
+
const item = adopted(dir)
|
|
49
|
+
const copies = ['MEMORY.md', 'user.md', '.history.jsonl', '.audit.jsonl'].map((name) => [name, readFileSync(join(dir, name), 'utf8')])
|
|
50
|
+
withdrawManagedSource(dir, 'source-1', 'message-1', withdrawalOptions(dir))
|
|
51
|
+
pruneCandidates(dir, { apply: true })
|
|
52
|
+
for (const [name, raw] of copies) writeFileSync(join(dir, name), raw)
|
|
53
|
+
assert.equal(searchStore(dir, '低反差').hits.length, 0)
|
|
54
|
+
assert.equal(searchStore(dir, '唯一细节').hits.length, 0)
|
|
55
|
+
assert.equal(searchStore(dir, '低反差', { mode: 'ranked' }).hits.length, 0)
|
|
56
|
+
assert.equal(lookupLinks(dir, { id: item.memoryId }).found, false)
|
|
57
|
+
assert.doesNotMatch(JSON.stringify(statusOf(dir).recent), /唯一判断|唯一细节|低反差/)
|
|
58
|
+
const oldHome = process.env.DSH_HOME
|
|
59
|
+
const home = realpathSync(mkdtempSync(join(tmpdir(), 'dsh-withdraw-provider-')))
|
|
60
|
+
try {
|
|
61
|
+
cpSync(dir, join(home, 'memory'), { recursive: true })
|
|
62
|
+
process.env.DSH_HOME = home
|
|
63
|
+
let context
|
|
64
|
+
apply({ systemPrompt: { context: (c) => context = c }, workingDirectory: { get: () => dir }, tools: { register() {} } }, { autoCapture: { enabled: false } })
|
|
65
|
+
const rendered = context.text({ agent: { session: {} } })
|
|
66
|
+
assert.match(rendered, /memory-store/)
|
|
67
|
+
assert.doesNotMatch(rendered, /唯一判断|唯一细节/)
|
|
68
|
+
} finally { if (oldHome === undefined) delete process.env.DSH_HOME; else process.env.DSH_HOME = oldHome; rmSync(home, { recursive: true, force: true }) }
|
|
69
|
+
}))
|
|
70
|
+
test('后续写入不会把恢复旧文件中的撤回条目重新复制为新记忆或关系', () => box((dir) => {
|
|
71
|
+
adopted(dir)
|
|
72
|
+
const index = readFileSync(join(dir, 'MEMORY.md'), 'utf8'), topic = readFileSync(join(dir, 'user.md'), 'utf8')
|
|
73
|
+
withdrawManagedSource(dir, 'source-1', 'message-1', withdrawalOptions(dir))
|
|
74
|
+
writeFileSync(join(dir, 'MEMORY.md'), index)
|
|
75
|
+
writeFileSync(join(dir, 'user.md'), topic)
|
|
76
|
+
const fresh = writeMemory({ type: 'user', dir, line: indexLine('user', '另一个来源的新独立约定'), detail: '新的主题细节保留' })
|
|
77
|
+
assert.doesNotMatch(readFileSync(join(dir, 'MEMORY.md'), 'utf8'), /唯一判断/)
|
|
78
|
+
assert.doesNotMatch(readFileSync(join(dir, 'user.md'), 'utf8'), /唯一细节/)
|
|
79
|
+
assert.doesNotMatch(JSON.stringify(lookupLinks(dir, { id: fresh.id })), /唯一判断|唯一细节/)
|
|
80
|
+
}))
|
|
81
|
+
test('撤回对包含伪日期条目的多行细节也完全生效', () => box((dir) => {
|
|
82
|
+
adopted(dir, '唯一细节第一段\n- 2026-10-29|唯一细节第二段不能泄漏')
|
|
83
|
+
const topic = readFileSync(join(dir, 'user.md'), 'utf8')
|
|
84
|
+
withdrawManagedSource(dir, 'source-1', 'message-1', withdrawalOptions(dir))
|
|
85
|
+
writeFileSync(join(dir, 'user.md'), topic)
|
|
86
|
+
assert.equal(searchStore(dir, '唯一细节').hits.length, 0)
|
|
87
|
+
}))
|
|
88
|
+
test('伪造已撤回候选的标记,不得让无关正文被当成权威撤回块', () => box((dir) => {
|
|
89
|
+
const item = adopted(dir)
|
|
90
|
+
withdrawManagedSource(dir, 'source-1', 'message-1', withdrawalOptions(dir))
|
|
91
|
+
writeFileSync(join(dir, 'user.md'), `# user\n\n- 2026-10-29|[candidate:${item.id}]\n无关正文必须保持可检索\n`)
|
|
92
|
+
assert.equal(searchStore(dir, '无关正文').hits.length, 1)
|
|
93
|
+
}))
|
|
94
|
+
test('同根晚到写入被拒绝;新根经显式审核可重新确认同一判断', () => box((dir) => {
|
|
95
|
+
const item = adopted(dir)
|
|
96
|
+
withdrawManagedSource(dir, 'source-1', 'message-1', withdrawalOptions(dir))
|
|
97
|
+
assert.throws(() => writeMemory({ type: 'user', dir, line: indexLine('user', '旧来源换个措辞重新写入'), provenance: { sessionId: 'source-1', evidence: item.payload.evidence } }), /source-withdrawn/)
|
|
98
|
+
assert.equal(stageCandidate(dir, { ...item.payload, summary: '旧根再次改写也不应进入候选' }).status, 'withdrawn')
|
|
99
|
+
assert.throws(() => writeMemory({ type: 'user', dir, line: indexLine('user', item.payload.summary) }), /withdrawn-content/)
|
|
100
|
+
const fresh = adopted(dir, item.payload.detail, 'fresh-owner-message')
|
|
101
|
+
assert.equal(searchStore(dir, item.payload.summary).hits.length, 1)
|
|
102
|
+
assert.equal(listCandidates(dir, { status: 'adopted' }).records[0].id, fresh.id)
|
|
103
|
+
}))
|
|
104
|
+
test('轮转的超过512KiB日志也清除来源原文', () => box((dir) => {
|
|
105
|
+
const item = adopted(dir)
|
|
106
|
+
const filler = (JSON.stringify({ op: 'unrelated', note: 'x'.repeat(100) }) + '\n').repeat(5000)
|
|
107
|
+
for (const name of ['.audit.jsonl', '.history.jsonl']) writeFileSync(join(dir, name + '.1'), filler + readFileSync(join(dir, name), 'utf8'))
|
|
108
|
+
withdrawManagedSource(dir, 'source-1', 'message-1', withdrawalOptions(dir))
|
|
109
|
+
for (const name of ['.audit.jsonl.1', '.history.jsonl.1']) assert.ok(!readFileSync(join(dir, name), 'utf8').includes(item.payload.summary))
|
|
110
|
+
}))
|
|
111
|
+
test('旧版无标记细节不能假装被追踪并静默清除', () => box((dir) => {
|
|
112
|
+
adopted(dir)
|
|
113
|
+
writeFileSync(join(dir, 'user.md'), '# user\n\n- 2026-10-29|唯一细节:只用于室内对白镜头。\n')
|
|
114
|
+
assert.throws(() => withdrawManagedSource(dir, 'source-1', 'message-1'), /untracked-detail/)
|
|
115
|
+
assert.equal(listCandidates(dir, { status: 'adopted' }).total, 1)
|
|
116
|
+
}))
|
|
117
|
+
test('同一有效文本还有独立采纳来源时,不静默删除它', () => box((dir) => {
|
|
118
|
+
adopted(dir)
|
|
119
|
+
forgetMemory(dir, '唯一判断')
|
|
120
|
+
adopted(dir, '另一个独立来源的细节', 'independent-source')
|
|
121
|
+
assert.throws(() => withdrawManagedSource(dir, 'source-1', 'message-1'), /shared-effective-memory/)
|
|
122
|
+
assert.equal(searchStore(dir, '唯一判断').hits.length, 1)
|
|
123
|
+
assert.equal(listCandidates(dir, { status: 'adopted' }).total, 2)
|
|
124
|
+
}))
|
|
125
|
+
test('旧自动捕获有审计来源且仅有摘要时可以撤回,无来源细节则拒绝', () => box((dir) => {
|
|
126
|
+
const evidence = [{ messageId: 'message-1', hash: 'b'.repeat(64), quote: '这句用户原话用于核对来源' }]
|
|
127
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '旧自动捕获的唯一摘要'), provenance: { sessionId: 'source-1', evidence, via: 'auto-capture' } })
|
|
128
|
+
withdrawManagedSource(dir, 'source-1', 'message-1', withdrawalOptions(dir))
|
|
129
|
+
assert.equal(searchStore(dir, '唯一摘要').hits.length, 0)
|
|
130
|
+
}))
|
|
131
|
+
test('已经只撤回候选入口的来源,仍可继续清理有效摘要', () => box((dir) => {
|
|
132
|
+
const evidence = [{ messageId: 'message-1', hash: 'b'.repeat(64), quote: '这条来源同时产生摘要与候选' }]
|
|
133
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '这个旧摘要也必须被有效撤回'), provenance: { sessionId: 'source-1', evidence } })
|
|
134
|
+
withdrawSource(dir, 'source-1', 'message-1')
|
|
135
|
+
const options = withdrawalOptions(dir)
|
|
136
|
+
assert.equal(withdrawManagedSource(dir, 'source-1', 'message-1', options).effectiveRemoved, 1)
|
|
137
|
+
assert.equal(searchStore(dir, '旧摘要').hits.length, 0)
|
|
138
|
+
}))
|
|
139
|
+
test('无法追踪旧自动捕获的主题细节时拒绝,不把部分清理当完成', () => box((dir) => {
|
|
140
|
+
const evidence = [{ messageId: 'message-1', hash: 'b'.repeat(64), quote: '这条旧来源没有受管细节标记' }]
|
|
141
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '旧捕获包含未标记细节'), detail: '不能凭猜测删除的原始细节', provenance: { sessionId: 'source-1', evidence } })
|
|
142
|
+
assert.throws(() => withdrawManagedSource(dir, 'source-1', 'message-1'), /untracked-detail/)
|
|
143
|
+
assert.equal(searchStore(dir, '未标记细节').hits.length, 1)
|
|
144
|
+
}))
|
|
145
|
+
test('旧写入只有会话但没有消息来源时,不声称已清完该来源', () => box((dir) => {
|
|
146
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '缺少原消息定位的历史决定'), provenance: { sessionId: 'source-1', via: 'auto-capture' } })
|
|
147
|
+
assert.throws(() => withdrawManagedSource(dir, 'source-1', 'message-1'), /missing-source-lineage/)
|
|
148
|
+
assert.equal(searchStore(dir, '历史决定').hits.length, 1)
|
|
149
|
+
}))
|
|
150
|
+
for (const at of ['journal', 0, 1, 2, 3, 4]) test(`撤回在 ${at} 中断后可恢复且不能读取半撤回内容`, () => box((dir) => {
|
|
151
|
+
adopted(dir)
|
|
152
|
+
const options = withdrawalOptions(dir)
|
|
153
|
+
assert.throws(() => withdrawManagedSource(dir, 'source-1', 'message-1', { ...options, publicationFault: (e) => { if (e.phase === at || e.phase === 'file' && e.index === at) throw new Error('interrupted') } }), /interrupted/)
|
|
154
|
+
assert.throws(() => searchStore(dir, '低反差'), /needs-recovery/)
|
|
155
|
+
recoverMemoryPublication(dir, { apply: true })
|
|
156
|
+
assert.equal(searchStore(dir, '低反差').hits.length, 0)
|
|
157
|
+
assert.equal(withdrawManagedSource(dir, 'source-1', 'message-1', options).duplicate, true)
|
|
158
|
+
}))
|
|
159
|
+
test('外部修改、坏日志或过期预览均不被强行覆盖', () => box((dir) => {
|
|
160
|
+
adopted(dir)
|
|
161
|
+
const options = withdrawalOptions(dir)
|
|
162
|
+
writeMemory({ type: 'project', dir, line: indexLine('project', '刚刚新增的一项独立决定') })
|
|
163
|
+
assert.throws(() => withdrawManagedSource(dir, 'source-1', 'message-1', options), /stale-index/)
|
|
164
|
+
writeFileSync(join(dir, '.audit.jsonl'), 'invalid JSON')
|
|
165
|
+
assert.throws(() => withdrawManagedSource(dir, 'source-1', 'message-1'), /corrupt-log/)
|
|
166
|
+
assert.equal(listCandidates(dir, { status: 'adopted' }).total, 1)
|
|
167
|
+
}))
|