recallnest 2.3.0 → 2.6.0
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 +187 -0
- package/README.md +49 -38
- package/README_CN.md +46 -31
- package/assets/ui/app.js +1031 -0
- package/assets/ui/index.html +224 -0
- package/assets/ui/styles.css +781 -0
- package/config.json.example +4 -1
- package/docs/api-reference.md +1291 -0
- package/docs/architecture.md +168 -0
- package/docs/connector-spec.md +126 -0
- package/eval/cases-canary.json +58 -0
- package/eval/cases-scope-robustness.json +278 -0
- package/eval/cases.json +117 -20
- package/package.json +25 -4
- package/src/__tests__/access-tracker-novelty.test.ts +304 -0
- package/src/__tests__/activity-counter.test.ts +125 -0
- package/src/__tests__/adaptive-fidelity.test.ts +102 -0
- package/src/__tests__/admission-control.test.ts +206 -0
- package/src/__tests__/anchor-generator.test.ts +67 -0
- package/src/__tests__/api-server-security.test.ts +102 -0
- package/src/__tests__/auto-consolidation.test.ts +168 -0
- package/src/__tests__/auto-recall.test.ts +131 -0
- package/src/__tests__/babel-integration.test.ts +144 -0
- package/src/__tests__/batch-dedup.test.ts +65 -0
- package/src/__tests__/belief-history.test.ts +220 -0
- package/src/__tests__/canary-eval.test.ts +114 -0
- package/src/__tests__/capture-engine.test.ts +1328 -0
- package/src/__tests__/capture-heuristic.test.ts +171 -0
- package/src/__tests__/cascade-forget.test.ts +210 -0
- package/src/__tests__/category-min-scores.test.ts +116 -0
- package/src/__tests__/cc8-cc1-essential-context.test.ts +298 -0
- package/src/__tests__/circuit-breaker.test.ts +99 -0
- package/src/__tests__/cluster-consolidation.test.ts +945 -0
- package/src/__tests__/confidence-metadata.test.ts +125 -0
- package/src/__tests__/confidence-tracker.test.ts +261 -0
- package/src/__tests__/conflict-advisor.test.ts +206 -0
- package/src/__tests__/conflict-escalation.test.ts +149 -0
- package/src/__tests__/conflict-lifecycle.test.ts +83 -0
- package/src/__tests__/conflict-output.test.ts +85 -0
- package/src/__tests__/conflict-store.test.ts +418 -0
- package/src/__tests__/connector-v1.test.ts +181 -0
- package/src/__tests__/consolidation-engine.test.ts +430 -0
- package/src/__tests__/constructive-retrieval.test.ts +509 -0
- package/src/__tests__/context-collapse-renderer.test.ts +214 -0
- package/src/__tests__/context-composer-pins.test.ts +64 -0
- package/src/__tests__/context-composer-task-selection.test.ts +24 -0
- package/src/__tests__/context-reconstructor.test.ts +65 -0
- package/src/__tests__/context-renderer.test.ts +91 -0
- package/src/__tests__/continuity-eval.test.ts +275 -0
- package/src/__tests__/continuity-schema.test.ts +97 -0
- package/src/__tests__/contracts.test.ts +262 -0
- package/src/__tests__/conversation-importer.test.ts +692 -0
- package/src/__tests__/core-recall-eval.test.ts +235 -0
- package/src/__tests__/core-summary.test.ts +109 -0
- package/src/__tests__/data-checkup.test.ts +276 -0
- package/src/__tests__/decay-exemption.test.ts +129 -0
- package/src/__tests__/dedup-actions.test.ts +58 -0
- package/src/__tests__/dedup-guard.test.ts +39 -0
- package/src/__tests__/deterministic-id.test.ts +39 -0
- package/src/__tests__/distill-lock.test.ts +305 -0
- package/src/__tests__/distill-session-argshape.test.ts +79 -0
- package/src/__tests__/doctor.test.ts +319 -0
- package/src/__tests__/dream-pipeline.test.ts +780 -0
- package/src/__tests__/drill-down.test.ts +108 -0
- package/src/__tests__/embedder.test.ts +122 -0
- package/src/__tests__/emotion-decay.test.ts +46 -0
- package/src/__tests__/emotion-detector.test.ts +56 -0
- package/src/__tests__/emotion-phase1-integration.test.ts +153 -0
- package/src/__tests__/emotion-retrieval.test.ts +56 -0
- package/src/__tests__/entity-resolver.test.ts +58 -0
- package/src/__tests__/env-config.test.ts +151 -0
- package/src/__tests__/error-signature-boost.test.ts +87 -0
- package/src/__tests__/error-signature.test.ts +95 -0
- package/src/__tests__/error-taxonomy.test.ts +129 -0
- package/src/__tests__/ethics-layer.test.ts +476 -0
- package/src/__tests__/event-segmenter.test.ts +153 -0
- package/src/__tests__/feature-flags-integration.test.ts +54 -0
- package/src/__tests__/frequency-tracker.test.ts +113 -0
- package/src/__tests__/freshness.test.ts +186 -0
- package/src/__tests__/graph-export.test.ts +489 -0
- package/src/__tests__/hotness-blend.test.ts +283 -0
- package/src/__tests__/ingest-chunking.test.ts +44 -0
- package/src/__tests__/ingest.test.ts +360 -0
- package/src/__tests__/interference-detector.test.ts +203 -0
- package/src/__tests__/kg-extractor.test.ts +192 -0
- package/src/__tests__/kg-store.test.ts +300 -0
- package/src/__tests__/llm-client-pivot.test.ts +628 -0
- package/src/__tests__/llm-consolidation.test.ts +226 -0
- package/src/__tests__/maintenance-locks.test.ts +189 -0
- package/src/__tests__/mcp-efficiency.test.ts +288 -0
- package/src/__tests__/mcp-related-scopes.test.ts +151 -0
- package/src/__tests__/memory-boundaries.test.ts +282 -0
- package/src/__tests__/memory-evolution-b2-b3-e1.test.ts +345 -0
- package/src/__tests__/memory-evolution.test.ts +271 -0
- package/src/__tests__/memory-health-rebalance.test.ts +144 -0
- package/src/__tests__/memory-lint.test.ts +421 -0
- package/src/__tests__/memory-output.test.ts +215 -0
- package/src/__tests__/memory-promotion.test.ts +315 -0
- package/src/__tests__/migrate-dedup-ids.test.ts +65 -0
- package/src/__tests__/multi-vector.test.ts +242 -0
- package/src/__tests__/narrative-architecture.test.ts +456 -0
- package/src/__tests__/noise-filter.test.ts +205 -0
- package/src/__tests__/noise-prototype-bank.test.ts +280 -0
- package/src/__tests__/obsidian-connector.test.ts +363 -0
- package/src/__tests__/omitted-reporting.test.ts +98 -0
- package/src/__tests__/pending-review.test.ts +77 -0
- package/src/__tests__/pii-redact.test.ts +333 -0
- package/src/__tests__/pivot-apply.test.ts +373 -0
- package/src/__tests__/pivot-distill-supervisor.test.ts +457 -0
- package/src/__tests__/pivot-distill.test.ts +1178 -0
- package/src/__tests__/post-retrieval-filter.test.ts +178 -0
- package/src/__tests__/ppr-traversal.test.ts +256 -0
- package/src/__tests__/prediction-engine.test.ts +349 -0
- package/src/__tests__/predictive-prospective-memory.test.ts +416 -0
- package/src/__tests__/preference-guard-smoke.test.ts +211 -0
- package/src/__tests__/preference-matcher.test.ts +282 -0
- package/src/__tests__/preference-slots.test.ts +205 -0
- package/src/__tests__/prospective-memory.test.ts +283 -0
- package/src/__tests__/pull-from-macbook-sync-only.test.ts +679 -0
- package/src/__tests__/query-entity-detector.test.ts +171 -0
- package/src/__tests__/query-expander-alias.test.ts +146 -0
- package/src/__tests__/query-expander.test.ts +34 -0
- package/src/__tests__/recall-gate.test.ts +266 -0
- package/src/__tests__/recall-governor.test.ts +227 -0
- package/src/__tests__/recall-mode.test.ts +64 -0
- package/src/__tests__/resolve-db-path.test.ts +130 -0
- package/src/__tests__/resolve-tier.test.ts +64 -0
- package/src/__tests__/result-synthesizer.test.ts +198 -0
- package/src/__tests__/resume-context.test.ts +4898 -0
- package/src/__tests__/retrieval-eval.test.ts +163 -0
- package/src/__tests__/retrieval-trace.test.ts +213 -0
- package/src/__tests__/retriever-boundary-weight.test.ts +136 -0
- package/src/__tests__/retriever-lazy-init-dispatch.test.ts +123 -0
- package/src/__tests__/retroactive-boost.test.ts +223 -0
- package/src/__tests__/salience-scoring.test.ts +106 -0
- package/src/__tests__/scope-inventory.test.ts +200 -0
- package/src/__tests__/scope-policy.test.ts +69 -0
- package/src/__tests__/scope-suggester.test.ts +80 -0
- package/src/__tests__/session-checkpoint.test.ts +291 -0
- package/src/__tests__/session-distiller.test.ts +640 -0
- package/src/__tests__/session-gc.test.ts +215 -0
- package/src/__tests__/skill-engine.test.ts +552 -0
- package/src/__tests__/skill-promotion.test.ts +654 -0
- package/src/__tests__/skill-verifier.test.ts +122 -0
- package/src/__tests__/source-heartbeat.test.ts +250 -0
- package/src/__tests__/stable-context-length-contract.test.ts +62 -0
- package/src/__tests__/store-patch-metadata.test.ts +205 -0
- package/src/__tests__/store-read-consistency.test.ts +122 -0
- package/src/__tests__/store-schema-migration.test.ts +87 -0
- package/src/__tests__/store-scope-audit.test.ts +81 -0
- package/src/__tests__/store-upsert.test.ts +201 -0
- package/src/__tests__/supersede-chain.test.ts +154 -0
- package/src/__tests__/temporal-parser.test.ts +241 -0
- package/src/__tests__/temporal-validity.test.ts +160 -0
- package/src/__tests__/term-registry-terminal-roster.test.ts +127 -0
- package/src/__tests__/term-registry.test.ts +121 -0
- package/src/__tests__/tool-error-surfacing.test.ts +98 -0
- package/src/__tests__/tool-output-compressor.test.ts +160 -0
- package/src/__tests__/topic-tag.test.ts +113 -0
- package/src/__tests__/trusted-memory.test.ts +839 -0
- package/src/__tests__/ui-server-security.test.ts +93 -0
- package/src/__tests__/usage-tracker.test.ts +287 -0
- package/src/__tests__/version-manager.test.ts +213 -0
- package/src/__tests__/workflow-observation-skill-outcome.test.ts +352 -0
- package/src/__tests__/workflow-observation.test.ts +433 -0
- package/src/__tests__/write-verifier.test.ts +122 -0
- package/src/access-tracker.ts +90 -34
- package/src/activity-counter.ts +46 -18
- package/src/adaptive-retrieval.ts +11 -0
- package/src/aliases.ts +133 -0
- package/src/api-server.ts +36 -9
- package/src/audit-log.ts +4 -2
- package/src/auto-consolidation.ts +8 -3
- package/src/auto-gc.ts +85 -21
- package/src/auto-recall.ts +61 -1
- package/src/belief-history.ts +164 -0
- package/src/capture-engine.ts +142 -32
- package/src/cli.ts +268 -41
- package/src/confidence-tracker.ts +36 -18
- package/src/conflict-engine.ts +24 -3
- package/src/consolidation-engine.ts +255 -23
- package/src/context-composer-stable-selection.ts +7 -1
- package/src/context-composer.ts +8 -1
- package/src/data-checkup.ts +96 -7
- package/src/decay-engine.ts +59 -7
- package/src/distill-lock.ts +194 -15
- package/src/doctor.ts +13 -4
- package/src/dream-pipeline.ts +473 -41
- package/src/env-config.ts +118 -0
- package/src/error-signature.ts +153 -0
- package/src/error-taxonomy.ts +115 -0
- package/src/eval.ts +402 -14
- package/src/freshness.ts +192 -0
- package/src/http-request-policy.ts +148 -0
- package/src/idempotency.ts +8 -0
- package/src/ingest.ts +308 -12
- package/src/kg-extractor.ts +13 -3
- package/src/kg-store.ts +236 -44
- package/src/llm-client.ts +106 -11
- package/src/llm-consolidation.ts +2 -1
- package/src/mcp-server.ts +87 -1800
- package/src/mcp-tool-deps.ts +48 -0
- package/src/mcp-tools-advanced.ts +954 -0
- package/src/mcp-tools-core.ts +637 -0
- package/src/mcp-tools-governance.ts +475 -0
- package/src/memory-boundaries.ts +26 -3
- package/src/memory-evolution.ts +73 -1
- package/src/memory-health-rebalance.ts +4 -0
- package/src/memory-lint.ts +183 -10
- package/src/memory-output.ts +129 -4
- package/src/memory-promotion.ts +246 -0
- package/src/memory-schema.ts +25 -2
- package/src/memory-store-port.ts +46 -0
- package/src/multi-vector.ts +2 -1
- package/src/narrative-schema.ts +2 -1
- package/src/noise-filter.ts +57 -6
- package/src/noise-prototype-bank.ts +408 -0
- package/src/pii-detector.ts +207 -5
- package/src/pivot-apply.ts +399 -0
- package/src/preference-matcher.ts +37 -3
- package/src/query-expander.ts +90 -9
- package/src/recall-gate.ts +305 -0
- package/src/result-synthesizer.ts +2 -1
- package/src/retention-policy.ts +2 -1
- package/src/retrieval-profiles.ts +46 -4
- package/src/retriever.ts +354 -157
- package/src/retroactive-boost.ts +6 -2
- package/src/runtime-config.ts +78 -6
- package/src/scope-suggester.ts +113 -0
- package/src/session-distiller.ts +23 -2
- package/src/session-engine.ts +2 -1
- package/src/session-schema.ts +1 -0
- package/src/session-store.ts +5 -0
- package/src/skill-engine.ts +78 -0
- package/src/skill-promotion.ts +304 -48
- package/src/skill-schema.ts +16 -3
- package/src/skill-verifier.ts +152 -0
- package/src/store.ts +321 -57
- package/src/term-registry.ts +35 -0
- package/src/tool-output-compressor.ts +3 -1
- package/src/ui-server.ts +9 -1
- package/src/usage-tracker.ts +242 -0
- package/src/version-manager.ts +3 -2
- package/src/version.ts +19 -0
- package/src/workflow-observation-engine.ts +2 -1
- package/src/workflow-observation-schema.ts +2 -1
- package/src/workflow-observation-store.ts +6 -2
- package/.env +0 -4
- package/CLAUDE.md +0 -46
- package/config.json +0 -55
- package/daily-recall.sh +0 -110
- package/logs/daily-recall.error.log +0 -0
- package/logs/daily-recall.log +0 -60
- package/logs/ingest-2026-04-11.log +0 -110
- package/logs/ingest-2026-04-12.log +0 -90
- package/logs/ingest-2026-04-13.log +0 -87
- package/logs/launchagent-stderr.log +0 -0
- package/logs/launchagent-stdout.log +0 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { createRetriever, type RetrievalResultSet } from "../retriever.js";
|
|
4
|
+
|
|
5
|
+
// P-omitted: 验证召回因 limit 上限截断时,对调用方暴露 omitted(提高 limit 可见)。
|
|
6
|
+
// 借鉴 RepoPrompt CE「剪枝必回报」——召回从黑箱变可协商。
|
|
7
|
+
|
|
8
|
+
function buildCandidate(id: string, score: number) {
|
|
9
|
+
return {
|
|
10
|
+
entry: {
|
|
11
|
+
id,
|
|
12
|
+
text: `distinct memory content number ${id}`,
|
|
13
|
+
vector: [1, 0, 0],
|
|
14
|
+
category: "preferences",
|
|
15
|
+
scope: "memory:agent",
|
|
16
|
+
importance: 0.8,
|
|
17
|
+
timestamp: Date.parse("2026-03-16T00:00:00.000Z"),
|
|
18
|
+
metadata: JSON.stringify({}),
|
|
19
|
+
},
|
|
20
|
+
score,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 最小过滤 config:mode=vector 跳过 BM25/rerank;minScore/hardMinScore=0 + 高分候选避免 hard-min 截断;
|
|
25
|
+
// filterNoise=false;RIF/sourceDiversity 字段缺省即不启用;MMR 只重排不删。
|
|
26
|
+
// 于是过滤链净效果 = sort → slice(limit),omitted = candidates - limit 可精确断言。
|
|
27
|
+
function makeRetriever(
|
|
28
|
+
candidates: ReturnType<typeof buildCandidate>[],
|
|
29
|
+
configOverride: Record<string, unknown> = {},
|
|
30
|
+
) {
|
|
31
|
+
return createRetriever({
|
|
32
|
+
hasFtsSupport: false,
|
|
33
|
+
// 模拟真实 MemoryStore.vectorSearch:尊重 k 参数(按 k 截断返回)。
|
|
34
|
+
// 这样测试能真实反映 over-fetch 是否生效——若 vector-only 不 over-fetch、只取 limit 条,
|
|
35
|
+
// 候选池 ≤ limit、omitted 必为 0,下面断言会失败(Codex review 暴露的假绿点)。
|
|
36
|
+
async vectorSearch(_vec: number[], k: number) { return candidates.slice(0, k); },
|
|
37
|
+
} as any, {
|
|
38
|
+
async embedQuery() { return [1, 0, 0]; },
|
|
39
|
+
async embedPassage() { return [1, 0, 0]; },
|
|
40
|
+
} as any, {
|
|
41
|
+
mode: "vector",
|
|
42
|
+
rerank: "none",
|
|
43
|
+
filterNoise: false,
|
|
44
|
+
hardMinScore: 0,
|
|
45
|
+
minScore: 0,
|
|
46
|
+
recencyWeight: 0,
|
|
47
|
+
timeDecayHalfLifeDays: 0,
|
|
48
|
+
...configOverride,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe("retrieval omitted reporting (P-omitted)", () => {
|
|
53
|
+
it("reports omitted count + reason when candidates exceed limit", async () => {
|
|
54
|
+
const candidates = Array.from({ length: 8 }, (_, i) => buildCandidate(`m${i}`, 0.9 - i * 0.01));
|
|
55
|
+
const retriever = makeRetriever(candidates);
|
|
56
|
+
|
|
57
|
+
const results = await retriever.retrieve({ query: "user reply style preference detail", limit: 3, category: "preferences" }) as RetrievalResultSet;
|
|
58
|
+
|
|
59
|
+
expect(results).toHaveLength(3);
|
|
60
|
+
expect(results.omitted).toBeDefined();
|
|
61
|
+
expect(results.omitted?.count).toBe(5); // 8 candidates - limit 3
|
|
62
|
+
expect(results.omitted?.reason).toBe("limit");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("leaves omitted undefined when candidates fit within limit", async () => {
|
|
66
|
+
const candidates = Array.from({ length: 2 }, (_, i) => buildCandidate(`m${i}`, 0.9 - i * 0.01));
|
|
67
|
+
const retriever = makeRetriever(candidates);
|
|
68
|
+
|
|
69
|
+
const results = await retriever.retrieve({ query: "user reply style preference detail", limit: 5, category: "preferences" }) as RetrievalResultSet;
|
|
70
|
+
|
|
71
|
+
expect(results).toHaveLength(2);
|
|
72
|
+
expect(results.omitted).toBeUndefined();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("persists omitted through the validity filter (regression: array is rebuilt downstream)", async () => {
|
|
76
|
+
// retrieve() runs filterByValidity which returns a NEW array, dropping any
|
|
77
|
+
// property hung on the result array. omitted must survive via the resultSet snapshot.
|
|
78
|
+
const candidates = Array.from({ length: 6 }, (_, i) => buildCandidate(`m${i}`, 0.9 - i * 0.01));
|
|
79
|
+
const retriever = makeRetriever(candidates);
|
|
80
|
+
|
|
81
|
+
const results = await retriever.retrieve({ query: "user reply style preference detail", limit: 2, category: "preferences" }) as RetrievalResultSet;
|
|
82
|
+
|
|
83
|
+
expect(results).toHaveLength(2);
|
|
84
|
+
expect(results.omitted?.count).toBe(4); // 6 - 2
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("reports omitted even when sourceDiversity caps the diversified pool (P3-1)", async () => {
|
|
88
|
+
// 启用 sourceDiversity 时 applySourceDiversity 先把 diversified 压到 limit;
|
|
89
|
+
// omitted 必须从 source-diversity 之前的候选池(clusterDeduped)算,否则恒为 0(Codex 复审 P3-1)。
|
|
90
|
+
const candidates = Array.from({ length: 8 }, (_, i) => buildCandidate(`m${i}`, 0.9 - i * 0.01));
|
|
91
|
+
const retriever = makeRetriever(candidates, { sourceDiversity: 0.5 });
|
|
92
|
+
|
|
93
|
+
const results = await retriever.retrieve({ query: "user reply style preference detail", limit: 3, category: "preferences" }) as RetrievalResultSet;
|
|
94
|
+
|
|
95
|
+
expect(results).toHaveLength(3);
|
|
96
|
+
expect(results.omitted?.count).toBe(5); // 8 - 3,即使 source diversity 压缩了 diversified
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, it, expect } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
parseEvolution,
|
|
4
|
+
patchEvolution,
|
|
5
|
+
defaultEvolution,
|
|
6
|
+
buildPendingReviewMetadata,
|
|
7
|
+
isPendingReview,
|
|
8
|
+
resolvePendingReview,
|
|
9
|
+
isActiveMemory,
|
|
10
|
+
} from "../memory-evolution.js";
|
|
11
|
+
|
|
12
|
+
describe("HP-6: Pending review (delayed judgment)", () => {
|
|
13
|
+
describe("buildPendingReviewMetadata", () => {
|
|
14
|
+
it("marks entry as pending_review", () => {
|
|
15
|
+
const meta = patchEvolution("{}", defaultEvolution());
|
|
16
|
+
const result = buildPendingReviewMetadata(meta);
|
|
17
|
+
const evo = parseEvolution(result);
|
|
18
|
+
expect(evo.status).toBe("pending_review");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("preserves other evolution fields", () => {
|
|
22
|
+
const meta = patchEvolution("{}", { ...defaultEvolution(), accessCount: 5 });
|
|
23
|
+
const result = buildPendingReviewMetadata(meta);
|
|
24
|
+
const evo = parseEvolution(result);
|
|
25
|
+
expect(evo.status).toBe("pending_review");
|
|
26
|
+
expect(evo.accessCount).toBe(5);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("isPendingReview", () => {
|
|
31
|
+
it("returns true for pending_review status", () => {
|
|
32
|
+
const meta = buildPendingReviewMetadata(patchEvolution("{}", defaultEvolution()));
|
|
33
|
+
expect(isPendingReview(meta)).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("returns false for active status", () => {
|
|
37
|
+
const meta = patchEvolution("{}", defaultEvolution());
|
|
38
|
+
expect(isPendingReview(meta)).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("returns false for undefined", () => {
|
|
42
|
+
expect(isPendingReview(undefined)).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("resolvePendingReview", () => {
|
|
47
|
+
it("transitions pending_review → active", () => {
|
|
48
|
+
const meta = buildPendingReviewMetadata(patchEvolution("{}", defaultEvolution()));
|
|
49
|
+
expect(parseEvolution(meta).status).toBe("pending_review");
|
|
50
|
+
|
|
51
|
+
const resolved = resolvePendingReview(meta);
|
|
52
|
+
expect(parseEvolution(resolved).status).toBe("active");
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("isActiveMemory includes pending_review", () => {
|
|
57
|
+
it("active → true", () => {
|
|
58
|
+
const meta = patchEvolution("{}", defaultEvolution());
|
|
59
|
+
expect(isActiveMemory(meta)).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("pending_review → true (participates in search)", () => {
|
|
63
|
+
const meta = buildPendingReviewMetadata(patchEvolution("{}", defaultEvolution()));
|
|
64
|
+
expect(isActiveMemory(meta)).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("superseded → false", () => {
|
|
68
|
+
const meta = patchEvolution("{}", { ...defaultEvolution(), status: "superseded" });
|
|
69
|
+
expect(isActiveMemory(meta)).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("archived → false", () => {
|
|
73
|
+
const meta = patchEvolution("{}", { ...defaultEvolution(), status: "archived" });
|
|
74
|
+
expect(isActiveMemory(meta)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { redactSecrets, redactForLog, scanForPII } from "../pii-detector.js";
|
|
4
|
+
import { persistMemory } from "../capture-engine.js";
|
|
5
|
+
import type { AuditEntry } from "../audit-log.js";
|
|
6
|
+
|
|
7
|
+
const TEST_SCOPE = "project:test";
|
|
8
|
+
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// redactSecrets — rule coverage
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
describe("redactSecrets", () => {
|
|
14
|
+
it("scrubs Anthropic API keys with the specific type", () => {
|
|
15
|
+
const r = redactSecrets("my key is sk-ant-api03-AbCdEf12345678 ok");
|
|
16
|
+
expect(r.text).toBe("my key is [REDACTED:anthropic_key] ok");
|
|
17
|
+
expect(r.redacted).toBe(1);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("does not scrub the sk tail of hyphenated slugs (task-, risk-)", () => {
|
|
21
|
+
for (const slug of [
|
|
22
|
+
"pivot-decision-task-assignment-worker-stability-fix",
|
|
23
|
+
"pivot-judgment-shift-risk-gated-review-decision-process",
|
|
24
|
+
"note about task-ant-colony-simulation-experiment-log",
|
|
25
|
+
]) {
|
|
26
|
+
const r = redactSecrets(slug);
|
|
27
|
+
expect(r.redacted).toBe(0);
|
|
28
|
+
expect(r.text).toBe(slug);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("still scrubs sk- keys that start their own token", () => {
|
|
33
|
+
const quoted = redactSecrets('key="sk-AbCdEfGhIjKlMnOpQrStUvWx1234"');
|
|
34
|
+
expect(quoted.redacted).toBe(1);
|
|
35
|
+
const lineStart = redactSecrets("sk-AbCdEfGhIjKlMnOpQrStUvWx1234 trailing");
|
|
36
|
+
expect(lineStart.redacted).toBe(1);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("scrubs GitHub tokens", () => {
|
|
40
|
+
const r = redactSecrets("push with ghp_ABCDEFGHIJKLMNOPQRSTUV1234567890");
|
|
41
|
+
expect(r.text).toContain("[REDACTED:github_token]");
|
|
42
|
+
expect(r.text).not.toContain("ghp_");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("scrubs Slack tokens", () => {
|
|
46
|
+
const r = redactSecrets("xoxb-123456789-abcdefghij is the bot token");
|
|
47
|
+
expect(r.text).toContain("[REDACTED:slack_token]");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("scrubs AWS access key IDs", () => {
|
|
51
|
+
const r = redactSecrets("aws AKIAIOSFODNN7EXAMPLE used");
|
|
52
|
+
expect(r.text).toContain("[REDACTED:aws_access_key]");
|
|
53
|
+
expect(r.text).not.toContain("AKIA");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("scrubs Google API keys", () => {
|
|
57
|
+
// Real Google API keys are exactly "AIza" + 35 chars
|
|
58
|
+
const r = redactSecrets(`key ${"AIzaSyB" + "x".repeat(32)} in config`);
|
|
59
|
+
expect(r.text).toContain("[REDACTED:google_api_key]");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("scrubs JWTs", () => {
|
|
63
|
+
const jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U";
|
|
64
|
+
const r = redactSecrets(`token: bearer-less ${jwt} end`);
|
|
65
|
+
expect(r.text).toContain("[REDACTED:jwt]");
|
|
66
|
+
expect(r.text).not.toContain("eyJhbGci");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("scrubs Bearer headers", () => {
|
|
70
|
+
const r = redactSecrets("Authorization: Bearer abcdef1234567890XYZ_token");
|
|
71
|
+
expect(r.text).toContain("[REDACTED:bearer_token]");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("scrubs PEM private key blocks including the body", () => {
|
|
75
|
+
const pem = "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA\nsecretbody\n-----END RSA PRIVATE KEY-----";
|
|
76
|
+
const r = redactSecrets(`config:\n${pem}\ndone`);
|
|
77
|
+
expect(r.text).toContain("[REDACTED:pem_private_key]");
|
|
78
|
+
expect(r.text).not.toContain("MIIEowIBAAKCAQEA");
|
|
79
|
+
expect(r.text).not.toContain("secretbody");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("keeps the key name for password assignments, scrubs only the value", () => {
|
|
83
|
+
const r = redactSecrets("db config password=SuperSecret99 in env");
|
|
84
|
+
expect(r.text).toContain("password=[REDACTED:password]");
|
|
85
|
+
expect(r.text).not.toContain("SuperSecret99");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("keeps the key name for api_key/token/secret assignments", () => {
|
|
89
|
+
const r = redactSecrets("set api_key=abcdefghij1234567890XYZAB then restart");
|
|
90
|
+
expect(r.text).toContain("api_key=[REDACTED:api_key]");
|
|
91
|
+
expect(r.text).not.toContain("abcdefghij1234567890XYZAB");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("scrubs Chinese national ID numbers (checksum-valid)", () => {
|
|
95
|
+
const r = redactSecrets("身份证 11010519491231002X 登记");
|
|
96
|
+
expect(r.text).toContain("[REDACTED:id_number]");
|
|
97
|
+
expect(r.text).not.toContain("11010519491231002X");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("scrubs credit card numbers (Luhn-valid)", () => {
|
|
101
|
+
const r = redactSecrets("card 4111 1111 1111 1111 charged");
|
|
102
|
+
expect(r.text).toContain("[REDACTED:credit_card]");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// codex review 2026-07-10: JSON/YAML/config credential forms must be caught
|
|
106
|
+
it("scrubs JSON-style quoted credential keys", () => {
|
|
107
|
+
const r = redactSecrets('config: {"password": "SuperSecret99", "region": "us"}');
|
|
108
|
+
expect(r.text).not.toContain("SuperSecret99");
|
|
109
|
+
expect(r.text).toContain("[REDACTED:password]");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("scrubs JSON-style quoted api_key", () => {
|
|
113
|
+
const r = redactSecrets('{"api_key": "abcdefghij1234567890XYZAB"}');
|
|
114
|
+
expect(r.text).not.toContain("abcdefghij1234567890XYZAB");
|
|
115
|
+
expect(r.text).toContain("[REDACTED:api_key]");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("scrubs YAML/config assignments with spaces around the separator", () => {
|
|
119
|
+
const r = redactSecrets("password = SuperSecret99\nsecret : abcdefghij1234567890XYZAB");
|
|
120
|
+
expect(r.text).not.toContain("SuperSecret99");
|
|
121
|
+
expect(r.text).not.toContain("abcdefghij1234567890XYZAB");
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// ============================================================================
|
|
126
|
+
// redactSecrets — false-positive guards
|
|
127
|
+
// ============================================================================
|
|
128
|
+
|
|
129
|
+
describe("redactSecrets false-positive guards", () => {
|
|
130
|
+
it("does NOT scrub git commit hashes (40-char hex)", () => {
|
|
131
|
+
const sha = "699bea8f3d2c1a0b9e8d7c6b5a4f3e2d1c0b9a87";
|
|
132
|
+
const r = redactSecrets(`fixed in commit ${sha} on main`);
|
|
133
|
+
expect(r.text).toContain(sha);
|
|
134
|
+
expect(r.redacted).toBe(0);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("does NOT scrub phone numbers or emails (detection-only)", () => {
|
|
138
|
+
const r = redactSecrets("联系 13812345678 或 alice@example.com");
|
|
139
|
+
expect(r.text).toContain("13812345678");
|
|
140
|
+
expect(r.text).toContain("alice@example.com");
|
|
141
|
+
expect(r.redacted).toBe(0);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// codex review 2026-07-10: broad numeric patterns must checksum before scrub
|
|
145
|
+
it("does NOT scrub 19-digit snowflake IDs (boundary guard)", () => {
|
|
146
|
+
const id = "1891234567890123456";
|
|
147
|
+
const r = redactSecrets(`tweet id ${id} archived`);
|
|
148
|
+
expect(r.text).toContain(id);
|
|
149
|
+
expect(r.redacted).toBe(0);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("does NOT scrub 18-digit numbers failing the ID checksum", () => {
|
|
153
|
+
const fake = "110105194912310020"; // checksum digit wrong on purpose
|
|
154
|
+
const r = redactSecrets(`订单号 ${fake} 已发货`);
|
|
155
|
+
expect(r.text).toContain(fake);
|
|
156
|
+
expect(r.redacted).toBe(0);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("does NOT scrub 16-digit numbers failing Luhn", () => {
|
|
160
|
+
const orderNo = "1234567890123456"; // Luhn-invalid
|
|
161
|
+
const r = redactSecrets(`订单 ${orderNo} 状态正常`);
|
|
162
|
+
expect(r.text).toContain(orderNo);
|
|
163
|
+
expect(r.redacted).toBe(0);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("leaves ordinary Chinese engineering notes untouched", () => {
|
|
167
|
+
const text = "修复 admission 门控:noise-filter 英文 greeting 加词边界 + rejection reason 透传";
|
|
168
|
+
const r = redactSecrets(text);
|
|
169
|
+
expect(r.text).toBe(text);
|
|
170
|
+
expect(r.redacted).toBe(0);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("leaves ordinary English text untouched", () => {
|
|
174
|
+
const text = "The dream pipeline gathers active memories and consolidates clusters.";
|
|
175
|
+
const r = redactSecrets(text);
|
|
176
|
+
expect(r.text).toBe(text);
|
|
177
|
+
expect(r.redacted).toBe(0);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// ============================================================================
|
|
182
|
+
// redactForLog — redact-then-truncate ordering
|
|
183
|
+
// ============================================================================
|
|
184
|
+
|
|
185
|
+
describe("redactForLog", () => {
|
|
186
|
+
it("redacts before truncating so a cut cannot leak a secret prefix", () => {
|
|
187
|
+
// Secret starts inside the 60-char window; naive slice-then-redact would leak its head
|
|
188
|
+
const text = "x".repeat(50) + " sk-ant-api03-SECRETSECRETSECRET tail";
|
|
189
|
+
const logged = redactForLog(text, 60);
|
|
190
|
+
expect(logged).not.toContain("sk-ant");
|
|
191
|
+
expect(logged.length).toBeLessThanOrEqual(60);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("keeps clean text as-is within the limit", () => {
|
|
195
|
+
expect(redactForLog("short clean text", 60)).toBe("short clean text");
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// ============================================================================
|
|
200
|
+
// scanForPII — backward compatibility
|
|
201
|
+
// ============================================================================
|
|
202
|
+
|
|
203
|
+
describe("scanForPII backward compatibility", () => {
|
|
204
|
+
it("still detects and masks without altering behavior", () => {
|
|
205
|
+
const result = scanForPII("password=SuperSecret99");
|
|
206
|
+
expect(result.hasPII).toBe(true);
|
|
207
|
+
expect(result.detections[0].type).toBe("password");
|
|
208
|
+
// masked match must not contain the full secret
|
|
209
|
+
expect(result.detections[0].match).not.toBe("password=SuperSecret99");
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("reports new vendor token types", () => {
|
|
213
|
+
const result = scanForPII("sk-ant-api03-AbCdEf12345678");
|
|
214
|
+
expect(result.hasPII).toBe(true);
|
|
215
|
+
expect(result.detections.some((d) => d.type === "anthropic_key")).toBe(true);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// ============================================================================
|
|
220
|
+
// persistMemory integration — redaction reaches every surface
|
|
221
|
+
// ============================================================================
|
|
222
|
+
|
|
223
|
+
function createDeps() {
|
|
224
|
+
const storedEntries: any[] = [];
|
|
225
|
+
const embeddedTexts: string[] = [];
|
|
226
|
+
const auditEntries: Array<Omit<AuditEntry, "timestamp">> = [];
|
|
227
|
+
let seq = 1;
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
storedEntries,
|
|
231
|
+
embeddedTexts,
|
|
232
|
+
auditEntries,
|
|
233
|
+
deps: {
|
|
234
|
+
embedder: {
|
|
235
|
+
async embedPassage(text: string) {
|
|
236
|
+
embeddedTexts.push(text);
|
|
237
|
+
return [text.length, 1, 0];
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
store: {
|
|
241
|
+
async store(entry: any) {
|
|
242
|
+
const stored = {
|
|
243
|
+
...entry,
|
|
244
|
+
id: `00000000-0000-0000-0000-${String(seq).padStart(12, "0")}`,
|
|
245
|
+
timestamp: 1_700_000_000_000 + seq,
|
|
246
|
+
};
|
|
247
|
+
seq += 1;
|
|
248
|
+
storedEntries.push(stored);
|
|
249
|
+
return stored;
|
|
250
|
+
},
|
|
251
|
+
async list() {
|
|
252
|
+
return [];
|
|
253
|
+
},
|
|
254
|
+
async update() {
|
|
255
|
+
return null;
|
|
256
|
+
},
|
|
257
|
+
async getById() {
|
|
258
|
+
return null;
|
|
259
|
+
},
|
|
260
|
+
async get() {
|
|
261
|
+
return null;
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
auditLogger: {
|
|
265
|
+
log(entry: Omit<AuditEntry, "timestamp">) {
|
|
266
|
+
auditEntries.push(entry);
|
|
267
|
+
},
|
|
268
|
+
getRecent: () => [],
|
|
269
|
+
exportAll: () => [],
|
|
270
|
+
count: () => 0,
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
describe("persistMemory redaction integration", () => {
|
|
277
|
+
it("embeds, stores, and audit-logs the scrubbed text, never the raw secret", async () => {
|
|
278
|
+
const h = createDeps();
|
|
279
|
+
const result = await persistMemory(h.deps as any, {
|
|
280
|
+
text: "部署踩坑:用了 password=SuperSecret99 连测试库,改环境变量后修复,教训是配置不进代码",
|
|
281
|
+
category: "cases",
|
|
282
|
+
importance: 0.9,
|
|
283
|
+
scope: TEST_SCOPE,
|
|
284
|
+
source: "manual",
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
expect(result.disposition).not.toBe("rejected");
|
|
288
|
+
// Embedding API never saw the secret
|
|
289
|
+
expect(h.embeddedTexts.length).toBeGreaterThan(0);
|
|
290
|
+
for (const t of h.embeddedTexts) {
|
|
291
|
+
expect(t).not.toContain("SuperSecret99");
|
|
292
|
+
}
|
|
293
|
+
// Stored text is scrubbed but keeps the key name for readability
|
|
294
|
+
expect(h.storedEntries[0].text).toContain("password=[REDACTED:password]");
|
|
295
|
+
expect(h.storedEntries[0].text).not.toContain("SuperSecret99");
|
|
296
|
+
// Audit log details are scrubbed too
|
|
297
|
+
for (const e of h.auditEntries) {
|
|
298
|
+
expect(e.details ?? "").not.toContain("SuperSecret99");
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("records piiWarning with redacted count in metadata", async () => {
|
|
303
|
+
const h = createDeps();
|
|
304
|
+
await persistMemory(h.deps as any, {
|
|
305
|
+
text: "记录一下:测试环境 token=abcdefghij1234567890XYZAB 已经轮换过了,流程要固化下来",
|
|
306
|
+
category: "cases",
|
|
307
|
+
importance: 0.9,
|
|
308
|
+
scope: TEST_SCOPE,
|
|
309
|
+
source: "manual",
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
const meta = JSON.parse(h.storedEntries[0].metadata ?? "{}");
|
|
313
|
+
expect(meta.piiWarning).toBeDefined();
|
|
314
|
+
expect(meta.piiWarning.redacted).toBeGreaterThan(0);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("writes a reject entry to the audit log when admission rejects", async () => {
|
|
318
|
+
const h = createDeps();
|
|
319
|
+
const result = await persistMemory(h.deps as any, {
|
|
320
|
+
text: "ok", // below minTextLength → admission reject
|
|
321
|
+
category: "cases",
|
|
322
|
+
importance: 0.9,
|
|
323
|
+
scope: TEST_SCOPE,
|
|
324
|
+
source: "manual",
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
expect(result.disposition).toBe("rejected");
|
|
328
|
+
const rejects = h.auditEntries.filter((e) => e.operation === "reject");
|
|
329
|
+
expect(rejects.length).toBe(1);
|
|
330
|
+
expect(rejects[0].scope).toBe(TEST_SCOPE);
|
|
331
|
+
expect(rejects[0].details ?? "").toContain("ok");
|
|
332
|
+
});
|
|
333
|
+
});
|