@myclaw163/clawclaw-cli 0.8.2 → 0.8.4

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.
Files changed (81) hide show
  1. package/README.md +1 -1
  2. package/docs/configuration.md +1 -1
  3. package/docs/custom-modules/README.md +4 -4
  4. package/docs/custom-modules/api-reference.md +20 -10
  5. package/docs/custom-modules/behavior.md +10 -6
  6. package/docs/custom-modules/perception.md +26 -3
  7. package/docs/custom-modules/trigger.md +1 -1
  8. package/docs/runtime/README.md +1 -1
  9. package/docs/runtime/behavior-runtime.md +6 -2
  10. package/docs/runtime/owner-lifecycle-and-observability.md +1 -1
  11. package/package.json +1 -1
  12. package/skills/clawclaw/SKILL.md +30 -37
  13. package/skills/clawclaw/references/COMMANDS.md +2 -3
  14. package/skills/clawclaw/references/GAME-MECHANICS.md +16 -18
  15. package/skills/clawclaw/references/STREAM.md +25 -10
  16. package/src/behaviors/official-finite/do-task.ts +70 -0
  17. package/src/behaviors/official-finite/index.test.ts +69 -22
  18. package/src/behaviors/official-finite/index.ts +0 -3
  19. package/src/commands/behavior.test.ts +1 -1
  20. package/src/commands/load.ts +8 -3
  21. package/src/commands/trigger.ts +12 -5
  22. package/src/lib/behavior-export.test.ts +53 -9
  23. package/src/lib/behavior-export.ts +26 -3
  24. package/src/lib/clawclaw-skill-spectate-guidance.test.ts +21 -0
  25. package/src/lib/trigger-export.test.ts +80 -0
  26. package/src/lib/trigger-export.ts +210 -0
  27. package/src/lib/user-data.test.ts +4 -0
  28. package/src/lib/user-data.ts +4 -0
  29. package/src/pipeline/canonical-event-presentation.test.ts +2 -0
  30. package/src/pipeline/event-format.test.ts +113 -0
  31. package/src/pipeline/event-format.ts +51 -5
  32. package/src/pipeline/event-hints.test.ts +28 -3
  33. package/src/pipeline/event-hints.ts +20 -6
  34. package/src/pipeline/game-sync-event-normalization.test.ts +49 -1
  35. package/src/pipeline/game-sync-event-normalization.ts +9 -0
  36. package/src/pipeline/role-display.ts +22 -0
  37. package/src/runtime/clawclaw/agent-ability-state.ts +69 -0
  38. package/src/runtime/clawclaw/agent-event-policy.test.ts +6 -0
  39. package/src/runtime/clawclaw/agent-event-policy.ts +5 -0
  40. package/src/runtime/clawclaw/behavior-decision.test.ts +14 -0
  41. package/src/runtime/clawclaw/behavior-decision.ts +8 -1
  42. package/src/runtime/clawclaw/behavior-input.test.ts +22 -0
  43. package/src/runtime/clawclaw/behavior-input.ts +3 -0
  44. package/src/runtime/clawclaw/owner.test.ts +16 -0
  45. package/src/runtime/clawclaw/owner.ts +21 -8
  46. package/src/runtime/clawclaw/plugins/action/action-plugin.test.ts +24 -0
  47. package/src/runtime/clawclaw/plugins/action/action-plugin.ts +8 -1
  48. package/src/runtime/clawclaw/plugins/action/automated-action-normalization.ts +0 -1
  49. package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +1 -1
  50. package/src/runtime/clawclaw/plugins/behavior/behavior-action.test.ts +3 -0
  51. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +246 -0
  52. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +135 -6
  53. package/src/runtime/clawclaw/plugins/behavior/official-goal-behavior.ts +0 -9
  54. package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.test.ts +118 -1
  55. package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.ts +13 -2
  56. package/src/runtime/clawclaw/plugins/derived-event/derived-event-rules.ts +156 -0
  57. package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +0 -1
  58. package/src/runtime/clawclaw-event-catalog.test.ts +6 -0
  59. package/src/runtime/clawclaw-event-catalog.ts +6 -0
  60. package/src/runtime/framework/action-diagnostics.ts +1 -0
  61. package/src/runtime/framework/modules/runtime-module-loader.test.ts +1 -1
  62. package/src/runtime/framework/modules/runtime-module-loader.ts +0 -1
  63. package/src/sdk/action.test.ts +8 -0
  64. package/src/sdk/action.ts +8 -6
  65. package/src/sdk/behavior-plugin/index.ts +11 -2
  66. package/src/sdk/behavior-toolkit/model.ts +2 -0
  67. package/src/sdk/module-input/index.ts +16 -2
  68. package/src/strategies/custom-module.test.ts +0 -1
  69. package/src/strategies/game-utils.test.ts +0 -1
  70. package/src/strategies/goals/avoid-players-top.test.ts +0 -1
  71. package/src/strategies/goals/crab-sabotage-top.test.ts +11 -25
  72. package/src/strategies/goals/crab-sabotage-top.ts +5 -61
  73. package/src/strategies/goals/kill-lone-top.test.ts +0 -1
  74. package/src/strategies/goals/kill-target-top.test.ts +0 -1
  75. package/src/strategies/goals/lone-kill-task-top.test.ts +0 -1
  76. package/src/strategies/goals/normal-shrimp-top.test.ts +0 -1
  77. package/src/strategies/goals/paradise-fish-top.test.ts +0 -1
  78. package/src/strategies/goals/task-only-top.test.ts +0 -1
  79. package/src/strategies/goals/warrior-shrimp-top.test.ts +0 -1
  80. package/src/strategies/types.ts +0 -6
  81. package/src/behaviors/official-finite/trigger-alarm.ts +0 -53
