@mebular/mcp 0.3.5 → 0.3.8
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/bin/mebular.mjs +120 -0
- package/package.json +4 -4
- package/src/adapter-health.mjs +213 -0
- package/src/adapter-provision.mjs +405 -0
- package/src/adapter-state.mjs +154 -0
- package/src/adapters/dsh.mjs +830 -0
- package/src/adapters/hermes.mjs +966 -0
- package/src/adapters/index.mjs +91 -0
- package/src/adapters/opencode.mjs +757 -0
- package/src/clients.mjs +184 -13
- package/src/first-run.mjs +343 -0
- package/src/memory-capability.mjs +15 -0
- package/src/memory-runtime-config.mjs +2 -0
- package/src/memory-runtime-effective.mjs +91 -0
- package/src/memory-runtime-supervisor.mjs +247 -2
- package/src/provision.mjs +11 -1
- package/src/serve.mjs +798 -11
- package/src/server.mjs +36 -0
- package/src/tools.mjs +8 -0
- package/src/version.mjs +1 -1
|
@@ -1180,6 +1180,11 @@ function clampOffset(value) {
|
|
|
1180
1180
|
return Math.max(n, 0);
|
|
1181
1181
|
}
|
|
1182
1182
|
|
|
1183
|
+
/** 可选 asOf:只接受有限数字;缺省/非法不写入请求(读路径回落当前时间)。 */
|
|
1184
|
+
function parseAsOf(value) {
|
|
1185
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1183
1188
|
function clampDepth(value) {
|
|
1184
1189
|
const n = typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : 2;
|
|
1185
1190
|
return Math.min(Math.max(n, 0), MAX_DEPTH);
|
|
@@ -1409,6 +1414,7 @@ function recordView(memory, node, { userId = 'user', withScore = false, detailAv
|
|
|
1409
1414
|
ownerType: owner.ownerType,
|
|
1410
1415
|
ownerId: owner.ownerId,
|
|
1411
1416
|
provenance: buildProvenance(metadata, domain, owner, node),
|
|
1417
|
+
...(isObject(memory?.provenance) ? { retrievalProvenance: memory.provenance } : {}),
|
|
1412
1418
|
content,
|
|
1413
1419
|
text,
|
|
1414
1420
|
tags,
|
|
@@ -4925,6 +4931,7 @@ export function createMemoryCapabilityHandler({
|
|
|
4925
4931
|
});
|
|
4926
4932
|
const limit = clampLimit(payload.limit);
|
|
4927
4933
|
const offset = clampOffset(payload.offset);
|
|
4934
|
+
const asOf = parseAsOf(payload.asOf);
|
|
4928
4935
|
const types = parseTypes(payload.types ?? undefined);
|
|
4929
4936
|
if (resolved.domains.length === 0) {
|
|
4930
4937
|
return sendOk({
|
|
@@ -4945,6 +4952,7 @@ export function createMemoryCapabilityHandler({
|
|
|
4945
4952
|
offset,
|
|
4946
4953
|
...(payload.includeHistory !== undefined ? { includeHistory: payload.includeHistory === true } : {}),
|
|
4947
4954
|
...(payload.includeArchived !== undefined ? { includeArchived: payload.includeArchived === true } : {}),
|
|
4955
|
+
...(asOf !== undefined ? { asOf } : {}),
|
|
4948
4956
|
filters: { ...(filters ?? {}), namespace: resolved.domains },
|
|
4949
4957
|
});
|
|
4950
4958
|
const cache = new Map();
|
|
@@ -4963,12 +4971,16 @@ export function createMemoryCapabilityHandler({
|
|
|
4963
4971
|
detailAvailability,
|
|
4964
4972
|
}));
|
|
4965
4973
|
const total = typeof result?.totalMatches === 'number' ? result.totalMatches : records.length;
|
|
4974
|
+
const truncated = result?.truncated === true;
|
|
4966
4975
|
return sendOk({
|
|
4967
4976
|
records,
|
|
4968
4977
|
total,
|
|
4969
4978
|
offset,
|
|
4970
4979
|
limit,
|
|
4980
|
+
// hasMore 只表示“当前分页是否还有下一页”;truncated 单独表达
|
|
4981
|
+
// “结果是否被图扩展预算截断”,避免空页也永远 hasMore=true。
|
|
4971
4982
|
hasMore: offset + records.length < total,
|
|
4983
|
+
...(truncated ? { truncated: true } : {}),
|
|
4972
4984
|
tookMs: typeof result?.queryTimeMs === 'number' ? result.queryTimeMs : 0,
|
|
4973
4985
|
usedDomains: resolved.domains,
|
|
4974
4986
|
}, resolved.warnings);
|
|
@@ -4997,6 +5009,7 @@ export function createMemoryCapabilityHandler({
|
|
|
4997
5009
|
service: currentService,
|
|
4998
5010
|
});
|
|
4999
5011
|
const limit = clampLimit(payload.limit);
|
|
5012
|
+
const asOf = parseAsOf(payload.asOf);
|
|
5000
5013
|
const types = parseTypes(payload.types ?? undefined);
|
|
5001
5014
|
if (resolved.domains.length === 0) {
|
|
5002
5015
|
return sendOk({ records: [], relations: [], total: 0, tookMs: 0 }, resolved.warnings);
|
|
@@ -5009,6 +5022,7 @@ export function createMemoryCapabilityHandler({
|
|
|
5009
5022
|
...(payload.includeHistory !== undefined ? { includeHistory: payload.includeHistory === true } : {}),
|
|
5010
5023
|
...(payload.includeRelations !== undefined ? { includeRelations: payload.includeRelations === true } : {}),
|
|
5011
5024
|
...(payload.includeArchived !== undefined ? { includeArchived: payload.includeArchived === true } : {}),
|
|
5025
|
+
...(asOf !== undefined ? { asOf } : {}),
|
|
5012
5026
|
filters: { ...(filters ?? {}), namespace: resolved.domains },
|
|
5013
5027
|
});
|
|
5014
5028
|
const cache = new Map();
|
|
@@ -5038,6 +5052,7 @@ export function createMemoryCapabilityHandler({
|
|
|
5038
5052
|
records,
|
|
5039
5053
|
relations,
|
|
5040
5054
|
total: records.length,
|
|
5055
|
+
...(result?.truncated === true ? { truncated: true } : {}),
|
|
5041
5056
|
tookMs: typeof result?.queryTimeMs === 'number' ? result.queryTimeMs : 0,
|
|
5042
5057
|
usedDomains: resolved.domains,
|
|
5043
5058
|
}, resolved.warnings);
|
|
@@ -78,6 +78,8 @@ export async function readMemoryRuntimeModelSource({ home, env = process.env } =
|
|
|
78
78
|
const normalized = normalizeMemoryRuntimeModelSource(model.source);
|
|
79
79
|
return {
|
|
80
80
|
source: normalized ?? DEFAULT_MEMORY_RUNTIME_MODEL_SOURCE,
|
|
81
|
+
/** 缺失 ≠ 显式 heuristic;effective mode 依赖这个标志。 */
|
|
82
|
+
sourcePresent: model.source !== undefined && model.source !== null,
|
|
81
83
|
defaultSource: DEFAULT_MEMORY_RUNTIME_MODEL_SOURCE,
|
|
82
84
|
values: [...MEMORY_RUNTIME_MODEL_SOURCES],
|
|
83
85
|
exists,
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// 0.4.0 Memory Runtime effective mode(MCP 轻量视图)。
|
|
2
|
+
//
|
|
3
|
+
// 与 sidecar `src/sidecar/effective.ts` 同一张推导表:
|
|
4
|
+
// - `memoryRuntime.enabled` 是 immersive 的唯一真值;
|
|
5
|
+
// - enabled=false / 缺少字段 → lightweight / off,不启动 sidecar;
|
|
6
|
+
// - enabled=true 但 `model.source` 缺失 → off(保持 0.3 baseline,不能静默 heuristic);
|
|
7
|
+
// - enabled=true + 显式 heuristic → heuristic;
|
|
8
|
+
// - enabled=true + local/host/api/auto → model(0.4.0 local 仅 assisted/shadow);
|
|
9
|
+
// - 旧 intervention.mode 不参与 production,只产生稳定 warning(显式 off 也不例外);
|
|
10
|
+
// - shadow 只能由内部 diagnostic 打开。
|
|
11
|
+
//
|
|
12
|
+
// 本模块只做纯计算,不读文件/环境;不返回 token、keyRef 或配置正文。
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
DEFAULT_MEMORY_RUNTIME_MODEL_SOURCE,
|
|
16
|
+
normalizeMemoryRuntimeModelSource,
|
|
17
|
+
} from './memory-runtime-config.mjs';
|
|
18
|
+
|
|
19
|
+
export const MEMORY_RUNTIME_MODES = Object.freeze(['lightweight', 'immersive']);
|
|
20
|
+
export const LEGACY_INTERVENTION_MODE_IGNORED = 'legacy_intervention_mode_ignored';
|
|
21
|
+
export const MODEL_SOURCE_MISSING = 'model_source_missing';
|
|
22
|
+
|
|
23
|
+
function isObject(value) {
|
|
24
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 由 MCP 配置 + sidecar model.source 推导 effective mode。
|
|
29
|
+
*
|
|
30
|
+
* 输入 `config` 是 MCP `<home>/config.json` 的(结构化)视图;`modelSource`/`sourcePresent`
|
|
31
|
+
* 由调用方从 sidecar 只读视图传入。`legacyMode`/`legacyModePresent` 只在状态诊断时需要。
|
|
32
|
+
*/
|
|
33
|
+
export function resolveMemoryRuntimeEffectiveMode({
|
|
34
|
+
config,
|
|
35
|
+
modelSource,
|
|
36
|
+
sourcePresent,
|
|
37
|
+
legacyMode = null,
|
|
38
|
+
legacyModePresent,
|
|
39
|
+
diagnosticShadow = false,
|
|
40
|
+
} = {}) {
|
|
41
|
+
const root = isObject(config) ? config : {};
|
|
42
|
+
const memoryRuntime = isObject(root.memoryRuntime) ? root.memoryRuntime : {};
|
|
43
|
+
const enabled = memoryRuntime.enabled === true;
|
|
44
|
+
const configuredSource = modelSource
|
|
45
|
+
?? root.modelSource
|
|
46
|
+
?? (isObject(root.model) ? root.model.source : undefined);
|
|
47
|
+
const source = normalizeMemoryRuntimeModelSource(configuredSource)
|
|
48
|
+
?? DEFAULT_MEMORY_RUNTIME_MODEL_SOURCE;
|
|
49
|
+
// 未显式传入 presence 时,按“是否提供了 source”推断;空配置视为缺失。
|
|
50
|
+
const present = sourcePresent
|
|
51
|
+
?? (configuredSource !== undefined && configuredSource !== null);
|
|
52
|
+
|
|
53
|
+
// enabled=false 时没有 sidecar,也不进入介入;但诊断仍按 source 推导。
|
|
54
|
+
const production = !present ? 'off' : source === 'heuristic' ? 'heuristic' : 'model';
|
|
55
|
+
const effectivePlanner = enabled ? production : 'off';
|
|
56
|
+
const effective = !enabled ? 'off' : (diagnosticShadow && present ? 'shadow' : production);
|
|
57
|
+
|
|
58
|
+
const legacyModeValue = legacyMode ?? null;
|
|
59
|
+
const legacyModeIsPresent = legacyModePresent
|
|
60
|
+
?? (legacyModeValue !== null);
|
|
61
|
+
let legacyModeIgnored = false;
|
|
62
|
+
let legacyModeConflict = false;
|
|
63
|
+
const warnings = [];
|
|
64
|
+
|
|
65
|
+
// 只有 immersive 的缺失 source 才值得告警;lightweight 不启动介入。
|
|
66
|
+
if (enabled && !present) warnings.push(MODEL_SOURCE_MISSING);
|
|
67
|
+
|
|
68
|
+
if (legacyModeIsPresent && legacyModeValue !== null) {
|
|
69
|
+
// 显式旧值一律不参与 production;显式 off 也不例外(W1a review B2)。
|
|
70
|
+
legacyModeIgnored = legacyModeValue !== production;
|
|
71
|
+
legacyModeConflict = (legacyModeValue === 'off' && production === 'model')
|
|
72
|
+
|| (legacyModeValue === 'heuristic' && production === 'model')
|
|
73
|
+
|| (legacyModeValue === 'model' && production === 'heuristic');
|
|
74
|
+
if (legacyModeIgnored) warnings.push(LEGACY_INTERVENTION_MODE_IGNORED);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
mode: enabled ? 'immersive' : 'lightweight',
|
|
79
|
+
enabled,
|
|
80
|
+
effective,
|
|
81
|
+
planner: effectivePlanner,
|
|
82
|
+
source,
|
|
83
|
+
sourcePresent: present,
|
|
84
|
+
derivedMode: production,
|
|
85
|
+
legacyModePresent: legacyModeIsPresent && legacyModeValue !== null,
|
|
86
|
+
legacyModeIgnored,
|
|
87
|
+
legacyModeConflict,
|
|
88
|
+
diagnosticShadow: enabled && present && diagnosticShadow === true,
|
|
89
|
+
warnings,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -409,8 +409,90 @@ function traceProvenanceView(provenance) {
|
|
|
409
409
|
};
|
|
410
410
|
}
|
|
411
411
|
|
|
412
|
+
/**
|
|
413
|
+
* 介入决策只透出可解释的枚举/数值/短原因;不返回任何 prompt、正文或 token。
|
|
414
|
+
* 旧 trace 没有 decision → null,读取方按“无策略信息”展示。
|
|
415
|
+
*/
|
|
416
|
+
function traceDecisionView(decision) {
|
|
417
|
+
if (!isObject(decision)) return null;
|
|
418
|
+
const action = ['speak', 'defer', 'silent'].includes(decision.action) ? decision.action : null;
|
|
419
|
+
if (!action) return null;
|
|
420
|
+
const depth = ['gist', 'detail', 'quote'].includes(decision.depth) ? decision.depth : null;
|
|
421
|
+
const view = {
|
|
422
|
+
action,
|
|
423
|
+
depth,
|
|
424
|
+
confidence: traceNumber(decision.confidence),
|
|
425
|
+
reason: traceText(decision.reason, 128),
|
|
426
|
+
policyVersion: traceText(decision.policyVersion, 64),
|
|
427
|
+
// 三态:旧 record / shadow 记录可能没有该字段,不把缺失伪造成“未执行”。
|
|
428
|
+
enforced: typeof decision.enforced === 'boolean' ? decision.enforced : null,
|
|
429
|
+
};
|
|
430
|
+
// 可选:部分 runtime 在 decision 上直接标记影子决策。
|
|
431
|
+
if (typeof decision.shadow === 'boolean') view.shadow = decision.shadow;
|
|
432
|
+
return view;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* 只保留 id 型引用;即使未来写成对象数组,也不把正文/token 带到浏览器。
|
|
437
|
+
*/
|
|
438
|
+
function traceIdList(value, max = 100) {
|
|
439
|
+
if (!Array.isArray(value)) return null;
|
|
440
|
+
return value.slice(0, max).map((entry) => {
|
|
441
|
+
if (typeof entry === 'string') return traceText(entry, 256);
|
|
442
|
+
if (isObject(entry)) return traceText(entry.fragmentId ?? entry.sourceId ?? entry.id, 256);
|
|
443
|
+
return null;
|
|
444
|
+
}).filter(Boolean);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function traceAppliedView(applied) {
|
|
448
|
+
if (!isObject(applied)) return null;
|
|
449
|
+
const action = ['speak', 'defer', 'silent'].includes(applied.action) ? applied.action : null;
|
|
450
|
+
if (!action) return null;
|
|
451
|
+
const depth = ['gist', 'detail', 'quote'].includes(applied.depth) ? applied.depth : null;
|
|
452
|
+
let fragments = null;
|
|
453
|
+
if (typeof applied.fragments === 'number' && Number.isFinite(applied.fragments)) {
|
|
454
|
+
fragments = Math.max(0, Math.trunc(applied.fragments));
|
|
455
|
+
} else if (Array.isArray(applied.fragments)) {
|
|
456
|
+
fragments = traceIdList(applied.fragments);
|
|
457
|
+
}
|
|
458
|
+
const view = { action, depth, fragments };
|
|
459
|
+
// 影子模式的反事实建议:仅枚举/短值,缺失时不输出键,便于上层区分 off baseline。
|
|
460
|
+
if (['speak', 'defer', 'silent'].includes(applied.wouldAction)) view.wouldAction = applied.wouldAction;
|
|
461
|
+
if (['gist', 'detail', 'quote'].includes(applied.wouldDepth)) view.wouldDepth = applied.wouldDepth;
|
|
462
|
+
// runtime 的 suppressed 可能是布尔,也可能是被抑制的 fragmentId 数组;
|
|
463
|
+
// 数组与 fragments 同样只保留 id,不携带正文。shadow 判定不使用该字段。
|
|
464
|
+
if (typeof applied.suppressed === 'boolean') view.suppressed = applied.suppressed;
|
|
465
|
+
else if (Array.isArray(applied.suppressed)) view.suppressed = traceIdList(applied.suppressed);
|
|
466
|
+
return view;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function traceOutcomeView(outcome) {
|
|
470
|
+
if (!isObject(outcome)) return null;
|
|
471
|
+
const kind = traceText(outcome.kind, 64);
|
|
472
|
+
if (!kind) return null;
|
|
473
|
+
return {
|
|
474
|
+
kind,
|
|
475
|
+
at: traceNumber(outcome.at),
|
|
476
|
+
source: traceText(outcome.source, 64),
|
|
477
|
+
confidence: traceNumber(outcome.confidence),
|
|
478
|
+
note: traceText(outcome.note, 200),
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* 旧 trace:schema<2,或 0.3.2/0.3.5 等旧写入方没有 decision/applied。
|
|
484
|
+
* 这类记录不是“当前策略沉默”,调用方必须按“无策略信息”展示。
|
|
485
|
+
*/
|
|
486
|
+
function traceIsLegacy(trace, decision, applied) {
|
|
487
|
+
const schema = traceNumber(trace?.schema);
|
|
488
|
+
if (schema === null || schema < 2) return true;
|
|
489
|
+
return decision === null && applied === null;
|
|
490
|
+
}
|
|
491
|
+
|
|
412
492
|
function traceSummaryView(trace) {
|
|
413
493
|
const feedback = trace?.feedback;
|
|
494
|
+
const decision = traceDecisionView(trace?.decision);
|
|
495
|
+
const applied = traceAppliedView(trace?.applied);
|
|
414
496
|
return {
|
|
415
497
|
id: traceText(trace?.id, 160),
|
|
416
498
|
recallId: traceText(trace?.recallId, 160),
|
|
@@ -434,6 +516,10 @@ function traceSummaryView(trace) {
|
|
|
434
516
|
injected: traceNumber(trace?.counts?.injected),
|
|
435
517
|
tokens: traceNumber(trace?.counts?.tokens),
|
|
436
518
|
},
|
|
519
|
+
legacy: traceIsLegacy(trace, decision, applied),
|
|
520
|
+
decision,
|
|
521
|
+
applied,
|
|
522
|
+
outcome: traceOutcomeView(trace?.outcome),
|
|
437
523
|
timing: { totalMs: traceNumber(trace?.timing?.totalMs) },
|
|
438
524
|
model: {
|
|
439
525
|
query: traceText(trace?.model?.query, 128),
|
|
@@ -485,6 +571,9 @@ function traceDetailView(trace) {
|
|
|
485
571
|
owner: traceOwnerView(fragment?.owner),
|
|
486
572
|
provenance: traceProvenanceView(fragment?.provenance),
|
|
487
573
|
})) : [],
|
|
574
|
+
outcomeHistory: Array.isArray(trace?.outcomeHistory)
|
|
575
|
+
? trace.outcomeHistory.slice(0, 5).map(traceOutcomeView).filter(Boolean)
|
|
576
|
+
: [],
|
|
488
577
|
};
|
|
489
578
|
}
|
|
490
579
|
|
|
@@ -550,6 +639,7 @@ export class MemoryRuntimeSupervisor {
|
|
|
550
639
|
|
|
551
640
|
this.child = null;
|
|
552
641
|
this.childToken = null;
|
|
642
|
+
this.adminToken = null;
|
|
553
643
|
this.childSecrets = [];
|
|
554
644
|
this.managedInstance = null;
|
|
555
645
|
this.dormantLock = null;
|
|
@@ -1085,6 +1175,7 @@ export class MemoryRuntimeSupervisor {
|
|
|
1085
1175
|
degraded: result.state === 'degraded',
|
|
1086
1176
|
capability: null,
|
|
1087
1177
|
model: null,
|
|
1178
|
+
intervention: null,
|
|
1088
1179
|
};
|
|
1089
1180
|
}
|
|
1090
1181
|
const data = result.body?.data ?? result.body ?? {};
|
|
@@ -1124,6 +1215,23 @@ export class MemoryRuntimeSupervisor {
|
|
|
1124
1215
|
}
|
|
1125
1216
|
: null,
|
|
1126
1217
|
model,
|
|
1218
|
+
// 0.4.0 effective intervention:sidecar 已经是唯一推导源,这里只做白名单。
|
|
1219
|
+
intervention: isObject(data.intervention)
|
|
1220
|
+
? {
|
|
1221
|
+
mode: typeof data.intervention.mode === 'string' ? data.intervention.mode : null,
|
|
1222
|
+
planner: typeof data.intervention.planner === 'string' ? data.intervention.planner : null,
|
|
1223
|
+
source: typeof data.intervention.source === 'string' ? data.intervention.source : null,
|
|
1224
|
+
sourcePresent: data.intervention.sourcePresent === true,
|
|
1225
|
+
derivedMode: typeof data.intervention.derivedMode === 'string' ? data.intervention.derivedMode : null,
|
|
1226
|
+
legacyModePresent: data.intervention.legacyModePresent === true,
|
|
1227
|
+
legacyModeIgnored: data.intervention.legacyModeIgnored === true,
|
|
1228
|
+
legacyModeConflict: data.intervention.legacyModeConflict === true,
|
|
1229
|
+
diagnosticShadow: data.intervention.diagnosticShadow === true,
|
|
1230
|
+
warnings: Array.isArray(data.intervention.warnings)
|
|
1231
|
+
? data.intervention.warnings.filter((warning) => typeof warning === 'string')
|
|
1232
|
+
: [],
|
|
1233
|
+
}
|
|
1234
|
+
: null,
|
|
1127
1235
|
};
|
|
1128
1236
|
}
|
|
1129
1237
|
|
|
@@ -1439,6 +1547,128 @@ export class MemoryRuntimeSupervisor {
|
|
|
1439
1547
|
return { statusCode: 200, body: result.body ?? { ok: true, data } };
|
|
1440
1548
|
}
|
|
1441
1549
|
|
|
1550
|
+
/**
|
|
1551
|
+
* Console 侧 outcome 归因(sidecar `POST /recall/:traceId/outcome`)。
|
|
1552
|
+
*
|
|
1553
|
+
* 只允许 MCP admin 代理:请求必须由 MCP admin 写端点进入,并在此附加
|
|
1554
|
+
* supervisor 持有的独立 admin secret;host token 不能冒充 console。
|
|
1555
|
+
* 不接受 note/raw/bindingToken,source 固定为 console。
|
|
1556
|
+
*/
|
|
1557
|
+
async submitRecallOutcome(traceId, outcome) {
|
|
1558
|
+
const id = typeof traceId === 'string' ? traceId.trim() : '';
|
|
1559
|
+
if (!/^tr_[A-Za-z0-9._:-]{1,128}$/.test(id)) {
|
|
1560
|
+
return { statusCode: 400, body: { ok: false, error: 'invalid_trace_id', message: 'trace id 格式非法' } };
|
|
1561
|
+
}
|
|
1562
|
+
if (!isObject(outcome)) {
|
|
1563
|
+
return { statusCode: 400, body: { ok: false, error: 'invalid_outcome', message: 'outcome 必须是对象' } };
|
|
1564
|
+
}
|
|
1565
|
+
const kinds = new Set([
|
|
1566
|
+
'accepted', 'ignored', 'reasked', 'corrected',
|
|
1567
|
+
'dismissed', 'missed', 'too_shallow', 'too_deep',
|
|
1568
|
+
]);
|
|
1569
|
+
const kind = typeof outcome.kind === 'string' ? outcome.kind : '';
|
|
1570
|
+
if (!kinds.has(kind)) {
|
|
1571
|
+
return { statusCode: 400, body: { ok: false, error: 'invalid_outcome', message: 'outcome.kind 非法' } };
|
|
1572
|
+
}
|
|
1573
|
+
const body = { kind, source: 'console' };
|
|
1574
|
+
if (outcome.confidence !== undefined && outcome.confidence !== null) {
|
|
1575
|
+
if (typeof outcome.confidence !== 'number' || !Number.isFinite(outcome.confidence)
|
|
1576
|
+
|| outcome.confidence < 0 || outcome.confidence > 1) {
|
|
1577
|
+
return { statusCode: 400, body: { ok: false, error: 'invalid_outcome', message: 'outcome.confidence 必须是 0..1 的数字' } };
|
|
1578
|
+
}
|
|
1579
|
+
body.confidence = outcome.confidence;
|
|
1580
|
+
}
|
|
1581
|
+
// 独立 admin secret 只在 supervisor 启动 managed child 时存在;
|
|
1582
|
+
// attached/external sidecar 或旧 MCP 没有它时明确 fail closed。
|
|
1583
|
+
if (typeof this.adminToken !== 'string' || this.adminToken.length === 0) {
|
|
1584
|
+
return {
|
|
1585
|
+
statusCode: 409,
|
|
1586
|
+
body: {
|
|
1587
|
+
ok: false,
|
|
1588
|
+
error: 'admin_channel_unavailable',
|
|
1589
|
+
message: 'console outcome 通道未启用',
|
|
1590
|
+
},
|
|
1591
|
+
};
|
|
1592
|
+
}
|
|
1593
|
+
const result = await this.#proxyHostApi(`recall/${encodeURIComponent(id)}/outcome`, {
|
|
1594
|
+
method: 'POST',
|
|
1595
|
+
body,
|
|
1596
|
+
admin: true,
|
|
1597
|
+
});
|
|
1598
|
+
if (!result.available) {
|
|
1599
|
+
return {
|
|
1600
|
+
statusCode: result.disabled === true ? 409 : 503,
|
|
1601
|
+
body: {
|
|
1602
|
+
ok: false,
|
|
1603
|
+
error: result.disabled === true ? 'memory_runtime_disabled' : 'sidecar_unavailable',
|
|
1604
|
+
enabled: result.enabled,
|
|
1605
|
+
state: result.state,
|
|
1606
|
+
message: result.disabled === true
|
|
1607
|
+
? '记忆运行时未启用'
|
|
1608
|
+
: 'Memory Runtime sidecar 未就绪',
|
|
1609
|
+
...(result.error ? { details: result.error } : {}),
|
|
1610
|
+
},
|
|
1611
|
+
};
|
|
1612
|
+
}
|
|
1613
|
+
const data = result.body?.data ?? result.body ?? null;
|
|
1614
|
+
if (!result.ok) {
|
|
1615
|
+
return {
|
|
1616
|
+
statusCode: result.statusCode ?? 502,
|
|
1617
|
+
body: {
|
|
1618
|
+
ok: false,
|
|
1619
|
+
error: data?.error?.code ?? result.body?.error?.code ?? 'outcome_failed',
|
|
1620
|
+
message: proxyErrorMessage(data, proxyErrorMessage(result.body, '召回结果上报失败')),
|
|
1621
|
+
},
|
|
1622
|
+
};
|
|
1623
|
+
}
|
|
1624
|
+
return { statusCode: 200, body: result.body ?? { ok: true, data } };
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
/**
|
|
1628
|
+
* 重置本机介入策略状态(sidecar `POST /intervention/reset`)。
|
|
1629
|
+
*
|
|
1630
|
+
* 与 feedback 共用 host token 代理;轻量模式/未就绪返回明确 disabled/503,
|
|
1631
|
+
* sidecar 报错或 `ok:false` 原样失败,不伪造成功。
|
|
1632
|
+
*/
|
|
1633
|
+
async resetInterventionPolicy() {
|
|
1634
|
+
const result = await this.#proxyHostApi('intervention/reset', {
|
|
1635
|
+
method: 'POST',
|
|
1636
|
+
body: {},
|
|
1637
|
+
timeoutMs: 5000,
|
|
1638
|
+
});
|
|
1639
|
+
if (!result.available) {
|
|
1640
|
+
return {
|
|
1641
|
+
statusCode: result.disabled === true ? 409 : 503,
|
|
1642
|
+
body: {
|
|
1643
|
+
ok: false,
|
|
1644
|
+
error: result.disabled === true ? 'memory_runtime_disabled' : 'sidecar_unavailable',
|
|
1645
|
+
enabled: result.enabled,
|
|
1646
|
+
state: result.state,
|
|
1647
|
+
message: result.disabled === true
|
|
1648
|
+
? '记忆运行时未启用'
|
|
1649
|
+
: 'Memory Runtime sidecar 未就绪',
|
|
1650
|
+
...(result.error ? { details: result.error } : {}),
|
|
1651
|
+
},
|
|
1652
|
+
};
|
|
1653
|
+
}
|
|
1654
|
+
const envelope = isObject(result.body) ? result.body : {};
|
|
1655
|
+
const data = envelope.data ?? result.body ?? null;
|
|
1656
|
+
if (!result.ok || envelope.ok === false) {
|
|
1657
|
+
return {
|
|
1658
|
+
statusCode: result.ok ? 502 : (result.statusCode ?? 502),
|
|
1659
|
+
body: {
|
|
1660
|
+
ok: false,
|
|
1661
|
+
error: data?.error?.code ?? envelope.error?.code ?? 'policy_reset_failed',
|
|
1662
|
+
message: proxyErrorMessage(data, proxyErrorMessage(envelope, '介入策略重置失败')),
|
|
1663
|
+
},
|
|
1664
|
+
};
|
|
1665
|
+
}
|
|
1666
|
+
return {
|
|
1667
|
+
statusCode: 200,
|
|
1668
|
+
body: envelope.ok === undefined ? { ok: true, data } : envelope,
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1442
1672
|
async #resolveHostToken(discovery) {
|
|
1443
1673
|
const fromEnv = stringOrNull(this.env?.MEBULAR_MEMORY_RUNTIME_TOKEN);
|
|
1444
1674
|
if (fromEnv) return { token: fromEnv, source: 'override', refreshable: false };
|
|
@@ -1557,7 +1787,12 @@ export class MemoryRuntimeSupervisor {
|
|
|
1557
1787
|
return sameDiscoveryInstance(await readDiscovery(this.paths.discoveryPath), discovery);
|
|
1558
1788
|
}
|
|
1559
1789
|
|
|
1560
|
-
async #proxyHostApi(pathname, {
|
|
1790
|
+
async #proxyHostApi(pathname, {
|
|
1791
|
+
method = 'GET',
|
|
1792
|
+
body = null,
|
|
1793
|
+
timeoutMs = 2500,
|
|
1794
|
+
admin = false,
|
|
1795
|
+
} = {}) {
|
|
1561
1796
|
await this.status().catch(() => undefined);
|
|
1562
1797
|
if (!this.isEnabled()) {
|
|
1563
1798
|
return {
|
|
@@ -1620,6 +1855,11 @@ export class MemoryRuntimeSupervisor {
|
|
|
1620
1855
|
accept: 'application/json',
|
|
1621
1856
|
authorization: `Bearer ${token}`,
|
|
1622
1857
|
'x-request-id': requestId,
|
|
1858
|
+
// W4 B1:console outcome 只在 MCP 进程内附加独立 admin secret;
|
|
1859
|
+
// feedback/reset 等普通代理不发送该 header。
|
|
1860
|
+
...(admin && typeof this.adminToken === 'string' && this.adminToken.length > 0
|
|
1861
|
+
? { 'x-mebular-admin-token': this.adminToken }
|
|
1862
|
+
: {}),
|
|
1623
1863
|
};
|
|
1624
1864
|
const init = { method, headers, signal: controller.signal };
|
|
1625
1865
|
if (body !== null && body !== undefined) {
|
|
@@ -1951,7 +2191,11 @@ export class MemoryRuntimeSupervisor {
|
|
|
1951
2191
|
|
|
1952
2192
|
const hostToken = stringOrNull(this.env?.MEBULAR_MEMORY_RUNTIME_TOKEN)
|
|
1953
2193
|
?? randomBytes(32).toString('base64url');
|
|
1954
|
-
|
|
2194
|
+
// W4:独立 admin secret 只注入 sidecar child;宿主 adapter 不持有,
|
|
2195
|
+
// discovery/日志/状态都不暴露。console outcome 只走这个通道。
|
|
2196
|
+
const adminToken = randomBytes(32).toString('base64url');
|
|
2197
|
+
this.adminToken = adminToken;
|
|
2198
|
+
this.childSecrets = [token, hostToken, adminToken];
|
|
1955
2199
|
const spec = commandSpec(resolved.command);
|
|
1956
2200
|
const args = [
|
|
1957
2201
|
...spec.args,
|
|
@@ -1965,6 +2209,7 @@ export class MemoryRuntimeSupervisor {
|
|
|
1965
2209
|
MEBULAR_MEMORY_RUNTIME_HOME: this.paths.dataDir,
|
|
1966
2210
|
MEBULAR_CAPABILITY_TOKEN: token,
|
|
1967
2211
|
MEBULAR_MEMORY_RUNTIME_TOKEN: hostToken,
|
|
2212
|
+
MEBULAR_MEMORY_RUNTIME_ADMIN_TOKEN: adminToken,
|
|
1968
2213
|
...(stringOrNull(this.getMcpUrl()) ? { MEBULAR_MCP_URL: stringOrNull(this.getMcpUrl()) } : {}),
|
|
1969
2214
|
};
|
|
1970
2215
|
|
package/src/provision.mjs
CHANGED
|
@@ -180,7 +180,14 @@ export function normalizeExplicitDeviceId(value) {
|
|
|
180
180
|
return /^device-+/i.test(raw) ? deriveDeviceId(raw) : (sanitize(raw) || null);
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
-
export function provisionStatus({
|
|
183
|
+
export function provisionStatus({
|
|
184
|
+
home,
|
|
185
|
+
mcpPort,
|
|
186
|
+
phase = 'UNINITIALIZED',
|
|
187
|
+
provisioned = false,
|
|
188
|
+
firstRun = null,
|
|
189
|
+
firstRunWarning = null,
|
|
190
|
+
} = {}) {
|
|
184
191
|
const name = defaultDeviceName();
|
|
185
192
|
const base = {
|
|
186
193
|
ok: true,
|
|
@@ -195,6 +202,9 @@ export function provisionStatus({ home, mcpPort, phase = 'UNINITIALIZED', provis
|
|
|
195
202
|
// 兼容旧控制台字段:P1 起初始化不再需要重启。
|
|
196
203
|
restartRequired: false,
|
|
197
204
|
restarting: false,
|
|
205
|
+
// 0.4.0 W1b:全新空 home 的一次性首启披露状态;已有用户/完成后为 null。
|
|
206
|
+
...(firstRun ? { firstRun } : {}),
|
|
207
|
+
...(firstRunWarning ? { firstRunWarning } : {}),
|
|
198
208
|
};
|
|
199
209
|
if (phase === 'INITIALIZING') {
|
|
200
210
|
return {
|