@clawtopia/werewolf-evolution 0.1.9 → 0.1.10

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
@@ -108,6 +108,7 @@ interface BoundRun {
108
108
  senderId?: string;
109
109
  senderIsOwner: boolean;
110
110
  consumed: boolean;
111
+ proposalAttempts: number;
111
112
  promptContext?: string;
112
113
  boundAt: number;
113
114
  expiresAt: number;
@@ -162,6 +163,8 @@ declare class EvolutionContextStore {
162
163
  consume(runId: string, sessionKey: string, now?: number): BoundRun;
163
164
  authorizeToolCall(runId: string | undefined, sessionKey: string | undefined, toolCallId: string | undefined, now?: number, agentId?: string): BoundRun;
164
165
  consumeToolCall(toolCallId: string, sessionKey?: string, now?: number): BoundRun;
166
+ beginProposalToolCall(toolCallId: string, sessionKey?: string, now?: number): BoundRun;
167
+ private beginToolCall;
165
168
  cleanup(now?: number): void;
166
169
  }
167
170
 
package/dist/index.js CHANGED
@@ -480,6 +480,7 @@ var EvolutionContextStore = class {
480
480
  senderId: event.senderId,
481
481
  senderIsOwner: event.senderIsOwner === true,
482
482
  consumed: false,
483
+ proposalAttempts: 0,
483
484
  boundAt: now,
484
485
  expiresAt: now + CAPTURE_TTL_MS
485
486
  };
@@ -538,13 +539,30 @@ var EvolutionContextStore = class {
538
539
  return bound;
539
540
  }
540
541
  consumeToolCall(toolCallId, sessionKey, now = Date.now()) {
542
+ const bound = this.beginToolCall(toolCallId, sessionKey, now);
543
+ return this.consume(bound.runId, bound.sessionKey, now);
544
+ }
545
+ beginProposalToolCall(toolCallId, sessionKey, now = Date.now()) {
546
+ const bound = this.beginToolCall(toolCallId, sessionKey, now);
547
+ if (bound.inbound.kind !== "debrief") throw new Error("DEBRIEF_CAPABILITY_REQUIRED");
548
+ if (bound.proposalAttempts >= 2) {
549
+ bound.consumed = true;
550
+ throw new Error("CAPABILITY_ALREADY_CONSUMED");
551
+ }
552
+ bound.proposalAttempts += 1;
553
+ return bound;
554
+ }
555
+ beginToolCall(toolCallId, sessionKey, now = Date.now()) {
541
556
  this.cleanup(now);
542
557
  const authorized = this.toolCalls.get(toolCallId);
543
558
  this.toolCalls.delete(toolCallId);
544
559
  if (!authorized || authorized.expiresAt < now || !sessionKey || authorized.sessionKey !== sessionKey) {
545
560
  throw new Error("UNAUTHORIZED_TOOL_CALL");
546
561
  }
547
- return this.consume(authorized.runId, authorized.sessionKey, now);
562
+ const bound = this.getBound(authorized.runId, authorized.sessionKey, now);
563
+ if (!bound) throw new Error("NO_BOUND_CAPABILITY");
564
+ if (bound.consumed) throw new Error("CAPABILITY_ALREADY_CONSUMED");
565
+ return bound;
548
566
  }
549
567
  cleanup(now = Date.now()) {
550
568
  for (const [key, value] of this.inboundByRun) {
@@ -725,6 +743,7 @@ var ProposalError = class extends Error {
725
743
  this.name = "ProposalError";
726
744
  }
727
745
  code;
746
+ retryable = false;
728
747
  };
729
748
  function requireText(proposal, key, min = 2) {
730
749
  const value = proposal[key];
@@ -896,11 +915,17 @@ function parseAndValidateProposal(raw, envelope, evidence, activeProfile) {
896
915
  if (!window) throw new ProposalError("DECISION_WINDOW_NOT_FOUND", "Proposal does not reference a trusted decision window");
897
916
  const decisionType = requireText(proposal, "decisionType");
898
917
  if (!window.allowedDecisionTypes.includes(decisionType)) {
899
- throw new ProposalError("DECISION_TYPE_NOT_ALLOWED", "decisionType is not valid for the trusted action window");
918
+ throw new ProposalError(
919
+ "DECISION_TYPE_NOT_ALLOWED",
920
+ `decisionType must be copied from allowedDecisionTypes: ${JSON.stringify(window.allowedDecisionTypes)}`
921
+ );
900
922
  }
901
923
  const alternativeActionType = requireText(proposal, "alternativeActionType");
902
924
  if (!window.availableActionTypes.includes(alternativeActionType)) {
903
- throw new ProposalError("ALTERNATIVE_ACTION_UNAVAILABLE", "The proposed alternative action was unavailable at that decision point");
925
+ throw new ProposalError(
926
+ "ALTERNATIVE_ACTION_UNAVAILABLE",
927
+ `alternativeActionType must be copied from availableActionTypes: ${JSON.stringify(window.availableActionTypes)}`
928
+ );
904
929
  }
905
930
  if (proposal.source === "SELF" && window.actorSeat !== envelope.game.recipientSeat) {
906
931
  throw new ProposalError("SELF_ACTOR_MISMATCH", "SELF evidence must anchor the recipient's own action");
@@ -2772,11 +2797,14 @@ var WerewolfEvolutionService = class {
2772
2797
  "- SKIP contains only schemaVersion, action, source, and a concrete skipReason.",
2773
2798
  "- Every non-SKIP proposal must provide profile, decisionWindowId, decisionType, alternativeActionType, anchorFactId, knownFactRefs, outcomeFactRefs, trigger, rule, boundary, behavior, alternativeAction, expectedLocalEffect, observedLocalEffect, falsifier, and alternatives.",
2774
2799
  "- Never send null for an omitted field. Omit the key instead. alternativeTargetSeat may be null only to represent an explicit DAY_VOTE abstention.",
2800
+ "- Copy decisionType exactly from the selected decisionWindow.allowedDecisionTypes. Copy alternativeActionType exactly from that same window.availableActionTypes; these are different fields and must not be mixed.",
2801
+ "- If alternativeActionType=DAY_VOTE, copy a legal seat from that window.availableTargetSeats, or send alternativeTargetSeat=null for explicit abstention. Do not omit the target for DAY_VOTE.",
2775
2802
  "Execution versus strategy contract:",
2776
2803
  "- 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
2804
  "- 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.",
2779
- "After the tool returns, regardless of accepted or rejected status, return exactly NO_REPLY. Never retry the tool call.",
2805
+ "Call werewolf_debrief_commit after checking the complete proposal against this contract.",
2806
+ "A rejected result with retryable=true means one field/semantic correction is allowed: correct only the reported issue using the same trusted window and call the tool once more. A rejected result with retryable=false is final; never call again.",
2807
+ "After a committed, duplicate, incident, or final rejected result, return exactly NO_REPLY. Never use a generic write tool or retry indefinitely.",
2780
2808
  "Do not return NO_REPLY before werewolf_debrief_commit returns a result.",
2781
2809
  "Never use generic write, edit, patch, shell, or memory-write tools for debrief state."
2782
2810
  ].join("\n");
@@ -2851,66 +2879,77 @@ var WerewolfEvolutionService = class {
2851
2879
  }, hooks);
2852
2880
  }
2853
2881
  commit(toolCallId, ctx, input, hooks = {}) {
2854
- const bound = this.contexts.consumeToolCall(toolCallId, ctx.sessionKey);
2855
- const workspaceDir = requireToolWorkspace(ctx, bound);
2856
- if (bound.inbound.kind !== "debrief") throw new Error("DEBRIEF_CAPABILITY_REQUIRED");
2857
- const captured = bound.inbound;
2858
- if (captured.conflict) throw new Error("DEBRIEF_ID_HASH_CONFLICT");
2859
- if (input.debriefId !== captured.envelope.debriefId) throw new Error("DEBRIEF_ID_MISMATCH");
2860
- const paths = resolveWorkspacePaths(workspaceDir);
2861
- return runWorkspaceTransaction(paths, () => {
2862
- assertWorkspaceSetupReady(paths, this.version);
2863
- const current = readWorkspaceState(paths);
2864
- const prior = current.ledger.entries.find((entry2) => entry2.debriefId === captured.envelope.debriefId);
2865
- if (prior) {
2866
- if (prior.bodyHash !== captured.bodyHash) throw new Error("DEBRIEF_ID_HASH_CONFLICT");
2867
- return { changes: /* @__PURE__ */ new Map(), result: { status: "duplicate", debriefId: captured.envelope.debriefId } };
2868
- }
2869
- if (current.ledger.entries.some((entry2) => entry2.gameId === captured.envelope.game.gameId)) {
2870
- return { changes: /* @__PURE__ */ new Map(), result: { status: "duplicate-game", gameId: captured.envelope.game.gameId } };
2871
- }
2872
- if (current.ledger.legacyGameIds.includes(captured.envelope.game.gameId)) {
2873
- return { changes: /* @__PURE__ */ new Map(), result: { status: "duplicate-game", gameId: captured.envelope.game.gameId } };
2874
- }
2875
- const next = clone(current);
2876
- const incidents = incidentReasons(captured.envelope);
2877
- let result;
2878
- if (incidents.length > 0) {
2879
- result = applyIncident(next, captured.envelope, captured.bodyHash, incidents);
2880
- } else {
2881
- const activeProfile = activeRuntimeProfile(current, captured.envelope.game.profile);
2882
- const validated = parseAndValidateProposal(
2883
- input.proposal,
2884
- captured.envelope,
2885
- current.evidence,
2886
- activeProfile
2887
- );
2888
- result = applyProposal(
2889
- next,
2890
- captured.envelope,
2891
- captured.bodyHash,
2892
- validated.proposal,
2893
- validated.risk,
2894
- validated.decisionWindow
2895
- );
2896
- }
2897
- next.agentsText = current.agentsText;
2898
- const changes = workspaceChanges(paths, next);
2899
- const changedProfiles = addProfileUpdates(paths, current, next, changes);
2900
- const finalAgentChange = workspaceChanges(paths, next).get(paths.agents);
2901
- if (finalAgentChange === void 0) changes.delete(paths.agents);
2902
- else changes.set(paths.agents, finalAgentChange);
2903
- return {
2904
- changes,
2905
- result: { status: "committed", debriefId: captured.envelope.debriefId, ...result },
2906
- verify: () => {
2907
- verifyWorkspaceDocuments(paths);
2908
- for (const [profile, expected] of changedProfiles) {
2909
- verifyProfileOverride(expected.file, profile, expected.content);
2910
- }
2882
+ const bound = this.contexts.beginProposalToolCall(toolCallId, ctx.sessionKey);
2883
+ let terminal = true;
2884
+ try {
2885
+ const workspaceDir = requireToolWorkspace(ctx, bound);
2886
+ if (bound.inbound.kind !== "debrief") throw new Error("DEBRIEF_CAPABILITY_REQUIRED");
2887
+ const captured = bound.inbound;
2888
+ if (captured.conflict) throw new Error("DEBRIEF_ID_HASH_CONFLICT");
2889
+ if (input.debriefId !== captured.envelope.debriefId) throw new Error("DEBRIEF_ID_MISMATCH");
2890
+ const paths = resolveWorkspacePaths(workspaceDir);
2891
+ return runWorkspaceTransaction(paths, () => {
2892
+ assertWorkspaceSetupReady(paths, this.version);
2893
+ const current = readWorkspaceState(paths);
2894
+ const prior = current.ledger.entries.find((entry2) => entry2.debriefId === captured.envelope.debriefId);
2895
+ if (prior) {
2896
+ if (prior.bodyHash !== captured.bodyHash) throw new Error("DEBRIEF_ID_HASH_CONFLICT");
2897
+ return { changes: /* @__PURE__ */ new Map(), result: { status: "duplicate", debriefId: captured.envelope.debriefId } };
2911
2898
  }
2912
- };
2913
- }, hooks);
2899
+ if (current.ledger.entries.some((entry2) => entry2.gameId === captured.envelope.game.gameId)) {
2900
+ return { changes: /* @__PURE__ */ new Map(), result: { status: "duplicate-game", gameId: captured.envelope.game.gameId } };
2901
+ }
2902
+ if (current.ledger.legacyGameIds.includes(captured.envelope.game.gameId)) {
2903
+ return { changes: /* @__PURE__ */ new Map(), result: { status: "duplicate-game", gameId: captured.envelope.game.gameId } };
2904
+ }
2905
+ const next = clone(current);
2906
+ const incidents = incidentReasons(captured.envelope);
2907
+ let result;
2908
+ if (incidents.length > 0) {
2909
+ result = applyIncident(next, captured.envelope, captured.bodyHash, incidents);
2910
+ } else {
2911
+ const activeProfile = activeRuntimeProfile(current, captured.envelope.game.profile);
2912
+ const validated = parseAndValidateProposal(
2913
+ input.proposal,
2914
+ captured.envelope,
2915
+ current.evidence,
2916
+ activeProfile
2917
+ );
2918
+ result = applyProposal(
2919
+ next,
2920
+ captured.envelope,
2921
+ captured.bodyHash,
2922
+ validated.proposal,
2923
+ validated.risk,
2924
+ validated.decisionWindow
2925
+ );
2926
+ }
2927
+ next.agentsText = current.agentsText;
2928
+ const changes = workspaceChanges(paths, next);
2929
+ const changedProfiles = addProfileUpdates(paths, current, next, changes);
2930
+ const finalAgentChange = workspaceChanges(paths, next).get(paths.agents);
2931
+ if (finalAgentChange === void 0) changes.delete(paths.agents);
2932
+ else changes.set(paths.agents, finalAgentChange);
2933
+ return {
2934
+ changes,
2935
+ result: { status: "committed", debriefId: captured.envelope.debriefId, ...result },
2936
+ verify: () => {
2937
+ verifyWorkspaceDocuments(paths);
2938
+ for (const [profile, expected] of changedProfiles) {
2939
+ verifyProfileOverride(expected.file, profile, expected.content);
2940
+ }
2941
+ }
2942
+ };
2943
+ }, hooks);
2944
+ } catch (error) {
2945
+ if (error instanceof ProposalError && bound.proposalAttempts < 2) {
2946
+ error.retryable = true;
2947
+ terminal = false;
2948
+ }
2949
+ throw error;
2950
+ } finally {
2951
+ if (terminal && !bound.consumed) this.contexts.consume(bound.runId, bound.sessionKey);
2952
+ }
2914
2953
  }
2915
2954
  decideSuggestion(toolCallId, ctx, input, hooks = {}) {
2916
2955
  const bound = this.contexts.consumeToolCall(toolCallId, ctx.sessionKey);
@@ -2949,7 +2988,14 @@ var WerewolfEvolutionService = class {
2949
2988
  }
2950
2989
  };
2951
2990
  function proposalFailure(error) {
2952
- if (error instanceof ProposalError) return { status: "rejected", code: error.code, message: error.message };
2991
+ if (error instanceof ProposalError) {
2992
+ return {
2993
+ status: "rejected",
2994
+ code: error.code,
2995
+ message: error.message,
2996
+ retryable: error.retryable
2997
+ };
2998
+ }
2953
2999
  return { status: "rejected", code: "EVOLUTION_ERROR", message: error instanceof Error ? error.message : String(error) };
2954
3000
  }
2955
3001
 
@@ -2978,8 +3024,12 @@ var proposalParameters = Type.Object({
2978
3024
  description: "Existing lesson id for SUPPORT, COUNTER, or REFINE. Omit for NEW and SKIP; never send null."
2979
3025
  })),
2980
3026
  profile: Type.Optional(Type.String()),
2981
- decisionWindowId: Type.Optional(Type.String()),
2982
- decisionType: Type.Optional(Type.String()),
3027
+ decisionWindowId: Type.Optional(Type.String({
3028
+ description: "Copy one exact decisionWindowId from trusted decisionWindows; never invent one."
3029
+ })),
3030
+ decisionType: Type.Optional(Type.String({
3031
+ description: "Copy one exact value from the selected window's allowedDecisionTypes. This is a decision category, not an actionType."
3032
+ })),
2983
3033
  executionOperation: Type.Optional(Type.Union([
2984
3034
  Type.Literal("REPORT_DECIDED_ROLE_CLAIM"),
2985
3035
  Type.Literal("EXPLAIN_DECIDED_VOTE"),
@@ -2989,8 +3039,12 @@ var proposalParameters = Type.Object({
2989
3039
  ], {
2990
3040
  description: "Exact operation from executionTemplates. Omit for strategy proposals; never invent an operation."
2991
3041
  })),
2992
- alternativeActionType: Type.Optional(Type.String()),
2993
- alternativeTargetSeat: Type.Optional(Type.Union([Type.Integer(), Type.Null()])),
3042
+ alternativeActionType: Type.Optional(Type.String({
3043
+ description: "Copy one exact game action from the selected window's availableActionTypes, for example DAY_SPEAK or DAY_VOTE. Never copy decisionType here."
3044
+ })),
3045
+ alternativeTargetSeat: Type.Optional(Type.Union([Type.Integer(), Type.Null()], {
3046
+ description: "For DAY_VOTE, choose an exact seat from availableTargetSeats or null for explicit abstention. Omit when the selected action has no target."
3047
+ })),
2994
3048
  anchorFactId: Type.Optional(Type.String()),
2995
3049
  knownFactRefs: Type.Optional(Type.Array(Type.String())),
2996
3050
  outcomeFactRefs: Type.Optional(Type.Array(Type.String())),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawtopia/werewolf-evolution",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "Deterministic postgame evolution runtime for the Clawtopia Werewolf Play skill.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",