chatroom-cli 1.91.1 → 1.91.2

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/dist/index.js CHANGED
@@ -108117,7 +108117,7 @@ var init_start_subscriptions = __esm(() => {
108117
108117
  });
108118
108118
 
108119
108119
  // src/daemon/entry/enhancer/constants.ts
108120
- var ENHANCER_AGENT_ROLE = "enhancer", ENHANCER_AGENT_END_GRACE_MS = 3000, ENHANCER_JOB_POLL_INTERVAL_MS = 500;
108120
+ var ENHANCER_AGENT_ROLE = "enhancer", ENHANCER_AGENT_END_GRACE_MS = 3000;
108121
108121
 
108122
108122
  // src/daemon/entry/enhancer/enhancer-log.ts
108123
108123
  function formatEnhancerLogLine(message) {
@@ -108132,100 +108132,143 @@ function writeEnhancerLog(message) {
108132
108132
  }
108133
108133
  var ENHANCER_LOG_PREFIX = "[enhancer]";
108134
108134
 
108135
+ // src/daemon/entry/enhancer/job-outcome-subscription.ts
108136
+ function subscribeToEnhancerJobOutcome(args2) {
108137
+ let currentState = null;
108138
+ let resolveOutcome = null;
108139
+ let rejectOutcome = null;
108140
+ let stopped = false;
108141
+ const outcome = new Promise((resolve5, reject) => {
108142
+ resolveOutcome = resolve5;
108143
+ rejectOutcome = reject;
108144
+ });
108145
+ const unsub = args2.wsClient.onUpdate(api.web.enhancer.index.getJobOutcome, {
108146
+ sessionId: args2.sessionId,
108147
+ chatroomId: args2.chatroomId,
108148
+ jobId: args2.jobId
108149
+ }, (state) => {
108150
+ if (stopped)
108151
+ return;
108152
+ if (!state) {
108153
+ rejectOutcome?.(new Error("Enhancer job not found"));
108154
+ resolveOutcome = null;
108155
+ rejectOutcome = null;
108156
+ return;
108157
+ }
108158
+ currentState = state;
108159
+ if (state.status === "complete" || state.status === "failed" || state.status === "cancelled") {
108160
+ resolveOutcome?.(state);
108161
+ resolveOutcome = null;
108162
+ rejectOutcome = null;
108163
+ }
108164
+ }, (error51) => {
108165
+ if (stopped)
108166
+ return;
108167
+ rejectOutcome?.(error51);
108168
+ resolveOutcome = null;
108169
+ rejectOutcome = null;
108170
+ });
108171
+ return {
108172
+ outcome,
108173
+ getCurrentState: () => currentState,
108174
+ stop: () => {
108175
+ if (stopped)
108176
+ return;
108177
+ stopped = true;
108178
+ unsub();
108179
+ resolveOutcome = null;
108180
+ rejectOutcome = null;
108181
+ }
108182
+ };
108183
+ }
108184
+ var init_job_outcome_subscription = __esm(() => {
108185
+ init_api3();
108186
+ });
108187
+
108135
108188
  // src/daemon/entry/enhancer/wait-for-enhancer-job.ts
108136
108189
  async function waitForEnhancerJobResolution(params) {
108137
- const { sessionId, chatroomId, jobId, backend: backend2, onFailure, onSalvageComplete } = params;
108190
+ const { sessionId, chatroomId, jobId, wsClient: wsClient2, onFailure, onSalvageComplete } = params;
108138
108191
  let outcome = null;
108139
108192
  let salvagedText = "";
108140
- const pollInterval = setInterval(async () => {
108193
+ let agentEndTimer = null;
108194
+ let resolveWait = null;
108195
+ const waitPromise = new Promise((resolve5) => {
108196
+ resolveWait = resolve5;
108197
+ });
108198
+ const subscription = subscribeToEnhancerJobOutcome({
108199
+ wsClient: wsClient2,
108200
+ sessionId,
108201
+ chatroomId,
108202
+ jobId
108203
+ });
108204
+ const finish = (resolution) => {
108141
108205
  if (outcome)
108142
108206
  return;
108143
- try {
108144
- const status3 = await backend2.query(api.web.enhancer.index.getJob, {
108145
- sessionId,
108146
- chatroomId,
108147
- jobId
108148
- });
108149
- if (status3?.status === "complete") {
108150
- outcome = "complete";
108151
- writeEnhancerLog(`completed job=${jobId}`);
108152
- }
108153
- } catch {}
108154
- }, ENHANCER_JOB_POLL_INTERVAL_MS);
108207
+ outcome = resolution;
108208
+ resolveWait?.();
108209
+ resolveWait = null;
108210
+ };
108211
+ const failAfterAgentEnd = () => {
108212
+ writeEnhancerLog("agent_end: turn ended without complete — failing terminal");
108213
+ onFailure("Agent exited without completing enhancer job", true);
108214
+ finish("failed");
108215
+ };
108216
+ subscription.outcome.then((state) => {
108217
+ if (outcome)
108218
+ return;
108219
+ const resolution = state.status === "complete" ? "complete" : "failed";
108220
+ if (resolution === "complete")
108221
+ writeEnhancerLog(`completed job=${jobId}`);
108222
+ finish(resolution);
108223
+ }).catch((error51) => {
108224
+ if (outcome)
108225
+ return;
108226
+ writeEnhancerLog(`enhancer outcome subscription failed: ${error51 instanceof Error ? error51.message : String(error51)}`);
108227
+ onFailure("Enhancer job outcome subscription failed", false);
108228
+ finish("failed");
108229
+ });
108155
108230
  params.onAssistantText?.((text) => {
108156
108231
  salvagedText += text;
108157
108232
  });
108158
108233
  params.onAgentEnd?.(() => {
108159
108234
  if (outcome)
108160
108235
  return;
108161
- const check4 = () => {
108236
+ agentEndTimer = setTimeout(() => {
108162
108237
  if (outcome)
108163
108238
  return;
108164
- backend2.query(api.web.enhancer.index.getJob, {
108165
- sessionId,
108166
- chatroomId,
108167
- jobId
108168
- }).then((status3) => {
108169
- if (outcome)
108170
- return;
108171
- if (status3?.status === "complete") {
108172
- outcome = "complete";
108173
- return;
108174
- }
108175
- if (status3?.status === "running") {
108176
- const trimmed = salvagedText.trim();
108177
- if (trimmed && onSalvageComplete) {
108178
- onSalvageComplete(trimmed).then(() => {
108179
- if (outcome)
108180
- return;
108181
- backend2.query(api.web.enhancer.index.getJob, {
108182
- sessionId,
108183
- chatroomId,
108184
- jobId
108185
- }).then((afterSalvage) => {
108186
- if (afterSalvage?.status === "complete") {
108187
- outcome = "complete";
108188
- writeEnhancerLog("agent_end: salvaged assistant text via complete");
108189
- return;
108190
- }
108191
- outcome = "failed";
108192
- writeEnhancerLog("agent_end: turn ended without complete — failing terminal");
108193
- onFailure("Agent exited without completing enhancer job", true);
108194
- });
108195
- }).catch(() => {
108196
- outcome = "failed";
108197
- writeEnhancerLog("agent_end: turn ended without complete — failing terminal");
108198
- onFailure("Agent exited without completing enhancer job", true);
108199
- });
108200
- } else {
108201
- outcome = "failed";
108202
- writeEnhancerLog("agent_end: turn ended without complete — failing terminal");
108203
- onFailure("Agent exited without completing enhancer job", true);
108204
- }
108205
- }
108239
+ const state = subscription.getCurrentState();
108240
+ if (state?.status === "complete") {
108241
+ finish("complete");
108242
+ return;
108243
+ }
108244
+ if (state?.status !== "running") {
108245
+ failAfterAgentEnd();
108246
+ return;
108247
+ }
108248
+ const trimmed = salvagedText.trim();
108249
+ if (!trimmed || !onSalvageComplete) {
108250
+ failAfterAgentEnd();
108251
+ return;
108252
+ }
108253
+ onSalvageComplete(trimmed).catch(() => {
108254
+ failAfterAgentEnd();
108206
108255
  });
108207
- };
108208
- setTimeout(check4, ENHANCER_AGENT_END_GRACE_MS);
108256
+ }, ENHANCER_AGENT_END_GRACE_MS);
108209
108257
  });
108210
108258
  params.onExit(() => {
108211
108259
  if (outcome)
108212
108260
  return;
108213
- outcome = "failed";
108214
108261
  onFailure("Agent process exited without completing enhancer job", false);
108262
+ finish("failed");
108215
108263
  });
108216
- await new Promise((resolve5) => {
108217
- const check4 = setInterval(() => {
108218
- if (outcome) {
108219
- clearInterval(check4);
108220
- resolve5();
108221
- }
108222
- }, 100);
108223
- });
108224
- clearInterval(pollInterval);
108264
+ await waitPromise;
108265
+ if (agentEndTimer)
108266
+ clearTimeout(agentEndTimer);
108267
+ subscription.stop();
108225
108268
  return outcome ?? "failed";
108226
108269
  }
108227
108270
  var init_wait_for_enhancer_job = __esm(() => {
108228
- init_api3();
108271
+ init_job_outcome_subscription();
108229
108272
  });
108230
108273
 
108231
108274
  // src/daemon/entry/enhancer-inbound-registry.ts
@@ -108242,7 +108285,7 @@ async function dispatchEnhancerInboundEvent(event) {
108242
108285
  var handler3;
108243
108286
 
108244
108287
  // src/daemon/entry/enhancer/job-subscriber.ts
108245
- async function processEnhancerJobForSpawn(sessionId, machineId, convexUrl, backend2, agentServices, job, inFlight2) {
108288
+ async function processEnhancerJobForSpawn(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices, job, inFlight2) {
108246
108289
  if (inFlight2.has(job.jobId))
108247
108290
  return;
108248
108291
  inFlight2.add(job.jobId);
@@ -108298,7 +108341,7 @@ async function processEnhancerJobForSpawn(sessionId, machineId, convexUrl, backe
108298
108341
  sessionId,
108299
108342
  chatroomId: payload.chatroomId,
108300
108343
  jobId: payload.jobId,
108301
- backend: backend2,
108344
+ wsClient: wsClient2,
108302
108345
  onAssistantText: spawned.onAssistantText ? (cb) => spawned.onAssistantText?.(cb) : undefined,
108303
108346
  onAgentEnd: spawned.onAgentEnd ? (cb) => spawned.onAgentEnd?.(cb) : undefined,
108304
108347
  onExit: (cb) => spawned.onExit(() => cb()),
@@ -108340,30 +108383,30 @@ async function processEnhancerJobForSpawn(sessionId, machineId, convexUrl, backe
108340
108383
  }
108341
108384
  }
108342
108385
  }
108343
- function processEnhancerJobs(sessionId, machineId, convexUrl, backend2, agentServices, jobs, inFlight2) {
108386
+ function processEnhancerJobs(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices, jobs, inFlight2) {
108344
108387
  for (const job of jobs ?? []) {
108345
- processEnhancerJobForSpawn(sessionId, machineId, convexUrl, backend2, agentServices, job, inFlight2);
108388
+ processEnhancerJobForSpawn(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices, job, inFlight2);
108346
108389
  }
108347
108390
  }
108348
- async function drainPendingEnhancerJobs(sessionId, machineId, convexUrl, backend2, agentServices, inFlight2) {
108391
+ async function drainPendingEnhancerJobs(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices, inFlight2) {
108349
108392
  const jobs = await backend2.query(api.daemon.enhancer.index.pendingForMachine, {
108350
108393
  sessionId,
108351
108394
  machineId
108352
108395
  });
108353
108396
  if (!jobs?.length)
108354
108397
  return;
108355
- processEnhancerJobs(sessionId, machineId, convexUrl, backend2, agentServices, jobs, inFlight2);
108398
+ processEnhancerJobs(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices, jobs, inFlight2);
108356
108399
  }
108357
- function startEnhancerJobSubscriber(sessionId, machineId, convexUrl, backend2, agentServices) {
108400
+ function startEnhancerJobSubscriber(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices) {
108358
108401
  const inFlight2 = new Set;
108359
108402
  registerEnhancerInboundHandler(async () => {
108360
- await drainPendingEnhancerJobs(sessionId, machineId, convexUrl, backend2, agentServices, inFlight2);
108403
+ await drainPendingEnhancerJobs(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices, inFlight2);
108361
108404
  });
108362
108405
  return {
108363
108406
  stop: () => {
108364
108407
  unregisterEnhancerInboundHandler();
108365
108408
  },
108366
- drainPendingEnhancerJobs: () => drainPendingEnhancerJobs(sessionId, machineId, convexUrl, backend2, agentServices, inFlight2)
108409
+ drainPendingEnhancerJobs: () => drainPendingEnhancerJobs(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices, inFlight2)
108367
108410
  };
108368
108411
  }
108369
108412
  var init_job_subscriber = __esm(() => {
@@ -108372,8 +108415,8 @@ var init_job_subscriber = __esm(() => {
108372
108415
  });
108373
108416
 
108374
108417
  // src/daemon/entry/enhancer/start-subscriptions.ts
108375
- function startEnhancerSubscriptions(sessionId, machineId, convexUrl, backend2, agentServices) {
108376
- return startEnhancerJobSubscriber(sessionId, machineId, convexUrl, backend2, agentServices);
108418
+ function startEnhancerSubscriptions(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices) {
108419
+ return startEnhancerJobSubscriber(sessionId, machineId, convexUrl, wsClient2, backend2, agentServices);
108377
108420
  }
108378
108421
  var init_start_subscriptions2 = __esm(() => {
108379
108422
  init_job_subscriber();
@@ -114660,7 +114703,7 @@ function createDaemonRuntime(deps) {
114660
114703
  backend: session2.backend,
114661
114704
  convexUrl: session2.convexUrl
114662
114705
  }, activeSessions, harnesses);
114663
- enhancerWorkerHandle = startEnhancerSubscriptions(session2.sessionId, session2.machineId, session2.convexUrl, session2.backend, session2.agentServices);
114706
+ enhancerWorkerHandle = startEnhancerSubscriptions(session2.sessionId, session2.machineId, session2.convexUrl, deps.wsClient, session2.backend, session2.agentServices);
114664
114707
  }
114665
114708
  console.log(`
114666
114709
  Listening for commands...`);
@@ -117671,4 +117714,4 @@ program2.hook("preAction", async (_thisCommand, actionCommand) => {
117671
117714
  });
117672
117715
  program2.parse();
117673
117716
 
117674
- //# debugId=D279A4D8F6CF2F1C64756E2164756E21
117717
+ //# debugId=E1D1B59D5776760164756E2164756E21