@@ -5,6 +5,7 @@ import {
5
5
  existsSync,
6
6
  readFileSync,
7
7
  readdirSync,
8
+ unlinkSync,
8
9
  } from 'node:fs';
9
10
  import { createHash } from 'node:crypto';
10
11
  import {
@@ -17,6 +18,7 @@ import { fileURLToPath } from 'node:url';
17
18
  import { getWorkspaceDir } from './init-command.js';
18
19
  import {
19
20
  hashOfficialResourceContent,
21
+ isOfficialResourceUnmodified,
20
22
  readOfficialResourceManifest,
21
23
  syncOfficialResources,
22
24
  writeOfficialResourceManifest,
@@ -38,6 +40,7 @@ import {
38
40
  import { deepFreezeBehaviorValue } from '../runtime/clawclaw/plugins/behavior/behavior-manifest.js';
39
41
 
40
42
  const OFFICIAL_BEHAVIOR_EXPORT_FORMAT = 1 as const;
43
+ const RETIRED_OFFICIAL_BEHAVIOR_IDS = new Set(['trigger_alarm']);
41
44
 
42
45
  const OFFICIAL_LONG_BEHAVIOR_FILES = Object.freeze([
43
46
  'avoid-lone.ts',
@@ -75,7 +78,6 @@ const OFFICIAL_FINITE_BEHAVIOR_FILES = Object.freeze([
75
78
  'patrol-area.ts',
76
79
  'report-corpse.ts',
77
80
  'seek-kill-opportunity.ts',
78
- 'trigger-alarm.ts',
79
81
  ] as const);
80
82
 
81
83
  export type OfficialBehaviorFamily = 'long' | 'finite';
@@ -221,8 +223,8 @@ function listBundledOfficialBehaviorSources(): readonly BundledOfficialBehaviorS
221
223
  if (sources.filter(({ family }) => family === 'long').length !== 20) {
222
224
  throw new Error('Expected exactly 20 bundled long Behaviors.');
223
225
  }
224
- if (sources.filter(({ family }) => family === 'finite').length !== 13) {
225
- throw new Error('Expected exactly 13 bundled finite Behaviors.');
226
+ if (sources.filter(({ family }) => family === 'finite').length !== 12) {
227
+ throw new Error('Expected exactly 12 bundled finite Behaviors.');
226
228
  }
227
229
  return Object.freeze(sources);
228
230
  }
@@ -318,6 +320,25 @@ function recordOfficialBehaviorProvenance(
318
320
  if (changed) writeOfficialResourceManifest(path, manifest);
319
321
  }
320
322
 
323
+ function retireRemovedOfficialBehaviors(workspaceDir: string): void {
324
+ const manifestPath = getUserDataOfficialBehaviorsManifestFile(workspaceDir);
325
+ const manifest = readOfficialBehaviorManifest(workspaceDir);
326
+ let changed = false;
327
+ for (const id of RETIRED_OFFICIAL_BEHAVIOR_IDS) {
328
+ const entry = manifest[id];
329
+ if (!entry) continue;
330
+ const destination = join(getUserDataBehaviorsDir(workspaceDir), `${id}.ts`);
331
+ if (existsSync(destination) && !isOfficialResourceUnmodified(destination, entry)) {
332
+ // Preserve user edits, but the retired id is filtered from Runtime discovery.
333
+ continue;
334
+ }
335
+ if (existsSync(destination)) unlinkSync(destination);
336
+ delete manifest[id];
337
+ changed = true;
338
+ }
339
+ if (changed) writeOfficialResourceManifest(manifestPath, manifest);
340
+ }
341
+
321
342
  export interface SyncOfficialBehaviorsOptions {
322
343
  readonly workspaceDir?: string;
323
344
  readonly ids?: readonly string[];
@@ -348,6 +369,7 @@ export async function syncOfficialBehaviors(
348
369
  force: options.force,
349
370
  notFoundMessage: (id) => `Built-in Behavior '${id}' not found.`,
350
371
  });
372
+ if (options.ids === undefined) retireRemovedOfficialBehaviors(workspaceDir);
351
373
  recordOfficialBehaviorProvenance(workspaceDir, bundled);
352
374
  return result;
353
375
  }
@@ -413,6 +435,7 @@ export function listWorkspaceClawclawBehaviorSources(
413
435
  const manifest = readOfficialBehaviorManifest(workspaceDir);
414
436
  return Object.freeze(
415
437
  listClawclawBehaviorSources(getUserDataBehaviorsDir(workspaceDir))
438
+ .filter((source) => !RETIRED_OFFICIAL_BEHAVIOR_IDS.has(source.id))
416
439
  .map((source) => trustedOfficialSource(workspaceDir, source, bundled, manifest)),
417
440
  );
418
441
  }
@@ -0,0 +1,21 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { describe, expect, it } from 'vitest';
4
+
5
+ function skillText(path: string): string {
6
+ return readFileSync(join(process.cwd(), 'skills', 'clawclaw', path), 'utf8');
7
+ }
8
+
9
+ describe('official ClawClaw Skill spectate guidance', () => {
10
+ it('leaves host-specific spectate-link guidance to CCL Event presentation', () => {
11
+ const skill = skillText('SKILL.md');
12
+ const commands = skillText(join('references', 'COMMANDS.md'));
13
+ const stream = skillText(join('references', 'STREAM.md'));
14
+
15
+ expect(skill).not.toMatch(/分享给用户作为 markdown 链接/u);
16
+ expect(skill).not.toMatch(/有 `url` 就按上条完整分享/u);
17
+ expect(skill).not.toMatch(/事件里有 `url` \/ `spectate_url` 时按原样分享/u);
18
+ expect(commands).not.toMatch(/分享完整房间码和可用的完整观战链接/u);
19
+ expect(stream).not.toMatch(/详情带完整 `url` 时直接分享/u);
20
+ });
21
+ });
@@ -0,0 +1,80 @@
1
+ import { existsSync, mkdtempSync, rmSync } from 'node:fs';
2
+ import { tmpdir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ import { afterEach, describe, expect, it } from 'vitest';
5
+ import {
6
+ listOfficialTriggerDefinitions,
7
+ listWorkspaceClawclawTriggerSources,
8
+ rewriteBuiltinTriggerImports,
9
+ syncOfficialTriggers,
10
+ } from './trigger-export.js';
11
+ import { getUserDataOfficialTriggersManifestFile } from './user-data.js';
12
+
13
+ const roots: string[] = [];
14
+
15
+ function workspace(): string {
16
+ const root = mkdtempSync(join(tmpdir(), 'ccl-trigger-export-'));
17
+ roots.push(root);
18
+ return root;
19
+ }
20
+
21
+ afterEach(() => {
22
+ for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
23
+ });
24
+
25
+ describe('official Trigger materialization', () => {
26
+ it('keeps the reviewed official registry empty without inventing modules', async () => {
27
+ const root = workspace();
28
+ const result = await syncOfficialTriggers({ workspaceDir: root });
29
+
30
+ expect(listOfficialTriggerDefinitions()).toEqual([]);
31
+ expect(result).toEqual({
32
+ added: [],
33
+ updated: [],
34
+ unchanged: [],
35
+ preservedModified: [],
36
+ preservedUntracked: [],
37
+ preservedDeleted: [],
38
+ });
39
+ expect(listWorkspaceClawclawTriggerSources(root)).toEqual([]);
40
+ expect(existsSync(getUserDataOfficialTriggersManifestFile(root))).toBe(false);
41
+ });
42
+
43
+ it('rewrites a future bundled Trigger to the public SDK boundary', () => {
44
+ const source = `
45
+ import { ClawclawTrigger } from '../contracts.js';
46
+ export class DangerTrigger extends ClawclawTrigger {
47
+ static override readonly manifest = {
48
+ apiVersion: 1,
49
+ id: 'danger.nearby',
50
+ name: 'Danger nearby',
51
+ description: 'Detect danger.',
52
+ inputs: [],
53
+ consumes: ['player_entered_view'],
54
+ defaultBehavior: 'flee',
55
+ defaultBehaviorInput: {},
56
+ defaultReflexPriority: 10,
57
+ defaultCooldownMs: 500,
58
+ defaultRearm: 'after_clear',
59
+ defaultMaxRuntimeMs: 5000,
60
+ } as const;
61
+ }
62
+ `;
63
+
64
+ const rewritten = rewriteBuiltinTriggerImports(
65
+ source,
66
+ { id: 'danger.nearby', className: 'DangerTrigger' },
67
+ '0.8.2',
68
+ );
69
+ expect(rewritten).toContain("from '@myclaw163/clawclaw-cli/trigger-plugin'");
70
+ expect(rewritten).toContain('export default class DangerTrigger');
71
+ expect(rewritten).toContain('official Trigger danger.nearby@0.8.2');
72
+ });
73
+
74
+ it('rejects an id until it enters the reviewed registry', async () => {
75
+ await expect(syncOfficialTriggers({
76
+ workspaceDir: workspace(),
77
+ ids: ['danger.nearby'],
78
+ })).rejects.toThrow("Built-in Trigger module 'danger.nearby' not found.");
79
+ });
80
+ });
@@ -0,0 +1,210 @@
1
+ // Materialize bundled official Trigger modules into the unified user-owned
2
+ // directory. The registry is intentionally empty until concrete Trigger
3
+ // semantics are reviewed; keeping the full pipeline here makes that addition
4
+ // a resource change rather than another Runtime architecture change.
5
+ import { existsSync, readFileSync } from 'node:fs';
6
+ import { dirname, join, resolve } from 'node:path';
7
+ import { fileURLToPath } from 'node:url';
8
+ import { getWorkspaceDir } from './init-command.js';
9
+ import {
10
+ hashOfficialResourceContent,
11
+ readOfficialResourceManifest,
12
+ syncOfficialResources,
13
+ type OfficialResourceSyncResult,
14
+ } from './official-resource-sync.js';
15
+ import {
16
+ ensureUserDataLayout,
17
+ getUserDataOfficialTriggersManifestFile,
18
+ getUserDataTriggersDir,
19
+ } from './user-data.js';
20
+ import {
21
+ listClawclawTriggerSources,
22
+ type ResolvedClawclawTriggerSource,
23
+ } from '../runtime/clawclaw/plugins/trigger/trigger-source.js';
24
+
25
+ const OFFICIAL_TRIGGER_EXPORT_FORMAT = 1;
26
+
27
+ export interface OfficialTriggerDefinition {
28
+ readonly id: string;
29
+ readonly fileName: string;
30
+ readonly className: string;
31
+ }
32
+
33
+ interface BundledTriggerSource extends OfficialTriggerDefinition {
34
+ readonly path: string;
35
+ readonly source: string;
36
+ readonly sourceHash: string;
37
+ }
38
+
39
+ export interface OfficialTriggerProvenance {
40
+ readonly kind: 'official';
41
+ readonly source: string;
42
+ }
43
+
44
+ export type WorkspaceClawclawTriggerSource = ResolvedClawclawTriggerSource & Readonly<{
45
+ provenance?: Readonly<OfficialTriggerProvenance>;
46
+ }>;
47
+
48
+ // Concrete official Trigger modules are deliberately pending product review.
49
+ const OFFICIAL_TRIGGER_DEFINITIONS: readonly OfficialTriggerDefinition[] = Object.freeze([]);
50
+
51
+ function bundledTriggersDir(): string {
52
+ return join(
53
+ dirname(fileURLToPath(import.meta.url)),
54
+ '..',
55
+ 'runtime',
56
+ 'clawclaw',
57
+ 'plugins',
58
+ 'trigger',
59
+ 'modules',
60
+ );
61
+ }
62
+
63
+ function readPackageVersion(): string {
64
+ try {
65
+ const path = join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'package.json');
66
+ const parsed = JSON.parse(readFileSync(path, 'utf8')) as { version?: unknown };
67
+ return typeof parsed.version === 'string' ? parsed.version : '0.0.0';
68
+ } catch {
69
+ return '0.0.0';
70
+ }
71
+ }
72
+
73
+ function listBundledTriggerSources(): BundledTriggerSource[] {
74
+ const root = bundledTriggersDir();
75
+ return OFFICIAL_TRIGGER_DEFINITIONS.map((definition) => {
76
+ const path = join(root, definition.fileName);
77
+ if (!existsSync(path)) throw new Error(`Bundled Trigger source for '${definition.id}' is missing.`);
78
+ const source = readFileSync(path, 'utf8');
79
+ return {
80
+ ...definition,
81
+ path,
82
+ source,
83
+ sourceHash: hashOfficialResourceContent(
84
+ `${source}\0trigger-export-v${OFFICIAL_TRIGGER_EXPORT_FORMAT}`,
85
+ ),
86
+ };
87
+ });
88
+ }
89
+
90
+ /** Public metadata for tooling; currently returns an empty reviewed registry. */
91
+ export function listOfficialTriggerDefinitions(): readonly Readonly<{
92
+ id: string;
93
+ fileName: string;
94
+ className: string;
95
+ sourceHash: string;
96
+ }>[] {
97
+ return listBundledTriggerSources().map((source) => Object.freeze({
98
+ id: source.id,
99
+ fileName: source.fileName,
100
+ className: source.className,
101
+ sourceHash: source.sourceHash,
102
+ }));
103
+ }
104
+
105
+ /** Converts one package-owned Trigger into a standalone public-SDK source. */
106
+ export function rewriteBuiltinTriggerImports(
107
+ source: string,
108
+ definition: Pick<OfficialTriggerDefinition, 'id' | 'className'>,
109
+ version: string,
110
+ ): string {
111
+ const manifestId = /\bstatic\s+(?:override\s+)?(?:readonly\s+)?manifest\s*=\s*\{[\s\S]*?\bid\s*:\s*(['"])(.*?)\1/.exec(source)?.[2];
112
+ if (manifestId !== definition.id) {
113
+ throw new Error(`Bundled Trigger metadata does not match '${definition.id}'.`);
114
+ }
115
+ const classPattern = new RegExp(
116
+ `export\\s+(?:default\\s+)?class\\s+${definition.className}\\s+extends\\s+ClawclawTrigger\\b`,
117
+ );
118
+ if (!classPattern.test(source)) {
119
+ throw new Error(`Bundled Trigger '${definition.id}' has an unexpected class export.`);
120
+ }
121
+ const rewritten = source
122
+ .replace(
123
+ /from\s+['"]\.\.?\/[^'"]+['"]/g,
124
+ `from '@myclaw163/clawclaw-cli/trigger-plugin'`,
125
+ )
126
+ .replace(
127
+ new RegExp(`export\\s+class\\s+${definition.className}\\b`),
128
+ `export default class ${definition.className}`,
129
+ )
130
+ .replace(
131
+ new RegExp(`\\n?export\\s+default\\s+${definition.className}\\s*;?\\s*$`),
132
+ '\n',
133
+ );
134
+ return `// Materialized from official Trigger ${definition.id}@${version} - customize freely\n${rewritten}`;
135
+ }
136
+
137
+ function materializeTrigger(source: BundledTriggerSource, version: string): string {
138
+ return rewriteBuiltinTriggerImports(source.source, source, version);
139
+ }
140
+
141
+ export interface SyncOfficialTriggersOptions {
142
+ readonly workspaceDir?: string;
143
+ readonly ids?: readonly string[];
144
+ readonly force?: boolean;
145
+ }
146
+
147
+ export async function syncOfficialTriggers(
148
+ options: SyncOfficialTriggersOptions = {},
149
+ ): Promise<OfficialResourceSyncResult> {
150
+ const workspaceDir = options.workspaceDir ?? getWorkspaceDir();
151
+ ensureUserDataLayout(workspaceDir);
152
+ const destinationDir = getUserDataTriggersDir(workspaceDir);
153
+ const installedSources = new Map(
154
+ listClawclawTriggerSources(destinationDir).map((source) => [source.id, source]),
155
+ );
156
+ return syncOfficialResources({
157
+ sources: listBundledTriggerSources(),
158
+ manifestPath: getUserDataOfficialTriggersManifestFile(workspaceDir),
159
+ destinationPath: (source) => join(destinationDir, `${source.id}.ts`),
160
+ hasUntrackedConflict: (source, destination) => {
161
+ const installed = installedSources.get(source.id);
162
+ return installed !== undefined && resolve(installed.sourcePath) !== resolve(destination);
163
+ },
164
+ materialize: materializeTrigger,
165
+ version: readPackageVersion(),
166
+ ids: options.ids,
167
+ force: options.force,
168
+ restoreMissingTracked: true,
169
+ notFoundMessage: (id) => `Built-in Trigger module '${id}' not found.`,
170
+ });
171
+ }
172
+
173
+ function trustedOfficialTrigger(
174
+ workspaceDir: string,
175
+ source: ResolvedClawclawTriggerSource,
176
+ bundled: ReadonlyMap<string, BundledTriggerSource>,
177
+ ): WorkspaceClawclawTriggerSource {
178
+ const official = bundled.get(source.id);
179
+ if (!official) return source;
180
+ const canonicalPath = resolve(getUserDataTriggersDir(workspaceDir), `${source.id}.ts`);
181
+ if (resolve(source.sourcePath) !== canonicalPath) return source;
182
+ const entry = readOfficialResourceManifest(
183
+ getUserDataOfficialTriggersManifestFile(workspaceDir),
184
+ )[source.id];
185
+ if (!entry || entry.sourceHash !== official.sourceHash || !entry.installedHash) return source;
186
+ let installed: string;
187
+ try {
188
+ installed = readFileSync(source.sourcePath, 'utf8');
189
+ } catch {
190
+ return source;
191
+ }
192
+ const expected = materializeTrigger(official, entry.version ?? '0.0.0');
193
+ if (hashOfficialResourceContent(installed) !== entry.installedHash || installed !== expected) return source;
194
+ return Object.freeze({
195
+ ...source,
196
+ provenance: Object.freeze({
197
+ kind: 'official',
198
+ source: `triggers/${official.fileName}`,
199
+ }),
200
+ });
201
+ }
202
+
203
+ /** Discover unified Trigger sources and prove official provenance by exact content. */
204
+ export function listWorkspaceClawclawTriggerSources(
205
+ workspaceDir = getWorkspaceDir(),
206
+ ): WorkspaceClawclawTriggerSource[] {
207
+ const bundled = new Map(listBundledTriggerSources().map((source) => [source.id, source]));
208
+ return listClawclawTriggerSources(getUserDataTriggersDir(workspaceDir))
209
+ .map((source) => trustedOfficialTrigger(workspaceDir, source, bundled));
210
+ }
@@ -12,6 +12,7 @@ import {
12
12
  getUserDataMemoryFile,
13
13
  getUserDataPerceptionsDir,
14
14
  getUserDataPersonaFile,
15
+ getUserDataOfficialTriggersManifestFile,
15
16
  getUserDataTriggersDir,
16
17
  } from './user-data.js';
17
18
 
@@ -74,6 +75,9 @@ describe('user-data layout migration', () => {
74
75
  expect(existsSync(getUserDataBehaviorsDir(ws))).toBe(true);
75
76
  expect(existsSync(getUserDataPerceptionsDir(ws))).toBe(true);
76
77
  expect(existsSync(getUserDataTriggersDir(ws))).toBe(true);
78
+ expect(getUserDataOfficialTriggersManifestFile(ws)).toBe(
79
+ join(getUserDataTriggersDir(ws), '.official-manifest.json'),
80
+ );
77
81
  expect(existsSync(join(ws, 'user-data', 'strategies'))).toBe(false);
78
82
  expect(existsSync(join(ws, 'user-data', 'intents'))).toBe(false);
79
83
  expect(existsSync(join(ws, 'strategies', 'custom.ts'))).toBe(true);
@@ -97,6 +97,10 @@ export function getUserDataOfficialPerceptionsManifestFile(workspaceDir: string)
97
97
  return join(getUserDataPerceptionsDir(workspaceDir), '.official-manifest.json');
98
98
  }
99
99
 
100
+ export function getUserDataOfficialTriggersManifestFile(workspaceDir: string): string {
101
+ return join(getUserDataTriggersDir(workspaceDir), '.official-manifest.json');
102
+ }
103
+
100
104
  export function getUserDataHubDir(workspaceDir: string): string {
101
105
  return join(getUserDataDir(workspaceDir), 'hub');
102
106
  }
@@ -36,6 +36,7 @@ describe('canonical Agent Event presentation', () => {
36
36
  const presented = presentCanonicalEvent(canonical('killed', {
37
37
  tick: 7,
38
38
  killer_name: 'Alpha',
39
+ killer_role: 'crab_assassin',
39
40
  target_name: 'Bravo',
40
41
  hint: 'backend-owned hint',
41
42
  next_step: 'backend-owned next step',
@@ -43,6 +44,7 @@ describe('canonical Agent Event presentation', () => {
43
44
  }), null);
44
45
 
45
46
  expect(presented.hint).toContain('Alpha');
47
+ expect(presented.hint).toContain('Assassin Crab');
46
48
  expect(presented.hint).not.toContain('backend-owned hint');
47
49
  expect(presented).not.toHaveProperty('notice');
48
50
  expect(presented).not.toHaveProperty('next_step');
@@ -47,6 +47,7 @@ describe('event-format', () => {
47
47
  room: '情报室',
48
48
  killer_name: '哈基米南北绿豆',
49
49
  killer_seat: 8,
50
+ killer_role: 'neutral_octopus',
50
51
  hint: 'YOU ARE DEAD: 哈基米南北绿豆 removed you from play. Your hands are off the board now — alert your user, then spectate or leave while your teammates weaponize what you learned.',
51
52
  };
52
53
 
@@ -56,6 +57,7 @@ describe('event-format', () => {
56
57
 
57
58
  expect(message).toContain('t5330 killed');
58
59
  expect(message).toContain('8号哈基米南北绿豆');
60
+ expect(message).toContain('8号哈基米南北绿豆(章鱼)');
59
61
  expect(formatted).not.toHaveProperty('notice');
60
62
  expect(formatted).not.toHaveProperty('hint');
61
63
  expect(JSON.stringify(formatted)).not.toContain('Your hands are off the board');
@@ -68,10 +70,26 @@ describe('event-format', () => {
68
70
  room: '制氧舱',
69
71
  killer_kind: 'system',
70
72
  killer_name: 'Shrimp God',
73
+ killer_role: 'neutral_octopus',
71
74
  });
72
75
 
73
76
  expect(message).toContain('被Shrimp God击杀');
74
77
  expect(message).not.toContain('?号Shrimp God');
78
+ expect(message).not.toContain('章鱼');
79
+ });
80
+
81
+ it('formats emergency interruption as a private second-person fact', () => {
82
+ const message = formatEventMessage({
83
+ type: 'emergency_interrupted',
84
+ tick: 486,
85
+ actor_name: 'Alpha',
86
+ actor_seat: 2,
87
+ task_name: '修复火灾-发动机',
88
+ });
89
+
90
+ expect(message).toContain('你在处理修复火灾-发动机时死亡');
91
+ expect(message).not.toContain('Alpha');
92
+ expect(message).not.toContain('未知玩家');
75
93
  });
76
94
 
77
95
  it('downgrades self-killer murder_witnessed presentation to neutral diagnostics', () => {
@@ -539,6 +557,7 @@ describe('event-format', () => {
539
557
  status: 'cooldown_charged',
540
558
  cooldown_remaining_secs: 4.25,
541
559
  });
560
+ expect(state?.abilities).toBeUndefined();
542
561
  expect(state?.players).toEqual([
543
562
  { name: 'me', seat: 1, known_status: { is_alive: true } },
544
563
  { name: 'nearby', seat: 2, known_status: { is_alive: true } },
@@ -703,6 +722,68 @@ describe('event-format', () => {
703
722
  expect(BACKEND_EVENT_HINT_FORMATTERS.octopus_time_start(event, {})).toContain('emergency timeout');
704
723
  });
705
724
 
725
+ it('formats emergency countdown and ability transition facts', () => {
726
+ expect(formatEventMessage({
727
+ type: 'emergency_countdown_warning',
728
+ tick: 12,
729
+ task_name: '修复供氧',
730
+ threshold_secs: 30,
731
+ remaining_secs: 29.8,
732
+ })).toBe('t12 emergency_countdown_warning 紧急任务修复供氧只剩29.8秒(30秒提醒)。');
733
+ expect(formatEventMessage({
734
+ type: 'ability.ready',
735
+ tick: 13,
736
+ ability_id: 'kill',
737
+ ability_name: '攻击',
738
+ })).toBe('t13 ability.ready 能力“攻击”现在可以使用。');
739
+ expect(formatEventMessage({
740
+ type: 'ability.exhausted',
741
+ tick: 14,
742
+ ability_id: 'kill',
743
+ ability_name: '攻击',
744
+ })).toBe('t14 ability.exhausted 能力“攻击”已无剩余次数。');
745
+ });
746
+
747
+ it('includes current emergency and stateful abilities in Agent State', () => {
748
+ const state = compactStateForEvents({
749
+ phase: 'wandering',
750
+ tick: 88,
751
+ you: {
752
+ player: { name: 'me', seat: 1 },
753
+ role: 'shrimp_pistol',
754
+ is_alive: true,
755
+ kill_cooldown_secs: 0,
756
+ kills_remaining: 1,
757
+ },
758
+ emergency: {
759
+ active: true,
760
+ task_name: '修复供氧',
761
+ room: '制氧舱',
762
+ x: 120,
763
+ y: 240,
764
+ remaining_secs: 27.5,
765
+ internal_id: 'must-not-leak',
766
+ },
767
+ });
768
+
769
+ expect(state?.emergency).toEqual({
770
+ active: true,
771
+ task_name: '修复供氧',
772
+ room: '制氧舱',
773
+ x: 120,
774
+ y: 240,
775
+ remaining_secs: 27.5,
776
+ });
777
+ expect(state?.abilities).toEqual([{
778
+ id: 'kill',
779
+ name: '攻击',
780
+ ready: true,
781
+ cooldown_remaining_secs: 0,
782
+ remaining_uses: 1,
783
+ effective_range_px: 160,
784
+ }]);
785
+ });
786
+
706
787
  it('tells every surviving role that emergency repair is real and available', () => {
707
788
  const hint = BACKEND_EVENT_HINT_FORMATTERS.emergency_started({
708
789
  type: 'emergency_started',
@@ -715,6 +796,37 @@ describe('event-format', () => {
715
796
  expect(hint).not.toContain('Lobsters —');
716
797
  });
717
798
 
799
+ it('explains sabotage-caused emergencies without exposing a saboteur identity', () => {
800
+ const event = {
801
+ type: 'emergency_started',
802
+ task_name: '稳定反应堆',
803
+ room: '反应堆',
804
+ timeout_secs: 80,
805
+ cause: 'sabotage',
806
+ };
807
+
808
+ expect(formatEventMessage(event)).toContain('蟹刚完成对应破坏任务');
809
+ const hint = BACKEND_EVENT_HINT_FORMATTERS.emergency_started(event, {});
810
+ expect(hint).toContain('A Crab completed the paired sabotage task');
811
+ expect(hint).toContain("identity is not exposed");
812
+ });
813
+
814
+ it('presents a private sabotage task interruption with its authoritative cooldown', () => {
815
+ const event = {
816
+ type: 'task_interrupted',
817
+ task_id: '102',
818
+ task_name: '投放毒药',
819
+ reason: 'emergency_started',
820
+ cooldown_remaining_secs: 100,
821
+ };
822
+
823
+ expect(formatEventMessage(event)).toContain('任务未消耗');
824
+ expect(formatEventMessage(event)).toContain('100秒');
825
+ const hint = BACKEND_EVENT_HINT_FORMATTERS.task_interrupted(event, {});
826
+ expect(hint).toContain('was interrupted');
827
+ expect(hint).toContain('not consumed');
828
+ });
829
+
718
830
  it('states when a meeting, rather than a player, resolves an emergency', () => {
719
831
  const event = {
720
832
  type: 'emergency_resolved',
@@ -755,6 +867,7 @@ describe('event-format', () => {
755
867
  'octopus_time_start',
756
868
  'role_assigned',
757
869
  'speech_skipped',
870
+ 'task_interrupted',
758
871
  'vote_phase_start',
759
872
  'vote_speech_phase_ended',
760
873
  ];