@octopi-agent/engine 0.86.38 → 0.90.11

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.
Files changed (83) hide show
  1. package/dist/config-schema/agent.js +12 -5
  2. package/dist/config-schema/context.d.ts +1 -1
  3. package/dist/config-schema/engine.d.ts +4 -4
  4. package/dist/config-schema/governance.js +3 -2
  5. package/dist/config-schema/models.d.ts +2 -2
  6. package/dist/config-schema/shared.d.ts +3 -0
  7. package/dist/config-schema/shared.js +12 -0
  8. package/dist/config-schema/substrate.d.ts +1 -1
  9. package/dist/config.d.ts +31 -9
  10. package/dist/config.js +66 -74
  11. package/dist/harness/agent/builder.d.ts +3 -2
  12. package/dist/harness/agent/builder.js +11 -7
  13. package/dist/harness/agent/config-bridge.js +4 -1
  14. package/dist/harness/agent/tool-purpose.d.ts +9 -1
  15. package/dist/harness/agent/tool-purpose.js +11 -2
  16. package/dist/harness/agent/wire-agent-builder.js +9 -2
  17. package/dist/harness/collaboration/autonomous-subsystem/boundary/validator.d.ts +11 -1
  18. package/dist/harness/collaboration/autonomous-subsystem/boundary/validator.js +11 -1
  19. package/dist/harness/collaboration/autonomous-subsystem/loader.d.ts +13 -0
  20. package/dist/harness/collaboration/autonomous-subsystem/loader.js +16 -1
  21. package/dist/harness/collaboration/autonomous-subsystem/runtime.d.ts +4 -0
  22. package/dist/harness/collaboration/autonomous-subsystem/runtime.js +3 -1
  23. package/dist/harness/context/default-context-engine.js +4 -4
  24. package/dist/harness/context/layer-types.d.ts +36 -0
  25. package/dist/harness/context/layer-types.js +38 -0
  26. package/dist/harness/context/root-instructions.js +1 -1
  27. package/dist/harness/context/system-prompt-assembler.js +10 -4
  28. package/dist/harness/context/tool-result-aging.js +2 -2
  29. package/dist/harness/context/turn-envelope.d.ts +1 -1
  30. package/dist/harness/context/turn-envelope.js +1 -1
  31. package/dist/harness/extension/execution-environment/effect-policy.d.ts +11 -1
  32. package/dist/harness/extension/execution-environment/effect-policy.js +24 -0
  33. package/dist/harness/extension/execution-environment/index.d.ts +1 -1
  34. package/dist/harness/extension/execution-environment/index.js +1 -1
  35. package/dist/harness/extension/execution-environment/isolation.js +5 -1
  36. package/dist/harness/extension/execution-environment/sandbox.js +36 -8
  37. package/dist/harness/extension/plugin-ecosystem/tools/allowlisted-run.d.ts +6 -0
  38. package/dist/harness/extension/plugin-ecosystem/tools/allowlisted-run.js +17 -0
  39. package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +10 -17
  40. package/dist/harness/extension/plugin-ecosystem/tools/git-runner.d.ts +31 -0
  41. package/dist/harness/extension/plugin-ecosystem/tools/git-runner.js +104 -0
  42. package/dist/harness/extension/plugin-ecosystem/tools/git-status-snapshot.d.ts +111 -0
  43. package/dist/harness/extension/plugin-ecosystem/tools/git-status-snapshot.js +197 -0
  44. package/dist/harness/extension/plugin-ecosystem/tools/git-tools.d.ts +8 -8
  45. package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +9 -100
  46. package/dist/harness/extension/plugin-ecosystem/tools/index.d.ts +4 -0
  47. package/dist/harness/extension/plugin-ecosystem/tools/index.js +2 -0
  48. package/dist/harness/index.d.ts +2 -0
  49. package/dist/harness/index.js +1 -0
  50. package/dist/harness/knowledge/file-identity.d.ts +8 -0
  51. package/dist/harness/knowledge/file-identity.js +26 -1
  52. package/dist/harness/knowledge/grounding.js +5 -5
  53. package/dist/harness/knowledge/index-store.d.ts +31 -4
  54. package/dist/harness/knowledge/index-store.js +121 -39
  55. package/dist/harness/knowledge/ingest.d.ts +115 -115
  56. package/dist/harness/knowledge/ingest.js +150 -150
  57. package/dist/harness/knowledge/knowledge-serve-child.js +1 -1
  58. package/dist/harness/knowledge/purge.js +4 -1
  59. package/dist/harness/memory/backfill-coverage.js +3 -1
  60. package/dist/harness/observability/observer/hub.js +30 -1
  61. package/dist/harness/observability/observer/types.d.ts +23 -0
  62. package/dist/harness/run/runner.d.ts +16 -0
  63. package/dist/harness/run/runner.js +139 -26
  64. package/dist/harness/session/history/port.js +15 -5
  65. package/dist/harness/session/in-memory-store.d.ts +5 -0
  66. package/dist/harness/session/in-memory-store.js +33 -2
  67. package/dist/harness/session/title.js +25 -9
  68. package/dist/harness/shared/events/agent-event-map.d.ts +26 -0
  69. package/dist/harness/shared/events/agent-event-map.js +2 -0
  70. package/dist/index.d.ts +4 -2
  71. package/dist/index.js +3 -2
  72. package/dist/integration/observability/run-telemetry.d.ts +8 -0
  73. package/dist/integration/observability/run-telemetry.js +31 -8
  74. package/dist/integration/observability/trace-collector.js +26 -4
  75. package/dist/integration/observability/trace-events.d.ts +1 -0
  76. package/dist/integration/observability/trace-events.js +2 -0
  77. package/dist/integration/observability/trace-logger.js +27 -17
  78. package/dist/integration/storage/jsonl.d.ts +19 -1
  79. package/dist/integration/storage/jsonl.js +96 -3
  80. package/dist/paths.d.ts +19 -0
  81. package/dist/paths.js +38 -1
  82. package/dist/subsystems/safety-guard/config.yaml +3 -1
  83. package/package.json +2 -2
