@defend-tech/opencode-optima 0.1.51 → 0.1.52

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
@@ -9767,7 +9767,17 @@ async function sendOpenCodeSessionEventDirect({ baseUrl, sessionId, text, agent,
9767
9767
  }
9768
9768
  throw firstError || new Error("OpenCode direct prompt delivery failed.");
9769
9769
  }
9770
- async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, structuredPayload) {
9770
+ function tagOpenCodePromptResult(result, deliveryMethod) {
9771
+ if (result && typeof result === "object") {
9772
+ try {
9773
+ Object.defineProperty(result, "__optimaPromptDelivery", { value: deliveryMethod, enumerable: false, configurable: true });
9774
+ } catch {
9775
+ return { result, __optimaPromptDelivery: deliveryMethod };
9776
+ }
9777
+ }
9778
+ return result;
9779
+ }
9780
+ async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, structuredPayload, deliveryMethod) {
9771
9781
  const attempts = [
9772
9782
  { ...structuredPayload, path: { id: sessionId } },
9773
9783
  { ...structuredPayload, path: { sessionID: sessionId } },
@@ -9777,14 +9787,14 @@ async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, s
9777
9787
  let firstError = null;
9778
9788
  for (const attempt of attempts) {
9779
9789
  try {
9780
- return assertOpenCodePromptAccepted(await method(attempt));
9790
+ return tagOpenCodePromptResult(assertOpenCodePromptAccepted(await method(attempt)), deliveryMethod);
9781
9791
  } catch (error) {
9782
9792
  firstError ??= error;
9783
9793
  }
9784
9794
  }
9785
9795
  throw firstError;
9786
9796
  }
9787
- async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false, legacyOnly = false } = {}) {
9797
+ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false, legacyOnly = false, allowDirectFallback = true } = {}) {
9788
9798
  const directBaseUrl = opencodeBaseUrl || baseUrl;
9789
9799
  const parts = [{ type: "text", text }];
9790
9800
  const flatPayload = { directory, agent, parts };
@@ -9794,21 +9804,21 @@ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, direct
9794
9804
  body: { agent, parts }
9795
9805
  };
9796
9806
  let firstError = null;
9797
- if (!direct && typeof client?.session?.promptAsync === "function") {
9807
+ if (!direct && typeof client?.session?.prompt === "function") {
9798
9808
  try {
9799
- return await callOpenCodePromptWithFallbacks(client.session.promptAsync.bind(client.session), sessionId, flatPayload, structuredPayload);
9809
+ return await callOpenCodePromptWithFallbacks(client.session.prompt.bind(client.session), sessionId, flatPayload, structuredPayload, "prompt");
9800
9810
  } catch (error) {
9801
9811
  firstError ??= error;
9802
9812
  }
9803
9813
  }
9804
- if (!direct && typeof client?.session?.prompt === "function") {
9814
+ if (!direct && typeof client?.session?.promptAsync === "function") {
9805
9815
  try {
9806
- return await callOpenCodePromptWithFallbacks(client.session.prompt.bind(client.session), sessionId, flatPayload, structuredPayload);
9816
+ return await callOpenCodePromptWithFallbacks(client.session.promptAsync.bind(client.session), sessionId, flatPayload, structuredPayload, "prompt_async");
9807
9817
  } catch (error) {
9808
9818
  firstError ??= error;
9809
9819
  }
9810
9820
  }
9811
- if (directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, directory, fetchImpl, legacyOnly });
9821
+ if (allowDirectFallback && directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, directory, fetchImpl, legacyOnly });
9812
9822
  if (firstError) throw firstError;
9813
9823
  throw new Error("OpenCode client does not expose session.prompt or session.promptAsync.");
9814
9824
  }
@@ -9886,46 +9896,44 @@ async function applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason,
9886
9896
  return { ok: false, error: error.message, tagName };
9887
9897
  }
9888
9898
  }
