@covro/covro 0.12.1-dev.4 → 0.12.1-dev.6
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 +1 -1
- package/README.zh-CN.md +1 -1
- package/dist/cli/service-restart.js +3 -0
- package/dist/cli/service-restart.js.map +2 -2
- package/dist/codex/adapter.js +34 -47
- package/dist/codex/adapter.js.map +2 -2
- package/dist/drizzle/0011_session_loops.sql +23 -0
- package/dist/drizzle/meta/0011_snapshot.json +903 -0
- package/dist/drizzle/meta/_journal.json +7 -0
- package/dist/server/capacity-retry.js +2 -2
- package/dist/server/capacity-retry.js.map +2 -2
- package/dist/server/db/index.js.map +2 -2
- package/dist/server/db/operations.js +2 -1
- package/dist/server/db/operations.js.map +2 -2
- package/dist/server/db/schema.js +26 -0
- package/dist/server/db/schema.js.map +2 -2
- package/dist/server/db/session-loops.js +106 -0
- package/dist/server/db/session-loops.js.map +7 -0
- package/dist/server/http.js +23 -0
- package/dist/server/http.js.map +2 -2
- package/dist/server/main.js +4 -1
- package/dist/server/main.js.map +2 -2
- package/dist/server/projects.js +7 -2
- package/dist/server/projects.js.map +2 -2
- package/dist/server/service.js +294 -10
- package/dist/server/service.js.map +2 -2
- package/dist/server/workspaces.js +5 -2
- package/dist/server/workspaces.js.map +2 -2
- package/dist/shared/service-restart.js.map +2 -2
- package/dist/shared/session-loop.js +19 -0
- package/dist/shared/session-loop.js.map +7 -0
- package/dist/shared/updates.js.map +1 -1
- package/dist/shared/validation.js +12 -0
- package/dist/shared/validation.js.map +2 -2
- package/dist/updates/manager.js +13 -0
- package/dist/updates/manager.js.map +2 -2
- package/dist/web/assets/{abnfDiagram-WEYOIA3C-CfL2upMv.js → abnfDiagram-WEYOIA3C-BPz_mbq5.js} +1 -1
- package/dist/web/assets/{arc-B8P0_gJ3.js → arc-BeOvtHL4.js} +1 -1
- package/dist/web/assets/{architecture-WOLXFQ4H-Dx8Y7HAv.js → architecture-WOLXFQ4H-WLRz0W9w.js} +1 -1
- package/dist/web/assets/{architectureDiagram-ZHVYKT53-PE8uZdYJ.js → architectureDiagram-ZHVYKT53-DTDfMEYj.js} +1 -1
- package/dist/web/assets/{blockDiagram-RJAE7QA3-C6YOWwwf.js → blockDiagram-RJAE7QA3-PyPgoi1Y.js} +1 -1
- package/dist/web/assets/{c4Diagram-XDPPPE54-CXmkBz06.js → c4Diagram-XDPPPE54-BQfgP9jq.js} +1 -1
- package/dist/web/assets/channel-DQKuI1j2.js +1 -0
- package/dist/web/assets/{chunk-2E7WHVMD-CktmjPe8.js → chunk-2E7WHVMD-3waIADk6.js} +1 -1
- package/dist/web/assets/{chunk-4EMNKR2T-CWfc5hHF.js → chunk-4EMNKR2T-eWWWT8CY.js} +1 -1
- package/dist/web/assets/{chunk-55XCU76K-5ouIimWC.js → chunk-55XCU76K-2Jzog9Lv.js} +1 -1
- package/dist/web/assets/{chunk-7S6XSTFO-DS_UZlTN.js → chunk-7S6XSTFO-CM8MWMQJ.js} +1 -1
- package/dist/web/assets/{chunk-AQDTSKZB-B0aU7jvv.js → chunk-AQDTSKZB-DnqnkkkG.js} +1 -1
- package/dist/web/assets/{chunk-DSNLSHJZ-CaAXgYsE.js → chunk-DSNLSHJZ-B9ddXFI5.js} +1 -1
- package/dist/web/assets/{chunk-HAGFOTED-B24hglTY.js → chunk-HAGFOTED-CqzOI5J2.js} +1 -1
- package/dist/web/assets/{chunk-HNE4Q36P-DLFCRuMX.js → chunk-HNE4Q36P-v1EfTSVs.js} +1 -1
- package/dist/web/assets/{chunk-NQOKRVIF-_ORmZhR3.js → chunk-NQOKRVIF-DEmzbUb0.js} +1 -1
- package/dist/web/assets/{chunk-UHW7PFBP-Dd2QYGXY.js → chunk-UHW7PFBP-BN-F3z6T.js} +1 -1
- package/dist/web/assets/{chunk-VQ5W62QI-B6Wl1vCx.js → chunk-VQ5W62QI-XkbVevnu.js} +1 -1
- package/dist/web/assets/{chunk-WQQL4CDK-o-F8BVzT.js → chunk-WQQL4CDK-CSJqC2F3.js} +1 -1
- package/dist/web/assets/{chunk-XXDRQBXY-DGKvyDsF.js → chunk-XXDRQBXY-zGGY7raL.js} +1 -1
- package/dist/web/assets/{chunk-YZO67HFG-aKbt_Djq.js → chunk-YZO67HFG-Buy4qZr3.js} +1 -1
- package/dist/web/assets/{chunk-ZAUX2LTX-BI4TR8xC.js → chunk-ZAUX2LTX-BrK-e7cm.js} +1 -1
- package/dist/web/assets/{chunk-ZCHMQSOR-Df0qQCY7.js → chunk-ZCHMQSOR-Bvxa_VFE.js} +1 -1
- package/dist/web/assets/{classDiagram-v2-4MYWR3UA-CY7Ndtdz.js → classDiagram-v2-4MYWR3UA-SUC4TkGL.js} +1 -1
- package/dist/web/assets/{cose-bilkent-JH36ORCC-DSsLWsfp.js → cose-bilkent-JH36ORCC-DGfUru66.js} +1 -1
- package/dist/web/assets/{cynefin-EF2NZ3EQ-ORXztfh3.js → cynefin-EF2NZ3EQ-C4DORkEC.js} +1 -1
- package/dist/web/assets/{cynefinDiagram-WF24UDRP-2hoxPZDJ.js → cynefinDiagram-WF24UDRP-DJMZYVQX.js} +1 -1
- package/dist/web/assets/{dagre-NK4GIZVX-Dxb1BVWJ.js → dagre-NK4GIZVX-BO4R98Hv.js} +1 -1
- package/dist/web/assets/{diagram-CBK3MWTC-CeabmcX-.js → diagram-CBK3MWTC-Mdeep_xa.js} +1 -1
- package/dist/web/assets/{diagram-I5XIIV2D-CDdEo0zR.js → diagram-I5XIIV2D-DZslB2qP.js} +1 -1
- package/dist/web/assets/{diagram-ODIL2UCW-DAmhtOl4.js → diagram-ODIL2UCW-DP5RkBsw.js} +1 -1
- package/dist/web/assets/{diagram-PFBEJUXE-DZrjMTo5.js → diagram-PFBEJUXE-Cu5muNMl.js} +1 -1
- package/dist/web/assets/{diagram-RJA3I4VV-CyetWt9a.js → diagram-RJA3I4VV-DYtB9wjg.js} +1 -1
- package/dist/web/assets/{diagram-TYUNFSNR-DrNBo31w.js → diagram-TYUNFSNR-CnD7r81l.js} +1 -1
- package/dist/web/assets/{dist-wBx7EsPP.js → dist-wZJmkd7a.js} +1 -1
- package/dist/web/assets/{ebnfDiagram-V4P3TTPT-3azAtjW2.js → ebnfDiagram-V4P3TTPT-B1r-cBvT.js} +1 -1
- package/dist/web/assets/{elk-QONTBCTQ-BYibnVW1.js → elk-QONTBCTQ-BzcqHr4T.js} +1 -1
- package/dist/web/assets/{erDiagram-KYQIGQAA-CbFAhZ0K.js → erDiagram-KYQIGQAA-Y6vtKhfZ.js} +1 -1
- package/dist/web/assets/{eventmodeling-K75KTNOO-L0FcCnh_.js → eventmodeling-K75KTNOO-DIHB3Zmn.js} +1 -1
- package/dist/web/assets/flowDiagram-IUGLLWBU-C0Xx1cky.js +1 -0
- package/dist/web/assets/{ganttDiagram-R7GPNTJY-CM-5movO.js → ganttDiagram-R7GPNTJY-DtsKynSx.js} +1 -1
- package/dist/web/assets/{gitGraph-VSP46ZUC-DWXyHKFe.js → gitGraph-VSP46ZUC-CQm3okru.js} +1 -1
- package/dist/web/assets/{gitGraphDiagram-PDYL7XHY-ClnFqOQQ.js → gitGraphDiagram-PDYL7XHY-CyohbOVo.js} +1 -1
- package/dist/web/assets/{index-Ns5fm-Kl.css → index-D4z03hlo.css} +1 -1
- package/dist/web/assets/index-DXNYTrN6.js +166 -0
- package/dist/web/assets/{info-OHQRW6UA-DIs0BHXF.js → info-OHQRW6UA-Cp9nitYm.js} +1 -1
- package/dist/web/assets/{infoDiagram-MVDRKAX5-C1_DbSCr.js → infoDiagram-MVDRKAX5-rkrbJIai.js} +1 -1
- package/dist/web/assets/{ishikawaDiagram-HPEHV2GE-VpiM0FMG.js → ishikawaDiagram-HPEHV2GE-PW0d2b76.js} +1 -1
- package/dist/web/assets/{journeyDiagram-BOSHW7O7-xH45BIep.js → journeyDiagram-BOSHW7O7-BZq2duGh.js} +1 -1
- package/dist/web/assets/{kanban-definition-FNSA4HC5-DkVZqvmq.js → kanban-definition-FNSA4HC5-s_6ALXKA.js} +1 -1
- package/dist/web/assets/{line-CVVQQK-J.js → line-Dm9i07KD.js} +1 -1
- package/dist/web/assets/{linear-DALC1WJP.js → linear-BMkxokpS.js} +1 -1
- package/dist/web/assets/{mermaid-parser.core-BiNQvcQp.js → mermaid-parser.core-CxtXr5Ra.js} +3 -3
- package/dist/web/assets/{mermaid.core-D-_gEHi-.js → mermaid.core-CJwxTz_n.js} +4 -4
- package/dist/web/assets/{mindmap-definition-6OY57UG3-CDn7a7di.js → mindmap-definition-6OY57UG3-CB3BKsze.js} +1 -1
- package/dist/web/assets/{packet-JDAUHWVQ-BqXCG-VL.js → packet-JDAUHWVQ-BkQv_7ev.js} +1 -1
- package/dist/web/assets/{pegDiagram-ECIUJNBX-D26k-vK6.js → pegDiagram-ECIUJNBX-D9HvJ1dJ.js} +1 -1
- package/dist/web/assets/{pie-XZMESJXO-BzugFQlC.js → pie-XZMESJXO-D42fRk2w.js} +1 -1
- package/dist/web/assets/{pieDiagram-GNMPY74T-CFFh2jEE.js → pieDiagram-GNMPY74T-BwNdUbqR.js} +1 -1
- package/dist/web/assets/{quadrantDiagram-D57K4MC3-BdxTipFV.js → quadrantDiagram-D57K4MC3-YZuKGqpp.js} +1 -1
- package/dist/web/assets/{radar-ABXABTNO-CmP_Tkk1.js → radar-ABXABTNO-uXeynJOk.js} +1 -1
- package/dist/web/assets/{railroad-I3PHUGI6-iWEj5PTd.js → railroad-I3PHUGI6-BZccU5VI.js} +1 -1
- package/dist/web/assets/{railroad-abnf-LNEFI6M7-DdluYQS_.js → railroad-abnf-LNEFI6M7-D9NHdgyX.js} +1 -1
- package/dist/web/assets/{railroad-ebnf-DWYD2UWJ-B2ekqTZO.js → railroad-ebnf-DWYD2UWJ-CWWxgCTZ.js} +1 -1
- package/dist/web/assets/{railroad-peg-JR7BVNR7-BsiAmzwy.js → railroad-peg-JR7BVNR7-D1-82BCx.js} +1 -1
- package/dist/web/assets/{railroadDiagram-Y72NRBL7-Bq5bT77v.js → railroadDiagram-Y72NRBL7-DzsHR4yE.js} +1 -1
- package/dist/web/assets/{requirementDiagram-EQYSW5RA-BiHubcKH.js → requirementDiagram-EQYSW5RA-D6AL4RYo.js} +1 -1
- package/dist/web/assets/{sankeyDiagram-SY24JHO5-DJYlu994.js → sankeyDiagram-SY24JHO5-1eR3fcdc.js} +1 -1
- package/dist/web/assets/{sequenceDiagram-BLEHNHQU-Cy3bK9DO.js → sequenceDiagram-BLEHNHQU-C0JuEJ5h.js} +1 -1
- package/dist/web/assets/{src-CZ-9bvj-.js → src-CiKw2Fy4.js} +1 -1
- package/dist/web/assets/{stateDiagram-v2-A6IESKWZ-Ceonzb5Y.js → stateDiagram-v2-A6IESKWZ-B6knGfGj.js} +1 -1
- package/dist/web/assets/{swimlanes-B3ENRKFQ-B8GPDh5I.js → swimlanes-B3ENRKFQ-Cpu3FfwV.js} +1 -1
- package/dist/web/assets/swimlanesDiagram-AYQRV4AK-BFQ7uuQY.js +8 -0
- package/dist/web/assets/{timeline-definition-JXYKGLGB-DGIn4Lky.js → timeline-definition-JXYKGLGB-BYQUWBHu.js} +1 -1
- package/dist/web/assets/{treeView-D4JQ5SDB-CwUDOtLK.js → treeView-D4JQ5SDB-C7Kl_YrT.js} +1 -1
- package/dist/web/assets/{treemap-SAJKECNS-Cw1lsxWY.js → treemap-SAJKECNS-BeNd4Uit.js} +1 -1
- package/dist/web/assets/{usecaseDiagram-WPOLBNNX-Cf-7XCFK.js → usecaseDiagram-WPOLBNNX-Cq2HeQxU.js} +1 -1
- package/dist/web/assets/{vennDiagram-Q3Y3IDCW-WDII2_mW.js → vennDiagram-Q3Y3IDCW-CzLcEkch.js} +1 -1
- package/dist/web/assets/{wardley-7MLQ67FV-DVuWP4SO.js → wardley-7MLQ67FV-D27zWEUR.js} +1 -1
- package/dist/web/assets/{wardleyDiagram-IVAIDKWA-BZ04NZcX.js → wardleyDiagram-IVAIDKWA-CGLyUtxt.js} +1 -1
- package/dist/web/assets/{xychartDiagram-4URNHDU4-B7xGmh10.js → xychartDiagram-4URNHDU4-CfEwU-KA.js} +1 -1
- package/dist/web/index.html +2 -2
- package/package.json +4 -4
- package/dist/web/assets/channel-DCgcdDVm.js +0 -1
- package/dist/web/assets/flowDiagram-IUGLLWBU-DC340tOR.js +0 -1
- package/dist/web/assets/index-bkos8JIV.js +0 -166
- package/dist/web/assets/swimlanesDiagram-AYQRV4AK-C9IDlMZz.js +0 -8
package/dist/server/service.js
CHANGED
|
@@ -49,7 +49,8 @@ import { readLocalMarkdown } from "./markdown.js";
|
|
|
49
49
|
import { readLocalText } from "./text-files.js";
|
|
50
50
|
import { readLocalJson } from "./json-files.js";
|
|
51
51
|
import { sessionCreationResult } from "./session-creation.js";
|
|
52
|
-
import { createSessionSchema } from "../shared/validation.js";
|
|
52
|
+
import { createSessionLoopSchema, createSessionSchema } from "../shared/validation.js";
|
|
53
|
+
import { SessionLoopStore, sessionLoopView } from "./db/session-loops.js";
|
|
53
54
|
class GatewayService {
|
|
54
55
|
constructor(options) {
|
|
55
56
|
this.options = options;
|
|
@@ -89,11 +90,19 @@ class GatewayService {
|
|
|
89
90
|
);
|
|
90
91
|
this.currentEpoch = this.codex.epoch;
|
|
91
92
|
this.store.recover();
|
|
93
|
+
this.loops = new SessionLoopStore(this.store.database);
|
|
94
|
+
this.loops.skipMissed(Date.now());
|
|
92
95
|
this.removeListener = this.codex.onEvent((event) => this.consume(event));
|
|
93
96
|
}
|
|
94
97
|
options;
|
|
95
98
|
logger;
|
|
96
99
|
capacityRetries;
|
|
100
|
+
loops;
|
|
101
|
+
loopSchedulerStopped = false;
|
|
102
|
+
loopTimer;
|
|
103
|
+
loopTick;
|
|
104
|
+
loopsOffline = false;
|
|
105
|
+
manualStarts = /* @__PURE__ */ new Map();
|
|
97
106
|
projectClones;
|
|
98
107
|
shell;
|
|
99
108
|
events;
|
|
@@ -263,7 +272,8 @@ class GatewayService {
|
|
|
263
272
|
blockers.add(activity);
|
|
264
273
|
reason ??= detail;
|
|
265
274
|
};
|
|
266
|
-
if (this.capacityRetries.active()
|
|
275
|
+
if (this.capacityRetries.active((job) => kind === "backup" || !this.scheduledCapacityRetry(job)))
|
|
276
|
+
block("retry", "\u7B49\u5F85\u6A21\u578B\u5BB9\u91CF\u91CD\u8BD5\u7ED3\u675F\u6216\u53D6\u6D88");
|
|
267
277
|
if (this.projectClones.hasActive) block("clone", "\u7B49\u5F85\u9879\u76EE\u514B\u9686\u5B8C\u6210\u6216\u53D6\u6D88");
|
|
268
278
|
if (this.shell.active()) block("shell", "\u7B49\u5F85 Shell \u547D\u4EE4\u7ED3\u675F");
|
|
269
279
|
if (this.instructionWrites.size || this.defaultWrites.size || this.inFlightOperations.size || this.resumes.size || this.pendingThreadCreations.size)
|
|
@@ -792,6 +802,7 @@ class GatewayService {
|
|
|
792
802
|
}
|
|
793
803
|
updateArchiveState(threadId, archived) {
|
|
794
804
|
const thread = this.projection.get(threadId);
|
|
805
|
+
if (archived) this.loops.archive(threadId);
|
|
795
806
|
if (!thread || Boolean(thread.archived) === archived) return;
|
|
796
807
|
if (archived) this.capacityRetries.clear(threadId);
|
|
797
808
|
thread.archived = archived;
|
|
@@ -823,14 +834,17 @@ class GatewayService {
|
|
|
823
834
|
metadata.historyVisible = this.historyVisible(metadata);
|
|
824
835
|
return { ...metadata, turns: [], actions: [] };
|
|
825
836
|
}
|
|
826
|
-
async
|
|
837
|
+
async refreshThreadDetail(threadId) {
|
|
827
838
|
const cached = this.projection.get(threadId);
|
|
828
839
|
await this.loadThread(threadId);
|
|
829
840
|
if (cached && this.codex.ready) await this.refreshThreadMetadata(threadId);
|
|
830
841
|
const thread = this.projection.get(threadId);
|
|
831
842
|
this.workspaces.assertOwnership(thread.projectId, thread.cwd, thread.id);
|
|
832
843
|
await this.refreshGoal(thread);
|
|
833
|
-
return
|
|
844
|
+
return thread;
|
|
845
|
+
}
|
|
846
|
+
async getThread(threadId) {
|
|
847
|
+
return this.threadView(await this.refreshThreadDetail(threadId));
|
|
834
848
|
}
|
|
835
849
|
getThreadAttention(threadId) {
|
|
836
850
|
const thread = this.projection.get(threadId);
|
|
@@ -839,7 +853,7 @@ class GatewayService {
|
|
|
839
853
|
return pendingAttention(this.threadView(thread));
|
|
840
854
|
}
|
|
841
855
|
async getThreadSnapshot(threadId) {
|
|
842
|
-
await this.
|
|
856
|
+
await this.refreshThreadDetail(threadId);
|
|
843
857
|
const thread = this.projection.get(threadId);
|
|
844
858
|
this.workspaces.assertOwnership(thread.projectId, thread.cwd, thread.id);
|
|
845
859
|
return {
|
|
@@ -1137,11 +1151,257 @@ class GatewayService {
|
|
|
1137
1151
|
});
|
|
1138
1152
|
}
|
|
1139
1153
|
async startTurn(threadId, input) {
|
|
1140
|
-
|
|
1154
|
+
this.manualStarts.set(threadId, (this.manualStarts.get(threadId) ?? 0) + 1);
|
|
1155
|
+
try {
|
|
1156
|
+
return await this.dispatchTurn(threadId, input);
|
|
1157
|
+
} finally {
|
|
1158
|
+
const remaining = this.manualStarts.get(threadId) - 1;
|
|
1159
|
+
if (remaining) this.manualStarts.set(threadId, remaining);
|
|
1160
|
+
else this.manualStarts.delete(threadId);
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
loopWorkspace(threadId) {
|
|
1164
|
+
const link = this.store.getThreadLink(threadId);
|
|
1165
|
+
if (!link) throw new AppError("THREAD_NOT_FOUND", "Session not found", 404);
|
|
1166
|
+
return this.workspaces.assertOwnership(link.projectId, link.cwd, threadId);
|
|
1167
|
+
}
|
|
1168
|
+
async listSessionLoops(threadId) {
|
|
1169
|
+
if (!this.store.getThreadLink(threadId)) await this.loadThread(threadId);
|
|
1170
|
+
const workspace = this.loopWorkspace(threadId);
|
|
1171
|
+
return this.loops.list(threadId, workspace.workspaceKey);
|
|
1141
1172
|
}
|
|
1142
|
-
async
|
|
1173
|
+
async createSessionLoop(threadId, input) {
|
|
1174
|
+
input = createSessionLoopSchema.parse(input);
|
|
1175
|
+
return this.perform("loop.create", threadId, input, async () => {
|
|
1176
|
+
const thread = await this.loadThread(threadId);
|
|
1177
|
+
this.ensureManaged(thread);
|
|
1178
|
+
this.ensureCompatibilityConfirmed();
|
|
1179
|
+
if (!(thread.hasMessages || thread.turns.some((turn) => isTerminal(turn.status))))
|
|
1180
|
+
throw new AppError("LOOP_UNSAVED_THREAD", "Send a message to save this session first", 409);
|
|
1181
|
+
if (parseQuestionReplies(input.prompt))
|
|
1182
|
+
throw new AppError("LOOP_INVALID_PROMPT", "A loop requires a regular prompt", 400);
|
|
1183
|
+
const workspace = this.loopWorkspace(threadId);
|
|
1184
|
+
const runtime = thread.runtimeSettings;
|
|
1185
|
+
const permissionMode = runtime?.permissionMode;
|
|
1186
|
+
const hasPermissionOverrides = input.settings?.permissionMode !== void 0 || input.settings?.approvalPolicy !== void 0 || input.settings?.approvalReviewer !== void 0 || input.settings?.sandbox !== void 0;
|
|
1187
|
+
const settings = this.retrySettings(runtime, {
|
|
1188
|
+
...runtime?.collaborationMode ? { collaborationMode: runtime.collaborationMode } : {},
|
|
1189
|
+
...!hasPermissionOverrides && permissionMode && permissionMode !== "custom" ? { permissionMode } : {},
|
|
1190
|
+
...input.settings
|
|
1191
|
+
});
|
|
1192
|
+
const now = Date.now();
|
|
1193
|
+
this.store.dispatch(
|
|
1194
|
+
input.clientOperationId,
|
|
1195
|
+
void 0,
|
|
1196
|
+
void 0,
|
|
1197
|
+
(tx) => this.loops.create(tx, {
|
|
1198
|
+
id: input.clientOperationId,
|
|
1199
|
+
threadId,
|
|
1200
|
+
workspaceKey: workspace.workspaceKey,
|
|
1201
|
+
settings,
|
|
1202
|
+
prompt: input.prompt,
|
|
1203
|
+
intervalMs: input.intervalMs,
|
|
1204
|
+
status: "active",
|
|
1205
|
+
nextRunAt: now + input.intervalMs
|
|
1206
|
+
})
|
|
1207
|
+
);
|
|
1208
|
+
return { result: sessionLoopView(this.loops.get(input.clientOperationId)) };
|
|
1209
|
+
});
|
|
1210
|
+
}
|
|
1211
|
+
async controlSessionLoop(threadId, loopId, input) {
|
|
1212
|
+
return this.perform("loop.control", threadId, { ...input, loopId }, async () => {
|
|
1213
|
+
const thread = input.action === "resume" ? await this.loadThread(threadId) : void 0;
|
|
1214
|
+
const { workspaceKey } = this.loopWorkspace(threadId);
|
|
1215
|
+
const row = this.loops.get(loopId);
|
|
1216
|
+
if (!row || row.threadId !== threadId || row.workspaceKey !== workspaceKey)
|
|
1217
|
+
throw new AppError("LOOP_NOT_FOUND", "Loop not found", 404);
|
|
1218
|
+
const now = Date.now();
|
|
1219
|
+
const canChange = row.version === input.version && (input.action === "cancel" ? row.status === "active" || row.status === "paused" : input.action === "pause" ? row.status === "active" : row.status === "paused" && thread?.archived !== true && row.lastResult !== "unknown");
|
|
1220
|
+
if (!canChange)
|
|
1221
|
+
throw new AppError("LOOP_CONFLICT", "The loop has changed or cannot be resumed", 409);
|
|
1222
|
+
this.store.dispatch(
|
|
1223
|
+
input.clientOperationId,
|
|
1224
|
+
void 0,
|
|
1225
|
+
void 0,
|
|
1226
|
+
(tx) => this.loops.control(tx, row, input.action, now)
|
|
1227
|
+
);
|
|
1228
|
+
const retry = this.capacityRetries.current(threadId);
|
|
1229
|
+
if (input.action !== "resume" && retry && row.lastOperationId && [retry.operationId, retry.previousOperationId].includes(row.lastOperationId) && this.capacityRetries.pending(threadId) && !this.store.activeForThread(threadId))
|
|
1230
|
+
this.capacityRetries.cancel(retry);
|
|
1231
|
+
return { result: sessionLoopView(this.loops.get(loopId)) };
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
startLoopScheduler() {
|
|
1235
|
+
if (this.loopTimer || this.loopSchedulerStopped) return;
|
|
1236
|
+
this.loopTimer = setInterval(() => {
|
|
1237
|
+
void this.tickSessionLoops().catch(
|
|
1238
|
+
(error) => this.logger.warn("loop.scheduler_failed", errorFields(error))
|
|
1239
|
+
);
|
|
1240
|
+
}, 1e3);
|
|
1241
|
+
this.loopTimer.unref();
|
|
1242
|
+
}
|
|
1243
|
+
stopLoopScheduler() {
|
|
1244
|
+
this.loopSchedulerStopped = true;
|
|
1245
|
+
if (this.loopTimer) clearInterval(this.loopTimer);
|
|
1246
|
+
}
|
|
1247
|
+
loopDispatchDeferred() {
|
|
1248
|
+
return this.loopSchedulerStopped || !this.accepting || this.maintenance || this.options.maintenancePending?.() === true;
|
|
1249
|
+
}
|
|
1250
|
+
scheduledCapacityRetry(job) {
|
|
1251
|
+
return this.loops.byRun(job.operationId) ?? (job.previousOperationId ? this.loops.byRun(job.previousOperationId) : void 0);
|
|
1252
|
+
}
|
|
1253
|
+
tickSessionLoops() {
|
|
1254
|
+
this.loopTick ??= this.runSessionLoops().finally(() => {
|
|
1255
|
+
this.loopTick = void 0;
|
|
1256
|
+
});
|
|
1257
|
+
return this.loopTick;
|
|
1258
|
+
}
|
|
1259
|
+
async runSessionLoops() {
|
|
1260
|
+
if (this.loopSchedulerStopped || !this.accepting || this.maintenance) return;
|
|
1261
|
+
const now = Date.now();
|
|
1262
|
+
if (!this.codex.ready || this.codex.diagnostics().authenticated === false) {
|
|
1263
|
+
this.loopsOffline = true;
|
|
1264
|
+
return;
|
|
1265
|
+
}
|
|
1266
|
+
if (this.loopsOffline) {
|
|
1267
|
+
this.loops.skipMissed(now);
|
|
1268
|
+
this.loopsOffline = false;
|
|
1269
|
+
}
|
|
1270
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
1271
|
+
for (const row of this.loops.unfinished()) {
|
|
1272
|
+
const group = grouped.get(row.threadId) ?? { unfinished: [] };
|
|
1273
|
+
group.unfinished.push(row);
|
|
1274
|
+
grouped.set(row.threadId, group);
|
|
1275
|
+
}
|
|
1276
|
+
const dueLoops = this.loops.due(now);
|
|
1277
|
+
if (dueLoops.length && !this.loopDispatchDeferred()) {
|
|
1278
|
+
for (const row of dueLoops) {
|
|
1279
|
+
const group = grouped.get(row.threadId) ?? { unfinished: [] };
|
|
1280
|
+
group.due ??= row;
|
|
1281
|
+
grouped.set(row.threadId, group);
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
await Promise.all(
|
|
1285
|
+
[...grouped.values()].map(async ({ unfinished, due }) => {
|
|
1286
|
+
for (const row of unfinished) await this.settleLoopRun(row);
|
|
1287
|
+
if (due) await this.runDueLoop(due);
|
|
1288
|
+
})
|
|
1289
|
+
);
|
|
1290
|
+
}
|
|
1291
|
+
async settleLoopRun(row) {
|
|
1292
|
+
const operationId = row.lastOperationId;
|
|
1293
|
+
if (!operationId) return;
|
|
1294
|
+
const retry = this.capacityRetries.current(row.threadId);
|
|
1295
|
+
if (this.capacityRetries.pending(row.threadId) && (retry?.operationId === operationId || retry?.previousOperationId === operationId))
|
|
1296
|
+
return;
|
|
1297
|
+
const operation = this.store.get(operationId);
|
|
1298
|
+
const active = this.store.activeForThread(row.threadId);
|
|
1299
|
+
const marker = active?.operationId === operationId ? active : void 0;
|
|
1300
|
+
if (marker?.state === "active") return;
|
|
1301
|
+
let thread;
|
|
1302
|
+
try {
|
|
1303
|
+
thread = await this.loadThread(row.threadId);
|
|
1304
|
+
} catch (error) {
|
|
1305
|
+
const code = this.asAppError(error).code;
|
|
1306
|
+
if (code === "THREAD_NOT_FOUND") this.loops.suspend(row, "deleted");
|
|
1307
|
+
else if ([
|
|
1308
|
+
"PROJECT_NOT_FOUND",
|
|
1309
|
+
"PROJECT_UNAVAILABLE",
|
|
1310
|
+
"PROJECT_OWNERSHIP",
|
|
1311
|
+
"WORKSPACE_UNAVAILABLE"
|
|
1312
|
+
].includes(code))
|
|
1313
|
+
this.loops.suspend(row, "unavailable");
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
const turnId = operation?.result?.turnId ?? marker?.turnId ?? row.lastTurnId;
|
|
1317
|
+
const turn = turnId ? thread.turns.find((entry) => entry.id === turnId) : thread.turns.find(
|
|
1318
|
+
(entry) => entry.items.some((item) => item.type === "userMessage" && item.clientId === operationId)
|
|
1319
|
+
);
|
|
1320
|
+
if (turn && isTerminal(turn.status)) {
|
|
1321
|
+
this.loops.record(row, turn.status, turn.id);
|
|
1322
|
+
} else if (operation?.reconciledAt != null) {
|
|
1323
|
+
this.loops.record(row, "reconciled", turnId ?? null);
|
|
1324
|
+
} else if (operation?.state === "failed") {
|
|
1325
|
+
this.loops.record(row, "failed", turnId ?? null);
|
|
1326
|
+
} else if (operation?.state === "unknown" || marker?.state === "unknown") {
|
|
1327
|
+
this.loops.record(row, "unknown", turnId ?? null);
|
|
1328
|
+
this.loops.suspend(row, "uncertain");
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
recordLoopTurn(operationId, status, turnId) {
|
|
1332
|
+
const row = this.loops.byRun(operationId);
|
|
1333
|
+
if (row && !this.capacityRetries.pending(row.threadId))
|
|
1334
|
+
this.loops.record(row, status, turnId);
|
|
1335
|
+
}
|
|
1336
|
+
assertLoopIdle(thread, row) {
|
|
1337
|
+
if (this.loopDispatchDeferred()) throw new AppError("LOOP_WAITING", "The service is busy", 409);
|
|
1338
|
+
if (this.manualStarts.has(thread.id) || this.capacityRetries.pending(thread.id) || this.resumes.has(thread.id) || this.store.activeForThread(thread.id) || isActive(thread.status) || thread.status === "unknown" || thread.turns.some((turn) => isActive(turn.status) || turn.status === "unknown") || thread.actions.some((action) => ["pending", "responding"].includes(action.status)))
|
|
1339
|
+
throw new AppError("LOOP_WAITING", "The session is busy", 409);
|
|
1340
|
+
if (this.loopWorkspace(thread.id).workspaceKey !== row.workspaceKey)
|
|
1341
|
+
throw new AppError("LOOP_WORKSPACE_CHANGED", "The loop workspace has changed", 409);
|
|
1342
|
+
}
|
|
1343
|
+
async runDueLoop(row) {
|
|
1344
|
+
if (this.loopDispatchDeferred()) return;
|
|
1345
|
+
const current = this.loops.get(row.id);
|
|
1346
|
+
if (current?.status !== "active" || current.version !== row.version || ["running", "unknown"].includes(current.lastResult ?? ""))
|
|
1347
|
+
return;
|
|
1348
|
+
const operationId = randomUUID();
|
|
1349
|
+
try {
|
|
1350
|
+
const thread = await this.loadThread(row.threadId);
|
|
1351
|
+
if (thread.archived) {
|
|
1352
|
+
this.loops.archive(thread.id);
|
|
1353
|
+
return;
|
|
1354
|
+
}
|
|
1355
|
+
this.assertLoopIdle(thread, row);
|
|
1356
|
+
this.ensureCompatibilityConfirmed();
|
|
1357
|
+
if (!thread.managed || this.store.getThreadLink(thread.id)?.managedEpoch !== this.codex.epoch)
|
|
1358
|
+
await this.resumeThread(thread.id, {
|
|
1359
|
+
clientOperationId: randomUUID(),
|
|
1360
|
+
settings: row.settings
|
|
1361
|
+
});
|
|
1362
|
+
if (this.loopDispatchDeferred()) return;
|
|
1363
|
+
await this.dispatchTurn(
|
|
1364
|
+
thread.id,
|
|
1365
|
+
{
|
|
1366
|
+
clientOperationId: operationId,
|
|
1367
|
+
text: row.prompt,
|
|
1368
|
+
settings: row.settings
|
|
1369
|
+
},
|
|
1370
|
+
void 0,
|
|
1371
|
+
void 0,
|
|
1372
|
+
row
|
|
1373
|
+
);
|
|
1374
|
+
} catch (error) {
|
|
1375
|
+
switch (this.asAppError(error).code) {
|
|
1376
|
+
case "THREAD_NOT_FOUND":
|
|
1377
|
+
this.loops.suspend(row, "deleted");
|
|
1378
|
+
break;
|
|
1379
|
+
case "THREAD_ARCHIVED":
|
|
1380
|
+
this.loops.archive(row.threadId);
|
|
1381
|
+
break;
|
|
1382
|
+
case "LOOP_WAITING":
|
|
1383
|
+
case "LOOP_CONFLICT":
|
|
1384
|
+
case "THREAD_BUSY":
|
|
1385
|
+
case "CODEX_UNAVAILABLE":
|
|
1386
|
+
case "COMPATIBILITY_CONFIRMATION_REQUIRED":
|
|
1387
|
+
case "RESTART_MAINTENANCE":
|
|
1388
|
+
case "UPDATE_MAINTENANCE":
|
|
1389
|
+
case "BACKUP_MAINTENANCE":
|
|
1390
|
+
break;
|
|
1391
|
+
case "RESULT_UNKNOWN":
|
|
1392
|
+
this.loops.suspend(row, "uncertain");
|
|
1393
|
+
break;
|
|
1394
|
+
default: {
|
|
1395
|
+
const dispatched = this.loops.byRun(operationId);
|
|
1396
|
+
if (dispatched) this.loops.record(dispatched, "failed", null);
|
|
1397
|
+
else this.loops.suspend(row, "unavailable");
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
async dispatchTurn(threadId, input, retry, expectedTurnId, loop) {
|
|
1143
1403
|
return this.perform("turn.start", threadId, input, async () => {
|
|
1144
|
-
if (!retry && !expectedTurnId && this.capacityRetries.view(threadId)?.status === "waiting")
|
|
1404
|
+
if (!loop && !retry && !expectedTurnId && this.capacityRetries.view(threadId)?.status === "waiting")
|
|
1145
1405
|
this.capacityRetries.clear(threadId);
|
|
1146
1406
|
const thread = await this.loadThread(threadId);
|
|
1147
1407
|
this.ensureManaged(thread);
|
|
@@ -1172,6 +1432,16 @@ class GatewayService {
|
|
|
1172
1432
|
...input,
|
|
1173
1433
|
text: input.taskKind === "init" ? PROJECT_INIT_PROMPT : fileText
|
|
1174
1434
|
};
|
|
1435
|
+
if (loop) this.assertLoopIdle(this.projection.get(threadId) ?? thread, loop);
|
|
1436
|
+
const retryLoop = retry?.previousOperationId ? this.loops.byRun(retry.previousOperationId) : void 0;
|
|
1437
|
+
if (retryLoop && this.loopDispatchDeferred())
|
|
1438
|
+
throw new AppError("LOOP_WAITING", "The service is busy", 409);
|
|
1439
|
+
const claimLoop = loop ? (tx) => this.loops.claim(tx, loop, input.clientOperationId, Date.now()) : retryLoop && retry?.previousOperationId ? (tx) => this.loops.retry(
|
|
1440
|
+
tx,
|
|
1441
|
+
retryLoop.id,
|
|
1442
|
+
retry.previousOperationId,
|
|
1443
|
+
input.clientOperationId
|
|
1444
|
+
) : void 0;
|
|
1175
1445
|
this.store.dispatch(
|
|
1176
1446
|
input.clientOperationId,
|
|
1177
1447
|
{
|
|
@@ -1182,7 +1452,8 @@ class GatewayService {
|
|
|
1182
1452
|
message.text || message.images?.length ? {
|
|
1183
1453
|
threadId,
|
|
1184
1454
|
item: submittedUserMessage(input.clientOperationId, message)
|
|
1185
|
-
} : void 0
|
|
1455
|
+
} : void 0,
|
|
1456
|
+
claimLoop
|
|
1186
1457
|
);
|
|
1187
1458
|
if (retry && !this.capacityRetries.owns(retry))
|
|
1188
1459
|
throw new AppError("TURN_CONFLICT", "\u5BB9\u91CF\u91CD\u8BD5\u5DF2\u53D6\u6D88\u6216\u88AB\u66FF\u4EE3", 409);
|
|
@@ -1212,8 +1483,10 @@ class GatewayService {
|
|
|
1212
1483
|
if (retryOf) this.retryLinks(threadId).set(turn.id, retryOf);
|
|
1213
1484
|
if (candidate.cancelled && retry) await this.interruptCancelledCapacity(candidate, turn.id);
|
|
1214
1485
|
const projectedTurn = this.projection.get(threadId)?.turns.find((entry) => entry.id === turn.id);
|
|
1215
|
-
if (projectedTurn && isTerminal(projectedTurn.status))
|
|
1486
|
+
if (projectedTurn && isTerminal(projectedTurn.status)) {
|
|
1216
1487
|
this.store.finishTurn(threadId, turn.id, this.codex.epoch);
|
|
1488
|
+
this.recordLoopTurn(input.clientOperationId, projectedTurn.status, turn.id);
|
|
1489
|
+
}
|
|
1217
1490
|
this.publishThread(threadId);
|
|
1218
1491
|
return {
|
|
1219
1492
|
result: { threadId, turnId: turn.id, ...retryOf ? { retryOf } : {} },
|
|
@@ -1690,6 +1963,7 @@ class GatewayService {
|
|
|
1690
1963
|
}
|
|
1691
1964
|
async shutdown() {
|
|
1692
1965
|
this.accepting = false;
|
|
1966
|
+
this.stopLoopScheduler();
|
|
1693
1967
|
await Promise.allSettled([...this.instructionWrites, ...this.defaultWrites]);
|
|
1694
1968
|
this.capacityRetries.clearAll();
|
|
1695
1969
|
await this.projectClones.close();
|
|
@@ -1697,6 +1971,8 @@ class GatewayService {
|
|
|
1697
1971
|
this.projection.runtimeReset();
|
|
1698
1972
|
this.removeListener();
|
|
1699
1973
|
await this.codex.stop();
|
|
1974
|
+
await this.loopTick?.catch(() => {
|
|
1975
|
+
});
|
|
1700
1976
|
}
|
|
1701
1977
|
async perform(kind, target, input, work) {
|
|
1702
1978
|
if (!this.accepting) throw new AppError("CODEX_UNAVAILABLE", "\u670D\u52A1\u6B63\u5728\u5173\u95ED", 503);
|
|
@@ -2106,6 +2382,8 @@ class GatewayService {
|
|
|
2106
2382
|
this.retryLinks(event.threadId).set(event.turn.id, job.rootTurnId);
|
|
2107
2383
|
}
|
|
2108
2384
|
if (event.type === "runtime") {
|
|
2385
|
+
if (!event.diagnostics.ready || event.diagnostics.codexEpoch !== this.currentEpoch)
|
|
2386
|
+
this.loopsOffline = true;
|
|
2109
2387
|
this.compatibilityConfirmation(event.diagnostics);
|
|
2110
2388
|
if (!event.diagnostics.ready || event.diagnostics.codexEpoch !== this.currentEpoch) {
|
|
2111
2389
|
this.currentEpoch = event.diagnostics.codexEpoch;
|
|
@@ -2249,6 +2527,12 @@ class GatewayService {
|
|
|
2249
2527
|
this.turnBaselines.delete(marker.operationId);
|
|
2250
2528
|
}
|
|
2251
2529
|
if (isTerminal(event.turn.status)) {
|
|
2530
|
+
if (marker?.turnId === event.turn.id || matchesDispatch && eventMarker?.turnId === null)
|
|
2531
|
+
this.recordLoopTurn(
|
|
2532
|
+
(marker ?? eventMarker).operationId,
|
|
2533
|
+
event.turn.status,
|
|
2534
|
+
event.turn.id
|
|
2535
|
+
);
|
|
2252
2536
|
this.store.finishTurn(thread.id, event.turn.id, this.codex.epoch);
|
|
2253
2537
|
this.projection.expireTurn(thread.id, event.turn.id);
|
|
2254
2538
|
if (newCompletion && !this.capacityRetries.pending(thread.id)) {
|