@jjh32456/longlive 0.0.0-stage → 2.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 +235 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +362 -0
- package/README.en.md +64 -0
- package/README.md +256 -2
- package/SPEC.md +211 -0
- package/build-info.json +1665 -0
- package/cordis.patch.yml +208 -0
- package/docs/A1-MIGRATION.md +75 -0
- package/docs/ANSWER-GUARD.md +92 -0
- package/docs/ARCHITECTURE.md +128 -0
- package/docs/AUTO-RECALL-WIRING.md +115 -0
- package/docs/AUTO-RECALL.md +237 -0
- package/docs/CONCEPTS.md +143 -0
- package/docs/CONFIG.md +839 -0
- package/docs/ERRORS.md +271 -0
- package/docs/FAQ.md +186 -0
- package/docs/FEATURES.md +579 -0
- package/docs/INDEX-FRESHNESS.md +21 -0
- package/docs/METRICS.md +244 -0
- package/docs/MIGRATION-1.6.0.md +220 -0
- package/docs/OPERATIONS.md +439 -0
- package/docs/PANEL-INTEGRATION.md +190 -0
- package/docs/PRIVACY.md +88 -0
- package/docs/QUICKSTART.md +155 -0
- package/docs/RAW-INDEX-POLICY.md +7 -0
- package/docs/README.en.md +48 -0
- package/docs/RESIDENT-WATCHER.md +90 -0
- package/docs/SESSION-WATCH.md +214 -0
- package/docs/TOOLS.md +184 -0
- package/docs/TROUBLESHOOTING.md +329 -0
- package/icon.svg +11 -0
- package/lib/client.js +436 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +163 -4
- package/runbook.md +343 -0
- package/scripts/archive-digest-build.mjs +668 -0
- package/scripts/auto-truncate-run.ps1 +752 -0
- package/scripts/backfill-auto-cards.mjs +98 -0
- package/scripts/edges-build.mjs +1003 -0
- package/scripts/index-maintain.mjs +39 -0
- package/scripts/index-reconcile.mjs +49 -0
- package/scripts/index-status.mjs +28 -0
- package/scripts/install-resident-watcher.ps1 +15 -0
- package/scripts/install-session-watch.ps1 +332 -0
- package/scripts/install-surgery-task.ps1 +174 -0
- package/scripts/lib/auto-truncate-run.ps1 +512 -0
- package/scripts/lib/session-watch-fixture.mjs +207 -0
- package/scripts/longlive-doctor.mjs +578 -0
- package/scripts/metrics-read.mjs +79 -0
- package/scripts/metrics-sample.mjs +215 -0
- package/scripts/migrate-180.mjs +163 -0
- package/scripts/migrate-200.mjs +8 -0
- package/scripts/migrate-a1-legacy.mjs +62 -0
- package/scripts/migrate-lifecycle.mjs +157 -0
- package/scripts/pack.mjs +392 -0
- package/scripts/recall-index-build.mjs +128 -0
- package/scripts/recall-links-build.mjs +420 -0
- package/scripts/rescore-cards.mjs +36 -0
- package/scripts/resident-watcher-status.ps1 +15 -0
- package/scripts/resident-watcher.ps1 +643 -0
- package/scripts/session-watch-drill.mjs +553 -0
- package/scripts/session-watch-status.ps1 +146 -0
- package/scripts/session-watch.mjs +1367 -0
- package/scripts/surgery-inspect.mjs +122 -0
- package/scripts/surgery-run.cmd +34 -0
- package/scripts/truncate-cli.mjs +279 -0
- package/scripts/unattended-surgery.ps1 +1019 -0
- package/scripts/uninstall-resident-watcher.ps1 +15 -0
- package/scripts/uninstall-session-watch.ps1 +192 -0
- package/scripts/verify-card-anchors.mjs +138 -0
- package/scripts/watch-loop.ps1 +916 -0
- package/src/actions/audit.js +125 -0
- package/src/actions/evidence.js +122 -0
- package/src/actions/fs.js +162 -0
- package/src/activity/index.js +873 -0
- package/src/activity/model.js +351 -0
- package/src/activity/store.js +786 -0
- package/src/activity/summary.js +180 -0
- package/src/activity/thinking.js +191 -0
- package/src/adapters/constants.js +11 -0
- package/src/adapters/engram/data/aliases.json +51 -0
- package/src/adapters/engram/data/policy.json +8 -0
- package/src/adapters/engram/data/routing-rules.json +546 -0
- package/src/adapters/engram/index.js +278 -0
- package/src/adapters/registry.js +315 -0
- package/src/answer/audit.js +238 -0
- package/src/answer/claim-guard.mjs +234 -0
- package/src/answer/evidence.js +30 -0
- package/src/answer/guard.js +1370 -0
- package/src/answer/index.js +1086 -0
- package/src/answer/query.js +111 -0
- package/src/answer/second-hop.mjs +101 -0
- package/src/auto-truncate-status.js +164 -0
- package/src/auto-truncate.js +1181 -0
- package/src/autoprocedure/candidate.js +342 -0
- package/src/autoprocedure/config.js +122 -0
- package/src/autoprocedure/evidence.js +345 -0
- package/src/autoprocedure/index.js +83 -0
- package/src/autoprocedure/ledger.js +329 -0
- package/src/autoprocedure/lint.js +232 -0
- package/src/autoprocedure/llm.js +409 -0
- package/src/autoprocedure/locks.js +102 -0
- package/src/autoprocedure/paths.js +39 -0
- package/src/autoprocedure/privacy.js +87 -0
- package/src/autoprocedure/rules.js +288 -0
- package/src/autoprocedure/runtime.js +718 -0
- package/src/autoprocedure/session-reader.js +328 -0
- package/src/backup.js +310 -0
- package/src/bus/bus.mjs +1446 -0
- package/src/codec.js +269 -0
- package/src/compaction/cut-engine.js +44 -0
- package/src/compaction/engine-core.js +1237 -0
- package/src/compaction/entry.js +228 -0
- package/src/compaction.js +230 -0
- package/src/conclusion.js +366 -0
- package/src/config.js +835 -0
- package/src/d6/runtime.js +517 -0
- package/src/d6/surface.js +181 -0
- package/src/deps.js +189 -0
- package/src/engine-status.js +141 -0
- package/src/features.js +1035 -0
- package/src/handoff/index.js +1077 -0
- package/src/handoff/model.js +1058 -0
- package/src/handoff/store.js +1233 -0
- package/src/heartbeat.js +385 -0
- package/src/home.js +16 -0
- package/src/host-version.js +280 -0
- package/src/hot-truncate-diag.js +98 -0
- package/src/identity/card-marker.js +281 -0
- package/src/identity/handover.js +845 -0
- package/src/identity/index.js +337 -0
- package/src/identity/inject-position.js +133 -0
- package/src/identity/writeback.js +344 -0
- package/src/identity.js +118 -0
- package/src/index.js +647 -0
- package/src/lifecycle/gap-clocks.js +364 -0
- package/src/lifecycle/index.js +269 -0
- package/src/lifecycle/proactive.js +1426 -0
- package/src/live-target.js +212 -0
- package/src/live.js +1603 -0
- package/src/log-rotation.js +153 -0
- package/src/memory/a1-migration.js +190 -0
- package/src/memory/activity-capture.js +229 -0
- package/src/memory/anchor-fallback.js +116 -0
- package/src/memory/anchors.js +194 -0
- package/src/memory/archive-cold-state.mjs +794 -0
- package/src/memory/archive-staging.mjs +305 -0
- package/src/memory/archive.mjs +415 -0
- package/src/memory/audit-records.js +222 -0
- package/src/memory/auto-cards.js +441 -0
- package/src/memory/capture-host.js +297 -0
- package/src/memory/capture.js +496 -0
- package/src/memory/card-anchors.js +203 -0
- package/src/memory/card-priority.js +110 -0
- package/src/memory/cards.js +425 -0
- package/src/memory/catchup.mjs +534 -0
- package/src/memory/conclusion-distill.js +381 -0
- package/src/memory/conclusion-drafts.js +243 -0
- package/src/memory/conflicts.js +1060 -0
- package/src/memory/consolidate.js +834 -0
- package/src/memory/decay.js +311 -0
- package/src/memory/distill.js +485 -0
- package/src/memory/event-index.js +1826 -0
- package/src/memory/evidence-chain.js +295 -0
- package/src/memory/evidence.js +213 -0
- package/src/memory/filter-runner.js +555 -0
- package/src/memory/filter.js +1035 -0
- package/src/memory/forget-v2.js +680 -0
- package/src/memory/forget.js +531 -0
- package/src/memory/frames-activation.js +249 -0
- package/src/memory/frontmatter.js +454 -0
- package/src/memory/fts-query.js +342 -0
- package/src/memory/governance.js +604 -0
- package/src/memory/index-freshness.js +234 -0
- package/src/memory/index.js +435 -0
- package/src/memory/l2-gate.js +169 -0
- package/src/memory/land.mjs +743 -0
- package/src/memory/lifecycle.js +235 -0
- package/src/memory/lock.js +121 -0
- package/src/memory/maintenance.js +1149 -0
- package/src/memory/manifest.js +245 -0
- package/src/memory/memory-health.mjs +607 -0
- package/src/memory/migrate-200.js +1681 -0
- package/src/memory/migrate-lifecycle.js +1808 -0
- package/src/memory/migrate.js +139 -0
- package/src/memory/paths.js +229 -0
- package/src/memory/procedure-candidate.mjs +377 -0
- package/src/memory/procedure.js +982 -0
- package/src/memory/prospective-memory.mjs +362 -0
- package/src/memory/provenance.js +119 -0
- package/src/memory/recall-feedback.mjs +406 -0
- package/src/memory/recall-v2.js +218 -0
- package/src/memory/recall.js +357 -0
- package/src/memory/reconcile.js +265 -0
- package/src/memory/records.js +433 -0
- package/src/memory/reinforce.js +281 -0
- package/src/memory/retention.js +162 -0
- package/src/memory/review.js +124 -0
- package/src/memory/state.js +279 -0
- package/src/memory/tiers.js +168 -0
- package/src/memory/topic-phrases.js +73 -0
- package/src/memory/trust.js +97 -0
- package/src/memory/turn-budget.mjs +290 -0
- package/src/memory/version-chain.js +264 -0
- package/src/memory/wake-bridge.mjs +2999 -0
- package/src/memory/wal.js +137 -0
- package/src/memory/working-frame.js +779 -0
- package/src/metrics/classify.mjs +191 -0
- package/src/metrics/estimate.mjs +108 -0
- package/src/metrics/index.mjs +204 -0
- package/src/metrics/measure.mjs +462 -0
- package/src/metrics/prestep-latency.mjs +307 -0
- package/src/metrics/store.mjs +264 -0
- package/src/original.js +294 -0
- package/src/policy/threshold-policy.js +224 -0
- package/src/policy-profile.js +463 -0
- package/src/recall/anaphora.mjs +87 -0
- package/src/recall/archive-config.mjs +130 -0
- package/src/recall/archive-fallback.mjs +390 -0
- package/src/recall/archive-search.mjs +428 -0
- package/src/recall/archive-state.mjs +588 -0
- package/src/recall/brain.mjs +501 -0
- package/src/recall/cards.mjs +202 -0
- package/src/recall/current-line.mjs +110 -0
- package/src/recall/edges.js +398 -0
- package/src/recall/embedding.mjs +698 -0
- package/src/recall/entities.mjs +112 -0
- package/src/recall/explain.mjs +443 -0
- package/src/recall/force-policy.mjs +138 -0
- package/src/recall/gate-log.mjs +280 -0
- package/src/recall/gate-metrics.mjs +191 -0
- package/src/recall/index.js +1337 -0
- package/src/recall/inject-order.mjs +279 -0
- package/src/recall/link-recall.mjs +183 -0
- package/src/recall/links.mjs +696 -0
- package/src/recall/privacy.mjs +208 -0
- package/src/recall/recall-core.mjs +810 -0
- package/src/recall/shared-visibility.js +302 -0
- package/src/recall/time-normalizer.mjs +196 -0
- package/src/recall/turn-budget-inject.mjs +154 -0
- package/src/retention/cold-gate.mjs +146 -0
- package/src/retention/config.js +163 -0
- package/src/retention/index.js +801 -0
- package/src/role/activation.js +191 -0
- package/src/role/attention.js +318 -0
- package/src/role/base.js +36 -0
- package/src/role/channels.js +423 -0
- package/src/role/index.js +425 -0
- package/src/role/interfaces.js +115 -0
- package/src/role/personality.js +370 -0
- package/src/role/readers.js +151 -0
- package/src/role/relationships.js +477 -0
- package/src/role/store.js +202 -0
- package/src/role/writeprotect.js +373 -0
- package/src/router/aliases.json +182 -0
- package/src/router/background/agent-runner.mjs +239 -0
- package/src/router/background/anchor.mjs +323 -0
- package/src/router/background/audit.mjs +337 -0
- package/src/router/background/budget-ledger.mjs +648 -0
- package/src/router/background/host-sim.mjs +451 -0
- package/src/router/background/index.mjs +81 -0
- package/src/router/background/memory.mjs +63 -0
- package/src/router/background/mock-llm.mjs +203 -0
- package/src/router/background/queue.mjs +224 -0
- package/src/router/background/retry.mjs +255 -0
- package/src/router/background/router.mjs +1459 -0
- package/src/router/background/stream.mjs +276 -0
- package/src/router/background/tool-policy.mjs +255 -0
- package/src/router/design-headings.json +221 -0
- package/src/router/face/config.mjs +155 -0
- package/src/router/face/context.mjs +284 -0
- package/src/router/face/face.mjs +466 -0
- package/src/router/face/identity.mjs +130 -0
- package/src/router/face/index.mjs +65 -0
- package/src/router/face/journal.mjs +119 -0
- package/src/router/face/llm.mjs +252 -0
- package/src/router/face/outbox.mjs +153 -0
- package/src/router/face/paths.mjs +54 -0
- package/src/router/judge.mjs +987 -0
- package/src/router/policies/generic.json +131 -0
- package/src/router/route.js +1766 -0
- package/src/router/router.mjs +535 -0
- package/src/router/routing-rules.json +816 -0
- package/src/router/run-sim.mjs +530 -0
- package/src/router/runtime.js +569 -0
- package/src/router/service/apply.mjs +1218 -0
- package/src/router/service/board.mjs +712 -0
- package/src/router/service/caller.mjs +131 -0
- package/src/router/service/config.mjs +361 -0
- package/src/router/service/context.mjs +314 -0
- package/src/router/service/hint.mjs +668 -0
- package/src/router/service/lease.mjs +494 -0
- package/src/router/service/live.mjs +251 -0
- package/src/router/service/lock.mjs +550 -0
- package/src/router/service/merge.mjs +255 -0
- package/src/router/service/model-resolve.mjs +350 -0
- package/src/router/service/paths.mjs +168 -0
- package/src/router/service/proposals.mjs +1461 -0
- package/src/router/service/queue.mjs +477 -0
- package/src/router/service/reconcile.mjs +271 -0
- package/src/router/service/service.mjs +2998 -0
- package/src/router/service/sources.mjs +352 -0
- package/src/router/service/status.mjs +520 -0
- package/src/router/service/store.mjs +476 -0
- package/src/router/service/ticklock.mjs +239 -0
- package/src/router/service/tokens.mjs +95 -0
- package/src/router/service/tools.mjs +248 -0
- package/src/router/service/wake.mjs +544 -0
- package/src/router/service/wal.mjs +685 -0
- package/src/router/shadow.mjs +821 -0
- package/src/surgery-recovery.js +1909 -0
- package/src/tool-output.js +485 -0
- package/src/tools.js +3262 -0
- package/src/truncate-visible.js +2416 -0
- package/src/truncate.js +999 -0
- package/src/ui/actions.js +1312 -0
- package/src/ui/flags.js +259 -0
- package/src/ui/panel-api.js +371 -0
- package/src/ui/panel-auth.js +251 -0
- package/src/ui/panel-integrity.js +243 -0
- package/src/ui/routes-readonly.js +759 -0
- package/src/ui/routes.js +284 -0
- package/src/util/append-store.js +368 -0
- package/src/util/local-day.js +30 -0
- package/src/util/redact.js +227 -0
- package/src/util/stale-locks.js +148 -0
- package/src/version.js +99 -0
- package/test/fixtures/pre-step-container.mjs +100 -0
- package/test/p1_probe.mjs +97 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
// src/autoprocedure/rules.js — A 模式:纯本地规则蒸馏(D1,默认路径)。
|
|
2
|
+
//
|
|
3
|
+
// 零 LLM、零网络、零 provider 探测:全部四段文本来自本地模板 + 事件字段。
|
|
4
|
+
// 提取对象:已完成工具调用序列(工具名 + 关键参数 + 结果状态)、exit code /
|
|
5
|
+
// 错误行、失败错误行、验证命令。蒸馏前每个片段先过 privacy 门(F9 第一道),
|
|
6
|
+
// 命中即丢弃该片段并计数(由 runtime 记进账本)。
|
|
7
|
+
//
|
|
8
|
+
// 不变量:没有成功步骤 → 不产出候选(eligible:false),由 runtime 记
|
|
9
|
+
// terminal reject reason=ineligible;绝不为了产出而编造步骤。
|
|
10
|
+
|
|
11
|
+
import { buildTurnItems } from '../original.js';
|
|
12
|
+
import { dayString } from '../memory/frontmatter.js';
|
|
13
|
+
import { sanitizeFragment } from './privacy.js';
|
|
14
|
+
import { makeSessionEvidence, makeD2Evidence, sessionToolResultText } from './evidence.js';
|
|
15
|
+
|
|
16
|
+
export const RULES_MAX_STEPS = 10;
|
|
17
|
+
export const RULES_MAX_FAILURES = 5;
|
|
18
|
+
export const RULES_CLIP = 200;
|
|
19
|
+
export const RULES_NAME_MAX = 120;
|
|
20
|
+
|
|
21
|
+
const ERROR_LINE = /(?:^|\r?\n)\s*(?:error|ERROR|Error|错误|失败|异常|Exception|Traceback|EACCES|EPERM|ENOENT|not found|No such file|permission denied|denied|timed out|超时|exit code [1-9])[^\r\n]*/g;
|
|
22
|
+
|
|
23
|
+
function clip(text, max = RULES_CLIP) {
|
|
24
|
+
const value = String(text ?? '').replace(/\s+/gu, ' ').trim();
|
|
25
|
+
return value.length <= max ? value : `${value.slice(0, Math.max(1, max - 1))}…`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function cjkToken(value) {
|
|
29
|
+
return /[\u3400-\u9fff]/.test(value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Split a task sentence into trigger-word candidates (deterministic, no deps). */
|
|
33
|
+
export function triggerCandidates(userText, toolNames = []) {
|
|
34
|
+
const stop = new Set([
|
|
35
|
+
'the', 'and', 'for', 'with', 'this', 'that', 'from', 'into', 'then',
|
|
36
|
+
'然后', '我们', '可以', '一个', '这个', '那个', '需要', '进行', '使用', '任务', '文件', '命令',
|
|
37
|
+
'现在', '请先', '帮我', '一下', '这样', '就是', '还是', '已经', '如果', '之后', '接着',
|
|
38
|
+
]);
|
|
39
|
+
const seen = new Set();
|
|
40
|
+
const tokens = [];
|
|
41
|
+
const push = (raw) => {
|
|
42
|
+
let token = String(raw ?? '').replace(/[,\[\],、]/g, '').trim();
|
|
43
|
+
// Light segmentation for CJK run-on sentences: split on conjunctions, then
|
|
44
|
+
// trim a leading polite verb. No dictionary, no dependency — deterministic.
|
|
45
|
+
token = token.replace(/^[请帮给我把将再就还]+/u, '');
|
|
46
|
+
if (token.length < 2 || token.length > 60) return;
|
|
47
|
+
const key = token.toLowerCase();
|
|
48
|
+
if (stop.has(key) || /^\d+$/.test(token) || seen.has(key)) return;
|
|
49
|
+
if (!cjkToken(token) && token.length < 3) return;
|
|
50
|
+
seen.add(key);
|
|
51
|
+
tokens.push(token);
|
|
52
|
+
};
|
|
53
|
+
for (const raw of String(userText ?? '').split(/[\s,,。、;;::!?!?()()\[\]【】"'“”‘’`<>|/\\]+/u)) {
|
|
54
|
+
for (const piece of raw.split(/并|和|与|及|然后|接着|之后|再去|还有/u)) push(piece);
|
|
55
|
+
}
|
|
56
|
+
for (const name of Array.isArray(toolNames) ? toolNames : []) push(name);
|
|
57
|
+
// Prefer the task words (CJK) first, then tool names; the per-key cap is 3.
|
|
58
|
+
const cjk = tokens.filter((token) => cjkToken(token)).slice(0, 3);
|
|
59
|
+
const rest = tokens.filter((token) => !cjkToken(token)).slice(0, 3);
|
|
60
|
+
return [...new Set([...cjk, ...rest])].slice(0, 5);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function parseCallArgs(text) {
|
|
64
|
+
const raw = String(text ?? '').trim();
|
|
65
|
+
if (raw === '') return null;
|
|
66
|
+
try {
|
|
67
|
+
const parsed = JSON.parse(raw);
|
|
68
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) return parsed;
|
|
69
|
+
} catch {
|
|
70
|
+
/* a non-JSON argument string is still usable text */
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function argSummary(args, rawText) {
|
|
76
|
+
if (!args) return clip(rawText, 160);
|
|
77
|
+
const priority = ['command', 'cmd', 'script', 'path', 'filePath', 'file_path', 'file',
|
|
78
|
+
'pattern', 'query', 'url', 'target', 'name', 'prompt'];
|
|
79
|
+
const parts = [];
|
|
80
|
+
for (const key of priority) {
|
|
81
|
+
const value = args[key];
|
|
82
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
83
|
+
parts.push(clip(value, 160));
|
|
84
|
+
if (parts.length >= 2) break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (parts.length === 0) {
|
|
88
|
+
for (const [key, value] of Object.entries(args)) {
|
|
89
|
+
if (parts.length >= 2) break;
|
|
90
|
+
if (typeof value === 'string' && value.trim() !== '' && key !== 'description') parts.push(`${key}=${clip(value, 80)}`);
|
|
91
|
+
else if (typeof value === 'number' || typeof value === 'boolean') parts.push(`${key}=${value}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return parts.join(';') || clip(rawText, 160);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function exitCodeOf(resultText) {
|
|
98
|
+
const match = /(?:exit\s*code|退出码|\$LASTEXITCODE)[^0-9-]{0,16}(-?\d+)/i.exec(String(resultText ?? ''));
|
|
99
|
+
return match ? Number(match[1]) : null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function failureLinesOf(resultText) {
|
|
103
|
+
const out = [];
|
|
104
|
+
const text = String(resultText ?? '');
|
|
105
|
+
ERROR_LINE.lastIndex = 0;
|
|
106
|
+
let match = ERROR_LINE.exec(text);
|
|
107
|
+
while (match !== null && out.length < 3) {
|
|
108
|
+
const line = match[0].replace(/^\s+/, '').trim();
|
|
109
|
+
if (line !== '') out.push(clip(line, 160));
|
|
110
|
+
match = ERROR_LINE.exec(text);
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function slugOf(sessionId, turn) {
|
|
116
|
+
// Deterministic suffix: same session/turn = same candidate slug (idempotent,
|
|
117
|
+
// and never derived from user text so a crafted name cannot escape staging).
|
|
118
|
+
let h = 2166136261;
|
|
119
|
+
const text = `${sessionId}|${turn}`;
|
|
120
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
121
|
+
h ^= text.charCodeAt(i);
|
|
122
|
+
h = Math.imul(h, 16777619);
|
|
123
|
+
}
|
|
124
|
+
return `auto-${turn}-${(h >>> 0).toString(16).padStart(8, '0')}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Build the local (A-mode) candidate draft for one completed turn.
|
|
129
|
+
*
|
|
130
|
+
* @returns {{eligible:boolean, reason?:string, stats:object, draft?:object}}
|
|
131
|
+
*/
|
|
132
|
+
export function buildRuleDraft({
|
|
133
|
+
sessionId,
|
|
134
|
+
turn,
|
|
135
|
+
parsed,
|
|
136
|
+
turnSample,
|
|
137
|
+
minToolCalls = 8,
|
|
138
|
+
memoryRoot = '',
|
|
139
|
+
home = '',
|
|
140
|
+
now = new Date(),
|
|
141
|
+
readFile = null,
|
|
142
|
+
}) {
|
|
143
|
+
const stats = { completedCalls: 0, successfulCalls: 0, privacyDropped: 0, evidenceDropped: 0 };
|
|
144
|
+
const turns = buildTurnItems(parsed?.events ?? []);
|
|
145
|
+
const items = turns.get(turn) ?? [];
|
|
146
|
+
const calls = items.filter((item) => item.kind === 'call');
|
|
147
|
+
const results = new Map();
|
|
148
|
+
for (const item of items) {
|
|
149
|
+
if (item.kind === 'tool' && item.toolCallId) results.set(item.toolCallId, item);
|
|
150
|
+
}
|
|
151
|
+
const completed = calls.filter((call) => call.toolCallId && results.has(call.toolCallId));
|
|
152
|
+
stats.completedCalls = completed.length;
|
|
153
|
+
if (completed.length < Math.max(0, minToolCalls)) {
|
|
154
|
+
return { eligible: false, reason: 'min_tool_calls', stats: { ...stats, requiredCalls: minToolCalls } };
|
|
155
|
+
}
|
|
156
|
+
const successful = completed.filter((call) => results.get(call.toolCallId)?.isError !== true);
|
|
157
|
+
stats.successfulCalls = successful.length;
|
|
158
|
+
if (successful.length === 0) {
|
|
159
|
+
return { eligible: false, reason: 'no_successful_calls', stats };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const privacyOptions = { memoryRoot, home };
|
|
163
|
+
const sanitize = (text, max) => {
|
|
164
|
+
const result = sanitizeFragment(clip(text, max), privacyOptions);
|
|
165
|
+
if (!result.ok) {
|
|
166
|
+
stats.privacyDropped += 1;
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return result.text;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const userItem = items.find((item) => item.kind === 'message' && item.role === 'user' && item.text.trim() !== '');
|
|
173
|
+
const rawUserText = userItem ? userItem.text : '';
|
|
174
|
+
const userText = sanitize(rawUserText, 300) ?? '';
|
|
175
|
+
|
|
176
|
+
const evidence = [];
|
|
177
|
+
const steps = [];
|
|
178
|
+
const calledToolNames = [];
|
|
179
|
+
const failureLines = [];
|
|
180
|
+
const turnSeq = { seqStart: turnSample.seqStart, seqEnd: turnSample.seqEnd, turnDigest: turnSample.digest };
|
|
181
|
+
|
|
182
|
+
for (const call of successful) {
|
|
183
|
+
const result = results.get(call.toolCallId);
|
|
184
|
+
const toolName = call.toolName || result?.toolName || 'tool';
|
|
185
|
+
if (!calledToolNames.includes(toolName)) calledToolNames.push(toolName);
|
|
186
|
+
const args = parseCallArgs(call.text);
|
|
187
|
+
const summary = argSummary(args, call.text);
|
|
188
|
+
const exitCode = exitCodeOf(result?.text);
|
|
189
|
+
const stepText = sanitize(
|
|
190
|
+
`调用 ${toolName}:${summary}${exitCode === 0 ? '(退出码 0)' : (exitCode !== null ? `(退出码 ${exitCode})` : '')}`,
|
|
191
|
+
400,
|
|
192
|
+
);
|
|
193
|
+
if (stepText === null) continue;
|
|
194
|
+
const stepIndex = steps.length + 1;
|
|
195
|
+
steps.push(stepText);
|
|
196
|
+
// Session evidence hangs on the tool/call event line (the step's own record).
|
|
197
|
+
const callEvidence = makeSessionEvidence({
|
|
198
|
+
step: stepIndex,
|
|
199
|
+
sessionId,
|
|
200
|
+
turn,
|
|
201
|
+
turnSample,
|
|
202
|
+
seq: call.seq,
|
|
203
|
+
});
|
|
204
|
+
if (callEvidence) evidence.push(callEvidence);
|
|
205
|
+
else stats.evidenceDropped += 1;
|
|
206
|
+
|
|
207
|
+
// D2 evidence is optional and additive: it anchors the archived tool output.
|
|
208
|
+
if (result && typeof call.toolCallId === 'string' && call.toolCallId !== '') {
|
|
209
|
+
const d2 = makeD2Evidence({
|
|
210
|
+
step: stepIndex,
|
|
211
|
+
memoryRoot,
|
|
212
|
+
sessionId,
|
|
213
|
+
turn,
|
|
214
|
+
seqStart: turnSeq.seqStart,
|
|
215
|
+
seqEnd: turnSeq.seqEnd,
|
|
216
|
+
turnDigest: turnSeq.turnDigest,
|
|
217
|
+
callId: call.toolCallId,
|
|
218
|
+
sessionResultText: sessionToolResultText(parsed?.events, call.toolCallId),
|
|
219
|
+
readFile,
|
|
220
|
+
});
|
|
221
|
+
if (d2.ok) evidence.push(d2.evidence);
|
|
222
|
+
else if (d2.code !== 'E_EVIDENCE_D2_MISSING') stats.evidenceDropped += 1;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
for (const line of failureLinesOf(result?.text)) {
|
|
226
|
+
if (failureLines.length >= RULES_MAX_FAILURES) break;
|
|
227
|
+
const clean = sanitize(line, 200);
|
|
228
|
+
if (clean !== null) failureLines.push(clean);
|
|
229
|
+
}
|
|
230
|
+
if (steps.length >= RULES_MAX_STEPS) break;
|
|
231
|
+
}
|
|
232
|
+
if (steps.length === 0) {
|
|
233
|
+
return { eligible: false, reason: 'no_steps', stats };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const trigger = triggerCandidates(userText, calledToolNames).slice(0, 5);
|
|
237
|
+
if (trigger.length === 0) {
|
|
238
|
+
return { eligible: false, reason: 'no_trigger', stats };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const toolSequence = calledToolNames.join(' → ') || '(未记录工具名)';
|
|
242
|
+
const when = userText !== ''
|
|
243
|
+
? `当用户提出与「${clip(userText, 120)}」类似的任务时,按本次成功路径执行。`
|
|
244
|
+
: `当需要复现本次工具链(${toolSequence})时使用。`;
|
|
245
|
+
const failureModes = failureLines.length > 0
|
|
246
|
+
? failureLines.join('\n')
|
|
247
|
+
: '(本次轨迹未记录失败;成功路径见「怎么做」,复用时仍需自行验证。)';
|
|
248
|
+
const example = `第 ${turn} 轮,共 ${completed.length} 次工具调用(成功 ${successful.length} 次)。`
|
|
249
|
+
+ `工具序列:${toolSequence}。`
|
|
250
|
+
+ (userText !== '' ? `触发任务:${clip(userText, 80)}。` : '');
|
|
251
|
+
const cleanWhen = sanitize(when, 600) ?? `当需要复现本次工具链(${toolSequence})时使用。`;
|
|
252
|
+
const cleanFailure = sanitize(failureModes, 1000) ?? '(本次轨迹未记录失败。)';
|
|
253
|
+
const cleanExample = sanitize(example, 600) ?? `第 ${turn} 轮:${toolSequence}。`;
|
|
254
|
+
|
|
255
|
+
const baseName = userText !== '' ? clip(userText, 60) : toolSequence;
|
|
256
|
+
const name = clip(`自动候选·第${turn}轮:${baseName}`, RULES_NAME_MAX);
|
|
257
|
+
const slug = slugOf(sessionId, turn);
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
eligible: true,
|
|
261
|
+
stats,
|
|
262
|
+
draft: {
|
|
263
|
+
name,
|
|
264
|
+
slug,
|
|
265
|
+
trigger,
|
|
266
|
+
salience: 3,
|
|
267
|
+
strength: 3,
|
|
268
|
+
lastUsed: dayString(now instanceof Date ? now : new Date(now)),
|
|
269
|
+
sections: {
|
|
270
|
+
when: cleanWhen,
|
|
271
|
+
steps,
|
|
272
|
+
failureModes: cleanFailure,
|
|
273
|
+
example: cleanExample,
|
|
274
|
+
},
|
|
275
|
+
evidence,
|
|
276
|
+
source: {
|
|
277
|
+
sessionId,
|
|
278
|
+
turn,
|
|
279
|
+
seqStart: turnSample.seqStart,
|
|
280
|
+
seqEnd: turnSample.seqEnd,
|
|
281
|
+
turnDigest: turnSample.digest,
|
|
282
|
+
},
|
|
283
|
+
generator: 'rules',
|
|
284
|
+
llm: { used: false, model: null, tokens: { in: 0, out: 0 } },
|
|
285
|
+
stats,
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
}
|