9899
+ function openCodeBlockingPromptVerification(result, sessionId) {
9900
+ if (result?.__optimaPromptDelivery !== "prompt") return null;
9901
+ const response = result?.response ?? result;
9902
+ const data = response?.data ?? result?.data ?? null;
9903
+ const parts = normalizePromptResponseParts(result);
9904
+ const messageId = data?.id ?? response?.id ?? result?.id ?? data?.messageID ?? response?.messageID ?? result?.messageID;
9905
+ const deliveredSessionId = response?.sessionID ?? response?.sessionId ?? data?.sessionID ?? data?.sessionId ?? result?.sessionID ?? result?.sessionId;
9906
+ if (deliveredSessionId && String(deliveredSessionId) !== String(sessionId)) {
9907
+ throw new Error(`OpenCode blocking prompt targeted foreign session ${deliveredSessionId}.`);
9908
+ }
9909
+ if (parts.length > 0 || messageId) return { ok: true, method: parts.length > 0 ? "blocking_prompt_parts" : "blocking_prompt_message", messageId: messageId ? String(messageId) : null, sessionId: deliveredSessionId ? String(deliveredSessionId) : String(sessionId), parts: parts.length };
9910
+ return null;
9911
+ }
9889
9912
  async function deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent, text, directory, opencodeBaseUrl, eventMarkers = [], verifySessionEventDelivery = verifyOpenCodeSessionEventDelivery, applyBlockerOnFailure = true } = {}) {
9890
9913
  const beforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, limit: 50 }).catch(() => null);
9891
- const sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl });
9914
+ const sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, allowDirectFallback: false });
9915
+ let blockingPromptVerification = null;
9892
9916
  let admissionVerification = null;
9893
9917
  try {
9894
- admissionVerification = openCodePromptAdmissionVerification(sendResult, sessionId);
9918
+ blockingPromptVerification = openCodeBlockingPromptVerification(sendResult, sessionId);
9919
+ if (!blockingPromptVerification) admissionVerification = openCodePromptAdmissionVerification(sendResult, sessionId);
9895
9920
  } catch (error) {
9896
9921
  appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_failed", taskId, sessionId, reason: error.message, fallbackAttempted: false });
9897
9922
  if (!applyBlockerOnFailure) return { ok: false, action: "message_delivery_failed", reason: error.message, taskId, sessionId, fallbackAttempted: false };
9898
9923
  const blocker2 = await applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason: error.message, source: "delivery_admission_failed" });
9899
9924
  return { ok: false, action: "message_delivery_failed", reason: error.message, taskId, sessionId, fallbackAttempted: false, blockerTag: blocker2 };
9900
9925
  }
9926
+ if (blockingPromptVerification) return { ok: true, verification: blockingPromptVerification, admissionVerification: null, fallback: false };
9901
9927
  let verification = await verifySessionEventDelivery(openCodeClient, { sessionId, beforeMessages, expectedText: text, markers: eventMarkers });
9902
9928
  if (verification?.ok) return { ok: true, verification, admissionVerification, fallback: false };
9903
- if (verification?.reason === "message_verification_unavailable" && !admissionVerification) {
9904
- return { ok: true, verification: { ok: true, method: "legacy_prompt_accepted", skipped: true }, fallback: false };
9905
- }
9906
9929
  if (admissionVerification) {
9907
9930
  appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_admitted_but_invisible", taskId, sessionId, admission: admissionVerification, reason: verification?.reason || "message_not_visible" });
9908
9931
  }
