@defend-tech/opencode-optima 0.1.63 → 0.1.65

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
@@ -9337,16 +9337,11 @@ async function syncOpenChamberWorktreeVisibility({ openchamberBaseUrl, opencodeB
9337
9337
  gitWorktree,
9338
9338
  projectId: project.id
9339
9339
  };
9340
- if (!visibility.worktree || !visibility.workspace || !visibility.projectDirectory) {
9340
+ if (!visibility.worktree || !visibility.projectDirectory) {
9341
9341
  throw new Error(`OpenCode visibility verification failed for ${worktreePath}: ${JSON.stringify(visibility)}`);
9342
9342
  }
9343
9343
  return visibility;
9344
9344
  }
9345
- function assertOpenChamberClickUpWorktreePath({ baseWorktree, worktreePath } = {}) {
9346
- if (!isClickUpDerivedWorktreeSibling(worktreePath, baseWorktree)) {
9347
- throw new Error(`OpenChamber worktree path is outside the configured ClickUp sibling scope: ${worktreePath}`);
9348
- }
9349
- }
9350
9345
  async function createOpenChamberClickUpWorktree({ openchamberBaseUrl, baseUrl, baseWorktree, branch, worktreePath, startPoint, branchExists = false, fetchImpl = globalThis.fetch } = {}) {
9351
9346
  const effectiveOpenChamberBaseUrl = openchamberBaseUrl || baseUrl;
9352
9347
  const worktreeName = clickUpOpenChamberWorktreeName(branch);
@@ -9369,7 +9364,6 @@ async function createOpenChamberClickUpWorktree({ openchamberBaseUrl, baseUrl, b
9369
9364
  return { created, worktree: path5.resolve(createdDirectory), branch: createdBranch, verified };
9370
9365
  }
9371
9366
  async function registerOpenChamberClickUpWorktree({ openchamberBaseUrl, opencodeBaseUrl, baseUrl, baseWorktree, branch, worktreePath, fetchImpl = globalThis.fetch, source = "reuse" } = {}) {
9372
- assertOpenChamberClickUpWorktreePath({ baseWorktree, worktreePath });
9373
9367
  const visibility = await syncOpenChamberWorktreeVisibility({ openchamberBaseUrl: openchamberBaseUrl || baseUrl, opencodeBaseUrl: opencodeBaseUrl || baseUrl, baseWorktree, worktreePath, branch, fetchImpl });
9374
9368
  return { branch, worktree: path5.resolve(worktreePath), reused: true, provider: "openchamber", openChamber: { source, visibility } };
9375
9369
  }
@@ -9423,30 +9417,6 @@ async function ensureClickUpTaskWorktreeForWebhook({ opencodeBaseUrl = "", openc
9423
9417
  } catch (error) {
9424
9418
  const message = `OpenChamber worktree provisioning failed for ${options.taskId || "unknown task"}; raw git fallback is disabled to avoid invisible worktrees. ${error.message}`;
9425
9419
  appendClickUpWebhookLocalLog(webhookWorktree, { type: "openchamber_worktree_failed", taskId: options.taskId || null, message });
9426
- if (typeof clickupClient?.postTaskComment === "function") {
9427
- const taskId = options.taskId || "unknown task";
9428
- const ledgerPath = clickUpCommentLedgerPath(webhookWorktree);
9429
- const comment = `${message}
9430
-
9431
- Optima did not run raw git worktree fallback. Please verify clickup.openchamber.base_url and OpenCode/OpenChamber endpoint configuration.`;
9432
- let dedupe = { post: true, key: clickUpWorktreeFailureCommentKey({ taskId, message }) };
9433
- try {
9434
- dedupe = shouldPostClickUpWorktreeFailureComment({ ledgerPath, taskId, message });
9435
- } catch (ledgerError) {
9436
- appendClickUpWebhookLocalLog(webhookWorktree, { type: "openchamber_worktree_comment_dedupe_failed", taskId, message: ledgerError.message });
9437
- }
9438
- if (dedupe.post) {
9439
- try {
9440
- await clickupClient.postTaskComment({ taskId: options.taskId, comment });
9441
- recordClickUpWorktreeFailureComment({ ledgerPath, taskId, message, posted: true });
9442
- } catch (commentError) {
9443
- appendClickUpWebhookLocalLog(webhookWorktree, { type: "openchamber_worktree_blocker_comment_failed", taskId: options.taskId || null, message: commentError.message });
9444
- }
9445
- } else {
9446
- appendClickUpWebhookLocalLog(webhookWorktree, { type: "openchamber_worktree_blocker_comment_deduped", taskId, key: dedupe.key, reason: dedupe.reason });
9447
- recordClickUpWorktreeFailureComment({ ledgerPath, taskId, message, posted: false, skippedReason: dedupe.reason });
9448
- }
9449
- }
9450
9420
  throw new Error(message);
9451
9421
  }
9452
9422
  }
@@ -10392,35 +10362,6 @@ function recordClickUpCommentVersionProcessed({ ledgerPath, key, taskId, eventTy
10392
10362
  recordedAt: at.toISOString()
10393
10363
  });
10394
10364
  }
10395
- function clickUpWorktreeFailureCommentKey({ taskId, message } = {}) {
10396
- const hash = crypto.createHash("sha256").update(String(message || "")).digest("hex").slice(0, 16);
10397
- return [String(taskId || "unknown").trim() || "unknown", "worktree_failure", hash].join(":");
10398
- }
10399
- function shouldPostClickUpWorktreeFailureComment({ ledgerPath, taskId, message, now = /* @__PURE__ */ new Date(), windowMs = CLICKUP_WORKTREE_FAILURE_COMMENT_DEDUPE_MS } = {}) {
10400
- const key = clickUpWorktreeFailureCommentKey({ taskId, message });
10401
- const nowMs = now instanceof Date ? now.getTime() : new Date(now).getTime();
10402
- for (const entry of readClickUpCommentLedgerEntries(ledgerPath).reverse()) {
10403
- if (entry?.key !== key) continue;
10404
- const postedAtMs = new Date(entry.postedAt || entry.recordedAt || entry.at || 0).getTime();
10405
- if (Number.isFinite(postedAtMs) && Number.isFinite(nowMs) && nowMs - postedAtMs <= windowMs) {
10406
- return { post: false, key, reason: "recent_duplicate" };
10407
- }
10408
- if (entry?.message === message) return { post: false, key, reason: "latest_equivalent" };
10409
- }
10410
- return { post: true, key };
10411
- }
10412
- function recordClickUpWorktreeFailureComment({ ledgerPath, taskId, message, posted = true, skippedReason = null, at = /* @__PURE__ */ new Date() } = {}) {
10413
- appendClickUpCommentLedgerEntry(ledgerPath, {
10414
- key: clickUpWorktreeFailureCommentKey({ taskId, message }),
10415
- taskId,
10416
- eventType: "worktree_failure",
10417
- action: posted ? "worktree_failure_comment_posted" : "worktree_failure_comment_skipped",
10418
- message,
10419
- posted,
10420
- skippedReason,
10421
- postedAt: at.toISOString()
10422
- });
10423
- }
10424
10365
  function clickUpTaskIdFromPayload(payload = {}) {
10425
10366
  return String(payload.task_id || payload.taskId || payload.task?.id || payload.task?.task_id || "").trim();
10426
10367
  }
@@ -10983,15 +10924,9 @@ async function verifyOpenCodeSessionEventDelivery(client, { sessionId, directory
10983
10924
  return { ok: false, reason: lastError };
10984
10925
  }
10985
10926
  async function postClickUpLaunchFailureComment({ clickupClient, worktree, taskId, reason, source } = {}) {
10986
- if (typeof clickupClient?.postTaskComment !== "function") return { ok: false, skipped: true, reason: "post_comment_unavailable" };
10987
- try {
10988
- await clickupClient.postTaskComment({ taskId, comment: `Optima launch failure: ${reason || "launch_failed"}. Worktree visibility alone is not successful routing; Optima requires an OpenCode session plus verified prompt admission/delivery.` });
10989
- appendClickUpWebhookLocalLog(worktree, { type: "launch_failure_comment_posted", taskId, reason, source });
10990
- return { ok: true };
10991
- } catch (error) {
10992
- appendClickUpWebhookLocalLog(worktree, { type: "launch_failure_comment_failed", taskId, reason, source, message: error.message });
10993
- return { ok: false, error: error.message };
10994
- }
10927
+ void clickupClient;
10928
+ appendClickUpWebhookLocalLog(worktree, { type: "launch_failure_comment_skipped", taskId, reason, source, policy: "local_log_only" });
10929
+ return { ok: false, skipped: true, reason: "local_log_only" };
10995
10930
  }
10996
10931
  async function applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason, source, tagName = CLICKUP_BLOCKER_TAG_NAME } = {}) {
10997
10932
  if (typeof clickupClient?.addTaskTag !== "function") {
@@ -11352,7 +11287,9 @@ async function routeClickUpWebhookEventUnlocked({ payload, config, state = {}, w
11352
11287
  const sessionTitle = formatClickUpSessionTitle({ taskId, payloadTask: payload.task, task });
11353
11288
  const existingMetadata = clickUpTaskAgentMetadata(task, config.routing.metadataFieldId);
11354
11289
  const metadata = normalizeAgentMetadataJson(existingMetadata);
11355
- const existingSessionId = getNestedMetadataValue(metadata, config.routing.metadataKey);
11290
+ const rawExistingSessionId = getNestedMetadataValue(metadata, config.routing.metadataKey);
11291
+ const existingSessionId = typeof rawExistingSessionId === "string" && rawExistingSessionId.startsWith("ses_") ? rawExistingSessionId : "";
11292
+ if (rawExistingSessionId !== void 0 && !existingSessionId) appendClickUpWebhookLocalLog(worktree, { type: "pm_session_metadata_ignored", taskId, valueType: typeof rawExistingSessionId, reason: "not_session_id" });
11356
11293
  const taskType = clickUpTaskType(task);
11357
11294
  const parentTaskId = clickUpParentTaskId(task) || taskId;
11358
11295
  const subtaskId = parentTaskId && parentTaskId !== taskId ? taskId : "";
@@ -11382,6 +11319,12 @@ async function routeClickUpWebhookEventUnlocked({ payload, config, state = {}, w
11382
11319
  const prompt = formatClickUpWebhookPrompt({ eventType, taskId, payload, branch: taskRoute.branch, worktree: taskRoute.worktree, deliveryEvidencePath, humanRoleContext });
11383
11320
  if (!existingSessionId) {
11384
11321
  let pendingSessionId = getNestedMetadataValue(metadata, clickUpPendingSessionKey(config.routing.metadataKey)) || state.pendingSessions?.[taskId];
11322
+ if (pendingSessionId && !await sessionExists(openCodeClient, pendingSessionId, { directory: taskRoute.worktree })) {
11323
+ appendClickUpWebhookLocalLog(worktree, { type: "pending_session_stale", taskId, sessionId: pendingSessionId, worktree: taskRoute.worktree });
11324
+ const { [taskId]: _stalePending, ...remainingPending2 } = stateToPersist.pendingSessions || {};
11325
+ stateToPersist = { ...stateToPersist, pendingSessions: remainingPending2 };
11326
+ pendingSessionId = "";
11327
+ }
11385
11328
  if (!pendingSessionId) {
11386
11329
  try {
11387
11330
  pendingSessionId = await createSession(openCodeClient, { title: sessionTitle, directory: taskRoute.worktree, agent: config.routing.targetAgent });
@@ -9344,16 +9344,11 @@ async function syncOpenChamberWorktreeVisibility({ openchamberBaseUrl, opencodeB
9344
9344
  gitWorktree,
9345
9345
  projectId: project.id
9346
9346
  };
9347
- if (!visibility.worktree || !visibility.workspace || !visibility.projectDirectory) {
9347
+ if (!visibility.worktree || !visibility.projectDirectory) {
9348
9348
  throw new Error(`OpenCode visibility verification failed for ${worktreePath}: ${JSON.stringify(visibility)}`);
9349
9349
  }
9350
9350
  return visibility;
9351
9351
  }
9352
- function assertOpenChamberClickUpWorktreePath({ baseWorktree, worktreePath } = {}) {
9353
- if (!isClickUpDerivedWorktreeSibling(worktreePath, baseWorktree)) {
9354
- throw new Error(`OpenChamber worktree path is outside the configured ClickUp sibling scope: ${worktreePath}`);
9355
- }
9356
- }
9357
9352
  async function createOpenChamberClickUpWorktree({ openchamberBaseUrl, baseUrl, baseWorktree, branch, worktreePath, startPoint, branchExists = false, fetchImpl = globalThis.fetch } = {}) {
9358
9353
  const effectiveOpenChamberBaseUrl = openchamberBaseUrl || baseUrl;
9359
9354
  const worktreeName = clickUpOpenChamberWorktreeName(branch);
@@ -9376,7 +9371,6 @@ async function createOpenChamberClickUpWorktree({ openchamberBaseUrl, baseUrl, b
9376
9371
  return { created, worktree: path5.resolve(createdDirectory), branch: createdBranch, verified };
9377
9372
  }
9378
9373
  async function registerOpenChamberClickUpWorktree({ openchamberBaseUrl, opencodeBaseUrl, baseUrl, baseWorktree, branch, worktreePath, fetchImpl = globalThis.fetch, source = "reuse" } = {}) {
9379
- assertOpenChamberClickUpWorktreePath({ baseWorktree, worktreePath });
9380
9374
  const visibility = await syncOpenChamberWorktreeVisibility({ openchamberBaseUrl: openchamberBaseUrl || baseUrl, opencodeBaseUrl: opencodeBaseUrl || baseUrl, baseWorktree, worktreePath, branch, fetchImpl });
9381
9375
  return { branch, worktree: path5.resolve(worktreePath), reused: true, provider: "openchamber", openChamber: { source, visibility } };
9382
9376
  }
@@ -9430,30 +9424,6 @@ async function ensureClickUpTaskWorktreeForWebhook({ opencodeBaseUrl = "", openc
9430
9424
  } catch (error) {
9431
9425
  const message = `OpenChamber worktree provisioning failed for ${options.taskId || "unknown task"}; raw git fallback is disabled to avoid invisible worktrees. ${error.message}`;
9432
9426
  appendClickUpWebhookLocalLog(webhookWorktree, { type: "openchamber_worktree_failed", taskId: options.taskId || null, message });
9433
- if (typeof clickupClient?.postTaskComment === "function") {
9434
- const taskId = options.taskId || "unknown task";
9435
- const ledgerPath = clickUpCommentLedgerPath(webhookWorktree);
9436
- const comment = `${message}
9437
-
9438
- Optima did not run raw git worktree fallback. Please verify clickup.openchamber.base_url and OpenCode/OpenChamber endpoint configuration.`;
9439
- let dedupe = { post: true, key: clickUpWorktreeFailureCommentKey({ taskId, message }) };
9440
- try {
9441
- dedupe = shouldPostClickUpWorktreeFailureComment({ ledgerPath, taskId, message });
9442
- } catch (ledgerError) {
9443
- appendClickUpWebhookLocalLog(webhookWorktree, { type: "openchamber_worktree_comment_dedupe_failed", taskId, message: ledgerError.message });
9444
- }
9445
- if (dedupe.post) {
9446
- try {
9447
- await clickupClient.postTaskComment({ taskId: options.taskId, comment });
9448
- recordClickUpWorktreeFailureComment({ ledgerPath, taskId, message, posted: true });
9449
- } catch (commentError) {
9450
- appendClickUpWebhookLocalLog(webhookWorktree, { type: "openchamber_worktree_blocker_comment_failed", taskId: options.taskId || null, message: commentError.message });
9451
- }
9452
- } else {
9453
- appendClickUpWebhookLocalLog(webhookWorktree, { type: "openchamber_worktree_blocker_comment_deduped", taskId, key: dedupe.key, reason: dedupe.reason });
9454
- recordClickUpWorktreeFailureComment({ ledgerPath, taskId, message, posted: false, skippedReason: dedupe.reason });
9455
- }
9456
- }
9457
9427
  throw new Error(message);
9458
9428
  }
9459
9429
  }
@@ -10399,35 +10369,6 @@ function recordClickUpCommentVersionProcessed({ ledgerPath, key, taskId, eventTy
10399
10369
  recordedAt: at.toISOString()
10400
10370
  });
10401
10371
  }
10402
- function clickUpWorktreeFailureCommentKey({ taskId, message } = {}) {
10403
- const hash = crypto.createHash("sha256").update(String(message || "")).digest("hex").slice(0, 16);
10404
- return [String(taskId || "unknown").trim() || "unknown", "worktree_failure", hash].join(":");
10405
- }
10406
- function shouldPostClickUpWorktreeFailureComment({ ledgerPath, taskId, message, now = /* @__PURE__ */ new Date(), windowMs = CLICKUP_WORKTREE_FAILURE_COMMENT_DEDUPE_MS } = {}) {
10407
- const key = clickUpWorktreeFailureCommentKey({ taskId, message });
10408
- const nowMs = now instanceof Date ? now.getTime() : new Date(now).getTime();
10409
- for (const entry of readClickUpCommentLedgerEntries(ledgerPath).reverse()) {
10410
- if (entry?.key !== key) continue;
10411
- const postedAtMs = new Date(entry.postedAt || entry.recordedAt || entry.at || 0).getTime();
10412
- if (Number.isFinite(postedAtMs) && Number.isFinite(nowMs) && nowMs - postedAtMs <= windowMs) {
10413
- return { post: false, key, reason: "recent_duplicate" };
10414
- }
10415
- if (entry?.message === message) return { post: false, key, reason: "latest_equivalent" };
10416
- }
10417
- return { post: true, key };
10418
- }
10419
- function recordClickUpWorktreeFailureComment({ ledgerPath, taskId, message, posted = true, skippedReason = null, at = /* @__PURE__ */ new Date() } = {}) {
10420
- appendClickUpCommentLedgerEntry(ledgerPath, {
10421
- key: clickUpWorktreeFailureCommentKey({ taskId, message }),
10422
- taskId,
10423
- eventType: "worktree_failure",
10424
- action: posted ? "worktree_failure_comment_posted" : "worktree_failure_comment_skipped",
10425
- message,
10426
- posted,
10427
- skippedReason,
10428
- postedAt: at.toISOString()
10429
- });
10430
- }
10431
10372
  function clickUpTaskIdFromPayload(payload = {}) {
10432
10373
  return String(payload.task_id || payload.taskId || payload.task?.id || payload.task?.task_id || "").trim();
10433
10374
  }
@@ -10990,15 +10931,9 @@ async function verifyOpenCodeSessionEventDelivery(client, { sessionId, directory
10990
10931
  return { ok: false, reason: lastError };
10991
10932
  }
10992
10933
  async function postClickUpLaunchFailureComment({ clickupClient, worktree, taskId, reason, source } = {}) {
10993
- if (typeof clickupClient?.postTaskComment !== "function") return { ok: false, skipped: true, reason: "post_comment_unavailable" };
10994
- try {
10995
- await clickupClient.postTaskComment({ taskId, comment: `Optima launch failure: ${reason || "launch_failed"}. Worktree visibility alone is not successful routing; Optima requires an OpenCode session plus verified prompt admission/delivery.` });
10996
- appendClickUpWebhookLocalLog(worktree, { type: "launch_failure_comment_posted", taskId, reason, source });
10997
- return { ok: true };
10998
- } catch (error) {
10999
- appendClickUpWebhookLocalLog(worktree, { type: "launch_failure_comment_failed", taskId, reason, source, message: error.message });
11000
- return { ok: false, error: error.message };
11001
- }
10934
+ void clickupClient;
10935
+ appendClickUpWebhookLocalLog(worktree, { type: "launch_failure_comment_skipped", taskId, reason, source, policy: "local_log_only" });
10936
+ return { ok: false, skipped: true, reason: "local_log_only" };
11002
10937
  }
11003
10938
  async function applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason, source, tagName = CLICKUP_BLOCKER_TAG_NAME } = {}) {
11004
10939
  if (typeof clickupClient?.addTaskTag !== "function") {
@@ -11359,7 +11294,9 @@ async function routeClickUpWebhookEventUnlocked({ payload, config, state = {}, w
11359
11294
  const sessionTitle = formatClickUpSessionTitle({ taskId, payloadTask: payload.task, task });
11360
11295
  const existingMetadata = clickUpTaskAgentMetadata(task, config.routing.metadataFieldId);
11361
11296
  const metadata = normalizeAgentMetadataJson(existingMetadata);
11362
- const existingSessionId = getNestedMetadataValue(metadata, config.routing.metadataKey);
11297
+ const rawExistingSessionId = getNestedMetadataValue(metadata, config.routing.metadataKey);
11298
+ const existingSessionId = typeof rawExistingSessionId === "string" && rawExistingSessionId.startsWith("ses_") ? rawExistingSessionId : "";
11299
+ if (rawExistingSessionId !== void 0 && !existingSessionId) appendClickUpWebhookLocalLog(worktree, { type: "pm_session_metadata_ignored", taskId, valueType: typeof rawExistingSessionId, reason: "not_session_id" });
11363
11300
  const taskType = clickUpTaskType(task);
11364
11301
  const parentTaskId = clickUpParentTaskId(task) || taskId;
11365
11302
  const subtaskId = parentTaskId && parentTaskId !== taskId ? taskId : "";
@@ -11389,6 +11326,12 @@ async function routeClickUpWebhookEventUnlocked({ payload, config, state = {}, w
11389
11326
  const prompt = formatClickUpWebhookPrompt({ eventType, taskId, payload, branch: taskRoute.branch, worktree: taskRoute.worktree, deliveryEvidencePath, humanRoleContext });
11390
11327
  if (!existingSessionId) {
11391
11328
  let pendingSessionId = getNestedMetadataValue(metadata, clickUpPendingSessionKey(config.routing.metadataKey)) || state.pendingSessions?.[taskId];
11329
+ if (pendingSessionId && !await sessionExists(openCodeClient, pendingSessionId, { directory: taskRoute.worktree })) {
11330
+ appendClickUpWebhookLocalLog(worktree, { type: "pending_session_stale", taskId, sessionId: pendingSessionId, worktree: taskRoute.worktree });
11331
+ const { [taskId]: _stalePending, ...remainingPending2 } = stateToPersist.pendingSessions || {};
11332
+ stateToPersist = { ...stateToPersist, pendingSessions: remainingPending2 };
11333
+ pendingSessionId = "";
11334
+ }
11392
11335
  if (!pendingSessionId) {
11393
11336
  try {
11394
11337
  pendingSessionId = await createSession(openCodeClient, { title: sessionTitle, directory: taskRoute.worktree, agent: config.routing.targetAgent });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defend-tech/opencode-optima",
3
- "version": "0.1.63",
3
+ "version": "0.1.65",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+ssh://git@github.com/defend-tech/opencode-optima.git"