dsh-memory-bond 0.0.0-stage → 1.0.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 +10 -0
- package/LICENSE +21 -0
- package/README.md +181 -2
- package/README.zh-CN.md +180 -0
- package/SECURITY.md +29 -0
- package/client/memory-icon.svg +21 -0
- package/cordis.patch.yml +3 -0
- package/dist/audit.js +286 -0
- package/dist/automatic.js +16 -0
- package/dist/cache.js +40 -0
- package/dist/client.js +2035 -0
- package/dist/command-scope.js +123 -0
- package/dist/commands.js +156 -0
- package/dist/consolidation-store.js +357 -0
- package/dist/consolidation.js +182 -0
- package/dist/domain.js +245 -0
- package/dist/encoder.js +138 -0
- package/dist/export-files.js +161 -0
- package/dist/file-import-store.js +120 -0
- package/dist/file-import.js +265 -0
- package/dist/foreground-attempts.js +91 -0
- package/dist/guidance.js +242 -0
- package/dist/host.js +19 -0
- package/dist/index-policy.js +191 -0
- package/dist/index.js +349 -0
- package/dist/knowledge-hooks.js +398 -0
- package/dist/knowledge.js +359 -0
- package/dist/maintenance-episodes.js +73 -0
- package/dist/maintenance-import.js +92 -0
- package/dist/maintenance-store.js +738 -0
- package/dist/maintenance.js +376 -0
- package/dist/model-review.js +16 -0
- package/dist/personal.js +60 -0
- package/dist/policy.js +382 -0
- package/dist/prompt-copy.js +93 -0
- package/dist/proposal-receipts.js +70 -0
- package/dist/proposal-review.js +656 -0
- package/dist/propose-contract.js +38 -0
- package/dist/question-answers.js +176 -0
- package/dist/retrieval.js +126 -0
- package/dist/scope-narrowing.js +49 -0
- package/dist/search-index.js +164 -0
- package/dist/search-words.js +5 -0
- package/dist/semantic.js +157 -0
- package/dist/service.js +1473 -0
- package/dist/sharing.js +289 -0
- package/dist/sources.js +149 -0
- package/dist/store.js +1578 -0
- package/dist/tool-experience.js +87 -0
- package/dist/turn-review-store.js +159 -0
- package/dist/turn-review.js +459 -0
- package/dist/ui.js +346 -0
- package/dist/vector-index.js +232 -0
- package/docs/architecture.zh-CN.md +52 -0
- package/docs/benchmark.zh-CN.md +60 -0
- package/docs/benchmarks/repository-tasks.json +5355 -0
- package/docs/images/bond-memory-principle-en.svg +644 -0
- package/docs/images/bond-memory-principle-zh.svg +644 -0
- package/docs/images/bond-memory-principle.drawio +644 -0
- package/docs/images/memory-settings-en.png +0 -0
- package/docs/images/memory-settings-zh.png +0 -0
- package/docs/images/memory-turn-recalled-en.png +0 -0
- package/docs/images/memory-turn-recalled-zh.png +0 -0
- package/docs/images/memory-turn-saved-en.png +0 -0
- package/docs/images/memory-turn-saved-zh.png +0 -0
- package/docs/media/memory-bond-name-en.gif +0 -0
- package/docs/media/memory-bond-name-en.mp4 +0 -0
- package/docs/media/memory-bond-name-zh.gif +0 -0
- package/docs/media/memory-bond-name-zh.mp4 +0 -0
- package/locale/detail.en.md +13 -0
- package/locale/detail.zh.md +13 -0
- package/locale/en.json +345 -0
- package/locale/zh.json +345 -0
- package/native/embeddings/NOTICE.md +12 -0
- package/package.json +117 -4
- package/scripts/benchmark-bond.mjs +72 -0
- package/scripts/benchmark-conditions.mjs +144 -0
- package/scripts/benchmark-example.mjs +37 -0
- package/scripts/benchmark-scale.mjs +119 -0
- package/scripts/benchmark-scenarios.mjs +57 -0
- package/scripts/benchmark-tasks.mjs +244 -0
- package/src/audit.ts +286 -0
- package/src/automatic.ts +21 -0
- package/src/cache.ts +38 -0
- package/src/command-scope.ts +125 -0
- package/src/commands.ts +159 -0
- package/src/consolidation-store.ts +350 -0
- package/src/consolidation.ts +176 -0
- package/src/domain.ts +254 -0
- package/src/encoder.ts +142 -0
- package/src/export-files.ts +178 -0
- package/src/file-import-store.ts +139 -0
- package/src/file-import.ts +256 -0
- package/src/foreground-attempts.ts +97 -0
- package/src/guidance.ts +265 -0
- package/src/host.ts +119 -0
- package/src/index-policy.ts +231 -0
- package/src/index.ts +333 -0
- package/src/knowledge-hooks.ts +456 -0
- package/src/knowledge.ts +432 -0
- package/src/maintenance-episodes.ts +76 -0
- package/src/maintenance-import.ts +107 -0
- package/src/maintenance-store.ts +868 -0
- package/src/maintenance.ts +380 -0
- package/src/model-review.ts +27 -0
- package/src/personal.ts +68 -0
- package/src/policy.ts +508 -0
- package/src/prompt-copy.ts +99 -0
- package/src/proposal-receipts.ts +59 -0
- package/src/proposal-review.ts +614 -0
- package/src/propose-contract.ts +39 -0
- package/src/question-answers.ts +181 -0
- package/src/retrieval.ts +133 -0
- package/src/scope-narrowing.ts +58 -0
- package/src/search-index.ts +177 -0
- package/src/search-words.ts +6 -0
- package/src/semantic.ts +178 -0
- package/src/service.ts +1488 -0
- package/src/sharing.ts +349 -0
- package/src/sources.ts +150 -0
- package/src/store.ts +1829 -0
- package/src/tool-experience.ts +93 -0
- package/src/turn-review-store.ts +158 -0
- package/src/turn-review.ts +418 -0
- package/src/ui.ts +385 -0
- package/src/vector-index.ts +228 -0
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import { prompts } from "./prompt-copy.js";
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { bashScope } from "./command-scope.js";
|
|
4
|
+
import { originalBashCall } from "./tool-experience.js";
|
|
5
|
+
import { projectRelative } from "./domain.js";
|
|
6
|
+
import { isKnowledge, knowledgeValid, recordSuccess, runbookBasis } from "./knowledge.js";
|
|
7
|
+
import { messageText, registerConditionalTool, } from "./host.js";
|
|
8
|
+
const active = new WeakMap();
|
|
9
|
+
const ttl = 5 * 60_000;
|
|
10
|
+
function table(memory, agent) {
|
|
11
|
+
let agents = active.get(memory);
|
|
12
|
+
if (!agents)
|
|
13
|
+
active.set(memory, (agents = new WeakMap()));
|
|
14
|
+
let table = agents.get(agent);
|
|
15
|
+
if (!table)
|
|
16
|
+
agents.set(agent, (table = new Map()));
|
|
17
|
+
for (const [key, t] of table)
|
|
18
|
+
if (t.until <= Date.now())
|
|
19
|
+
table.delete(key);
|
|
20
|
+
return table;
|
|
21
|
+
}
|
|
22
|
+
function issue(memory, agent, d, position, rows) {
|
|
23
|
+
const generation = memory.assistantGeneration();
|
|
24
|
+
if (!rows.length || generation === undefined)
|
|
25
|
+
return;
|
|
26
|
+
const entries = table(memory, agent);
|
|
27
|
+
if ([...entries.values()].some((t) => t.generation === generation && t.position === position && t.scope === d.id && t.root === d.root && t.cwd === d.cwd))
|
|
28
|
+
return;
|
|
29
|
+
const ticket = 'k-' + randomUUID();
|
|
30
|
+
entries.set(ticket, {
|
|
31
|
+
generation,
|
|
32
|
+
scope: d.id,
|
|
33
|
+
root: d.root,
|
|
34
|
+
cwd: d.cwd,
|
|
35
|
+
position,
|
|
36
|
+
until: Date.now() + ttl,
|
|
37
|
+
refs: rows.slice(0, 8).map(({ id, rev }) => ({ id, rev })),
|
|
38
|
+
});
|
|
39
|
+
if (entries.size > 64)
|
|
40
|
+
entries.delete(entries.keys().next().value);
|
|
41
|
+
memory.store.audit(d.id, agent.id, 'ticket_offer', { outcome: Math.min(rows.length, 8) });
|
|
42
|
+
return {
|
|
43
|
+
id: randomUUID(),
|
|
44
|
+
role: 'user',
|
|
45
|
+
source: { kind: 'project-memory-ticket' },
|
|
46
|
+
content: [
|
|
47
|
+
{
|
|
48
|
+
type: 'text',
|
|
49
|
+
text: prompts[memory.promptLanguage(agent)].ticketOffer + ticket,
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function readTicket(memory, agent, key, call, lookup) {
|
|
55
|
+
const generation = memory.assistantGeneration();
|
|
56
|
+
if (generation === undefined)
|
|
57
|
+
return { items: [] };
|
|
58
|
+
const d = memory.domain(agent);
|
|
59
|
+
const grant = typeof key === 'string' && /^k-[a-f\d-]{36}$/iu.test(key) ? table(memory, agent).get(key) : undefined;
|
|
60
|
+
if (!grant || grant.generation !== generation || !d || d.id !== grant.scope || d.root !== grant.root || d.cwd !== grant.cwd)
|
|
61
|
+
return { items: [] };
|
|
62
|
+
// No await between the final authorization and projection. Another host action may change cwd
|
|
63
|
+
// or withdraw a head while the async tool caller is suspended.
|
|
64
|
+
const rows = grant.refs.flatMap((ref) => {
|
|
65
|
+
const head = memory.store.read(d.id, ref.id, ref.rev);
|
|
66
|
+
if (!head ||
|
|
67
|
+
!isKnowledge(head) ||
|
|
68
|
+
head.kind === 'episode' ||
|
|
69
|
+
head.kind === 'guard' ||
|
|
70
|
+
!knowledgeValid(d, head).ok)
|
|
71
|
+
return [];
|
|
72
|
+
return [
|
|
73
|
+
{
|
|
74
|
+
id: head.id,
|
|
75
|
+
rev: head.rev,
|
|
76
|
+
kind: head.kind,
|
|
77
|
+
action: head.action,
|
|
78
|
+
when: head.when,
|
|
79
|
+
paths: head.paths,
|
|
80
|
+
knowledge: head.knowledge,
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
});
|
|
84
|
+
if (call && lookup)
|
|
85
|
+
lookup(agent, d, call, rows);
|
|
86
|
+
memory.store.audit(d.id, agent.id, 'ticket_read', { call, outcome: rows.length });
|
|
87
|
+
return { items: rows };
|
|
88
|
+
}
|
|
89
|
+
function invocation(memory, exec) {
|
|
90
|
+
if (exec.name !== 'bash' || !exec.agent || !exec.callId)
|
|
91
|
+
return;
|
|
92
|
+
const generation = memory.assistantGeneration();
|
|
93
|
+
if (generation === undefined)
|
|
94
|
+
return;
|
|
95
|
+
const d = memory.domain(exec.agent);
|
|
96
|
+
if (!d || d.id === memory.personal.id)
|
|
97
|
+
return;
|
|
98
|
+
const args = exec.arguments;
|
|
99
|
+
if (typeof args?.command !== 'string' || (args.workdir !== undefined && typeof args.workdir !== 'string'))
|
|
100
|
+
return;
|
|
101
|
+
const scope = bashScope(d, args.command, args.workdir);
|
|
102
|
+
// Key the match on bashScope's own canonical command, not the raw call text: a
|
|
103
|
+
// whitespace-only restatement of the exact same invocation (double space, a
|
|
104
|
+
// trailing space, ...) must still reach guard/runbook/episode/trigger matching
|
|
105
|
+
// instead of silently opting the whole hook out of the call.
|
|
106
|
+
if (!scope)
|
|
107
|
+
return;
|
|
108
|
+
return {
|
|
109
|
+
generation,
|
|
110
|
+
d,
|
|
111
|
+
command: scope.command,
|
|
112
|
+
cwd: scope.cwd,
|
|
113
|
+
workdir: projectRelative(d.root, scope.cwd) || '.',
|
|
114
|
+
revisions: [],
|
|
115
|
+
episodes: [],
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/** The final named result must carry the real execution-local Bash value, including the expected cwd. */
|
|
119
|
+
function exitStatus(result, invocation) {
|
|
120
|
+
const value = result.value;
|
|
121
|
+
if (result.isError !== false ||
|
|
122
|
+
!value ||
|
|
123
|
+
value.cwd !== invocation.cwd ||
|
|
124
|
+
!Number.isSafeInteger(value.exitCode) ||
|
|
125
|
+
value.signal !== null ||
|
|
126
|
+
value.aborted !== false ||
|
|
127
|
+
value.timedOut !== false ||
|
|
128
|
+
value.stopped ||
|
|
129
|
+
value.sandbox?.denied ||
|
|
130
|
+
value.sandbox?.runnerFailed)
|
|
131
|
+
return;
|
|
132
|
+
return Number(value.exitCode);
|
|
133
|
+
}
|
|
134
|
+
function guardMatches(row, item, anchors) {
|
|
135
|
+
if (!isKnowledge(row) ||
|
|
136
|
+
row.kind !== 'guard' ||
|
|
137
|
+
!['user_command', 'owner_card'].includes(String(row.channel)))
|
|
138
|
+
return false;
|
|
139
|
+
const match = row.knowledge?.match;
|
|
140
|
+
return match?.command === item.command && match.workdir === item.workdir && knowledgeValid(item.d, row, anchors).ok;
|
|
141
|
+
}
|
|
142
|
+
export function registerKnowledge(ctx, memory, lookup, receipts) {
|
|
143
|
+
// Guard is called only after native ask/cancel/deny resolution. Returning undefined can never allow a denied call.
|
|
144
|
+
const attempts = new WeakMap();
|
|
145
|
+
ctx.on('session/event', (session, event) => {
|
|
146
|
+
if (event.type === 'turn/end') {
|
|
147
|
+
const agent = ctx.agents.get(session.id);
|
|
148
|
+
if (agent?.session === session)
|
|
149
|
+
memory.bashEvidence.endTurn(agent);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (event.type !== 'tool/call' ||
|
|
153
|
+
event.data.name !== 'bash' ||
|
|
154
|
+
!event.data.arguments ||
|
|
155
|
+
!event.data.callId)
|
|
156
|
+
return;
|
|
157
|
+
const agent = ctx.agents.get(session.id);
|
|
158
|
+
if (!agent)
|
|
159
|
+
return;
|
|
160
|
+
let args;
|
|
161
|
+
try {
|
|
162
|
+
args = JSON.parse(event.data.arguments);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const item = invocation(memory, { agent, name: 'bash', arguments: args, callId: event.data.callId });
|
|
168
|
+
if (!item)
|
|
169
|
+
return;
|
|
170
|
+
// A downstream ask/cancel/deny may prevent the monotonic guard from ever running.
|
|
171
|
+
const anchors = new Map();
|
|
172
|
+
for (const row of memory.store.commandCandidates(item.d.id, item.command, item.workdir, 'attempt')) {
|
|
173
|
+
if (guardMatches(row, item, anchors))
|
|
174
|
+
memory.store.audit(item.d.id, agent.id, 'guard_attempt', {
|
|
175
|
+
id: row.id,
|
|
176
|
+
rev: row.rev,
|
|
177
|
+
call: event.data.callId,
|
|
178
|
+
seq: event.seq,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
ctx.tools.guard((exec) => {
|
|
183
|
+
const item = invocation(memory, exec);
|
|
184
|
+
if (!item || !exec.agent)
|
|
185
|
+
return;
|
|
186
|
+
let calls = attempts.get(exec.agent);
|
|
187
|
+
if (!calls)
|
|
188
|
+
attempts.set(exec.agent, (calls = new Map()));
|
|
189
|
+
calls.set(exec.callId, item);
|
|
190
|
+
if (calls.size > 128)
|
|
191
|
+
calls.delete(calls.keys().next().value);
|
|
192
|
+
if (exec.agent.session.header.origin !== 'subagent') {
|
|
193
|
+
try {
|
|
194
|
+
memory.rootDomain(exec.agent, true);
|
|
195
|
+
const call = originalBashCall(exec.agent, exec.callId);
|
|
196
|
+
if (call)
|
|
197
|
+
item.evidence = { ...call, storeId: memory.storeId(exec.agent.session) };
|
|
198
|
+
}
|
|
199
|
+
catch { /* A read-only session cannot offer a memory proposal. */ }
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
const runbookMatches = [];
|
|
203
|
+
const anchors = new Map();
|
|
204
|
+
for (const row of memory.store.commandCandidates(item.d.id, item.command, item.workdir, 'guard')) {
|
|
205
|
+
if (!isKnowledge(row))
|
|
206
|
+
continue;
|
|
207
|
+
if (row.kind === 'runbook' &&
|
|
208
|
+
row.knowledge?.steps?.verify === item.command &&
|
|
209
|
+
row.knowledge.steps.workdir === item.workdir &&
|
|
210
|
+
knowledgeValid(item.d, row, anchors).ok) {
|
|
211
|
+
runbookMatches.push({ ref: { id: row.id, rev: row.rev }, basis: runbookBasis(row) });
|
|
212
|
+
}
|
|
213
|
+
if (row.kind === 'episode' &&
|
|
214
|
+
row.knowledge?.failure?.command === item.command &&
|
|
215
|
+
row.knowledge.failure.workdir === item.workdir &&
|
|
216
|
+
knowledgeValid(item.d, row, anchors).ok)
|
|
217
|
+
item.episodes.push({ id: row.id, rev: row.rev });
|
|
218
|
+
if (!guardMatches(row, item, anchors))
|
|
219
|
+
continue;
|
|
220
|
+
const record = { id: row.id, rev: row.rev, call: exec.callId };
|
|
221
|
+
const reason = prompts[memory.promptLanguage(exec.agent)].guard + ' [' + row.id + '@' + row.rev + '] ' + row.action;
|
|
222
|
+
try {
|
|
223
|
+
memory.store.audit(item.d.id, exec.agent.id, 'guard_block', record);
|
|
224
|
+
receipts?.blocked(exec.agent, item.d, exec.callId, { id: row.id, rev: row.rev }, reason);
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
// Receipt persistence cannot turn a denial into permission to dispatch.
|
|
228
|
+
ctx.logger.warn('项目记忆无法记录本次守卫回执:' + String(error));
|
|
229
|
+
}
|
|
230
|
+
return reason;
|
|
231
|
+
}
|
|
232
|
+
// A (verify, workdir) signature shared by more than one runbook cannot be
|
|
233
|
+
// attributed to either from one real run, so this run credits neither.
|
|
234
|
+
if (runbookMatches.length === 1)
|
|
235
|
+
item.revisions.push(runbookMatches[0]);
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
// The guard contract has no "inconclusive" answer, only allow or deny: a
|
|
239
|
+
// thrown error must not read as a denial, but a store failure that would
|
|
240
|
+
// otherwise have blocked this call must not pass through silently either.
|
|
241
|
+
ctx.logger.warn('项目记忆读取失败,本次 Bash 调用未经过项目规则检查:' + String(error));
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
function sameInvocation(exec) {
|
|
245
|
+
const item = exec.agent && attempts.get(exec.agent)?.get(exec.callId);
|
|
246
|
+
if (!item || item.generation !== memory.assistantGeneration())
|
|
247
|
+
return;
|
|
248
|
+
const now = exec.agent && memory.domain(exec.agent);
|
|
249
|
+
return now?.id === item.d.id && now.root === item.d.root && now.cwd === item.d.cwd
|
|
250
|
+
? item
|
|
251
|
+
: undefined;
|
|
252
|
+
}
|
|
253
|
+
ctx.on('tools/post-execute', async (exec, result, next) => {
|
|
254
|
+
const decision = await next();
|
|
255
|
+
if (decision.kind !== 'accept' ||
|
|
256
|
+
!exec.agent ||
|
|
257
|
+
Object.hasOwn(decision, 'value') ||
|
|
258
|
+
Object.hasOwn(decision, 'content'))
|
|
259
|
+
return decision;
|
|
260
|
+
const item = sameInvocation(exec);
|
|
261
|
+
if (!item)
|
|
262
|
+
return decision;
|
|
263
|
+
const code = exitStatus(result, item);
|
|
264
|
+
item.rawExit = code;
|
|
265
|
+
if (code === undefined || code === 0)
|
|
266
|
+
return decision;
|
|
267
|
+
try {
|
|
268
|
+
const anchors = new Map();
|
|
269
|
+
const rows = memory.store.commandCandidates(item.d.id, item.command, item.workdir, 'failure').filter((row) => {
|
|
270
|
+
if (!isKnowledge(row) || ['episode', 'guard'].includes(row.kind))
|
|
271
|
+
return false;
|
|
272
|
+
const trigger = row.knowledge?.trigger;
|
|
273
|
+
return (trigger?.command === item.command &&
|
|
274
|
+
trigger.workdir === item.workdir &&
|
|
275
|
+
knowledgeValid(item.d, row, anchors).ok);
|
|
276
|
+
});
|
|
277
|
+
const message = issue(memory, exec.agent, item.d, 'post:' + exec.callId, rows);
|
|
278
|
+
return message
|
|
279
|
+
? { ...decision, additionalContexts: [...(decision.additionalContexts ?? []), message] }
|
|
280
|
+
: decision;
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
// This listener only ever adds an informational hint: a store failure
|
|
284
|
+
// must not replace the tool's already-computed, possibly-successful result.
|
|
285
|
+
ctx.logger.warn('项目记忆读取失败,本次工具结果未附加触发提示:' + String(error));
|
|
286
|
+
return decision;
|
|
287
|
+
}
|
|
288
|
+
}, { prepend: true });
|
|
289
|
+
ctx.on('tools/result', (exec, result) => {
|
|
290
|
+
if (!exec.agent)
|
|
291
|
+
return;
|
|
292
|
+
const item = sameInvocation(exec);
|
|
293
|
+
attempts.get(exec.agent)?.delete(exec.callId);
|
|
294
|
+
const code = item && exitStatus(result, item);
|
|
295
|
+
if (!item || code === undefined || item.rawExit !== code)
|
|
296
|
+
return;
|
|
297
|
+
try {
|
|
298
|
+
receipts?.executed(exec, item.command, item.cwd, code);
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
ctx.logger.warn('项目记忆无法记录本次执行回执:' + String(error));
|
|
302
|
+
}
|
|
303
|
+
if (item.evidence) {
|
|
304
|
+
try {
|
|
305
|
+
const domain = memory.rootDomain(exec.agent, true);
|
|
306
|
+
memory.bashEvidence.observe(exec.agent, exec.callId, {
|
|
307
|
+
...item.evidence, domain, generation: item.generation, command: item.command, cwd: item.cwd, workdir: item.workdir, exitCode: code,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
catch { /* A read-only session cannot offer a memory proposal. */ }
|
|
311
|
+
}
|
|
312
|
+
if (code !== 0) {
|
|
313
|
+
const delegated = exec.agent.session.header.origin === 'subagent';
|
|
314
|
+
const root = delegated ? ctx.get('subagents')?.rootOf(exec.agent) : exec.agent;
|
|
315
|
+
if (!root)
|
|
316
|
+
return;
|
|
317
|
+
try {
|
|
318
|
+
if (memory.rootDomain(root, true).id !== item.d.id)
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
catch {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
const occurrence = JSON.stringify([memory.storeId(root.session), root.id]);
|
|
325
|
+
const signature = JSON.stringify(['bash', item.command, item.workdir, code]);
|
|
326
|
+
for (const ref of item.episodes) {
|
|
327
|
+
const row = memory.store.read(item.d.id, ref.id, ref.rev);
|
|
328
|
+
if (row && knowledgeValid(item.d, row).ok)
|
|
329
|
+
memory.store.recordEpisodeFailure(item.d.id, ref.id, ref.rev, signature, occurrence);
|
|
330
|
+
}
|
|
331
|
+
memory.store.recurEpisodes(item.d.id, signature, occurrence);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (!item.revisions.length)
|
|
335
|
+
return;
|
|
336
|
+
const stable = item.revisions.filter(({ ref, basis }) => {
|
|
337
|
+
const row = memory.store.read(item.d.id, ref.id, ref.rev);
|
|
338
|
+
return row && isKnowledge(row) && runbookBasis(row) === basis && knowledgeValid(item.d, row).ok;
|
|
339
|
+
});
|
|
340
|
+
if (!stable.length)
|
|
341
|
+
return;
|
|
342
|
+
recordSuccess(exec.agent, item.d, {
|
|
343
|
+
callId: exec.callId,
|
|
344
|
+
sessionId: exec.agent.id,
|
|
345
|
+
storeId: memory.storeId(exec.agent.session),
|
|
346
|
+
timestamp: Date.now(),
|
|
347
|
+
command: item.command,
|
|
348
|
+
workdir: item.workdir,
|
|
349
|
+
exitCode: 0,
|
|
350
|
+
}, stable);
|
|
351
|
+
});
|
|
352
|
+
ctx.on('agent/pre-step', async ({ agent, turn, step, signal }, next) => {
|
|
353
|
+
const decision = await next();
|
|
354
|
+
if (decision.kind !== 'enter' || signal?.aborted || !agent)
|
|
355
|
+
return decision;
|
|
356
|
+
if (memory.assistantGeneration() === undefined)
|
|
357
|
+
return decision;
|
|
358
|
+
const direct = decision.messages.filter(m => m.source?.kind === 'user').map(messageText);
|
|
359
|
+
if (!direct.length)
|
|
360
|
+
return decision;
|
|
361
|
+
const d = memory.domain(agent);
|
|
362
|
+
if (!d || d.id === memory.personal.id)
|
|
363
|
+
return decision;
|
|
364
|
+
try {
|
|
365
|
+
const anchors = new Map();
|
|
366
|
+
const rows = memory.store.messageCandidates(d.id, direct).filter((row) => {
|
|
367
|
+
if (!isKnowledge(row) || ['episode', 'guard'].includes(row.kind))
|
|
368
|
+
return false;
|
|
369
|
+
const trigger = row.knowledge?.trigger?.text;
|
|
370
|
+
return (!!trigger &&
|
|
371
|
+
direct.some(message => message.includes(trigger)) &&
|
|
372
|
+
knowledgeValid(d, row, anchors).ok);
|
|
373
|
+
});
|
|
374
|
+
const hint = issue(memory, agent, d, 'pre:' + turn + '/' + step, rows);
|
|
375
|
+
return hint ? { ...decision, messages: [...decision.messages, hint] } : decision;
|
|
376
|
+
}
|
|
377
|
+
catch (error) {
|
|
378
|
+
// This listener only ever adds an informational hint: a store failure
|
|
379
|
+
// must not fail the whole turn over a hint it could not compute.
|
|
380
|
+
ctx.logger.warn('项目记忆读取失败,本轮未附加触发提示:' + String(error));
|
|
381
|
+
return decision;
|
|
382
|
+
}
|
|
383
|
+
}, { prepend: true });
|
|
384
|
+
registerConditionalTool(ctx, {
|
|
385
|
+
name: 'project_memory_ticket',
|
|
386
|
+
description: prompts[memory.promptLanguage()].ticket,
|
|
387
|
+
parameters: {
|
|
388
|
+
type: 'object',
|
|
389
|
+
properties: { ticket: { type: 'string' } },
|
|
390
|
+
required: ['ticket'],
|
|
391
|
+
additionalProperties: false,
|
|
392
|
+
},
|
|
393
|
+
output: { schema: { type: 'string' }, render: (_a, v) => [{ type: 'text', text: v }] },
|
|
394
|
+
async execute(args, exec) {
|
|
395
|
+
return JSON.stringify(exec.agent ? readTicket(memory, exec.agent, args.ticket, exec.callId, lookup) : { items: [] });
|
|
396
|
+
},
|
|
397
|
+
}, () => memory.assistantGeneration() !== undefined);
|
|
398
|
+
}
|