@cortexkit/pi-magic-context 0.44.2 → 0.44.3

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.
@@ -9,6 +9,10 @@ import {
9
9
  sessionLog
10
10
  } from "./index-59dmj4zs.js";
11
11
  import {
12
+ registerSlowWriteReporter,
13
+ detectSqliteRuntime,
14
+ Database,
15
+ withPrivilegedWriter,
12
16
  removeSystemReminders,
13
17
  hasMeaningfulUserText2,
14
18
  extractTexts2,
@@ -28,10 +32,6 @@ import {
28
32
  assertOpenCodeStoreGeneration,
29
33
  openCodeDbPathExists,
30
34
  getOpenCodeDbProbeDescriptions,
31
- registerSlowWriteReporter,
32
- detectSqliteRuntime,
33
- Database,
34
- withPrivilegedWriter,
35
35
  closeQuietly,
36
36
  withReadOnlySessionDb,
37
37
  setDatabase,
@@ -42,7 +42,7 @@ import {
42
42
  cleanUserText2,
43
43
  visitRawSessionMessages2,
44
44
  readSessionChunk2
45
- } from "./index-gsff513e.js";
45
+ } from "./index-tckvezry.js";
46
46
  import {
47
47
  logSlowWriteTransaction
48
48
  } from "./index-97e5fzy3.js";
@@ -8800,6 +8800,10 @@ var dubiousOwnershipLoggedDirectories = new Set;
8800
8800
  var dubiousOwnershipWarnedDirectories = new Set;
8801
8801
  var transientGitIdentityReuseLoggedDirectories = new Set;
8802
8802
  var sessionIdentityCache = new Map;
8803
+ var homeProjectPermission = false;
8804
+ function setHomeProjectPermission(allowed) {
8805
+ homeProjectPermission = allowed;
8806
+ }
8803
8807
  var execFileSyncForIdentity = execFileSync;
8804
8808
  var userHomeDirectoryForIdentity = () => homedir3();
8805
8809
  var nowMs = () => Date.now();
@@ -8901,7 +8905,7 @@ function classifyGitError(error, rawDirectory) {
8901
8905
  }
8902
8906
  return new ProjectIdentityError("unknown", rawDirectory, "git rev-list failed while resolving project identity", asError(error));
8903
8907
  }
