mocode-ai 1.8.0 → 1.8.2

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.
@@ -13,12 +13,7 @@ let runningCursor = 0; // 缓冲内光标字符索引(0..len);运行态支持任
13
13
  let runningPlaceholder = '';
14
14
  let currentAbort = null;
15
15
  let listening = false; // onRunningKey 是否处于挂载期(start → stop 之间)
16
- /**
17
- * 运行态「预加载/排队」消息:运行中打字后按 Enter 入队(单条,再次入队即替换),
18
- * 当前 agent 结束后由主循环自动发送,无需用户在 INPUT 态再按一次 Enter。
19
- * null = 没有排队。mode 快照入队那一刻的模式,自动执行时沿用(用户选「沿用当前模式」)。
20
- */
21
- let queuedTurn = null;
16
+ let queuedTurns = [];
22
17
  /** 运行态打开的输入面板(Ctrl+G);非 null = 面板仍开着。主循环进 INPUT 态前须等它关闭,否则两个 keypress 监听抢键。 */
23
18
  let composerPending = null;
24
19
  /** 获取运行中输入缓冲(agent 结束后预填下一轮 INPUT 态)。 */
@@ -30,19 +25,32 @@ export function clearRunningInput() {
30
25
  runningInput = '';
31
26
  runningCursor = 0;
32
27
  }