@@ -30,7 +30,7 @@ async function main() {
30
30
  throw new Error('knowledge-serve-child: missing boot file path');
31
31
  }
32
32
  const raw = await readFile(bootPath, 'utf8');
33
- const boot = JSON.parse(raw.replace(/^/, ''));
33
+ const boot = JSON.parse(raw.replace(/^\uFEFF/, ''));
34
34
  try {
35
35
  await unlink(bootPath);
36
36
  }
@@ -16,8 +16,11 @@ export class KnowledgePurger {
16
16
  * 单文件 purge(合规 / 用户要求抹掉)
17
17
  */
18
18
  purgePath(sourceId, path) {
19
- this.index.purgePath(sourceId, path);
19
+ const matched = this.index.purgePath(sourceId, path);
20
20
  this.hits.clearForPath(sourceId, path);
21
+ // 别名形态命中的存储字面也要清痕(query 字面可能与存储字面不同);clear 幂等
22
+ for (const stored of matched)
23
+ this.hits.clearForPath(sourceId, stored);
21
24
  const describeStale = this.markDescribeStale(sourceId);
22
25
  return { sourceId, path, purged: true, describeStale };
23
26
  }
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import { createHash } from 'node:crypto';
9
9
  import { getTextContent } from '@octopi-agent/core/types.js';
10
+ import { isHumanSpeech } from '../context/layer-types.js';
10
11
  export const DEFAULT_PREFILTER = {
11
12
  minUserTurns: 2,
12
13
  minTotalChars: 200,
@@ -76,7 +77,8 @@ export function measureSessionDensity(messages) {
76
77
  const ts = typeof m.timestamp === 'number' ? m.timestamp : 0;
77
78
  if (ts > lastTs)
78
79
  lastTs = ts;
79
- if (m.role === 'user')
80
+ // 引擎载体(turn_end/runStopped…)也是 role=user,不计真实用户轮
81
+ if (isHumanSpeech(m))
80
82
  userTurns++;
81
83
  if (m.role === 'assistant')
82
84
  assistantTurns++;
@@ -67,6 +67,7 @@ const TIMELINE_TYPES = new Set([
67
67
  'sensitive_data.detected',
68
68
  'security.blocked',
69
69
  'security.behavior_blocked',
70
+ 'run.timing',
70
71
  ]);
71
72
  let runSeq = 0;
72
73
  /** Hub 兜底:scope 未带 runId 时使用(生产路径应由 Runner/createRunId 提供) */
@@ -449,7 +450,19 @@ export class ObserverHub {
449
450
  record.timeline.push(node);
450
451
  const max = this.config.retention.timelineEvents;
451
452
  if (record.timeline.length > max) {
452
- record.timeline.splice(0, record.timeline.length - max);
453
+ // 淘汰优先保留 run.timing(wait/build/run 分段是 run 级指标,不能随事件淘汰丢)
454
+ const excess = record.timeline.length - max;
455
+ let removed = 0;
456
+ for (let i = 0; i < record.timeline.length && removed < excess; i++) {
457
+ if (record.timeline[i].type !== 'run.timing') {
458
+ record.timeline.splice(i, 1);
459
+ i--;
460
+ removed++;
461
+ }
462
+ }
463
+ // 全是 run.timing 时按老规矩从头裁
464
+ if (removed < excess)
465
+ record.timeline.splice(0, excess - removed);
453
466
  }
454
467
  }
455
468
  const life = record.lifecycle;
@@ -915,6 +928,21 @@ function stripLayerPayload(snapshot, payload) {
915
928
  }
916
929
  function toTimelineNode(event) {
917
930
  const data = (event.data ?? {});
931
+ // F3 计时段:turn.end 带 llmTiming(LLM 调用计时);run.timing 带 wait/build/run_end 分段
932
+ const timing = data.llmTiming ??
933
+ (event.type === 'run.timing'
934
+ ? {
935
+ stage: data.stage,
936
+ waitMs: data.waitMs,
937
+ buildMs: data.buildMs,
938
+ durMs: data.durMs,
939
+ loopEnd: data.loopEnd,
940
+ stopReason: data.stopReason,
941
+ msgs: data.msgs,
942
+ sysChars: data.sysChars,
943
+ groundingTokens: data.groundingTokens,
944
+ }
945
+ : undefined);
918
946
  return {
919
947
  type: event.type,
920
948
  timestamp: event.timestamp,
@@ -925,5 +953,6 @@ function toTimelineNode(event) {
925
953
  durationMs: typeof data.durationMs === 'number' ? data.durationMs : undefined,
926
954
  reason: (data.reason ?? data.fallbackError),
927
955
  usage: data.usage,
956
+ timing,
928
957
  };
929
958
  }
@@ -264,6 +264,29 @@ export interface RunTimelineEventView {
264
264
  durationMs?: number;
265
265
  reason?: string;
266
266
  usage?: import('@octopi-agent/core/types/turn.js').TokenUsage;
267
+ /** F3 计时段(turn.end 的 LLM 计时 / run.timing 的 wait/build/run_end 分段) */
268
+ timing?: {
269
+ stage?: string;
270
+ waitMs?: number;
271
+ buildMs?: number;
272
+ durMs?: number;
273
+ /** LLM 调用总耗时(turn.end.llmTiming) */
274
+ durationMs?: number;
275
+ /** LLM 调用标识(可关联 model.call.end 行) */
276
+ callId?: string;
277
+ ttftMs?: number;
278
+ reqChars?: number;
279
+ toolChars?: number;
280
+ outChars?: number;
281
+ finishReason?: string;
282
+ /** run.timing=run_end 附带 */
283
+ loopEnd?: string;
284
+ stopReason?: string;
285
+ /** run.timing=context_ready 附带(sysChars 为稳定前缀规模,E8 后不含易变快照) */
286
+ msgs?: number;
287
+ sysChars?: number;
288
+ groundingTokens?: number;
289
+ };
267
290
  }
268
291
  export interface RunLifecycleView {
269
292
  startedAt?: number;
@@ -194,6 +194,8 @@ export declare class SessionAwareRunner {
194
194
  /** Effective effectPolicy.mode (for Runtime injection / UI) */
195
195
  private effectMode;
196
196
  private effectCommitManual;
197
+ /** store 缺 syncMessages 通道时只 warn 一次 */
198
+ private warnedMissingSync;
197
199
  private effectProcessLimits;
198
200
  private effectNetwork;
199
201
  private effectRequireJail;
@@ -426,6 +428,20 @@ export declare class SessionAwareRunner {
426
428
  private bakeTurnStart;
427
429
  /** 追加 turn 闭标(write-once;同 id 重复 → throw) */
428
430
  private appendTurnEnd;
431
+ /**
432
+ * Run 进行中转录增量落盘:把 durable 口径的历史对齐到存储。
433
+ *
434
+ * 口径与收尾合并一致(ephemeral 运行期消息不落历史);调用方契约:
435
+ * turn_start 定形之后才可同步(bake 原地改写 user 消息)。尽力而为——
436
+ * 失败不中断 Run(收尾整包 save 是权威写),store 无 syncMessages 时跳过。
437
+ *
438
+ * @param sessionId - 会话 id
439
+ * @param agentId - 本 Run 身份(assistant/tool 归因,首次同步前补齐)
440
+ * @param keptPrefix - 上下文租约点之前的 Discourse 前缀(与收尾 keptPrefix 同源)
441
+ * @param runMessages - 本 Run 视图消息(含 ephemeral,此处过滤)
442
+ * @returns 是否成功对齐(false 时调用方不得推进已同步计数,待下个事件重试)
443
+ */
444
+ private persistRunMessages;
429
445
  /** 旧路径:persona + injectedContext 字符串拼接;结果返回给 runContext,不写共享 Agent */
430
446
  private concatSystemPrompt;
431
447
  /**
@@ -14,7 +14,7 @@ import { getTextContent } from '@octopi-agent/core/types.js';
14
14
  import { tightenSessionRights } from '../governance/session-acl/rights.js';
15
15
  import { createSessionStateMachine } from '../session/state-machine.js';
16
16
  import { HeuristicTokenEstimator } from '../context/index.js';
17
- import { extractLayerQuery, isEngineCarrierSource } from '../context/layer-types.js';
17
+ import { extractLayerQuery } from '../context/layer-types.js';
18
18
  import { formatTurnEndContent, formatTurnId, formatTurnStartContent, formatTurnContextXml, isTurnStartContent, joinSlotXml, parseTurnIdFromContent, } from '../context/turn-envelope.js';
19
19
  import { splitRuntimeEnvironmentInjection, withRuntimeEnvironmentInjection } from '../context/runtime-datetime.js';
20
20
  import { loadRootInstructionsInjection } from '../context/root-instructions.js';
@@ -104,6 +104,7 @@ export function adaptLoopEvent(event, meta, state) {
104
104
  usage: event.usage,
105
105
  truncated: event.truncated,
106
106
  error: event.error,
107
+ llmTiming: event.llmTiming,
107
108
  },
108
109
  };
109
110
  case 'llm_stream_delta':
@@ -237,6 +238,8 @@ export class SessionAwareRunner {
237
238
  /** Effective effectPolicy.mode (for Runtime injection / UI) */
238
239
  effectMode = 'workspace';
239
240
  effectCommitManual = false;
241
+ /** store 缺 syncMessages 通道时只 warn 一次 */
242
+ warnedMissingSync = false;
240
243
  effectProcessLimits = true;
241
244
  effectNetwork;
242
245
  effectRequireJail;
@@ -420,9 +423,8 @@ export class SessionAwareRunner {
420
423
  async *handle(sessionId, input, runConfig, signal) {
421
424
  /** 本轮文件 undo(L3);中止保留,其余出口 commit */
422
425
  let turnFileUndo;
423
- // [timing probe] 长 session 首 run 延迟定位(诊断后移除或转正)
426
+ // run 收口计时(`[timing]` 探针转正,F3):wait/build/run_end 走 eventSink → trace
424
427
  const timingRunEnter = Date.now();
425
- console.log(`[timing] stage=run_enter ms=${timingRunEnter} session=${sessionId}`);
426
428
  // 1. 并发门控(如果配置了 SessionGate);排队期间响应 abort
427
429
  const gate = this.config.sessionGate;
428
430
  let gateRelease;
@@ -456,8 +458,20 @@ export class SessionAwareRunner {
456
458
  gateRelease?.();
457
459
  return;
458
460
  }
459
- // [timing probe] 排队/等锁耗时 = wait
460
- console.log(`[timing] stage=run_start ms=${Date.now()} session=${sessionId} wait=${Date.now() - timingRunEnter}`);
461
+ // run.timing 先落 trace(eventSink 时序保真);Hub 侧投递推迟到 recordRunStart 之后——
462
+ // 否则 run_start/context_ready 早于 run 记录创建,会被 ingestEvent 归到上一轮 run
463
+ const pendingObservedTimings = [];
464
+ const runStartTiming = {
465
+ type: 'run.timing',
466
+ timestamp: Date.now(),
467
+ sessionId,
468
+ agentId: runConfig.agentId ?? 'default',
469
+ data: { stage: 'run_start', waitMs: Date.now() - timingRunEnter },
470
+ };
471
+ this.config.eventSink?.(runStartTiming, { sessionId, agentId: runConfig.agentId ?? 'default' });
472
+ pendingObservedTimings.push(runStartTiming);
473
+ // buildMs 从拿锁后起算(含 wait 会双重计时;F3-d 耗时区按分段展示)
474
+ const runStartTs = Date.now();
461
475
  const _agentId = runConfig.agentId ?? 'default';
462
476
  /** Observer 收口状态(finally 统一关闭,含异常路径) */
463
477
  let observerRunId = '';
@@ -625,7 +639,7 @@ export class SessionAwareRunner {
625
639
  // P3:effectPolicy → L2 path containment + session 临时 root
626
640
  // 必须在 Runtime 注入之前,system prompt 才能带上 allowRoots / effectMode
627
641
  {
628
- const { resolveEffectiveEffectPolicy, readSessionEffectOverrides, mergeTransactionRoots } = await import('../extension/execution-environment/effect-policy.js');
642
+ const { resolveEffectiveEffectPolicy, readSessionEffectOverrides, mergeTransactionRoots, requiresJail } = await import('../extension/execution-environment/effect-policy.js');
629
643
  const { probeProcessIsolation } = await import('../extension/execution-environment/process-isolation.js');
630
644
  const isolation = await probeProcessIsolation();
631
645
  const eff = resolveEffectiveEffectPolicy(this.config.effectPolicy ?? {}, this.config.agentWorkspace ?? effectiveRunConfig.cwd ?? undefined, isolation);
@@ -639,10 +653,9 @@ export class SessionAwareRunner {
639
653
  this.effectShellEscape = eff.shellEscape;
640
654
  this.effectShellJournal = eff.shellJournal;
641
655
  this.effectNetwork = eff.network;
642
- this.effectRequireJail = eff.requestedMode === 'untrusted' && eff.mode === 'untrusted';
656
+ this.effectRequireJail = requiresJail(eff);
643
657
  this.effectRequireCredentialBindings =
644
- this.config.requireCredentialBindings === true ||
645
- (eff.requestedMode === 'untrusted' && eff.mode === 'untrusted');
658
+ this.config.requireCredentialBindings === true || requiresJail(eff);
646
659
  this.effectHomeWrite = eff.mode === 'project' || eff.mode === 'host';
647
660
  this.effectShellJournalLimit = eff.shellJournalLimit;
648
661
  this.effectUndoLimit = eff.undoLimit;
@@ -852,8 +865,25 @@ export class SessionAwareRunner {
852
865
  pendingGrounding = null;
853
866
  }
854
867
  }
855
- // [timing probe] system prompt 装配 + grounding 全部就绪;build = 组装耗时
856
- console.log(`[timing] stage=context_ready ms=${Date.now()} session=${sessionId} build=${Date.now() - timingRunEnter} msgs=${session.messages.length} sysChars=${(runContext.systemPrompt ?? '').length} grounding=${pendingGrounding ? pendingGrounding.tokens : 0}`);
868
+ // system prompt 装配 + grounding 全部就绪;buildMs = 组装耗时。
869
+ // 口径注记(arch/b0-cache-stable-context.md §5):sysChars 是稳定前缀规模,
870
+ // E8 后不含易变快照——勿与旧 `[timing]` 基线直接对比。
871
+ const contextReadyTiming = {
872
+ type: 'run.timing',
873
+ timestamp: Date.now(),
874
+ sessionId,
875
+ agentId: _agentId,
876
+ data: {
877
+ stage: 'context_ready',
878
+ buildMs: Date.now() - runStartTs,
879
+ msgs: session.messages.length,
880
+ sysChars: (runContext.systemPrompt ?? '').length,
881
+ groundingTokens: pendingGrounding ? pendingGrounding.tokens : 0,
882
+ },
883
+ };
884
+ this.config.eventSink?.(contextReadyTiming, { sessionId, agentId: _agentId });
885
+ // Hub 投递同样推迟到 recordRunStart 之后(见 run_start 处注释)
886
+ pendingObservedTimings.push(contextReadyTiming);
857
887
  // 更新 harness 的 sessionId/agentId(checkpoint 回退;权威身份在 RunScope ALS)
858
888
  this.harness.sessionId = sessionId;
859
889
  this.harness.agentId = effectiveRunConfig.agentId ?? 'default';
@@ -917,7 +947,9 @@ export class SessionAwareRunner {
917
947
  memoryEffects,
918
948
  undoRoots,
919
949
  networkPolicy: this.effectNetwork,
920
- requireCredentialBindings: this.effectMode === 'untrusted' || this.effectRequireCredentialBindings,
950
+ // 单源:effectRequireCredentialBindings 已在解析时含 requiresJail(eff)(= untrusted),
951
+ // 此处再判 effectMode 是等价冗余,已收编。
952
+ requireCredentialBindings: this.effectRequireCredentialBindings,
921
953
  requireJail: this.effectRequireJail,
922
954
  homeWrite: this.effectHomeWrite,
923
955
  noteSecurityDegraded: (reason) => {
@@ -1034,6 +1066,11 @@ export class SessionAwareRunner {
1034
1066
  runScope.toolRuntime.messages = baseMsgs;
1035
1067
  }
1036
1068
  }
1069
+ // Run 进行中转录增量落盘:turn_start 定形后立即同步一次(此后新消息
1070
+ // 定形随 loop 事件增量同步)。写失败不中断 Run(收尾 save 是权威写),
1071
+ // 失败不推进已同步计数,下个事件重试。
1072
+ const firstSynced = await this.persistRunMessages(sessionId, _agentId, contextLeasePrefix, runContext.messages);
1073
+ let syncedMessageCount = firstSynced ? runContext.messages.length : 0;
1037
1074
  // Observer 通道:Run 现场快照(摘要进事件;全文由 Hub/REST 提供)
1038
1075
  if (this._observerHub?.isEnabled()) {
1039
1076
  observerRunId =
@@ -1050,6 +1087,10 @@ export class SessionAwareRunner {
1050
1087
  messages: session.messages,
1051
1088
  }) || runId;
1052
1089
  if (observerRunId) {
1090
+ // run 记录已建:补投 run_start/context_ready 到 Hub(此前投会被归到上一轮 run)
1091
+ for (const pending of pendingObservedTimings.splice(0)) {
1092
+ this.emitObserved(pending);
1093
+ }
1053
1094
  const hubSnapshot = this._observerHub.getRunById(observerRunId);
1054
1095
  if (hubSnapshot) {
1055
1096
  this.emitObserved({
@@ -1103,6 +1144,19 @@ export class SessionAwareRunner {
1103
1144
  runScope,
1104
1145
  };
1105
1146
  for await (const loopEvent of this.agent.run(signal, undefined, agentRunOptions)) {
1147
+ // 消息数组增长 = 有新消息定形/入列(流式 delta 不 push 消息,不触发)。
1148
+ // 覆盖 assistant/tool/followUp/steering 全部 push 点:push 必发生在
1149
+ // 某个事件 yield 之前,下一迭代开头即可看到。
1150
+ if (runContext.messages.length !== syncedMessageCount) {
1151
+ if (await this.persistRunMessages(sessionId, _agentId, contextLeasePrefix, runContext.messages)) {
1152
+ syncedMessageCount = runContext.messages.length;
1153
+ }
1154
+ }
1155
+ else if (loopEvent.type === 'turn_end') {
1156
+ // 漂移兜底:turn 边界无条件对齐一次(旁路写者整包回退后即使暂无
1157
+ // 新消息也能自愈;无漂移时幂等零写)
1158
+ await this.persistRunMessages(sessionId, _agentId, contextLeasePrefix, runContext.messages);
1159
+ }
1106
1160
  if (loopEvent.type === 'agent_end' && loopEvent.reason === 'aborted') {
1107
1161
  runAborted = true;
1108
1162
  }
@@ -1312,15 +1366,10 @@ export class SessionAwareRunner {
1312
1366
  turnFileUndo = undefined;
1313
1367
  }
1314
1368
  if (abortedMidTurn) {
1315
- let userIdx = -1;
1316
- for (let i = session.messages.length - 1; i >= 0; i--) {
1317
- const m = session.messages[i];
1318
- // 与 isMidTurn / extractLayerQuery 同口径:引擎载体(含 turnEnd)不是真实 user
1319
- if (m.role === 'user' && !isEngineCarrierSource(m.metadata?.source)) {
1320
- userIdx = i;
1321
- break;
1322
- }
1323
- }
1369
+ // 中止轮的用户气泡 = 本轮 input 对象本身(push 于步骤 5,bake 之前就已在列)。
1370
+ // 按对象身份定位而非形状扫描:abort 落在 bake 之前的窗口(上下文装配含 LLM intent)
1371
+ // 时 input 尚未定形 turn_start,形状扫描会误锚到上一轮的开轮信封,截断掉整轮历史
1372
+ const userIdx = session.messages.lastIndexOf(input);
1324
1373
  if (userIdx >= 0) {
1325
1374
  session.metadata = {
1326
1375
  ...session.metadata,
@@ -1418,11 +1467,34 @@ export class SessionAwareRunner {
1418
1467
  throw err;
1419
1468
  }
1420
1469
  finally {
1421
- // [timing probe] run 收口必须留痕:本次事故里「run 结束原因」零日志,
1422
- // 只能靠反推落盘消息定位。reason = 观测口径,stopReason = 停止闸门。
1423
- console.log(`[timing] stage=run_end ms=${Date.now()} session=${sessionId}` +
1424
- ` dur=${Date.now() - timingRunEnter}` +
1425
- ` reason=${observerEndReason ?? (signal?.aborted ? 'aborted' : 'unknown')}` +
1470
+ // run 收口:reason = 观测口径,stopReason = 停止闸门。
1471
+ // 计时进 trace(run.timing);console 保留一条完整生命周期日志——前次事故
1472
+ // 「run 结束原因零日志、只能靠反推落盘消息定位」,该留痕不随探针删除。
1473
+ const runEndReason = observerEndReason ?? (signal?.aborted ? 'aborted' : 'unknown');
1474
+ const runEndDurMs = Date.now() - timingRunEnter;
1475
+ try {
1476
+ const runEndTiming = {
1477
+ type: 'run.timing',
1478
+ timestamp: Date.now(),
1479
+ sessionId,
1480
+ agentId: _agentId,
1481
+ data: {
1482
+ stage: 'run_end',
1483
+ durMs: runEndDurMs,
1484
+ reason: runEndReason,
1485
+ loopEnd: adaptState.loopEndReason,
1486
+ stopReason: adaptState.stopReason,
1487
+ },
1488
+ };
1489
+ this.config.eventSink?.(runEndTiming, { sessionId, agentId: _agentId });
1490
+ // Hub 只收有 run 记录的 run_end(recordRunStart 前失败时不发,避免归到上一轮 run)
1491
+ if (observerRunId)
1492
+ this.emitObserved(runEndTiming);
1493
+ }
1494
+ catch {
1495
+ // finally 内的观测失败不得吞掉后续收口(detachSession / ObserverHub)
1496
+ }
1497
+ console.log(`[Runner] run end session=${sessionId} reason=${runEndReason} dur=${runEndDurMs}ms` +
1426
1498
  ` loopEnd=${adaptState.loopEndReason ?? 'none'}` +
1427
1499
  (adaptState.stopReason ? ` stopReason=${adaptState.stopReason}` : ''));
1428
1500
  // 清理 task service 的活 session 缓存(避免长时间运行进程内存泄漏)
@@ -1641,6 +1713,47 @@ export class SessionAwareRunner {
1641
1713
  metadata: { source: 'turnEnd', turnId },
1642
1714
  });
1643
1715
  }
1716
+ /**
1717
+ * Run 进行中转录增量落盘:把 durable 口径的历史对齐到存储。
1718
+ *
1719
+ * 口径与收尾合并一致(ephemeral 运行期消息不落历史);调用方契约:
1720
+ * turn_start 定形之后才可同步(bake 原地改写 user 消息)。尽力而为——
1721
+ * 失败不中断 Run(收尾整包 save 是权威写),store 无 syncMessages 时跳过。
1722
+ *
1723
+ * @param sessionId - 会话 id
1724
+ * @param agentId - 本 Run 身份(assistant/tool 归因,首次同步前补齐)
1725
+ * @param keptPrefix - 上下文租约点之前的 Discourse 前缀(与收尾 keptPrefix 同源)
1726
+ * @param runMessages - 本 Run 视图消息(含 ephemeral,此处过滤)
1727
+ * @returns 是否成功对齐(false 时调用方不得推进已同步计数,待下个事件重试)
1728
+ */
1729
+ async persistRunMessages(sessionId, agentId, keptPrefix, runMessages) {
1730
+ const sync = this.store.syncMessages;
1731
+ if (!sync) {
1732
+ if (!this.warnedMissingSync) {
1733
+ this.warnedMissingSync = true;
1734
+ console.warn('[octopi] SessionStore lacks syncMessages; run transcript incremental persist disabled (final save remains authoritative)');
1735
+ }
1736
+ return true; // 无通道 = 非失败,不需要重试
1737
+ }
1738
+ try {
1739
+ // 模型 2 归因:assistant/tool 消息补齐本 Run 的 agentId。必须发生在该消息
1740
+ // 首次同步之前(已同步前缀不得再原地修改);收尾写回处保留同口径兜底循环。
1741
+ for (const m of runMessages) {
1742
+ if ((m.role === 'assistant' || m.role === 'tool') && m.agentId === undefined) {
1743
+ m.agentId = agentId;
1744
+ }
1745
+ }
1746
+ await sync.call(this.store, sessionId, [
1747
+ ...keptPrefix,
1748
+ ...runMessages.filter((m) => m.ephemeral !== true),
1749
+ ]);
1750
+ return true;
1751
+ }
1752
+ catch (err) {
1753
+ console.warn(`[octopi] run transcript incremental persist failed (session=${sessionId}): ${err instanceof Error ? err.message : String(err)}`);
1754
+ return false;
1755
+ }
1756
+ }
1644
1757
  /** 旧路径:persona + injectedContext 字符串拼接;结果返回给 runContext,不写共享 Agent */
1645
1758
  concatSystemPrompt(basePrompt, injectedContext, personaFromResolver) {
1646
1759
  if (injectedContext) {
@@ -11,6 +11,7 @@ import { join } from 'node:path';
11
11
  import { computeEffectiveRights } from '../../governance/session-acl/rights.js';
12
12
  import { sessionMatchesAgent } from '@octopi-agent/core/types/session.js';
13
13
  import { formatHistoryRef, parseHistoryRef, messageText } from './types.js';
14
+ import { isChatReplayHidden } from '../../context/layer-types.js';
14
15
  import { applyRoleWeight, authorAllows, extractSearchableFields, makeSnippet, matchFields, sessionScore, } from './score.js';
15
16
  const gunzipAsync = promisify(gunzip);
16
17
  /**
@@ -387,6 +388,15 @@ class DefaultSessionHistoryPort {
387
388
  visibleMessages(session, agentId) {
388
389
  const all = session.messages ?? [];
389
390
  const indexed = all.map((message, index) => ({ index, message }));
391
+ // 聊天回放隐藏面(turn_end 载体 / 托管 systemPrompt…)不得作为 user/system 命中露出——
392
+ // 与 observer / ConversationAdapter 同一谓词(layer-types isChatReplayHidden)
393
+ const visible = indexed.filter(({ message }) => !isChatReplayHidden({
394
+ role: message.role,
395
+ source: typeof message.metadata?.source === 'string'
396
+ ? String(message.metadata.source)
397
+ : undefined,
398
+ text: messageText(message),
399
+ }));
390
400
  const scope = this.options.historyScope ?? 'participated';
391
401
  if (scope === 'participated' && agentId) {
392
402
  const pick = {
@@ -401,7 +411,7 @@ class DefaultSessionHistoryPort {
401
411
  }
402
412
  const acl = this.options.sessionAcl;
403
413
  if (!acl || !agentId)
404
- return indexed;
414
+ return visible;
405
415
  const access = resolveHistoryAccess(acl, session, agentId, this.options.resolveAgentMax?.(agentId));
406
416
  if (!access)
407
417
  return [];
@@ -410,7 +420,7 @@ class DefaultSessionHistoryPort {
410
420
  case 'none':
411
421
  return [];
412
422
  case 'full':
413
- return indexed;
423
+ return visible;
414
424
  case 'from_grant': {
415
425
  const participant = access.participant;
416
426
  // 缺 seq/at 一律 fail-closed(E6)
@@ -418,12 +428,12 @@ class DefaultSessionHistoryPort {
418
428
  return [];
419
429
  }
420
430
  const at = participant.grantedAt;
421
- return indexed.filter(({ message }) => (message.timestamp ?? 0) >= at);
431
+ return visible.filter(({ message }) => (message.timestamp ?? 0) >= at);
422
432
  }
423
433
  case 'summary_tail':
424
- return indexed.slice(-20);
434
+ return visible.slice(-20);
425
435
  default:
426
- return indexed;
436
+ return visible;
427
437
  }
428
438
  }
429
439
  renderMessage(m, format, includeToolIo) {
@@ -14,6 +14,11 @@ export declare class InMemorySessionStore implements SessionStore<SessionData> {
14
14
  load(sessionId: string): Promise<SessionData | null>;
15
15
  save(sessionId: string, data: SessionData): Promise<void>;
16
16
  updateMetadata(sessionId: string, mutate: (meta: Record<string, unknown>) => void | Record<string, unknown>): Promise<void>;
17
+ /**
18
+ * 消息序列对齐。契约见 SessionStore.syncMessages;
19
+ * 内存后端直接替换消息数组(与 JsonlSessionStore 同口径:不存在则跳过)。
20
+ */
21
+ syncMessages(sessionId: string, messages: readonly unknown[]): Promise<void>;
17
22
  list(filter?: SessionListFilter): Promise<SessionMeta[]>;
18
23
  delete(sessionId: string): Promise<void>;
19
24
  exists(sessionId: string): Promise<boolean>;
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @layer harness/session — Session 域纯内存默认实现(非生产落盘后端)。
8
8
  */
9
- import { sessionMatchesAgent } from '@octopi-agent/core/types/session.js';
9
+ import { readTitleState, sessionMatchesAgent } from '@octopi-agent/core/types/session.js';
10
10
  function toIndexPick(s) {
11
11
  return {
12
12
  agentId: s.agentId ?? s.meta.agentId,
@@ -19,7 +19,12 @@ function toIndexPick(s) {
19
19
  export class InMemorySessionStore {
20
20
  sessions = new Map();
21
21
  async load(sessionId) {
22
- return this.sessions.get(sessionId) ?? null;
22
+ const prev = this.sessions.get(sessionId);
23
+ if (!prev)
24
+ return null;
25
+ // 与 JsonlSessionStore.load 同口径:meta 是投影(标题三键权威在 metadata,
26
+ // updateMetadata 写后 load().meta 立即新鲜,与索引后端行为一致)
27
+ return { ...prev, meta: { ...prev.meta, ...readTitleState(prev.metadata) } };
23
28
  }
24
29
  async save(sessionId, data) {
25
30
  const prev = this.sessions.get(sessionId);
@@ -28,6 +33,17 @@ export class InMemorySessionStore {
28
33
  ...(prev?.metadata ?? {}),
29
34
  ...(data.metadata ?? {}),
30
35
  };
36
+ // 与 JsonlSessionStore 对齐:标题三键按 titleUpdatedAt 仲裁,陈旧值不回滚更新写入;
37
+ // incoming 缺时间戳视为陈旧(写端恒打三键),同样归磁盘胜
38
+ const freshTitle = readTitleState(prev?.metadata);
39
+ const incomingTitle = readTitleState(data.metadata);
40
+ if (freshTitle.titleUpdatedAt !== undefined &&
41
+ (incomingTitle.titleUpdatedAt === undefined ||
42
+ incomingTitle.titleUpdatedAt < freshTitle.titleUpdatedAt)) {
43
+ metadata.title = freshTitle.title;
44
+ metadata.titleSource = freshTitle.titleSource;
45
+ metadata.titleUpdatedAt = freshTitle.titleUpdatedAt;
46
+ }
31
47
  this.sessions.set(sessionId, { ...data, id: sessionId, metadata });
32
48
  }
33
49
  async updateMetadata(sessionId, mutate) {
@@ -41,6 +57,19 @@ export class InMemorySessionStore {
41
57
  metadata: replaced === undefined ? meta : replaced,
42
58
  });
43
59
  }
60
+ /**
61
+ * 消息序列对齐。契约见 SessionStore.syncMessages;
62
+ * 内存后端直接替换消息数组(与 JsonlSessionStore 同口径:不存在则跳过)。
63
+ */
64
+ async syncMessages(sessionId, messages) {
65
+ const prev = this.sessions.get(sessionId);
66
+ if (!prev)
67
+ return;
68
+ this.sessions.set(sessionId, {
69
+ ...prev,
70
+ messages: messages.slice(),
71
+ });
72
+ }
44
73
  async list(filter) {
45
74
  const values = Array.from(this.sessions.values());
46
75
  return values
@@ -54,6 +83,8 @@ export class InMemorySessionStore {
54
83
  pinnedAt: typeof s.metadata?.pinnedAt === 'number' && Number.isFinite(s.metadata.pinnedAt)
55
84
  ? s.metadata.pinnedAt
56
85
  : s.meta.pinnedAt,
86
+ // 与 JsonlSessionStore.buildIndexEntry 同规则:标题权威在 metadata 三键(无旧数据回退)
87
+ ...readTitleState(s.metadata),
57
88
  }));
58
89
  }
59
90
  async delete(sessionId) {
@@ -3,7 +3,8 @@
3
3
  *
4
4
  * 目标:历史列表一眼可认;「你好」类碎消息不浪费 LLM 调用。
5
5
  */
6
- import { getTextContent } from '@octopi-agent/core/types.js';
6
+ import { extractUserMessageText } from '../context/turn-envelope.js';
7
+ import { isHumanSpeech } from '../context/layer-types.js';
7
8
  /** 首条有效用户消息达到该长度才可直接摘要 */
8
9
  export const TITLE_MIN_FIRST_USER_CHARS = 16;
9
10
  /** 累计有效用户内容达到该长度(且至少 1 轮完成)可摘要 */
@@ -12,18 +13,25 @@ export const TITLE_MIN_TOTAL_USER_CHARS = 40;
12
13
  export const TITLE_SNIPPET_MAX_CHARS = 24;
13
14
  /** 生成标题最大长度 */
14
15
  export const TITLE_MAX_CHARS = 40;
15
- /** 参与标题信号统计的用户消息(排除 slash 命令留痕) */
16
+ /** 参与标题信号统计的用户消息(排除 slash 命令留痕与机器插槽;唯一谓词在 layer-types) */
16
17
  function isTitleRelevantUserMessage(msg) {
17
- if (msg.role !== 'user')
18
+ if (!isHumanSpeech(msg))
18
19
  return false;
19
20
  const kind = msg.metadata?.kind;
20
21
  if (kind === 'command' || kind === 'command_result')
21
22
  return false;
22
23
  return true;
23
24
  }
24
- /** 提取纯文本并压空白 */
25
+ /**
26
+ * 提取纯文本并压空白。
27
+ *
28
+ * f126dc9 后真实用户消息的 content 是 `<turn_start>` 整封(含 turn_context
29
+ * 快照/grounding):必须解包取 `<user_message>` 原文,否则标题信号被信封
30
+ * boilerplate 吃满、snippet 被信封形状误判为载荷(OP-WEB-5② 残留)。
31
+ * 非信封正文(assistant / 载体 / 便签)原样返回。
32
+ */
25
33
  function messageText(msg) {
26
- return getTextContent(msg.content).replace(/\s+/g, ' ').trim();
34
+ return extractUserMessageText(msg.content).replace(/\s+/g, ' ').trim();
27
35
  }
28
36
  function safeDecodeURIComponent(s) {
29
37
  try {
@@ -239,11 +247,19 @@ export function isWeakTitle(title) {
239
247
  export function buildTitlePrompt(messages) {
240
248
  const entries = [];
241
249
  for (const msg of messages) {
242
- if (msg.role !== 'user' && msg.role !== 'assistant')
243
- continue;
244
- const kind = msg.metadata?.kind;
245
- if (kind === 'command' || kind === 'command_result')
250
+ if (msg.role === 'user') {
251
+ // 用户侧复用唯一谓词(载体/命令留痕过滤单源)
252
+ if (!isTitleRelevantUserMessage(msg))
253
+ continue;
254
+ }
255
+ else if (msg.role === 'assistant') {
256
+ const kind = msg.metadata?.kind;
257
+ if (kind === 'command' || kind === 'command_result')
258
+ continue;
259
+ }
260
+ else {
246
261
  continue;
262
+ }
247
263
  const text = titleText(msg);
248
264
  if (!text)
249
265
  continue;