dsh-cross-memory 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/README.md +253 -2
- package/cordis.patch.yml +27 -0
- package/docs/DETAIL.md +13 -0
- package/docs/DETAIL.zh.md +17 -0
- package/icon.svg +1 -0
- package/lib/backfill.js +273 -0
- package/lib/citations.js +305 -0
- package/lib/client.js +1660 -0
- package/lib/config.js +736 -0
- package/lib/consolidate.js +1119 -0
- package/lib/extract.js +570 -0
- package/lib/gate.js +137 -0
- package/lib/git-origin.js +224 -0
- package/lib/index.js +4482 -0
- package/lib/log.js +260 -0
- package/lib/loops.js +186 -0
- package/lib/notices.js +195 -0
- package/lib/notify.js +33 -0
- package/lib/persona.js +163 -0
- package/lib/query.js +93 -0
- package/lib/raw-recall.js +286 -0
- package/lib/reflection.js +291 -0
- package/lib/remote.js +490 -0
- package/lib/render.js +678 -0
- package/lib/retention.js +64 -0
- package/lib/schedule.js +245 -0
- package/lib/search.js +1068 -0
- package/lib/skills.js +220 -0
- package/lib/state.js +701 -0
- package/lib/storage.js +1424 -0
- package/lib/sync-runtime.js +268 -0
- package/lib/sync.js +620 -0
- package/lib/test/activity.test.js +74 -0
- package/lib/test/archive.test.js +182 -0
- package/lib/test/backfill.test.js +144 -0
- package/lib/test/citations.test.js +91 -0
- package/lib/test/client-wiring.test.js +134 -0
- package/lib/test/consolidate-job.test.js +95 -0
- package/lib/test/consolidate.test.js +430 -0
- package/lib/test/consolidation-selection.test.js +152 -0
- package/lib/test/dedupe-keys.test.js +230 -0
- package/lib/test/delta.test.js +93 -0
- package/lib/test/derived.test.js +110 -0
- package/lib/test/disputes-panel.test.js +341 -0
- package/lib/test/eval.test.js +171 -0
- package/lib/test/expand.test.js +164 -0
- package/lib/test/extract.test.js +1020 -0
- package/lib/test/git-origin.test.js +189 -0
- package/lib/test/injection.test.js +440 -0
- package/lib/test/kind-filter.test.js +49 -0
- package/lib/test/legacy-state.test.js +69 -0
- package/lib/test/log.test.js +156 -0
- package/lib/test/loops.test.js +86 -0
- package/lib/test/memory.test.js +855 -0
- package/lib/test/notices.test.js +142 -0
- package/lib/test/persona.test.js +86 -0
- package/lib/test/prefilter.test.js +73 -0
- package/lib/test/preset.test.js +77 -0
- package/lib/test/raw-recall.test.js +186 -0
- package/lib/test/recall.test.js +433 -0
- package/lib/test/reflection.test.js +64 -0
- package/lib/test/remote.test.js +238 -0
- package/lib/test/retention.test.js +117 -0
- package/lib/test/retrieval.test.js +142 -0
- package/lib/test/rotation.test.js +228 -0
- package/lib/test/schedule.test.js +112 -0
- package/lib/test/settings-card.test.js +58 -0
- package/lib/test/skills.test.js +129 -0
- package/lib/test/state.test.js +212 -0
- package/lib/test/subagent-contract.test.js +139 -0
- package/lib/test/sweep.test.js +159 -0
- package/lib/test/sync.test.js +314 -0
- package/lib/test/time.test.js +171 -0
- package/lib/time.js +303 -0
- package/lib/tool.js +330 -0
- package/lib/types/backfill.d.ts +96 -0
- package/lib/types/citations.d.ts +105 -0
- package/lib/types/config.d.ts +811 -0
- package/lib/types/consolidate.d.ts +742 -0
- package/lib/types/extract.d.ts +352 -0
- package/lib/types/gate.d.ts +83 -0
- package/lib/types/git-origin.d.ts +23 -0
- package/lib/types/index.d.ts +1316 -0
- package/lib/types/log.d.ts +144 -0
- package/lib/types/loops.d.ts +82 -0
- package/lib/types/notices.d.ts +117 -0
- package/lib/types/notify.d.ts +12 -0
- package/lib/types/persona.d.ts +76 -0
- package/lib/types/query.d.ts +45 -0
- package/lib/types/raw-recall.d.ts +84 -0
- package/lib/types/reflection.d.ts +122 -0
- package/lib/types/remote.d.ts +246 -0
- package/lib/types/render.d.ts +286 -0
- package/lib/types/retention.d.ts +66 -0
- package/lib/types/schedule.d.ts +116 -0
- package/lib/types/search.d.ts +583 -0
- package/lib/types/skills.d.ts +71 -0
- package/lib/types/state.d.ts +294 -0
- package/lib/types/storage.d.ts +458 -0
- package/lib/types/sync-runtime.d.ts +103 -0
- package/lib/types/sync.d.ts +222 -0
- package/lib/types/test/activity.test.d.ts +1 -0
- package/lib/types/test/archive.test.d.ts +1 -0
- package/lib/types/test/backfill.test.d.ts +1 -0
- package/lib/types/test/citations.test.d.ts +1 -0
- package/lib/types/test/client-wiring.test.d.ts +1 -0
- package/lib/types/test/consolidate-job.test.d.ts +1 -0
- package/lib/types/test/consolidate.test.d.ts +1 -0
- package/lib/types/test/consolidation-selection.test.d.ts +1 -0
- package/lib/types/test/dedupe-keys.test.d.ts +1 -0
- package/lib/types/test/delta.test.d.ts +1 -0
- package/lib/types/test/derived.test.d.ts +1 -0
- package/lib/types/test/disputes-panel.test.d.ts +1 -0
- package/lib/types/test/eval.test.d.ts +1 -0
- package/lib/types/test/expand.test.d.ts +1 -0
- package/lib/types/test/extract.test.d.ts +1 -0
- package/lib/types/test/git-origin.test.d.ts +1 -0
- package/lib/types/test/injection.test.d.ts +1 -0
- package/lib/types/test/kind-filter.test.d.ts +1 -0
- package/lib/types/test/legacy-state.test.d.ts +1 -0
- package/lib/types/test/log.test.d.ts +1 -0
- package/lib/types/test/loops.test.d.ts +1 -0
- package/lib/types/test/memory.test.d.ts +1 -0
- package/lib/types/test/notices.test.d.ts +1 -0
- package/lib/types/test/persona.test.d.ts +1 -0
- package/lib/types/test/prefilter.test.d.ts +1 -0
- package/lib/types/test/preset.test.d.ts +1 -0
- package/lib/types/test/raw-recall.test.d.ts +1 -0
- package/lib/types/test/recall.test.d.ts +1 -0
- package/lib/types/test/reflection.test.d.ts +1 -0
- package/lib/types/test/remote.test.d.ts +1 -0
- package/lib/types/test/retention.test.d.ts +1 -0
- package/lib/types/test/retrieval.test.d.ts +1 -0
- package/lib/types/test/rotation.test.d.ts +1 -0
- package/lib/types/test/schedule.test.d.ts +1 -0
- package/lib/types/test/settings-card.test.d.ts +1 -0
- package/lib/types/test/skills.test.d.ts +1 -0
- package/lib/types/test/state.test.d.ts +1 -0
- package/lib/types/test/subagent-contract.test.d.ts +1 -0
- package/lib/types/test/sweep.test.d.ts +1 -0
- package/lib/types/test/sync.test.d.ts +1 -0
- package/lib/types/test/time.test.d.ts +1 -0
- package/lib/types/time.d.ts +67 -0
- package/lib/types/tool.d.ts +9 -0
- package/lib/types/types.d.ts +374 -0
- package/lib/types/workspace.d.ts +20 -0
- package/lib/types.js +47 -0
- package/lib/workspace.js +65 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +126 -4
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the `--kind` flag and kind filtering.
|
|
3
|
+
*
|
|
4
|
+
* `kind` is only useful if it can be used to narrow a search and to label a
|
|
5
|
+
* memory written by hand; these tests pin both, including the deliberate choice
|
|
6
|
+
* to ignore an unknown kind rather than silently returning nothing.
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-cross-memory/test/kind-filter.test
|
|
9
|
+
*/
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import test from 'node:test';
|
|
12
|
+
import { browseMemories, searchMemories } from '../search.js';
|
|
13
|
+
/** Build one entry. */
|
|
14
|
+
function entry(id, kind, title, body, tags = []) {
|
|
15
|
+
return { id, scope: 'global', kind, title, body, tags, keys: [], createdAt: 1, updatedAt: 1, uses: 0, lastUsedAt: 0, lastSurfacedAt: 0, source: 'tool' };
|
|
16
|
+
}
|
|
17
|
+
const GROUPS = [{
|
|
18
|
+
scope: 'global',
|
|
19
|
+
label: 'global',
|
|
20
|
+
entries: [
|
|
21
|
+
entry('pref', 'preference', 'Prefer pnpm', 'Use pnpm, not npm.'),
|
|
22
|
+
entry('fail', 'failure', 'Never force-push', 'A force-push destroyed a branch once.'),
|
|
23
|
+
entry('proc', 'procedure', 'Release steps', 'Tag, build, publish.'),
|
|
24
|
+
entry('fact', 'fact', 'Repo layout', 'Sources live in src/.'),
|
|
25
|
+
],
|
|
26
|
+
}];
|
|
27
|
+
test('search filters by kind', () => {
|
|
28
|
+
assert.deepEqual(searchMemories(GROUPS, 'pnpm', { kinds: ['preference'] }).map((hit) => hit.entry.id), ['pref']);
|
|
29
|
+
assert.deepEqual(searchMemories(GROUPS, 'pnpm', { kinds: ['failure'] }), []);
|
|
30
|
+
assert.equal(searchMemories(GROUPS, 'push', { kinds: ['failure'] }).length, 1);
|
|
31
|
+
assert.equal(searchMemories(GROUPS, 'release', { kinds: ['procedure', 'knowledge'] }).length, 1);
|
|
32
|
+
});
|
|
33
|
+
test('browse filters by kind without a query', () => {
|
|
34
|
+
assert.deepEqual(browseMemories(GROUPS, { kinds: ['fact'] }).map((hit) => hit.entry.id), ['fact']);
|
|
35
|
+
assert.equal(browseMemories(GROUPS, { kinds: ['knowledge'] }).length, 0);
|
|
36
|
+
assert.equal(browseMemories(GROUPS).length, 4, 'no filter returns everything');
|
|
37
|
+
});
|
|
38
|
+
test('kind filtering composes with tag filtering', () => {
|
|
39
|
+
const tagged = [{
|
|
40
|
+
scope: 'global',
|
|
41
|
+
label: 'global',
|
|
42
|
+
entries: [
|
|
43
|
+
entry('a', 'preference', 'Tagged pref', 'body', ['tooling']),
|
|
44
|
+
entry('b', 'fact', 'Tagged fact', 'body', ['tooling']),
|
|
45
|
+
],
|
|
46
|
+
}];
|
|
47
|
+
assert.deepEqual(browseMemories(tagged, { kinds: ['preference'], tags: ['tooling'] }).map((hit) => hit.entry.id), ['a']);
|
|
48
|
+
assert.deepEqual(browseMemories(tagged, { kinds: ['preference'], tags: ['absent'] }), []);
|
|
49
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for importing and removing the pre-SQLite state file.
|
|
3
|
+
*
|
|
4
|
+
* Upgrading must not silently re-mine conversations that were already
|
|
5
|
+
* processed: a session with no watermark is treated as fresh, which spends
|
|
6
|
+
* quota re-reading its transcript. The import also has to be incapable of
|
|
7
|
+
* clobbering newer state.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-cross-memory/test/legacy-state.test
|
|
10
|
+
*/
|
|
11
|
+
import assert from 'node:assert/strict';
|
|
12
|
+
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
13
|
+
import { tmpdir } from 'node:os';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import test from 'node:test';
|
|
16
|
+
import { LEGACY_STATE_FILE, StateStore, importLegacyState, statePath } from '../state.js';
|
|
17
|
+
/** A temp memory store with a fresh state database. */
|
|
18
|
+
async function fixture(t) {
|
|
19
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-legacy-'));
|
|
20
|
+
const store = new StateStore(statePath(dir));
|
|
21
|
+
t.after(() => {
|
|
22
|
+
store.close();
|
|
23
|
+
return rm(dir, { recursive: true, force: true });
|
|
24
|
+
});
|
|
25
|
+
return { dir, store };
|
|
26
|
+
}
|
|
27
|
+
test('watermarks are imported and the legacy file is removed', async (t) => {
|
|
28
|
+
const { dir, store } = await fixture(t);
|
|
29
|
+
await writeFile(join(dir, LEGACY_STATE_FILE), JSON.stringify({
|
|
30
|
+
version: 1,
|
|
31
|
+
sessions: {
|
|
32
|
+
'session-a': { lastSeq: 12, at: 1_000, root: 'C:\\work', contributed: true },
|
|
33
|
+
'session-b': { lastSeq: 3, at: 2_000 },
|
|
34
|
+
'session-zero': { lastSeq: 0, at: 5 },
|
|
35
|
+
},
|
|
36
|
+
}), 'utf8');
|
|
37
|
+
const imported = await importLegacyState(store, dir);
|
|
38
|
+
assert.equal(imported, 2, 'only watermarks with a real lastSeq are imported');
|
|
39
|
+
assert.equal(store.getSession('session-a')?.lastSeq, 12);
|
|
40
|
+
assert.equal(store.getSession('session-a')?.root, 'C:\\work');
|
|
41
|
+
assert.equal(store.getSession('session-a')?.contributed, true);
|
|
42
|
+
assert.equal(store.getSession('session-b')?.lastSeq, 3);
|
|
43
|
+
assert.equal(store.getSession('session-zero'), undefined);
|
|
44
|
+
// The activity clock falls back to the watermark time, the closest honest proxy.
|
|
45
|
+
assert.equal(store.getSession('session-b')?.activityAt, 2_000);
|
|
46
|
+
await assert.rejects(() => readFile(join(dir, LEGACY_STATE_FILE), 'utf8'), /ENOENT/u);
|
|
47
|
+
});
|
|
48
|
+
test('the import is skipped when the database already holds sessions', async (t) => {
|
|
49
|
+
const { dir, store } = await fixture(t);
|
|
50
|
+
store.putSession('session-a', { lastSeq: 99, at: 9_000, contributed: true });
|
|
51
|
+
await writeFile(join(dir, LEGACY_STATE_FILE), JSON.stringify({
|
|
52
|
+
version: 1,
|
|
53
|
+
sessions: { 'session-a': { lastSeq: 1, at: 1 } },
|
|
54
|
+
}), 'utf8');
|
|
55
|
+
const imported = await importLegacyState(store, dir);
|
|
56
|
+
assert.equal(imported, 0, 'newer state is never overwritten');
|
|
57
|
+
assert.equal(store.getSession('session-a')?.lastSeq, 99);
|
|
58
|
+
await assert.rejects(() => readFile(join(dir, LEGACY_STATE_FILE), 'utf8'), /ENOENT/u);
|
|
59
|
+
});
|
|
60
|
+
test('a corrupt legacy file is removed without throwing', async (t) => {
|
|
61
|
+
const { dir, store } = await fixture(t);
|
|
62
|
+
await writeFile(join(dir, LEGACY_STATE_FILE), 'not json at all', 'utf8');
|
|
63
|
+
assert.equal(await importLegacyState(store, dir), 0);
|
|
64
|
+
await assert.rejects(() => readFile(join(dir, LEGACY_STATE_FILE), 'utf8'), /ENOENT/u);
|
|
65
|
+
});
|
|
66
|
+
test('no legacy file means nothing to do', async (t) => {
|
|
67
|
+
const { dir, store } = await fixture(t);
|
|
68
|
+
assert.equal(await importLegacyState(store, dir), undefined);
|
|
69
|
+
});
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the plugin's log sink.
|
|
3
|
+
*
|
|
4
|
+
* The behaviour worth pinning is the one that was silently broken: the host
|
|
5
|
+
* filters `warn` (level 2) and `debug` (3) out before any sink sees them when no
|
|
6
|
+
* exporter declares a threshold, so a sink registered the usual way records only
|
|
7
|
+
* `error` and `info`. These tests hold the override in place, and then the level
|
|
8
|
+
* semantics this plugin layers on top of it.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-cross-memory/test/log.test
|
|
11
|
+
*/
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
import { Context } from '@deepseek-ai/cordis';
|
|
14
|
+
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
15
|
+
import { tmpdir } from 'node:os';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
import test from 'node:test';
|
|
18
|
+
import { LOG_NAME, MemoryLog, allows, createFileSink, createLogExporter, formatLogMessage, logPath, pluginLogger, toLogLevel } from '../log.js';
|
|
19
|
+
/** Every severity, most serious first. */
|
|
20
|
+
const TYPES = ['error', 'warn', 'info', 'debug'];
|
|
21
|
+
/** The severities recorded at one verbosity. */
|
|
22
|
+
function recorded(level) {
|
|
23
|
+
return TYPES.filter((type) => allows(level, type));
|
|
24
|
+
}
|
|
25
|
+
test('toLogLevel accepts the documented names and falls back to info', () => {
|
|
26
|
+
for (const name of ['off', 'error', 'warn', 'info', 'debug'])
|
|
27
|
+
assert.equal(toLogLevel(name), name);
|
|
28
|
+
assert.equal(toLogLevel('verbose'), 'info');
|
|
29
|
+
assert.equal(toLogLevel(undefined), 'info');
|
|
30
|
+
assert.equal(toLogLevel(7), 'info');
|
|
31
|
+
assert.equal(toLogLevel('verbose', 'debug'), 'debug');
|
|
32
|
+
});
|
|
33
|
+
test('a level means "this and everything more serious"', () => {
|
|
34
|
+
assert.deepEqual(recorded('off'), []);
|
|
35
|
+
assert.deepEqual(recorded('error'), ['error']);
|
|
36
|
+
assert.deepEqual(recorded('warn'), ['error', 'warn']);
|
|
37
|
+
assert.deepEqual(recorded('info'), ['error', 'warn', 'info']);
|
|
38
|
+
assert.deepEqual(recorded('debug'), ['error', 'warn', 'info', 'debug']);
|
|
39
|
+
});
|
|
40
|
+
test('formatLogMessage renders the host placeholders on a single line', () => {
|
|
41
|
+
const exporter = { colors: false, export: () => undefined };
|
|
42
|
+
const message = {
|
|
43
|
+
sn: 1,
|
|
44
|
+
ts: Date.UTC(2026, 0, 2, 3, 4, 5),
|
|
45
|
+
name: 'dsh-cross-memory',
|
|
46
|
+
type: 'warn',
|
|
47
|
+
level: 2,
|
|
48
|
+
args: ['stored %d of %s\nsecond line', 3, 'x'],
|
|
49
|
+
};
|
|
50
|
+
assert.equal(formatLogMessage(exporter, message), '2026-01-02T03:04:05.000Z [warn] dsh-cross-memory stored 3 of x second line');
|
|
51
|
+
});
|
|
52
|
+
test('the file sink appends, rotates one generation, and refuses an unusable path', async (t) => {
|
|
53
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-log-'));
|
|
54
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
55
|
+
const path = join(dir, 'nested', 'dsh-cross-memory.log');
|
|
56
|
+
const sink = createFileSink(path, 40);
|
|
57
|
+
assert.ok(sink !== undefined);
|
|
58
|
+
sink.write('a'.repeat(50));
|
|
59
|
+
sink.write('second');
|
|
60
|
+
assert.equal(await readFile(path, 'utf8'), 'second\n');
|
|
61
|
+
assert.equal(await readFile(`${path}.1`, 'utf8'), `${'a'.repeat(50)}\n`);
|
|
62
|
+
assert.equal(createFileSink(' '), undefined, 'an empty path disables the sink');
|
|
63
|
+
// A path whose parent is a file cannot be created; the caller is told so
|
|
64
|
+
// rather than handed a sink that throws on the first write.
|
|
65
|
+
const blocker = join(dir, 'blocker');
|
|
66
|
+
await writeFile(blocker, 'not a directory', 'utf8');
|
|
67
|
+
const failures = [];
|
|
68
|
+
assert.equal(createFileSink(join(blocker, 'x.log'), undefined, (reason) => failures.push(reason)), undefined);
|
|
69
|
+
assert.equal(failures.length, 1, 'an unusable path is reported, not swallowed');
|
|
70
|
+
});
|
|
71
|
+
test('a write that fails after opening is reported, because the file is then stale', async (t) => {
|
|
72
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-log-'));
|
|
73
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
74
|
+
// The sink opens (the parent exists) but every append fails: the path is a
|
|
75
|
+
// directory. This is the case a user cannot see from the file itself — it
|
|
76
|
+
// simply stops growing, and nothing in it says why.
|
|
77
|
+
const asDirectory = join(dir, 'taken');
|
|
78
|
+
await mkdir(asDirectory, { recursive: true });
|
|
79
|
+
const failures = [];
|
|
80
|
+
const sink = createFileSink(asDirectory, undefined, (reason) => failures.push(reason));
|
|
81
|
+
assert.ok(sink !== undefined, 'the parent exists, so the sink is created');
|
|
82
|
+
sink.write('one');
|
|
83
|
+
sink.write('two');
|
|
84
|
+
assert.equal(failures.length, 2, 'each failed write is reported once');
|
|
85
|
+
assert.ok(failures.every((reason) => reason.length > 0), 'with a reason a user can act on');
|
|
86
|
+
});
|
|
87
|
+
test('logPath keeps the log inside the harness home', () => {
|
|
88
|
+
assert.equal(logPath(join('C:', 'home', '.dsh')), join('C:', 'home', '.dsh', 'logs', 'dsh-cross-memory.log'));
|
|
89
|
+
});
|
|
90
|
+
test('a real cordis sink records warn and debug, which the host would otherwise drop', async (t) => {
|
|
91
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-sink-'));
|
|
92
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
93
|
+
const path = join(dir, 'dsh-cross-memory.log');
|
|
94
|
+
let level = 'debug';
|
|
95
|
+
const ctx = new Context();
|
|
96
|
+
const sink = createFileSink(path);
|
|
97
|
+
assert.ok(sink !== undefined);
|
|
98
|
+
const exporter = createLogExporter(sink, () => level);
|
|
99
|
+
// The declaration that makes the difference: without a threshold for this
|
|
100
|
+
// logger the host falls back to 1 and everything below `info` is discarded
|
|
101
|
+
// before reaching us.
|
|
102
|
+
assert.equal(exporter.levels?.[LOG_NAME], 3);
|
|
103
|
+
// And the other direction, which is what keeps this file OURS: an exporter is
|
|
104
|
+
// process-wide, so naming only our logger leaves every other logger on the
|
|
105
|
+
// host's fallback threshold — which is how `web-server` ECONNRESET warnings
|
|
106
|
+
// ended up in this file on a real profile. `default: 0` closes that door
|
|
107
|
+
// without touching what the host does with anyone else's messages.
|
|
108
|
+
assert.equal(exporter.levels?.default, 0, 'foreign loggers are held at error, not at the host fallback');
|
|
109
|
+
ctx.logger.exporter(exporter);
|
|
110
|
+
const log = new MemoryLog(pluginLogger(ctx.logger), () => true);
|
|
111
|
+
log.warn('careful %d', 1);
|
|
112
|
+
log.info('stored %d memories', 2);
|
|
113
|
+
log.decision('archived %s', 'ancient');
|
|
114
|
+
log.debug('quiet detail');
|
|
115
|
+
// Somebody else's line must not land in this plugin's file — at ANY level.
|
|
116
|
+
// Our own `error` line is the interesting one: `default: 0` means a foreign
|
|
117
|
+
// logger is held to `error`, and the name check then drops even that, so the
|
|
118
|
+
// file is ours alone rather than "ours plus other plugins' failures".
|
|
119
|
+
ctx.logger('web-server').warn('ECONNRESET from another plugin');
|
|
120
|
+
ctx.logger('web-server').error('another plugin failed');
|
|
121
|
+
// A line of ours that went out through the service instead of the named logger
|
|
122
|
+
// carries the fiber's name, so `default: 0` holds it at `error` as well. It is
|
|
123
|
+
// therefore only recorded at error — which is why every call site goes through
|
|
124
|
+
// `pluginLogger`'s named logger, and why the prefix check exists only to rescue
|
|
125
|
+
// what the host already delivered rather than to widen the threshold.
|
|
126
|
+
ctx.logger.info('dsh-cross-memory: service-level info %d', 3);
|
|
127
|
+
ctx.logger.error('dsh-cross-memory: service-level error %d', 4);
|
|
128
|
+
const written = await readFile(path, 'utf8');
|
|
129
|
+
assert.match(written, /\[warn\] .*careful 1/u, 'a warning must reach the file');
|
|
130
|
+
assert.match(written, /\[info\] .*stored 2 memories/u);
|
|
131
|
+
assert.match(written, /\[info\] .*archived ancient/u, 'tracing raises a decision to info');
|
|
132
|
+
assert.match(written, /\[debug\] .*quiet detail/u);
|
|
133
|
+
assert.match(written, /service-level error 4/u, 'a prefixed line at error is ours even from the service');
|
|
134
|
+
assert.doesNotMatch(written, /another plugin/u, 'only this plugin\'s lines belong in its file');
|
|
135
|
+
assert.doesNotMatch(written, /service-level info 3/u, 'the fallback entry does not admit foreign-level noise');
|
|
136
|
+
// The verbosity is read per line, so a settings change applies immediately.
|
|
137
|
+
level = 'error';
|
|
138
|
+
log.warn('after the change');
|
|
139
|
+
assert.doesNotMatch(await readFile(path, 'utf8'), /after the change/u);
|
|
140
|
+
});
|
|
141
|
+
test('a decision is debug when tracing is off', () => {
|
|
142
|
+
const seen = [];
|
|
143
|
+
const logger = {
|
|
144
|
+
error: (...args) => seen.push(['error', String(args[0])]),
|
|
145
|
+
warn: (...args) => seen.push(['warn', String(args[0])]),
|
|
146
|
+
info: (...args) => seen.push(['info', String(args[0])]),
|
|
147
|
+
debug: (...args) => seen.push(['debug', String(args[0])]),
|
|
148
|
+
};
|
|
149
|
+
const quiet = new MemoryLog(logger, () => false);
|
|
150
|
+
quiet.decision('archived %s', 'x');
|
|
151
|
+
quiet.warn('a real warning');
|
|
152
|
+
assert.deepEqual(seen, [['debug', 'archived %s'], ['warn', 'a real warning']]);
|
|
153
|
+
const loud = new MemoryLog(logger, () => true);
|
|
154
|
+
loud.decision('reviewed %d', 2);
|
|
155
|
+
assert.deepEqual(seen.at(-1), ['info', 'reviewed %d']);
|
|
156
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the open-loop ledger.
|
|
3
|
+
*
|
|
4
|
+
* The rules pinned here are the ones that keep proactivity honest: one promise is
|
|
5
|
+
* ONE loop (dedupe), an undated note is never a reminder, a reminder repeats at
|
|
6
|
+
* most three times, and nothing stays open forever.
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-cross-memory/test/loops
|
|
9
|
+
*/
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import test from 'node:test';
|
|
12
|
+
import { LOOP_CAP, LOOP_TTL_MS, closeLoop, describeLoops, dueLoops, mergeLoops, pruneLoops, withLoop } from '../loops.js';
|
|
13
|
+
import { parseExtraction } from '../extract.js';
|
|
14
|
+
const NOW = Date.parse('2026-10-24T02:00:00Z');
|
|
15
|
+
const empty = { version: 1, loops: [] };
|
|
16
|
+
test('the same promise said twice is one loop', () => {
|
|
17
|
+
const first = mergeLoops(empty, [{ text: '把报价单发出去' }], NOW, 's1');
|
|
18
|
+
const second = mergeLoops(first, [{ text: '把报价单 发出去' }], NOW + 1000, 's2');
|
|
19
|
+
assert.equal(second.loops.length, 1, 'whitespace is not a new promise');
|
|
20
|
+
assert.equal(second.loops[0]?.openedAt, NOW, 'the first sighting keeps its age');
|
|
21
|
+
});
|
|
22
|
+
test('a later deadline replaces the old one instead of adding a loop', () => {
|
|
23
|
+
const first = mergeLoops(empty, [{ text: '周五前把报价单发出去', dueAt: NOW + 86_400_000 }], NOW);
|
|
24
|
+
const moved = mergeLoops(first, [{ text: '周五前把报价单发出去', dueAt: NOW + 4 * 86_400_000 }], NOW + 1000);
|
|
25
|
+
assert.equal(moved.loops.length, 1);
|
|
26
|
+
assert.equal(moved.loops[0]?.dueAt, NOW + 4 * 86_400_000, 'the user moved the promise');
|
|
27
|
+
});
|
|
28
|
+
test('only a dated loop may raise a reminder, at most three times a day apart', () => {
|
|
29
|
+
let store = mergeLoops(empty, [{ text: '下周再看部署脚本' }, { text: '明天把发票寄出去', dueAt: NOW - 1000 }], NOW);
|
|
30
|
+
const due = dueLoops(store, NOW);
|
|
31
|
+
assert.deepEqual(due.map((loop) => loop.text), ['明天把发票寄出去'], 'an undated note is not a promise');
|
|
32
|
+
const id = due[0].id;
|
|
33
|
+
store = withLoop(store, id, { remindedAt: NOW, remindCount: 1 });
|
|
34
|
+
assert.equal(dueLoops(store, NOW + 1000).length, 0, 'not twice in the same hour');
|
|
35
|
+
assert.equal(dueLoops(store, NOW + 86_400_001).length, 1, 'a day later is allowed');
|
|
36
|
+
store = withLoop(store, id, { remindedAt: NOW + 86_400_001, remindCount: 3 });
|
|
37
|
+
assert.equal(dueLoops(store, NOW + 10 * 86_400_000).length, 0, 'three reminders and it stops');
|
|
38
|
+
});
|
|
39
|
+
test('an untouched loop expires, and closing accepts a unique prefix', () => {
|
|
40
|
+
const store = mergeLoops(empty, [{ text: '把报销单交给财务' }], NOW);
|
|
41
|
+
const later = pruneLoops(store, NOW + LOOP_TTL_MS + 1);
|
|
42
|
+
assert.equal(later.loops[0]?.state, 'expired');
|
|
43
|
+
const id = store.loops[0].id;
|
|
44
|
+
const closed = closeLoop(store, id.slice(0, 6));
|
|
45
|
+
assert.equal(closed.closed?.id, id);
|
|
46
|
+
assert.equal(closed.store.loops[0]?.state, 'done');
|
|
47
|
+
assert.equal(closeLoop(store, 'zzz').reason !== undefined, true);
|
|
48
|
+
});
|
|
49
|
+
test('the ledger is capped so it cannot become a second backlog', () => {
|
|
50
|
+
const many = Array.from({ length: LOOP_CAP + 5 }, (_, index) => ({ text: '第' + index + '件要做的事' }));
|
|
51
|
+
const store = mergeLoops(empty, many, NOW);
|
|
52
|
+
const open = store.loops.filter((loop) => loop.state === 'open');
|
|
53
|
+
assert.equal(open.length, LOOP_CAP);
|
|
54
|
+
assert.equal(store.loops.length, LOOP_CAP + 5, 'the rest are dropped, not deleted');
|
|
55
|
+
});
|
|
56
|
+
test('the command view puts the soonest deadline first', () => {
|
|
57
|
+
const store = mergeLoops(empty, [
|
|
58
|
+
{ text: '没日期的笔记' },
|
|
59
|
+
{ text: '后天交报告', dueAt: NOW + 2 * 86_400_000 },
|
|
60
|
+
{ text: '今天交报告', dueAt: NOW + 1000 },
|
|
61
|
+
], NOW);
|
|
62
|
+
const view = describeLoops(store, NOW);
|
|
63
|
+
assert.match(view, /今天交报告[\s\S]*后天交报告/u);
|
|
64
|
+
});
|
|
65
|
+
test('extraction keeps a real deadline and drops an invented one', () => {
|
|
66
|
+
const reply = JSON.stringify({
|
|
67
|
+
memories: [],
|
|
68
|
+
summary: 's',
|
|
69
|
+
loops: [
|
|
70
|
+
{ text: '周五前把合同发出去', dueAt: '2026-10-30T00:00:00Z' },
|
|
71
|
+
{ text: '顺手清理一下桌面', dueAt: '不是日期' },
|
|
72
|
+
{ text: '' },
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
const parsed = parseExtraction(reply, 10, 'session-1');
|
|
76
|
+
assert.equal(parsed.loops.length, 2);
|
|
77
|
+
assert.equal(parsed.loops[0]?.dueAt, Date.parse('2026-10-30T00:00:00Z'));
|
|
78
|
+
assert.equal(parsed.loops[1]?.dueAt, undefined, 'an unparseable deadline is dropped, never defaulted');
|
|
79
|
+
// A model that reads "things the user would do" and answers with bare strings
|
|
80
|
+
// must not lose the promises: that exact reply shape appeared in the e2e run.
|
|
81
|
+
const absurd = parseExtraction(JSON.stringify({ memories: [], summary: 's', loops: [{ text: '把下周的评审材料准备好', dueAt: '1999-01-01' }] }), 10, 'session-1');
|
|
82
|
+
assert.equal(absurd.loops[0]?.text, '把下周的评审材料准备好');
|
|
83
|
+
assert.equal(absurd.loops[0]?.dueAt, undefined, 'a deadline years outside the clock is a guess, not a date');
|
|
84
|
+
const bare = parseExtraction(JSON.stringify({ memories: [], summary: 's', loops: ['把登录页的截图补齐', '发截图前先过一遍文案', ''] }), 10, 'session-1');
|
|
85
|
+
assert.deepEqual(bare.loops.map((loop) => loop.text), ['把登录页的截图补齐', '发截图前先过一遍文案']);
|
|
86
|
+
});
|