dominds 0.7.0 → 0.7.1

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.
@@ -57,6 +57,26 @@ const BASELINE_ENV_SNAPSHOT_CMD = 'uname -a';
57
57
  const PRIMING_VCS_SESSION_SLUG = 'rtws-vcs-inventory';
58
58
  const cacheByAgentId = new Map();
59
59
  const inflightByAgentId = new Map();
60
+ class AgentPrimingInterruptedError extends Error {
61
+ constructor(reason) {
62
+ super(`Agent Priming interrupted: ${reason}`);
63
+ this.name = 'AgentPrimingInterruptedError';
64
+ this.reason = reason;
65
+ }
66
+ }
67
+ function isAgentPrimingInterruptedError(error) {
68
+ return error instanceof AgentPrimingInterruptedError;
69
+ }
70
+ function throwIfAgentPrimingStopped(dlg, abortSignal) {
71
+ if (!abortSignal.aborted) {
72
+ return;
73
+ }
74
+ const reason = (0, dialog_run_state_1.getStopRequestedReason)(dlg.id);
75
+ if (reason === 'emergency_stop' || reason === 'user_stop') {
76
+ throw new AgentPrimingInterruptedError(reason);
77
+ }
78
+ throw new AgentPrimingInterruptedError('user_stop');
79
+ }
60
80
  async function emitSayingEventsAndPersist(dlg, content) {
61
81
  const calls = await (0, driver_entry_1.emitSayingEvents)(dlg, content);
62
82
  const genseq = dlg.activeGenSeqOrUndefined;
@@ -156,8 +176,23 @@ function scheduleAgentPrimingForNewDialog(dlg, options) {
156
176
  }
157
177
  // mode === 'do': wait for in-flight then run again for this dialog.
158
178
  if (options.mode === 'do') {
159
- return runAgentPrimingLive(dlg).then((next) => {
179
+ return runAgentPrimingLive(dlg)
180
+ .then((next) => {
160
181
  cacheByAgentId.set(agentId, next);
182
+ })
183
+ .catch((err) => {
184
+ if (isAgentPrimingInterruptedError(err)) {
185
+ log_1.log.info('Agent Priming interrupted; will retry on next dialog', undefined, {
186
+ agentId,
187
+ reason: err.reason,
188
+ });
189
+ }
190
+ else {
191
+ log_1.log.warn('Agent Priming live run failed; will retry on next dialog', err, {
192
+ agentId,
193
+ });
194
+ }
195
+ return undefined;
161
196
  });
162
197
  }
163
198
  return Promise.resolve();
@@ -170,7 +205,15 @@ function scheduleAgentPrimingForNewDialog(dlg, options) {
170
205
  })
171
206
  .catch((err) => {
172
207
  // Best-effort: avoid unhandled rejections; the dialog itself is already marked interrupted.
173
- log_1.log.warn('Agent Priming live run failed; will retry on next dialog', err, { agentId });
208
+ if (isAgentPrimingInterruptedError(err)) {
209
+ log_1.log.info('Agent Priming interrupted; will retry on next dialog', undefined, {
210
+ agentId,
211
+ reason: err.reason,
212
+ });
213
+ }
214
+ else {
215
+ log_1.log.warn('Agent Priming live run failed; will retry on next dialog', err, { agentId });
216
+ }
174
217
  return null;
175
218
  })
176
219
  .finally(() => {
@@ -834,7 +877,7 @@ function formatFbrTellaskBody(language, snapshotText, options) {
834
877
  ].join('\n');
835
878
  }
836
879
  async function generatePrimingNoteViaMainlineAgent(options) {
837
- const { dlg, shellSnapshotText, shellResponseText, vcsRound1Text, vcsRound2Text, fbrResponses, fbrTellaskHead, fbrCallId, } = options;
880
+ const { dlg, shellSnapshotText, shellResponseText, vcsRound1Text, vcsRound2Text, fbrResponses, fbrTellaskHead, fbrCallId, assertNotStopped, } = options;
838
881
  // Trigger a normal drive and rely on driver.ts context assembly.
839
882
  // Agent Priming must not trigger Diligence Push (“鞭策”); it should be best-effort
840
883
  // one-shot distillation with no keep-going injection.
@@ -949,6 +992,7 @@ async function generatePrimingNoteViaMainlineAgent(options) {
949
992
  ].join('\n');
950
993
  // IMPORTANT: this is an internal (non-persisted) prompt. driver.ts will inject it into
951
994
  // the LLM context for this drive only, without polluting dialog history.
995
+ assertNotStopped?.();
952
996
  await (0, driver_entry_1.driveDialogStream)(dlg, {
953
997
  content: internalPrompt,
954
998
  msgId: (0, id_1.generateShortId)(),
@@ -956,6 +1000,7 @@ async function generatePrimingNoteViaMainlineAgent(options) {
956
1000
  persistMode: 'internal',
957
1001
  skipTaskdoc: true,
958
1002
  }, true);
1003
+ assertNotStopped?.();
959
1004
  const afterMsgs = dlg.msgs.length;
960
1005
  if (afterMsgs <= beforeMsgs) {
961
1006
  throw new Error('Agent Priming distillation produced no new messages.');
@@ -1077,15 +1122,24 @@ function buildCoursePrefixMsgs(entry) {
1077
1122
  return out;
1078
1123
  }
1079
1124
  async function replayAgentPriming(dlg, entry) {
1125
+ const hadActiveRunBefore = (0, dialog_run_state_1.hasActiveRun)(dlg.id);
1126
+ const primingAbortSignal = (0, dialog_run_state_1.createActiveRun)(dlg.id);
1127
+ const ownsActiveRun = !hadActiveRunBefore;
1128
+ const assertNotStopped = () => {
1129
+ throwIfAgentPrimingStopped(dlg, primingAbortSignal);
1130
+ };
1080
1131
  const release = await dlg.acquire();
1132
+ let interruptedRunState = null;
1081
1133
  try {
1082
1134
  const language = (0, runtime_language_1.getWorkLanguage)();
1083
1135
  dlg.setCoursePrefixMsgs(buildCoursePrefixMsgs(entry));
1084
1136
  await (0, dialog_run_state_1.setDialogRunState)(dlg.id, { kind: 'proceeding' });
1137
+ assertNotStopped();
1085
1138
  // Phase 1: shell ask (and optional prelude intro)
1086
1139
  let shellCallId = null;
1087
1140
  let shellTellaskHead = null;
1088
1141
  try {
1142
+ assertNotStopped();
1089
1143
  await dlg.notifyGeneratingStart();
1090
1144
  await emitUiOnlyMarkdownEventsAndPersist(dlg, formatPreludeIntro(language, true, entry.shellPolicy, entry.shell.kind === 'specialist_tellask' ? entry.shell.specialistId : null));
1091
1145
  if (entry.shell.kind === 'specialist_tellask') {
@@ -1115,6 +1169,7 @@ async function replayAgentPriming(dlg, entry) {
1115
1169
  }
1116
1170
  // Phase 2: shell response (separate bubble)
1117
1171
  if (entry.shell.kind === 'specialist_tellask' && shellCallId && shellTellaskHead) {
1172
+ assertNotStopped();
1118
1173
  await dlg.receiveTeammateResponse(entry.shell.specialistId, shellTellaskHead, 'completed', dlg.id, {
1119
1174
  response: entry.shell.responseText,
1120
1175
  agentId: entry.shell.specialistId,
@@ -1129,6 +1184,7 @@ async function replayAgentPriming(dlg, entry) {
1129
1184
  let round2CallId = null;
1130
1185
  let round2TellaskHead = null;
1131
1186
  try {
1187
+ assertNotStopped();
1132
1188
  await dlg.notifyGeneratingStart();
1133
1189
  const round1Content = [
1134
1190
  `!?@${entry.vcs.specialistId} !tellaskSession ${entry.vcs.sessionSlug}`,
@@ -1151,6 +1207,7 @@ async function replayAgentPriming(dlg, entry) {
1151
1207
  }
1152
1208
  }
1153
1209
  if (round1CallId && round1TellaskHead) {
1210
+ assertNotStopped();
1154
1211
  await dlg.receiveTeammateResponse(entry.vcs.specialistId, round1TellaskHead, 'completed', dlg.id, {
1155
1212
  response: entry.vcs.round1.responseText,
1156
1213
  agentId: entry.vcs.specialistId,
@@ -1159,6 +1216,7 @@ async function replayAgentPriming(dlg, entry) {
1159
1216
  });
1160
1217
  }
1161
1218
  try {
1219
+ assertNotStopped();
1162
1220
  await dlg.notifyGeneratingStart();
1163
1221
  const round2Content = [
1164
1222
  `!?@${entry.vcs.specialistId} !tellaskSession ${entry.vcs.sessionSlug}`,
@@ -1181,6 +1239,7 @@ async function replayAgentPriming(dlg, entry) {
1181
1239
  }
1182
1240
  }
1183
1241
  if (round2CallId && round2TellaskHead) {
1242
+ assertNotStopped();
1184
1243
  await dlg.receiveTeammateResponse(entry.vcs.specialistId, round2TellaskHead, 'completed', dlg.id, {
1185
1244
  response: entry.vcs.round2.responseText,
1186
1245
  agentId: entry.vcs.specialistId,
@@ -1191,6 +1250,7 @@ async function replayAgentPriming(dlg, entry) {
1191
1250
  }
1192
1251
  else {
1193
1252
  try {
1253
+ assertNotStopped();
1194
1254
  await dlg.notifyGeneratingStart();
1195
1255
  await emitSayingEventsAndPersist(dlg, entry.vcs.round1NoteMarkdown);
1196
1256
  }
@@ -1203,6 +1263,7 @@ async function replayAgentPriming(dlg, entry) {
1203
1263
  }
1204
1264
  }
1205
1265
  try {
1266
+ assertNotStopped();
1206
1267
  await dlg.notifyGeneratingStart();
1207
1268
  await emitSayingEventsAndPersist(dlg, entry.vcs.round2NoteMarkdown);
1208
1269
  }
@@ -1221,6 +1282,7 @@ async function replayAgentPriming(dlg, entry) {
1221
1282
  const effort = Math.max(0, Math.floor(entry.fbr.effort));
1222
1283
  if (effort >= 1 && entry.fbr.responses.length > 0) {
1223
1284
  try {
1285
+ assertNotStopped();
1224
1286
  await dlg.notifyGeneratingStart();
1225
1287
  const fbrCallBody = [entry.fbr.selfTeaser, '', entry.fbr.tellaskBody].join('\n');
1226
1288
  const fbrCallContent = [
@@ -1248,6 +1310,7 @@ async function replayAgentPriming(dlg, entry) {
1248
1310
  const normalized = Math.max(1, effort);
1249
1311
  const responses = entry.fbr.responses.slice(0, normalized);
1250
1312
  for (let i = 0; i < responses.length; i++) {
1313
+ assertNotStopped();
1251
1314
  const raw = responses[i] ?? '';
1252
1315
  await dlg.receiveTeammateResponse(entry.fbr.responderAgentId, fbrTellaskHead, 'completed', dlg.id, {
1253
1316
  response: raw,
@@ -1260,6 +1323,7 @@ async function replayAgentPriming(dlg, entry) {
1260
1323
  }
1261
1324
  // Phase 5: summary bubble
1262
1325
  try {
1326
+ assertNotStopped();
1263
1327
  await dlg.notifyGeneratingStart();
1264
1328
  await emitSayingEventsAndPersist(dlg, entry.primingNote);
1265
1329
  }
@@ -1273,25 +1337,55 @@ async function replayAgentPriming(dlg, entry) {
1273
1337
  }
1274
1338
  }
1275
1339
  catch (err) {
1276
- log_1.log.warn('Agent Priming replay failed (best-effort)', err, { dialogId: dlg.id.valueOf() });
1340
+ if (isAgentPrimingInterruptedError(err)) {
1341
+ interruptedRunState = {
1342
+ kind: 'interrupted',
1343
+ reason: { kind: err.reason },
1344
+ };
1345
+ log_1.log.info('Agent Priming replay interrupted by stop request', undefined, {
1346
+ dialogId: dlg.id.valueOf(),
1347
+ reason: err.reason,
1348
+ });
1349
+ }
1350
+ else {
1351
+ log_1.log.warn('Agent Priming replay failed (best-effort)', err, { dialogId: dlg.id.valueOf() });
1352
+ }
1277
1353
  }
1278
1354
  finally {
1279
- let nextIdle = { kind: 'idle_waiting_user' };
1280
1355
  try {
1281
- nextIdle = await (0, dialog_run_state_1.computeIdleRunState)(dlg);
1356
+ if (interruptedRunState) {
1357
+ await (0, dialog_run_state_1.setDialogRunState)(dlg.id, interruptedRunState);
1358
+ }
1359
+ else {
1360
+ let nextIdle = { kind: 'idle_waiting_user' };
1361
+ try {
1362
+ nextIdle = await (0, dialog_run_state_1.computeIdleRunState)(dlg);
1363
+ }
1364
+ catch (err) {
1365
+ log_1.log.warn('Failed to compute idle runState after Agent Priming replay; falling back', err, {
1366
+ dialogId: dlg.id.valueOf(),
1367
+ });
1368
+ }
1369
+ await (0, dialog_run_state_1.setDialogRunState)(dlg.id, nextIdle);
1370
+ }
1282
1371
  }
1283
- catch (err) {
1284
- log_1.log.warn('Failed to compute idle runState after Agent Priming replay; falling back', err, {
1285
- dialogId: dlg.id.valueOf(),
1286
- });
1372
+ finally {
1373
+ if (ownsActiveRun) {
1374
+ (0, dialog_run_state_1.clearActiveRun)(dlg.id);
1375
+ }
1376
+ release();
1287
1377
  }
1288
- await (0, dialog_run_state_1.setDialogRunState)(dlg.id, nextIdle);
1289
- release();
1290
1378
  }
1291
1379
  }
1292
1380
  async function runAgentPrimingLive(dlg) {
1293
1381
  const createdAt = (0, time_1.formatUnifiedTimestamp)(new Date());
1294
1382
  const language = (0, runtime_language_1.getWorkLanguage)();
1383
+ const hadActiveRunBefore = (0, dialog_run_state_1.hasActiveRun)(dlg.id);
1384
+ const primingAbortSignal = (0, dialog_run_state_1.createActiveRun)(dlg.id);
1385
+ const ownsActiveRun = !hadActiveRunBefore;
1386
+ const assertNotStopped = () => {
1387
+ throwIfAgentPrimingStopped(dlg, primingAbortSignal);
1388
+ };
1295
1389
  const prevDisableDiligencePush = dlg.disableDiligencePush;
1296
1390
  // Agent Priming is a bounded bootstrap routine; no keep-going injection should appear
1297
1391
  // during the priming lifecycle (including any auto-revive drives triggered by subdialog replies).
@@ -1319,7 +1413,9 @@ async function runAgentPrimingLive(dlg) {
1319
1413
  const fbrResponsesForInjection = [];
1320
1414
  try {
1321
1415
  await (0, dialog_run_state_1.setDialogRunState)(dlg.id, { kind: 'proceeding' });
1416
+ assertNotStopped();
1322
1417
  const team = await team_1.Team.load();
1418
+ assertNotStopped();
1323
1419
  const member = team.getMember(dlg.agentId);
1324
1420
  const specialists = team.shellSpecialists
1325
1421
  .filter((s) => typeof s === 'string')
@@ -1339,6 +1435,7 @@ async function runAgentPrimingLive(dlg) {
1339
1435
  // Phase 1: shell ask (and optional prelude intro)
1340
1436
  if (shellPolicy === 'specialist_only' && specialistId !== null) {
1341
1437
  shellTellaskBody = formatShellTellaskBody(language, specialistId);
1438
+ assertNotStopped();
1342
1439
  await dlg.withLock(async () => {
1343
1440
  try {
1344
1441
  await dlg.notifyGeneratingStart();
@@ -1372,6 +1469,7 @@ async function runAgentPrimingLive(dlg) {
1372
1469
  // In both cases we skip the shell Tellask step and let the runtime capture a baseline snapshot.
1373
1470
  // Keep it safe and deterministic: no network, no writes.
1374
1471
  const unameOutput = await runUnameA();
1472
+ assertNotStopped();
1375
1473
  shellResponseText = unameOutput;
1376
1474
  snapshotText = unameOutput;
1377
1475
  const directNote = (() => {
@@ -1401,6 +1499,7 @@ async function runAgentPrimingLive(dlg) {
1401
1499
  ].join('\n');
1402
1500
  })();
1403
1501
  directNoteMarkdown = directNote;
1502
+ assertNotStopped();
1404
1503
  await dlg.withLock(async () => {
1405
1504
  try {
1406
1505
  await dlg.notifyGeneratingStart();
@@ -1435,6 +1534,7 @@ async function runAgentPrimingLive(dlg) {
1435
1534
  throw new Error('Missing shell tellaskHead');
1436
1535
  }
1437
1536
  const tellaskBody = shellTellaskBodyForSubdialog ?? shellTellaskBody;
1537
+ assertNotStopped();
1438
1538
  const sub = await dlg.withLock(async () => {
1439
1539
  return await dlg.createSubDialog(ensuredSpecialistId, ensuredShellTellaskHead, tellaskBody, {
1440
1540
  originMemberId: dlg.agentId,
@@ -1452,6 +1552,7 @@ async function runAgentPrimingLive(dlg) {
1452
1552
  collectiveTargets: [ensuredSpecialistId],
1453
1553
  });
1454
1554
  await (0, driver_entry_1.driveDialogStream)(sub, { content: initPrompt, msgId: (0, id_1.generateShortId)(), grammar: 'markdown', skipTaskdoc: true }, true);
1555
+ assertNotStopped();
1455
1556
  shellResponseText = extractLastAssistantSaying(sub.msgs);
1456
1557
  const toolResult = extractLastShellCmdResultText(sub.msgs);
1457
1558
  snapshotText = toolResult ? toolResult : shellResponseText;
@@ -1459,7 +1560,9 @@ async function runAgentPrimingLive(dlg) {
1459
1560
  // Specialist produced no usable output (misconfigured tools, provider issues, etc.).
1460
1561
  // Fall back to a runtime-executed `uname -a` so we can still proceed to FBR.
1461
1562
  snapshotText = await runUnameA();
1563
+ assertNotStopped();
1462
1564
  }
1565
+ assertNotStopped();
1463
1566
  await dlg.withLock(async () => {
1464
1567
  await dlg.receiveTeammateResponse(ensuredSpecialistId, ensuredShellTellaskHead, 'completed', sub.id, {
1465
1568
  response: shellResponseText,
@@ -1503,6 +1606,7 @@ async function runAgentPrimingLive(dlg) {
1503
1606
  }
1504
1607
  }
1505
1608
  });
1609
+ assertNotStopped();
1506
1610
  const round1Sub = await dlg.withLock(async () => {
1507
1611
  return await dlg.createSubDialog(ensuredSpecialistId, round1TellaskHead, round1TellaskBodyForSubdialog || vcsRound1Body, {
1508
1612
  originMemberId: dlg.agentId,
@@ -1514,6 +1618,7 @@ async function runAgentPrimingLive(dlg) {
1514
1618
  });
1515
1619
  const rootDialog = dlg instanceof dialog_1.RootDialog ? dlg : dlg instanceof dialog_1.SubDialog ? dlg.rootDialog : undefined;
1516
1620
  if (rootDialog) {
1621
+ assertNotStopped();
1517
1622
  rootDialog.registerSubdialog(round1Sub);
1518
1623
  await rootDialog.saveSubdialogRegistry();
1519
1624
  }
@@ -1531,10 +1636,12 @@ async function runAgentPrimingLive(dlg) {
1531
1636
  grammar: 'markdown',
1532
1637
  skipTaskdoc: true,
1533
1638
  }, true);
1639
+ assertNotStopped();
1534
1640
  vcsRound1ResponseText = extractLastAssistantSaying(round1Sub.msgs).trim();
1535
1641
  if (!vcsRound1ResponseText) {
1536
1642
  throw new Error('Specialist VCS session round-1 returned empty output');
1537
1643
  }
1644
+ assertNotStopped();
1538
1645
  await dlg.withLock(async () => {
1539
1646
  await dlg.receiveTeammateResponse(ensuredSpecialistId, round1TellaskHead, 'completed', round1Sub.id, {
1540
1647
  response: vcsRound1ResponseText,
@@ -1587,10 +1694,12 @@ async function runAgentPrimingLive(dlg) {
1587
1694
  grammar: 'markdown',
1588
1695
  skipTaskdoc: true,
1589
1696
  }, true);
1697
+ assertNotStopped();
1590
1698
  vcsRound2ResponseText = extractLastAssistantSaying(round1Sub.msgs).trim();
1591
1699
  if (!vcsRound2ResponseText) {
1592
1700
  throw new Error('Specialist VCS session round-2 returned empty output');
1593
1701
  }
1702
+ assertNotStopped();
1594
1703
  await dlg.withLock(async () => {
1595
1704
  await dlg.receiveTeammateResponse(ensuredSpecialistId, round2TellaskHead, 'completed', round1Sub.id, {
1596
1705
  response: vcsRound2ResponseText,
@@ -1609,6 +1718,7 @@ async function runAgentPrimingLive(dlg) {
1609
1718
  }
1610
1719
  }
1611
1720
  const runtimeInventory = await collectRtwsGitInventory();
1721
+ assertNotStopped();
1612
1722
  const runtimeRound1Text = formatRtwsGitInventoryRound1(language, runtimeInventory);
1613
1723
  const runtimeRound2Text = formatRtwsGitInventoryRound2(language, runtimeInventory);
1614
1724
  const runtimeInventoryText = [runtimeRound1Text, '', runtimeRound2Text].join('\n');
@@ -1630,6 +1740,7 @@ async function runAgentPrimingLive(dlg) {
1630
1740
  vcsRound1NoteMarkdown = formatRuntimeVcsRoundNote(language, 1, vcsRound1ResponseText);
1631
1741
  vcsRound2NoteMarkdown = formatRuntimeVcsRoundNote(language, 2, vcsRound2ResponseText);
1632
1742
  vcsInventoryText = runtimeInventoryText;
1743
+ assertNotStopped();
1633
1744
  await dlg.withLock(async () => {
1634
1745
  try {
1635
1746
  await dlg.notifyGeneratingStart();
@@ -1644,6 +1755,7 @@ async function runAgentPrimingLive(dlg) {
1644
1755
  }
1645
1756
  }
1646
1757
  });
1758
+ assertNotStopped();
1647
1759
  await dlg.withLock(async () => {
1648
1760
  try {
1649
1761
  await dlg.notifyGeneratingStart();
@@ -1683,6 +1795,7 @@ async function runAgentPrimingLive(dlg) {
1683
1795
  let fbrTellaskHead = null;
1684
1796
  // Phase 3: FBR ask (call bubble)
1685
1797
  if (fbrEffort >= 1) {
1798
+ assertNotStopped();
1686
1799
  await dlg.withLock(async () => {
1687
1800
  try {
1688
1801
  await dlg.notifyGeneratingStart();
@@ -1718,6 +1831,7 @@ async function runAgentPrimingLive(dlg) {
1718
1831
  const ensuredFbrTellaskHead = fbrTellaskHead;
1719
1832
  const perInstance = Array.from({ length: fbrEffort }, (_, idx) => idx + 1);
1720
1833
  const created = await Promise.all(perInstance.map(async (i) => {
1834
+ assertNotStopped();
1721
1835
  const instanceBody = fbrEffort > 1
1722
1836
  ? [
1723
1837
  fbrCallBody,
@@ -1727,6 +1841,7 @@ async function runAgentPrimingLive(dlg) {
1727
1841
  : 'Hint: try to provide a distinct angle vs other FBR drafts (e.g. security/constraints/verifiability/risk).',
1728
1842
  ].join('\n')
1729
1843
  : fbrCallBody;
1844
+ assertNotStopped();
1730
1845
  const sub = await dlg.withLock(async () => {
1731
1846
  return await dlg.createSubDialog(dlg.agentId, ensuredFbrTellaskHead, instanceBody, {
1732
1847
  originMemberId: dlg.agentId,
@@ -1749,10 +1864,13 @@ async function runAgentPrimingLive(dlg) {
1749
1864
  grammar: 'markdown',
1750
1865
  skipTaskdoc: true,
1751
1866
  }, true);
1867
+ assertNotStopped();
1752
1868
  const responseText = extractLastAssistantSaying(sub.msgs);
1753
1869
  return { sub, responseText };
1754
1870
  }));
1871
+ assertNotStopped();
1755
1872
  for (const r of created) {
1873
+ assertNotStopped();
1756
1874
  const responseText = r.responseText;
1757
1875
  fbrResponsesForCache.push(responseText);
1758
1876
  fbrResponsesForInjection.push({ subdialogId: r.sub.id.selfId, response: responseText });
@@ -1783,7 +1901,9 @@ async function runAgentPrimingLive(dlg) {
1783
1901
  fbrResponses: fbrResponsesForInjection,
1784
1902
  fbrTellaskHead: fbrTellaskHead,
1785
1903
  fbrCallId: fbrCallId,
1904
+ assertNotStopped,
1786
1905
  });
1906
+ assertNotStopped();
1787
1907
  const entry = {
1788
1908
  createdAt,
1789
1909
  workLanguage: language,
@@ -1844,6 +1964,17 @@ async function runAgentPrimingLive(dlg) {
1844
1964
  return entry;
1845
1965
  }
1846
1966
  catch (err) {
1967
+ if (isAgentPrimingInterruptedError(err)) {
1968
+ fatalRunState = {
1969
+ kind: 'interrupted',
1970
+ reason: { kind: err.reason },
1971
+ };
1972
+ log_1.log.info('Agent Priming live run interrupted by stop request', undefined, {
1973
+ dialogId: dlg.id.valueOf(),
1974
+ reason: err.reason,
1975
+ });
1976
+ throw err;
1977
+ }
1847
1978
  const errText = err instanceof Error ? (err.stack ?? err.message) : String(err);
1848
1979
  const errTextTrimmed = errText.trim().slice(0, 4000);
1849
1980
  fatalRunState = {
@@ -1882,20 +2013,27 @@ async function runAgentPrimingLive(dlg) {
1882
2013
  }
1883
2014
  finally {
1884
2015
  dlg.disableDiligencePush = prevDisableDiligencePush;
1885
- if (fatalRunState) {
1886
- await (0, dialog_run_state_1.setDialogRunState)(dlg.id, fatalRunState);
1887
- }
1888
- else {
1889
- let nextIdle = { kind: 'idle_waiting_user' };
1890
- try {
1891
- nextIdle = await (0, dialog_run_state_1.computeIdleRunState)(dlg);
2016
+ try {
2017
+ if (fatalRunState) {
2018
+ await (0, dialog_run_state_1.setDialogRunState)(dlg.id, fatalRunState);
1892
2019
  }
1893
- catch (err) {
1894
- log_1.log.warn('Failed to compute idle runState after Agent Priming live run; falling back', err, {
1895
- dialogId: dlg.id.valueOf(),
1896
- });
2020
+ else {
2021
+ let nextIdle = { kind: 'idle_waiting_user' };
2022
+ try {
2023
+ nextIdle = await (0, dialog_run_state_1.computeIdleRunState)(dlg);
2024
+ }
2025
+ catch (err) {
2026
+ log_1.log.warn('Failed to compute idle runState after Agent Priming live run; falling back', err, {
2027
+ dialogId: dlg.id.valueOf(),
2028
+ });
2029
+ }
2030
+ await (0, dialog_run_state_1.setDialogRunState)(dlg.id, nextIdle);
2031
+ }
2032
+ }
2033
+ finally {
2034
+ if (ownsActiveRun) {
2035
+ (0, dialog_run_state_1.clearActiveRun)(dlg.id);
1897
2036
  }
1898
- await (0, dialog_run_state_1.setDialogRunState)(dlg.id, nextIdle);
1899
2037
  }
1900
2038
  }
1901
2039
  }
@@ -0,0 +1,162 @@
1
+ # Keep-Going(鞭策)— 设计文档
2
+
3
+ ## 概述
4
+
5
+ Dominds 根对话旨在长期运行。根对话"停止"(变为空闲)通常不是操作员想要的:他们希望智能体持续推进,直到:
6
+
7
+ - 合法地暂停等待人类决策(Q4H),或
8
+ - 合法地暂停等待子对话(tellask/backfill)。
9
+
10
+ 本文档指定了一个运行时机制("keep-going"),仅针对**根/主对话**,防止对话停止:每当驱动程序即将停止时,它会自动发送一个简短的鞭策语(渲染为正常的用户气泡)并继续生成,除非对话处于合法暂停状态(Q4H 或待处理的子对话)。
11
+
12
+ ## 目标
13
+
14
+ - 防止根对话停止,除非处于合法暂停状态(Q4H / 子对话)。
15
+ - 保持行为可预测和有界(无无限循环)。
16
+ - 使鞭策语文本可按工作区(rtws)和语言配置。
17
+ - 提供清晰的用户控制的"禁用"机制。
18
+
19
+ ## 非目标
20
+
21
+ - 自动完成/自动将对话标记为完成。
22
+ - 将此行为应用于子对话(子对话保持范围,应向其调用者报告)。
23
+
24
+ ## 定义
25
+
26
+ - **根/主对话**:`RootDialog`(`dlg.id.rootId === dlg.id.selfId`),主要对话线程。
27
+ - **子对话**:`SubDialog`,为 tellask / 作用域工作创建。
28
+ - **Q4H**:"Questions for Human",通过 `!?@human` 发起,暂停对话进度直到人类响应。
29
+
30
+ ## 预期的"正常"完成路径(推荐)
31
+
32
+ 当智能体需要人类决策来结束时(例如,确认选择或决定是否将对话标记为完成),正确的路径是:
33
+
34
+ 1. 智能体发出 Q4H(`!?@human`)并提供必要的上下文和明确的决策请求。
35
+ 2. WebUI 清楚地呈现 Q4H。
36
+ 3. 人类决定并:
37
+ - 手动将根对话标记为"完成",或
38
+ - 提供请求的信息以便对话继续。
39
+
40
+ 这是"受控收敛"路径。keep-going 机制不应覆盖合法的暂停状态。
41
+
42
+ ## Keep-Going 行为("自动继续"回退)
43
+
44
+ ### 触发条件(必须全部满足)
45
+
46
+ - 对话是**根/主对话**(绝不会是子对话)。
47
+ - 对话**未暂停**:
48
+ - 没有待处理的 Q4H,并且
49
+ - 没有待处理的子对话(等待回填)。
50
+ - 驱动程序即将停止生成循环(即没有工具/函数输出需要另一次迭代)。
51
+
52
+ ### 操作
53
+
54
+ 运行时自动发送一个鞭策语(渲染为正常的用户气泡)并运行另一次生成迭代。
55
+
56
+ ### 有界性
57
+
58
+ 为避免无限循环,keep-going 有一个按对话的预算(每个成员的 `diligence-push-max`),控制对于给定对话在运行时强制 Q4H 暂停之前可以注入多少个自动继续的鞭策语。
59
+
60
+ - 默认值:**3**
61
+ - 如果 `< 1`,则该成员的 keep-going 有效禁用
62
+ - 可通过 `.minds/team.yaml` 中的 `diligence-push-max` 按成员配置
63
+
64
+ ### Q4H 时重置
65
+
66
+ 当对话因待处理的 Q4H(Questions for Human)而暂停时,keep-going 注入计数器会被重置。这确保在人类回答 Q4H 并恢复对话后,对话会获得新的 keep-going 预算。
67
+
68
+ ### 预算耗尽 → 强制 Q4H
69
+
70
+ 当 keep-going 预算耗尽时,运行时会创建一个 Q4H 条目,询问人类是否继续或停止。这将"有界性"转换为合法的暂停点,并避免无限自动继续循环。
71
+
72
+ ### 禁用开关
73
+
74
+ 可以通过以下任一方式按 rtws 禁用 keep-going:
75
+
76
+ - 如果选中的鞭策语文件存在但其内容为空/仅空白,则禁用 keep-going(不注入)。
77
+
78
+ 可以通过以下任一方式按成员禁用 keep-going:
79
+
80
+ - 如果 `diligence-push-max < 1`,则该成员的 keep-going 被禁用(不注入)。
81
+
82
+ ## 鞭策语解析
83
+
84
+ 让 `<rtws>` 为当前运行时工作区(即 `process.cwd()`)。
85
+
86
+ 解析顺序:
87
+
88
+ 1. `<rtws>/.minds/diligence.<work-lang-id>.md`(例如,`diligence.zh.md`)
89
+ 2. `<rtws>/.minds/diligence.md`(语言无关的回退)
90
+ 3. 内置回退文本(硬编码的 i18n;`zh` 是规范的并嵌入在源代码中)
91
+
92
+ 如果上述顺序中第一个存在的文件具有空/空白内容,则**禁用** keep-going。
93
+
94
+ 注意:鞭策语文件中的 YAML frontmatter 会被运行时忽略。如果存在,它被视为非内容元数据并从提示正文中剥离。
95
+
96
+ ### 团队成员上限:`diligence-push-max`
97
+
98
+ 每个团队成员可以选择通过 `.minds/team.yaml` 限制 keep-going:
99
+
100
+ ```yaml
101
+ members:
102
+ alice:
103
+ diligence-push-max: 10
104
+ ```
105
+
106
+ 规则:
107
+
108
+ - 如果缺失,`diligence-push-max` 对于该成员默认为 **3**。
109
+ - 如果 `diligence-push-max < 1`,则该成员的 keep-going 被禁用(不注入),即使鞭策语文件存在。
110
+ - 内置影子成员 `fuxi` 和 `pangu` 默认为 `diligence-push-max: 0`,除非在 team.yaml 中显式覆盖。
111
+
112
+ ## UX 备注
113
+
114
+ - Keep-going 是一个仅运行时的提示,但它应该是**可见的**:鞭策语渲染为正常的用户消息气泡(由运行时自动发送),以便操作员理解为什么会出现额外的迭代。
115
+ - 用户应该观察到智能体在仅工具操作后继续简短的跟进。
116
+ - 当智能体真正需要用户干预时,它应该使用 Q4H。Keep-going 不应试图"假装"完成。
117
+
118
+ ## 实现(后端)
119
+
120
+ ### 位置
121
+
122
+ 在 LLM 驱动程序循环中实现(`dominds/main/llm/driver.ts`),作为迭代后的小检查:
123
+
124
+ 1. 如果 `suspendForHuman` 为 true,则停止(Q4H / 子对话待处理)。
125
+ 2. 如果有任何工具反馈,则正常继续。
126
+ 3. 否则(仅限根),尝试 keep-going 自动继续:
127
+ - 如果禁用 → 正常停止。
128
+ - 如果预算耗尽 → 创建 Q4H 并停止。
129
+ - 否则 → 自动发送鞭策语并继续。
130
+
131
+ ### 消息类型
132
+
133
+ 我们将鞭策语作为自动发送的用户消息注入:
134
+
135
+ - 类型为 `prompting_msg`、角色为 `'user'` 的 `ChatMessage`
136
+
137
+ 这确保了:
138
+
139
+ - 它存在于模型上下文中
140
+ - 它作为人类消息记录持久化
141
+ - 它在聊天时间线中渲染为正常的用户气泡(像任何其他用户消息一样)
142
+
143
+ ## 可观察性
144
+
145
+ 建议的后续步骤(初始实现不需要):
146
+
147
+ - 当触发 keep-going 时添加结构化日志行,包括:
148
+ - 对话 id
149
+ - 语言
150
+ - 使用的鞭策语来源(语言特定/通用/内置/禁用)
151
+ - 为"keep-going 触发"和"keep-going 因空文件禁用"添加可选的指标计数器。
152
+
153
+ ## 测试
154
+
155
+ 回归测试应覆盖:
156
+
157
+ - 根对话:仅工具输出 → 鞭策语注入 → 继续响应
158
+ - 根对话:空助手输出 → 鞭策语注入 → 继续响应
159
+ - 子对话:无鞭策语注入
160
+ - 工作区配置:
161
+ - 当语言特定文件缺失时,`.minds/diligence.md` 被遵守
162
+ - 空的鞭策语文件禁用 keep-going