9909
- const canFallbackDirect = Boolean(opencodeBaseUrl);
9910
- if (canFallbackDirect) {
9911
- const retryBeforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, limit: 50 }).catch(() => beforeMessages);
9912
- const retrySendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, direct: true, legacyOnly: Boolean(admissionVerification) });
9913
- try {
9914
- admissionVerification = openCodePromptAdmissionVerification(retrySendResult, sessionId);
9915
- } catch (error) {
9916
- verification = { ok: false, reason: error.message };
9917
- }
9918
- verification = await verifySessionEventDelivery(openCodeClient, { sessionId, beforeMessages: retryBeforeMessages, expectedText: text, markers: eventMarkers });
9919
- if (verification?.ok) return { ok: true, verification, admissionVerification, fallback: true };
9920
- if (verification?.reason === "message_verification_unavailable" && !admissionVerification) {
9921
- return { ok: true, verification: { ok: true, method: "legacy_prompt_accepted", skipped: true }, fallback: true };
9922
- }
9923
- }
9924
- const reason = verification?.reason || "message_delivery_failed";
9925
- appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_failed", taskId, sessionId, reason, fallbackAttempted: canFallbackDirect });
9926
- if (!applyBlockerOnFailure) return { ok: false, action: "message_delivery_failed", reason, taskId, sessionId, fallbackAttempted: canFallbackDirect };
9927
- const blocker = await applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason, source: canFallbackDirect ? "delivery_fallback_failed" : "delivery_verification_failed" });
9928
- return { ok: false, action: "message_delivery_failed", reason, taskId, sessionId, fallbackAttempted: canFallbackDirect, blockerTag: blocker };
9932
+ const reason = verification?.reason || (admissionVerification ? "prompt_admission_not_delivered" : "message_delivery_failed");
9933
+ appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_failed", taskId, sessionId, reason, fallbackAttempted: false, httpFallbackDisabled: Boolean(opencodeBaseUrl) });
9934
+ if (!applyBlockerOnFailure) return { ok: false, action: "message_delivery_failed", reason, taskId, sessionId, fallbackAttempted: false };
9935
+ const blocker = await applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason, source: "delivery_verification_failed" });
9936
+ return { ok: false, action: "message_delivery_failed", reason, taskId, sessionId, fallbackAttempted: false, blockerTag: blocker };
9929
9937
  }
9930
9938
  async function recoverClickUpPmSession({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, staleSessionId, sessionTitle, taskRoute, metadataWithRouting, config, prompt, eventMarkers = [], deliveryEvidencePath, evidencePath, eventKey, createSession, verifySessionEventDelivery } = {}) {
9931
9939
  appendClickUpWebhookLocalLog(worktree, { type: "pm_session_recovery_started", taskId, staleSessionId, worktree: taskRoute?.worktree });
@@ -9774,7 +9774,17 @@ async function sendOpenCodeSessionEventDirect({ baseUrl, sessionId, text, agent,
9774
9774
  }
9775
9775
  throw firstError || new Error("OpenCode direct prompt delivery failed.");
9776
9776
  }
9777
- async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, structuredPayload) {
9777
+ function tagOpenCodePromptResult(result, deliveryMethod) {
9778
+ if (result && typeof result === "object") {
9779
+ try {
9780
+ Object.defineProperty(result, "__optimaPromptDelivery", { value: deliveryMethod, enumerable: false, configurable: true });
9781
+ } catch {
9782
+ return { result, __optimaPromptDelivery: deliveryMethod };
9783
+ }
9784
+ }
9785
+ return result;
9786
+ }
9787
+ async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, structuredPayload, deliveryMethod) {
9778
9788
  const attempts = [
9779
9789
  { ...structuredPayload, path: { id: sessionId } },
9780
9790
  { ...structuredPayload, path: { sessionID: sessionId } },
@@ -9784,14 +9794,14 @@ async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, s
9784
9794
  let firstError = null;
9785
9795
  for (const attempt of attempts) {
9786
9796
  try {
9787
- return assertOpenCodePromptAccepted(await method(attempt));
9797
+ return tagOpenCodePromptResult(assertOpenCodePromptAccepted(await method(attempt)), deliveryMethod);
9788
9798
  } catch (error) {
9789
9799
  firstError ??= error;
9790
9800
  }
9791
9801
  }
9792
9802
  throw firstError;
9793
9803
  }
9794
- async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false, legacyOnly = false } = {}) {
9804
+ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false, legacyOnly = false, allowDirectFallback = true } = {}) {
9795
9805
  const directBaseUrl = opencodeBaseUrl || baseUrl;
9796
9806
  const parts = [{ type: "text", text }];
9797
9807
  const flatPayload = { directory, agent, parts };
@@ -9801,21 +9811,21 @@ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, direct
9801
9811
  body: { agent, parts }
9802
9812
  };
9803
9813
  let firstError = null;
