@covro/covro 0.9.0-beta.2 → 0.9.1-dev.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.
Files changed (162) hide show
  1. package/README.md +10 -10
  2. package/README.zh-CN.md +10 -10
  3. package/dist/backups/manager.js +21 -1
  4. package/dist/backups/manager.js.map +2 -2
  5. package/dist/cli/backup.js +33 -2
  6. package/dist/cli/backup.js.map +2 -2
  7. package/dist/cli/daemon.js +84 -18
  8. package/dist/cli/daemon.js.map +2 -2
  9. package/dist/cli/logs.js +141 -0
  10. package/dist/cli/logs.js.map +7 -0
  11. package/dist/cli/main.js +33 -10
  12. package/dist/cli/main.js.map +2 -2
  13. package/dist/cli/paths.js +9 -1
  14. package/dist/cli/paths.js.map +2 -2
  15. package/dist/codex/adapter.js +137 -34
  16. package/dist/codex/adapter.js.map +3 -3
  17. package/dist/codex/permissions.js.map +1 -1
  18. package/dist/codex/thread-titles.js +143 -21
  19. package/dist/codex/thread-titles.js.map +2 -2
  20. package/dist/codex/transport.js +32 -2
  21. package/dist/codex/transport.js.map +2 -2
  22. package/dist/drizzle/0009_question_tracking.sql +15 -0
  23. package/dist/drizzle/meta/0009_snapshot.json +698 -0
  24. package/dist/drizzle/meta/_journal.json +7 -0
  25. package/dist/logging/bootstrap.js +25 -0
  26. package/dist/logging/bootstrap.js.map +7 -0
  27. package/dist/logging/config.js +38 -0
  28. package/dist/logging/config.js.map +7 -0
  29. package/dist/logging/context.js +11 -0
  30. package/dist/logging/context.js.map +7 -0
  31. package/dist/logging/fields.js +82 -0
  32. package/dist/logging/fields.js.map +7 -0
  33. package/dist/logging/lifecycle.js +14 -0
  34. package/dist/logging/lifecycle.js.map +7 -0
  35. package/dist/logging/logger.js +216 -0
  36. package/dist/logging/logger.js.map +7 -0
  37. package/dist/logging/retention.js +62 -0
  38. package/dist/logging/retention.js.map +7 -0
  39. package/dist/server/capacity-retry.js +26 -1
  40. package/dist/server/capacity-retry.js.map +2 -2
  41. package/dist/server/db/operations.js +69 -13
  42. package/dist/server/db/operations.js.map +3 -3
  43. package/dist/server/db/question-tracking.js +109 -0
  44. package/dist/server/db/question-tracking.js.map +7 -0
  45. package/dist/server/db/schema.js +22 -0
  46. package/dist/server/db/schema.js.map +2 -2
  47. package/dist/server/file-references.js +66 -0
  48. package/dist/server/file-references.js.map +7 -0
  49. package/dist/server/http.js +57 -2
  50. package/dist/server/http.js.map +3 -3
  51. package/dist/server/main.js +57 -10
  52. package/dist/server/main.js.map +2 -2
  53. package/dist/server/mcp/server.js +17 -3
  54. package/dist/server/mcp/server.js.map +2 -2
  55. package/dist/server/project-clones.js +19 -1
  56. package/dist/server/project-clones.js.map +2 -2
  57. package/dist/server/projects.js +6 -0
  58. package/dist/server/projects.js.map +2 -2
  59. package/dist/server/service.js +233 -24
  60. package/dist/server/service.js.map +3 -3
  61. package/dist/server/websocket.js +24 -4
  62. package/dist/server/websocket.js.map +2 -2
  63. package/dist/shared/file-references.js +17 -0
  64. package/dist/shared/file-references.js.map +7 -0
  65. package/dist/shared/question-resolution.js +27 -0
  66. package/dist/shared/question-resolution.js.map +7 -0
  67. package/dist/shared/thread-attention.js +1 -1
  68. package/dist/shared/thread-attention.js.map +2 -2
  69. package/dist/shared/validation.js +31 -2
  70. package/dist/shared/validation.js.map +2 -2
  71. package/dist/update-worker.cjs +17027 -299
  72. package/dist/updates/manager.js +32 -5
  73. package/dist/updates/manager.js.map +2 -2
  74. package/dist/updates/worker.js +37 -4
  75. package/dist/updates/worker.js.map +2 -2
  76. package/dist/web/assets/{abnfDiagram-WEYOIA3C--w3UpuXr.js → abnfDiagram-WEYOIA3C-CQZkXfp-.js} +1 -1
  77. package/dist/web/assets/{arc-BUuh0R80.js → arc-DJRor8HR.js} +1 -1
  78. package/dist/web/assets/{architecture-WOLXFQ4H-CzFbMy7D.js → architecture-WOLXFQ4H-DZaJXBgl.js} +1 -1
  79. package/dist/web/assets/{architectureDiagram-ZHVYKT53-CN0paUHo.js → architectureDiagram-ZHVYKT53-CWG5dJNj.js} +1 -1
  80. package/dist/web/assets/{blockDiagram-RJAE7QA3-BlEcWjxu.js → blockDiagram-RJAE7QA3-BQUqIJkJ.js} +1 -1
  81. package/dist/web/assets/{c4Diagram-XDPPPE54-cIzZ_a04.js → c4Diagram-XDPPPE54-DnBcwKTJ.js} +1 -1
  82. package/dist/web/assets/channel-BbrYcO7E.js +1 -0
  83. package/dist/web/assets/{chunk-2E7WHVMD-Dmpk3iMe.js → chunk-2E7WHVMD-BngH2Ufw.js} +1 -1
  84. package/dist/web/assets/{chunk-4EMNKR2T-UB3rAovL.js → chunk-4EMNKR2T-DYeP3Bhx.js} +1 -1
  85. package/dist/web/assets/{chunk-55XCU76K-BcatroAd.js → chunk-55XCU76K-D5VShDo8.js} +1 -1
  86. package/dist/web/assets/{chunk-7S6XSTFO-DQK4DCU8.js → chunk-7S6XSTFO-Ba1pg_hk.js} +1 -1
  87. package/dist/web/assets/{chunk-AQDTSKZB-CXisXsfT.js → chunk-AQDTSKZB-DURwkwsL.js} +1 -1
  88. package/dist/web/assets/{chunk-DSNLSHJZ-vhLg4EKI.js → chunk-DSNLSHJZ-C7jeRgVF.js} +1 -1
  89. package/dist/web/assets/{chunk-HAGFOTED-D-kcdAww.js → chunk-HAGFOTED-x7jOdotv.js} +1 -1
  90. package/dist/web/assets/{chunk-HNE4Q36P-v77NgJah.js → chunk-HNE4Q36P-7ttciOKk.js} +1 -1
  91. package/dist/web/assets/{chunk-NQOKRVIF-IlEQcb62.js → chunk-NQOKRVIF-DOCLR6ro.js} +1 -1
  92. package/dist/web/assets/{chunk-UHW7PFBP-CLUAsWGq.js → chunk-UHW7PFBP-BxrRE8sE.js} +1 -1
  93. package/dist/web/assets/{chunk-VQ5W62QI-CxJVJJ5o.js → chunk-VQ5W62QI-CI-o1RUW.js} +1 -1
  94. package/dist/web/assets/{chunk-WQQL4CDK-mUHh6eU9.js → chunk-WQQL4CDK-rRuU83QU.js} +1 -1
  95. package/dist/web/assets/{chunk-XXDRQBXY-ztDYcrOV.js → chunk-XXDRQBXY-BE9ZM7Uu.js} +1 -1
  96. package/dist/web/assets/{chunk-YZO67HFG-BUP1yXz1.js → chunk-YZO67HFG-D8dtSxEl.js} +1 -1
  97. package/dist/web/assets/{chunk-ZAUX2LTX-DTWlsdpw.js → chunk-ZAUX2LTX-IvcBRqbz.js} +1 -1
  98. package/dist/web/assets/{chunk-ZCHMQSOR-7wXSNeHf.js → chunk-ZCHMQSOR-cKu8XxfP.js} +1 -1
  99. package/dist/web/assets/{classDiagram-v2-4MYWR3UA-IO1StSMZ.js → classDiagram-v2-4MYWR3UA-CkfqQ9Yo.js} +1 -1
  100. package/dist/web/assets/{cose-bilkent-JH36ORCC-B2gINwTx.js → cose-bilkent-JH36ORCC-1bPvefKT.js} +1 -1
  101. package/dist/web/assets/{cynefin-EF2NZ3EQ-DldQxSM_.js → cynefin-EF2NZ3EQ-BY6wzSa-.js} +1 -1
  102. package/dist/web/assets/{cynefinDiagram-WF24UDRP-B8kYW1OK.js → cynefinDiagram-WF24UDRP-D50BVtv0.js} +1 -1
  103. package/dist/web/assets/{dagre-NK4GIZVX-50hmw1Qa.js → dagre-NK4GIZVX-B7Qm1Jdq.js} +1 -1
  104. package/dist/web/assets/{diagram-CBK3MWTC-CMgkahms.js → diagram-CBK3MWTC-CeIFKt5v.js} +1 -1
  105. package/dist/web/assets/{diagram-I5XIIV2D-TEG8qLDl.js → diagram-I5XIIV2D-Dfc7MXFW.js} +1 -1
  106. package/dist/web/assets/{diagram-ODIL2UCW-DIqdFHgz.js → diagram-ODIL2UCW-CXZZrT-9.js} +1 -1
  107. package/dist/web/assets/{diagram-PFBEJUXE-CR3PdTmV.js → diagram-PFBEJUXE-C6eOGtDm.js} +1 -1
  108. package/dist/web/assets/{diagram-RJA3I4VV-D4D99d6l.js → diagram-RJA3I4VV-k0B336_E.js} +1 -1
  109. package/dist/web/assets/{diagram-TYUNFSNR-BPpzxGYW.js → diagram-TYUNFSNR-NrdiYOBF.js} +1 -1
  110. package/dist/web/assets/{dist-CH0G0uBY.js → dist-CtMfJghl.js} +1 -1
  111. package/dist/web/assets/{ebnfDiagram-V4P3TTPT-hbQgF-Vc.js → ebnfDiagram-V4P3TTPT-C0leFtkB.js} +1 -1
  112. package/dist/web/assets/{elk-QONTBCTQ-DDoQmkEA.js → elk-QONTBCTQ-CZn2rKZM.js} +1 -1
  113. package/dist/web/assets/{erDiagram-KYQIGQAA-CNm3mqRH.js → erDiagram-KYQIGQAA-BYMfM1WV.js} +1 -1
  114. package/dist/web/assets/{eventmodeling-K75KTNOO-B5HxW52S.js → eventmodeling-K75KTNOO-Ja60tPeK.js} +1 -1
  115. package/dist/web/assets/flowDiagram-IUGLLWBU-BLrk_cO2.js +1 -0
  116. package/dist/web/assets/{ganttDiagram-R7GPNTJY-B62avW7T.js → ganttDiagram-R7GPNTJY-CZICCRW8.js} +1 -1
  117. package/dist/web/assets/{gitGraph-VSP46ZUC-ReIlHIT5.js → gitGraph-VSP46ZUC-DRc_cSkt.js} +1 -1
  118. package/dist/web/assets/{gitGraphDiagram-PDYL7XHY-i8pxBTq0.js → gitGraphDiagram-PDYL7XHY-t35f9Fnb.js} +1 -1
  119. package/dist/web/assets/index-BZ9PqQg2.js +164 -0
  120. package/dist/web/assets/{index-B565iZDe.css → index-DsTjqvUr.css} +1 -1
  121. package/dist/web/assets/{info-OHQRW6UA-B6LA8ucb.js → info-OHQRW6UA-pZsMXJHc.js} +1 -1
  122. package/dist/web/assets/{infoDiagram-MVDRKAX5-DczlQNdf.js → infoDiagram-MVDRKAX5-jmUfnFqf.js} +1 -1
  123. package/dist/web/assets/{ishikawaDiagram-HPEHV2GE-CBD57FtN.js → ishikawaDiagram-HPEHV2GE-BdG9yQpt.js} +1 -1
  124. package/dist/web/assets/{journeyDiagram-BOSHW7O7-DVA8uS2X.js → journeyDiagram-BOSHW7O7-C2a1PGRX.js} +1 -1
  125. package/dist/web/assets/{kanban-definition-FNSA4HC5-CIkZ1NfE.js → kanban-definition-FNSA4HC5-C42AxErt.js} +1 -1
  126. package/dist/web/assets/{line-ByVvn_kk.js → line-EWv7xvA8.js} +1 -1
  127. package/dist/web/assets/{linear-Bw9blK2e.js → linear-C1Is5BY1.js} +1 -1
  128. package/dist/web/assets/{mermaid-parser.core-AtqES89L.js → mermaid-parser.core-CUuvsxlV.js} +3 -3
  129. package/dist/web/assets/{mermaid.core-Bex0z1QG.js → mermaid.core-CQbvWwuf.js} +4 -4
  130. package/dist/web/assets/{mindmap-definition-6OY57UG3-mgzJO5ba.js → mindmap-definition-6OY57UG3-CiljBrtl.js} +1 -1
  131. package/dist/web/assets/{packet-JDAUHWVQ-Debj9TuA.js → packet-JDAUHWVQ-T_mLO3_Q.js} +1 -1
  132. package/dist/web/assets/{pegDiagram-ECIUJNBX-mQ4DoxRK.js → pegDiagram-ECIUJNBX-ETNDw70b.js} +1 -1
  133. package/dist/web/assets/{pie-XZMESJXO-DI8YDldK.js → pie-XZMESJXO-DRPBKu16.js} +1 -1
  134. package/dist/web/assets/{pieDiagram-GNMPY74T-CNJy_h2n.js → pieDiagram-GNMPY74T-eDg9dzvk.js} +1 -1
  135. package/dist/web/assets/{quadrantDiagram-D57K4MC3-B4lXMgDl.js → quadrantDiagram-D57K4MC3-B7H1kHfx.js} +1 -1
  136. package/dist/web/assets/{radar-ABXABTNO-DfuKLYyM.js → radar-ABXABTNO-0VrIrbJV.js} +1 -1
  137. package/dist/web/assets/{railroad-I3PHUGI6-DTOHUzqI.js → railroad-I3PHUGI6-CZRUQeE7.js} +1 -1
  138. package/dist/web/assets/{railroad-abnf-LNEFI6M7-B2l8iioC.js → railroad-abnf-LNEFI6M7-b_qK5vo2.js} +1 -1
  139. package/dist/web/assets/{railroad-ebnf-DWYD2UWJ-C3H7fg9D.js → railroad-ebnf-DWYD2UWJ-C5b_4_Tq.js} +1 -1
  140. package/dist/web/assets/{railroad-peg-JR7BVNR7-VHHw1cEa.js → railroad-peg-JR7BVNR7-BKwUkRaU.js} +1 -1
  141. package/dist/web/assets/{railroadDiagram-Y72NRBL7-K_XrJO0S.js → railroadDiagram-Y72NRBL7-svOvIMSI.js} +1 -1
  142. package/dist/web/assets/{requirementDiagram-EQYSW5RA-D5ESlwnp.js → requirementDiagram-EQYSW5RA-CDLDnvqj.js} +1 -1
  143. package/dist/web/assets/{sankeyDiagram-SY24JHO5-dwsdiMqK.js → sankeyDiagram-SY24JHO5-DeDHHqRF.js} +1 -1
  144. package/dist/web/assets/{sequenceDiagram-BLEHNHQU-stj1Lviw.js → sequenceDiagram-BLEHNHQU-CDXPdaxX.js} +1 -1
  145. package/dist/web/assets/{src-CA4eE_49.js → src-B0XyY-lf.js} +1 -1
  146. package/dist/web/assets/{stateDiagram-v2-A6IESKWZ-BW-W_CzJ.js → stateDiagram-v2-A6IESKWZ-DDrODUfL.js} +1 -1
  147. package/dist/web/assets/{swimlanes-B3ENRKFQ-e15HCKVA.js → swimlanes-B3ENRKFQ-ymaK7Yhs.js} +1 -1
  148. package/dist/web/assets/swimlanesDiagram-AYQRV4AK-DRI0j87K.js +8 -0
  149. package/dist/web/assets/{timeline-definition-JXYKGLGB-DY4VwJq-.js → timeline-definition-JXYKGLGB-BiV-bdYS.js} +1 -1
  150. package/dist/web/assets/{treeView-D4JQ5SDB-DkRmODGu.js → treeView-D4JQ5SDB-CnzoAGnh.js} +1 -1
  151. package/dist/web/assets/{treemap-SAJKECNS-D9BUe3jZ.js → treemap-SAJKECNS-BO9uvk_8.js} +1 -1
  152. package/dist/web/assets/{usecaseDiagram-WPOLBNNX-DRObRKJI.js → usecaseDiagram-WPOLBNNX-DdREY_H-.js} +1 -1
  153. package/dist/web/assets/{vennDiagram-Q3Y3IDCW-bKcQJRnj.js → vennDiagram-Q3Y3IDCW-BBICr-ZI.js} +1 -1
  154. package/dist/web/assets/{wardley-7MLQ67FV-J-w3edo1.js → wardley-7MLQ67FV-B12PES6O.js} +1 -1
  155. package/dist/web/assets/{wardleyDiagram-IVAIDKWA-CilZg4RE.js → wardleyDiagram-IVAIDKWA-d-MEoCil.js} +1 -1
  156. package/dist/web/assets/{xychartDiagram-4URNHDU4-BQ9GjKB4.js → xychartDiagram-4URNHDU4-C1YQy9qj.js} +1 -1
  157. package/dist/web/index.html +2 -2
  158. package/package.json +9 -7
  159. package/dist/web/assets/channel-COxn9xzm.js +0 -1
  160. package/dist/web/assets/flowDiagram-IUGLLWBU-CMoN_wDb.js +0 -1
  161. package/dist/web/assets/index-B8q_Rlh1.js +0 -163
  162. package/dist/web/assets/swimlanesDiagram-AYQRV4AK-yIxjbjFE.js +0 -8
