@hmharness/environments 0.20.0 → 0.20.2

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/adapters.js CHANGED
@@ -210,19 +210,18 @@ export class Arc3Environment {
210
210
  state: {
211
211
  gameId: this.session.gameId,
212
212
  guid: this.session.guid,
213
- score: f.score,
214
- reward: f.reward,
215
- status: f.status,
213
+ levelsCompleted: f.levels_completed ?? 0,
214
+ winLevels: f.win_levels ?? 0,
215
+ gameState: f.state ?? null,
216
216
  framePreview: Array.isArray(f.frame) ? f.frame.slice(0, 64) : f.frame,
217
217
  steps: this.session.steps,
218
- totalReward: this.session.totalReward,
219
218
  },
220
- availableActions: this.actionSpecs(),
219
+ availableActions: this.actionSpecs(f.available_actions),
221
220
  raw: f,
222
221
  };
223
222
  }
224
- actionSpecs() {
225
- return [
223
+ actionSpecs(available) {
224
+ const all = [
226
225
  { id: 'a1', type: 'ACTION1', description: 'simple action 1 (game-defined, e.g. move up / option A)', cost: 1 },
227
226
  { id: 'a2', type: 'ACTION2', description: 'simple action 2', cost: 1 },
228
227
  { id: 'a3', type: 'ACTION3', description: 'simple action 3', cost: 1 },
@@ -231,6 +230,13 @@ export class Arc3Environment {
231
230
  { id: 'a6', type: 'ACTION6', description: 'coordinate action: click/tap at (x,y) on the 64×64 grid', argsSchema: { x: 'number 0-63', y: 'number 0-63' }, cost: 1 },
232
231
  { id: 'a7', type: 'ACTION7', description: 'undo (games that support it)', cost: 1 },
233
232
  ];
233
+ // the live frame advertises which actions THIS game accepts — trust it
234
+ // (the API returns action NUMBERS: [6] means only ACTION6)
235
+ if (available && available.length > 0) {
236
+ const set = new Set(available.map((a) => (typeof a === 'number' ? `ACTION${a}` : String(a).toUpperCase())));
237
+ return all.filter((a) => set.has(a.type));
238
+ }
239
+ return all;
234
240
  }
235
241
  async act(action) {
236
242
  const bridge = this.ensureLive();
@@ -250,13 +256,16 @@ export class Arc3Environment {
250
256
  else {
251
257
  return { actionId: action.id, outcome: 'failure', error: { code: 'E_UNKNOWN_ACTION', message: `unknown ARC action ${action.type}` }, durationMs: Date.now() - started };
252
258
  }
259
+ const before = this.session.lastFrame?.levels_completed ?? 0;
260
+ const after = frame.levels_completed ?? 0;
253
261
  this.session.lastFrame = frame;
254
262
  this.session.steps += 1;
255
263
  this.session.totalReward += frame.reward ?? 0;
256
264
  return {
257
265
  actionId: action.id,
258
- outcome: frame.status === 'DEAD' || frame.reward !== undefined && frame.reward < 0 ? 'failure' : 'success',
259
- output: { score: frame.score, reward: frame.reward, status: frame.status },
266
+ // outcome: the call landed; a level win shows as progress
267
+ outcome: frame.status === 'DEAD' ? 'failure' : 'success',
268
+ output: { levelsCompleted: after, levelProgressed: after > before, reward: frame.reward, status: frame.status },
260
269
  durationMs: Date.now() - started,
261
270
  cost: 1,
262
271
  };
@@ -290,15 +299,16 @@ export class Arc3Environment {
290
299
  return { environmentId: this.id, metrics: {} };
291
300
  const summary = await bridge.getScorecard(this.session.cardId).catch(() => ({}));
292
301
  const envs = (summary.environments ?? []);
293
- const mine = envs.find((e) => e.game_id === this.session?.gameId) ?? summary;
302
+ const mine = envs.find((e) => e.id === this.session?.gameId || e.game_id === this.session?.gameId) ?? summary;
294
303
  const num = (v) => (typeof v === 'number' ? v : 0);
295
304
  return {
296
305
  environmentId: this.id,
297
306
  metrics: {
298
307
  steps: this.session.steps,
299
- totalReward: this.session.totalReward,
300
- score: num(mine.score),
301
- winRate: num(mine.win_rate),
308
+ levelsCompleted: num(mine.levels_completed),
309
+ levelCount: num(mine.level_count),
310
+ actions: num(mine.actions),
311
+ completed: num(mine.completed) || (mine.completed === true ? 1 : 0),
302
312
  },
303
313
  details: 'ARC-AGI-3 official scorecard',
304
314
  };
@@ -0,0 +1,6 @@
1
+ /** 16 high-contrast colors indexed 0..15 (approximation, stable) */
2
+ export declare const ARC3_PALETTE: Array<[number, number, number]>;
3
+ /** Composite the frame's grids side-by-side (white gutter) into a PNG. */
4
+ export declare function renderFramePng(frame: unknown, scale?: number): Buffer | null;
5
+ /** Describe the grid compactly for text-only models (color-run encoding). */
6
+ export declare function gridSummary(grid: number[][]): string;
@@ -0,0 +1,127 @@
1
+ /**
2
+ * @hmharness/environments - ARC-AGI-3 frame renderer (blueprint §14)
3
+ *
4
+ * The live frame is [gridA, gridB] — typically before/after grids — each a
5
+ * jagged array of rows whose cells are palette indices. We composite them
6
+ * side by side into ONE truecolor PNG with a zero-dependency encoder
7
+ * (zlib deflate + hand-built chunks), cells upscaled so the vision model
8
+ * can actually SEE the game. The 16-color palette is a stable approximation
9
+ * chosen for visual separability (exact official RGBs are not published in
10
+ * the API docs); consistency across runs is what reasoning needs.
11
+ */
12
+ import { deflateSync } from 'node:zlib';
13
+ /** 16 high-contrast colors indexed 0..15 (approximation, stable) */
14
+ export const ARC3_PALETTE = [
15
+ [0, 0, 0], // 0 black
16
+ [0, 116, 255], // 1 blue
17
+ [255, 121, 0], // 2 orange
18
+ [156, 183, 41], // 3 green
19
+ [255, 0, 200], // 4 magenta
20
+ [0, 200, 255], // 5 sky
21
+ [128, 128, 128], // 6 gray
22
+ [230, 230, 230], // 7 silver
23
+ [165, 42, 42], // 8 brown
24
+ [255, 215, 0], // 9 gold
25
+ [0, 255, 140], // 10 mint
26
+ [255, 80, 80], // 11 coral
27
+ [80, 80, 255], // 12 indigo
28
+ [200, 255, 100], // 13 lime
29
+ [255, 160, 255], // 14 pink
30
+ [60, 60, 90], // 15 slate
31
+ ];
32
+ function cellColor(grid, x, y) {
33
+ const row = grid[y];
34
+ if (!Array.isArray(row))
35
+ return ARC3_PALETTE[0];
36
+ const v = Number(row[x]);
37
+ return ARC3_PALETTE[Number.isInteger(v) && v >= 0 && v <= 15 ? v : 0];
38
+ }
39
+ /** Composite the frame's grids side-by-side (white gutter) into a PNG. */
40
+ export function renderFramePng(frame, scale = 8) {
41
+ const grids = Array.isArray(frame) ? frame.filter((g) => Array.isArray(g)) : [];
42
+ if (grids.length === 0)
43
+ return null;
44
+ const heights = grids.map((g) => g.length);
45
+ const widths = grids.map((g) => Math.max(1, ...g.map((r) => (Array.isArray(r) ? r.length : 0))));
46
+ const cellH = Math.max(...heights, 1);
47
+ const gutter = 4; // gutter cells between grids
48
+ const totalCellsW = widths.reduce((s, w) => s + w, 0) + gutter * (grids.length - 1);
49
+ const W = totalCellsW * scale;
50
+ const H = cellH * scale;
51
+ const raw = Buffer.alloc(H * (1 + W * 3));
52
+ let o = 0;
53
+ for (let py = 0; py < H; py++) {
54
+ raw[o++] = 0; // filter none
55
+ const gy = Math.floor(py / scale);
56
+ for (let px = 0; px < W; px++) {
57
+ const gx = Math.floor(px / scale);
58
+ // which grid and local x?
59
+ let rem = gx;
60
+ let gi = 0;
61
+ while (gi < grids.length - 1 && rem >= widths[gi]) {
62
+ rem -= widths[gi] + gutter;
63
+ gi++;
64
+ }
65
+ const inGutter = rem >= widths[gi] || gi >= grids.length;
66
+ let c;
67
+ if (inGutter || gy >= heights[gi])
68
+ c = [255, 255, 255];
69
+ else
70
+ c = cellColor(grids[gi], rem, gy);
71
+ raw[o++] = c[0];
72
+ raw[o++] = c[1];
73
+ raw[o++] = c[2];
74
+ }
75
+ }
76
+ return encodePng(W, H, raw);
77
+ }
78
+ /** minimal zero-dependency truecolor PNG encoder */
79
+ function encodePng(width, height, rawData) {
80
+ const sig = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
81
+ const ihdr = Buffer.alloc(13);
82
+ ihdr.writeUInt32BE(width, 0);
83
+ ihdr.writeUInt32BE(height, 4);
84
+ ihdr[8] = 8; // bit depth
85
+ ihdr[9] = 2; // color type: truecolor
86
+ const chunks = [sig, chunk(0x49484452, ihdr), chunk(0x49444154, deflateSync(rawData)), chunk(0x49454e44, Buffer.alloc(0))];
87
+ return Buffer.concat(chunks);
88
+ }
89
+ function chunk(type, data) {
90
+ const len = Buffer.alloc(4);
91
+ len.writeUInt32BE(data.length, 0);
92
+ const t = Buffer.alloc(4);
93
+ t.writeUInt32BE(type, 0);
94
+ const crc = Buffer.alloc(4);
95
+ crc.writeUInt32BE(crc32(Buffer.concat([t, data])), 0);
96
+ return Buffer.concat([len, t, data, crc]);
97
+ }
98
+ const CRC_TABLE = (() => {
99
+ const table = new Uint32Array(256);
100
+ for (let n = 0; n < 256; n++) {
101
+ let c = n;
102
+ for (let k = 0; k < 8; k++)
103
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
104
+ table[n] = c >>> 0;
105
+ }
106
+ return table;
107
+ })();
108
+ function crc32(buf) {
109
+ let c = 0xffffffff;
110
+ for (let i = 0; i < buf.length; i++)
111
+ c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
112
+ return (c ^ 0xffffffff) >>> 0;
113
+ }
114
+ /** Describe the grid compactly for text-only models (color-run encoding). */
115
+ export function gridSummary(grid) {
116
+ const palette = new Map();
117
+ for (const row of grid) {
118
+ if (!Array.isArray(row))
119
+ continue;
120
+ for (const v of row)
121
+ palette.set(Number(v), (palette.get(Number(v)) ?? 0) + 1);
122
+ }
123
+ const rows = grid.length;
124
+ const cols = Math.max(1, ...grid.map((r) => (Array.isArray(r) ? r.length : 0)));
125
+ const colors = [...palette.entries()].sort((a, b) => b[1] - a[1]).map(([v, n]) => `${v}×${n}`);
126
+ return `${rows}×${cols} cells; colors: ${colors.join(' ')}`;
127
+ }
package/dist/index.d.ts CHANGED
@@ -14,5 +14,6 @@ export { BrowserEnvironment, DesktopEnvironment, Arc3Environment } from './adapt
14
14
  export type { BrowserEnvOptions, DesktopEnvOptions, Arc3EnvOptions } from './adapters.js';
15
15
  export { Arc3RestBridge, ARC3_BASE } from './arc3-rest.js';
16
16
  export type { Arc3Frame, Arc3Game } from './arc3-rest.js';
17
+ export { renderFramePng, gridSummary, ARC3_PALETTE } from './arc3-render.js';
17
18
  export { browserActBridge, desktopActBridge } from './bridges.js';
18
19
  export type { ToolExecute } from './bridges.js';
package/dist/index.js CHANGED
@@ -10,4 +10,5 @@ export { TerminalEnvironment, runShell } from "./terminal.js";
10
10
  export { HarmonyOsEnvironment } from "./harmonyos.js";
11
11
  export { BrowserEnvironment, DesktopEnvironment, Arc3Environment } from "./adapters.js";
12
12
  export { Arc3RestBridge, ARC3_BASE } from "./arc3-rest.js";
13
+ export { renderFramePng, gridSummary, ARC3_PALETTE } from "./arc3-render.js";
13
14
  export { browserActBridge, desktopActBridge } from "./bridges.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/environments",
3
- "version": "0.20.0",
3
+ "version": "0.20.2",
4
4
  "description": "HMH Cognitive OS environment adapters: Terminal (native), HarmonyOS (via domain-harmony), Browser/Desktop (via host automation bridge), ARC3 (external adapter). Every adapter implements the @hmharness/cognitive Environment protocol and must pass the shared conformance test.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -26,7 +26,7 @@
26
26
  "node": ">=22"
27
27
  },
28
28
  "dependencies": {
29
- "@hmharness/kernel": "0.20.0",
30
- "@hmharness/cognitive": "0.20.0"
29
+ "@hmharness/kernel": "0.20.2",
30
+ "@hmharness/cognitive": "0.20.2"
31
31
  }
32
32
  }