@xiangyixie/dsh-superagent 0.19.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/long-goal.js CHANGED
@@ -294,8 +294,7 @@ function validatePhaseGraph(phases) {
294
294
 
295
295
  function goalPrompt(goal, phase, index, total, dependencySummaries) {
296
296
  return [
297
- `Long-term goal: ${goal.goal}`,
298
- '',
297
+ ...(goal.consoleOwned ? [] : [`Long-term goal: ${goal.goal}`, '']),
299
298
  `This run is phase ${index + 1} of ${total}: ${phase.title}`,
300
299
  '',
301
300
  'Phase objective:',
@@ -308,9 +307,8 @@ function goalPrompt(goal, phase, index, total, dependencySummaries) {
308
307
  '',
309
308
  ]
310
309
  : []),
311
- 'Work only on this phase. Later phases depend on what this run leaves on disk, so finish with concrete artifacts,',
312
- 'not a plan for someone else to execute. The phase acceptance conditions are checked after every round; a round',
313
- 'that does not meet them is retried with the exact failure instead of repeating the same work.',
310
+ 'Work only on this phase and provide its finished deliverables in the format requested in the objective.',
311
+ 'The phase acceptance conditions are checked after every round. If a round falls short, use the exact failure evidence to address it.',
314
312
  ].join('\n')
315
313
  }
316
314
 
@@ -333,6 +331,7 @@ function decomposePrompt(goal, maxPhases) {
333
331
  'Rules:',
334
332
  '- Use 2 to ' + maxPhases + ' phases, ordered; ids are p1, p2, ...',
335
333
  '- "dependsOn" only when the later phase needs an earlier phase\'s artifact. Independent phases may run at once.',
334
+ '- Write self-contained phase objectives. Carry the user’s relevant inputs, paths, output format and shared constraints into each affected objective. Leave other phases’ deliverables out.',
336
335
  '- Every phase needs at least one acceptance condition. Prefer a machine check (file exists, command exits 0);',
337
336
  ' a condition without a check is judged by an independent acceptance model from the phase report.',
338
337
  '- Never create a dependency cycle. Never reference a phase id that is not in the list.',
@@ -652,7 +651,7 @@ export function createLongGoalManager({
652
651
  muted: true,
653
652
  goalMode: null,
654
653
  overrides: { deadlineMinutes: 15, maxSubtasks: 2, maxParallel: 1 },
655
- origin: { kind: 'long-goal', goalId: goal.id, role: 'decompose', attempt },
654
+ origin: { kind: 'long-goal', goalId: goal.id, role: 'decompose', attempt, dispatchId: `${goal.id}/plan/${attempt}` },
656
655
  })
657
656
  goal.decomposeRunId = run.id
658
657
  event(goal, 'decompose.started', `attempt ${attempt}: run ${run.id}`)
@@ -965,7 +964,10 @@ export function createLongGoalManager({
965
964
  ...(budget.maxModelCalls ? { maxModelCalls: budget.maxModelCalls } : {}),
966
965
  ...(budget.maxTokens ? { maxTokens: budget.maxTokens } : {}),
967
966
  },
968
- origin: { kind: 'long-goal', goalId: goal.id, phaseId: phase.id, attempt: phase.attempts + 1 },
967
+ origin: {
968
+ kind: 'long-goal', goalId: goal.id, phaseId: phase.id, attempt: phase.attempts + 1,
969
+ dispatchId: `${goal.id}/phase/${phase.id}/${(phase.runIds?.length ?? 0) + 1}`,
970
+ },
969
971
  })
970
972
  phase.attempts += 1
971
973
  phase.runIds = Array.isArray(phase.runIds) ? phase.runIds : []