33
- /** 当前排队消息(运行中按 Enter 预加载);无则 null。主循环在 agent 结束后取走并自动发送。 */
34
- export function getQueuedTurn() {
35
- return queuedTurn;
28
+ /** 排队条数。 */
29
+ export function queuedTurnCount() {
30
+ return queuedTurns.length;
31
+ }
32
+ /** 队首(最早一条 / 下一条待自动发送)排队消息;无则 null。状态行 chip 始终展示它。 */
33
+ function peekQueuedHead() {
34
+ return queuedTurns[0] ?? null;
36
35
  }
37
- /** 取走并清空排队消息(消费一次)。 */
36
+ /** 入队一条排队消息(运行态 Enter);多条按 FIFO 自动发送。同步状态行 chip。 */
37
+ export function enqueueQueuedTurn(text, mode = getAgentMode()) {
38
+ queuedTurns.push({ text, mode });
39
+ syncQueuedChip();
40
+ }
41
+ /** 取走队首(最早一条)排队消息并同步 chip;无则 null。主循环逐条消费自动发送。 */
38
42
  export function consumeQueuedTurn() {
39
- const queued = queuedTurn;
40
- queuedTurn = null;
43
+ const queued = queuedTurns.shift() ?? null;
44
+ syncQueuedChip();
41
45
  return queued;
42
46
  }
43
- /** 取消排队(运行态 Esc 在已有排队时优先取消排队,而不是只清 typeahead)。 */
44
- export function clearQueuedTurn() {
45
- queuedTurn = null;
47
+ /** 把运行态排队队列镜像到状态行 chip:始终展示队首(第一条/下一条待发)+ 总条数;空队列撤下 chip。 */
48
+ function syncQueuedChip() {
49
+ const head = peekQueuedHead();
50
+ if (head)
51
+ layout.setQueuedTurn(head.text, head.mode, queuedTurns.length);
52
+ else
53
+ layout.setQueuedTurn(undefined);
46
54
  }
47
55
  /**
48
56
  * 等待运行态打开的输入面板关闭(无则立即返回)。
@@ -110,8 +118,12 @@ function onRunningKey(_str, key) {
110
118
  layout.setUserActive();
111
119
  // 滚动回看时打字 / 编辑(typeahead)不回尾——保持历史视图,便于运行中边看历史边预输入;
112
120
  // 回尾时机:Enter 在运行态是 no-op,真正回尾发生在 agent 结束后 INPUT 态按 Enter 提交(见 prompt.ts submit 前)。
113
- // Ctrl+C 4 层语义(RUNNING 态):有 typeahead → 清空(层 1,不中断);空 → abort(层 2,中断 agent)。
114
- // 两次 Ctrl+C 才中断(先清 typeahead 再 abort),与 INPUT 态 onCtrlC 的 fish 式一致。
121
+ // Ctrl+C 逐级回退(RUNNING 态,fish 式,与 INPUT 态 onCtrlC 一致),每次只退一级:
122
+ // 1. 有 typeahead 草稿 → 只清草稿(不动排队、不中断);
123
+ // 2. 有排队消息 → 把队尾(最新一条)撤回到输入框(其余排队保留),仍不中断 agent;
124
+ // 3. 再按一次(输入框有内容)→ 清空;
125
+ // 4. 输入框也空 → abort 当前 agent。
126
+ // 多条排队时重复 2→3 即可逐条撤回,直到队列清空才会暂停 agent。
115
127
  // raw 模式下 Ctrl+C 是按键不触发 SIGINT;signal 经 executeTool 串进工具,run_command/web_fetch 即时被杀。
116
128
  if (key.ctrl && key.name === 'c') {
117
129
  if (runningInput.length > 0) {
@@ -119,12 +131,15 @@ function onRunningKey(_str, key) {
119
131
  runningCursor = 0;
120
132
  paint();
121
133
  }
134
+ else if (queuedTurns.length > 0) {
135
+ // 撤回队尾到输入框:内容进 typeahead(agent 结束后预填进 INPUT 态),其余排队保留,不中断 agent。
136
+ const tail = queuedTurns.pop();
137
+ runningInput = tail.text;
138
+ runningCursor = runningInput.length;
139
+ syncQueuedChip(); // 撤/更新状态行 chip,不在内容区写提示
140
+ paint();
141
+ }
122
142
  else if (currentAbort && !currentAbort.signal.aborted) {
123
- // 中断当前任务 = 明确要停下,排队的下一跳也一并取消,避免 agent 一停又自动发新需求。
124
- if (queuedTurn) {
125
- queuedTurn = null;
126
- layout.setQueuedTurn(undefined); // 只撤状态行 chip,不在内容区写提示
127
- }
128
143
  appendCurrentSessionRuntimeEvent('abort', { phase: 'requested', source: 'keyboard' });
129
144
  currentAbort.abort();
130
145
  }
@@ -179,11 +194,11 @@ function onRunningKey(_str, key) {
179
194
  return;
180
195
  }
181
196
  if (key.name === 'escape') {
182
- // 有排队消息时 Esc 只取消排队,不清空 typeahead 草稿(避免误删正在打的字);
197
+ // 有排队消息时 Esc 只取消队尾(最新一条),不清空 typeahead 草稿(避免误删正在打的字);
183
198
  // 无排队时维持原语义:清空 typeahead。
184
- if (queuedTurn) {
185
- queuedTurn = null;
186
- layout.setQueuedTurn(undefined); // 只撤状态行 chip,不在内容区写提示
199
+ if (queuedTurns.length > 0) {
200
+ queuedTurns.pop();
201
+ syncQueuedChip(); // 撤/更新状态行 chip,不在内容区写提示
187
202
  paint();
188
203
  }
189
204
  else {
@@ -193,18 +208,16 @@ function onRunningKey(_str, key) {
193
208
  }
194
209
  return;
195
210
  }
196
- // Enter / Ctrl+J:把 typeahead 内容入队(单条,再次入队即替换)。当前 agent 结束后
197
- // 主循环经 consumeQueuedTurn 自动发送,无需在 INPUT 态再按一次 Enter。空内容 no-op。
211
+ // Enter / Ctrl+J:把 typeahead 内容追加到排队队列(支持多条,FIFO)。当前 agent 结束后
212
+ // 主循环经 consumeQueuedTurn 逐条自动发送,无需在 INPUT 态再按 Enter。空内容 no-op。
198
213
  if (key.name === 'return' || key.name === 'enter' || (key.ctrl && key.name === 'j')) {
199
214
  const text = runningInput.trim();
200
215
  if (!text)
201
216
  return;
202
- queuedTurn = { text: runningInput, mode: getAgentMode() }; // 单条:再次入队即替换
217
+ enqueueQueuedTurn(runningInput, getAgentMode());
203
218
  runningInput = '';
204
219
  runningCursor = 0;
205
- // 入队只反映在状态行 `● 状态` 右侧的常驻 chip(含预览/模式),不往 agent 输出区写行。
206
- layout.setQueuedTurn(queuedTurn.text, queuedTurn.mode);
207
- paint(); // 清空底部 typeahead 回显
220
+ paint(); // 清空底部 typeahead 回显;入队反映在状态行 `● 状态` 右侧的常驻 chip(预览/条数/模式)
208
221
  return;
209
222
  }
210
223
  // 可打印字符(>= 空格,非 ctrl/meta)→ 光标处插入 + 非 dim 回显(与空闲态一致)
@@ -24,7 +24,7 @@ import { renderChip } from '../attachments/image.js';
24
24
  import { contextState, appendCurrentSessionRuntimeEvent } from '../session/index.js';
25
25
  import { effectiveSystemPrompt } from '../skills/index.js';
26
26
  import { clearSkillActivation } from '../skills/activation.js';
27
- import { buildMemoryIndexSection, buildAboutYouSection, buildProjectGraphHint, initPersona, kickoffReflection, drainMemoryBackground, getLastReflectResult, clearLastReflectResult, snapshotTranscript, formatReflectResult, } from '../memory/index.js';
27
+ import { buildMemoryIndexSection, buildAboutYouSection, buildProjectGraphHint, initPersona, gcMemories, kickoffReflection, drainMemoryBackground, getLastReflectResult, clearLastReflectResult, snapshotTranscript, formatReflectResult, } from '../memory/index.js';
28
28
  import { collectQueryHistory } from '../session/query-history.js';
29
29
  import { ToolPolicyController } from '../tools/policy.js';
30
30
  import { routeToolGroups } from '../tools/router.js';
@@ -38,6 +38,9 @@ const emitter = stdin;
38
38
  // ── 运行态交互已移到 ./running-input.ts ──
39
39
  // ── pendingPrefill / pendingTimer / pendingAttachments 保留在此(awaitPendingRecall / echoInput 用)
40
40
  let pendingPrefill = null; // /rollback 选中后预填的 user 输入(下轮 INPUT 态消费)
41
+ // 排队队列里遇到斜杠命令时暂停自动消费:命令不静默自动跑,先填回输入框等用户确认;
42
+ // 确认前队列里后面的消息也不得抢先发送,待该输入框轮次结束再恢复逐条自动发送。
43
+ let queuedHold = false;
41
44
  // ── pending send 撤回窗口(用户按 Enter 后、agent 真发请求前)──
42
45
  // 500ms 内 Ctrl+C / Esc → 整条用户气泡从内容区擦掉 + 原行 prefilled 回输入框(可改可再发);
43
46
  // 期间再按 Enter 立即推进 / 时间到自然推进 → 走原流程 enterRunningMode + runTurn。
@@ -201,6 +204,16 @@ export async function startRepl(initialHistory, sessionId, sandboxRootOverride,
201
204
  // 启动期一次性身份归一(幂等,best-effort):把全局图里的用户指称碎片并入
202
205
  // person-primary,随后 About you 段才能渲染出完整画像。
203
206
  initPersona(isMemoryEnabled());
207
+ // 遗忘 GC 与反思解耦:启动时跑一次(30 天衰减/90 天清除/封顶),
208
+ // 不再依赖 AUTO_REFLECT 是否开启。静默容错——GC 失败绝不拖垮 REPL 启动。
209
+ if (isMemoryEnabled()) {
210
+ try {
211
+ gcMemories();
212
+ }
213
+ catch {
214
+ // best-effort
215
+ }
216
+ }
204
217
  // 开关现读:运行时 /memory_switch 重建 history[0] 时,About you 与 Memory Index 同步显隐。
205
218
  const buildSystemMessage = (planMode) => {
206
219
  const memoryOn = isMemoryEnabled();
@@ -690,13 +703,11 @@ export async function startRepl(initialHistory, sessionId, sandboxRootOverride,
690
703
  // 运行态 Ctrl+G 打开的输入面板可能在 agent 结束时仍开着:等它关闭再进 INPUT 态,
691
704
  // 否则面板与 promptWithSlashMenu 两个 keypress 监听抢键;面板确认的内容随后经 getRunningInput 预填。
692
705
  await waitForRunningComposer();
693
- // 消息预加载:上一轮运行中用户按 Enter 排了队(consumeQueuedTurn),agent 一结束就自动发送,
694
- // 跳过 INPUT 态输入框与撤回窗口(入队那一刻即已确认)。非空才自动发;排队时正在打的 typeahead
695
- // 草稿经 pendingPrefill 保住,留到下一轮输入框。
696
- const queued = consumeQueuedTurn();
706
+ // 消息预加载:上一轮运行中用户按 Enter 排了队(consumeQueuedTurn,支持多条 FIFO),agent 一结束就
707
+ // 逐条自动发送,跳过 INPUT 态输入框与撤回窗口(入队那一刻即已确认)。queuedHold=上一条是被拦的斜杠
708
+ // 命令、仍在输入框等确认,此时不消费队列(后面的消息不得抢先),等该输入框轮次结束再恢复。
709
+ const queued = queuedHold ? null : consumeQueuedTurn();
697
710
  const autoSend = !!queued && queued.text.trim().length > 0;
698
- if (queued)
699
- layout.setQueuedTurn(undefined); // chip 已被消费(或为空丢弃),从状态行撤下
700
711
  if (autoSend && queued) {
701
712
  // 排队时仍在输入框草稿里的字(typeahead)不能丢:回填为下一轮预填。
702
713
  if (getRunningInput().trim())
@@ -747,6 +758,7 @@ export async function startRepl(initialHistory, sessionId, sandboxRootOverride,
747
758
  }
748
759
  pendingPrefill = null; // 预填已消费,清空
749
760
  clearRunningInput(); // 预填已消费,清空(下轮运行态从空开始)
761
+ queuedHold = false; // 被拦的排队斜杠命令已在输入框处理完,恢复队列自动消费
750
762
  }
751
763
  let joined = input.join('\n');
752
764
  const line = joined.trim();
@@ -756,6 +768,7 @@ export async function startRepl(initialHistory, sessionId, sandboxRootOverride,
756
768
  // (否则排队 /clear、/exit、/rollback 会在用户没看着时触发),改为填回输入框由用户显式确认。
757
769
  if (autoSend && isCommandShape(line.split(/\s+/)[0] ?? '')) {
758
770
  pendingPrefill = input;
771
+ queuedHold = true; // 暂停消费队列,等该命令在输入框被确认/处理完,后面的排队消息才继续
759
772
  layout.contentWrite(`${ui.dim} ↳ ${t('repl.queuedCommandHeld')}${ui.reset}\n`);
760
773
  continue;
761
774
  }
@@ -98,7 +98,7 @@ export const editFileTool = {
98
98
  }
99
99
  const toLf = (s) => s.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
100
100
  // multi:数组形状校验(schema 已守,这里给富指导并归一化 CRLF)。
101
- let multiEdits = [];
101
+ const multiEdits = [];
102
102
  if (mode === 'multi') {
103
103
  const arr = args.replacements;
104
104
  if (!Array.isArray(arr) || arr.length === 0) {
@@ -1,19 +1,31 @@
1
1
  import { forgetEntry } from '../../memory/store.js';
2
+ import { invalidateEdge } from '../../memory/graph.js';
2
3
  // ---------- memory_forget ----------
3
- // 遗忘:默认归档(archived,从索引/默认搜索隐藏,可复活);mode=delete 硬删。pinned 拒删。
4
+ // 遗忘两类对象:
5
+ // - 记忆条目(默认):归档(archived,从索引/默认搜索隐藏,可复活)或 mode=delete 硬删;
6
+ // pinned 拒删。
7
+ // - 图谱边(kind=edge,id 形如 edge-…):时序失效(invalidAt,不删除可审计),
8
+ // 这是撤销错误画像/错误三元组的产品内出口(此前只能手改 memory-graph.json)。
4
9
  export const memoryForgetTool = {
5
10
  name: 'memory_forget',
6
- description: "Forget a memory entry: default archive (hidden, revivable via memory_update); mode=delete hard-deletes. Pinned entries can't be deleted (unpin first).",
11
+ description: "Forget a memory entry (kind=entry, default): archive (hidden, revivable via memory_update) or mode=delete hard-delete; pinned entries can't be deleted (unpin first). " +
12
+ "Or invalidate a knowledge-graph edge (kind=edge, id starts with 'edge-', shown by memory_graph neighbors/path): temporally revoked, kept for audit. Use edge to correct a wrong profile fact or triple.",
7
13
  risk: 'confirm',
8
14
  parameters: {
9
15
  type: 'object',
10
16
  properties: {
11
- id: { type: 'string' },
17
+ id: { type: 'string', description: 'Entry id, or edge id (edge-…) when kind=edge' },
12
18
  mode: {
13
19
  type: 'string',
14
20
  enum: ['archive', 'delete'],
15
- description: 'Default archive',
21
+ description: 'kind=entry only: default archive',
16
22
  },
23
+ kind: {
24
+ type: 'string',
25
+ enum: ['entry', 'edge', 'auto'],
26
+ description: "What to forget: entry (default), edge (graph triple, id 'edge-…'), or auto (edge-* prefix → edge, else entry)",
27
+ },
28
+ reason: { type: 'string', description: 'Why it is forgotten/withdrawn (audit trail for edges)' },
17
29
  },
18
30
  required: ['id'],
19
31
  },
@@ -21,12 +33,21 @@ export const memoryForgetTool = {
21
33
  const id = String(args.id ?? '').trim();
22
34
  if (!id)
23
35
  return '错误:缺少 id。';
36
+ const reason = typeof args.reason === 'string' ? args.reason.trim() : '';
37
+ const kind = args.kind === 'edge' || args.kind === 'entry' ? args.kind : 'auto';
38
+ const wantEdge = kind === 'edge' || (kind === 'auto' && id.startsWith('edge-'));
39
+ if (wantEdge) {
40
+ const r = invalidateEdge(id, reason || 'manual-forget');
41
+ if (r.ok)
42
+ return `已撤销图谱边 [${id}]${reason ? `(原因:${reason})` : ''}(时序失效,可审计)。`;
43
+ return `错误:找不到 active 图谱边 id="${id}"。`;
44
+ }
24
45
  const mode = args.mode === 'delete' ? 'delete' : 'archive';
25
46
  const r = forgetEntry(id, mode);
26
47
  if (r.ok)
27
48
  return `已${mode === 'delete' ? '硬删' : '归档'}记忆 [${id}]。`;
28
49
  if ('notFound' in r)
29
- return `错误:找不到记忆 id="${id}"。`;
50
+ return `错误:找不到记忆 id="${id}"。(图谱边请用 kind=edge)`;
30
51
  // pinned
31
52
  return `错误:[${id}] 已钉住,拒绝遗忘。先 memory_update(id="${id}", pinned=false) 解钉。`;
32
53
  },
@@ -45,6 +45,11 @@ export const memoryGraphTool = {
45
45
  enum: ['project', 'global'],
46
46
  description: 'add: which graph file to write, default project',
47
47
  },
48
+ prov: {
49
+ type: 'string',
50
+ enum: ['user_said', 'user_confirmed', 'tool_observed', 'reflection'],
51
+ description: 'add: provenance for belief grading. Global profile facts the user stated directly → user_said (internalized); reflection-only guesses → reflection (0.4, not internalized until confirmed).',
52
+ },
48
53
  },
49
54
  required: ['action'],
50
55
  },
@@ -98,12 +103,14 @@ export const memoryGraphTool = {
98
103
  const dst = String(args.dst ?? '').trim();
99
104
  if (!src || !relation || !dst)
100
105
  return '错误:add 需要 src、relation、dst。';
106
+ const prov = ['user_said', 'user_confirmed', 'tool_observed', 'reflection'].find((p) => args.prov === p);
101
107
  const r = addTriple({
102
108
  src,
103
109
  relation,
104
110
  dst,
105
111
  fact: typeof args.fact === 'string' ? args.fact : undefined,
106
112
  scope: args.scope === 'global' ? 'global' : 'project',
113
+ prov,
107
114
  });
108
115
  if (!r.ok)
109
116
  return `错误:三元组未写入 (${r.reason})。`;
@@ -61,10 +61,12 @@ export const state = {
61
61
  * composeModelLine 在 RUNNING 态把它画成 chip 放 context 进度条左侧;
62
62
  * 不主动触发重画——RUNNING 态 turnTimer 80ms 心跳重画自然取最新值。 */
63
63
  liveUsage: undefined,
64
- /** 运行态已排队、当前任务结束后自动发送的下一条消息(纯文本摘要,无=undefined)。 */
64
+ /** 运行态已排队消息:队首(第一条/下一条待自动发送)纯文本预览,无=undefined。 */
65
65
  queuedText: undefined,
66
- /** 排队消息入队时的模式,用于在 chip 上标注 Plan/Auto。 */
66
+ /** 队首排队消息入队时的模式,用于在 chip 上标注 Plan/Auto。 */
67
67
  queuedMode: undefined,
68
+ /** 排队剩余条数(>=1);1 = 单条不显示计数,>1 在 chip 上显示「×N」。 */
69
+ queuedCount: 1,
68
70
  statusText: '',
69
71
  spinnerFrame: undefined,
70
72
  turnStart: null, // RUNNING 态起点(Date.now());INPUT 态为 null。composeStatus 据此拼走时。
@@ -378,15 +378,17 @@ export function setLiveUsage(u) {
378
378
  state.liveUsage = u;
379
379
  }
380
380
  /**
381
- * 设置/清除运行态已排队的下一条消息 chip(显示在 `● 状态` 右侧)。
382
- * 立即重画一次保证入队瞬间可见;之后 RUNNING 态 80ms 心跳会自然带上。
381
+ * 设置/清除运行态已排队消息 chip(显示在 `● 状态` 右侧)。
382
+ * text = 队首(第一条/下一条待自动发送)预览,多条排队时保持展示第一条不变;
383
+ * count = 排队总条数(>1 显示「×N」)。立即重画一次保证入队瞬间可见;之后 80ms 心跳会自然带上。
383
384
  */
384
- export function setQueuedTurn(text, mode) {
385
+ export function setQueuedTurn(text, mode, count = 1) {
385
386
  state.queuedText = text;
386
387
  state.queuedMode = mode;
388
+ state.queuedCount = count;
387
389
  drawStatusBar();
388
390
  }
389
- /** 排队 chip:▸ 排队: <首行…>(Plan)。只拼左段,宽度另计(不含 ANSI)。 */
391
+ /** 排队 chip:▸ 排队[ ×N] · <第一条首行…>(Plan)。只拼左段,宽度另计(不含 ANSI)。 */
390
392
  function queuedChip() {
391
393
  const raw = state.queuedText;
392
394
  if (!raw)
@@ -394,7 +396,7 @@ function queuedChip() {
394
396
  const firstLine = raw.split('\n')[0] ?? '';
395
397
  const preview = truncateDisplay(firstLine.trim(), 32);
396
398
  const modeTag = state.queuedMode === 'plan' ? t('status.queuePlanTag') : '';
397
- const label = t('status.queued');
399
+ const label = state.queuedCount > 1 ? t('status.queuedCount', { count: state.queuedCount }) : t('status.queued');
398
400
  const plain = `${label} ${preview}${modeTag}`;
399
401
  const str = `${ui.yellow}▸ ${plain}${ui.reset}`;
400
402
  return { str, w: 2 + displayWidth(plain) };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mocode-ai",
3
- "version": "1.8.0",
3
+ "version": "1.8.2",
4
4
  "description": "终端编码 agent:LLM + tool-call 循环 + 流式输出(含思考)+ 27 个工具,接任意 OpenAI 兼容后端。",
5
5
  "type": "module",
6
6
  "bin": {