recallnest 2.2.1 → 2.5.4
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 +150 -0
- package/README.md +46 -35
- package/README_CN.md +41 -26
- 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/connectors/examples/email-adapter.ts +73 -0
- package/connectors/examples/logs-adapter.ts +103 -0
- package/connectors/examples/rss-adapter.ts +95 -0
- 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 +22 -1
- package/src/__tests__/access-tracker-novelty.test.ts +238 -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__/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__/canary-eval.test.ts +114 -0
- package/src/__tests__/capture-engine.test.ts +1238 -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 +33 -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 +782 -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 +374 -0
- package/src/__tests__/connector-v1.test.ts +181 -0
- package/src/__tests__/consolidation-engine.test.ts +333 -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 +216 -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 +84 -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 +321 -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__/graph-export.test.ts +489 -0
- package/src/__tests__/hotness-blend.test.ts +76 -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-consolidation.test.ts +226 -0
- package/src/__tests__/maintenance-locks.test.ts +174 -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 +250 -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__/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 +314 -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 +230 -0
- package/src/__tests__/preference-slots.test.ts +205 -0
- package/src/__tests__/prospective-memory.test.ts +283 -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-governor.test.ts +227 -0
- package/src/__tests__/recall-mode.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-trace.test.ts +213 -0
- package/src/__tests__/retriever-boundary-weight.test.ts +136 -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 +601 -0
- package/src/__tests__/skill-verifier.test.ts +122 -0
- package/src/__tests__/source-heartbeat.test.ts +250 -0
- package/src/__tests__/store-patch-metadata.test.ts +205 -0
- package/src/__tests__/store-scope-audit.test.ts +81 -0
- package/src/__tests__/store-upsert.test.ts +184 -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.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__/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 +38 -32
- package/src/activity-counter.ts +46 -18
- package/src/aliases.ts +133 -0
- package/src/api-server.ts +27 -7
- package/src/audit-log.ts +4 -2
- package/src/auto-consolidation.ts +8 -3
- package/src/auto-gc.ts +85 -21
- package/src/capture-engine.ts +54 -21
- package/src/cli.ts +199 -37
- package/src/confidence-tracker.ts +36 -18
- package/src/connector-types.ts +57 -0
- package/src/consolidation-engine.ts +109 -11
- package/src/context-composer.ts +8 -1
- package/src/conversation-importer.ts +25 -1
- package/src/data-checkup.ts +126 -7
- package/src/distill-lock.ts +194 -15
- package/src/doctor.ts +38 -2
- package/src/dream-pipeline.ts +118 -34
- package/src/env-config.ts +77 -0
- package/src/error-signature.ts +153 -0
- package/src/error-taxonomy.ts +115 -0
- package/src/eval.ts +251 -6
- package/src/idempotency.ts +8 -0
- package/src/ingest.ts +234 -10
- package/src/kg-extractor.ts +6 -2
- package/src/kg-store.ts +236 -44
- package/src/llm-consolidation.ts +2 -1
- package/src/mcp-server.ts +85 -1799
- package/src/mcp-tool-deps.ts +48 -0
- package/src/mcp-tools-advanced.ts +941 -0
- package/src/mcp-tools-core.ts +633 -0
- package/src/mcp-tools-governance.ts +475 -0
- package/src/memory-boundaries.ts +3 -1
- package/src/memory-evolution.ts +73 -1
- package/src/memory-lint.ts +183 -10
- package/src/memory-output.ts +114 -3
- package/src/memory-promotion.ts +246 -0
- package/src/memory-schema.ts +25 -2
- package/src/memory-store-port.ts +43 -0
- package/src/multi-vector.ts +2 -1
- package/src/narrative-schema.ts +2 -1
- package/src/noise-filter.ts +57 -6
- package/src/obsidian-connector.ts +282 -0
- package/src/pii-detector.ts +157 -5
- package/src/query-expander.ts +90 -9
- package/src/result-synthesizer.ts +2 -1
- package/src/retention-policy.ts +2 -1
- package/src/retriever.ts +259 -139
- package/src/runtime-config.ts +8 -1
- 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 +3 -2
- package/src/session-store.ts +5 -0
- package/src/skill-engine.ts +78 -0
- package/src/skill-promotion.ts +247 -47
- package/src/skill-schema.ts +16 -3
- package/src/skill-verifier.ts +152 -0
- package/src/source-heartbeat.ts +108 -0
- package/src/store.ts +267 -38
- package/src/tool-output-compressor.ts +3 -1
- package/src/ui-server.ts +2 -1
- package/src/usage-tracker.ts +242 -0
- package/src/version-manager.ts +3 -2
- 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 -30
- package/logs/ingest-2026-04-11.log +0 -110
- package/logs/ingest-2026-04-12.log +0 -90
- package/logs/launchagent-stderr.log +0 -0
- package/logs/launchagent-stdout.log +0 -0
|
@@ -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,314 @@
|
|
|
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("scrubs GitHub tokens", () => {
|
|
21
|
+
const r = redactSecrets("push with ghp_ABCDEFGHIJKLMNOPQRSTUV1234567890");
|
|
22
|
+
expect(r.text).toContain("[REDACTED:github_token]");
|
|
23
|
+
expect(r.text).not.toContain("ghp_");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("scrubs Slack tokens", () => {
|
|
27
|
+
const r = redactSecrets("xoxb-123456789-abcdefghij is the bot token");
|
|
28
|
+
expect(r.text).toContain("[REDACTED:slack_token]");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("scrubs AWS access key IDs", () => {
|
|
32
|
+
const r = redactSecrets("aws AKIAIOSFODNN7EXAMPLE used");
|
|
33
|
+
expect(r.text).toContain("[REDACTED:aws_access_key]");
|
|
34
|
+
expect(r.text).not.toContain("AKIA");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("scrubs Google API keys", () => {
|
|
38
|
+
// Real Google API keys are exactly "AIza" + 35 chars
|
|
39
|
+
const r = redactSecrets(`key ${"AIzaSyB" + "x".repeat(32)} in config`);
|
|
40
|
+
expect(r.text).toContain("[REDACTED:google_api_key]");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("scrubs JWTs", () => {
|
|
44
|
+
const jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U";
|
|
45
|
+
const r = redactSecrets(`token: bearer-less ${jwt} end`);
|
|
46
|
+
expect(r.text).toContain("[REDACTED:jwt]");
|
|
47
|
+
expect(r.text).not.toContain("eyJhbGci");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("scrubs Bearer headers", () => {
|
|
51
|
+
const r = redactSecrets("Authorization: Bearer abcdef1234567890XYZ_token");
|
|
52
|
+
expect(r.text).toContain("[REDACTED:bearer_token]");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("scrubs PEM private key blocks including the body", () => {
|
|
56
|
+
const pem = "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA\nsecretbody\n-----END RSA PRIVATE KEY-----";
|
|
57
|
+
const r = redactSecrets(`config:\n${pem}\ndone`);
|
|
58
|
+
expect(r.text).toContain("[REDACTED:pem_private_key]");
|
|
59
|
+
expect(r.text).not.toContain("MIIEowIBAAKCAQEA");
|
|
60
|
+
expect(r.text).not.toContain("secretbody");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("keeps the key name for password assignments, scrubs only the value", () => {
|
|
64
|
+
const r = redactSecrets("db config password=SuperSecret99 in env");
|
|
65
|
+
expect(r.text).toContain("password=[REDACTED:password]");
|
|
66
|
+
expect(r.text).not.toContain("SuperSecret99");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("keeps the key name for api_key/token/secret assignments", () => {
|
|
70
|
+
const r = redactSecrets("set api_key=abcdefghij1234567890XYZAB then restart");
|
|
71
|
+
expect(r.text).toContain("api_key=[REDACTED:api_key]");
|
|
72
|
+
expect(r.text).not.toContain("abcdefghij1234567890XYZAB");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("scrubs Chinese national ID numbers (checksum-valid)", () => {
|
|
76
|
+
const r = redactSecrets("身份证 11010519491231002X 登记");
|
|
77
|
+
expect(r.text).toContain("[REDACTED:id_number]");
|
|
78
|
+
expect(r.text).not.toContain("11010519491231002X");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("scrubs credit card numbers (Luhn-valid)", () => {
|
|
82
|
+
const r = redactSecrets("card 4111 1111 1111 1111 charged");
|
|
83
|
+
expect(r.text).toContain("[REDACTED:credit_card]");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// codex review 2026-07-10: JSON/YAML/config credential forms must be caught
|
|
87
|
+
it("scrubs JSON-style quoted credential keys", () => {
|
|
88
|
+
const r = redactSecrets('config: {"password": "SuperSecret99", "region": "us"}');
|
|
89
|
+
expect(r.text).not.toContain("SuperSecret99");
|
|
90
|
+
expect(r.text).toContain("[REDACTED:password]");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("scrubs JSON-style quoted api_key", () => {
|
|
94
|
+
const r = redactSecrets('{"api_key": "abcdefghij1234567890XYZAB"}');
|
|
95
|
+
expect(r.text).not.toContain("abcdefghij1234567890XYZAB");
|
|
96
|
+
expect(r.text).toContain("[REDACTED:api_key]");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("scrubs YAML/config assignments with spaces around the separator", () => {
|
|
100
|
+
const r = redactSecrets("password = SuperSecret99\nsecret : abcdefghij1234567890XYZAB");
|
|
101
|
+
expect(r.text).not.toContain("SuperSecret99");
|
|
102
|
+
expect(r.text).not.toContain("abcdefghij1234567890XYZAB");
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// ============================================================================
|
|
107
|
+
// redactSecrets — false-positive guards
|
|
108
|
+
// ============================================================================
|
|
109
|
+
|
|
110
|
+
describe("redactSecrets false-positive guards", () => {
|
|
111
|
+
it("does NOT scrub git commit hashes (40-char hex)", () => {
|
|
112
|
+
const sha = "699bea8f3d2c1a0b9e8d7c6b5a4f3e2d1c0b9a87";
|
|
113
|
+
const r = redactSecrets(`fixed in commit ${sha} on main`);
|
|
114
|
+
expect(r.text).toContain(sha);
|
|
115
|
+
expect(r.redacted).toBe(0);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("does NOT scrub phone numbers or emails (detection-only)", () => {
|
|
119
|
+
const r = redactSecrets("联系 13812345678 或 alice@example.com");
|
|
120
|
+
expect(r.text).toContain("13812345678");
|
|
121
|
+
expect(r.text).toContain("alice@example.com");
|
|
122
|
+
expect(r.redacted).toBe(0);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// codex review 2026-07-10: broad numeric patterns must checksum before scrub
|
|
126
|
+
it("does NOT scrub 19-digit snowflake IDs (boundary guard)", () => {
|
|
127
|
+
const id = "1891234567890123456";
|
|
128
|
+
const r = redactSecrets(`tweet id ${id} archived`);
|
|
129
|
+
expect(r.text).toContain(id);
|
|
130
|
+
expect(r.redacted).toBe(0);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("does NOT scrub 18-digit numbers failing the ID checksum", () => {
|
|
134
|
+
const fake = "110105194912310020"; // checksum digit wrong on purpose
|
|
135
|
+
const r = redactSecrets(`订单号 ${fake} 已发货`);
|
|
136
|
+
expect(r.text).toContain(fake);
|
|
137
|
+
expect(r.redacted).toBe(0);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("does NOT scrub 16-digit numbers failing Luhn", () => {
|
|
141
|
+
const orderNo = "1234567890123456"; // Luhn-invalid
|
|
142
|
+
const r = redactSecrets(`订单 ${orderNo} 状态正常`);
|
|
143
|
+
expect(r.text).toContain(orderNo);
|
|
144
|
+
expect(r.redacted).toBe(0);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("leaves ordinary Chinese engineering notes untouched", () => {
|
|
148
|
+
const text = "修复 admission 门控:noise-filter 英文 greeting 加词边界 + rejection reason 透传";
|
|
149
|
+
const r = redactSecrets(text);
|
|
150
|
+
expect(r.text).toBe(text);
|
|
151
|
+
expect(r.redacted).toBe(0);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("leaves ordinary English text untouched", () => {
|
|
155
|
+
const text = "The dream pipeline gathers active memories and consolidates clusters.";
|
|
156
|
+
const r = redactSecrets(text);
|
|
157
|
+
expect(r.text).toBe(text);
|
|
158
|
+
expect(r.redacted).toBe(0);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// ============================================================================
|
|
163
|
+
// redactForLog — redact-then-truncate ordering
|
|
164
|
+
// ============================================================================
|
|
165
|
+
|
|
166
|
+
describe("redactForLog", () => {
|
|
167
|
+
it("redacts before truncating so a cut cannot leak a secret prefix", () => {
|
|
168
|
+
// Secret starts inside the 60-char window; naive slice-then-redact would leak its head
|
|
169
|
+
const text = "x".repeat(50) + " sk-ant-api03-SECRETSECRETSECRET tail";
|
|
170
|
+
const logged = redactForLog(text, 60);
|
|
171
|
+
expect(logged).not.toContain("sk-ant");
|
|
172
|
+
expect(logged.length).toBeLessThanOrEqual(60);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("keeps clean text as-is within the limit", () => {
|
|
176
|
+
expect(redactForLog("short clean text", 60)).toBe("short clean text");
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// ============================================================================
|
|
181
|
+
// scanForPII — backward compatibility
|
|
182
|
+
// ============================================================================
|
|
183
|
+
|
|
184
|
+
describe("scanForPII backward compatibility", () => {
|
|
185
|
+
it("still detects and masks without altering behavior", () => {
|
|
186
|
+
const result = scanForPII("password=SuperSecret99");
|
|
187
|
+
expect(result.hasPII).toBe(true);
|
|
188
|
+
expect(result.detections[0].type).toBe("password");
|
|
189
|
+
// masked match must not contain the full secret
|
|
190
|
+
expect(result.detections[0].match).not.toBe("password=SuperSecret99");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("reports new vendor token types", () => {
|
|
194
|
+
const result = scanForPII("sk-ant-api03-AbCdEf12345678");
|
|
195
|
+
expect(result.hasPII).toBe(true);
|
|
196
|
+
expect(result.detections.some((d) => d.type === "anthropic_key")).toBe(true);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// ============================================================================
|
|
201
|
+
// persistMemory integration — redaction reaches every surface
|
|
202
|
+
// ============================================================================
|
|
203
|
+
|
|
204
|
+
function createDeps() {
|
|
205
|
+
const storedEntries: any[] = [];
|
|
206
|
+
const embeddedTexts: string[] = [];
|
|
207
|
+
const auditEntries: Array<Omit<AuditEntry, "timestamp">> = [];
|
|
208
|
+
let seq = 1;
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
storedEntries,
|
|
212
|
+
embeddedTexts,
|
|
213
|
+
auditEntries,
|
|
214
|
+
deps: {
|
|
215
|
+
embedder: {
|
|
216
|
+
async embedPassage(text: string) {
|
|
217
|
+
embeddedTexts.push(text);
|
|
218
|
+
return [text.length, 1, 0];
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
store: {
|
|
222
|
+
async store(entry: any) {
|
|
223
|
+
const stored = {
|
|
224
|
+
...entry,
|
|
225
|
+
id: `00000000-0000-0000-0000-${String(seq).padStart(12, "0")}`,
|
|
226
|
+
timestamp: 1_700_000_000_000 + seq,
|
|
227
|
+
};
|
|
228
|
+
seq += 1;
|
|
229
|
+
storedEntries.push(stored);
|
|
230
|
+
return stored;
|
|
231
|
+
},
|
|
232
|
+
async list() {
|
|
233
|
+
return [];
|
|
234
|
+
},
|
|
235
|
+
async update() {
|
|
236
|
+
return null;
|
|
237
|
+
},
|
|
238
|
+
async getById() {
|
|
239
|
+
return null;
|
|
240
|
+
},
|
|
241
|
+
async get() {
|
|
242
|
+
return null;
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
auditLogger: {
|
|
246
|
+
log(entry: Omit<AuditEntry, "timestamp">) {
|
|
247
|
+
auditEntries.push(entry);
|
|
248
|
+
},
|
|
249
|
+
getRecent: () => [],
|
|
250
|
+
exportAll: () => [],
|
|
251
|
+
count: () => 0,
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
describe("persistMemory redaction integration", () => {
|
|
258
|
+
it("embeds, stores, and audit-logs the scrubbed text, never the raw secret", async () => {
|
|
259
|
+
const h = createDeps();
|
|
260
|
+
const result = await persistMemory(h.deps as any, {
|
|
261
|
+
text: "部署踩坑:用了 password=SuperSecret99 连测试库,改环境变量后修复,教训是配置不进代码",
|
|
262
|
+
category: "cases",
|
|
263
|
+
importance: 0.9,
|
|
264
|
+
scope: TEST_SCOPE,
|
|
265
|
+
source: "manual",
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
expect(result.disposition).not.toBe("rejected");
|
|
269
|
+
// Embedding API never saw the secret
|
|
270
|
+
expect(h.embeddedTexts.length).toBeGreaterThan(0);
|
|
271
|
+
for (const t of h.embeddedTexts) {
|
|
272
|
+
expect(t).not.toContain("SuperSecret99");
|
|
273
|
+
}
|
|
274
|
+
// Stored text is scrubbed but keeps the key name for readability
|
|
275
|
+
expect(h.storedEntries[0].text).toContain("password=[REDACTED:password]");
|
|
276
|
+
expect(h.storedEntries[0].text).not.toContain("SuperSecret99");
|
|
277
|
+
// Audit log details are scrubbed too
|
|
278
|
+
for (const e of h.auditEntries) {
|
|
279
|
+
expect(e.details ?? "").not.toContain("SuperSecret99");
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("records piiWarning with redacted count in metadata", async () => {
|
|
284
|
+
const h = createDeps();
|
|
285
|
+
await persistMemory(h.deps as any, {
|
|
286
|
+
text: "记录一下:测试环境 token=abcdefghij1234567890XYZAB 已经轮换过了,流程要固化下来",
|
|
287
|
+
category: "cases",
|
|
288
|
+
importance: 0.9,
|
|
289
|
+
scope: TEST_SCOPE,
|
|
290
|
+
source: "manual",
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
const meta = JSON.parse(h.storedEntries[0].metadata ?? "{}");
|
|
294
|
+
expect(meta.piiWarning).toBeDefined();
|
|
295
|
+
expect(meta.piiWarning.redacted).toBeGreaterThan(0);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("writes a reject entry to the audit log when admission rejects", async () => {
|
|
299
|
+
const h = createDeps();
|
|
300
|
+
const result = await persistMemory(h.deps as any, {
|
|
301
|
+
text: "ok", // below minTextLength → admission reject
|
|
302
|
+
category: "cases",
|
|
303
|
+
importance: 0.9,
|
|
304
|
+
scope: TEST_SCOPE,
|
|
305
|
+
source: "manual",
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
expect(result.disposition).toBe("rejected");
|
|
309
|
+
const rejects = h.auditEntries.filter((e) => e.operation === "reject");
|
|
310
|
+
expect(rejects.length).toBe(1);
|
|
311
|
+
expect(rejects[0].scope).toBe(TEST_SCOPE);
|
|
312
|
+
expect(rejects[0].details ?? "").toContain("ok");
|
|
313
|
+
});
|
|
314
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { filterByRelevance, type FilterConfig } from "../post-retrieval-filter.js";
|
|
4
|
+
import type { LLMClient } from "../llm-client.js";
|
|
5
|
+
import type { RetrievalResult } from "../retriever.js";
|
|
6
|
+
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// Helpers
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
function makeResult(id: string, text: string, score: number): RetrievalResult {
|
|
12
|
+
return {
|
|
13
|
+
entry: {
|
|
14
|
+
id,
|
|
15
|
+
text,
|
|
16
|
+
vector: [],
|
|
17
|
+
category: "events",
|
|
18
|
+
scope: "test",
|
|
19
|
+
importance: 0.5,
|
|
20
|
+
timestamp: Date.now(),
|
|
21
|
+
},
|
|
22
|
+
score,
|
|
23
|
+
sources: {},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function makeMockLLM(response: boolean[] | null, shouldThrow = false): LLMClient {
|
|
28
|
+
return {
|
|
29
|
+
chatJson: async () => {
|
|
30
|
+
if (shouldThrow) throw new Error("LLM failure");
|
|
31
|
+
return response;
|
|
32
|
+
},
|
|
33
|
+
} as unknown as LLMClient;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Tests
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
describe("filterByRelevance", () => {
|
|
41
|
+
it("returns empty array for empty results", async () => {
|
|
42
|
+
const llm = makeMockLLM([]);
|
|
43
|
+
const out = await filterByRelevance([], "some query", llm);
|
|
44
|
+
expect(out).toEqual([]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("returns original results when query is empty", async () => {
|
|
48
|
+
const results = [makeResult("a", "hello", 0.8)];
|
|
49
|
+
const llm = makeMockLLM([true]);
|
|
50
|
+
const out = await filterByRelevance(results, "", llm);
|
|
51
|
+
expect(out).toEqual(results);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("returns all when LLM marks all as relevant", async () => {
|
|
55
|
+
const results = [
|
|
56
|
+
makeResult("a", "TypeScript project setup", 0.9),
|
|
57
|
+
makeResult("b", "Docker deployment notes", 0.85),
|
|
58
|
+
];
|
|
59
|
+
const llm = makeMockLLM([true, true]);
|
|
60
|
+
const out = await filterByRelevance(results, "project setup", llm);
|
|
61
|
+
expect(out).toHaveLength(2);
|
|
62
|
+
expect(out[0].entry.id).toBe("a");
|
|
63
|
+
expect(out[1].entry.id).toBe("b");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("filters out items LLM marks as irrelevant", async () => {
|
|
67
|
+
const results = [
|
|
68
|
+
makeResult("a", "TypeScript project setup", 0.9),
|
|
69
|
+
makeResult("b", "Favorite pizza recipe", 0.85),
|
|
70
|
+
makeResult("c", "Docker deployment notes", 0.8),
|
|
71
|
+
];
|
|
72
|
+
const llm = makeMockLLM([true, false, true]);
|
|
73
|
+
const out = await filterByRelevance(results, "project deployment", llm);
|
|
74
|
+
expect(out).toHaveLength(2);
|
|
75
|
+
expect(out[0].entry.id).toBe("a");
|
|
76
|
+
expect(out[1].entry.id).toBe("c");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("drops items below minScoreForFilter without LLM call", async () => {
|
|
80
|
+
const results = [
|
|
81
|
+
makeResult("a", "High score item", 0.9),
|
|
82
|
+
makeResult("b", "Low score item", 0.3),
|
|
83
|
+
makeResult("c", "Very low score", 0.1),
|
|
84
|
+
];
|
|
85
|
+
// LLM only sees 1 item (the one above threshold)
|
|
86
|
+
const llm = makeMockLLM([true]);
|
|
87
|
+
const out = await filterByRelevance(results, "test query", llm);
|
|
88
|
+
expect(out).toHaveLength(1);
|
|
89
|
+
expect(out[0].entry.id).toBe("a");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("returns all candidates when all are below minScoreForFilter", async () => {
|
|
93
|
+
const results = [
|
|
94
|
+
makeResult("a", "Low score A", 0.3),
|
|
95
|
+
makeResult("b", "Low score B", 0.2),
|
|
96
|
+
];
|
|
97
|
+
const llm = makeMockLLM([]);
|
|
98
|
+
const out = await filterByRelevance(results, "test query", llm);
|
|
99
|
+
expect(out).toHaveLength(0);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("truncates to maxItems", async () => {
|
|
103
|
+
const results = Array.from({ length: 15 }, (_, i) =>
|
|
104
|
+
makeResult(`item-${i}`, `Memory item ${i}`, 0.9 - i * 0.01),
|
|
105
|
+
);
|
|
106
|
+
// LLM receives only 5 items, marks all as relevant
|
|
107
|
+
const cfg: Partial<FilterConfig> = { maxItems: 5 };
|
|
108
|
+
const llm = makeMockLLM([true, true, true, true, true]);
|
|
109
|
+
const out = await filterByRelevance(results, "test query", llm, cfg);
|
|
110
|
+
expect(out).toHaveLength(5);
|
|
111
|
+
expect(out[0].entry.id).toBe("item-0");
|
|
112
|
+
expect(out[4].entry.id).toBe("item-4");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("returns candidates on LLM failure (graceful fallback)", async () => {
|
|
116
|
+
const results = [
|
|
117
|
+
makeResult("a", "Important memory", 0.9),
|
|
118
|
+
makeResult("b", "Another memory", 0.85),
|
|
119
|
+
];
|
|
120
|
+
const llm = makeMockLLM(null, true);
|
|
121
|
+
const out = await filterByRelevance(results, "test query", llm);
|
|
122
|
+
// Should return all candidates, not lose data
|
|
123
|
+
expect(out).toHaveLength(2);
|
|
124
|
+
expect(out[0].entry.id).toBe("a");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("returns candidates when LLM returns non-array", async () => {
|
|
128
|
+
const results = [
|
|
129
|
+
makeResult("a", "Important memory", 0.9),
|
|
130
|
+
];
|
|
131
|
+
const llm = makeMockLLM(null);
|
|
132
|
+
const out = await filterByRelevance(results, "test query", llm);
|
|
133
|
+
expect(out).toHaveLength(1);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("uses l0_abstract from metadata when available", async () => {
|
|
137
|
+
const result = makeResult("a", "Very long raw text that should not be used", 0.9);
|
|
138
|
+
result.entry.metadata = JSON.stringify({ l0_abstract: "Short summary" });
|
|
139
|
+
const calls: string[] = [];
|
|
140
|
+
const llm = {
|
|
141
|
+
chatJson: async (_sys: string, user: string) => {
|
|
142
|
+
calls.push(user);
|
|
143
|
+
return [true];
|
|
144
|
+
},
|
|
145
|
+
} as unknown as LLMClient;
|
|
146
|
+
|
|
147
|
+
await filterByRelevance([result], "test", llm);
|
|
148
|
+
expect(calls[0]).toContain("Short summary");
|
|
149
|
+
expect(calls[0]).not.toContain("Very long raw text");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("handles custom minScoreForFilter", async () => {
|
|
153
|
+
const results = [
|
|
154
|
+
makeResult("a", "High", 0.9),
|
|
155
|
+
makeResult("b", "Medium", 0.6),
|
|
156
|
+
makeResult("c", "Low", 0.4),
|
|
157
|
+
];
|
|
158
|
+
const cfg: Partial<FilterConfig> = { minScoreForFilter: 0.65 };
|
|
159
|
+
const llm = makeMockLLM([true]);
|
|
160
|
+
const out = await filterByRelevance(results, "test", llm, cfg);
|
|
161
|
+
expect(out).toHaveLength(1);
|
|
162
|
+
expect(out[0].entry.id).toBe("a");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("handles shorter LLM verdict array than candidates", async () => {
|
|
166
|
+
const results = [
|
|
167
|
+
makeResult("a", "Memory A", 0.9),
|
|
168
|
+
makeResult("b", "Memory B", 0.85),
|
|
169
|
+
makeResult("c", "Memory C", 0.8),
|
|
170
|
+
];
|
|
171
|
+
// LLM returns fewer verdicts than items
|
|
172
|
+
const llm = makeMockLLM([true, false]);
|
|
173
|
+
const out = await filterByRelevance(results, "test", llm);
|
|
174
|
+
// Only first item passes (index 0 = true, index 1 = false, index 2 = out of bounds)
|
|
175
|
+
expect(out).toHaveLength(1);
|
|
176
|
+
expect(out[0].entry.id).toBe("a");
|
|
177
|
+
});
|
|
178
|
+
});
|