9804
- if (!direct && typeof client?.session?.promptAsync === "function") {
9814
+ if (!direct && typeof client?.session?.prompt === "function") {
9805
9815
  try {
9806
- return await callOpenCodePromptWithFallbacks(client.session.promptAsync.bind(client.session), sessionId, flatPayload, structuredPayload);
9816
+ return await callOpenCodePromptWithFallbacks(client.session.prompt.bind(client.session), sessionId, flatPayload, structuredPayload, "prompt");
9807
9817
  } catch (error) {
9808
9818
  firstError ??= error;
9809
9819
  }
9810
9820
  }
9811
- if (!direct && typeof client?.session?.prompt === "function") {
9821
+ if (!direct && typeof client?.session?.promptAsync === "function") {
9812
9822
  try {
9813
- return await callOpenCodePromptWithFallbacks(client.session.prompt.bind(client.session), sessionId, flatPayload, structuredPayload);
9823
+ return await callOpenCodePromptWithFallbacks(client.session.promptAsync.bind(client.session), sessionId, flatPayload, structuredPayload, "prompt_async");
9814
9824
  } catch (error) {
9815
9825
  firstError ??= error;
9816
9826
  }
9817
9827
  }
9818
- if (directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, directory, fetchImpl, legacyOnly });
9828
+ if (allowDirectFallback && directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, directory, fetchImpl, legacyOnly });
9819
9829
  if (firstError) throw firstError;
9820
9830
  throw new Error("OpenCode client does not expose session.prompt or session.promptAsync.");
9821
9831
  }
@@ -9893,46 +9903,44 @@ async function applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason,
9893
9903
  return { ok: false, error: error.message, tagName };
9894
9904
  }
9895
9905
  }
9906
+ function openCodeBlockingPromptVerification(result, sessionId) {
9907
+ if (result?.__optimaPromptDelivery !== "prompt") return null;
9908
+ const response = result?.response ?? result;
9909
+ const data = response?.data ?? result?.data ?? null;
9910
+ const parts = normalizePromptResponseParts(result);
9911
+ const messageId = data?.id ?? response?.id ?? result?.id ?? data?.messageID ?? response?.messageID ?? result?.messageID;
9912
+ const deliveredSessionId = response?.sessionID ?? response?.sessionId ?? data?.sessionID ?? data?.sessionId ?? result?.sessionID ?? result?.sessionId;
9913
+ if (deliveredSessionId && String(deliveredSessionId) !== String(sessionId)) {
9914
+ throw new Error(`OpenCode blocking prompt targeted foreign session ${deliveredSessionId}.`);
9915
+ }
9916
+ if (parts.length > 0 || messageId) return { ok: true, method: parts.length > 0 ? "blocking_prompt_parts" : "blocking_prompt_message", messageId: messageId ? String(messageId) : null, sessionId: deliveredSessionId ? String(deliveredSessionId) : String(sessionId), parts: parts.length };
9917
+ return null;
9918
+ }
9896
9919
  async function deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent, text, directory, opencodeBaseUrl, eventMarkers = [], verifySessionEventDelivery = verifyOpenCodeSessionEventDelivery, applyBlockerOnFailure = true } = {}) {
9897
9920
  const beforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, limit: 50 }).catch(() => null);
9898
- const sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl });
9921
+ const sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, allowDirectFallback: false });
9922
+ let blockingPromptVerification = null;
9899
9923
  let admissionVerification = null;
9900
9924
  try {
9901
- admissionVerification = openCodePromptAdmissionVerification(sendResult, sessionId);
9925
+ blockingPromptVerification = openCodeBlockingPromptVerification(sendResult, sessionId);
9926
+ if (!blockingPromptVerification) admissionVerification = openCodePromptAdmissionVerification(sendResult, sessionId);
9902
9927
  } catch (error) {
9903
9928
  appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_failed", taskId, sessionId, reason: error.message, fallbackAttempted: false });
9904
9929
  if (!applyBlockerOnFailure) return { ok: false, action: "message_delivery_failed", reason: error.message, taskId, sessionId, fallbackAttempted: false };
9905
9930
  const blocker2 = await applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason: error.message, source: "delivery_admission_failed" });
