@matthewfl/pi-contemplator 0.1.4 → 0.1.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matthewfl/pi-contemplator",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "A Pi extension that keeps long-running agentic sessions on track with background memory, contemplation, and structural review.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -229,9 +229,11 @@ export class Contemplator {
229
229
  private deliveredProbeIds = new Set<string>();
230
230
  /** Probe ids passed to pi.sendMessage by this live extension runtime. */
231
231
  private queuedProbeIds = new Set<string>();
232
+ /** Probes whose provider-context delivery will establish the next response-spacing anchor. */
233
+ private probeCooldownPendingIds = new Set<string>();
232
234
  private sessionGeneration = 0;
233
235
  private latestCtx: MemoryUpdateCtx | undefined;
234
- /** Completed primary-model responses since the previous contemplator run. */
236
+ /** Completed primary-model responses since the current completion/probe-delivery spacing anchor. */
235
237
  private turnsSinceRun = 0;
236
238
  /** Used to avoid counting the final turn_end after its assistant message_end. */
237
239
  private assistantResponsesInCurrentTurn = 0;
@@ -311,6 +313,7 @@ export class Contemplator {
311
313
  this.reviewerSessions.clear();
312
314
  this.deliveredProbeIds.clear();
313
315
  this.queuedProbeIds.clear();
316
+ this.probeCooldownPendingIds.clear();
314
317
  this.latestCtx = undefined;
315
318
  this.turnsSinceRun = 0;
316
319
  this.assistantResponsesInCurrentTurn = 0;
@@ -359,9 +362,12 @@ export class Contemplator {
359
362
  });
360
363
  this.pi.on("context", (event: any, ctx: ExtensionContext) => {
361
364
  const deliveredMessages = event.messages?.filter((message: any) => message?.role === "custom" && message.customType === CONTEMPLATOR_SUGGESTION && typeof message.details?.probeId === "string") ?? [];
365
+ let cooldownAnchored = false;
362
366
  for (const delivered of deliveredMessages) {
363
367
  if (this.deliveredProbeIds.has(delivered.details.probeId)) continue;
364
368
  this.deliveredProbeIds.add(delivered.details.probeId);
369
+ this.probeCooldownPendingIds.delete(delivered.details.probeId);
370
+ cooldownAnchored = true;
365
371
  // Once Pi includes the probe in a provider context it is no longer in
366
372
  // either in-memory delivery queue. Keeping this id indefinitely caused
367
373
  // later tree restores to suppress a genuinely needed requeue.
@@ -375,6 +381,12 @@ export class Contemplator {
375
381
  this.markTipPersisted(ctx);
376
382
  debugLog("contemplator.suggestion_delivered", { probeId: delivered.details.probeId });
377
383
  }
384
+ if (cooldownAnchored) {
385
+ // Probe spacing begins only once Pi proves the probe reached an actual
386
+ // provider context. Responses generated before this point do not count.
387
+ this.turnsSinceRun = 0;
388
+ this.withDebugContext(ctx, () => this.observeTurn(ctx));
389
+ }
378
390
  });
379
391
  this.pi.on("turn_end", (_event: any, ctx: ExtensionContext) => {
380
392
  this.persistAgentActivity(ctx);
@@ -437,6 +449,7 @@ export class Contemplator {
437
449
  let resetProjection: ReturnType<typeof fullProjection> | undefined;
438
450
  if (resetTracking) {
439
451
  this.deliveredProbeIds.clear();
452
+ this.probeCooldownPendingIds.clear();
440
453
  if (!retainQueuedIds) this.queuedProbeIds.clear();
441
454
  this.inFlightReviewIds.clear();
442
455
  this.resolvingReviewIds.clear();
@@ -545,6 +558,9 @@ export class Contemplator {
545
558
  }
546
559
  this.restoredTipId = tipId;
547
560
  for (const [probeId, question] of undeliveredSuggestions) {
561
+ // An undelivered durable probe remains the cooldown anchor even when Pi's
562
+ // live queue survived an extension reload and must not be duplicated.
563
+ this.probeCooldownPendingIds.add(probeId);
548
564
  // A durable custom_message proves only that Pi inserted the probe at some
549
565
  // point; it does not prove an in-memory queue still owns it, and compaction
550
566
  // may have removed it from active model context. Suppress requeue only for
@@ -620,7 +636,7 @@ export class Contemplator {
620
636
  };
621
637
  }
622
638
  if (!this.pending) {
623
- this.publishState(this.running ? "running" : "idle");
639
+ this.publishState(this.running ? "running" : this.probeCooldownPendingIds.size > 0 ? "probe" : "idle");
624
640
  return;
625
641
  }
626
642
  // Activity values are cumulative send-time snapshots, not values frozen when
@@ -630,6 +646,11 @@ export class Contemplator {
630
646
  this.pending.mainAgentToolCalls = assistantToolCallCount(branchEntries);
631
647
  this.pending.mainAgentActiveTimeMs = agentActiveTimeMs(branchEntries);
632
648
  const enoughMemories = this.pending.reviews.length > 0 || this.pending.observations.length >= this.runtime.config.contemplatorMinNewObservations || this.pending.summaries.length >= this.runtime.config.contemplatorMinNewSummaries;
649
+ if (this.probeCooldownPendingIds.size > 0) {
650
+ this.publishState("probe");
651
+ debugLog("contemplator.waiting", { reason: "probe_delivery", pendingProbeCount: this.probeCooldownPendingIds.size });
652
+ return;
653
+ }
633
654
  if (!enoughMemories || this.turnsSinceRun < this.runtime.config.contemplatorMinTurns) {
634
655
  this.publishState(!enoughMemories ? "memories" : "responses");
635
656
  debugLog("contemplator.waiting", {
@@ -676,6 +697,7 @@ export class Contemplator {
676
697
  let failureMessage: string | undefined;
677
698
  let workerNotified = false;
678
699
  let promptPersisted = false;
700
+ let emittedProbeId: string | undefined;
679
701
  let workerWatchdog: ReturnType<typeof createWorkerStallWatchdog> | undefined;
680
702
  this.publishState("running", { lastStartedAt: startedAt, lastError: undefined });
681
703
  debugLog("contemplator.start", {
@@ -863,7 +885,7 @@ export class Contemplator {
863
885
  this.markTipPersisted(ctx);
864
886
  }
865
887
  }
866
- if (intervention?.kind === "probe" && sessionGeneration === this.sessionGeneration) this.queueProbe(ctx, intervention.question, "send_probe");
888
+ if (intervention?.kind === "probe" && sessionGeneration === this.sessionGeneration) emittedProbeId = this.queueProbe(ctx, intervention.question, "send_probe");
867
889
  if (intervention?.kind === "review" && this.runtime.config.reviewerEnabled && sessionGeneration === this.sessionGeneration) {
868
890
  const reviewerModel = await this.runtime.resolveModel({
869
891
  model: ctx.model,
@@ -917,13 +939,21 @@ export class Contemplator {
917
939
  if (flushEpoch !== this.flushEpoch) return;
918
940
  this.running = false;
919
941
  if (!failed) this.consecutiveFlushFailures = 0;
942
+ // Normal runs establish their spacing anchor at completion. A probe run
943
+ // instead anchors at provider-context delivery: if delivery already occurred
944
+ // during this run, retain responses counted since it; otherwise the pending
945
+ // probe gate blocks launches until the context event resets the counter.
946
+ if (emittedProbeId === undefined) this.turnsSinceRun = 0;
947
+ const waitingForProbe = this.probeCooldownPendingIds.size > 0;
920
948
  const pendingHasEnoughMemories = this.pending !== undefined && (
921
949
  this.pending.reviews.length > 0 ||
922
950
  this.pending.observations.length >= this.runtime.config.contemplatorMinNewObservations ||
923
951
  this.pending.summaries.length >= this.runtime.config.contemplatorMinNewSummaries
924
952
  );
925
- const waitingFor = !this.pending
926
- ? "idle"
953
+ const waitingFor = waitingForProbe
954
+ ? "probe"
955
+ : !this.pending
956
+ ? "idle"
927
957
  : !pendingHasEnoughMemories
928
958
  ? "memories"
929
959
  : this.turnsSinceRun < this.runtime.config.contemplatorMinTurns
@@ -948,7 +978,7 @@ export class Contemplator {
948
978
  }
949
979
  }
950
980
 
951
- private queueProbe(ctx: MemoryUpdateCtx, question: string, source: "send_probe" | "restore", existingProbeId?: string): void {
981
+ private queueProbe(ctx: MemoryUpdateCtx, question: string, source: "send_probe" | "restore", existingProbeId?: string): string {
952
982
  const probeId = existingProbeId ?? `${Date.now().toString(36)}-${Math.random().toString(16).slice(2, 8)}`;
953
983
  // Persist intent before touching Pi's in-memory queue. A crash in between
954
984
  // leaves a recoverable pending probe rather than an invisible lost one.
@@ -967,6 +997,7 @@ export class Contemplator {
967
997
  // unrelated observer update or compaction callback cannot restore and enqueue
968
998
  // a duplicate while the original idle steer is still pending.
969
999
  this.queuedProbeIds.add(probeId);
1000
+ this.probeCooldownPendingIds.add(probeId);
970
1001
  this.pi.sendMessage({
971
1002
  customType: CONTEMPLATOR_SUGGESTION,
972
1003
  content: `Background contemplator probe (advisory):\n${question}\n\nReferenced memories can be reviewed using the recall tool.`,
@@ -981,6 +1012,7 @@ export class Contemplator {
981
1012
  triggerTurn: "omitted",
982
1013
  source,
983
1014
  });
1015
+ return probeId;
984
1016
  }
985
1017
 
986
1018
  private queueStructuralReview(options: QueueStructuralReviewOptions): void {
@@ -65,6 +65,8 @@ function liveStateLine(state: ContemplatorRunState): string {
65
65
  if (state.running) return `LIVE · running for ${Math.max(0, Math.floor((Date.now() - (state.lastStartedAt ?? Date.now())) / 60_000))}m · ${pending}\n${timing}${error}`;
66
66
  const reason = state.waitingFor === "observer"
67
67
  ? "waiting for observer backlog"
68
+ : state.waitingFor === "probe"
69
+ ? "waiting for queued probe delivery"
68
70
  : state.waitingFor === "memories"
69
71
  ? "waiting for memory threshold"
70
72
  : state.waitingFor === "responses"
@@ -76,7 +78,7 @@ function liveStateLine(state: ContemplatorRunState): string {
76
78
  : state.waitingFor === "passive"
77
79
  ? "passive mode"
78
80
  : "idle";
79
- return `LIVE · ${reason} · ${pending} · ${state.responsesSinceRun} primary responses since last run\n${timing}${error}`;
81
+ return `LIVE · ${reason} · ${pending} · ${state.responsesSinceRun} primary responses since cooldown anchor\n${timing}${error}`;
80
82
  }
81
83
 
82
84
  export function renderContemplator(entries: Entry[], state?: ContemplatorRunState): string {
@@ -43,6 +43,7 @@ function formatRunAge(timestamp: number): string {
43
43
  function contemplatorWaitingLabel(waitingFor: Runtime["contemplatorState"]["waitingFor"]): string {
44
44
  switch (waitingFor) {
45
45
  case "observer": return "waiting for observer backlog";
46
+ case "probe": return "waiting for queued probe delivery";
46
47
  case "memories": return "waiting for memory threshold";
47
48
  case "responses": return "waiting for response spacing";
48
49
  case "ready": return "ready to launch";
package/src/config.ts CHANGED
@@ -61,7 +61,7 @@ export interface Config {
61
61
  reviewerModel?: ConfiguredModel;
62
62
  contemplatorMinNewObservations: number;
63
63
  contemplatorMinNewSummaries: number;
64
- /** Minimum completed primary-model responses between contemplator runs. */
64
+ /** Minimum primary-model responses after contemplator completion, or after delivery of its probe, before the next run. */
65
65
  contemplatorMinTurns: number;
66
66
  /** Stateless loss-aware summarizer for the old memory pool. */
67
67
  summarizerEnabled: boolean;
package/src/runtime.ts CHANGED
@@ -82,9 +82,9 @@ export interface ContemplatorRunState {
82
82
  pendingObservations: number;
83
83
  pendingSummaries: number;
84
84
  pendingReviews: number;
85
- /** Completed primary-model responses since the previous contemplator run. */
85
+ /** Completed primary-model responses since the current completion/probe-delivery spacing anchor. */
86
86
  responsesSinceRun: number;
87
- waitingFor: "disabled" | "passive" | "observer" | "memories" | "responses" | "ready" | "running" | "idle";
87
+ waitingFor: "disabled" | "passive" | "observer" | "probe" | "memories" | "responses" | "ready" | "running" | "idle";
88
88
  lastStartedAt?: number;
89
89
  lastCompletedAt?: number;
90
90
  lastError?: string;