@adhdev/daemon-core 0.9.82-rc.111 → 0.9.82-rc.113

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.mjs CHANGED
@@ -8540,11 +8540,17 @@ function buildSessionReadStateKey(sessionId, providerSessionId) {
8540
8540
  }
8541
8541
  function getSessionSeenAt(state, sessionId, providerSessionId) {
8542
8542
  const providerKey = buildSessionReadStateKey(sessionId, providerSessionId);
8543
- return state.sessionReads?.[providerKey] || state.sessionReads?.[sessionId] || 0;
8543
+ return Math.max(state.sessionReads?.[providerKey] || 0, state.sessionReads?.[sessionId] || 0);
8544
8544
  }
8545
8545
  function getSessionSeenMarker(state, sessionId, providerSessionId) {
8546
8546
  const providerKey = buildSessionReadStateKey(sessionId, providerSessionId);
8547
- return state.sessionReadMarkers?.[providerKey] || state.sessionReadMarkers?.[sessionId] || "";
8547
+ const providerSeenAt = state.sessionReads?.[providerKey] || 0;
8548
+ const sessionSeenAt = state.sessionReads?.[sessionId] || 0;
8549
+ const providerMarker = state.sessionReadMarkers?.[providerKey] || "";
8550
+ const sessionMarker = state.sessionReadMarkers?.[sessionId] || "";
8551
+ if (sessionSeenAt > providerSeenAt && sessionMarker) return sessionMarker;
8552
+ if (providerSeenAt > sessionSeenAt && providerMarker) return providerMarker;
8553
+ return providerMarker || sessionMarker;
8548
8554
  }