@@ -1,4 +1,12 @@
1
+ import { questionAnswers } from "../shared/question-resolution.js";
2
+ import { asyncQuestionId, parseQuestionReplies } from "../shared/async-questions.js";
3
+ import { QuestionTrackingStore } from "./db/question-tracking.js";
4
+ import { nullLogger } from "../logging/logger.js";
5
+ import { errorFields } from "../logging/fields.js";
6
+ import { logContext, withLogContext } from "../logging/context.js";
1
7
  import { hasSkillToken } from "../shared/skills.js";
8
+ import { referenceMessage, validateReferencePath } from "./file-references.js";
9
+ import { containsPath } from "./projects.js";
2
10
  import { CapacityRetries } from "./capacity-retry.js";
3
11
  import { isModelCapacityError } from "../shared/turn-errors.js";
4
12
  import { randomUUID } from "node:crypto";
@@ -39,8 +47,13 @@ import { createSessionSchema } from "../shared/validation.js";
39
47
  class GatewayService {
40
48
  constructor(options) {
41
49
  this.options = options;
50
+ this.logger = (options.logger ?? nullLogger).child({ component: "operations" });
42
51
  this.store = options.store;
43
52
  this.turnUsage = new TurnUsageStore(this.store.database);
53
+ this.questionTracking = new QuestionTrackingStore(
54
+ this.store.database,
55
+ this.logger.child({ component: "question_tracking" })
56
+ );
44
57
  this.projects = options.projects;
45
58
  this.workspaces = new WorkspaceRegistry(
46
59
  this.store.database,
@@ -54,7 +67,8 @@ class GatewayService {
54
67
  this.events.publishProjects(this.projects.list());
55
68
  return project;
56
69
  },
57
- (path) => this.projects.list().some((project) => project.canonicalRoot === path)
70
+ (path) => this.projects.list().some((project) => project.canonicalRoot === path),
71
+ this.logger.child({ component: "clones" })
58
72
  );
59
73
  this.codex = options.codex;
60
74
  this.shell = new ShellService(this.codex);
@@ -62,13 +76,15 @@ class GatewayService {
62
76
  this.projection = new Projection(options.limits?.itemBytes);
63
77
  this.capacityRetries = new CapacityRetries(
64
78
  (job) => this.dispatchCapacityRetry(job),
65
- (id) => this.publishThread(id)
79
+ (id) => this.publishThread(id),
80
+ this.logger.child({ component: "capacity_retry" })
66
81
  );
67
82
  this.currentEpoch = this.codex.epoch;
68
83
  this.store.recover();
69
84
  this.removeListener = this.codex.onEvent((event) => this.consume(event));
70
85
  }
71
86
  options;
87
+ logger;
72
88
  capacityRetries;
73
89
  projectClones;
74
90
  shell;
@@ -76,6 +92,7 @@ class GatewayService {
76
92
  projection;
77
93
  store;
78
94
  turnUsage;
95
+ questionTracking;
79
96
  projects;
80
97
  workspaces;
81
98
  codex;
@@ -294,6 +311,78 @@ class GatewayService {
294
311
  };
295
312
  });
296
313
  }
