@cortexkit/pi-magic-context 0.22.2 → 0.22.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.
package/dist/index.js CHANGED
@@ -140942,14 +140942,48 @@ function readRawSessionMessagesFromDb(db, sessionId) {
140942
140942
  var encoder = new TextEncoder;
140943
140943
  var TAG_PREFIX_REGEX = /^(?:§\d+§\s*)+/;
140944
140944
  var MALFORMED_TAG_PREFIX_REGEX = /^(?:§\d+">§(?:\d+§)?\s*)+/;
140945
+ var COMPLETE_TAG_PAIR_GLOBAL_REGEX = /\u00a7\d+\u00a7/g;
140946
+ var MALFORMED_TAG_GLOBAL_REGEX = /\u00a7\d+">(?:\u00a7(?:\d+\u00a7)?)?/g;
140947
+ var STRAY_SECTION_CHAR_REGEX = /\u00a7/g;
140948
+ function stripWellFormedLeadingTagPrefix(value) {
140949
+ return value.replace(/^(\u00a7\d+\u00a7\s*)+/, "");
140950
+ }
140951
+ function stripCompleteTagPairsGlobally(value) {
140952
+ return value.replace(COMPLETE_TAG_PAIR_GLOBAL_REGEX, "");
140953
+ }
140954
+ function stripMalformedTagNotationGlobally(value) {
140955
+ return value.replace(MALFORMED_TAG_GLOBAL_REGEX, "");
140956
+ }
140957
+ function stripTagSectionCharacters(value) {
140958
+ return value.replace(STRAY_SECTION_CHAR_REGEX, "");
140959
+ }
140960
+ function stripPersistedAssistantText(value) {
140961
+ let text = stripWellFormedLeadingTagPrefix(value);
140962
+ text = stripCompleteTagPairsGlobally(text);
140963
+ text = stripMalformedTagNotationGlobally(text);
140964
+ text = stripTagSectionCharacters(text);
140965
+ return text.trim();
140966
+ }
140945
140967
  function byteSize(value) {
140946
140968
  return encoder.encode(value).length;
140947
140969
  }
140948
140970
  function stripTagPrefix(value) {
140949
- let stripped = value.replace(MALFORMED_TAG_PREFIX_REGEX, "");
140950
- stripped = stripped.replace(TAG_PREFIX_REGEX, "");
140971
+ let stripped = value;
140972
+ for (let pass = 0;pass < 8; pass++) {
140973
+ const prev = stripped;
140974
+ stripped = stripped.replace(MALFORMED_TAG_PREFIX_REGEX, "");
140975
+ stripped = stripped.replace(TAG_PREFIX_REGEX, "");
140976
+ if (stripped === prev)
140977
+ break;
140978
+ }
140951
140979
  return stripped;
140952
140980
  }
140981
+ function peelLeadingMcTagNotation(value) {
140982
+ const body = stripTagPrefix(value);
140983
+ if (body === value)
140984
+ return { tagPrefix: "", body };
140985
+ return { tagPrefix: value.slice(0, value.length - body.length), body };
140986
+ }
140953
140987
  function prependTag(tagId, value) {
140954
140988
  const stripped = stripTagPrefix(value);
140955
140989
  return `§${tagId}§ ${stripped}`;
@@ -140991,7 +141025,9 @@ function hasMeaningfulPart(part) {
140991
141025
  return false;
140992
141026
  const type = part.type;
140993
141027
  if (type === "text") {
140994
- return typeof part.text === "string" && part.text.trim().length > 0;
141028
+ if (typeof part.text !== "string")
141029
+ return false;
141030
+ return stripTagPrefix(part.text).trim().length > 0;
140995
141031
  }
140996
141032
  if (typeof type !== "string")
140997
141033
  return false;
@@ -150220,7 +150256,7 @@ async function processDreamQueue(args) {
150220
150256
  return null;
150221
150257
  }
150222
150258
  const projectDirectory = args.sessionDirectoryOverride ?? resolveDreamSessionDirectory(entry.projectIdentity);
150223
- log(`[dreamer] dequeued project ${entry.projectIdentity} (dir=${projectDirectory}), starting dream run`);
150259
+ log(`[dreamer] dequeued project ${entry.projectIdentity}, starting dream run`);
150224
150260
  let result;
150225
150261
  try {
150226
150262
  result = await runDream({
@@ -150344,6 +150380,7 @@ async function readGitCommits(directory, options = {}) {
150344
150380
  if (revision.startsWith("-")) {
150345
150381
  throw new Error(`readGitCommits: refusing revision that looks like an option: "${revision}"`);
150346
150382
  }
150383
+ const projectLabel = options.projectIdentity ?? "<project>";
150347
150384
  const args = [
150348
150385
  "log",
150349
150386
  revision,
@@ -150366,11 +150403,11 @@ async function readGitCommits(directory, options = {}) {
150366
150403
  stdout = result.stdout;
150367
150404
  } catch (error) {
150368
150405
  const message = error instanceof Error ? error.message : String(error);
150369
- log(`[git-commits] readGitCommits failed at cwd=${directory}: ${message.slice(0, 500)}`);
150406
+ log(`[git-commits] readGitCommits failed for ${projectLabel}: ${message.slice(0, 500)}`);
150370
150407
  return [];
150371
150408
  }
150372
150409
  if (stdout.trim().length === 0) {
150373
- log(`[git-commits] readGitCommits returned empty stdout at cwd=${directory} (sinceMs=${options.sinceMs ?? "none"} args=${args.slice(0, 4).join(" ")})`);
150410
+ log(`[git-commits] readGitCommits returned empty stdout for ${projectLabel} (sinceMs=${options.sinceMs ?? "none"} args=${args.slice(0, 4).join(" ")})`);
150374
150411
  }
150375
150412
  return parseGitLogOutput(stdout);
150376
150413
  }
@@ -166163,7 +166200,8 @@ async function indexCommitsForProject(db, projectPath, directory, options) {
166163
166200
  const sinceMs = latestIndexed !== null ? Math.max(latestIndexed - 60000, Date.now() - options.sinceDays * MS_PER_DAY) : Date.now() - options.sinceDays * MS_PER_DAY;
166164
166201
  const commits = await readGitCommits(directory, {
166165
166202
  sinceMs,
166166
- maxCommits: options.maxCommits
166203
+ maxCommits: options.maxCommits,
166204
+ projectIdentity: projectPath
166167
166205
  });
166168
166206
  result.scanned = commits.length;
166169
166207
  if (commits.length === 0) {
@@ -166430,7 +166468,7 @@ async function startDreamScheduleTimer(args) {
166430
166468
  const isNewRegistration = !registeredProjects.has(args.directory);
166431
166469
  registeredProjects.set(args.directory, args);
166432
166470
  if (isNewRegistration) {
166433
- log(`[dreamer] registered project ${args.directory} (dreaming=${dreamingEnabled} embeddings=${embeddingSweepEnabled} commits=${commitIndexingEnabled}; total=${registeredProjects.size})`);
166471
+ log(`[dreamer] registered project ${args.projectIdentity} (dreaming=${dreamingEnabled} embeddings=${embeddingSweepEnabled} commits=${commitIndexingEnabled}; total=${registeredProjects.size})`);
166434
166472
  }
166435
166473
  if (!activeTimer) {
166436
166474
  log(`[dreamer] started independent schedule timer (every ${DREAM_TIMER_INTERVAL_MS / 60000}m)`);
@@ -166445,7 +166483,7 @@ async function startDreamScheduleTimer(args) {
166445
166483
  }
166446
166484
  return () => {
166447
166485
  registeredProjects.delete(args.directory);
166448
- log(`[dreamer] unregistered project ${args.directory} (remaining=${registeredProjects.size})`);
166486
+ log(`[dreamer] unregistered project ${args.projectIdentity} (remaining=${registeredProjects.size})`);
166449
166487
  if (registeredProjects.size === 0 && activeTimer) {
166450
166488
  clearInterval(activeTimer);
166451
166489
  activeTimer = null;
@@ -166493,7 +166531,7 @@ async function sweepProject(reg, origin, db, gitCommitEnabled = getProjectEmbedd
166493
166531
  return;
166494
166532
  }
166495
166533
  try {
166496
- log(`[dreamer] timer tick (${origin}) ${reg.directory} — checking schedule window "${reg.dreamerConfig.schedule}"`);
166534
+ log(`[dreamer] timer tick (${origin}) ${reg.projectIdentity} — checking schedule window "${reg.dreamerConfig.schedule}"`);
166497
166535
  checkScheduleAndEnqueue(db, reg.dreamerConfig.schedule, reg.projectIdentity);
166498
166536
  await processDreamQueue({
166499
166537
  db,
@@ -166508,7 +166546,7 @@ async function sweepProject(reg, origin, db, gitCommitEnabled = getProjectEmbedd
166508
166546
  fallbackModels: resolveFallbackChain(DREAMER_AGENT, reg.dreamerConfig.fallback_models)
166509
166547
  });
166510
166548
  } catch (error51) {
166511
- log(`[dreamer] timer-triggered queue processing failed for ${reg.directory}:`, error51);
166549
+ log(`[dreamer] timer-triggered queue processing failed for ${reg.projectIdentity}:`, error51);
166512
166550
  }
166513
166551
  }
166514
166552
  function startGitSweepLeaseRenewal(db, projectIdentity, holderId) {
@@ -166530,12 +166568,12 @@ async function sweepGitCommits(args) {
166530
166568
  const lease2 = acquireGitSweepLease(db, projectIdentity, holderId);
166531
166569
  if (!lease2.acquired) {
166532
166570
  const reason = lease2.reason === "cooldown_active" ? `cooldown active until ${lease2.nextAllowedAt}` : `lease held by ${lease2.leaseHolder ?? "another holder"} until ${lease2.leaseExpiresAt ?? "unknown"}`;
166533
- log(`[git-commits] sweep skipped for ${projectIdentity} (${directory}): ${reason}`);
166571
+ log(`[git-commits] sweep skipped for ${projectIdentity}: ${reason}`);
166534
166572
  return;
166535
166573
  }
166536
166574
  const startedAt = Date.now();
166537
166575
  const stopRenewal = startGitSweepLeaseRenewal(db, projectIdentity, holderId);
166538
- log(`[git-commits] sweep starting for ${directory} (project=${projectIdentity} sinceDays=${gitCommitIndexing.since_days} maxCommits=${gitCommitIndexing.max_commits})`);
166576
+ log(`[git-commits] sweep starting for ${projectIdentity} (sinceDays=${gitCommitIndexing.since_days} maxCommits=${gitCommitIndexing.max_commits})`);
166539
166577
  try {
166540
166578
  const result = await indexCommitsForProject(db, projectIdentity, directory, {
166541
166579
  sinceDays: gitCommitIndexing.since_days,
@@ -166555,7 +166593,7 @@ async function sweepGitCommits(args) {
166555
166593
  } catch (error51) {
166556
166594
  releaseGitSweepLease(db, projectIdentity, holderId);
166557
166595
  const elapsedMs = Date.now() - startedAt;
166558
- log(`[git-commits] sweep failed for ${directory} after ${elapsedMs}ms: ${error51 instanceof Error ? error51.message : String(error51)}`);
166596
+ log(`[git-commits] sweep failed for ${projectIdentity} after ${elapsedMs}ms: ${error51 instanceof Error ? error51.message : String(error51)}`);
166559
166597
  } finally {
166560
166598
  stopRenewal();
166561
166599
  }
@@ -176017,10 +176055,8 @@ function injectPiTemporalMarkers(messages) {
176017
176055
  if (prefix !== null) {
176018
176056
  const userMsg = msg;
176019
176057
  if (typeof userMsg.content === "string") {
176020
- if (!TEMPORAL_MARKER_PATTERN.test(stripTagPrefix2(userMsg.content))) {
176021
- const tagMatch = userMsg.content.match(/^(?:§\d+§\s*)+/);
176022
- const tagPrefix = tagMatch ? tagMatch[0] : "";
176023
- const body = userMsg.content.slice(tagPrefix.length);
176058
+ if (!TEMPORAL_MARKER_PATTERN.test(stripTagPrefix(userMsg.content))) {
176059
+ const { tagPrefix, body } = peelLeadingMcTagNotation(userMsg.content);
176024
176060
  messages[i] = {
176025
176061
  ...userMsg,
176026
176062
  content: tagPrefix + prefix + body
@@ -176031,9 +176067,7 @@ function injectPiTemporalMarkers(messages) {
176031
176067
  const firstTextIndex = userMsg.content.findIndex((p) => p && typeof p === "object" && p.type === "text");
176032
176068
  if (firstTextIndex >= 0) {
176033
176069
  const existing = userMsg.content[firstTextIndex];
176034
- const tagMatch = existing.text.match(/^(?:§\d+§\s*)+/);
176035
- const tagPrefix = tagMatch ? tagMatch[0] : "";
176036
- const body = existing.text.slice(tagPrefix.length);
176070
+ const { tagPrefix, body } = peelLeadingMcTagNotation(existing.text);
176037
176071
  if (!TEMPORAL_MARKER_PATTERN.test(body)) {
176038
176072
  const newContent = userMsg.content.slice();
176039
176073
  newContent[firstTextIndex] = {
@@ -176056,12 +176090,6 @@ function injectPiTemporalMarkers(messages) {
176056
176090
  }
176057
176091
  return injected;
176058
176092
  }
176059
- function stripTagPrefix2(text) {
176060
- const match = text.match(/^(?:§\d+§\s*)+/);
176061
- if (!match)
176062
- return text;
176063
- return text.slice(match[0].length);
176064
- }
176065
176093
 
176066
176094
  // src/timeout.ts
176067
176095
  function withTimeout(p, ms) {
@@ -180592,7 +180620,7 @@ function formatThresholdPercent(value) {
180592
180620
  // package.json
180593
180621
  var package_default = {
180594
180622
  name: "@cortexkit/pi-magic-context",
180595
- version: "0.22.2",
180623
+ version: "0.22.3",
180596
180624
  type: "module",
180597
180625
  description: "Pi coding agent extension for Magic Context — cross-session memory and context management",
180598
180626
  main: "dist/index.js",
@@ -181177,8 +181205,6 @@ function computePiPressure(usage, contextLimit) {
181177
181205
  }
181178
181206
 
181179
181207
  // src/strip-tag-prefix.ts
181180
- var LEADING_TAG_PREFIX_REGEX = /^(\u00a7\d+\u00a7\s*)+/;
181181
- var SECTION_CHAR_REGEX = /\u00a7/g;
181182
181208
  function stripTagPrefixFromAssistantMessage(message) {
181183
181209
  if (message.role !== "assistant")
181184
181210
  return false;
@@ -181192,7 +181218,7 @@ function stripTagPrefixFromAssistantMessage(message) {
181192
181218
  const textPart = part;
181193
181219
  if (typeof textPart.text !== "string")
181194
181220
  continue;
181195
- const stripped = textPart.text.replace(LEADING_TAG_PREFIX_REGEX, "").replace(SECTION_CHAR_REGEX, "");
181221
+ const stripped = stripPersistedAssistantText(textPart.text);
181196
181222
  if (stripped !== textPart.text) {
181197
181223
  textPart.text = stripped;
181198
181224
  mutated = true;
@@ -186357,7 +186383,7 @@ async function persistPiPressureFromMessageEnd(args) {
186357
186383
  updates.cacheAlertSent = true;
186358
186384
  const safeTokens = Math.max(observedSafeInputTokens, pressure.inputTokens);
186359
186385
  const modelLabel = provider2 && model ? `${provider2}/${model}` : "the active model";
186360
- await args.notifyIssue?.(`⚠️ Magic Context: Pi reports a context limit of ${formatTokens(contextLimit)} tokens for ${modelLabel} but you've successfully sent ${formatTokens(safeTokens)} tokens in this session — the cached limit looks wrong. Restart Pi if you suspect this is incorrect.`);
186386
+ await args.notifyIssue?.(`⚠️ Magic Context: Pi reports a context limit of ${formatTokens(contextLimit)} tokens for ${modelLabel} but you've successfully sent ${formatTokens(safeTokens)} tokens in this session — the reported limit looks wrong. Restart Pi if you suspect this is incorrect.`);
186361
186387
  }
186362
186388
  }
186363
186389
  updates.lastContextPercentage = percentage;
@@ -159835,14 +159835,48 @@ function readRawSessionMessagesFromDb(db, sessionId) {
159835
159835
  var encoder = new TextEncoder;
159836
159836
  var TAG_PREFIX_REGEX = /^(?:§\d+§\s*)+/;
159837
159837
  var MALFORMED_TAG_PREFIX_REGEX = /^(?:§\d+">§(?:\d+§)?\s*)+/;
159838
+ var COMPLETE_TAG_PAIR_GLOBAL_REGEX = /\u00a7\d+\u00a7/g;
159839
+ var MALFORMED_TAG_GLOBAL_REGEX = /\u00a7\d+">(?:\u00a7(?:\d+\u00a7)?)?/g;
159840
+ var STRAY_SECTION_CHAR_REGEX = /\u00a7/g;
159841
+ function stripWellFormedLeadingTagPrefix(value) {
159842
+ return value.replace(/^(\u00a7\d+\u00a7\s*)+/, "");
159843
+ }
159844
+ function stripCompleteTagPairsGlobally(value) {
159845
+ return value.replace(COMPLETE_TAG_PAIR_GLOBAL_REGEX, "");
159846
+ }
159847
+ function stripMalformedTagNotationGlobally(value) {
159848
+ return value.replace(MALFORMED_TAG_GLOBAL_REGEX, "");
159849
+ }
159850
+ function stripTagSectionCharacters(value) {
159851
+ return value.replace(STRAY_SECTION_CHAR_REGEX, "");
159852
+ }
159853
+ function stripPersistedAssistantText(value) {
159854
+ let text = stripWellFormedLeadingTagPrefix(value);
159855
+ text = stripCompleteTagPairsGlobally(text);
159856
+ text = stripMalformedTagNotationGlobally(text);
159857
+ text = stripTagSectionCharacters(text);
159858
+ return text.trim();
159859
+ }
159838
159860
  function byteSize(value) {
159839
159861
  return encoder.encode(value).length;
159840
159862
  }
159841
159863
  function stripTagPrefix(value) {
159842
- let stripped = value.replace(MALFORMED_TAG_PREFIX_REGEX, "");
159843
- stripped = stripped.replace(TAG_PREFIX_REGEX, "");
159864
+ let stripped = value;
159865
+ for (let pass = 0;pass < 8; pass++) {
159866
+ const prev = stripped;
159867
+ stripped = stripped.replace(MALFORMED_TAG_PREFIX_REGEX, "");
159868
+ stripped = stripped.replace(TAG_PREFIX_REGEX, "");
159869
+ if (stripped === prev)
159870
+ break;
159871
+ }
159844
159872
  return stripped;
159845
159873
  }
159874
+ function peelLeadingMcTagNotation(value) {
159875
+ const body = stripTagPrefix(value);
159876
+ if (body === value)
159877
+ return { tagPrefix: "", body };
159878
+ return { tagPrefix: value.slice(0, value.length - body.length), body };
159879
+ }
159846
159880
  function prependTag(tagId, value) {
159847
159881
  const stripped = stripTagPrefix(value);
159848
159882
  return `§${tagId}§ ${stripped}`;
@@ -159884,7 +159918,9 @@ function hasMeaningfulPart(part) {
159884
159918
  return false;
159885
159919
  const type = part.type;
159886
159920
  if (type === "text") {
159887
- return typeof part.text === "string" && part.text.trim().length > 0;
159921
+ if (typeof part.text !== "string")
159922
+ return false;
159923
+ return stripTagPrefix(part.text).trim().length > 0;
159888
159924
  }
159889
159925
  if (typeof type !== "string")
159890
159926
  return false;
@@ -167436,6 +167472,7 @@ async function readGitCommits(directory, options3 = {}) {
167436
167472
  if (revision.startsWith("-")) {
167437
167473
  throw new Error(`readGitCommits: refusing revision that looks like an option: "${revision}"`);
167438
167474
  }
167475
+ const projectLabel = options3.projectIdentity ?? "<project>";
167439
167476
  const args = [
167440
167477
  "log",
167441
167478
  revision,
@@ -167458,11 +167495,11 @@ async function readGitCommits(directory, options3 = {}) {
167458
167495
  stdout = result.stdout;
167459
167496
  } catch (error51) {
167460
167497
  const message = error51 instanceof Error ? error51.message : String(error51);
167461
- log(`[git-commits] readGitCommits failed at cwd=${directory}: ${message.slice(0, 500)}`);
167498
+ log(`[git-commits] readGitCommits failed for ${projectLabel}: ${message.slice(0, 500)}`);
167462
167499
  return [];
167463
167500
  }
167464
167501
  if (stdout.trim().length === 0) {
167465
- log(`[git-commits] readGitCommits returned empty stdout at cwd=${directory} (sinceMs=${options3.sinceMs ?? "none"} args=${args.slice(0, 4).join(" ")})`);
167502
+ log(`[git-commits] readGitCommits returned empty stdout for ${projectLabel} (sinceMs=${options3.sinceMs ?? "none"} args=${args.slice(0, 4).join(" ")})`);
167466
167503
  }
167467
167504
  return parseGitLogOutput(stdout);
167468
167505
  }
@@ -167646,7 +167683,8 @@ async function indexCommitsForProject(db, projectPath, directory, options3) {
167646
167683
  const sinceMs = latestIndexed !== null ? Math.max(latestIndexed - 60000, Date.now() - options3.sinceDays * MS_PER_DAY) : Date.now() - options3.sinceDays * MS_PER_DAY;
167647
167684
  const commits = await readGitCommits(directory, {
167648
167685
  sinceMs,
167649
- maxCommits: options3.maxCommits
167686
+ maxCommits: options3.maxCommits,
167687
+ projectIdentity: projectPath
167650
167688
  });
167651
167689
  result.scanned = commits.length;
167652
167690
  if (commits.length === 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-magic-context",
3
- "version": "0.22.2",
3
+ "version": "0.22.3",
4
4
  "type": "module",
5
5
  "description": "Pi coding agent extension for Magic Context — cross-session memory and context management",
6
6
  "main": "dist/index.js",