@clawtopia/werewolf-evolution 0.1.7 → 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
@@ -55,7 +55,7 @@ interface DebriefProposal {
55
55
  schemaVersion: 1;
56
56
  action: ProposalAction;
57
57
  source: ProposalSource;
58
- topicId?: string | null;
58
+ topicId?: string;
59
59
  profile?: string;
60
60
  decisionWindowId?: string;
61
61
  decisionType?: string;
@@ -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);
@@ -585,7 +599,7 @@ var proposalSchema = z2.object({
585
599
  schemaVersion: z2.literal(1),
586
600
  action: z2.enum(["SKIP", "SUPPORT", "COUNTER", "REFINE", "NEW"]),
587
601
  source: z2.enum(["SELF", "OBSERVED", "EXEMPLAR"]),
588
- topicId: z2.string().regex(SAFE_TOPIC).nullable().optional(),
602
+ topicId: z2.string().regex(SAFE_TOPIC).optional(),
589
603
  profile: z2.string().regex(SAFE_PROFILE2).optional(),
590
604
  decisionWindowId: z2.string().min(1).max(200).optional(),
591
605
  decisionType: z2.string().regex(SAFE_TOKEN).optional(),
@@ -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;
@@ -2747,7 +2764,18 @@ var WerewolfEvolutionService = class {
2747
2764
  `stateAvailable=${stateAvailable}`,
2748
2765
  "The proposal profile must equal activeProfile. The Arena envelope is not the profile authority.",
2749
2766
  "Scan SELF, OBSERVED, and EXEMPLAR, then submit at most one SKIP/SUPPORT/COUNTER/REFINE/NEW proposal.",
2750
- "Prefer a matching existing topic over NEW. Call werewolf_debrief_commit exactly once.",
2767
+ "Proposal action contract:",
2768
+ "- When existingTopics=[] choose only NEW or SKIP. SUPPORT/COUNTER/REFINE are invalid without an existing topic.",
2769
+ "- NEW must omit topicId entirely. SUPPORT/COUNTER/REFINE require an existing topicId copied exactly from existingTopics.",
2770
+ "- When existingTopics is not empty, prefer a matching existing topic over NEW; never invent or guess a topicId.",
2771
+ "Proposal field contract:",
2772
+ "- SKIP contains only schemaVersion, action, source, and a concrete skipReason.",
2773
+ "- Every non-SKIP proposal must provide profile, decisionWindowId, decisionType, alternativeActionType, anchorFactId, knownFactRefs, outcomeFactRefs, trigger, rule, boundary, behavior, alternativeAction, expectedLocalEffect, observedLocalEffect, falsifier, and alternatives.",
2774
+ "- Never send null for an omitted field. Omit the key instead. alternativeTargetSeat may be null only to represent an explicit DAY_VOTE abstention.",
2775
+ "Execution versus strategy contract:",
2776
+ "- executionOperation must be one of the exact template operations. To claim execution risk, copy that template's operation, decisionType, trigger, rule, boundary, and alternativeAction exactly.",
2777
+ "- Strategy proposals must omit executionOperation. CHOOSE_VOTE_TARGET and CHOOSE_ROLE_CLAIM_TIMING are decision types, never executionOperation values.",
2778
+ "Call werewolf_debrief_commit exactly once after checking the complete proposal against this contract.",
2751
2779
  "After the tool returns, regardless of accepted or rejected status, return exactly NO_REPLY. Never retry the tool call.",
2752
2780
  "Do not return NO_REPLY before werewolf_debrief_commit returns a result.",
2753
2781
  "Never use generic write, edit, patch, shell, or memory-write tools for debrief state."
@@ -2946,7 +2974,9 @@ var proposalParameters = Type.Object({
2946
2974
  schemaVersion: Type.Literal(1),
2947
2975
  action: Type.Union(["SKIP", "SUPPORT", "COUNTER", "REFINE", "NEW"].map((value) => Type.Literal(value))),
2948
2976
  source: Type.Union(["SELF", "OBSERVED", "EXEMPLAR"].map((value) => Type.Literal(value))),
2949
- topicId: Type.Optional(Type.Union([Type.String(), Type.Null()])),
2977
+ topicId: Type.Optional(Type.String({
2978
+ description: "Existing lesson id for SUPPORT, COUNTER, or REFINE. Omit for NEW and SKIP; never send null."
2979
+ })),
2950
2980
  profile: Type.Optional(Type.String()),
2951
2981
  decisionWindowId: Type.Optional(Type.String()),
2952
2982
  decisionType: Type.Optional(Type.String()),
@@ -2956,7 +2986,9 @@ var proposalParameters = Type.Object({
2956
2986
  Type.Literal("RETRACT_PUBLIC_JUDGMENT"),
2957
2987
  Type.Literal("AVOID_REPEATING_PRIOR_SPEAKERS"),
2958
2988
  Type.Literal("EXPLAIN_DECIDED_WOLF_TARGET")
2959
- ])),
2989
+ ], {
2990
+ description: "Exact operation from executionTemplates. Omit for strategy proposals; never invent an operation."
2991
+ })),
2960
2992
  alternativeActionType: Type.Optional(Type.String()),
2961
2993
  alternativeTargetSeat: Type.Optional(Type.Union([Type.Integer(), Type.Null()])),
2962
2994
  anchorFactId: Type.Optional(Type.String()),
@@ -2969,7 +3001,9 @@ var proposalParameters = Type.Object({
2969
3001
  alternativeAction: Type.Optional(Type.String()),
2970
3002
  expectedLocalEffect: Type.Optional(Type.String()),
2971
3003
  observedLocalEffect: Type.Optional(Type.String()),
2972
- falsifier: Type.Optional(Type.String()),
3004
+ falsifier: Type.Optional(Type.String({
3005
+ description: "Concrete later observation that would falsify the lesson. Required for every non-SKIP proposal."
3006
+ })),
2973
3007
  alternatives: Type.Optional(Type.Array(Type.String())),
2974
3008
  skipReason: Type.Optional(Type.String())
2975
3009
  }, { additionalProperties: false });
@@ -3019,10 +3053,20 @@ function registerWerewolfEvolution(api, service = getSharedWerewolfEvolutionServ
3019
3053
  api.registerTool((ctx) => createDebriefCommitTool(service, ctx), { name: "werewolf_debrief_commit" });
3020
3054
  api.registerTool((ctx) => createSuggestionDecideTool(service, ctx), { name: "werewolf_suggestion_decide" });
3021
3055
  api.on("message_received", (event, ctx) => {
3022
- 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
+ }
3023
3062
  });
3024
3063
  api.on("before_prompt_build", (event, ctx) => {
3025
- 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
+ }
3026
3070
  return service.buildPromptContext(ctx);
3027
3071
  });
3028
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.7",
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",