314
+ referenceScope(cwd, threadId, draftId) {
315
+ return {
316
+ cwd,
317
+ excluded: [
318
+ ...this.projects.referenceRoots().filter((root) => root !== cwd && containsPath(cwd, root)),
319
+ ...this.workspaces.allocations().filter((row) => !(threadId && row.threadId === threadId) && row.operationId !== draftId).map((row) => row.canonicalRoot)
320
+ ]
321
+ };
322
+ }
323
+ async searchFiles(input, signal) {
324
+ this.ensureReady();
325
+ if (!this.codex.searchFiles)
326
+ throw new AppError("METHOD_UNAVAILABLE", "Codex does not support fuzzyFileSearch", 409);
327
+ const thread = input.threadId ? await this.loadThread(input.threadId) : void 0;
328
+ if (thread && thread.projectId !== input.projectId)
329
+ throw new AppError("PROJECT_OWNERSHIP", "Session belongs to another workspace", 403);
330
+ const resolveCwd = () => thread ? this.workspaces.assertOwnership(thread.projectId, thread.cwd, thread.id).cwd : input.projectId === null ? this.workspaces.draft(input.draftId).cwd : this.projects.get(input.projectId).canonicalRoot;
331
+ const cwd = resolveCwd();
332
+ if (input.projectId !== null) this.workspaces.assertProjectDirectory(cwd);
333
+ const draftId = !thread && input.projectId === null ? input.draftId : void 0;
334
+ const scope = this.referenceScope(cwd, thread?.id, draftId);
335
+ const epoch = this.codex.epoch;
336
+ const timeout = AbortSignal.timeout(5e3);
337
+ const cancellation = AbortSignal.any([signal, timeout]);
338
+ try {
339
+ cancellation.throwIfAborted();
340
+ if (!input.query) return { cwd, paths: [], limited: false };
341
+ const result = await withLogContext(
342
+ { threadId: thread?.id },
343
+ () => this.codex.searchFiles(cwd, input.query, cancellation)
344
+ );
345
+ const paths = [];
346
+ let unreadable = false;
347
+ for (const file of result.files.slice(0, 50)) {
348
+ cancellation.throwIfAborted();
349
+ if (file.root !== cwd || file.match_type !== "file") continue;
350
+ const path = process.platform === "win32" ? file.path.replaceAll("\\", "/") : file.path;
351
+ try {
352
+ await validateReferencePath(path, scope);
353
+ if (!paths.includes(path)) paths.push(path);
354
+ } catch (error) {
355
+ if (error instanceof AppError && error.code === "FILE_REFERENCE_UNREADABLE")
356
+ unreadable = true;
357
+ else if (!(error instanceof AppError)) throw error;
358
+ }
359
+ }
360
+ cancellation.throwIfAborted();
361
+ this.ensureReady();
362
+ if (resolveCwd() !== cwd || this.codex.epoch !== epoch || JSON.stringify(this.referenceScope(cwd, thread?.id, draftId)) !== JSON.stringify(scope))
363
+ throw new AppError("FILE_REFERENCE_CHANGED", "Workspace changed; search again", 409);
364
+ if (unreadable && !paths.length)
365
+ throw new AppError("FILE_REFERENCE_UNREADABLE", "Matching files cannot be read", 403);
366
+ return { cwd, paths, limited: result.files.length >= 50 };
367
+ } catch (error) {
368
+ if (timeout.aborted)
369
+ throw new AppError("FILE_SEARCH_TIMEOUT", "File search timed out; narrow the path", 408);
370
+ if (signal.aborted) throw new AppError("FILE_SEARCH_CANCELLED", "File search cancelled", 499);
371
+ throw error;
372
+ }
373
+ }
374
+ async fileMessage(thread, text, files = []) {
375
+ if (!files.length) return text;
376
+ const epoch = this.codex.epoch;
377
+ const ownership = this.workspaces.assertOwnership(thread.projectId, thread.cwd, thread.id);
378
+ const scope = this.referenceScope(ownership.cwd, thread.id);
379
+ const result = await referenceMessage(text, files, scope);
380
+ this.ensureManaged(thread);
381
+ this.workspaces.assertOwnership(thread.projectId, thread.cwd, thread.id);
382
+ if (this.codex.epoch !== epoch || JSON.stringify(this.referenceScope(ownership.cwd, thread.id)) !== JSON.stringify(scope))
383
+ throw new AppError("FILE_REFERENCE_CHANGED", "Workspace changed; select the file again", 409);
384
+ return result;
385
+ }
297
386
  async listSkills(input) {
298
387
  this.ensureReady();
299
388
  if (!this.codex.listSkills)
@@ -594,7 +683,7 @@ class GatewayService {
594
683
  const thread = this.projection.get(threadId);
595
684
  if (!thread) return [];
596
685
  this.workspaces.assertOwnership(thread.projectId, thread.cwd, thread.id);
597
- return pendingAttention(thread);
686
+ return pendingAttention(this.threadView(thread));
598
687
  }
599
688
  async getThreadSnapshot(threadId) {
600
689
  await this.getThread(threadId);
@@ -620,7 +709,7 @@ class GatewayService {
620
709
  const root = links.get(turn.id);
621
710
  if (root) turn.retryOf = root;
622
711
  }
623
- return page;
712
+ return this.threadView(page);
624
713
  }
625
714
  async exportThread(threadId) {
626
715
  this.ensureReady();
@@ -921,9 +1010,14 @@ class GatewayService {
921
1010
  if (isActive(this.projection.get(threadId)?.status ?? thread.status))
922
1011
  throw new AppError("THREAD_BUSY", "Session already has an active task", 409);
923
1012
  }
1013
+ const fileText = input.files?.length ? await this.fileMessage(thread, input.text, input.files) : input.text;
1014
+ if (isActive(this.projection.get(threadId)?.status ?? thread.status))
1015
+ throw new AppError("THREAD_BUSY", "Session already has an active task", 409);
1016
+ if (parseQuestionReplies(fileText))
1017
+ await this.claimQuestionReplies(threadId, fileText, input.clientOperationId);
924
1018
  const message = {
925
1019
  ...input,
926
- text: input.taskKind === "init" ? PROJECT_INIT_PROMPT : input.text
1020
+ text: input.taskKind === "init" ? PROJECT_INIT_PROMPT : fileText
927
1021
  };
928
1022
  this.store.dispatch(
929
1023
  input.clientOperationId,
@@ -1191,16 +1285,24 @@ class GatewayService {
1191
1285
  409
1192
1286
  );
1193
1287
  this.ensureCompatibilityConfirmed();
1288
+ let message = input.text;
1289
+ if (input.files?.length) {
1290
+ const thread = await this.loadThread(threadId);
1291
+ message = await this.fileMessage(thread, input.text, input.files);
1292
+ await this.requireActiveTurn(threadId, input.expectedTurnId);
1293
+ }
1294
+ if (parseQuestionReplies(message))
1295
+ await this.claimQuestionReplies(threadId, message, input.clientOperationId);
1194
1296
  this.store.dispatch(input.clientOperationId, void 0, {
1195
1297
  threadId,
1196
1298
  turnId: input.expectedTurnId,
1197
- item: submittedUserMessage(input.clientOperationId, input)
1299
+ item: submittedUserMessage(input.clientOperationId, { ...input, text: message })
1198
1300
  });
1199
1301
  this.publishThread(threadId);
1200
1302
  const result = await this.codex.steerTurn(
1201
1303
  threadId,
1202
1304
  input.expectedTurnId,
1203
- input.text,
1305
+ message,
1204
1306
  input.images,
1205
1307
  input.clientOperationId
1206
1308
  );
@@ -1279,9 +1381,88 @@ class GatewayService {
1279
1381
  this.projection.resolveAction(actionId, "responding");
1280
1382
  if (action.threadId) this.publishThread(action.threadId);
1281
1383
  await this.codex.respond(actionId, input.resolution);
1384
+ if (action.kind === "questions") {
1385
+ this.projection.resolveAction(actionId, "responded");
1386
+ this.questionTracking.saveAction(
1387
+ action,
1388
+ questionAnswers(action.questions ?? [], input.resolution)
1389
+ );
1390
+ if (action.threadId) this.publishThread(action.threadId);
1391
+ }
1282
1392
  return { result: { actionId } };
1283
1393
  });
1284
1394
  }
1395
+ /** This ends Covro tracking only. It must never start/steer/interrupt a native turn. */
1396
+ async dismissQuestions(threadId, input) {
1397
+ return this.perform("question.dismiss", threadId, input, async () => {
1398
+ await this.loadThread(threadId);
1399
+ const history = this.normalize(await this.codex.readThread(threadId));
1400
+ this.workspaces.assertOwnership(history.projectId, history.cwd, history.id);
1401
+ this.projection.thread(history, true);
1402
+ const states = this.questionTracking.states(threadId);
1403
+ for (const id of new Set(input.questionIds)) {
1404
+ const question = this.findAsyncQuestion(history, id);
1405
+ if (!question) throw new AppError("QUESTION_EXPIRED", "\u8BE5\u95EE\u9898\u5DF2\u4E0D\u5B58\u5728\uFF0C\u8BF7\u5237\u65B0\u4F1A\u8BDD", 409);
1406
+ if (this.questionAnswered(history, id)) continue;
1407
+ if (states[id] && states[id].status !== "dismissed")
1408
+ throw new AppError("QUESTION_BUSY", "\u56DE\u7B54\u6B63\u5728\u63D0\u4EA4\u6216\u7ED3\u679C\u5F85\u6838\u5BF9\uFF0C\u8BF7\u5237\u65B0\u540E\u7EE7\u7EED", 409);
1409
+ }
1410
+ const ids = [...new Set(input.questionIds)].filter(
1411
+ (id) => !this.questionAnswered(history, id)
1412
+ );
1413
+ this.store.dispatch(input.clientOperationId);
1414
+ this.questionTracking.save(threadId, ids, "dismissed", input.clientOperationId);
1415
+ this.publishThread(threadId);
1416
+ return { result: { threadId, questionIds: ids } };
1417
+ });
1418
+ }
1419
+ questionItems(history) {
1420
+ return [...history.turns, ...this.projection.get(history.id)?.turns ?? []].flatMap(
1421
+ (turn) => turn.items
1422
+ );
1423
+ }
1424
+ findAsyncQuestion(history, id) {
1425
+ for (const item of this.questionItems(history)) {
1426
+ if (item.type !== "agentMessage") continue;
1427
+ const index = item.questions?.findIndex((_, index2) => asyncQuestionId(item.id, index2) === id);
1428
+ if (index !== void 0 && index >= 0) return { item, question: item.questions[index] };
1429
+ }
1430
+ }
1431
+ questionAnswered(history, id) {
1432
+ const source = this.findAsyncQuestion(history, id);
1433
+ return Boolean(
1434
+ source && this.questionItems(history).some(
1435
+ (item) => item.questionReplies?.some(
1436
+ (reply) => reply.questionItemId === id || reply.questionItemId === source.item.id && reply.question === source.question.title
1437
+ )
1438
+ )
1439
+ );
1440
+ }
1441
+ async claimQuestionReplies(threadId, text, operationId) {
1442
+ const replies = parseQuestionReplies(text);
1443
+ if (!replies) return;
1444
+ const history = this.normalize(await this.codex.readThread(threadId));
1445
+ this.workspaces.assertOwnership(history.projectId, history.cwd, history.id);
1446
+ this.projection.thread(history, true);
1447
+ const states = this.questionTracking.states(threadId);
1448
+ const ids = [];
1449
+ for (const reply of replies) {
1450
+ const source = this.findAsyncQuestion(history, reply.questionItemId) ?? (() => {
1451
+ const item = this.questionItems(history).find((item2) => item2.id === reply.questionItemId);
1452
+ const index2 = item?.questions?.findIndex((q) => q.title === reply.question);
1453
+ return item && index2 !== void 0 && index2 >= 0 ? this.findAsyncQuestion(history, asyncQuestionId(item.id, index2)) : void 0;
1454
+ })();
1455
+ const index = source?.item.questions?.indexOf(source.question);
1456
+ if (!source || index === void 0)
1457
+ throw new AppError("QUESTION_EXPIRED", "\u8BE5\u95EE\u9898\u5DF2\u4E0D\u5B58\u5728\uFF0C\u8BF7\u5237\u65B0\u4F1A\u8BDD", 409);
1458
+ if (!reply.answer.trim()) throw new AppError("VALIDATION_ERROR", "\u56DE\u7B54\u4E0D\u80FD\u4E3A\u7A7A");
1459
+ const id = asyncQuestionId(source.item.id, index);
1460
+ if (states[id] || this.questionAnswered(history, id) || ids.includes(id))
1461
+ throw new AppError("QUESTION_CONFLICT", "\u8BE5\u95EE\u9898\u5DF2\u5904\u7406\u6216\u6B63\u5728\u63D0\u4EA4\uFF0C\u8BF7\u5237\u65B0\u4F1A\u8BDD", 409);
1462
+ ids.push(id);
1463
+ }
1464
+ this.questionTracking.save(threadId, ids, "submitting", operationId);
1465
+ }
1285
1466
  getOperation(id) {
1286
1467
  const row = this.store.get(id);
1287
1468
  if (!row) throw new AppError("OPERATION_NOT_FOUND", "\u672A\u627E\u5230\u8BE5\u64CD\u4F5C", 404);
@@ -1363,13 +1544,32 @@ class GatewayService {
1363
1544
  } catch (error) {
1364
1545
  throw this.asAppError(error);
1365
1546
  }
1366
- if (!accepted.inserted) return this.operation(accepted.operation);
1547
+ const logger = this.logger.child({
1548
+ ...logContext(),
1549
+ operationId: input.clientOperationId,
1550
+ kind,
1551
+ ...this.store.getThreadLink(target) ? { threadId: target } : {}
1552
+ });
1553
+ const started = performance.now();
1554
+ if (!accepted.inserted) {
1555
+ logger.debug("operation.replayed", { state: accepted.operation.state, replayed: true });
1556
+ return this.operation(accepted.operation);
1557
+ }
1558
+ logger.info("operation.accepted");
1367
1559
  this.inFlightOperations.add(input.clientOperationId);
1368
1560
  try {
1369
- const { result, turnId, onAcknowledged } = await work();
1561
+ const { result, turnId, onAcknowledged } = await withLogContext(
1562
+ { operationId: input.clientOperationId },
1563
+ work
1564
+ );
1370
1565
  const operation = this.operation(
1371
1566
  this.store.acknowledge(input.clientOperationId, result, turnId)
1372
1567
  );
1568
+ logger.info("operation.acknowledged", {
1569
+ state: operation.status,
1570
+ turnId,
1571
+ durationMs: Math.round(performance.now() - started)
1572
+ });
1373
1573
  if (operation.status === "acknowledged") onAcknowledged?.();
1374
1574
  this.events.publish("operation", operation);
1375
1575
  return operation;
@@ -1385,10 +1585,20 @@ class GatewayService {
1385
1585
  uncertain
1386
1586
  )
1387
1587
  );
1588
+ logger.warn("operation.failed", {
1589
+ state: operation.status,
1590
+ uncertain,
1591
+ ...errorFields(appError),
1592
+ durationMs: Math.round(performance.now() - started)
1593
+ });
1388
1594
  if (dispatched && !uncertain && kind === "action.resolve" && this.projection.actions.get(target)?.status === "responding") {
1389
1595
  const action = this.projection.resolveAction(target, "pending");
1390
1596
  if (action?.threadId) this.publishThread(action.threadId);
1391
1597
  }
1598
+ if (!uncertain && (kind === "turn.start" || kind === "turn.steer")) {
1599
+ this.questionTracking.release(input.clientOperationId);
1600
+ this.publishThread(target);
1601
+ }
1392
1602
  if (kind === "turn.start")
1393
1603
  this.capacityRetries.dispatchFailed(target, input.clientOperationId);
1394
1604
  if (dispatched && (kind === "turn.start" || kind === "turn.steer"))
@@ -1549,19 +1759,7 @@ class GatewayService {
1549
1759
  } else if (action.kind === "elicitation") {
1550
1760
  validateElicitationResolution(action.elicitation, resolution);
1551
1761
  } else if (action.kind === "questions") {
1552
- if (!("answers" in resolution)) throw new AppError("VALIDATION_ERROR", "\u8BF7\u6309\u95EE\u9898\u63D0\u4EA4\u7B54\u6848");
1553
- const questions = action.questions ?? [];
1554
- if (Object.keys(resolution.answers).some(
1555
- (id) => !questions.some((question) => question.id === id)
1556
- ))
1557
- throw new AppError("VALIDATION_ERROR", "\u56DE\u7B54\u5305\u542B\u672A\u77E5\u95EE\u9898");
1558
- for (const question of questions) {
1559
- const answers = resolution.answers[question.id];
1560
- if (!answers?.length || answers.some(
1561
- (answer) => !answer.trim() || !question.isOther && question.options.length > 0 && !question.options.some((option) => option.label === answer)
1562
- ))
1563
- throw new AppError("VALIDATION_ERROR", `\u95EE\u9898 ${question.header} \u7684\u56DE\u7B54\u4E0D\u7B26\u5408\u9009\u9879\u8981\u6C42`);
1564
- }
1762
+ questionAnswers(action.questions ?? [], resolution);
1565
1763
  } else if (!("decision" in resolution) || "content" in resolution || "persist" in resolution || !action.options.some((option) => option.id === resolution.decision))
1566
1764
  throw new AppError("VALIDATION_ERROR", "\u8BF7\u9009\u62E9\u4E0A\u6E38\u63D0\u4F9B\u7684\u5408\u6CD5\u9009\u9879");
1567
1765
  }
@@ -1662,6 +1860,13 @@ class GatewayService {
1662
1860
  if (retry) result.capacityRetry = structuredClone(retry);
1663
1861
  else delete result.capacityRetry;
1664
1862
  if (this.capacityRetries.pending(thread.id)) result.status = "running";
1863
+ result.questionTracking = this.questionTracking.states(thread.id);
1864
+ const savedActions = this.questionTracking.actions(thread.id);
1865
+ const actions = new Map(savedActions.map((action) => [action.actionId, action]));
1866
+ for (const action of thread.actions)
1867
+ if (action.status === "pending" || action.status === "responding" || !actions.has(action.actionId))
1868
+ actions.set(action.actionId, action);
1869
+ result.actions = [...actions.values()];
1665
1870
  result.archived = thread.archived ?? false;
1666
1871
  result.historyVisible = this.historyVisible(thread);
1667
1872
  const marker = this.store.activeForThread(thread.id);
@@ -1758,8 +1963,11 @@ class GatewayService {
1758
1963
  if (action.codexEpoch !== this.codex.epoch) return;
1759
1964
  const known = this.projection.actions.has(action.actionId);
1760
1965
  const owner = action.threadId ? this.projection.get(action.threadId) : void 0;
1761
- const previous = new Set(owner ? pendingAttention(owner).map(attentionKey) : []);
1966
+ const previous = new Set(
1967
+ owner ? pendingAttention(this.threadView(owner)).map(attentionKey) : []
1968
+ );
1762
1969
  this.projection.action(action);
1970
+ if (action.kind === "questions") this.questionTracking.saveAction(action);
1763
1971
  if (action.threadId) {
1764
1972
  this.publishThread(action.threadId);
1765
1973
  if (!known) this.publishNewAttention(action.threadId, previous);
@@ -1768,6 +1976,7 @@ class GatewayService {
1768
1976
  }
1769
1977
  if (event.type === "actionResolved") {
1770
1978
  const action = this.projection.resolveAction(event.actionId, "responded");
1979
+ if (action?.kind === "questions") this.questionTracking.saveAction(action);
1771
1980
  if (action?.threadId) this.publishThread(action.threadId);
1772
1981
  return;
1773
1982
  }
@@ -1785,7 +1994,7 @@ class GatewayService {
1785
1994
  return;
1786
1995
  }
1787
1996
  const previousAttention = new Set(
1788
- event.type === "item" || event.type === "turn" ? pendingAttention(thread).map(attentionKey) : []
1997
+ event.type === "item" || event.type === "turn" ? pendingAttention(this.threadView(thread)).map(attentionKey) : []
1789
1998
  );
1790
1999
  if (event.type === "tokenUsage") {
1791
2000
  if (this.projection.tokenUsage(thread.id, event.usage)) this.publishThread(thread.id);