@clawtopia/werewolf-evolution 0.1.8 → 0.1.9

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.d.ts CHANGED
@@ -153,6 +153,7 @@ declare class EvolutionContextStore {
153
153
  capture(event: MessageReceivedLike, ctx: MessageContextLike, now?: number): CapturedInbound | undefined;
154
154
  getPending(runId?: string, sessionKey?: string, prompt?: string, now?: number): CapturedInbound | undefined;
155
155
  private storeInbound;
156
+ private removeInbound;
156
157
  bindAgentRun(event: AgentRunEventLike, ctx: AgentRunContextLike, binding: AgentAccountBinding | undefined, now?: number, explicitOwnerAuthorized?: boolean, pendingInbound?: CapturedInbound): BoundRun | undefined;
157
158
  getBound(runId?: string, sessionKey?: string, now?: number): BoundRun | undefined;
158
159
  getBoundForToolCall(runId: string | undefined, sessionKey: string | undefined, agentId: string | undefined, now?: number): BoundRun | undefined;
@@ -205,12 +206,12 @@ declare class WerewolfEvolutionService {
205
206
  private cleanupOrdinaryDirectMessages;
206
207
  private cleanupSystemDebriefSessions;
207
208
  private matchesSystemDebriefTarget;
208
- captureMessage(event: MessageReceivedLike, ctx: MessageContextLike): void;
209
+ captureMessage(event: MessageReceivedLike, ctx: MessageContextLike): CapturedInbound | undefined;
209
210
  isSystemDebriefMessage(event: MessageSendingLike, ctx: Pick<MessageContextLike, "channelId" | "accountId" | "sessionKey" | "conversationId">): boolean;
210
211
  private pendingOrdinaryDirectMessage;
211
212
  private consumeOrdinaryDirectMessage;
212
- bindAgentRun(event: AgentRunEventLike, ctx: AgentRunContextLike, binding: AgentAccountBinding | undefined, explicitOwnerAuthorized?: boolean, pendingInbound?: CapturedDebrief | CapturedSuggestionCommand, pendingOrdinary?: OrdinaryDirectMessage): void;
213
- bindPromptRun(event: PromptBuildEventLike, ctx: AgentRunContextLike, config: unknown): void;
213
+ bindAgentRun(event: AgentRunEventLike, ctx: AgentRunContextLike, binding: AgentAccountBinding | undefined, explicitOwnerAuthorized?: boolean, pendingInbound?: CapturedDebrief | CapturedSuggestionCommand, pendingOrdinary?: OrdinaryDirectMessage): BoundRun | undefined;
214
+ bindPromptRun(event: PromptBuildEventLike, ctx: AgentRunContextLike, config: unknown): BoundRun | undefined;
214
215
  finishRun(ctx: Pick<AgentRunContextLike, "runId" | "sessionKey">): void;
215
216
  buildPromptContext(ctx: PromptContextLike, hooks?: TransactionHooks): {
216
217
  prependContext: string;
package/dist/index.js CHANGED
@@ -436,13 +436,24 @@ var EvolutionContextStore = class {
436
436
  }
437
437
  storeInbound(inbound) {
438
438
  if (inbound.runId) {
439
+ const previous = this.inboundByRun.get(inbound.runId);
440
+ if (previous) this.removeInbound(previous);
439
441
  this.inboundByRun.set(inbound.runId, inbound);
440
- return;
441
442
  }
442
443
  const pending = this.inboundBySession.get(inbound.sessionKey) ?? [];
443
444
  pending.push(inbound);
444
445
  this.inboundBySession.set(inbound.sessionKey, pending);
445
446
  }
447
+ removeInbound(inbound) {
448
+ if (inbound.runId && this.inboundByRun.get(inbound.runId) === inbound) {
449
+ this.inboundByRun.delete(inbound.runId);
450
+ }
451
+ const pending = this.inboundBySession.get(inbound.sessionKey);
452
+ if (!pending) return;
453
+ const index = pending.indexOf(inbound);
454
+ if (index >= 0) pending.splice(index, 1);
455
+ if (pending.length === 0) this.inboundBySession.delete(inbound.sessionKey);
456
+ }
446
457
  bindAgentRun(event, ctx, binding, now = Date.now(), explicitOwnerAuthorized = false, pendingInbound) {
447
458
  this.cleanup(now);
448
459
  if (!ctx.runId || !ctx.sessionKey || !ctx.agentId || !ctx.workspaceDir) return void 0;
@@ -459,12 +470,7 @@ var EvolutionContextStore = class {
459
470
  } else {
460
471
  if (!explicitOwnerAuthorized || event.senderIsOwner !== true || !event.senderId || normalizedSender(event.senderId) !== normalizedSender(inbound.from)) return void 0;
461
472
  }
462
- if (runInbound) {
463
- this.inboundByRun.delete(ctx.runId);
464
- } else {
465
- sessionPending.splice(pendingIndex, 1);
466
- if (sessionPending.length === 0) this.inboundBySession.delete(ctx.sessionKey);
467
- }
473
+ this.removeInbound(inbound);
468
474
  const bound = {
469
475
  runId: ctx.runId,
470
476
  sessionKey: ctx.sessionKey,
@@ -542,7 +548,7 @@ var EvolutionContextStore = class {
542
548
  }
543
549
  cleanup(now = Date.now()) {
544
550
  for (const [key, value] of this.inboundByRun) {
545
- if (now - value.capturedAt > CAPTURE_TTL_MS) this.inboundByRun.delete(key);
551
+ if (now - value.capturedAt > CAPTURE_TTL_MS) this.removeInbound(value);
546
552
  }
547
553
  for (const [key, values] of this.inboundBySession) {
548
554
  const current = values.filter((value) => now - value.capturedAt <= CAPTURE_TTL_MS);
@@ -558,13 +564,21 @@ var EvolutionContextStore = class {
558
564
  for (const [key, value] of this.hashByDebriefId) {
559
565
  if (now - value.capturedAt > CAPTURE_TTL_MS) this.hashByDebriefId.delete(key);
560
566
  }
561
- while (this.inboundByRun.size > MAX_CAPTURES) this.inboundByRun.delete(this.inboundByRun.keys().next().value);
567
+ while (this.inboundByRun.size > MAX_CAPTURES) {
568
+ const first = this.inboundByRun.values().next().value;
569
+ if (!first) break;
570
+ this.removeInbound(first);
571
+ }
562
572
  while ([...this.inboundBySession.values()].reduce((total, values) => total + values.length, 0) > MAX_CAPTURES) {
563
573
  const firstKey = this.inboundBySession.keys().next().value;
564
574
  if (!firstKey) break;
565
575
  const values = this.inboundBySession.get(firstKey);
566
- values.shift();
567
- if (values.length === 0) this.inboundBySession.delete(firstKey);
576
+ const first = values[0];
577
+ if (!first) {
578
+ this.inboundBySession.delete(firstKey);
579
+ continue;
580
+ }
581
+ this.removeInbound(first);
568
582
  }
569
583
  while (this.boundByRun.size > MAX_CAPTURES) this.boundByRun.delete(this.boundByRun.keys().next().value);
570
584
  while (this.toolCalls.size > MAX_CAPTURES) this.toolCalls.delete(this.toolCalls.keys().next().value);
@@ -2517,6 +2531,7 @@ var WerewolfEvolutionService = class {
2517
2531
  }
2518
2532
  this.cleanupOrdinaryDirectMessages(now);
2519
2533
  }
2534
+ return captured;
2520
2535
  }
2521
2536
  isSystemDebriefMessage(event, ctx) {
2522
2537
  const now = Date.now();
@@ -2577,6 +2592,7 @@ var WerewolfEvolutionService = class {
2577
2592
  });
2578
2593
  this.consumeOrdinaryDirectMessage(ctx.runId, ordinary);
2579
2594
  }
2595
+ return bound;
2580
2596
  }
2581
2597
  bindPromptRun(event, ctx, config) {
2582
2598
  const pending = this.contexts.getPending(ctx.runId, ctx.sessionKey, event.prompt);
@@ -2598,7 +2614,7 @@ var WerewolfEvolutionService = class {
2598
2614
  workspaceAuthorized = false;
2599
2615
  }
2600
2616
  }
2601
- this.bindAgentRun(
2617
+ const bound = this.bindAgentRun(
2602
2618
  {
2603
2619
  channelId,
2604
2620
  accountId,
@@ -2611,6 +2627,7 @@ var WerewolfEvolutionService = class {
2611
2627
  pending,
2612
2628
  ordinary
2613
2629
  );
2630
+ return bound ?? this.contexts.getBound(ctx.runId, ctx.sessionKey);
2614
2631
  }
2615
2632
  finishRun(ctx) {
2616
2633
  if (!ctx.runId) return;
@@ -3036,10 +3053,20 @@ function registerWerewolfEvolution(api, service = getSharedWerewolfEvolutionServ
3036
3053
  api.registerTool((ctx) => createDebriefCommitTool(service, ctx), { name: "werewolf_debrief_commit" });
3037
3054
  api.registerTool((ctx) => createSuggestionDecideTool(service, ctx), { name: "werewolf_suggestion_decide" });
3038
3055
  api.on("message_received", (event, ctx) => {
3039
- service.captureMessage(event, ctx);
3056
+ const captured = service.captureMessage(event, ctx);
3057
+ if (!captured && event.content.includes(DEBRIEF_HEADER)) {
3058
+ api.logger.warn(
3059
+ `[werewolf-evolution] DEBRIEF_CAPTURE_REJECTED runId=${event.runId ?? ctx.runId ?? "unknown"} sessionKey=${event.sessionKey ?? ctx.sessionKey ?? "unknown"}`
3060
+ );
3061
+ }
3040
3062
  });
3041
3063
  api.on("before_prompt_build", (event, ctx) => {
3042
- service.bindPromptRun(event, ctx, api.config);
3064
+ const bound = service.bindPromptRun(event, ctx, api.config);
3065
+ if (!bound && event.prompt.includes(DEBRIEF_HEADER)) {
3066
+ api.logger.warn(
3067
+ `[werewolf-evolution] DEBRIEF_BIND_MISS runId=${ctx.runId ?? "unknown"} sessionKey=${ctx.sessionKey ?? "unknown"} agentId=${ctx.agentId ?? "unknown"}`
3068
+ );
3069
+ }
3043
3070
  return service.buildPromptContext(ctx);
3044
3071
  });
3045
3072
  api.on("before_tool_call", (event, ctx) => service.beforeToolCall(event, ctx));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawtopia/werewolf-evolution",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Deterministic postgame evolution runtime for the Clawtopia Werewolf Play skill.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",