@@ -1122,7 +1124,15 @@ export function createLongGoalManager({
1122
1124
  if (!release) return
1123
1125
  try {
1124
1126
  const goal = await store.read(goalId)
1125
- if (!goal || !GOAL_TICKABLE.has(goal.status)) return
1127
+ if (!goal) return
1128
+ if (goal.status === 'paused' && goal.pausedReason === 'owner-unavailable') {
1129
+ if (!await owner(goal)) return
1130
+ goal.status = goal.phases.length ? 'running' : 'planning'
1131
+ goal.pausedReason = null
1132
+ event(goal, 'goal.resumed', 'the owning session is available again')
1133
+ await store.write(goal)
1134
+ }
1135
+ if (!GOAL_TICKABLE.has(goal.status)) return
1126
1136
  await refreshUsage(goal)
1127
1137
 
1128
1138
  if (goal.status === 'needs-decision') {
@@ -1292,7 +1302,7 @@ export function createLongGoalManager({
1292
1302
  }
1293
1303
  }
1294
1304
  const after = await store.list()
1295
- if (after.some(goal => GOAL_ACTIVE.has(goal.status) || watching(goal))) arm()
1305
+ if (after.some(autoResumeFor)) arm()
1296
1306
  return processed
1297
1307
  })()
1298
1308
  try {
@@ -1312,7 +1322,7 @@ export function createLongGoalManager({
1312
1322
  if (!goal?.autoResume) return false
1313
1323
  if (GOAL_ACTIVE.has(goal.status)) return true
1314
1324
  if (watching(goal)) return true
1315
- return goal.status === 'paused' && goal.pausedReason !== 'manual'
1325
+ return goal.status === 'paused' && goal.pausedReason === 'owner-unavailable'
1316
1326
  }
1317
1327
 
1318
1328
  /** Load goals and mark work left active by a dead host as resumable, not failed. */
@@ -1443,6 +1453,7 @@ export function createLongGoalManager({
1443
1453
  startedAt: phases.length > 0 ? new Date(now()).toISOString() : null,
1444
1454
  finishedAt: null,
1445
1455
  ownerSessionId: String(input.parentSessionId ?? input.ownerSessionId ?? '').trim() || null,
1456
+ ...(input.consoleOwned === true ? { consoleOwned: true } : {}),
1446
1457
  cwd,
1447
1458
  autoResume: input.autoResume !== false,
1448
1459
  ...limits,
@@ -1481,6 +1492,19 @@ export function createLongGoalManager({
1481
1492
  return goal
1482
1493
  }
1483
1494
 
1495
+ async function changeGoal(goalId, change) {
1496
+ if (!GOAL_ID_PATTERN.test(String(goalId ?? ''))) throw new Error(`unknown long goal ${goalId}`)
1497
+ const release = await acquireFileLock(store.file(goalId).replace(/\.json$/, '.lock'), {
1498
+ staleMs: 60000, now, retries: 20,
1499
+ })
1500
+ if (!release) throw new Error(`goal ${goalId} is being updated; try again shortly`)
1501
+ try {
1502
+ return await change()
1503
+ } finally {
1504
+ await release()
1505
+ }
1506
+ }
1507
+
1484
1508
  async function status(goalId) {
1485
1509
  const goal = await get(goalId)
1486
1510
  const runs = {}
@@ -1752,11 +1776,11 @@ export function createLongGoalManager({
1752
1776
  status,
1753
1777
  list,
1754
1778
  tick,
1755
- answer,
1756
- resume,
1757
- pause,
1758
- cancel,
1759
- retryPhase,
1779
+ answer: (id, ...args) => changeGoal(id, () => answer(id, ...args)),
1780
+ resume: id => changeGoal(id, () => resume(id)),
1781
+ pause: id => changeGoal(id, () => pause(id)),
1782
+ cancel: id => changeGoal(id, () => cancel(id)),
1783
+ retryPhase: (id, phaseId) => changeGoal(id, () => retryPhase(id, phaseId)),
1760
1784
  store,
1761
1785
  formatStatus,
1762
1786
  }
@@ -1183,7 +1183,7 @@ function aggregatorPrompt(run, completed, context, carried = [], wantUserMemory
1183
1183
  function deterministicJoin(run) {
1184
1184
  const completed = (run.subtasks ?? []).filter(subtask => subtask.status === 'completed')
1185
1185
  if (completed.length === 0) return null
1186
- const parts = [`# ${run.goal}`, '', `_Deterministic merge of ${completed.length} subtask report(s)._`, '']
1186
+ const parts = run.goalMode ? [] : [`# ${run.goal}`, '', `_Deterministic merge of ${completed.length} subtask report(s)._`, '']
1187
1187
  for (const subtask of completed) {
1188
1188
  parts.push(`## ${subtask.title}`, '', subtask.result ?? '', '')
1189
1189
  }
@@ -2515,7 +2515,7 @@ export function createOrchestrator(deps) {
2515
2515
  }
2516
2516
  }
2517
2517
  let degraded = null
2518
- if (unsupported && entry.targets.every(target => target.status === 'failed')) {
2518
+ if (options.fallbackToFollowup !== false && unsupported && entry.targets.every(target => target.status === 'failed')) {
2519
2519
  // No live channel at all. Do not pretend the instruction was delivered:
2520
2520
  // queue it exactly where superagent_followup would, and label the
2521
2521
  // receipt as degraded so status cannot read as a live delivery.
@@ -2708,6 +2708,7 @@ export function createOrchestrator(deps) {
2708
2708
  for (const subtask of run.subtasks ?? []) {
2709
2709
  if (subtask.status === 'running') subtask.status = 'pending'
2710
2710
  }
2711
+ markPendingInjectionsMissed(run, 'the host stopped before the worker read the instruction')
2711
2712
  // The owner died before it could settle, so publish what is already known
2712
2713
  // about the spend: a reclaimed run must not claim its cost is unknown when
2713
2714
  // its child logs were readable.
@@ -2730,6 +2731,19 @@ export function createOrchestrator(deps) {
2730
2731
  }
2731
2732
  }
2732
2733
 
2734
+ function markPendingInjectionsMissed(run, reason) {
2735
+ let changed = false
2736
+ for (const injection of run.injections ?? []) {
2737
+ for (const target of injection.targets ?? []) {
2738
+ if (target.status !== 'pending') continue
2739
+ stopWatch(run.id, injection.id, target.childId)
2740
+ finishInjectionTarget(run, target, 'missed', { reason })
2741
+ changed = true
2742
+ }
2743
+ }
2744
+ if (changed) refreshInjectionStats(run)
2745
+ }
2746
+
2733
2747
  /**
2734
2748
  * Claim the exclusive right to schedule a run.
2735
2749
  *
@@ -3813,7 +3827,10 @@ export function createOrchestrator(deps) {
3813
3827
  // since added an instruction or answered a question: the shortcut would
3814
3828
  // return the old text and silently drop the decision.
3815
3829
  const hasInstructions = (run.followups ?? []).length > 0
3816
- if (completed.length === 1 && !hasInstructions) return `# ${run.goal}\n\n${completed[0].result ?? ''}`.trim()
3830
+ if (completed.length === 1 && !hasInstructions) {
3831
+ const result = completed[0].result ?? ''
3832
+ return run.goalMode ? result.trim() : `# ${run.goal}\n\n${result}`.trim()
3833
+ }
3817
3834
 
3818
3835
  try {
3819
3836
  // Instructions that arrived while the workers ran land here, and this is
@@ -3935,18 +3952,7 @@ export function createOrchestrator(deps) {
3935
3952
 
3936
3953
  // A live injection has no boundary of its own: once the run is over, an
3937
3954
  // untouched receipt is a miss, and the queue that carried it is gone.
3938
- let injectionsChanged = false
3939
- for (const injection of run.injections ?? []) {
3940
- for (const target of injection.targets ?? []) {
3941
- if (target.status !== 'pending') continue
3942
- stopWatch(run.id, injection.id, target.childId)
3943
- finishInjectionTarget(run, target, 'missed', {
3944
- reason: 'the run settled before the child reached another step boundary',
3945
- })
3946
- injectionsChanged = true
3947
- }
3948
- }
3949
- if (injectionsChanged) refreshInjectionStats(run)
3955
+ markPendingInjectionsMissed(run, 'the run settled before the child reached another step boundary')
3950
3956
 
3951
3957
  // The aggregator appends a fenced notebook block to its report. Strip it in
3952
3958
  // every case so the user never sees plugin syntax, then persist it when the
@@ -4951,6 +4957,10 @@ export function createOrchestrator(deps) {
4951
4957
  // The goal claim is held across "list, check, write" so two processes
4952
4958
  // sharing a state directory cannot both conclude that nothing is running.
4953
4959
  return serialize(() => withGoalGuard(dedupeKey, async () => {
4960
+ if (origin?.kind === 'long-goal' && origin.dispatchId) {
4961
+ const existing = (await store.list()).find(run => run.origin?.dispatchId === origin.dispatchId)
4962
+ if (existing) return existing
4963
+ }
4954
4964
  const active = await activeCount()
4955
4965
  // The same goal twice is much more often a retry than a second piece of
4956
4966
  // work: the caller lost the run id, or a model re-issued the call. Return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiangyixie/dsh-superagent",
3
- "version": "0.19.0",
3
+ "version": "0.22.0",
4
4
  "description": "长周期自主执行插件:一个目标拆成可并行的子任务,交付一份合并报告,重启后仍可继续。",
5
5
  "author": "Admire (LittleChenLiya)",
6
6
  "type": "module",