9906
9931
  return { ok: false, action: "message_delivery_failed", reason: error.message, taskId, sessionId, fallbackAttempted: false, blockerTag: blocker2 };
9907
9932
  }
9933
+ if (blockingPromptVerification) return { ok: true, verification: blockingPromptVerification, admissionVerification: null, fallback: false };
9908
9934
  let verification = await verifySessionEventDelivery(openCodeClient, { sessionId, beforeMessages, expectedText: text, markers: eventMarkers });
9909
9935
  if (verification?.ok) return { ok: true, verification, admissionVerification, fallback: false };
9910
- if (verification?.reason === "message_verification_unavailable" && !admissionVerification) {
9911
- return { ok: true, verification: { ok: true, method: "legacy_prompt_accepted", skipped: true }, fallback: false };
9912
- }
9913
9936
  if (admissionVerification) {
9914
9937
  appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_admitted_but_invisible", taskId, sessionId, admission: admissionVerification, reason: verification?.reason || "message_not_visible" });
9915
9938
  }
9916
- const canFallbackDirect = Boolean(opencodeBaseUrl);
9917
- if (canFallbackDirect) {
9918
- const retryBeforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, limit: 50 }).catch(() => beforeMessages);
9919
- const retrySendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, direct: true, legacyOnly: Boolean(admissionVerification) });
9920
- try {
9921
- admissionVerification = openCodePromptAdmissionVerification(retrySendResult, sessionId);
9922
- } catch (error) {
9923
- verification = { ok: false, reason: error.message };
9924
- }
9925
- verification = await verifySessionEventDelivery(openCodeClient, { sessionId, beforeMessages: retryBeforeMessages, expectedText: text, markers: eventMarkers });
9926
- if (verification?.ok) return { ok: true, verification, admissionVerification, fallback: true };
9927
- if (verification?.reason === "message_verification_unavailable" && !admissionVerification) {
9928
- return { ok: true, verification: { ok: true, method: "legacy_prompt_accepted", skipped: true }, fallback: true };
9929
- }
9930
- }
9931
- const reason = verification?.reason || "message_delivery_failed";
9932
- appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_failed", taskId, sessionId, reason, fallbackAttempted: canFallbackDirect });
9933
- if (!applyBlockerOnFailure) return { ok: false, action: "message_delivery_failed", reason, taskId, sessionId, fallbackAttempted: canFallbackDirect };
9934
- const blocker = await applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason, source: canFallbackDirect ? "delivery_fallback_failed" : "delivery_verification_failed" });
9935
- return { ok: false, action: "message_delivery_failed", reason, taskId, sessionId, fallbackAttempted: canFallbackDirect, blockerTag: blocker };
9939
+ const reason = verification?.reason || (admissionVerification ? "prompt_admission_not_delivered" : "message_delivery_failed");
9940
+ appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_failed", taskId, sessionId, reason, fallbackAttempted: false, httpFallbackDisabled: Boolean(opencodeBaseUrl) });
9941
+ if (!applyBlockerOnFailure) return { ok: false, action: "message_delivery_failed", reason, taskId, sessionId, fallbackAttempted: false };
9942
+ const blocker = await applyClickUpBlockerTag({ clickupClient, worktree, taskId, reason, source: "delivery_verification_failed" });
9943
+ return { ok: false, action: "message_delivery_failed", reason, taskId, sessionId, fallbackAttempted: false, blockerTag: blocker };
9936
9944
  }
9937
9945
  async function recoverClickUpPmSession({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, staleSessionId, sessionTitle, taskRoute, metadataWithRouting, config, prompt, eventMarkers = [], deliveryEvidencePath, evidencePath, eventKey, createSession, verifySessionEventDelivery } = {}) {
9938
9946
  appendClickUpWebhookLocalLog(worktree, { type: "pm_session_recovery_started", taskId, staleSessionId, worktree: taskRoute?.worktree });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defend-tech/opencode-optima",
3
- "version": "0.1.51",
3
+ "version": "0.1.52",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+ssh://git@github.com/defend-tech/opencode-optima.git"