8549
8555
  function getSessionNotificationDismissal(state, sessionId, providerSessionId) {
8550
8556
  const providerKey = buildSessionReadStateKey(sessionId, providerSessionId);
@@ -16936,7 +16942,20 @@ async function handleChatHistory(h, args) {
16936
16942
  if (visibleCount > excludeRecentCount) excludeRecentCount = visibleCount;
16937
16943
  }
16938
16944
  const workspace = typeof args?.workspace === "string" ? args.workspace : typeof h.currentSession?.workspace === "string" ? h.currentSession.workspace : void 0;
16939
- const result = readProviderChatHistory(agentStr, {
16945
+ const exactNativeHistoryScope = Boolean(
16946
+ typeof args?.targetSessionId === "string" && args.targetSessionId.trim() || typeof args?.historySessionId === "string" && args.historySessionId.trim() || typeof args?.providerSessionId === "string" && args.providerSessionId.trim()
16947
+ );
16948
+ const result = supportsCliNativeTranscript(agentStr, provider) && isNativeSourceCanonicalHistory(provider?.canonicalHistory) ? readCliProviderNativeHistory(agentStr, {
16949
+ canonicalHistory: provider?.canonicalHistory,
16950
+ historySessionId,
16951
+ workspace,
16952
+ offset: offset || 0,
16953
+ limit: limit || 30,
16954
+ excludeRecentCount,
16955
+ historyBehavior: provider?.historyBehavior,
16956
+ scripts: provider?.scripts,
16957
+ exactSessionScoped: exactNativeHistoryScope
16958
+ }) : readProviderChatHistory(agentStr, {
16940
16959
  canonicalHistory: provider?.canonicalHistory,
16941
16960
  historySessionId,
16942
16961
  workspace,
@@ -16946,6 +16965,26 @@ async function handleChatHistory(h, args) {
16946
16965
  historyBehavior: provider?.historyBehavior,
16947
16966
  scripts: provider?.scripts
16948
16967
  });
16968
+ if (supportsCliNativeTranscript(agentStr, provider) && isNativeSourceCanonicalHistory(provider?.canonicalHistory)) {
16969
+ const lookup = result.lookup === "workspace" ? "workspace" : "session";
16970
+ const messages = Array.isArray(result.messages) ? normalizeNativeHistoryMessages(agentStr, result.messages) : [];
16971
+ const historyProviderSessionId = typeof result?.providerSessionId === "string" ? result.providerSessionId : readHistorySessionIdFromMessages(messages) || historySessionId;
16972
+ const safeMapping = hasSafeNativeHistoryMapping({
16973
+ historySessionId: lookup === "workspace" ? void 0 : historySessionId,
16974
+ providerSessionId: lookup === "workspace" ? void 0 : historyProviderSessionId,
16975
+ workspace,
16976
+ nativeMessages: messages
16977
+ });
16978
+ if (result.source === "provider-native" && messages.length > 0 && !safeMapping) {
16979
+ return {
16980
+ success: true,
16981
+ messages: [],
16982
+ hasMore: false,
16983
+ source: "native-unavailable",
16984
+ agent: agentStr
16985
+ };
16986
+ }
16987
+ }
16949
16988
  return { success: true, ...result, agent: agentStr };
16950
16989
  } catch (e) {
16951
16990
  return { success: false, error: e.message };
@@ -19577,6 +19616,16 @@ function normalizeProviderSessionId(provider, providerSessionId) {
19577
19616
  }
19578
19617
 
19579
19618
  // src/providers/cli-provider-instance.ts
19619
+ function isIdleStatus(value) {
19620
+ const status = typeof value === "string" ? value.trim().toLowerCase() : "";
19621
+ return !status || status === "idle" || status === "ready";
19622
+ }
19623
+ function getMessageTime(message) {
19624
+ if (!message || typeof message !== "object") return 0;
19625
+ const record = message;
19626
+ const value = Number(record.receivedAt ?? record.timestamp ?? 0);
19627
+ return Number.isFinite(value) ? value : 0;
19628
+ }
19580
19629
  var COMPLETED_FINALIZATION_RETRY_MS = 1e3;
19581
19630
  var COMPLETED_FINALIZATION_MAX_WAIT_MS = 3e4;
19582
19631
  var IMAGE_MIME_EXTENSIONS = {
@@ -19838,7 +19887,7 @@ var CliProviderInstance = class {
19838
19887
  await this.adapter.spawn();
19839
19888
  await this.enforceFreshSessionLaunchIfNeeded();
19840
19889
  this.maybeAppendRuntimeRecoveryMessage(this.adapter.getRuntimeMetadata());
19841
- if (this.providerSessionId) {
19890
+ if (this.providerSessionId && this.shouldHydrateExistingProviderHistory()) {
19842
19891
  this.restorePersistedHistoryFromCurrentSession();
19843
19892
  }
19844
19893
  if (this.providerSessionId && this.launchMode === "resume") {
@@ -19920,28 +19969,37 @@ var CliProviderInstance = class {
19920
19969
  this.provider,
19921
19970
  typeof adapterStatus?.providerSessionId === "string" ? adapterStatus.providerSessionId : ""
19922
19971
  );
19923
- if (adapterProviderSessionId) {
19924
- this.promoteProviderSessionId(adapterProviderSessionId);
19925
- }
19926
19972
  const autoApproveActive = this.maybeAutoApproveStatus(adapterStatus, Date.now());
19927
19973
  const visibleStatus = parseErrorMessage || parsedStatus?.status === "error" ? "error" : autoApproveActive ? "generating" : adapterStatus.status;
19974
+ const runtime = this.adapter.getRuntimeMetadata();
19975
+ this.maybeAppendRuntimeRecoveryMessage(runtime);
19976
+ let parsedMessages = Array.isArray(parsedStatus?.messages) ? parsedStatus.messages : [];
19928
19977
  const parsedProviderSessionId = normalizeProviderSessionId(
19929
19978
  this.provider,
19930
19979
  typeof parsedStatus?.providerSessionId === "string" ? parsedStatus.providerSessionId : ""
19931
19980
  );
19932
- if (parsedProviderSessionId) {
19981
+ const suppressFreshLaunchStartupReplay = this.shouldSuppressFreshLaunchStartupReplay(
19982
+ parsedMessages,
19983
+ parsedStatus,
19984
+ adapterStatus,
19985
+ parsedProviderSessionId
19986
+ );
19987
+ if (adapterProviderSessionId && !suppressFreshLaunchStartupReplay) {
19988
+ this.promoteProviderSessionId(adapterProviderSessionId);
19989
+ }
19990
+ if (parsedProviderSessionId && !suppressFreshLaunchStartupReplay) {
19933
19991
  this.promoteProviderSessionId(parsedProviderSessionId);
19934
19992
  }
19935
- const runtime = this.adapter.getRuntimeMetadata();
19936
- this.maybeAppendRuntimeRecoveryMessage(runtime);
19993
+ if (suppressFreshLaunchStartupReplay) {
19994
+ parsedMessages = [];
19995
+ }
19937
19996
  const activeChatId = this.providerSessionId || runtime?.runtimeId || this.instanceId;
19938
- let parsedMessages = Array.isArray(parsedStatus?.messages) ? parsedStatus.messages : [];
19939
19997
  const historyMessageCount = Number.isFinite(parsedStatus?.historyMessageCount) ? Math.max(0, Number(parsedStatus.historyMessageCount)) : null;
19940
19998
  if (historyMessageCount !== null) {
19941
19999
  parsedMessages = historyMessageCount > 0 ? parsedMessages.slice(-historyMessageCount) : [];
19942
20000
  }
19943
20001
  const mergedMessages = this.mergeConversationMessages(parsedMessages);
19944
- const canonicalBackedHistory = this.syncCanonicalSavedHistoryIfNeeded();
20002
+ const canonicalBackedHistory = this.shouldHydrateExistingProviderHistory() ? this.syncCanonicalSavedHistoryIfNeeded() : false;
19945
20003
  const statusMessages = canonicalBackedHistory && this.lastPersistedHistoryMessages.length > 0 ? this.lastPersistedHistoryMessages.map((message) => ({
19946
20004
  role: message.role,
19947
20005
  content: message.content,
@@ -19984,7 +20042,10 @@ var CliProviderInstance = class {
19984
20042
  this.lastPersistedHistoryMessages = normalizedMessagesToSave;
19985
20043
  }
19986
20044
  }
19987
- this.applyProviderResponse(parsedStatus, { phase: "immediate" });
20045
+ this.applyProviderResponse(
20046
+ suppressFreshLaunchStartupReplay && parsedStatus && typeof parsedStatus === "object" ? { ...parsedStatus, providerSessionId: void 0 } : parsedStatus,
20047
+ { phase: "immediate" }
20048
+ );
19988
20049
  const surface = resolveProviderStateSurface({
19989
20050
  summaryMetadata: this.summaryMetadata,
19990
20051
  controlValues: this.controlValues
@@ -20736,7 +20797,9 @@ ${effect.notification.body || ""}`.trim();
20736
20797
  this.providerSessionId = nextSessionId;
20737
20798
  this.historyWriter.promoteHistorySession(this.type, previousHistorySessionId, nextSessionId);
20738
20799
  this.historyWriter.writeSessionStart(this.type, nextSessionId, this.workingDir, this.instanceId);
20739
- this.restorePersistedHistoryFromCurrentSession();
20800
+ if (this.shouldHydrateExistingProviderHistory()) {
20801
+ this.restorePersistedHistoryFromCurrentSession();
20802
+ }
20740
20803
  this.adapter.updateRuntimeMeta({ providerSessionId: nextSessionId });
20741
20804
  this.onProviderSessionResolved?.({
20742
20805
  instanceId: this.instanceId,
@@ -20748,6 +20811,18 @@ ${effect.notification.body || ""}`.trim();
20748
20811
  });
20749
20812
  LOG.info("CLI", `[${this.type}] discovered provider session id: ${nextSessionId}`);
20750
20813
  }
20814
+ shouldHydrateExistingProviderHistory() {
20815
+ return this.launchMode === "resume" || this.launchMode === "manual";
20816
+ }
20817
+ shouldSuppressFreshLaunchStartupReplay(parsedMessages, parsedStatus, adapterStatus, parsedProviderSessionId = "") {
20818
+ if (this.launchMode !== "new") return false;
20819
+ if (this.providerSessionId) return false;
20820
+ if (!Array.isArray(parsedMessages) || parsedMessages.length === 0) return false;
20821
+ if (!isIdleStatus(adapterStatus?.status) || !isIdleStatus(parsedStatus?.status)) return false;
20822
+ if (parsedProviderSessionId) return true;
20823
+ const newestMessageAt = parsedMessages.reduce((newest, message) => Math.max(newest, getMessageTime(message)), 0);
20824
+ return newestMessageAt === 0;
20825
+ }
20751
20826
  syncCanonicalSavedHistoryIfNeeded() {
20752
20827
  if (!this.providerSessionId) return false;
20753
20828
  const canonicalHistory = this.provider.canonicalHistory;
@@ -31336,7 +31411,8 @@ function prepareSessionChatTailUpdate(input) {
31336
31411
  update: null
31337
31412
  };
31338
31413
  }
31339
- const fullMessages = normalizeChatMessages(Array.isArray(result.messages) ? result.messages : []);
31414
+ const rawMessages = Array.isArray(result.messages) ? result.messages : Array.isArray(result.messagesTail) ? result.messagesTail : [];
31415
+ const fullMessages = normalizeChatMessages(rawMessages);
31340
31416
  const messages = fullMessages;
31341
31417
  const title = typeof result.title === "string" ? result.title : void 0;
31342
31418
  const activeModal = normalizeChatTailActiveModal(result.activeModal);