8904
- function resolveProjectIdentityStrict(directory, allowHomeProject = false) {
8908
+ function resolveProjectIdentityStrict(directory, allowHomeProject = homeProjectPermission) {
8905
8909
  assertProjectAllowed(directory, allowHomeProject);
8906
8910
  const canonical = path2.resolve(directory);
8907
8911
  const cached = identityCache.get(canonical);
@@ -9029,7 +9033,7 @@ function assertProjectAllowed(directory, allowHomeProject) {
9029
9033
  throw new ProjectIdentityError("home_project_disabled", directory, "Home project memory is disabled; set allow_home_project to opt in");
9030
9034
  }
9031
9035
  }
9032
- function resolveProjectIdentity(directory, allowHomeProject = false) {
9036
+ function resolveProjectIdentity(directory, allowHomeProject = homeProjectPermission) {
9033
9037
  assertProjectAllowed(directory, allowHomeProject);
9034
9038
  const canonical = path2.resolve(directory);
9035
9039
  const cachedFallback = directoryFallbackCache.get(canonical);
@@ -9075,7 +9079,7 @@ function resolveProjectIdentity(directory, allowHomeProject = false) {
9075
9079
  throw error;
9076
9080
  }
9077
9081
  }
9078
- function resolveProjectIdentityOrFallback(directory, allowHomeProject = false) {
9082
+ function resolveProjectIdentityOrFallback(directory, allowHomeProject = homeProjectPermission) {
9079
9083
  try {
9080
9084
  return resolveProjectIdentity(directory, allowHomeProject);
9081
9085
  } catch (error) {
@@ -9136,7 +9140,7 @@ function directoryHasGitMetadata(directory) {
9136
9140
  function isUsableProjectIdentity(identity) {
9137
9141
  return typeof identity === "string" && identity.trim().length > 0;
9138
9142
  }
9139
- function resolveProjectIdentityForSession(directory, allowHomeProject = false) {
9143
+ function resolveProjectIdentityForSession(directory, allowHomeProject = homeProjectPermission) {
9140
9144
  const resolvedDirectory = path2.resolve(directory);
9141
9145
  const cacheKey = `${allowHomeProject ? "1" : "0"}\x00${resolvedDirectory}`;
9142
9146
  const cached = sessionIdentityCache.get(cacheKey);
@@ -20633,7 +20637,7 @@ function finishDatabaseOpen(db, dbPath, explicitDbPath, latestSupportedVersion)
20633
20637
  { readRawSessionMessagePage, readRawSessionMessages },
20634
20638
  { startMessageTimeBackfill }
20635
20639
  ] = await Promise.all([
20636
- import("./read-session-chunk-zhtcx9f1.js"),
20640
+ import("./read-session-chunk-z2weg25w.js"),
20637
20641
  import("./message-time-backfill-5yrp95cx.js")
20638
20642
  ]);
20639
20643
  await startMessageTimeBackfill(db, Object.assign(readRawSessionMessages, {
@@ -23898,7 +23902,12 @@ function pruneSynapseBatchLedgerForProject(db, projectIdentity, ttlMs = SYNAPSE_
23898
23902
  const ledgerTable = db.prepare("SELECT 1 AS present FROM sqlite_master WHERE type = 'table' AND name = 'synapse_batch_ledger'").get();
23899
23903
  if (!ledgerTable)
23900
23904
  return 0;
23901
- return db.prepare("DELETE FROM synapse_batch_ledger WHERE session_id IN (?, ?) AND updated_at < ?").run(projectIdentity, `shadow:${projectIdentity}`, Date.now() - ttlMs).changes;
23905
+ const shadow = `shadow:${projectIdentity}`;
23906
+ const cutoff = Date.now() - ttlMs;
23907
+ const predicate = "session_id IN (?, ?) AND updated_at < ?";
23908
+ if (!db.prepare(`SELECT 1 FROM synapse_batch_ledger WHERE ${predicate} LIMIT 1`).get(projectIdentity, shadow, cutoff))
23909
+ return 0;
23910
+ return db.prepare(`DELETE FROM synapse_batch_ledger WHERE ${predicate}`).run(projectIdentity, shadow, cutoff).changes;
23902
23911
  }
23903
23912
  // ../plugin/src/features/magic-context/storage-m0-mutation-log.ts
23904
23913
  var M0_MUTATION_TYPES = new Set([
@@ -26445,6 +26454,21 @@ function getSdkOutputLimit(providerID, modelID) {
26445
26454
  loadPersistedApiCacheOnce();
26446
26455
  return lookupMetadataWithTagFallback(apiCache, providerID, modelID)?.outputLimit;
26447
26456
  }
26457
+ function getSdkInputLimit(providerID, modelID) {
26458
+ loadPersistedApiCacheOnce();
26459
+ if (!apiCache)
26460
+ return;
26461
+ const direct = apiCache.get(`${providerID}/${modelID}`)?.inputLimit;
26462
+ if (isSaneLimit(direct))
26463
+ return direct;
26464
+ const colon = modelID.indexOf(":");
26465
+ if (colon > 0) {
26466
+ const tagless = apiCache.get(`${providerID}/${modelID.slice(0, colon)}`)?.inputLimit;
26467
+ if (isSaneLimit(tagless))
26468
+ return tagless;
26469
+ }
26470
+ return;
26471
+ }
26448
26472
  function lookupMetadataWithTagFallback(cache, providerID, modelID) {
26449
26473
  if (!cache)
26450
26474
  return;
@@ -28518,6 +28542,23 @@ var SESSION_CHUNK_REPAIR_BATCH_SIZE = 100;
28518
28542
  var upsertSessionProjectStatements = new WeakMap;
28519
28543
  var repairSessionChunkProjectStatements = new WeakMap;
28520
28544
  var repairProjectChunkProjectStatements = new WeakMap;
28545
+ var misScopedProjectChunkStatements = new WeakMap;
28546
+ var MIS_SCOPED_PROJECT_CHUNK_IDS_SQL = `
28547
+ SELECT e.id FROM compartment_chunk_embeddings e
28548
+ JOIN session_projects sp ON sp.session_id = e.session_id AND sp.harness = e.harness
28549
+ WHERE e.project_path = ? AND sp.project_path <> e.project_path
28550
+ UNION ALL
28551
+ SELECT e.id FROM session_projects sp
28552
+ JOIN compartment_chunk_embeddings e ON e.session_id = sp.session_id
28553
+ WHERE sp.project_path = ? AND e.harness = sp.harness AND e.project_path <> sp.project_path`;
28554
+ function hasMisScopedCompartmentChunkEmbeddingsForProject(db, projectPath) {
28555
+ let stmt = misScopedProjectChunkStatements.get(db);
28556
+ if (!stmt) {
28557
+ stmt = db.prepare(`SELECT 1 FROM (${MIS_SCOPED_PROJECT_CHUNK_IDS_SQL}) LIMIT 1`);
28558
+ misScopedProjectChunkStatements.set(db, stmt);
28559
+ }
28560
+ return !!stmt.get(projectPath, projectPath);
28561
+ }
28521
28562
  function getUpsertSessionProjectStatement(db) {
28522
28563
  let stmt = upsertSessionProjectStatements.get(db);
28523
28564
  if (!stmt) {
@@ -28553,23 +28594,11 @@ function getRepairProjectChunkProjectStatement(db) {
28553
28594
  if (!stmt) {
28554
28595
  stmt = db.prepare(`UPDATE compartment_chunk_embeddings
28555
28596
  SET project_path = (
28556
- SELECT sp.project_path
28557
- FROM session_projects sp
28597
+ SELECT sp.project_path FROM session_projects sp
28558
28598
  WHERE sp.session_id = compartment_chunk_embeddings.session_id
28559
28599
  AND sp.harness = compartment_chunk_embeddings.harness
28560
- LIMIT 1
28561
28600
  )
28562
- WHERE EXISTS (
28563
- SELECT 1
28564
- FROM session_projects sp
28565
- WHERE sp.session_id = compartment_chunk_embeddings.session_id
28566
- AND sp.harness = compartment_chunk_embeddings.harness
28567
- AND sp.project_path <> compartment_chunk_embeddings.project_path
28568
- AND (
28569
- sp.project_path = ?
28570
- OR compartment_chunk_embeddings.project_path = ?
28571
- )
28572
- )`);
28601
+ WHERE id IN (${MIS_SCOPED_PROJECT_CHUNK_IDS_SQL})`);
28573
28602
  repairProjectChunkProjectStatements.set(db, stmt);
28574
28603
  }
28575
28604
  return stmt;
@@ -28589,6 +28618,8 @@ function recordSessionProjectIdentity(db, sessionId, projectPath) {
28589
28618
  function repairMisScopedCompartmentChunkEmbeddingsForProject(db, projectPath) {
28590
28619
  if (!projectPath)
28591
28620
  return 0;
28621
+ if (!hasMisScopedCompartmentChunkEmbeddingsForProject(db, projectPath))
28622
+ return 0;
28592
28623
  return getRepairProjectChunkProjectStatement(db).run(projectPath, projectPath).changes;
28593
28624
  }
28594
28625
 
@@ -29059,6 +29090,14 @@ function recordActiveEmbeddingIdentity(db, projectIdentity, currentProviderIdent
29059
29090
  if (currentProviderIdentity === OFF_PROVIDER_IDENTITY) {
29060
29091
  return;
29061
29092
  }
29093
+ const scopes = [["chunk", currentChunkIdentity]];
29094
+ if (features.memoryEnabled)
29095
+ scopes.push(["memory", currentProviderIdentity]);
29096
+ if (features.gitCommitEnabled)
29097
+ scopes.push(["commit", currentProviderIdentity]);
29098
+ const active = db.prepare("SELECT 1 FROM embedding_identity_active WHERE project_path = ? AND scope = ? AND model_id = ?");
29099
+ if (scopes.every(([scope, model]) => active.get(projectIdentity, scope, model)) && !hasMisScopedCompartmentChunkEmbeddingsForProject(db, projectIdentity))
29100
+ return;
29062
29101
  const now = Date.now();
29063
29102
  const transactionStartedAt = performance.now();
29064
29103
  db.exec("BEGIN IMMEDIATE");
@@ -29256,7 +29295,9 @@ function registerProjectEmbedding(db, projectIdentity, config, features, sourceD
29256
29295
  observationMode: false
29257
29296
  };
29258
29297
  projectRegistrations.set(projectIdentity, registration);
29259
- persistPrimaryDescriptor(db, registration);
29298
+ if (generationChanged || !db.prepare("SELECT 1 FROM embedding_registrations WHERE project_path = ? AND provider_identity = ? AND chunk_model_id = ? AND generation = ?").get(projectIdentity, providerIdentity, registration.chunkModelId, generation)) {
29299
+ persistPrimaryDescriptor(db, registration);
29300
+ }
29260
29301
  if (!canReuseProvider) {
29261
29302
  disposeProvider(prior?.provider ?? null);
29262
29303
  }
@@ -39563,4 +39604,4 @@ function registerMagicContextTools(pi, opts) {
39563
39604
  }
39564
39605
  }
39565
39606
 
39566
- export { COMPACTION_ENABLED_PATH, isDreamerRunnable, isCompactionEnabled, formatConfigParseStatusLine, formatConfigParseNotice, claimConfigParseFailuresOnce, parseJsoncRecovering, detectConfigFile, canonicalModelIdentity, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, ompModelRefToCanonical, resolveModelRefForOmp, setStoragePrivatePermissionEnforcement, MIN_PLAUSIBLE_CONTEXT_LIMIT, catalogOutputReserve, reloadWindowOverlay, getWindowOverlay, resolveWindowOverlayFacts, deriveWindowGeometry, hasTrustedAbsoluteWall, applyProvenInputFloor, formatWindowDerivationLine, isSaneLimit, resolveOutputReserve, getSdkContextLimit, getSdkOutputLimit, cortexKitUserConfigBasePath, cortexKitProjectConfigBasePath, migrateMagicContextConfigLocations, withContentLanguageDirective, parseCron, nextOccurrence, nextDueAtMs, resolveModelConfigValue, DEFAULT_HISTORIAN_TIMEOUT_MS, DREAM_TASK_PROMOTION_DEFAULTS, LIVE_RELOAD_CONFIG_PATHS, getProtectedTokensTierOverrides, FAIL_CLOSED_DOCTOR_COMMAND, formatFailClosedBlockingMessage, createFailClosedBlockingError, isFailClosedBlockingError, shouldBypassFailClosedBlock, createFailClosedController, ProjectIdentityError, resolveProjectIdentityStrict, isUserHomeDirectory, resolveProjectIdentity, isUsableProjectIdentity, resolveProjectIdentityForSession, beginBootQuietPeriod, scheduleAfterBootQuiet, clearCompressionDepth, clearCompressionDepthRange, formatSynapseLaneDescriptor, buildCanonicalChunkTextFromFts, buildCompartmentSummaryFallbackText, canonicalizeInMemoryChunkTextForEmbedding, chunkCanonicalText, chunkEmbeddingWindowsAreCurrent, replaceCompartmentChunkEmbeddings, deleteChunkEmbedBackoffForSession, readSessionActivity, observeSessionActivity, backfillSessionActivity, getMessageIndexSourceIdentity, isMessageIndexSourceCurrent, getLastIndexedOrdinal, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, indexSingleMessage, indexMessagesAfterOrdinal, sweepOrphanedOpenCodeMessageIndexes, MAX_EXECUTE_THRESHOLD, escalationBands, HYGIENE_PROVIDER_UNITS_VERSION, sessionDecisionCalibration, transitionSessionHygieneUnits, computeProtectionWindow, readEpochFloorSnapshot, getProtectionWindowForSession, persistCachedM0, clearCachedM0M1, readReplayDocument, updateReplayDocument, isProviderOverflowFailClosedProven, describeProtectedTailDrainBudgetSkip, loadProtectedTailMeta, markProtectedTailPolicyV3Seeded, recordProtectedTailPublicationFloor, recordProtectedTailNoEligibleHead, getWrapupInProgressState, isWrapupInProgress, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, reserveProtectedTailDrainTokens, clearEmergencyDrainLatch, recordHistorianDrainFailure, clearHistorianDrainFailure, rollbackProtectedTailDrainReservation, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, markChannel1PostReduceGracePending, captureChannel1PostReduceGraceBaseline, getChannel2NudgeState, getChannel2NudgeClaim, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getPersistedNoteNudge, setPersistedNoteNudgeTrigger, setPersistedNoteNudgeTriggerMessageId, getNoteNudgeAnchors, getAutoSearchHintDecisions, deliverNoteNudgeAtomic, appendAutoSearchHintDecision, pruneNoteNudgeAnchors, pruneAutoSearchHintDecisions, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, getNoteLastReadAt, incrementHistorianFailure, clearHistorianFailureState, getOverflowState, recordOverflowDetected, clearEmergencyRecovery, clearDetectedContextLimit, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, THINKING_BINDING_RECOVERY_FROZEN_PREFIX, THINKING_BINDING_STRIP_ORDER_END_MARKER, thinkingBindingRecoveryFrozenId, getThinkingBindingRecoveryTarget, armThinkingBindingRecovery, clearThinkingBindingRecoveryIf, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getProcessedImageStrippedIds, addProcessedImageStrippedIds, getPendingCompactionMarkerState, getCompactionMarkerHealth, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, setPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, getSessionsWithPendingPiMarker, setSessionWorkMetrics, getSessionWorkMetrics, resolveEpochFloorForPass, getOrCreateSessionMeta, updateSessionMeta, advanceToolReclaimWatermark, retryPendingSessionCleanups, retryPendingRustSessionCleanupsForProject, recordSessionProjectIdentity, embedPauseBySession, embedRunStateBySession, autoEmbedAttemptedBySession, autoEmbedIdentityBySession, invalidateAutoEmbedSession, getEmbedDrainUiStatus, piHarnessKindFromExecutable, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLease, releaseCompartmentLeaseBestEffort, isCompartmentLeaseHeld, isNoContentCompartment, HAS_COMPARTMENT_CONTENT_SQL, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, getCompartmentsByEndMessageId, appendCompartments, saveRecompStagingPass, getRecompStaging, clearRecompStaging, getRecompPartialRange, setRecompPartialRange, escapeXmlAttr, escapeXmlContent, getModuleNoteEvaluationBridge, getContextStoreUuid, getAuthorityManagedMarker, drainMirrorPages, V2_MEMORY_CATEGORIES, parseCompartmentOutput, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, sourceNameForMemory, computeWorkspaceEpochFingerprint, readProjectDocsCanonical, BoundedSessionMap, registerLkgPersistence, signatureForFields, lkgContentFields, lkgContentDigestFromFields, exactReusablePrefix, incrementalLkgContentDigests, captureSlot, getSlot, dropSlot, noteEntry, encodePiContentDecision, getPiContentDecisions, freezePiContentDecision, getNativeReplayState, saveNativeToolInputs, addNativeReasoningIds, copySessionStateForClone, getErrorMessage, describeError, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, applySqliteTuningPragmas, runSqliteOptimize, openDatabase, openDatabaseAsync, closeDatabase, queueM0Mutation, getMaxM0MutationId, queueMemoryMutation, getMemoryMutationsForRender, getMemoryMutationsForRenderByProjects, getMaxMemoryMutationId, getMaxMemoryMutationIdForProjects, getNotes, getSessionNotes, getPendingSmartNotes, getReadySmartNotes, markNoteReady, markNoteChecked, queuePendingOp, getPendingOps, getPendingOpsCount, clearPendingOps, removePendingOp, PRIMER_CANDIDATE_TTL_MS, PRIMER_CANDIDATE_MAX_AGE_MS, primerOccurrenceKey, primerOccurrenceUtcDay, insertPrimerCandidates, updatePrimerCandidateEmbedding, getPrimerCandidatesByIds, getPrimerCandidatesForPromotion, countPrimerCandidatesForProject, getActivePrimers, createPrimer, updatePrimerSupport, updatePrimerAnswer, GLOBAL_USER_PROFILE_PROJECT_PATH, getProjectState, bumpProjectUserProfileVersion, saveSourceContent, getSourceContents, recordSubagentInvocation, getLatestHistorianInvocationId, noteTouchedAt, noteNudgePickIndex, formatNoteNudge, promptSurfaceHashMaterial, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, SYNTH_USER_ID_PREFIX, resolvePiStableId, readPiSessionSnapshot, readPiSessionMessages, readPiSessionMessagePage, findLastModelKeyFromBranch, convertEntriesToRawMessages, convertEntriesToRawMessagePage, computeCueContentHash, hasMuralCueColumns, getMuralCueState, memoryNeedsCue, setMuralCue, recordMuralCueRejection, invalidateMemory, computeNormalizedHash, hasMemoryShareableColumn, hasMemoryClassifiedAtColumn, getUnclassifiedMemoryIds, insertMemory, getMemoryByHash, getMemoriesByProject, getMemoriesByProjects, getMaxMemoryIdForProjects, getMemoryById, setMemoryClassification, archiveMemory, getMemoryCountsByStatus, USER_MEMORY_CANDIDATE_TTL_MS, insertUserMemoryCandidates, getUserMemoryCandidates, deleteUserMemoryCandidates, pruneExpiredUserMemoryCandidates, insertUserMemory, getActiveUserMemories, updateUserMemoryContent, dismissUserMemory, getTaskScheduleState, getFailingDreamTasks, getMostRecentTaskRunAt, pruneNonCanonicalTaskRows, deleteTaskScheduleRowsForProject, seedTaskScheduleState, writeTaskScheduleState, isRetrospectiveWindowProcessed, recordRetrospectiveWindowProcessed, curateCategoryForMemoryCategory, peekCurateCategoryScope, beginCurateCategoryRun, curateTaskStateAfterSuccess, cosineSimilarity, GIT_SWEEP_LEASE_RENEWAL_MS, acquireGitSweepLease, renewGitSweepLease, markGitSweepSuccessAndRelease, parkGitSweepNonIndexable, releaseGitSweepLease, describeShadowBackfillWriteRefusal, contentSha256, sweepStaleEmbeddingIdentitiesForProject, enqueueShadowEmbeddingItems, getProjectEmbeddingSnapshot, getProjectChunkEmbeddingModelId, getProjectEmbeddingMaxInputTokens, embedTextForProject, embedBatchForProject, embedItemsForProject, embedUnembeddedMemoriesForProject, drainCommitBacklogForProject, embedSessionCompartmentChunks, getEmbeddingCoverageStatus, getAutoEmbeddingSessionCoverage, promoteSessionFactsDurable, embedPromotedFacts, recordMemoryMapping, recordMemoryVerifications, getUnmappedMemoryIds, clearMemoryVerifications, getMemoryVerifications, resolveGitTopLevel, readGitHead, readGitChangedFilesSince, readGitFileChangeTimesSince, verificationFileExists, normalizeVerificationFiles, isDirectiveShapedProjectRule, takeCurateSafetyRefusalCount, wakePlaneStatus, indexCommitsForProject, embedUnembeddedCommits, loadPiConfig, loadPiConfigDetailed, unregisterPiProjectEmbeddings, ensureProjectRegisteredFromPiDirectory, resolvePiHarnessDetection, resolvePiHarnessKind, MEMORY_MURAL_BLOCK, buildMagicContextSection, resolveMuralWire, updateCompactionMarkerAfterPublication, COMPARTMENT_RENDER_EPOCH, MEMORY_RENDER_FORMAT_EPOCH, encodeCachedM0UpgradeIdentity, decodeCachedM0UpgradeIdentity, DEFAULT_HISTORY_BUDGET_TOKENS, renderCompartmentAtTier, renderDecayedCompartments, extractM0Block, TEMPORAL_MARKER_PATTERN, temporalMarkerPrefix, clearInjectionCache, getVisibleMemoryIds, renderHistorianMemoryBlock, DEFAULT_MEMORY_BUDGET_TOKENS, DEFAULT_USER_PROFILE_BUDGET_TOKENS, trimMemoriesToBudgetV2, trimWorkspaceMemoriesToBudgetV2, trimUserMemoriesToBudget, renderMemoryBlockV2, stripMemoryMuralBlock, unifiedSearch, rememberTodowriteToolCallTodos, parseTodos, setTodoSnapshot, registerTodoOverlay, registerTodoStateLifecycle, syncCtxMemoryToolEnabled, registerMagicContextTools };
39607
+ export { COMPACTION_ENABLED_PATH, isDreamerRunnable, isCompactionEnabled, formatConfigParseStatusLine, formatConfigParseNotice, claimConfigParseFailuresOnce, parseJsoncRecovering, detectConfigFile, canonicalModelIdentity, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, ompModelRefToCanonical, resolveModelRefForOmp, setStoragePrivatePermissionEnforcement, MIN_PLAUSIBLE_CONTEXT_LIMIT, catalogOutputReserve, reloadWindowOverlay, getWindowOverlay, resolveWindowOverlayFacts, deriveWindowGeometry, hasTrustedAbsoluteWall, applyProvenInputFloor, formatWindowDerivationLine, isSaneLimit, resolveOutputReserve, getSdkWindowGeometry, getSdkContextLimit, getSdkOutputLimit, getSdkInputLimit, cortexKitUserConfigBasePath, cortexKitProjectConfigBasePath, migrateMagicContextConfigLocations, withContentLanguageDirective, parseCron, nextOccurrence, nextDueAtMs, resolveModelConfigValue, DEFAULT_HISTORIAN_TIMEOUT_MS, DREAM_TASK_PROMOTION_DEFAULTS, LIVE_RELOAD_CONFIG_PATHS, getProtectedTokensTierOverrides, FAIL_CLOSED_DOCTOR_COMMAND, formatFailClosedBlockingMessage, createFailClosedBlockingError, isFailClosedBlockingError, shouldBypassFailClosedBlock, createFailClosedController, setHomeProjectPermission, ProjectIdentityError, resolveProjectIdentityStrict, isUserHomeDirectory, resolveProjectIdentity, isUsableProjectIdentity, resolveProjectIdentityForSession, beginBootQuietPeriod, scheduleAfterBootQuiet, clearCompressionDepth, clearCompressionDepthRange, formatSynapseLaneDescriptor, buildCanonicalChunkTextFromFts, buildCompartmentSummaryFallbackText, canonicalizeInMemoryChunkTextForEmbedding, chunkCanonicalText, chunkEmbeddingWindowsAreCurrent, replaceCompartmentChunkEmbeddings, deleteChunkEmbedBackoffForSession, readSessionActivity, observeSessionActivity, backfillSessionActivity, getMessageIndexSourceIdentity, isMessageIndexSourceCurrent, getLastIndexedOrdinal, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, indexSingleMessage, indexMessagesAfterOrdinal, sweepOrphanedOpenCodeMessageIndexes, MAX_EXECUTE_THRESHOLD, escalationBands, HYGIENE_PROVIDER_UNITS_VERSION, sessionDecisionCalibration, transitionSessionHygieneUnits, computeProtectionWindow, readEpochFloorSnapshot, getProtectionWindowForSession, persistCachedM0, clearCachedM0M1, readReplayDocument, updateReplayDocument, isProviderOverflowFailClosedProven, describeProtectedTailDrainBudgetSkip, loadProtectedTailMeta, markProtectedTailPolicyV3Seeded, recordProtectedTailPublicationFloor, recordProtectedTailNoEligibleHead, getWrapupInProgressState, isWrapupInProgress, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, reserveProtectedTailDrainTokens, clearEmergencyDrainLatch, recordHistorianDrainFailure, clearHistorianDrainFailure, rollbackProtectedTailDrainReservation, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, markChannel1PostReduceGracePending, captureChannel1PostReduceGraceBaseline, getChannel2NudgeState, getChannel2NudgeClaim, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getPersistedNoteNudge, setPersistedNoteNudgeTrigger, setPersistedNoteNudgeTriggerMessageId, getNoteNudgeAnchors, getAutoSearchHintDecisions, deliverNoteNudgeAtomic, appendAutoSearchHintDecision, pruneNoteNudgeAnchors, pruneAutoSearchHintDecisions, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, getNoteLastReadAt, incrementHistorianFailure, clearHistorianFailureState, getOverflowState, recordOverflowDetected, clearEmergencyRecovery, clearDetectedContextLimit, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, THINKING_BINDING_RECOVERY_FROZEN_PREFIX, THINKING_BINDING_STRIP_ORDER_END_MARKER, thinkingBindingRecoveryFrozenId, getThinkingBindingRecoveryTarget, armThinkingBindingRecovery, clearThinkingBindingRecoveryIf, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getProcessedImageStrippedIds, addProcessedImageStrippedIds, getPendingCompactionMarkerState, getCompactionMarkerHealth, clearPendingCompactionMarkerStateIf, getPendingPiCompactionMarkerState, setPendingPiCompactionMarkerState, clearPendingPiCompactionMarkerStateIf, getSessionsWithPendingPiMarker, setSessionWorkMetrics, getSessionWorkMetrics, resolveEpochFloorForPass, getOrCreateSessionMeta, updateSessionMeta, advanceToolReclaimWatermark, retryPendingSessionCleanups, retryPendingRustSessionCleanupsForProject, recordSessionProjectIdentity, embedPauseBySession, embedRunStateBySession, autoEmbedAttemptedBySession, autoEmbedIdentityBySession, invalidateAutoEmbedSession, getEmbedDrainUiStatus, piHarnessKindFromExecutable, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLease, releaseCompartmentLeaseBestEffort, isCompartmentLeaseHeld, isNoContentCompartment, HAS_COMPARTMENT_CONTENT_SQL, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, getCompartmentsByEndMessageId, appendCompartments, saveRecompStagingPass, getRecompStaging, clearRecompStaging, getRecompPartialRange, setRecompPartialRange, escapeXmlAttr, escapeXmlContent, getModuleNoteEvaluationBridge, getContextStoreUuid, getAuthorityManagedMarker, drainMirrorPages, V2_MEMORY_CATEGORIES, parseCompartmentOutput, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, sourceNameForMemory, computeWorkspaceEpochFingerprint, readProjectDocsCanonical, BoundedSessionMap, registerLkgPersistence, signatureForFields, lkgContentFields, lkgContentDigestFromFields, exactReusablePrefix, incrementalLkgContentDigests, captureSlot, getSlot, dropSlot, noteEntry, encodePiContentDecision, getPiContentDecisions, freezePiContentDecision, getNativeReplayState, saveNativeToolInputs, addNativeReasoningIds, copySessionStateForClone, getErrorMessage, describeError, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, applySqliteTuningPragmas, runSqliteOptimize, openDatabase, openDatabaseAsync, closeDatabase, queueM0Mutation, getMaxM0MutationId, queueMemoryMutation, getMemoryMutationsForRender, getMemoryMutationsForRenderByProjects, getMaxMemoryMutationId, getMaxMemoryMutationIdForProjects, getNotes, getSessionNotes, getPendingSmartNotes, getReadySmartNotes, markNoteReady, markNoteChecked, queuePendingOp, getPendingOps, getPendingOpsCount, clearPendingOps, removePendingOp, PRIMER_CANDIDATE_TTL_MS, PRIMER_CANDIDATE_MAX_AGE_MS, primerOccurrenceKey, primerOccurrenceUtcDay, insertPrimerCandidates, updatePrimerCandidateEmbedding, getPrimerCandidatesByIds, getPrimerCandidatesForPromotion, countPrimerCandidatesForProject, getActivePrimers, createPrimer, updatePrimerSupport, updatePrimerAnswer, GLOBAL_USER_PROFILE_PROJECT_PATH, getProjectState, bumpProjectUserProfileVersion, saveSourceContent, getSourceContents, recordSubagentInvocation, getLatestHistorianInvocationId, noteTouchedAt, noteNudgePickIndex, formatNoteNudge, promptSurfaceHashMaterial, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, SYNTH_USER_ID_PREFIX, resolvePiStableId, readPiSessionSnapshot, readPiSessionMessages, readPiSessionMessagePage, findLastModelKeyFromBranch, convertEntriesToRawMessages, convertEntriesToRawMessagePage, computeCueContentHash, hasMuralCueColumns, getMuralCueState, memoryNeedsCue, setMuralCue, recordMuralCueRejection, invalidateMemory, computeNormalizedHash, hasMemoryShareableColumn, hasMemoryClassifiedAtColumn, getUnclassifiedMemoryIds, insertMemory, getMemoryByHash, getMemoriesByProject, getMemoriesByProjects, getMaxMemoryIdForProjects, getMemoryById, setMemoryClassification, archiveMemory, getMemoryCountsByStatus, USER_MEMORY_CANDIDATE_TTL_MS, insertUserMemoryCandidates, getUserMemoryCandidates, deleteUserMemoryCandidates, pruneExpiredUserMemoryCandidates, insertUserMemory, getActiveUserMemories, updateUserMemoryContent, dismissUserMemory, getTaskScheduleState, getFailingDreamTasks, getMostRecentTaskRunAt, pruneNonCanonicalTaskRows, deleteTaskScheduleRowsForProject, seedTaskScheduleState, writeTaskScheduleState, isRetrospectiveWindowProcessed, recordRetrospectiveWindowProcessed, curateCategoryForMemoryCategory, peekCurateCategoryScope, beginCurateCategoryRun, curateTaskStateAfterSuccess, cosineSimilarity, GIT_SWEEP_LEASE_RENEWAL_MS, acquireGitSweepLease, renewGitSweepLease, markGitSweepSuccessAndRelease, parkGitSweepNonIndexable, releaseGitSweepLease, describeShadowBackfillWriteRefusal, contentSha256, sweepStaleEmbeddingIdentitiesForProject, enqueueShadowEmbeddingItems, getProjectEmbeddingSnapshot, getProjectChunkEmbeddingModelId, getProjectEmbeddingMaxInputTokens, embedTextForProject, embedBatchForProject, embedItemsForProject, embedUnembeddedMemoriesForProject, drainCommitBacklogForProject, embedSessionCompartmentChunks, getEmbeddingCoverageStatus, getAutoEmbeddingSessionCoverage, promoteSessionFactsDurable, embedPromotedFacts, recordMemoryMapping, recordMemoryVerifications, getUnmappedMemoryIds, clearMemoryVerifications, getMemoryVerifications, resolveGitTopLevel, readGitHead, readGitChangedFilesSince, readGitFileChangeTimesSince, verificationFileExists, normalizeVerificationFiles, isDirectiveShapedProjectRule, takeCurateSafetyRefusalCount, wakePlaneStatus, indexCommitsForProject, embedUnembeddedCommits, loadPiConfig, loadPiConfigDetailed, unregisterPiProjectEmbeddings, ensureProjectRegisteredFromPiDirectory, resolvePiHarnessDetection, resolvePiHarnessKind, MEMORY_MURAL_BLOCK, buildMagicContextSection, resolveMuralWire, updateCompactionMarkerAfterPublication, COMPARTMENT_RENDER_EPOCH, MEMORY_RENDER_FORMAT_EPOCH, encodeCachedM0UpgradeIdentity, decodeCachedM0UpgradeIdentity, DEFAULT_HISTORY_BUDGET_TOKENS, renderCompartmentAtTier, renderDecayedCompartments, extractM0Block, TEMPORAL_MARKER_PATTERN, temporalMarkerPrefix, clearInjectionCache, getVisibleMemoryIds, renderHistorianMemoryBlock, DEFAULT_MEMORY_BUDGET_TOKENS, DEFAULT_USER_PROFILE_BUDGET_TOKENS, trimMemoriesToBudgetV2, trimWorkspaceMemoriesToBudgetV2, trimUserMemoriesToBudget, renderMemoryBlockV2, stripMemoryMuralBlock, unifiedSearch, rememberTodowriteToolCallTodos, parseTodos, setTodoSnapshot, registerTodoOverlay, registerTodoStateLifecycle, syncCtxMemoryToolEnabled, registerMagicContextTools };
@@ -2296,7 +2296,10 @@ function installTransactionRouting(db, readonly) {
2296
2296
  writable: true,
2297
2297
  value: (sql) => {
2298
2298
  if (/^\s*BEGIN\s+(?:IMMEDIATE|EXCLUSIVE)(?:\s+TRANSACTION)?\s*;?\s*$/i.test(sql)) {
2299
- retryAcquisition(() => nativeExec(sql), sql.trim());
2299
+ if (transformPassScope.getStore()?.active || backgroundWriterScope.getStore())
2300
+ acquireShort(db, () => nativeExec(sql), sql.trim());
2301
+ else
2302
+ nativeExec(sql);
2300
2303
  return db;
2301
2304
  }
2302
2305
  return nativeExec(sql);
@@ -2399,8 +2402,27 @@ function buildNodeSqliteDatabaseClass(DatabaseSync) {
2399
2402
  return NodeSqliteDatabase;
2400
2403
  }
2401
2404
  var Database = TrackedDatabase;
2405
+ function pragmaValue(db, name) {
2406
+ const row = db.prepare(`PRAGMA ${name}`).get();
2407
+ if (!row)
2408
+ return;
2409
+ return row[name] ?? Object.values(row)[0];
2410
+ }
2411
+ function pragmaNumber(db, name) {
2412
+ try {
2413
+ const value = pragmaValue(db, name);
2414
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
2415
+ } catch {
2416
+ return null;
2417
+ }
2418
+ }
2402
2419
  var privilegeDepth = new WeakMap;
2403
2420
  var transformPassScope = new AsyncLocalStorage;
2421
+ var admissionScope = new AsyncLocalStorage;
2422
+ var backgroundWriterScope = new AsyncLocalStorage;
2423
+ function withSqliteBackgroundWriter(operation) {
2424
+ return backgroundWriterScope.run(true, operation);
2425
+ }
2404
2426
  function isTransientSqliteError(error) {
2405
2427
  if (!error || typeof error !== "object")
2406
2428
  return false;
@@ -2412,28 +2434,32 @@ class SqliteAcquisitionBusyError extends Error {
2412
2434
  code = "SQLITE_BUSY";
2413
2435
  stage;
2414
2436
  constructor(cause, stage = "BEGIN IMMEDIATE") {
2415
- super("SQLite writer acquisition remained busy after 3 attempts", { cause });
2437
+ super("SQLite writer acquisition remained busy", { cause });
2416
2438
  this.name = "SqliteAcquisitionBusyError";
2417
2439
  this.stage = stage;
2418
2440
  }
2419
2441
  }
2420
- function retryAcquisition(acquire, stage) {
2421
- if (!transformPassScope.getStore()?.active) {
2442
+ var SHORT_BUSY_TIMEOUT_MS = 25;
2443
+ var FOREGROUND_IN_PASS_BUSY_TIMEOUT_MS = 250;
2444
+ function acquireShort(db, acquire, site) {
2445
+ const started = performance.now();
2446
+ const previous = pragmaNumber(db, "busy_timeout");
2447
+ let acquired = false;
2448
+ try {
2449
+ const timeout = transformPassScope.getStore()?.active && !admissionScope.getStore() && !backgroundWriterScope.getStore() ? FOREGROUND_IN_PASS_BUSY_TIMEOUT_MS : SHORT_BUSY_TIMEOUT_MS;
2450
+ db.exec(`PRAGMA busy_timeout=${timeout}`);
2422
2451
  acquire();
2423
- return;
2424
- }
2425
- const delays = [500, 1000];
2426
- for (let attempt = 0;; attempt++) {
2427
- try {
2428
- acquire();
2429
- return;
2430
- } catch (error) {
2431
- if (error instanceof SqliteAcquisitionBusyError || !isTransientSqliteError(error))
2432
- throw error;
2433
- if (attempt === delays.length)
2434
- throw new SqliteAcquisitionBusyError(error, stage);
2435
- Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delays[attempt]);
2436
- }
2452
+ acquired = true;
2453
+ } catch (error) {
2454
+ if (transformPassScope.getStore()?.active && isTransientSqliteError(error))
2455
+ throw new SqliteAcquisitionBusyError(error, site);
2456
+ throw error;
2457
+ } finally {
2458
+ if (previous !== null)
2459
+ db.exec(`PRAGMA busy_timeout=${previous}`);
2460
+ const elapsed = performance.now() - started;
2461
+ if (elapsed >= 250)
2462
+ console.warn(`[${new Date().toISOString()}] [magic-context] sqlite acquisition site=${site} lane=${transformPassScope.getStore()?.active ? "foreground" : "background"} elapsed=${Math.round(elapsed)}ms attempts=1 outcome=${acquired ? "acquired" : "busy"}`);
2437
2463
  }
2438
2464
  }
2439
2465
  function isInTransaction(db) {
@@ -2448,7 +2474,13 @@ function withPrivilegedWriter(db, operation) {
2448
2474
  if (nested) {
2449
2475
  db.exec(`SAVEPOINT ${savepoint}`);
2450
2476
  } else {
2451
- retryAcquisition(() => db.exec("BEGIN IMMEDIATE"), "BEGIN IMMEDIATE");
2477
+ try {
2478
+ db.exec("BEGIN IMMEDIATE");
2479
+ } catch (error) {
2480
+ if (transformPassScope.getStore()?.active && isTransientSqliteError(error))
2481
+ throw new SqliteAcquisitionBusyError(error);
2482
+ throw error;
2483
+ }
2452
2484
  }
2453
2485
  privilegeDepth.set(db, previousDepth + 1);
2454
2486
  try {
@@ -4989,4 +5021,4 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
4989
5021
  toolResultBoundaries
4990
5022
  };
4991
5023
  }
4992
- export { CTX_REDUCE_KEEP, newestCtxReduceTagNumbers, textMentionsRecentCommit, removeSystemReminders, hasMeaningfulUserText2, extractTexts2, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isRecord, stableStringify, estimateImageTokensFromDataUrl, TODO_STATUSES, TODO_PRIORITIES, TITLE_DONE_STATUSES, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, compareMeasuredTailPrefix, formatTailHygienePrefixMismatch, freezeTailHygieneMeasurement, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, planEmergencyDrop, measureEmergencyTag, updateTagByteSize, getRecentTagOwnerMessageIds, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, getInertWhitespaceAssistantTags, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, getAssignableTagNumberByMessageId, deriveTagLoadFloor, TAG_SELECT_COLUMNS, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, retreatPastHostUnservedRows, resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, openCodeDbPathExists, getOpenCodeDbProbeDescriptions, recordOpenCodeDbReadFailure, clearOpenCodeDbReadFailure, claimOpenCodeDbDiagnosticOnce, registerSlowWriteReporter, detectSqliteRuntime, Database, withPrivilegedWriter, closeQuietly, withReadOnlySessionDb, RAW_SUMMARY_TEXT_MAX_CHARS, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, DreamTokenBudgetExceeded, TOKEN_BUDGET_FINALIZE_MESSAGE, TOKEN_BUDGET_TOOL_REFUSAL, registerBudgetFinalizeChild, releaseBudgetFinalizeChild, createDreamTokenBudget, droppedInputMarker, createDroppedInputGuard, isEditTool, applyEditMarkerToInput, setDatabase, loadToolDefinitionMeasurements, resolveDecisionCalibration, providerMass, localBudget, calibrationForModelKey, historyLocalBudget, SKELETON_REAL_INPUT_MAX_BYTES, toolInputStringBytes, resolveHostServedBoundaryId2, hasRawMessageProvider2, setRawMessageProvider2, withRawMessageProvider2, cleanUserText2, withRawSessionMessageCache2, readRawSessionMessages2, readRawSessionMessagePage2, getRawSessionMessageOrdinalCount2, visitRawSessionMessages2, readRawSessionMessageRange2, primeTailRawMessageCache2, getCachedAbsoluteMessageCount2, primeInMemoryTailRawMessageCache2, readRawSessionMessageIdOrdinalsForRange2, hasRawSessionMessageById2, readRawSessionMessageOrdinalById2, compareRawSessionMessageOrder2, getRawSessionMessageCount2, getRawSessionTagKeysThrough2, getLegacyProtectedTailStartOrdinal2, readSessionChunk2 };
5024
+ export { registerSlowWriteReporter, detectSqliteRuntime, Database, withSqliteBackgroundWriter, withPrivilegedWriter, CTX_REDUCE_KEEP, newestCtxReduceTagNumbers, textMentionsRecentCommit, removeSystemReminders, hasMeaningfulUserText2, extractTexts2, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isRecord, stableStringify, estimateImageTokensFromDataUrl, TODO_STATUSES, TODO_PRIORITIES, TITLE_DONE_STATUSES, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, compareMeasuredTailPrefix, formatTailHygienePrefixMismatch, freezeTailHygieneMeasurement, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, planEmergencyDrop, measureEmergencyTag, updateTagByteSize, getRecentTagOwnerMessageIds, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, getInertWhitespaceAssistantTags, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, getAssignableTagNumberByMessageId, deriveTagLoadFloor, TAG_SELECT_COLUMNS, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, retreatPastHostUnservedRows, resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, openCodeDbPathExists, getOpenCodeDbProbeDescriptions, recordOpenCodeDbReadFailure, clearOpenCodeDbReadFailure, claimOpenCodeDbDiagnosticOnce, closeQuietly, withReadOnlySessionDb, RAW_SUMMARY_TEXT_MAX_CHARS, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, DreamTokenBudgetExceeded, TOKEN_BUDGET_FINALIZE_MESSAGE, TOKEN_BUDGET_TOOL_REFUSAL, registerBudgetFinalizeChild, releaseBudgetFinalizeChild, createDreamTokenBudget, droppedInputMarker, createDroppedInputGuard, isEditTool, applyEditMarkerToInput, setDatabase, loadToolDefinitionMeasurements, resolveDecisionCalibration, providerMass, localBudget, calibrationForModelKey, historyLocalBudget, SKELETON_REAL_INPUT_MAX_BYTES, toolInputStringBytes, resolveHostServedBoundaryId2, hasRawMessageProvider2, setRawMessageProvider2, withRawMessageProvider2, cleanUserText2, withRawSessionMessageCache2, readRawSessionMessages2, readRawSessionMessagePage2, getRawSessionMessageOrdinalCount2, visitRawSessionMessages2, readRawSessionMessageRange2, primeTailRawMessageCache2, getCachedAbsoluteMessageCount2, primeInMemoryTailRawMessageCache2, readRawSessionMessageIdOrdinalsForRange2, hasRawSessionMessageById2, readRawSessionMessageOrdinalById2, compareRawSessionMessageOrder2, getRawSessionMessageCount2, getRawSessionTagKeysThrough2, getLegacyProtectedTailStartOrdinal2, readSessionChunk2 };
package/dist/index.js CHANGED
@@ -38,8 +38,10 @@ import {
38
38
  formatWindowDerivationLine,
39
39
  isSaneLimit,
40
40
  resolveOutputReserve,
41
+ getSdkWindowGeometry,
41
42
  getSdkContextLimit,
42
43
  getSdkOutputLimit,
44
+ getSdkInputLimit,
43
45
  cortexKitUserConfigBasePath,
44
46
  cortexKitProjectConfigBasePath,
45
47
  migrateMagicContextConfigLocations,
@@ -58,6 +60,7 @@ import {
58
60
  isFailClosedBlockingError,
59
61
  shouldBypassFailClosedBlock,
60
62
  createFailClosedController,
63
+ setHomeProjectPermission,
61
64
  ProjectIdentityError,
62
65
  resolveProjectIdentityStrict,
63
66
  isUserHomeDirectory,
@@ -423,8 +426,10 @@ import {
423
426
  registerTodoStateLifecycle,
424
427
  syncCtxMemoryToolEnabled,
425
428
  registerMagicContextTools
426
- } from "./index-esnhck8v.js";
429
+ } from "./index-atb1ecv9.js";
427
430
  import {
431
+ Database,
432
+ withSqliteBackgroundWriter,
428
433
  CTX_REDUCE_KEEP,
429
434
  newestCtxReduceTagNumbers,
430
435
  textMentionsRecentCommit,
@@ -500,7 +505,6 @@ import {
500
505
  recordOpenCodeDbReadFailure,
501
506
  clearOpenCodeDbReadFailure,
502
507
  claimOpenCodeDbDiagnosticOnce,
503
- Database,
504
508
  closeQuietly,
505
509
  RAW_SUMMARY_TEXT_MAX_CHARS,
506
510
  completedToolArcCrossesBoundary,
@@ -543,7 +547,7 @@ import {
543
547
  getRawSessionTagKeysThrough2,
544
548
  getLegacyProtectedTailStartOrdinal2,
545
549
  readSessionChunk2
546
- } from "./index-gsff513e.js";
550
+ } from "./index-tckvezry.js";
547
551
  import {
548
552
  pushNotification2
549
553
  } from "./index-b3eqj1g6.js";
@@ -1071,7 +1075,7 @@ async function reconcileSessionIndex(db, sessionId, readMessages) {
1071
1075
  const messages = readMessages.readPage ? readMessages.readPage(sessionId, cursor, RECONCILIATION_BATCH_SIZE, finalWatermark) : (fallbackSnapshot ?? []).filter((message) => message.ordinal > cursor && message.ordinal <= pageEnd);
1072
1076
  const retainedMessages = fallbackSnapshot ?? messages;
1073
1077
  heapHolder.activeReconcilerBuffers.set(sessionId, retainedMessages);
1074
- indexMessagesAfterOrdinal(db, sessionId, messages, cursor, pageEnd);
1078
+ withSqliteBackgroundWriter(() => indexMessagesAfterOrdinal(db, sessionId, messages, cursor, pageEnd));
1075
1079
  const nextCursor = getMessageIndexReconciliationStartOrdinal(db, sessionId);
1076
1080
  if (nextCursor <= cursor)
1077
1081
  break;
@@ -1097,6 +1101,8 @@ function scheduleReconciliation(db, sessionId, readMessages) {
1097
1101
  defer(() => {
1098
1102
  reconcileSessionIndex(db, sessionId, readMessages).catch((error) => {
1099
1103
  logIndexingError(sessionId, "reconciliation", error);
1104
+ if (isDatabaseLockedError(error))
1105
+ setTimeout(() => scheduleReconciliation(db, sessionId, readMessages), 1000).unref();
1100
1106
  }).finally(() => {
1101
1107
  heapHolder.reconciliationScheduledSessions.delete(sessionId);
1102
1108
  });
@@ -1126,7 +1132,7 @@ function scheduleIncrementalIndex(db, sessionId, messageId, messageSource) {
1126
1132
  return;
1127
1133
  }
1128
1134
  const wasReconciled = heapHolder.reconciledSessions.delete(sessionId);
1129
- indexSingleMessage(db, sessionId, message);
1135
+ withSqliteBackgroundWriter(() => indexSingleMessage(db, sessionId, message));
1130
1136
  const finalWatermark = getLastIndexedOrdinal(db, sessionId);
1131
1137
  if (wasReconciled && isMessageIndexReconciledThrough(db, sessionId, finalWatermark)) {
1132
1138
  heapHolder.reconciledSessions.add(sessionId);
@@ -1135,6 +1141,8 @@ function scheduleIncrementalIndex(db, sessionId, messageId, messageSource) {
1135
1141
  }).catch((error) => {
1136
1142
  heapHolder.reconciledSessions.delete(sessionId);
1137
1143
  logIndexingError(sessionId, `incremental index for ${messageId}`, error);
1144
+ if (isDatabaseLockedError(error))
1145
+ setTimeout(() => scheduleIncrementalIndex(db, sessionId, messageId, messageSource), 1000).unref();
1138
1146
  }).finally(() => {
1139
1147
  heapHolder.pendingIncrementalKeys.delete(schedulingKey);
1140
1148
  });
@@ -1989,6 +1997,21 @@ function resolveKnownHistorianContextLimit(historianModelOverride) {
1989
1997
  const limit = getSdkContextLimit(providerID, modelID, undefined, { reservation: "none" });
1990
1998
  return typeof limit === "number" && limit > 0 ? limit : undefined;
1991
1999
  }
2000
+ function resolveHistorianProducerLimits(modelKey) {
2001
+ if (!modelKey?.includes("/"))
2002
+ return {};
2003
+ const [provider, ...parts] = modelKey.split("/");
2004
+ const model = parts.join("/");
2005
+ if (!provider || !model)
2006
+ return {};
2007
+ const input = getSdkInputLimit(provider, model);
2008
+ const window = getSdkWindowGeometry(provider, model)?.derivation.window;
2009
+ const context = window ?? (input === undefined ? resolveKnownHistorianContextLimit(modelKey) : undefined);
2010
+ return {
2011
+ ...context !== undefined ? { context } : {},
2012
+ ...input !== undefined ? { input } : {}
2013
+ };
2014
+ }
1992
2015
  function resolveHistorianContextLimit(historianModelOverride) {
1993
2016
  if (typeof historianModelOverride === "string" && historianModelOverride.includes("/")) {
1994
2017
  return resolveKnownHistorianContextLimit(historianModelOverride) ?? DEFAULT_HISTORIAN_CONTEXT_FALLBACK;
@@ -4185,6 +4208,27 @@ function forgetHistorianOutputCap(sessionId) {
4185
4208
  historianCaps.delete(sessionId);
4186
4209
  }
4187
4210
 
4211
+ // ../plugin/src/shared/run-token-log.ts
4212
+ function count(value) {
4213
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
4214
+ }
4215
+ function runTokenLog(tokens, maxTokens, finish) {
4216
+ const usage = tokens && typeof tokens === "object" ? tokens : {};
4217
+ const cache = usage.cache && typeof usage.cache === "object" ? usage.cache : {};
4218
+ return {
4219
+ input: count(usage.input ?? usage.input_tokens),
4220
+ output: count(usage.output ?? usage.output_tokens),
4221
+ reasoning: count(usage.reasoning ?? usage.reasoning_tokens),
4222
+ cache_read: count(usage.cacheRead ?? usage.cache_read ?? usage.cached_input_tokens ?? cache.read),
4223
+ cache_write: count(usage.cacheWrite ?? usage.cache_write ?? usage.cache_write_tokens ?? cache.write),
4224
+ max_tokens: count(maxTokens),
4225
+ finish_reason: typeof finish === "string" ? finish : null
4226
+ };
4227
+ }
4228
+ function formatRunTokenLog(tokens) {
4229
+ return `tokens=${JSON.stringify(tokens)}`;
4230
+ }
4231
+
4188
4232
  // ../plugin/src/hooks/magic-context/historian-prompt.generated.ts
4189
4233
  var COMPARTMENT_AGENT_SYSTEM_PROMPT = `# Historian
4190
4234
 
@@ -5347,11 +5391,14 @@ function clampProducerAtomChars(text, maxChars, marker) {
5347
5391
  const tailChars = Math.floor(keptChars / 2);
5348
5392
  return `${text.slice(0, headChars)}${marker}${text.slice(text.length - tailChars)}`;
5349
5393
  }
5350
- function producerInputTokenLimit(contextLimitTokens, maxOutputTokens) {
5351
- if (typeof contextLimitTokens !== "number" || !Number.isFinite(contextLimitTokens) || contextLimitTokens <= 0 || !Number.isFinite(maxOutputTokens) || maxOutputTokens < 0) {
5394
+ function producerInputTokenLimit(contextLimitTokens, maxOutputTokens, inputLimitTokens) {
5395
+ if (!Number.isFinite(maxOutputTokens) || maxOutputTokens < 0)
5352
5396
  return;
5353
- }
5354
- const usableInputTokens = Math.floor(contextLimitTokens - maxOutputTokens);
5397
+ const shared = typeof contextLimitTokens === "number" && Number.isFinite(contextLimitTokens) && contextLimitTokens > 0 ? contextLimitTokens - maxOutputTokens : undefined;
5398
+ const input = typeof inputLimitTokens === "number" && Number.isFinite(inputLimitTokens) && inputLimitTokens > 0 ? inputLimitTokens : undefined;
5399
+ if (shared === undefined && input === undefined)
5400
+ return;
5401
+ const usableInputTokens = Math.floor(Math.min(shared ?? Infinity, input ?? Infinity));
5355
5402
  if (usableInputTokens <= 0)
5356
5403
  return;
5357
5404
  const limit = Math.floor(usableInputTokens * (1 - PRODUCER_WINDOW_REFUSAL_MARGIN));
@@ -5363,15 +5410,15 @@ function historianProducerReserve(window, configuredOutput, catalogOutput) {
5363
5410
  return window === undefined ? 0 : catalogOutputReserve(window, catalogOutput);
5364
5411
  }
5365
5412
  function producerWindowFailureReason(input) {
5366
- const { producerSourceTokens, contextLimitTokens, maxOutputTokens } = input;
5367
- const producerInputLimitTokens = producerInputTokenLimit(contextLimitTokens, maxOutputTokens);
5368
- if (producerInputLimitTokens === undefined || typeof contextLimitTokens !== "number" || !Number.isFinite(producerSourceTokens) || producerSourceTokens <= 0) {
5413
+ const { producerSourceTokens, contextLimitTokens, inputLimitTokens, maxOutputTokens } = input;
5414
+ const producerInputLimitTokens = producerInputTokenLimit(contextLimitTokens, maxOutputTokens, inputLimitTokens);
5415
+ if (producerInputLimitTokens === undefined || !Number.isFinite(producerSourceTokens) || producerSourceTokens <= 0) {
5369
5416
  return null;
5370
5417
  }
5371
- const usableInputTokens = Math.max(0, Math.floor(contextLimitTokens - maxOutputTokens));
5418
+ const usableInputTokens = Math.floor(Math.min(contextLimitTokens === undefined ? Infinity : Math.max(0, contextLimitTokens - maxOutputTokens), inputLimitTokens ?? Infinity));
5372
5419
  if (producerSourceTokens <= producerInputLimitTokens)
5373
5420
  return null;
5374
- return `producer_source_exceeds_window producer_source_tokens=${Math.round(producerSourceTokens)} usable_input_tokens=${usableInputTokens} producer_input_limit_tokens=${producerInputLimitTokens} context_limit_tokens=${Math.round(contextLimitTokens)} max_output_tokens=${Math.round(maxOutputTokens)} estimator_margin=${PRODUCER_WINDOW_REFUSAL_MARGIN}`;
5421
+ return `producer_source_exceeds_window producer_source_tokens=${Math.round(producerSourceTokens)} usable_input_tokens=${usableInputTokens} producer_input_limit_tokens=${producerInputLimitTokens} context_limit_tokens=${contextLimitTokens === undefined ? "unknown" : Math.round(contextLimitTokens)} max_output_tokens=${Math.round(maxOutputTokens)} estimator_margin=${PRODUCER_WINDOW_REFUSAL_MARGIN}`;
5375
5422
  }
5376
5423
  function splitMarkerPair() {
5377
5424
  return `
@@ -5380,7 +5427,7 @@ ${HISTORIAN_TRUNCATION_MARKER}
5380
5427
  `;
5381
5428
  }
5382
5429
  function fitAtomicHistorianSourceToProducerWindow(args) {
5383
- const producerInputLimitTokens = producerInputTokenLimit(args.contextLimitTokens, args.maxOutputTokens);
5430
+ const producerInputLimitTokens = producerInputTokenLimit(args.contextLimitTokens, args.maxOutputTokens, args.inputLimitTokens);
5384
5431
  const originalTokens = estimateTokens(args.text);
5385
5432
  if (producerInputLimitTokens === undefined || originalTokens < producerInputLimitTokens || producerInputLimitTokens <= 0) {
5386
5433
  return { text: args.text, producerInputLimitTokens, removedTokens: 0 };
@@ -5414,7 +5461,7 @@ function fitAtomicHistorianSourceToProducerWindow(args) {
5414
5461
  };
5415
5462
  }
5416
5463
  function producerPromptFailureReason(input) {
5417
- const limit = producerInputTokenLimit(input.contextLimitTokens, input.maxOutputTokens);
5464
+ const limit = producerInputTokenLimit(input.contextLimitTokens, input.maxOutputTokens, input.inputLimitTokens);
5418
5465
  const tokens = providerMass({ prose: input.sourceLocal, system: input.systemLocal, tools: input.toolsLocal }, calibrationForModelKey(input.modelKey), true);
5419
5466
  if (limit === undefined)
5420
5467
  return null;
@@ -5525,12 +5572,15 @@ function createV1HiddenCompletionExecutor(client, db, directory) {
5525
5572
  throw new Error(`Historian host recorded an assistant error${finish}: ${describeAssistantFailure(assistantFailure.error)}`);
5526
5573
  }
5527
5574
  const text = extractLatestAssistantText(messages);
5575
+ const latest = Array.isArray(messages) ? messages.filter((message) => isRecord(message) && isRecord(message.info) && message.info.role === "assistant").sort((left, right) => historianMessageCreatedAt(right) - historianMessageCreatedAt(left))[0] : undefined;
5576
+ const info = latest && isRecord(latest.info) ? latest.info : {};
5528
5577
  return {
5529
5578
  messages,
5530
5579
  text,
5531
5580
  reasoning: text ? null : extractLatestHistorianReasoning(messages),
5532
5581
  lengthCapped: hasLengthCappedOutput(messages),
5533
- usage: sumTokensFromChildMessages(messages)
5582
+ usage: sumTokensFromChildMessages(messages),
5583
+ tokenLog: runTokenLog(info.tokens, undefined, info.finish ?? info.finish_reason ?? info.finishReason)
5534
5584
  };
5535
5585
  },
5536
5586
  async close(handle, settlement) {
@@ -5549,8 +5599,8 @@ function createV1HiddenCompletionExecutor(client, db, directory) {
5549
5599
  }
5550
5600
  };
5551
5601
  }
5552
- function historianReasoningBudgetDiagnostic(outputTokens) {
5553
- return `historian ran out of output budget while reasoning (length-capped at ${outputTokens} tokens, no text) — set historian.maxTokens or route historian.model to a low-reasoning lane/variant`;
5602
+ function historianReasoningBudgetDiagnostic(outputTokens, tokens) {
5603
+ return `historian ran out of output budget while reasoning (length-capped at ${outputTokens} tokens, no text${tokens ? `; ${formatRunTokenLog(tokens)}` : ""}) — set historian.maxTokens or route historian.model to a low-reasoning lane/variant`;
5554
5604
  }
5555
5605
  async function runValidatedHistorianPass(args) {
5556
5606
  const firstRun = await runHistorianPrompt({
@@ -5739,8 +5789,10 @@ async function runHistorianPrompt(args) {
5739
5789
  const modelKey = selected ? `${selected.providerID}/${selected.modelID}` : undefined;
5740
5790
  const system = withContentLanguageDirective(agentId === HISTORIAN_EDITOR_AGENT ? HISTORIAN_EDITOR_SYSTEM_PROMPT : COMPARTMENT_AGENT_SYSTEM_PROMPT, args.language);
5741
5791
  const contextLimitTokens = selected ? getSdkContextLimit(selected.providerID, selected.modelID, undefined, { reservation: "none" }) : undefined;
5742
- const reserve = historianProducerReserve(contextLimitTokens, args.maxOutputTokens, selected ? getSdkOutputLimit(selected.providerID, selected.modelID) : undefined);
5743
- if (contextLimitTokens !== undefined && producerInputTokenLimit(contextLimitTokens, reserve) === undefined && modelKey && !unknownProducerWindows.has(modelKey)) {
5792
+ const producerLimits = selected ? resolveHistorianProducerLimits(modelKey) : {};
5793
+ const producerContext = producerLimits.context ?? (producerLimits.input === undefined ? contextLimitTokens : undefined);
5794
+ const reserve = historianProducerReserve(producerContext, args.maxOutputTokens, selected ? getSdkOutputLimit(selected.providerID, selected.modelID) : undefined);
5795
+ if (contextLimitTokens !== undefined && producerInputTokenLimit(producerContext, reserve, producerLimits.input) === undefined && modelKey && !unknownProducerWindows.has(modelKey)) {
5744
5796
  unknownProducerWindows.add(modelKey);
5745
5797
  sessionLog(parentSessionId, `producer window inconsistent for ${modelKey}: window=${contextLimitTokens} reserve=${reserve}; sending unguarded`);
5746
5798
  }
@@ -5749,7 +5801,8 @@ async function runHistorianPrompt(args) {
5749
5801
  systemLocal: estimateTokens(system),
5750
5802
  toolsLocal: 0,
5751
5803
  modelKey,
5752
- contextLimitTokens,
5804
+ contextLimitTokens: producerContext,
5805
+ inputLimitTokens: producerLimits.input,
5753
5806
  maxOutputTokens: reserve
5754
5807
  });
5755
5808
  if (failure)
@@ -5783,9 +5836,15 @@ async function runHistorianPrompt(args) {
5783
5836
  }
5784
5837
  completion = await executor.collect(handle, 50);
5785
5838
  const lengthCapped = completion.lengthCapped;
5839
+ const tokens = {
5840
+ ...runTokenLog(undefined, args.maxOutputTokens),
5841
+ ...completion.tokenLog,
5842
+ max_tokens: args.maxOutputTokens ?? null
5843
+ };
5844
+ sessionLog(parentSessionId, `historian response_chars=${(completion.text ?? completion.reasoning ?? "").length} ${formatRunTokenLog(tokens)}`);
5786
5845
  const textResult = completion.text;
5787
5846
  const reasoningResult = textResult ? null : completion.reasoning;
5788
- const emptyError = !textResult && reasoningResult && lengthCapped ? historianReasoningBudgetDiagnostic(completion.usage.output) : !textResult && !reasoningResult ? "Historian returned no assistant output." : !textResult ? "Historian returned reasoning but no assistant text." : lengthCapped ? "Historian returned length-capped output." : null;
5847
+ const emptyError = !textResult && reasoningResult && lengthCapped ? historianReasoningBudgetDiagnostic(completion.usage.output, tokens) : !textResult && !reasoningResult ? "Historian returned no assistant output." : !textResult ? "Historian returned reasoning but no assistant text." : lengthCapped ? `Historian returned length-capped output. ${formatRunTokenLog(tokens)}` : null;
5789
5848
  const invocationId = recordInvocation({
5790
5849
  status: emptyError ? "empty" : "completed",
5791
5850
  messages: completion.messages,
@@ -6159,7 +6218,7 @@ function startLeaseHeartbeat(db, holderId, leaseKey, onLost, intervalOrAcquisiti
6159
6218
  }
6160
6219
  };
6161
6220
  beat();
6162
- const timer = lost ? undefined : setInterval(beat, intervalMs);
6221
+ const timer = lost ? undefined : setInterval(() => withSqliteBackgroundWriter(beat), intervalMs);
6163
6222
  return {
6164
6223
  stop: () => {
6165
6224
  if (timer)
@@ -6878,8 +6937,10 @@ async function runHiddenSingleShotPrompt(args) {
6878
6937
  callContext: args.callContext,
6879
6938
  fetchOutput: () => args.executor.collect(handle, 50),
6880
6939
  validateOutput: (completion) => {
6940
+ const tokens = completion.tokenLog ?? runTokenLog(undefined);
6941
+ sessionLog(args.parentSessionId ?? "dreamer", `${args.callContext} response_chars=${(completion.text ?? completion.reasoning ?? "").length} ${formatRunTokenLog(tokens)}`);
6881
6942
  if (completion.lengthCapped) {
6882
- throw new Error(completion.reasoning && !completion.text ? `${args.callContext} ran out of output budget while reasoning (length-capped at ${completion.usage.output} tokens, no text) — set dreamer.maxTokens or use a low-reasoning model` : `${args.callContext} returned length-capped output`);
6943
+ throw new Error(completion.reasoning && !completion.text ? `${args.callContext} ran out of output budget while reasoning (length-capped at ${completion.usage.output} tokens, no text; ${formatRunTokenLog(tokens)}) — set dreamer.maxTokens or use a low-reasoning model` : `${args.callContext} returned length-capped output; ${formatRunTokenLog(tokens)}`);
6883
6944
  }
6884
6945
  const text = completion.text;
6885
6946
  if (!text && !args.allowEmpty)
@@ -7714,8 +7775,10 @@ async function classifyOneChunk(args, chunk, anchors, sliceMs, signal) {
7714
7775
  fetchOutput: () => executor.collect(opened, 50),
7715
7776
  validateOutput: (completion) => {
7716
7777
  const messages = completion.messages ?? [];
7778
+ const tokens = completion.tokenLog ?? runTokenLog(undefined);
7779
+ sessionLog(args.parentSessionId ?? "dreamer", `dreamer:classify response_chars=${(completion.text ?? completion.reasoning ?? "").length} ${formatRunTokenLog(tokens)}`);
7717
7780
  if (completion.lengthCapped) {
7718
- throw new Error(completion.reasoning && !completion.text ? `classify ran out of output budget while reasoning (length-capped at ${completion.usage.output} tokens, no text) — set dreamer.maxTokens or use a low-reasoning model` : "classify returned length-capped output");
7781
+ throw new Error(completion.reasoning && !completion.text ? `classify ran out of output budget while reasoning (length-capped at ${completion.usage.output} tokens, no text; ${formatRunTokenLog(tokens)}) — set dreamer.maxTokens or use a low-reasoning model` : `classify returned length-capped output; ${formatRunTokenLog(tokens)}`);
7719
7782
  }
7720
7783
  const text = completion.text;
7721
7784
  if (!text)
@@ -14433,6 +14496,205 @@ function clearDreamerTickFailure(db) {
14433
14496
  db.prepare("DELETE FROM schema_migrations_meta WHERE key = ?").run(DREAMER_TICK_FAILURE_KEY);
14434
14497
  }
14435
14498
 
14499
+ // ../plugin/src/hooks/magic-context/lkg-persist.ts
14500
+ import { createHash as createHash11 } from "node:crypto";
14501
+ function parseStringArray(value) {
14502
+ if (typeof value !== "string")
14503
+ return null;
14504
+ let parsed;
14505
+ try {
14506
+ parsed = JSON.parse(value);
14507
+ } catch {
14508
+ return null;
14509
+ }
14510
+ if (!Array.isArray(parsed))
14511
+ return null;
14512
+ const result = [];
14513
+ for (const entry of parsed) {
14514
+ if (typeof entry !== "string" || entry.length === 0)
14515
+ return null;
14516
+ result.push(entry);
14517
+ }
14518
+ return result;
14519
+ }
14520
+ function parseNullableString(value) {
14521
+ if (value === null || value === undefined)
14522
+ return null;
14523
+ return typeof value === "string" ? value : undefined;
14524
+ }
14525
+ function parseNullableInteger(value) {
14526
+ if (value === null || value === undefined)
14527
+ return;
14528
+ return typeof value === "number" && Number.isSafeInteger(value) ? value : undefined;
14529
+ }
14530
+ function parsePersistedLkgSlot(row) {
14531
+ if (!row || typeof row !== "object")
14532
+ return;
14533
+ const record = row;
14534
+ const jsonPrefix = record.json_prefix;
14535
+ const lastInputMessageId = record.last_input_message_id;
14536
+ const capturedAt = record.captured_at;
14537
+ let inputIdsRaw = record.input_id_seq;
14538
+ let piOutputEntryIds;
14539
+ if (typeof inputIdsRaw === "string") {
14540
+ try {
14541
+ const metadata = JSON.parse(inputIdsRaw);
14542
+ if (metadata && !Array.isArray(metadata) && metadata.version === 1 && Array.isArray(metadata.piOutputEntryIds)) {
14543
+ if (!metadata.piOutputEntryIds.every((id) => id === null || typeof id === "string" && id.length > 0))
14544
+ return;
14545
+ piOutputEntryIds = metadata.piOutputEntryIds;
14546
+ inputIdsRaw = JSON.stringify(metadata.inputIds);
14547
+ }
14548
+ } catch {
14549
+ return;
14550
+ }
14551
+ }
14552
+ const inputIdSeq = parseStringArray(inputIdsRaw);
14553
+ const inputContentDigests = parseStringArray(record.input_content_digests);
14554
+ const modelKey = parseNullableString(record.model_key);
14555
+ const providerKey = parseNullableString(record.provider_key);
14556
+ if (typeof jsonPrefix !== "string" || typeof lastInputMessageId !== "string" || lastInputMessageId.length === 0 || typeof capturedAt !== "number" || !Number.isFinite(capturedAt) || inputIdSeq === null || inputContentDigests === null || inputContentDigests.length !== inputIdSeq.length || modelKey === undefined || providerKey === undefined) {
14557
+ return;
14558
+ }
14559
+ if (piOutputEntryIds) {
14560
+ try {
14561
+ const output = JSON.parse(jsonPrefix);
14562
+ const inputs = new Set(inputIdSeq);
14563
+ if (!Array.isArray(output) || output.length !== piOutputEntryIds.length || piOutputEntryIds.some((id) => id !== null && !inputs.has(id)))
14564
+ return;
14565
+ } catch {
14566
+ return;
14567
+ }
14568
+ }
14569
+ let inputContentSignatures;
14570
+ if (record.input_content_signatures !== null && record.input_content_signatures !== undefined) {
14571
+ const parsed = parseStringArray(record.input_content_signatures);
14572
+ if (parsed === null || parsed.length !== inputIdSeq.length)
14573
+ return;
14574
+ inputContentSignatures = parsed;
14575
+ }
14576
+ const slot = {
14577
+ jsonPrefix,
14578
+ inputIdSeq,
14579
+ inputContentDigests,
14580
+ lastInputMessageId,
14581
+ modelKey,
14582
+ providerKey,
14583
+ capturedAt
14584
+ };
14585
+ if (inputContentSignatures)
14586
+ slot.inputContentSignatures = inputContentSignatures;
14587
+ if (piOutputEntryIds)
14588
+ slot.piOutputEntryIds = piOutputEntryIds;
14589
+ const rowVersion = parseNullableInteger(record.row_version);
14590
+ if (rowVersion !== undefined)
14591
+ slot.rowVersion = rowVersion;
14592
+ const captureSequence = parseNullableInteger(record.capture_sequence);
14593
+ if (captureSequence !== undefined)
14594
+ slot.captureSequence = captureSequence;
14595
+ return slot;
14596
+ }
14597
+ var persistedFingerprints = new WeakMap;
14598
+ function saveLkgSlotToDb(db, sessionId, slot) {
14599
+ const fingerprint = createHash11("sha256").update(JSON.stringify([
14600
+ slot.jsonPrefix,
14601
+ slot.inputIdSeq,
14602
+ slot.inputContentDigests,
14603
+ slot.inputContentSignatures,
14604
+ slot.piOutputEntryIds,
14605
+ slot.lastInputMessageId,
14606
+ slot.modelKey,
14607
+ slot.providerKey,
14608
+ slot.rowVersion,
14609
+ slot.captureSequence
14610
+ ])).digest("hex");
14611
+ let saved = persistedFingerprints.get(db);
14612
+ if (saved?.get(sessionId) === fingerprint)
14613
+ return true;
14614
+ try {
14615
+ db.prepare(`INSERT INTO lkg_slots (
14616
+ session_id, json_prefix, input_id_seq, input_content_digests,
14617
+ input_content_signatures, last_input_message_id, model_key, provider_key,
14618
+ captured_at, row_version, capture_sequence
14619
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
14620
+ ON CONFLICT(session_id) DO UPDATE SET
14621
+ json_prefix = excluded.json_prefix,
14622
+ input_id_seq = excluded.input_id_seq,
14623
+ input_content_digests = excluded.input_content_digests,
14624
+ input_content_signatures = excluded.input_content_signatures,
14625
+ last_input_message_id = excluded.last_input_message_id,
14626
+ model_key = excluded.model_key,
14627
+ provider_key = excluded.provider_key,
14628
+ captured_at = excluded.captured_at,
14629
+ row_version = excluded.row_version,
14630
+ capture_sequence = excluded.capture_sequence`).run(sessionId, slot.jsonPrefix, JSON.stringify(slot.piOutputEntryIds ? {
14631
+ version: 1,
14632
+ inputIds: slot.inputIdSeq,
14633
+ piOutputEntryIds: slot.piOutputEntryIds
14634
+ } : slot.inputIdSeq), JSON.stringify(slot.inputContentDigests), slot.inputContentSignatures ? JSON.stringify(slot.inputContentSignatures) : null, slot.lastInputMessageId, slot.modelKey, slot.providerKey, slot.capturedAt, slot.rowVersion ?? null, slot.captureSequence ?? null);
14635
+ if (!saved) {
14636
+ saved = new Map;
14637
+ persistedFingerprints.set(db, saved);
14638
+ }
14639
+ if (saved.size >= 1000) {
14640
+ const oldest = saved.keys().next().value;
14641
+ if (oldest !== undefined)
14642
+ saved.delete(oldest);
14643
+ }
14644
+ saved.set(sessionId, fingerprint);
14645
+ return true;
14646
+ } catch (error) {
14647
+ sessionLog(sessionId, "LKG snapshot persistence failed (in-memory slot retained):", error);
14648
+ return false;
14649
+ }
14650
+ }
14651
+ function clearPersistedLkgSlot(db, sessionId) {
14652
+ try {
14653
+ db.prepare("DELETE FROM lkg_slots WHERE session_id = ?").run(sessionId);
14654
+ persistedFingerprints.get(db)?.delete(sessionId);
14655
+ } catch (error) {
14656
+ sessionLog(sessionId, "LKG snapshot durable clear failed:", error);
14657
+ }
14658
+ }
14659
+ function loadPersistedLkgSlot(db, sessionId) {
14660
+ let row;
14661
+ try {
14662
+ row = db.prepare("SELECT * FROM lkg_slots WHERE session_id = ?").get(sessionId);
14663
+ } catch (error) {
14664
+ sessionLog(sessionId, "LKG snapshot durable load failed:", error);
14665
+ return;
14666
+ }
14667
+ if (!row)
14668
+ return;
14669
+ const slot = parsePersistedLkgSlot(row);
14670
+ if (!slot) {
14671
+ clearPersistedLkgSlot(db, sessionId);
14672
+ return;
14673
+ }
14674
+ return slot;
14675
+ }
14676
+ function pruneStaleLkgSlots(db, now = Date.now()) {
14677
+ const previousTimeout = db.prepare("PRAGMA busy_timeout").get();
14678
+ try {
14679
+ db.exec("PRAGMA busy_timeout = 0");
14680
+ const result = db.prepare(`DELETE FROM lkg_slots WHERE captured_at < ? AND NOT EXISTS (
14681
+ SELECT 1 FROM session_projects sp
14682
+ WHERE sp.session_id = lkg_slots.session_id AND sp.updated_at >= ?
14683
+ )`).run(now - 7 * 24 * 60 * 60 * 1000, now - 7 * 24 * 60 * 60 * 1000);
14684
+ if (result.changes)
14685
+ persistedFingerprints.delete(db);
14686
+ return result.changes;
14687
+ } finally {
14688
+ db.exec(`PRAGMA busy_timeout = ${Number(previousTimeout.timeout) || 0}`);
14689
+ }
14690
+ }
14691
+ function createDbLkgPersistence(db) {
14692
+ return {
14693
+ load: (sessionId) => loadPersistedLkgSlot(db, sessionId),
14694
+ clear: (sessionId) => clearPersistedLkgSlot(db, sessionId)
14695
+ };
14696
+ }
14697
+
14436
14698
  // ../plugin/src/plugin/dream-timer.ts
14437
14699
  var DREAM_TIMER_INTERVAL_MS = 15 * 60 * 1000;
14438
14700
  var GIT_COMMIT_BACKLOG_DRAIN_MAX_MS = 5 * 60 * 1000;
@@ -14595,6 +14857,11 @@ function persistTickOutcome(db, failure) {
14595
14857
  }
14596
14858
  }
14597
14859
  async function runMessageHistoryMaintenance(db) {
14860
+ try {
14861
+ pruneStaleLkgSlots(db);
14862
+ } catch (error) {
14863
+ log("[magic-context] LKG pruning deferred:", error);
14864
+ }
14598
14865
  const cleanup = retryPendingSessionCleanups(db);
14599
14866
  if (cleanup.cleared > 0 || cleanup.failedSessionIds.length > 0) {
14600
14867
  log(`[message-index] pending session cleanup: cleared=${cleanup.cleared} failed=${cleanup.failedSessionIds.length}`);
@@ -15492,6 +15759,15 @@ class PiSubagentRunner {
15492
15759
  if (settled)
15493
15760
  return;
15494
15761
  settled = true;
15762
+ if (accountingMessages.length > 0 && /historian|dreamer|classify/i.test(options.agent)) {
15763
+ const last = [...accountingMessages].reverse().find((message) => message !== null && typeof message === "object" && message.role === "assistant");
15764
+ const responseChars = result.ok ? result.assistantText.length : last?.content?.filter((part) => typeof part === "object" && part !== null && part.type === "text" && typeof part.text === "string").reduce((sum, part) => sum + part.text.length, 0) ?? 0;
15765
+ const tokenDetails = formatRunTokenLog(runTokenLog(last?.usage, options.maxOutputTokens, last?.stopReason));
15766
+ if (!result.ok && result.reason === "truncated") {
15767
+ result = { ...result, error: `${result.error}; ${tokenDetails}` };
15768
+ }
15769
+ sessionLog(options.accountingSessionId ?? "subagent", `${options.agent} response_chars=${responseChars} ${tokenDetails}`);
15770
+ }
15495
15771
  const outcome = budget ? {
15496
15772
  ...result,
15497
15773
  meta: { ...result.meta, tokenBudget: budget.snapshot() }
@@ -20545,10 +20821,10 @@ function advanceToolReclaimWatermarkToCurrentMax(db, sessionId) {
20545
20821
  }
20546
20822
 
20547
20823
  // ../plugin/src/shared/tag-transcript.ts
20548
- import { createHash as createHash11 } from "node:crypto";
20824
+ import { createHash as createHash12 } from "node:crypto";
20549
20825
  var TEXT_TAG_IDENTITY_MARKER = ":mc-text-v1:";
20550
20826
  function textIdentityDigest(value) {
20551
- return createHash11("sha256").update(value).digest("hex");
20827
+ return createHash12("sha256").update(value).digest("hex");
20552
20828
  }
20553
20829
  function buildContentDerivedTextIds(messageId, parts) {
20554
20830
  const sources = parts.filter((part) => part.kind === "text").map((part) => stripTagPrefix(part.getText() ?? ""));
@@ -23812,8 +24088,10 @@ async function runHistorianSubagentWithTransientRetriesGuarded(args) {
23812
24088
  try {
23813
24089
  const key = piModelRefToCanonical(args.options.model ?? "");
23814
24090
  const window = args.resolveContextLimit?.(key) ?? resolveKnownHistorianContextLimit(key);
23815
- const reserve = historianProducerReserve(window, args.options.maxOutputTokens, args.resolveOutputLimit?.(key));
23816
- if (window !== undefined && producerInputTokenLimit(window, reserve) === undefined && !loggedInconsistentWindows.has(key)) {
24091
+ const limits = args.resolveContextLimit ? { context: window, input: undefined } : resolveHistorianProducerLimits(key);
24092
+ const context = limits.context ?? (limits.input === undefined ? window : undefined);
24093
+ const reserve = historianProducerReserve(context, args.options.maxOutputTokens, args.resolveOutputLimit?.(key));
24094
+ if (window !== undefined && producerInputTokenLimit(context, reserve, limits.input) === undefined && !loggedInconsistentWindows.has(key)) {
23817
24095
  loggedInconsistentWindows.add(key);
23818
24096
  sessionLog(args.sessionId, `producer window inconsistent for ${key}: window=${window} reserve=${reserve}; sending unguarded`);
23819
24097
  }
@@ -23822,7 +24100,8 @@ async function runHistorianSubagentWithTransientRetriesGuarded(args) {
23822
24100
  systemLocal: estimateTokens(args.options.systemPrompt),
23823
24101
  toolsLocal: 0,
23824
24102
  modelKey: key,
23825
- contextLimitTokens: window,
24103
+ contextLimitTokens: context,
24104
+ inputLimitTokens: limits.input,
23826
24105
  maxOutputTokens: reserve
23827
24106
  });
23828
24107
  if (failure)
@@ -27559,162 +27838,6 @@ function registerPiGuardedContext(pi, handler) {
27559
27838
  });
27560
27839
  }
27561
27840
 
27562
- // ../plugin/src/hooks/magic-context/lkg-persist.ts
27563
- function parseStringArray(value) {
27564
- if (typeof value !== "string")
27565
- return null;
27566
- let parsed;
27567
- try {
27568
- parsed = JSON.parse(value);
27569
- } catch {
27570
- return null;
27571
- }
27572
- if (!Array.isArray(parsed))
27573
- return null;
27574
- const result = [];
27575
- for (const entry of parsed) {
27576
- if (typeof entry !== "string" || entry.length === 0)
27577
- return null;
27578
- result.push(entry);
27579
- }
27580
- return result;
27581
- }
27582
- function parseNullableString(value) {
27583
- if (value === null || value === undefined)
27584
- return null;
27585
- return typeof value === "string" ? value : undefined;
27586
- }
27587
- function parseNullableInteger(value) {
27588
- if (value === null || value === undefined)
27589
- return;
27590
- return typeof value === "number" && Number.isSafeInteger(value) ? value : undefined;
27591
- }
27592
- function parsePersistedLkgSlot(row) {
27593
- if (!row || typeof row !== "object")
27594
- return;
27595
- const record = row;
27596
- const jsonPrefix = record.json_prefix;
27597
- const lastInputMessageId = record.last_input_message_id;
27598
- const capturedAt = record.captured_at;
27599
- let inputIdsRaw = record.input_id_seq;
27600
- let piOutputEntryIds;
27601
- if (typeof inputIdsRaw === "string") {
27602
- try {
27603
- const metadata = JSON.parse(inputIdsRaw);
27604
- if (metadata && !Array.isArray(metadata) && metadata.version === 1 && Array.isArray(metadata.piOutputEntryIds)) {
27605
- if (!metadata.piOutputEntryIds.every((id) => id === null || typeof id === "string" && id.length > 0))
27606
- return;
27607
- piOutputEntryIds = metadata.piOutputEntryIds;
27608
- inputIdsRaw = JSON.stringify(metadata.inputIds);
27609
- }
27610
- } catch {
27611
- return;
27612
- }
27613
- }
27614
- const inputIdSeq = parseStringArray(inputIdsRaw);
27615
- const inputContentDigests = parseStringArray(record.input_content_digests);
27616
- const modelKey = parseNullableString(record.model_key);
27617
- const providerKey = parseNullableString(record.provider_key);
27618
- if (typeof jsonPrefix !== "string" || typeof lastInputMessageId !== "string" || lastInputMessageId.length === 0 || typeof capturedAt !== "number" || !Number.isFinite(capturedAt) || inputIdSeq === null || inputContentDigests === null || inputContentDigests.length !== inputIdSeq.length || modelKey === undefined || providerKey === undefined) {
27619
- return;
27620
- }
27621
- if (piOutputEntryIds) {
27622
- try {
27623
- const output = JSON.parse(jsonPrefix);
27624
- const inputs = new Set(inputIdSeq);
27625
- if (!Array.isArray(output) || output.length !== piOutputEntryIds.length || piOutputEntryIds.some((id) => id !== null && !inputs.has(id)))
27626
- return;
27627
- } catch {
27628
- return;
27629
- }
27630
- }
27631
- let inputContentSignatures;
27632
- if (record.input_content_signatures !== null && record.input_content_signatures !== undefined) {
27633
- const parsed = parseStringArray(record.input_content_signatures);
27634
- if (parsed === null || parsed.length !== inputIdSeq.length)
27635
- return;
27636
- inputContentSignatures = parsed;
27637
- }
27638
- const slot = {
27639
- jsonPrefix,
27640
- inputIdSeq,
27641
- inputContentDigests,
27642
- lastInputMessageId,
27643
- modelKey,
27644
- providerKey,
27645
- capturedAt
27646
- };
27647
- if (inputContentSignatures)
27648
- slot.inputContentSignatures = inputContentSignatures;
27649
- if (piOutputEntryIds)
27650
- slot.piOutputEntryIds = piOutputEntryIds;
27651
- const rowVersion = parseNullableInteger(record.row_version);
27652
- if (rowVersion !== undefined)
27653
- slot.rowVersion = rowVersion;
27654
- const captureSequence = parseNullableInteger(record.capture_sequence);
27655
- if (captureSequence !== undefined)
27656
- slot.captureSequence = captureSequence;
27657
- return slot;
27658
- }
27659
- function saveLkgSlotToDb(db, sessionId, slot) {
27660
- try {
27661
- db.prepare(`INSERT INTO lkg_slots (
27662
- session_id, json_prefix, input_id_seq, input_content_digests,
27663
- input_content_signatures, last_input_message_id, model_key, provider_key,
27664
- captured_at, row_version, capture_sequence
27665
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
27666
- ON CONFLICT(session_id) DO UPDATE SET
27667
- json_prefix = excluded.json_prefix,
27668
- input_id_seq = excluded.input_id_seq,
27669
- input_content_digests = excluded.input_content_digests,
27670
- input_content_signatures = excluded.input_content_signatures,
27671
- last_input_message_id = excluded.last_input_message_id,
27672
- model_key = excluded.model_key,
27673
- provider_key = excluded.provider_key,
27674
- captured_at = excluded.captured_at,
27675
- row_version = excluded.row_version,
27676
- capture_sequence = excluded.capture_sequence`).run(sessionId, slot.jsonPrefix, JSON.stringify(slot.piOutputEntryIds ? {
27677
- version: 1,
27678
- inputIds: slot.inputIdSeq,
27679
- piOutputEntryIds: slot.piOutputEntryIds
27680
- } : slot.inputIdSeq), JSON.stringify(slot.inputContentDigests), slot.inputContentSignatures ? JSON.stringify(slot.inputContentSignatures) : null, slot.lastInputMessageId, slot.modelKey, slot.providerKey, slot.capturedAt, slot.rowVersion ?? null, slot.captureSequence ?? null);
27681
- return true;
27682
- } catch (error) {
27683
- sessionLog(sessionId, "LKG snapshot persistence failed (in-memory slot retained):", error);
27684
- return false;
27685
- }
27686
- }
27687
- function clearPersistedLkgSlot(db, sessionId) {
27688
- try {
27689
- db.prepare("DELETE FROM lkg_slots WHERE session_id = ?").run(sessionId);
27690
- } catch (error) {
27691
- sessionLog(sessionId, "LKG snapshot durable clear failed:", error);
27692
- }
27693
- }
27694
- function loadPersistedLkgSlot(db, sessionId) {
27695
- let row;
27696
- try {
27697
- row = db.prepare("SELECT * FROM lkg_slots WHERE session_id = ?").get(sessionId);
27698
- } catch (error) {
27699
- sessionLog(sessionId, "LKG snapshot durable load failed:", error);
27700
- return;
27701
- }
27702
- if (!row)
27703
- return;
27704
- const slot = parsePersistedLkgSlot(row);
27705
- if (!slot) {
27706
- clearPersistedLkgSlot(db, sessionId);
27707
- return;
27708
- }
27709
- return slot;
27710
- }
27711
- function createDbLkgPersistence(db) {
27712
- return {
27713
- load: (sessionId) => loadPersistedLkgSlot(db, sessionId),
27714
- clear: (sessionId) => clearPersistedLkgSlot(db, sessionId)
27715
- };
27716
- }
27717
-
27718
27841
  // ../plugin/src/hooks/magic-context/openai-compat-adjacency.ts
27719
27842
  function assertOpenAiCompatAdjacency(messages) {
27720
27843
  const violations = [];
@@ -29350,7 +29473,7 @@ function replayStrippedInlineThinkingPi(args) {
29350
29473
  }
29351
29474
 
29352
29475
  // src/served-array-ledger.ts
29353
- import { createHash as createHash12 } from "node:crypto";
29476
+ import { createHash as createHash13 } from "node:crypto";
29354
29477
  import * as fs2 from "node:fs";
29355
29478
  import * as path6 from "node:path";
29356
29479
  var PI_SERVED_ARRAY_TAIL_MESSAGES = 40;
@@ -29365,7 +29488,7 @@ var flushTimer = null;
29365
29488
  var swallowedWriteCount = 0;
29366
29489
  var lastWriteError = null;
29367
29490
  function sha256(value) {
29368
- return createHash12("sha256").update(value).digest("hex");
29491
+ return createHash13("sha256").update(value).digest("hex");
29369
29492
  }
29370
29493
  function safeSessionFileStem(sessionId) {
29371
29494
  const readable = sessionId.replace(/[^A-Za-z0-9._-]/g, "_").slice(0, 96);
@@ -29673,7 +29796,7 @@ function stripPiProcessedImages(args) {
29673
29796
  }
29674
29797
 
29675
29798
  // src/system-prompt.ts
29676
- import { createHash as createHash13 } from "node:crypto";
29799
+ import { createHash as createHash14 } from "node:crypto";
29677
29800
  var MAGIC_CONTEXT_MARKER = "## Magic Context";
29678
29801
  var stickyDateBySession = new Map;
29679
29802
  function buildMagicContextBlock(opts) {
@@ -29704,7 +29827,7 @@ function processSystemPromptForCache(args) {
29704
29827
  const liveDate = dateMatch ? dateMatch[0] : null;
29705
29828
  const stickyDate = stickyDateBySession.get(sessionId);
29706
29829
  const stableCandidate = liveDate && stickyDate && liveDate !== stickyDate ? systemPrompt.replace(DATE_PATTERN, stickyDate) : systemPrompt;
29707
- const stableCandidateHash = createHash13("md5").update(promptSurfaceHashMaterial(stableCandidate, args.promptSurfacePreset)).digest("hex");
29830
+ const stableCandidateHash = createHash14("md5").update(promptSurfaceHashMaterial(stableCandidate, args.promptSurfacePreset)).digest("hex");
29708
29831
  const contentOrPresetChanged = !isFirstHash && stableCandidateHash !== previousHash;
29709
29832
  const dateMayAdvance = isCacheBusting || contentOrPresetChanged;
29710
29833
  if (liveDate && !stickyDate) {
@@ -29718,7 +29841,7 @@ function processSystemPromptForCache(args) {
29718
29841
  sessionLog(sessionId, `system prompt date frozen: real=${liveDate}, using=${stickyDate} (cache-stable pass)`);
29719
29842
  }
29720
29843
  }
29721
- const currentHash = createHash13("md5").update(promptSurfaceHashMaterial(frozenPrompt, args.promptSurfacePreset)).digest("hex");
29844
+ const currentHash = createHash14("md5").update(promptSurfaceHashMaterial(frozenPrompt, args.promptSurfacePreset)).digest("hex");
29722
29845
  const hashChanged = !isFirstHash && currentHash !== previousHash;
29723
29846
  if (hashChanged) {
29724
29847
  sessionLog(sessionId, `system prompt hash changed: ${previousHash} → ${currentHash} (len=${frozenPrompt.length})`);
@@ -35816,7 +35939,7 @@ function buildStatusView(source, options) {
35816
35939
  // package.json
35817
35940
  var package_default = {
35818
35941
  name: "@cortexkit/pi-magic-context",
35819
- version: "0.44.2",
35942
+ version: "0.44.3",
35820
35943
  type: "module",
35821
35944
  description: "Pi and OMP coding agent extension for Magic Context — cross-session memory and context management",
35822
35945
  main: "dist/index.js",
@@ -35864,7 +35987,6 @@ var package_default = {
35864
35987
  "comment-json": "^5.0.0",
35865
35988
  "onnxruntime-web": "1.26.0-dev.20260416-b7804b056c",
35866
35989
  "quickjs-emscripten": "^0.32.0",
35867
- typebox: "^1.3.1",
35868
35990
  zod: "^4.1.8"
35869
35991
  },
35870
35992
  optionalDependencies: {
@@ -35884,11 +36006,13 @@ var package_default = {
35884
36006
  "pi-ai-086": "npm:@earendil-works/pi-ai@0.86.0",
35885
36007
  "pi-coding-agent-086": "npm:@earendil-works/pi-coding-agent@0.86.0",
35886
36008
  "pi-coding-agent-087": "npm:@earendil-works/pi-coding-agent@0.87.1",
36009
+ typebox: "^1.3.1",
35887
36010
  typescript: "^5.8.0"
35888
36011
  },
35889
36012
  peerDependencies: {
35890
- "@earendil-works/pi-coding-agent": ">=0.80.2",
35891
- "@earendil-works/pi-tui": ">=0.80.2"
36013
+ "@earendil-works/pi-coding-agent": "*",
36014
+ "@earendil-works/pi-tui": "*",
36015
+ typebox: "*"
35892
36016
  },
35893
36017
  peerDependenciesMeta: {
35894
36018
  "@earendil-works/pi-coding-agent": {
@@ -35896,6 +36020,9 @@ var package_default = {
35896
36020
  },
35897
36021
  "@earendil-works/pi-tui": {
35898
36022
  optional: true
36023
+ },
36024
+ typebox: {
36025
+ optional: true
35899
36026
  }
35900
36027
  },
35901
36028
  exports: {
@@ -37557,6 +37684,7 @@ async function src_default(pi) {
37557
37684
  const bootProjectDir = process.cwd();
37558
37685
  ensureConfigLocationsMigrated(bootProjectDir);
37559
37686
  const bootConfig = loadPiConfig({ cwd: bootProjectDir });
37687
+ setHomeProjectPermission(bootConfig.config.allow_home_project);
37560
37688
  if (!bootConfig.config.enabled) {
37561
37689
  info("plugin DISABLED via config (enabled: false) — skipping registration");
37562
37690
  return;
@@ -23,7 +23,7 @@ import {
23
23
  getRawSessionTagKeysThrough2,
24
24
  getLegacyProtectedTailStartOrdinal2,
25
25
  readSessionChunk2
26
- } from "./index-gsff513e.js";
26
+ } from "./index-tckvezry.js";
27
27
  import"./index-59dmj4zs.js";
28
28
  export {
29
29
  cleanUserText2 as cleanUserText,
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  TOKEN_BUDGET_TOOL_REFUSAL
3
- } from "./index-gsff513e.js";
3
+ } from "./index-tckvezry.js";
4
4
  import {
5
5
  setHarness,
6
6
  log
@@ -13,7 +13,7 @@ import {
13
13
  ensureProjectRegisteredFromPiDirectory,
14
14
  resolvePiHarnessKind,
15
15
  registerMagicContextTools
16
- } from "./index-esnhck8v.js";
16
+ } from "./index-atb1ecv9.js";
17
17
  import"./index-97e5fzy3.js";
18
18
 
19
19
  // src/subagent-entry.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-magic-context",
3
- "version": "0.44.2",
3
+ "version": "0.44.3",
4
4
  "type": "module",
5
5
  "description": "Pi and OMP coding agent extension for Magic Context — cross-session memory and context management",
6
6
  "main": "dist/index.js",
@@ -48,7 +48,6 @@
48
48
  "comment-json": "^5.0.0",
49
49
  "onnxruntime-web": "1.26.0-dev.20260416-b7804b056c",
50
50
  "quickjs-emscripten": "^0.32.0",
51
- "typebox": "^1.3.1",
52
51
  "zod": "^4.1.8"
53
52
  },
54
53
  "optionalDependencies": {
@@ -68,11 +67,13 @@
68
67
  "pi-ai-086": "npm:@earendil-works/pi-ai@0.86.0",
69
68
  "pi-coding-agent-086": "npm:@earendil-works/pi-coding-agent@0.86.0",
70
69
  "pi-coding-agent-087": "npm:@earendil-works/pi-coding-agent@0.87.1",
70
+ "typebox": "^1.3.1",
71
71
  "typescript": "^5.8.0"
72
72
  },
73
73
  "peerDependencies": {
74
- "@earendil-works/pi-coding-agent": ">=0.80.2",
75
- "@earendil-works/pi-tui": ">=0.80.2"
74
+ "@earendil-works/pi-coding-agent": "*",
75
+ "@earendil-works/pi-tui": "*",
76
+ "typebox": "*"
76
77
  },
77
78
  "peerDependenciesMeta": {
78
79
  "@earendil-works/pi-coding-agent": {
@@ -80,6 +81,9 @@
80
81
  },
81
82
  "@earendil-works/pi-tui": {
82
83
  "optional": true
84
+ },
85
+ "typebox": {
86
+ "optional": true
83
87
  }
84
88
  },
85
89
  "exports": {