@gonovus-ai/spec-flow-cli 1.4.0 → 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 +3 -3
- 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 +9 -0
- package/dist/runner/orchestrator.js +435 -11
- 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 +23 -1
- package/dist/runner/telemetry-bridge.js +168 -4
- package/dist/runner/telemetry-bridge.js.map +1 -1
- package/dist/runner/types.d.ts +63 -1
- package/dist/runner/types.js +6 -0
- 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 +5 -2
- package/guides/delivery.md +1 -1
- package/guides/discuss.md +1 -1
- package/guides/work-items.md +45 -1
- package/package.json +1 -1
|
@@ -4,11 +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 { isWorkItemAgentEvent, } from "./types.js";
|
|
8
|
-
import { loadWorkflowIfPresent, renderWorkItemPrompt } from "./workflow.js";
|
|
7
|
+
import { isBoardAgentEvent, isWorkItemAgentEvent, } from "./types.js";
|
|
8
|
+
import { loadWorkflowIfPresent, renderDiscussionPrompt, renderWorkItemPrompt } from "./workflow.js";
|
|
9
9
|
import { prepareWorkItemWorkspace, resolveEffectiveWorkspaceMode, runLifecycleHook, summarizeWorkspaceChanges, } from "./workspace-manager.js";
|
|
10
10
|
const MAX_SESSION_EVENT_PAGES = 50;
|
|
11
11
|
const SESSION_EVENTS_PAGE_SIZE = 200;
|
|
12
|
+
const MIN_DISCUSSION_CONCURRENCY = 4;
|
|
12
13
|
const EMPTY_HOOKS = { timeoutMs: 60_000 };
|
|
13
14
|
function hasExecutableWorkflowFields(wf) {
|
|
14
15
|
return (wf.runner !== undefined ||
|
|
@@ -44,7 +45,7 @@ export function isSessionControlEventType(eventType) {
|
|
|
44
45
|
export function createDefaultRunnerAdapter(config, cliVersion) {
|
|
45
46
|
switch (config.runner) {
|
|
46
47
|
case "codex":
|
|
47
|
-
return new CodexAppServerAdapter(config.codexCommand, cliVersion);
|
|
48
|
+
return new CodexAppServerAdapter(config.codexCommand, cliVersion, { persistent: true });
|
|
48
49
|
case "claude":
|
|
49
50
|
return new ClaudeStreamAdapter(config.claudeCommand);
|
|
50
51
|
case "acp":
|
|
@@ -64,6 +65,11 @@ export class AgentRunOrchestrator {
|
|
|
64
65
|
activeByWorkItem = new Map();
|
|
65
66
|
activeBySession = new Map();
|
|
66
67
|
pendingQueue = [];
|
|
68
|
+
activeByDiscussionThread = new Map();
|
|
69
|
+
pendingDiscussionQueue = [];
|
|
70
|
+
inFlightDiscussionMessageKeys = new Set();
|
|
71
|
+
cachedAdapter = null;
|
|
72
|
+
createdAdapters = new Set();
|
|
67
73
|
workflow = null;
|
|
68
74
|
frozenExecutableWorkflow = null;
|
|
69
75
|
freezeExecutableOnReload = false;
|
|
@@ -74,6 +80,25 @@ export class AgentRunOrchestrator {
|
|
|
74
80
|
constructor(deps) {
|
|
75
81
|
this.deps = deps;
|
|
76
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
|
+
}
|
|
77
102
|
now() {
|
|
78
103
|
return this.deps.now?.() ?? Date.now();
|
|
79
104
|
}
|
|
@@ -131,7 +156,9 @@ export class AgentRunOrchestrator {
|
|
|
131
156
|
};
|
|
132
157
|
return this.workflow;
|
|
133
158
|
}
|
|
134
|
-
if (!isInitialLoad &&
|
|
159
|
+
if (!isInitialLoad &&
|
|
160
|
+
this.freezeExecutableOnReload &&
|
|
161
|
+
this.frozenExecutableWorkflow !== null) {
|
|
135
162
|
const frozen = this.frozenExecutableWorkflow;
|
|
136
163
|
this.workflow = {
|
|
137
164
|
sourcePath: loaded.sourcePath,
|
|
@@ -271,7 +298,10 @@ export class AgentRunOrchestrator {
|
|
|
271
298
|
}
|
|
272
299
|
}
|
|
273
300
|
hasActiveWork() {
|
|
274
|
-
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);
|
|
275
305
|
}
|
|
276
306
|
hasPendingWaiters() {
|
|
277
307
|
for (const run of this.activeByWorkItem.values()) {
|
|
@@ -288,6 +318,16 @@ export class AgentRunOrchestrator {
|
|
|
288
318
|
minSeq = item.event.agentSeq;
|
|
289
319
|
}
|
|
290
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
|
+
}
|
|
291
331
|
return minSeq;
|
|
292
332
|
}
|
|
293
333
|
async handleEvent(event) {
|
|
@@ -300,12 +340,17 @@ export class AgentRunOrchestrator {
|
|
|
300
340
|
eventType: event.eventType,
|
|
301
341
|
workItemKey: event.workItemKey,
|
|
302
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 }),
|
|
303
346
|
});
|
|
304
347
|
return;
|
|
305
348
|
}
|
|
349
|
+
if (isBoardAgentEvent(event)) {
|
|
350
|
+
await this.onDiscussionEvent(event);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
306
353
|
if (!isWorkItemAgentEvent(event)) {
|
|
307
|
-
// Discussion Board `thread.mentioned` / `thread.message_created` events are served
|
|
308
|
-
// through `specflow discuss` (SPF-74); skip cleanly while advancing the cursor.
|
|
309
354
|
return;
|
|
310
355
|
}
|
|
311
356
|
switch (event.eventType) {
|
|
@@ -333,6 +378,26 @@ export class AgentRunOrchestrator {
|
|
|
333
378
|
break;
|
|
334
379
|
}
|
|
335
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
|
+
}
|
|
336
401
|
async onAssigned(event) {
|
|
337
402
|
const existing = this.activeByWorkItem.get(event.workItemId);
|
|
338
403
|
if (existing !== undefined) {
|
|
@@ -602,10 +667,314 @@ export class AgentRunOrchestrator {
|
|
|
602
667
|
dequeuedAny = true;
|
|
603
668
|
this.launchDispatch(dispatch, cfg);
|
|
604
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
|
+
}
|
|
605
698
|
if (dequeuedAny) {
|
|
606
699
|
this.deps.onQueueChanged?.();
|
|
607
700
|
}
|
|
608
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
|
+
}
|
|
609
978
|
launchDispatch(dispatch, cfg) {
|
|
610
979
|
const { event } = dispatch;
|
|
611
980
|
let resolveRunDone;
|
|
@@ -731,7 +1100,7 @@ export class AgentRunOrchestrator {
|
|
|
731
1100
|
workspacePath: preparedWorkspace.path,
|
|
732
1101
|
workspaceMode: preparedWorkspace.mode,
|
|
733
1102
|
});
|
|
734
|
-
const adapter = this.
|
|
1103
|
+
const adapter = this.getOrCreateAdapter(cfg);
|
|
735
1104
|
let currentPrompt = dispatch.overridePrompt ??
|
|
736
1105
|
renderWorkItemPrompt(context, this.workflow, dispatch.isFollowUpMention);
|
|
737
1106
|
let currentResumeThreadId = reusableThreadId;
|
|
@@ -1110,11 +1479,25 @@ export class AgentRunOrchestrator {
|
|
|
1110
1479
|
await run.handle.kill();
|
|
1111
1480
|
}
|
|
1112
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
|
+
}
|
|
1113
1493
|
}
|
|
1114
1494
|
async waitForIdle() {
|
|
1115
|
-
while (this.
|
|
1495
|
+
while (this.hasActiveWork()) {
|
|
1116
1496
|
await this.drainQueue();
|
|
1117
|
-
const activePromises = [
|
|
1497
|
+
const activePromises = [
|
|
1498
|
+
...[...this.activeByWorkItem.values()].map((r) => r.completionPromise),
|
|
1499
|
+
...[...this.activeByDiscussionThread.values()].map((r) => r.completionPromise),
|
|
1500
|
+
];
|
|
1118
1501
|
if (activePromises.length === 0)
|
|
1119
1502
|
break;
|
|
1120
1503
|
await Promise.allSettled(activePromises);
|
|
@@ -1131,6 +1514,8 @@ export class AgentRunOrchestrator {
|
|
|
1131
1514
|
this.stallTimer = null;
|
|
1132
1515
|
}
|
|
1133
1516
|
this.pendingQueue.length = 0;
|
|
1517
|
+
this.pendingDiscussionQueue.length = 0;
|
|
1518
|
+
this.inFlightDiscussionMessageKeys.clear();
|
|
1134
1519
|
const runs = [...this.activeByWorkItem.values()];
|
|
1135
1520
|
for (const run of runs) {
|
|
1136
1521
|
run.stopRequested = true;
|
|
@@ -1140,11 +1525,50 @@ export class AgentRunOrchestrator {
|
|
|
1140
1525
|
await run.handle.stop().catch(() => undefined);
|
|
1141
1526
|
}
|
|
1142
1527
|
}
|
|
1143
|
-
|
|
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)));
|
|
1144
1546
|
})();
|
|
1145
1547
|
return this.shutdownPromise;
|
|
1146
1548
|
}
|
|
1147
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
|
+
}
|
|
1148
1572
|
function mapNonRemoteStopFailure(reason) {
|
|
1149
1573
|
switch (reason) {
|
|
1150
1574
|
case "unassigned":
|