@gonovus-ai/spec-flow-cli 1.3.1 → 1.4.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.
- package/README.md +17 -3
- package/dist/cli.js +113 -22
- package/dist/cli.js.map +1 -1
- package/dist/commands/work-item-output.d.ts +18 -0
- package/dist/discuss-commands.d.ts +3 -0
- package/dist/discuss-commands.js +763 -0
- package/dist/discuss-commands.js.map +1 -0
- package/dist/member-references.d.ts +28 -0
- package/dist/member-references.js +94 -0
- package/dist/member-references.js.map +1 -0
- package/dist/runner/adapters/codex-app-server.d.ts +14 -1
- package/dist/runner/adapters/codex-app-server.js +541 -292
- package/dist/runner/adapters/codex-app-server.js.map +1 -1
- package/dist/runner/adapters/exec-adapter.js +31 -11
- package/dist/runner/adapters/exec-adapter.js.map +1 -1
- package/dist/runner/orchestrator.d.ts +10 -1
- package/dist/runner/orchestrator.js +438 -8
- package/dist/runner/orchestrator.js.map +1 -1
- package/dist/runner/redact.d.ts +1 -0
- package/dist/runner/redact.js +1 -0
- package/dist/runner/redact.js.map +1 -1
- package/dist/runner/state-store.d.ts +4 -1
- package/dist/runner/state-store.js +61 -1
- package/dist/runner/state-store.js.map +1 -1
- package/dist/runner/telemetry-bridge.d.ts +24 -2
- package/dist/runner/telemetry-bridge.js +168 -4
- package/dist/runner/telemetry-bridge.js.map +1 -1
- package/dist/runner/types.d.ts +70 -2
- package/dist/runner/types.js +13 -1
- package/dist/runner/types.js.map +1 -1
- package/dist/runner/workflow.d.ts +5 -1
- package/dist/runner/workflow.js +58 -0
- package/dist/runner/workflow.js.map +1 -1
- package/dist/work-item-delivery-commands.js +1 -2
- package/dist/work-item-delivery-commands.js.map +1 -1
- package/guides/agent-sessions.md +8 -2
- package/guides/delivery.md +1 -1
- package/guides/discuss.md +72 -0
- package/guides/search.md +4 -2
- package/guides/work-items.md +58 -4
- package/guides/workspace-admin.md +4 -0
- package/package.json +1 -1
|
@@ -4,10 +4,12 @@ import { CodexAppServerAdapter } from "./adapters/codex-app-server.js";
|
|
|
4
4
|
import { CustomExecAdapter } from "./adapters/exec-adapter.js";
|
|
5
5
|
import { raceWithTimeout } from "./process-utils.js";
|
|
6
6
|
import { buildSanitizedChildEnv, sanitizeAndClamp } from "./redact.js";
|
|
7
|
-
import {
|
|
7
|
+
import { isBoardAgentEvent, isWorkItemAgentEvent, } from "./types.js";
|
|
8
|
+
import { loadWorkflowIfPresent, renderDiscussionPrompt, renderWorkItemPrompt } from "./workflow.js";
|
|
8
9
|
import { prepareWorkItemWorkspace, resolveEffectiveWorkspaceMode, runLifecycleHook, summarizeWorkspaceChanges, } from "./workspace-manager.js";
|
|
9
10
|
const MAX_SESSION_EVENT_PAGES = 50;
|
|
10
11
|
const SESSION_EVENTS_PAGE_SIZE = 200;
|
|
12
|
+
const MIN_DISCUSSION_CONCURRENCY = 4;
|
|
11
13
|
const EMPTY_HOOKS = { timeoutMs: 60_000 };
|
|
12
14
|
function hasExecutableWorkflowFields(wf) {
|
|
13
15
|
return (wf.runner !== undefined ||
|
|
@@ -43,7 +45,7 @@ export function isSessionControlEventType(eventType) {
|
|
|
43
45
|
export function createDefaultRunnerAdapter(config, cliVersion) {
|
|
44
46
|
switch (config.runner) {
|
|
45
47
|
case "codex":
|
|
46
|
-
return new CodexAppServerAdapter(config.codexCommand, cliVersion);
|
|
48
|
+
return new CodexAppServerAdapter(config.codexCommand, cliVersion, { persistent: true });
|
|
47
49
|
case "claude":
|
|
48
50
|
return new ClaudeStreamAdapter(config.claudeCommand);
|
|
49
51
|
case "acp":
|
|
@@ -63,6 +65,11 @@ export class AgentRunOrchestrator {
|
|
|
63
65
|
activeByWorkItem = new Map();
|
|
64
66
|
activeBySession = new Map();
|
|
65
67
|
pendingQueue = [];
|
|
68
|
+
activeByDiscussionThread = new Map();
|
|
69
|
+
pendingDiscussionQueue = [];
|
|
70
|
+
inFlightDiscussionMessageKeys = new Set();
|
|
71
|
+
cachedAdapter = null;
|
|
72
|
+
createdAdapters = new Set();
|
|
66
73
|
workflow = null;
|
|
67
74
|
frozenExecutableWorkflow = null;
|
|
68
75
|
freezeExecutableOnReload = false;
|
|
@@ -73,6 +80,25 @@ export class AgentRunOrchestrator {
|
|
|
73
80
|
constructor(deps) {
|
|
74
81
|
this.deps = deps;
|
|
75
82
|
}
|
|
83
|
+
getOrCreateAdapter(cfg) {
|
|
84
|
+
const key = JSON.stringify({
|
|
85
|
+
runner: cfg.runner,
|
|
86
|
+
codexCommand: cfg.codexCommand,
|
|
87
|
+
claudeCommand: cfg.claudeCommand,
|
|
88
|
+
acpCommand: cfg.acpCommand ?? null,
|
|
89
|
+
execCommand: cfg.execCommand ?? null,
|
|
90
|
+
extraArgs: cfg.extraArgs,
|
|
91
|
+
});
|
|
92
|
+
if (this.cachedAdapter !== null && this.cachedAdapter.key === key) {
|
|
93
|
+
return this.cachedAdapter.adapter;
|
|
94
|
+
}
|
|
95
|
+
const adapter = this.deps.adapterFactory(cfg);
|
|
96
|
+
if (adapter.dispose !== undefined) {
|
|
97
|
+
this.cachedAdapter = { key, adapter };
|
|
98
|
+
this.createdAdapters.add(adapter);
|
|
99
|
+
}
|
|
100
|
+
return adapter;
|
|
101
|
+
}
|
|
76
102
|
now() {
|
|
77
103
|
return this.deps.now?.() ?? Date.now();
|
|
78
104
|
}
|
|
@@ -130,7 +156,9 @@ export class AgentRunOrchestrator {
|
|
|
130
156
|
};
|
|
131
157
|
return this.workflow;
|
|
132
158
|
}
|
|
133
|
-
if (!isInitialLoad &&
|
|
159
|
+
if (!isInitialLoad &&
|
|
160
|
+
this.freezeExecutableOnReload &&
|
|
161
|
+
this.frozenExecutableWorkflow !== null) {
|
|
134
162
|
const frozen = this.frozenExecutableWorkflow;
|
|
135
163
|
this.workflow = {
|
|
136
164
|
sourcePath: loaded.sourcePath,
|
|
@@ -270,7 +298,10 @@ export class AgentRunOrchestrator {
|
|
|
270
298
|
}
|
|
271
299
|
}
|
|
272
300
|
hasActiveWork() {
|
|
273
|
-
return this.activeByWorkItem.size > 0 ||
|
|
301
|
+
return (this.activeByWorkItem.size > 0 ||
|
|
302
|
+
this.pendingQueue.length > 0 ||
|
|
303
|
+
this.activeByDiscussionThread.size > 0 ||
|
|
304
|
+
this.pendingDiscussionQueue.length > 0);
|
|
274
305
|
}
|
|
275
306
|
hasPendingWaiters() {
|
|
276
307
|
for (const run of this.activeByWorkItem.values()) {
|
|
@@ -287,6 +318,16 @@ export class AgentRunOrchestrator {
|
|
|
287
318
|
minSeq = item.event.agentSeq;
|
|
288
319
|
}
|
|
289
320
|
}
|
|
321
|
+
for (const item of this.pendingDiscussionQueue) {
|
|
322
|
+
if (minSeq === null || item.event.agentSeq < minSeq) {
|
|
323
|
+
minSeq = item.event.agentSeq;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
for (const run of this.activeByDiscussionThread.values()) {
|
|
327
|
+
if (minSeq === null || run.earliestAgentSeq < minSeq) {
|
|
328
|
+
minSeq = run.earliestAgentSeq;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
290
331
|
return minSeq;
|
|
291
332
|
}
|
|
292
333
|
async handleEvent(event) {
|
|
@@ -299,9 +340,19 @@ export class AgentRunOrchestrator {
|
|
|
299
340
|
eventType: event.eventType,
|
|
300
341
|
workItemKey: event.workItemKey,
|
|
301
342
|
sessionId: event.sessionId,
|
|
343
|
+
...(event.channelId === null ? {} : { channelId: event.channelId }),
|
|
344
|
+
...(event.threadId === null ? {} : { threadId: event.threadId }),
|
|
345
|
+
...(event.messageId === null ? {} : { messageId: event.messageId }),
|
|
302
346
|
});
|
|
303
347
|
return;
|
|
304
348
|
}
|
|
349
|
+
if (isBoardAgentEvent(event)) {
|
|
350
|
+
await this.onDiscussionEvent(event);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (!isWorkItemAgentEvent(event)) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
305
356
|
switch (event.eventType) {
|
|
306
357
|
case "work_item.assigned":
|
|
307
358
|
await this.onAssigned(event);
|
|
@@ -327,6 +378,26 @@ export class AgentRunOrchestrator {
|
|
|
327
378
|
break;
|
|
328
379
|
}
|
|
329
380
|
}
|
|
381
|
+
async onDiscussionEvent(event) {
|
|
382
|
+
// Event-level guard: never reply to messages authored by this same Agent.
|
|
383
|
+
if (event.actorAgentId !== null && event.actorAgentId === event.targetAgentId) {
|
|
384
|
+
this.deps.onDiagnostic(`Skipping self-authored discussion event on thread ${event.threadId} (message ${event.messageId}).`);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
// Persisted deduplication guard across restarts and redeliveries.
|
|
388
|
+
if (this.deps.stateStore.hasHandledDiscussionMessage(event.threadId, event.messageId, event.agentSeq)) {
|
|
389
|
+
this.deps.onDiagnostic(`Skipping already-handled discussion message ${event.messageId} on thread ${event.threadId} (seq ${String(event.agentSeq)}).`);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
const dedupKey = `${event.threadId}:${event.messageId}`;
|
|
393
|
+
if (this.inFlightDiscussionMessageKeys.has(dedupKey)) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
this.inFlightDiscussionMessageKeys.add(dedupKey);
|
|
397
|
+
this.pendingDiscussionQueue.push({ event });
|
|
398
|
+
this.deps.onQueueChanged?.();
|
|
399
|
+
await this.drainQueue();
|
|
400
|
+
}
|
|
330
401
|
async onAssigned(event) {
|
|
331
402
|
const existing = this.activeByWorkItem.get(event.workItemId);
|
|
332
403
|
if (existing !== undefined) {
|
|
@@ -596,10 +667,314 @@ export class AgentRunOrchestrator {
|
|
|
596
667
|
dequeuedAny = true;
|
|
597
668
|
this.launchDispatch(dispatch, cfg);
|
|
598
669
|
}
|
|
670
|
+
const discussionConcurrency = Math.max(cfg.concurrency, MIN_DISCUSSION_CONCURRENCY);
|
|
671
|
+
while (this.activeByDiscussionThread.size < discussionConcurrency &&
|
|
672
|
+
this.pendingDiscussionQueue.length > 0) {
|
|
673
|
+
const nextIndex = this.pendingDiscussionQueue.findIndex((item) => !this.activeByDiscussionThread.has(item.event.threadId));
|
|
674
|
+
if (nextIndex === -1)
|
|
675
|
+
break;
|
|
676
|
+
const first = this.pendingDiscussionQueue[nextIndex];
|
|
677
|
+
if (first === undefined)
|
|
678
|
+
break;
|
|
679
|
+
const targetThreadId = first.event.threadId;
|
|
680
|
+
// Coalesce all currently queued events for this same discussion thread into one turn,
|
|
681
|
+
// preserving chronological order so follow-up messages are answered coherently.
|
|
682
|
+
const mergedEvents = [];
|
|
683
|
+
for (let i = 0; i < this.pendingDiscussionQueue.length;) {
|
|
684
|
+
const candidate = this.pendingDiscussionQueue[i];
|
|
685
|
+
if (candidate?.event.threadId === targetThreadId) {
|
|
686
|
+
this.pendingDiscussionQueue.splice(i, 1);
|
|
687
|
+
mergedEvents.push(candidate.event);
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
i += 1;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
if (mergedEvents.length === 0)
|
|
694
|
+
break;
|
|
695
|
+
dequeuedAny = true;
|
|
696
|
+
this.launchDiscussionDispatch(mergedEvents, cfg);
|
|
697
|
+
}
|
|
599
698
|
if (dequeuedAny) {
|
|
600
699
|
this.deps.onQueueChanged?.();
|
|
601
700
|
}
|
|
602
701
|
}
|
|
702
|
+
launchDiscussionDispatch(mergedEvents, cfg) {
|
|
703
|
+
const triggerEvent = mergedEvents[mergedEvents.length - 1];
|
|
704
|
+
if (triggerEvent === undefined)
|
|
705
|
+
return;
|
|
706
|
+
let earliestSeq = triggerEvent.agentSeq;
|
|
707
|
+
for (const ev of mergedEvents) {
|
|
708
|
+
if (ev.agentSeq < earliestSeq)
|
|
709
|
+
earliestSeq = ev.agentSeq;
|
|
710
|
+
}
|
|
711
|
+
let resolveRunDone;
|
|
712
|
+
const completionPromise = new Promise((resolve) => {
|
|
713
|
+
resolveRunDone = resolve;
|
|
714
|
+
});
|
|
715
|
+
const activeRun = {
|
|
716
|
+
threadId: triggerEvent.threadId,
|
|
717
|
+
channelId: triggerEvent.channelId,
|
|
718
|
+
earliestAgentSeq: earliestSeq,
|
|
719
|
+
runnerThreadId: undefined,
|
|
720
|
+
handle: null,
|
|
721
|
+
lastActivityAt: this.now(),
|
|
722
|
+
stopRequested: false,
|
|
723
|
+
stopReason: null,
|
|
724
|
+
completionPromise,
|
|
725
|
+
};
|
|
726
|
+
this.activeByDiscussionThread.set(triggerEvent.threadId, activeRun);
|
|
727
|
+
void this.executeDiscussionRun(mergedEvents, cfg, activeRun).finally(() => {
|
|
728
|
+
this.activeByDiscussionThread.delete(triggerEvent.threadId);
|
|
729
|
+
for (const ev of mergedEvents) {
|
|
730
|
+
this.inFlightDiscussionMessageKeys.delete(`${ev.threadId}:${ev.messageId}`);
|
|
731
|
+
}
|
|
732
|
+
resolveRunDone();
|
|
733
|
+
this.deps.onQueueChanged?.();
|
|
734
|
+
void this.drainQueue();
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
async executeDiscussionRun(mergedEvents, cfg, run) {
|
|
738
|
+
const triggerEvent = mergedEvents[mergedEvents.length - 1];
|
|
739
|
+
if (triggerEvent === undefined)
|
|
740
|
+
return;
|
|
741
|
+
const allTriggerMessageIds = mergedEvents.map((e) => e.messageId);
|
|
742
|
+
const maxAgentSeq = mergedEvents.reduce((max, ev) => Math.max(max, ev.agentSeq), 0);
|
|
743
|
+
const persistDiscussion = async (options) => {
|
|
744
|
+
const current = this.deps.stateStore.getDiscussion(triggerEvent.threadId);
|
|
745
|
+
const nextHandledIds = [...(current?.handledMessageIds ?? [])];
|
|
746
|
+
if (options.markMessagesHandled) {
|
|
747
|
+
for (const id of allTriggerMessageIds) {
|
|
748
|
+
nextHandledIds.push(id);
|
|
749
|
+
}
|
|
750
|
+
if (options.replyMessageId !== undefined && options.replyMessageId !== null) {
|
|
751
|
+
nextHandledIds.push(options.replyMessageId);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
await this.deps.stateStore.upsertDiscussion({
|
|
755
|
+
threadId: triggerEvent.threadId,
|
|
756
|
+
channelId: triggerEvent.channelId,
|
|
757
|
+
runnerKind: cfg.runner,
|
|
758
|
+
runnerThreadId: options.runnerThreadId !== undefined
|
|
759
|
+
? options.runnerThreadId
|
|
760
|
+
: (run.runnerThreadId ?? current?.runnerThreadId ?? null),
|
|
761
|
+
lastHandledMessageId: options.markMessagesHandled
|
|
762
|
+
? triggerEvent.messageId
|
|
763
|
+
: (current?.lastHandledMessageId ?? null),
|
|
764
|
+
lastRepliedMessageId: options.replyMessageId ?? current?.lastRepliedMessageId ?? null,
|
|
765
|
+
lastHandledAgentSeq: options.markMessagesHandled
|
|
766
|
+
? Math.max(current?.lastHandledAgentSeq ?? 0, maxAgentSeq)
|
|
767
|
+
: (current?.lastHandledAgentSeq ?? 0),
|
|
768
|
+
handledMessageIds: nextHandledIds,
|
|
769
|
+
updatedAt: new Date().toISOString(),
|
|
770
|
+
});
|
|
771
|
+
};
|
|
772
|
+
try {
|
|
773
|
+
const persisted = this.deps.stateStore.getDiscussion(triggerEvent.threadId);
|
|
774
|
+
const context = await this.deps.telemetry.fetchDiscussionThreadContext(triggerEvent, persisted?.lastHandledMessageId);
|
|
775
|
+
if (context === null) {
|
|
776
|
+
this.deps.onDiagnostic(`Discussion thread ${triggerEvent.threadId} is no longer accessible (404); skipping.`);
|
|
777
|
+
await persistDiscussion({ markMessagesHandled: true });
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
const triggerMsg = context.triggeringMessage;
|
|
781
|
+
if (triggerMsg === null ||
|
|
782
|
+
triggerMsg.deleted ||
|
|
783
|
+
triggerMsg.isSystemNotice ||
|
|
784
|
+
triggerMsg.authorAgentId === triggerEvent.targetAgentId) {
|
|
785
|
+
this.deps.onDiagnostic(`Skipping non-actionable or self-authored discussion message ${triggerEvent.messageId} on thread ${triggerEvent.threadId}.`);
|
|
786
|
+
await persistDiscussion({ markMessagesHandled: true });
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
const reusableThreadId = persisted?.runnerKind === cfg.runner && persisted.runnerThreadId !== null
|
|
790
|
+
? persisted.runnerThreadId
|
|
791
|
+
: undefined;
|
|
792
|
+
run.runnerThreadId = reusableThreadId;
|
|
793
|
+
const isResumedSession = reusableThreadId !== undefined;
|
|
794
|
+
const triggerIdSet = new Set(allTriggerMessageIds);
|
|
795
|
+
const newMessages = selectNewDiscussionMessages(context.messages, persisted?.lastHandledMessageId ?? null, triggerEvent.targetAgentId, triggerIdSet, triggerMsg);
|
|
796
|
+
const prompt = renderDiscussionPrompt(context, {
|
|
797
|
+
isResumedSession,
|
|
798
|
+
newMessages,
|
|
799
|
+
});
|
|
800
|
+
this.deps.emitStatus({
|
|
801
|
+
runnerEvent: "discussion_started",
|
|
802
|
+
channelId: context.channelId,
|
|
803
|
+
threadId: context.threadId,
|
|
804
|
+
messageId: triggerEvent.messageId,
|
|
805
|
+
runner: cfg.runner,
|
|
806
|
+
resumed: isResumedSession,
|
|
807
|
+
});
|
|
808
|
+
const adapter = this.getOrCreateAdapter(cfg);
|
|
809
|
+
const handle = await adapter.startTurn({
|
|
810
|
+
targetKind: "discussion",
|
|
811
|
+
sessionId: `discussion-${context.threadId}-${triggerEvent.messageId}`,
|
|
812
|
+
workItemKey: `discussion:${context.threadId}`,
|
|
813
|
+
workItemTitle: context.threadTitle,
|
|
814
|
+
discussion: {
|
|
815
|
+
workspaceId: context.workspaceId,
|
|
816
|
+
channelId: context.channelId,
|
|
817
|
+
channelKind: context.channelKind,
|
|
818
|
+
channelName: context.channelName,
|
|
819
|
+
threadId: context.threadId,
|
|
820
|
+
threadTitle: context.threadTitle,
|
|
821
|
+
messageId: triggerEvent.messageId,
|
|
822
|
+
},
|
|
823
|
+
cwd: cfg.cwd,
|
|
824
|
+
prompt,
|
|
825
|
+
...(reusableThreadId === undefined ? {} : { resumeThreadId: reusableThreadId }),
|
|
826
|
+
...(cfg.model === undefined ? {} : { model: cfg.model }),
|
|
827
|
+
sandbox: "read-only",
|
|
828
|
+
approval: "never",
|
|
829
|
+
extraArgs: cfg.extraArgs,
|
|
830
|
+
environment: this.deps.environment,
|
|
831
|
+
event: triggerEvent,
|
|
832
|
+
callbacks: {
|
|
833
|
+
onThreadId: (tid) => {
|
|
834
|
+
run.runnerThreadId = tid;
|
|
835
|
+
void persistDiscussion({ runnerThreadId: tid, markMessagesHandled: false });
|
|
836
|
+
},
|
|
837
|
+
onPlan: () => {
|
|
838
|
+
run.lastActivityAt = this.now();
|
|
839
|
+
return Promise.resolve();
|
|
840
|
+
},
|
|
841
|
+
onThought: () => {
|
|
842
|
+
run.lastActivityAt = this.now();
|
|
843
|
+
return Promise.resolve();
|
|
844
|
+
},
|
|
845
|
+
onStep: () => {
|
|
846
|
+
run.lastActivityAt = this.now();
|
|
847
|
+
return Promise.resolve();
|
|
848
|
+
},
|
|
849
|
+
onAsk: () => Promise.resolve({
|
|
850
|
+
elicitationId: "",
|
|
851
|
+
selectedOptionId: null,
|
|
852
|
+
selectedOptionLabel: null,
|
|
853
|
+
freeText: null,
|
|
854
|
+
cancelled: true,
|
|
855
|
+
}),
|
|
856
|
+
onAuthRequired: () => Promise.resolve(false),
|
|
857
|
+
onActivity: () => {
|
|
858
|
+
run.lastActivityAt = this.now();
|
|
859
|
+
},
|
|
860
|
+
onDiagnostic: this.deps.onDiagnostic,
|
|
861
|
+
},
|
|
862
|
+
});
|
|
863
|
+
run.handle = handle;
|
|
864
|
+
if (run.stopRequested || this.shuttingDown) {
|
|
865
|
+
if (run.stopReason === "stall_timeout") {
|
|
866
|
+
await handle.kill().catch(() => undefined);
|
|
867
|
+
}
|
|
868
|
+
else {
|
|
869
|
+
await handle.stop().catch(() => undefined);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
const result = await handle.completion;
|
|
873
|
+
run.handle = null;
|
|
874
|
+
if (result.runnerThreadId !== undefined) {
|
|
875
|
+
run.runnerThreadId = result.runnerThreadId;
|
|
876
|
+
}
|
|
877
|
+
if (run.stopRequested || result.outcome === "interrupted") {
|
|
878
|
+
const markHandledOnStop = run.stopReason === "stall_timeout";
|
|
879
|
+
await persistDiscussion({
|
|
880
|
+
runnerThreadId: run.runnerThreadId ?? null,
|
|
881
|
+
markMessagesHandled: markHandledOnStop,
|
|
882
|
+
});
|
|
883
|
+
this.deps.emitStatus({
|
|
884
|
+
runnerEvent: "discussion_interrupted",
|
|
885
|
+
channelId: context.channelId,
|
|
886
|
+
threadId: context.threadId,
|
|
887
|
+
messageId: triggerEvent.messageId,
|
|
888
|
+
reason: run.stopReason ?? "interrupted",
|
|
889
|
+
});
|
|
890
|
+
return;
|
|
891
|
+
}
|
|
892
|
+
if (result.outcome === "failed") {
|
|
893
|
+
const message = result.errorMessage ?? "Runner discussion execution failed";
|
|
894
|
+
await persistDiscussion({
|
|
895
|
+
runnerThreadId: run.runnerThreadId ?? null,
|
|
896
|
+
markMessagesHandled: false,
|
|
897
|
+
});
|
|
898
|
+
this.deps.emitStatus({
|
|
899
|
+
runnerEvent: "discussion_failed",
|
|
900
|
+
channelId: context.channelId,
|
|
901
|
+
threadId: context.threadId,
|
|
902
|
+
messageId: triggerEvent.messageId,
|
|
903
|
+
errorMessage: sanitizeAndClamp(message, 300),
|
|
904
|
+
});
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
const finalText = result.finalMessage.trim();
|
|
908
|
+
if (finalText.length === 0) {
|
|
909
|
+
this.deps.onDiagnostic(`Discussion turn on thread ${context.threadId} completed with empty output; skipping reply.`);
|
|
910
|
+
await persistDiscussion({
|
|
911
|
+
runnerThreadId: run.runnerThreadId ?? null,
|
|
912
|
+
markMessagesHandled: true,
|
|
913
|
+
});
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
const posted = await this.deps.telemetry.postDiscussionReply({
|
|
917
|
+
workspaceId: context.workspaceId,
|
|
918
|
+
threadId: context.threadId,
|
|
919
|
+
targetAgentId: triggerEvent.targetAgentId,
|
|
920
|
+
triggerMessageId: triggerEvent.messageId,
|
|
921
|
+
bodyMarkdown: finalText,
|
|
922
|
+
});
|
|
923
|
+
if (posted.kind === "ok") {
|
|
924
|
+
await persistDiscussion({
|
|
925
|
+
runnerThreadId: run.runnerThreadId ?? null,
|
|
926
|
+
replyMessageId: posted.messageId,
|
|
927
|
+
markMessagesHandled: true,
|
|
928
|
+
});
|
|
929
|
+
this.deps.emitStatus({
|
|
930
|
+
runnerEvent: "discussion_replied",
|
|
931
|
+
channelId: context.channelId,
|
|
932
|
+
threadId: context.threadId,
|
|
933
|
+
triggerMessageId: triggerEvent.messageId,
|
|
934
|
+
replyMessageId: posted.messageId,
|
|
935
|
+
runner: cfg.runner,
|
|
936
|
+
...(run.runnerThreadId === undefined ? {} : { runnerThreadId: run.runnerThreadId }),
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
else if (posted.kind === "rejected") {
|
|
940
|
+
await persistDiscussion({
|
|
941
|
+
runnerThreadId: run.runnerThreadId ?? null,
|
|
942
|
+
markMessagesHandled: true,
|
|
943
|
+
});
|
|
944
|
+
this.deps.emitStatus({
|
|
945
|
+
runnerEvent: "discussion_failed",
|
|
946
|
+
channelId: context.channelId,
|
|
947
|
+
threadId: context.threadId,
|
|
948
|
+
messageId: triggerEvent.messageId,
|
|
949
|
+
errorMessage: `Discussion reply rejected (HTTP ${String(posted.status)})`,
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
else {
|
|
953
|
+
await persistDiscussion({
|
|
954
|
+
runnerThreadId: run.runnerThreadId ?? null,
|
|
955
|
+
markMessagesHandled: false,
|
|
956
|
+
});
|
|
957
|
+
this.deps.emitStatus({
|
|
958
|
+
runnerEvent: "discussion_failed",
|
|
959
|
+
channelId: context.channelId,
|
|
960
|
+
threadId: context.threadId,
|
|
961
|
+
messageId: triggerEvent.messageId,
|
|
962
|
+
errorMessage: sanitizeAndClamp(posted.message ?? `Discussion reply failed (HTTP ${String(posted.status ?? 0)})`, 300),
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
catch (error) {
|
|
967
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
968
|
+
this.deps.onDiagnostic(`Discussion run failed for thread ${triggerEvent.threadId}: ${message}`);
|
|
969
|
+
this.deps.emitStatus({
|
|
970
|
+
runnerEvent: "discussion_failed",
|
|
971
|
+
channelId: triggerEvent.channelId,
|
|
972
|
+
threadId: triggerEvent.threadId,
|
|
973
|
+
messageId: triggerEvent.messageId,
|
|
974
|
+
errorMessage: sanitizeAndClamp(message, 300),
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
}
|
|
603
978
|
launchDispatch(dispatch, cfg) {
|
|
604
979
|
const { event } = dispatch;
|
|
605
980
|
let resolveRunDone;
|
|
@@ -725,7 +1100,7 @@ export class AgentRunOrchestrator {
|
|
|
725
1100
|
workspacePath: preparedWorkspace.path,
|
|
726
1101
|
workspaceMode: preparedWorkspace.mode,
|
|
727
1102
|
});
|
|
728
|
-
const adapter = this.
|
|
1103
|
+
const adapter = this.getOrCreateAdapter(cfg);
|
|
729
1104
|
let currentPrompt = dispatch.overridePrompt ??
|
|
730
1105
|
renderWorkItemPrompt(context, this.workflow, dispatch.isFollowUpMention);
|
|
731
1106
|
let currentResumeThreadId = reusableThreadId;
|
|
@@ -1104,11 +1479,25 @@ export class AgentRunOrchestrator {
|
|
|
1104
1479
|
await run.handle.kill();
|
|
1105
1480
|
}
|
|
1106
1481
|
}
|
|
1482
|
+
for (const run of this.activeByDiscussionThread.values()) {
|
|
1483
|
+
if (run.stopRequested) {
|
|
1484
|
+
continue;
|
|
1485
|
+
}
|
|
1486
|
+
if (now - run.lastActivityAt > timeoutMs && run.handle !== null) {
|
|
1487
|
+
run.stopRequested = true;
|
|
1488
|
+
run.stopReason ??= "stall_timeout";
|
|
1489
|
+
this.deps.onDiagnostic(`Discussion turn on thread ${run.threadId} stalled after ${String(timeoutMs)}ms; terminating.`);
|
|
1490
|
+
await run.handle.kill();
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1107
1493
|
}
|
|
1108
1494
|
async waitForIdle() {
|
|
1109
|
-
while (this.
|
|
1495
|
+
while (this.hasActiveWork()) {
|
|
1110
1496
|
await this.drainQueue();
|
|
1111
|
-
const activePromises = [
|
|
1497
|
+
const activePromises = [
|
|
1498
|
+
...[...this.activeByWorkItem.values()].map((r) => r.completionPromise),
|
|
1499
|
+
...[...this.activeByDiscussionThread.values()].map((r) => r.completionPromise),
|
|
1500
|
+
];
|
|
1112
1501
|
if (activePromises.length === 0)
|
|
1113
1502
|
break;
|
|
1114
1503
|
await Promise.allSettled(activePromises);
|
|
@@ -1125,6 +1514,8 @@ export class AgentRunOrchestrator {
|
|
|
1125
1514
|
this.stallTimer = null;
|
|
1126
1515
|
}
|
|
1127
1516
|
this.pendingQueue.length = 0;
|
|
1517
|
+
this.pendingDiscussionQueue.length = 0;
|
|
1518
|
+
this.inFlightDiscussionMessageKeys.clear();
|
|
1128
1519
|
const runs = [...this.activeByWorkItem.values()];
|
|
1129
1520
|
for (const run of runs) {
|
|
1130
1521
|
run.stopRequested = true;
|
|
@@ -1134,11 +1525,50 @@ export class AgentRunOrchestrator {
|
|
|
1134
1525
|
await run.handle.stop().catch(() => undefined);
|
|
1135
1526
|
}
|
|
1136
1527
|
}
|
|
1137
|
-
|
|
1528
|
+
const discussionRuns = [...this.activeByDiscussionThread.values()];
|
|
1529
|
+
for (const run of discussionRuns) {
|
|
1530
|
+
run.stopRequested = true;
|
|
1531
|
+
run.stopReason ??= "local_shutdown";
|
|
1532
|
+
if (run.handle !== null) {
|
|
1533
|
+
await run.handle.stop().catch(() => undefined);
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
await raceWithTimeout(Promise.allSettled([
|
|
1537
|
+
...runs.map((r) => r.completionPromise),
|
|
1538
|
+
...discussionRuns.map((r) => r.completionPromise),
|
|
1539
|
+
]).then(() => undefined), 10_000, undefined);
|
|
1540
|
+
const adapters = [...this.createdAdapters];
|
|
1541
|
+
this.createdAdapters.clear();
|
|
1542
|
+
this.cachedAdapter = null;
|
|
1543
|
+
await Promise.allSettled(adapters.map((adapter) => adapter.dispose === undefined
|
|
1544
|
+
? Promise.resolve()
|
|
1545
|
+
: adapter.dispose().catch(() => undefined)));
|
|
1138
1546
|
})();
|
|
1139
1547
|
return this.shutdownPromise;
|
|
1140
1548
|
}
|
|
1141
1549
|
}
|
|
1550
|
+
function selectNewDiscussionMessages(messages, lastHandledMessageId, selfAgentId, triggerMessageIds, triggerMessage) {
|
|
1551
|
+
const actionable = messages.filter((m) => !m.deleted && !m.isSystemNotice);
|
|
1552
|
+
let startIndex = 0;
|
|
1553
|
+
if (lastHandledMessageId !== null) {
|
|
1554
|
+
for (let i = actionable.length - 1; i >= 0; i -= 1) {
|
|
1555
|
+
if (actionable[i]?.id === lastHandledMessageId) {
|
|
1556
|
+
startIndex = i + 1;
|
|
1557
|
+
break;
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
const sliced = actionable
|
|
1562
|
+
.slice(startIndex)
|
|
1563
|
+
.filter((m) => m.authorAgentId !== selfAgentId || triggerMessageIds.has(m.id));
|
|
1564
|
+
if (sliced.length === 0) {
|
|
1565
|
+
return [triggerMessage];
|
|
1566
|
+
}
|
|
1567
|
+
if (!sliced.some((m) => m.id === triggerMessage.id)) {
|
|
1568
|
+
return [...sliced, triggerMessage];
|
|
1569
|
+
}
|
|
1570
|
+
return sliced;
|
|
1571
|
+
}
|
|
1142
1572
|
function mapNonRemoteStopFailure(reason) {
|
|
1143
1573
|
switch (reason) {
|
|
1144
1574
|
case "unassigned":
|