@hmharness/environments 0.19.0
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.d.ts +98 -0
- package/dist/adapters.js +224 -0
- package/dist/harmonyos.d.ts +36 -0
- package/dist/harmonyos.js +126 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +11 -0
- package/dist/terminal.d.ts +39 -0
- package/dist/terminal.js +247 -0
- package/package.json +32 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/environments - Browser / Desktop / ARC3 adapters
|
|
3
|
+
*
|
|
4
|
+
* Browser (ENV-011..013): observation over the CDP HTTP endpoint (tab list,
|
|
5
|
+
* titles, URLs) — real, zero-dependency. Actions require a CDP WebSocket
|
|
6
|
+
* bridge supplied by the host (the agent's browser tooling); without the
|
|
7
|
+
* bridge every act() answers a structured E_NO_CDP_BRIDGE, never a fake
|
|
8
|
+
* success. Evaluation is page/task assertions — bridge-gated too.
|
|
9
|
+
*
|
|
10
|
+
* Desktop (ENV-014..016): observation via window enumeration (real);
|
|
11
|
+
* act/evaluate require the desktop automation trio bridge (agent tools).
|
|
12
|
+
*
|
|
13
|
+
* ARC3 (ARC-001..003): adapter skeleton — requires the ARC-AGI-3 SDK/task
|
|
14
|
+
* bundle at runtime; register() refuses to fake runs without it.
|
|
15
|
+
*/
|
|
16
|
+
import type { Environment, Observation, Action, ActionResult, Snapshot, EnvironmentScore, Capability } from '@hmharness/cognitive';
|
|
17
|
+
export interface BrowserEnvOptions {
|
|
18
|
+
/** CDP debugging address (default Chrome :9222) */
|
|
19
|
+
cdpBase?: string;
|
|
20
|
+
/** host-provided action bridge (Runtime.evaluate etc.) */
|
|
21
|
+
act?: (action: Action, tabId: string) => Promise<{
|
|
22
|
+
outcome: 'success' | 'failure' | 'unknown';
|
|
23
|
+
output?: unknown;
|
|
24
|
+
error?: {
|
|
25
|
+
code: string;
|
|
26
|
+
message: string;
|
|
27
|
+
};
|
|
28
|
+
}>;
|
|
29
|
+
}
|
|
30
|
+
export declare class BrowserEnvironment implements Environment {
|
|
31
|
+
private opts;
|
|
32
|
+
id: string;
|
|
33
|
+
version: string;
|
|
34
|
+
private tabs;
|
|
35
|
+
private history;
|
|
36
|
+
constructor(opts?: BrowserEnvOptions);
|
|
37
|
+
private get base();
|
|
38
|
+
capabilities(): Promise<Capability[]>;
|
|
39
|
+
reset(): Promise<Observation>;
|
|
40
|
+
observe(): Promise<Observation>;
|
|
41
|
+
private actionSpecs;
|
|
42
|
+
act(action: Action): Promise<ActionResult>;
|
|
43
|
+
snapshot(): Promise<Snapshot>;
|
|
44
|
+
restore(): Promise<void>;
|
|
45
|
+
evaluate(): Promise<EnvironmentScore>;
|
|
46
|
+
close(): Promise<void>;
|
|
47
|
+
private endpointUp;
|
|
48
|
+
}
|
|
49
|
+
export interface DesktopEnvOptions {
|
|
50
|
+
act?: (action: Action) => Promise<{
|
|
51
|
+
outcome: 'success' | 'failure' | 'unknown';
|
|
52
|
+
output?: unknown;
|
|
53
|
+
error?: {
|
|
54
|
+
code: string;
|
|
55
|
+
message: string;
|
|
56
|
+
};
|
|
57
|
+
}>;
|
|
58
|
+
}
|
|
59
|
+
export declare class DesktopEnvironment implements Environment {
|
|
60
|
+
private opts;
|
|
61
|
+
id: string;
|
|
62
|
+
version: string;
|
|
63
|
+
private windows;
|
|
64
|
+
constructor(opts?: DesktopEnvOptions);
|
|
65
|
+
capabilities(): Promise<Capability[]>;
|
|
66
|
+
reset(): Promise<Observation>;
|
|
67
|
+
observe(): Promise<Observation>;
|
|
68
|
+
private actionSpecs;
|
|
69
|
+
act(action: Action): Promise<ActionResult>;
|
|
70
|
+
snapshot(): Promise<Snapshot>;
|
|
71
|
+
restore(): Promise<void>;
|
|
72
|
+
evaluate(): Promise<EnvironmentScore>;
|
|
73
|
+
close(): Promise<void>;
|
|
74
|
+
}
|
|
75
|
+
export interface Arc3EnvOptions {
|
|
76
|
+
/** path/endpoint to the ARC-AGI-3 SDK task bundle; REQUIRED for real runs */
|
|
77
|
+
taskBundle?: string;
|
|
78
|
+
/** host bridge executing ARC interactions (SDK client) */
|
|
79
|
+
interact?: (action: Action) => Promise<{
|
|
80
|
+
outcome: 'success' | 'failure' | 'unknown';
|
|
81
|
+
observation?: unknown;
|
|
82
|
+
}>;
|
|
83
|
+
}
|
|
84
|
+
export declare class Arc3Environment implements Environment {
|
|
85
|
+
private opts;
|
|
86
|
+
id: string;
|
|
87
|
+
version: string;
|
|
88
|
+
constructor(opts?: Arc3EnvOptions);
|
|
89
|
+
capabilities(): Promise<Capability[]>;
|
|
90
|
+
private refuse;
|
|
91
|
+
reset(): Promise<Observation>;
|
|
92
|
+
observe(): Promise<Observation>;
|
|
93
|
+
act(action: Action): Promise<ActionResult>;
|
|
94
|
+
snapshot(): Promise<Snapshot>;
|
|
95
|
+
restore(): Promise<void>;
|
|
96
|
+
evaluate(): Promise<EnvironmentScore>;
|
|
97
|
+
close(): Promise<void>;
|
|
98
|
+
}
|
package/dist/adapters.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { stableHash } from '@hmharness/cognitive';
|
|
2
|
+
import { runShell } from "./terminal.js";
|
|
3
|
+
export class BrowserEnvironment {
|
|
4
|
+
opts;
|
|
5
|
+
id = 'browser';
|
|
6
|
+
version = '1.0.0';
|
|
7
|
+
tabs = [];
|
|
8
|
+
history = [];
|
|
9
|
+
constructor(opts = {}) {
|
|
10
|
+
this.opts = opts;
|
|
11
|
+
}
|
|
12
|
+
get base() {
|
|
13
|
+
return this.opts.cdpBase ?? 'http://127.0.0.1:9222';
|
|
14
|
+
}
|
|
15
|
+
async capabilities() {
|
|
16
|
+
const live = await this.endpointUp();
|
|
17
|
+
return [
|
|
18
|
+
{ kind: 'observe', detail: 'tab list (titles/urls) via CDP /json', limitation: live ? undefined : 'no browser at ' + this.base },
|
|
19
|
+
{ kind: 'act', detail: 'navigate/click/type via host CDP bridge', limitation: this.opts.act ? undefined : 'act bridge not configured — actions return E_NO_CDP_BRIDGE' },
|
|
20
|
+
{ kind: 'evaluate', detail: 'page assertions via act bridge', limitation: this.opts.act ? undefined : 'bridge-gated' },
|
|
21
|
+
{ kind: 'network', detail: 'through the attached browser only' },
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
async reset() {
|
|
25
|
+
this.history = [];
|
|
26
|
+
return this.observe();
|
|
27
|
+
}
|
|
28
|
+
async observe() {
|
|
29
|
+
try {
|
|
30
|
+
const res = await fetch(this.base + '/json', { signal: AbortSignal.timeout(2_000) });
|
|
31
|
+
const list = (await res.json());
|
|
32
|
+
this.tabs = list.filter((t) => t.type === 'page');
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
this.tabs = [];
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
environmentId: this.id,
|
|
39
|
+
timestamp: new Date().toISOString(),
|
|
40
|
+
state: { tabs: this.tabs.map((t) => ({ id: t.id, title: t.title, url: t.url })), history: this.history.slice(-5) },
|
|
41
|
+
availableActions: this.actionSpecs(),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
actionSpecs() {
|
|
45
|
+
const dead = !this.opts.act;
|
|
46
|
+
return [
|
|
47
|
+
{ id: 'navigate', type: 'navigate', description: 'open a URL in the active tab', argsSchema: { url: 'string' }, cost: 2 },
|
|
48
|
+
{ id: 'click', type: 'click', description: 'click a selector', argsSchema: { selector: 'string' }, cost: 1 },
|
|
49
|
+
{ id: 'type', type: 'type-text', description: 'type text into a selector', argsSchema: { selector: 'string', text: 'string' }, cost: 1 },
|
|
50
|
+
].map((s) => (dead ? { ...s, description: `${s.description} (needs act bridge)` } : s));
|
|
51
|
+
}
|
|
52
|
+
async act(action) {
|
|
53
|
+
const started = Date.now();
|
|
54
|
+
if (!this.opts.act) {
|
|
55
|
+
return { actionId: action.id, outcome: 'failure', error: { code: 'E_NO_CDP_BRIDGE', message: 'browser act requires a host CDP bridge (BrowserEnvOptions.act)' }, durationMs: Date.now() - started };
|
|
56
|
+
}
|
|
57
|
+
if (this.tabs.length === 0) {
|
|
58
|
+
return { actionId: action.id, outcome: 'failure', error: { code: 'E_NO_TABS', message: 'no browser tabs reachable at ' + this.base }, durationMs: Date.now() - started };
|
|
59
|
+
}
|
|
60
|
+
const r = await this.opts.act(action, this.tabs[0].id);
|
|
61
|
+
if (action.type === 'navigate' && r.outcome === 'success') {
|
|
62
|
+
this.history.push({ url: String(action.args.url ?? ''), at: new Date().toISOString() });
|
|
63
|
+
}
|
|
64
|
+
return { actionId: action.id, ...r, durationMs: Date.now() - started };
|
|
65
|
+
}
|
|
66
|
+
async snapshot() {
|
|
67
|
+
const obs = await this.observe();
|
|
68
|
+
return { environmentId: this.id, version: 1, takenAt: new Date().toISOString(), stateHash: stableHash(obs.state), payload: obs.state };
|
|
69
|
+
}
|
|
70
|
+
async restore() {
|
|
71
|
+
// navigation history restore requires the act bridge; recorded only
|
|
72
|
+
}
|
|
73
|
+
async evaluate() {
|
|
74
|
+
return { environmentId: this.id, metrics: { tabsReachable: this.tabs.length, bridgeConfigured: this.opts.act ? 1 : 0 } };
|
|
75
|
+
}
|
|
76
|
+
async close() {
|
|
77
|
+
this.tabs = [];
|
|
78
|
+
}
|
|
79
|
+
async endpointUp() {
|
|
80
|
+
try {
|
|
81
|
+
await fetch(this.base + '/json/version', { signal: AbortSignal.timeout(1_500) });
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export class DesktopEnvironment {
|
|
90
|
+
opts;
|
|
91
|
+
id = 'desktop';
|
|
92
|
+
version = '1.0.0';
|
|
93
|
+
windows = [];
|
|
94
|
+
constructor(opts = {}) {
|
|
95
|
+
this.opts = opts;
|
|
96
|
+
}
|
|
97
|
+
async capabilities() {
|
|
98
|
+
return [
|
|
99
|
+
{ kind: 'observe', detail: 'window list (title + pid) via OS process enumeration' },
|
|
100
|
+
{ kind: 'act', detail: 'click/type/hotkey via host automation trio', limitation: this.opts.act ? undefined : 'automation bridge not configured — actions return E_NO_AUTOMATION_BRIDGE' },
|
|
101
|
+
{ kind: 'display', detail: 'screenshots through the host bridge only' },
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
async reset() {
|
|
105
|
+
return this.observe();
|
|
106
|
+
}
|
|
107
|
+
async observe() {
|
|
108
|
+
if (process.platform === 'win32') {
|
|
109
|
+
const r = await runShell('powershell -NoProfile -Command "Get-Process | Where-Object { $_.MainWindowTitle } | Select-Object Id,MainWindowTitle | ConvertTo-Json -Compress"', process.cwd(), 10_000);
|
|
110
|
+
try {
|
|
111
|
+
const parsed = JSON.parse(r.stdout || '[]');
|
|
112
|
+
this.windows = (Array.isArray(parsed) ? parsed : [parsed]).map((p) => ({ pid: p.Id, title: p.MainWindowTitle }));
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
this.windows = [];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
const r = await runShell('ps -eo pid,comm --no-headers', process.cwd(), 10_000).catch(() => ({ code: 1, stdout: '', stderr: '' }));
|
|
120
|
+
this.windows = r.stdout.split('\n').filter((l) => l.trim()).slice(0, 40).map((l) => {
|
|
121
|
+
const [pid, ...rest] = l.trim().split(/\s+/);
|
|
122
|
+
return { pid: Number(pid) || 0, title: rest.join(' ') };
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
environmentId: this.id,
|
|
127
|
+
timestamp: new Date().toISOString(),
|
|
128
|
+
state: { windows: this.windows },
|
|
129
|
+
availableActions: this.actionSpecs(),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
actionSpecs() {
|
|
133
|
+
return [
|
|
134
|
+
{ id: 'click', type: 'desktop-click', description: 'click at coordinates', argsSchema: { x: 'number', y: 'number' }, cost: 1 },
|
|
135
|
+
{ id: 'type', type: 'desktop-type', description: 'type text', argsSchema: { text: 'string' }, cost: 1 },
|
|
136
|
+
{ id: 'hotkey', type: 'desktop-hotkey', description: 'press a hotkey combo', argsSchema: { combo: 'string' }, cost: 1 },
|
|
137
|
+
];
|
|
138
|
+
}
|
|
139
|
+
async act(action) {
|
|
140
|
+
const started = Date.now();
|
|
141
|
+
if (!this.opts.act) {
|
|
142
|
+
return { actionId: action.id, outcome: 'failure', error: { code: 'E_NO_AUTOMATION_BRIDGE', message: 'desktop act requires the host automation trio bridge (DesktopEnvOptions.act)' }, durationMs: Date.now() - started };
|
|
143
|
+
}
|
|
144
|
+
const r = await this.opts.act(action);
|
|
145
|
+
return { actionId: action.id, ...r, durationMs: Date.now() - started };
|
|
146
|
+
}
|
|
147
|
+
async snapshot() {
|
|
148
|
+
return { environmentId: this.id, version: 1, takenAt: new Date().toISOString(), stateHash: stableHash(this.windows), payload: { windows: this.windows } };
|
|
149
|
+
}
|
|
150
|
+
async restore() {
|
|
151
|
+
// desktop UI state is not restorable; divergence audit only
|
|
152
|
+
}
|
|
153
|
+
async evaluate() {
|
|
154
|
+
return { environmentId: this.id, metrics: { windowsVisible: this.windows.length, bridgeConfigured: this.opts.act ? 1 : 0 } };
|
|
155
|
+
}
|
|
156
|
+
async close() {
|
|
157
|
+
this.windows = [];
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
export class Arc3Environment {
|
|
161
|
+
opts;
|
|
162
|
+
id = 'arc3';
|
|
163
|
+
version = '0.1.0';
|
|
164
|
+
constructor(opts = {}) {
|
|
165
|
+
this.opts = opts;
|
|
166
|
+
}
|
|
167
|
+
async capabilities() {
|
|
168
|
+
const ready = Boolean(this.opts.taskBundle && this.opts.interact);
|
|
169
|
+
return [
|
|
170
|
+
{ kind: 'observe', detail: 'ARC task grid state via SDK', limitation: ready ? undefined : 'ARC-AGI-3 SDK task bundle + interact bridge required' },
|
|
171
|
+
{ kind: 'act', detail: 'ARC actions mapped through the SDK', limitation: ready ? undefined : 'not configured — adapter refuses to fake runs' },
|
|
172
|
+
{ kind: 'evaluate', detail: 'ARC official scoring via SDK replay' },
|
|
173
|
+
];
|
|
174
|
+
}
|
|
175
|
+
refuse() {
|
|
176
|
+
throw new Error('ARC3 adapter is a skeleton: configure Arc3EnvOptions.taskBundle + interact (ARC-001); no fake runs');
|
|
177
|
+
}
|
|
178
|
+
async reset() {
|
|
179
|
+
if (!this.opts.interact)
|
|
180
|
+
return this.refuse();
|
|
181
|
+
const r = await this.opts.interact({ id: 'reset', type: 'reset', args: {} });
|
|
182
|
+
return {
|
|
183
|
+
environmentId: this.id,
|
|
184
|
+
timestamp: new Date().toISOString(),
|
|
185
|
+
state: r.observation ?? null,
|
|
186
|
+
availableActions: [
|
|
187
|
+
{ id: 'submit', type: 'submit', description: 'submit an answer grid', argsSchema: { grid: 'unknown' }, cost: 5 },
|
|
188
|
+
],
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
async observe() {
|
|
192
|
+
if (!this.opts.interact)
|
|
193
|
+
return this.refuse();
|
|
194
|
+
const r = await this.opts.interact({ id: 'observe', type: 'observe', args: {} });
|
|
195
|
+
return {
|
|
196
|
+
environmentId: this.id,
|
|
197
|
+
timestamp: new Date().toISOString(),
|
|
198
|
+
state: r.observation ?? null,
|
|
199
|
+
availableActions: [{ id: 'submit', type: 'submit', description: 'submit an answer grid', argsSchema: { grid: 'unknown' }, cost: 5 }],
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
async act(action) {
|
|
203
|
+
if (!this.opts.interact)
|
|
204
|
+
return this.refuse();
|
|
205
|
+
const r = await this.opts.interact(action);
|
|
206
|
+
return { actionId: action.id, outcome: r.outcome, output: r.observation };
|
|
207
|
+
}
|
|
208
|
+
async snapshot() {
|
|
209
|
+
if (!this.opts.interact)
|
|
210
|
+
return this.refuse();
|
|
211
|
+
return { environmentId: this.id, version: 1, takenAt: new Date().toISOString(), stateHash: 'arc3-pending', payload: null };
|
|
212
|
+
}
|
|
213
|
+
async restore() {
|
|
214
|
+
this.refuse();
|
|
215
|
+
}
|
|
216
|
+
async evaluate() {
|
|
217
|
+
if (!this.opts.interact)
|
|
218
|
+
return this.refuse();
|
|
219
|
+
return { environmentId: this.id, metrics: {} };
|
|
220
|
+
}
|
|
221
|
+
async close() {
|
|
222
|
+
/* nothing held */
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/environments - HarmonyOS environment (ENV-020/021/022)
|
|
3
|
+
*
|
|
4
|
+
* Bridges the Cognitive OS protocol onto hdc (HarmonyOS Device Connector).
|
|
5
|
+
* observe = device list + front app; act = shell/aa start/install (via host
|
|
6
|
+
* hdc); evaluate = app-running + recent hilog error count. Device WRITE
|
|
7
|
+
* actions are marked irreversible — the goal-layer approval gate applies.
|
|
8
|
+
*/
|
|
9
|
+
import type { Environment, Observation, Action, ActionResult, Snapshot, EnvironmentScore, Capability } from '@hmharness/cognitive';
|
|
10
|
+
export interface HarmonyOsEnvOptions {
|
|
11
|
+
/** hdc binary; defaults to plain "hdc" from PATH */
|
|
12
|
+
hdc?: string;
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
}
|
|
15
|
+
export declare class HarmonyOsEnvironment implements Environment {
|
|
16
|
+
id: string;
|
|
17
|
+
version: string;
|
|
18
|
+
private readonly hdc;
|
|
19
|
+
private readonly timeoutMs;
|
|
20
|
+
private lastStates;
|
|
21
|
+
/** restore attempts are kept OUT of the observed state — restore() must
|
|
22
|
+
* never mutate what observe() reports, or snapshot hashes always drift */
|
|
23
|
+
private restoreAttempts;
|
|
24
|
+
constructor(opts?: HarmonyOsEnvOptions);
|
|
25
|
+
capabilities(): Promise<Capability[]>;
|
|
26
|
+
reset(): Promise<Observation>;
|
|
27
|
+
observe(): Promise<Observation>;
|
|
28
|
+
private actionSpecs;
|
|
29
|
+
act(action: Action): Promise<ActionResult>;
|
|
30
|
+
snapshot(): Promise<Snapshot>;
|
|
31
|
+
restore(snapshot: Snapshot): Promise<void>;
|
|
32
|
+
evaluate(): Promise<EnvironmentScore>;
|
|
33
|
+
close(): Promise<void>;
|
|
34
|
+
private hdcOk;
|
|
35
|
+
private hdcRun;
|
|
36
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { stableHash } from '@hmharness/cognitive';
|
|
2
|
+
import { runShell } from "./terminal.js";
|
|
3
|
+
export class HarmonyOsEnvironment {
|
|
4
|
+
id = 'harmonyos';
|
|
5
|
+
version = '1.0.0';
|
|
6
|
+
hdc;
|
|
7
|
+
timeoutMs;
|
|
8
|
+
lastStates = [];
|
|
9
|
+
/** restore attempts are kept OUT of the observed state — restore() must
|
|
10
|
+
* never mutate what observe() reports, or snapshot hashes always drift */
|
|
11
|
+
restoreAttempts = [];
|
|
12
|
+
constructor(opts = {}) {
|
|
13
|
+
this.hdc = opts.hdc ?? 'hdc';
|
|
14
|
+
this.timeoutMs = opts.timeoutMs ?? 20_000;
|
|
15
|
+
}
|
|
16
|
+
async capabilities() {
|
|
17
|
+
const reachable = await this.hdcOk();
|
|
18
|
+
return [
|
|
19
|
+
{ kind: 'observe', detail: 'device list + front application' },
|
|
20
|
+
{ kind: 'act', detail: 'hdc shell / aa start / install', limitation: reachable ? undefined : 'hdc not reachable' },
|
|
21
|
+
{ kind: 'evaluate', detail: 'front-app check + hilog error count' },
|
|
22
|
+
{ kind: 'process', detail: `hdc invocations, ${this.timeoutMs}ms timeout` },
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
async reset() {
|
|
26
|
+
this.lastStates = [];
|
|
27
|
+
return this.observe();
|
|
28
|
+
}
|
|
29
|
+
async observe() {
|
|
30
|
+
const r = await this.hdcRun('list targets');
|
|
31
|
+
const devices = r.stdout.split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !l.includes('[Empty]'));
|
|
32
|
+
let frontApp;
|
|
33
|
+
if (devices.length > 0) {
|
|
34
|
+
const aa = await this.hdcRun('shell aa dump -l').catch(() => ({ code: 1, stdout: '', stderr: '' }));
|
|
35
|
+
const m = aa.stdout.match(/app\s+:\s*\[?([a-zA-Z0-9_.]+)/i);
|
|
36
|
+
frontApp = m?.[1];
|
|
37
|
+
}
|
|
38
|
+
// observe() is READ-ONLY: history records state CHANGES (act/reset),
|
|
39
|
+
// never observations — otherwise every snapshot hash drifts
|
|
40
|
+
return {
|
|
41
|
+
environmentId: this.id,
|
|
42
|
+
timestamp: new Date().toISOString(),
|
|
43
|
+
state: { devices, frontApp, history: this.lastStates.slice(-3) },
|
|
44
|
+
availableActions: this.actionSpecs(devices.length > 0),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
actionSpecs(ready) {
|
|
48
|
+
const limitation = ready ? undefined : 'no connected device';
|
|
49
|
+
return [
|
|
50
|
+
{ id: 'shell', type: 'hdc-shell', description: 'run a shell command on the device', argsSchema: { cmd: 'string' }, cost: 2 },
|
|
51
|
+
{ id: 'aa-start', type: 'hdc-aa-start', description: 'start an ability', argsSchema: { bundle: 'string', ability: 'string' }, cost: 3, irreversible: true },
|
|
52
|
+
{ id: 'install', type: 'hdc-install', description: 'install a hap on the device', argsSchema: { path: 'string' }, cost: 8, irreversible: true },
|
|
53
|
+
].map((s) => ({ ...s, description: limitation ? `${s.description} (unavailable: ${limitation})` : s.description }));
|
|
54
|
+
}
|
|
55
|
+
async act(action) {
|
|
56
|
+
const started = Date.now();
|
|
57
|
+
const probe = await this.hdcRun('list targets');
|
|
58
|
+
const targets = probe.stdout.split(/\r?\n/).filter((l) => l.trim() && !l.includes('[Empty]'));
|
|
59
|
+
this.lastStates.push({ at: new Date().toISOString(), devices: targets.length });
|
|
60
|
+
if (targets.length === 0) {
|
|
61
|
+
return { actionId: action.id, outcome: 'failure', error: { code: 'E_NO_DEVICE', message: 'no HarmonyOS device connected' }, durationMs: Date.now() - started };
|
|
62
|
+
}
|
|
63
|
+
switch (action.type) {
|
|
64
|
+
case 'hdc-shell': {
|
|
65
|
+
const cmd = String(action.args.cmd ?? '');
|
|
66
|
+
if (!cmd.trim())
|
|
67
|
+
return { actionId: action.id, outcome: 'failure', error: { code: 'E_ARGS', message: 'cmd required' }, durationMs: Date.now() - started };
|
|
68
|
+
const r = await this.hdcRun(`shell ${cmd}`);
|
|
69
|
+
return { actionId: action.id, outcome: r.code === 0 ? 'success' : 'failure', output: r.stdout.slice(0, 2_000), error: r.code === 0 ? undefined : { code: 'E_HDC', message: r.stderr.slice(0, 300) }, durationMs: Date.now() - started, cost: 2 };
|
|
70
|
+
}
|
|
71
|
+
case 'hdc-aa-start': {
|
|
72
|
+
const bundle = String(action.args.bundle ?? '');
|
|
73
|
+
const ability = String(action.args.ability ?? '');
|
|
74
|
+
if (!bundle || !ability)
|
|
75
|
+
return { actionId: action.id, outcome: 'failure', error: { code: 'E_ARGS', message: 'bundle and ability required' }, durationMs: Date.now() - started };
|
|
76
|
+
const r = await this.hdcRun(`shell aa start -a ${ability} -b ${bundle}`);
|
|
77
|
+
return { actionId: action.id, outcome: r.code === 0 && /start ability successfully|success/i.test(r.stdout) ? 'success' : 'failure', output: r.stdout.slice(0, 1_000), error: r.code === 0 ? undefined : { code: 'E_AA', message: r.stderr.slice(0, 300) }, durationMs: Date.now() - started, cost: 3 };
|
|
78
|
+
}
|
|
79
|
+
case 'hdc-install': {
|
|
80
|
+
const path = String(action.args.path ?? '');
|
|
81
|
+
if (!path)
|
|
82
|
+
return { actionId: action.id, outcome: 'failure', error: { code: 'E_ARGS', message: 'path required' }, durationMs: Date.now() - started };
|
|
83
|
+
const r = await this.hdcRun(`install "${path}"`);
|
|
84
|
+
return { actionId: action.id, outcome: r.code === 0 && /success|install ok/i.test(r.stdout + r.stderr) ? 'success' : 'failure', output: (r.stdout + r.stderr).slice(0, 1_000), durationMs: Date.now() - started, cost: 8 };
|
|
85
|
+
}
|
|
86
|
+
default:
|
|
87
|
+
return { actionId: action.id, outcome: 'failure', error: { code: 'E_UNKNOWN_ACTION', message: `unknown action type ${action.type}` }, durationMs: Date.now() - started };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async snapshot() {
|
|
91
|
+
const obs = await this.observe();
|
|
92
|
+
return {
|
|
93
|
+
environmentId: this.id,
|
|
94
|
+
version: 1,
|
|
95
|
+
takenAt: new Date().toISOString(),
|
|
96
|
+
stateHash: stableHash(obs.state),
|
|
97
|
+
payload: obs.state,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async restore(snapshot) {
|
|
101
|
+
// device state is not snapshot-restorable; divergence audit only
|
|
102
|
+
this.restoreAttempts.push(snapshot.takenAt);
|
|
103
|
+
}
|
|
104
|
+
async evaluate() {
|
|
105
|
+
const obs = await this.observe();
|
|
106
|
+
const state = obs.state;
|
|
107
|
+
const metrics = {
|
|
108
|
+
devicesConnected: state.devices.length,
|
|
109
|
+
frontAppPresent: state.frontApp ? 1 : 0,
|
|
110
|
+
};
|
|
111
|
+
if (state.frontApp) {
|
|
112
|
+
const logs = await this.hdcRun('shell hilog -x -z 100').catch(() => ({ code: 1, stdout: '', stderr: '' }));
|
|
113
|
+
metrics.recentErrors = (logs.stdout.match(/\b(F|E)\//g) ?? []).length;
|
|
114
|
+
}
|
|
115
|
+
return { environmentId: this.id, metrics };
|
|
116
|
+
}
|
|
117
|
+
async close() {
|
|
118
|
+
this.lastStates = [];
|
|
119
|
+
}
|
|
120
|
+
hdcOk() {
|
|
121
|
+
return this.hdcRun('list targets').then((r) => r.code === 0).catch(() => false);
|
|
122
|
+
}
|
|
123
|
+
hdcRun(args) {
|
|
124
|
+
return runShell(`${this.hdc} ${args}`, process.cwd(), this.timeoutMs);
|
|
125
|
+
}
|
|
126
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/environments - Cognitive OS environment adapters
|
|
3
|
+
*
|
|
4
|
+
* Terminal is fully native; HarmonyOS bridges hdc; Browser observes via CDP
|
|
5
|
+
* HTTP with host-bridged actions; Desktop observes windows with host-bridged
|
|
6
|
+
* actions; ARC3 is an honest skeleton awaiting the ARC SDK. All adapters
|
|
7
|
+
* must pass cognitive's EnvironmentRegistry.conformance().
|
|
8
|
+
*/
|
|
9
|
+
export { TerminalEnvironment, runShell } from './terminal.js';
|
|
10
|
+
export type { TerminalEnvOptions } from './terminal.js';
|
|
11
|
+
export { HarmonyOsEnvironment } from './harmonyos.js';
|
|
12
|
+
export type { HarmonyOsEnvOptions } from './harmonyos.js';
|
|
13
|
+
export { BrowserEnvironment, DesktopEnvironment, Arc3Environment } from './adapters.js';
|
|
14
|
+
export type { BrowserEnvOptions, DesktopEnvOptions, Arc3EnvOptions } from './adapters.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/environments - Cognitive OS environment adapters
|
|
3
|
+
*
|
|
4
|
+
* Terminal is fully native; HarmonyOS bridges hdc; Browser observes via CDP
|
|
5
|
+
* HTTP with host-bridged actions; Desktop observes windows with host-bridged
|
|
6
|
+
* actions; ARC3 is an honest skeleton awaiting the ARC SDK. All adapters
|
|
7
|
+
* must pass cognitive's EnvironmentRegistry.conformance().
|
|
8
|
+
*/
|
|
9
|
+
export { TerminalEnvironment, runShell } from "./terminal.js";
|
|
10
|
+
export { HarmonyOsEnvironment } from "./harmonyos.js";
|
|
11
|
+
export { BrowserEnvironment, DesktopEnvironment, Arc3Environment } from "./adapters.js";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Environment, Observation, Action, ActionResult, Snapshot, EnvironmentScore, Capability, ResetOptions } from '@hmharness/cognitive';
|
|
2
|
+
export interface TerminalEnvOptions {
|
|
3
|
+
/** working root; created when missing on reset() */
|
|
4
|
+
workspaceDir: string;
|
|
5
|
+
timeoutMs?: number;
|
|
6
|
+
/** max tracked files in observations/snapshots */
|
|
7
|
+
maxTrackedFiles?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare class TerminalEnvironment implements Environment {
|
|
10
|
+
private opts;
|
|
11
|
+
id: string;
|
|
12
|
+
version: string;
|
|
13
|
+
private cwd;
|
|
14
|
+
private lastOutputs;
|
|
15
|
+
private actionCounter;
|
|
16
|
+
private readonly timeoutMs;
|
|
17
|
+
private readonly maxFiles;
|
|
18
|
+
constructor(opts: TerminalEnvOptions);
|
|
19
|
+
capabilities(): Promise<Capability[]>;
|
|
20
|
+
reset(opts?: ResetOptions): Promise<Observation>;
|
|
21
|
+
observe(): Promise<Observation>;
|
|
22
|
+
private actionSpecs;
|
|
23
|
+
act(action: Action): Promise<ActionResult>;
|
|
24
|
+
snapshot(): Promise<Snapshot>;
|
|
25
|
+
/** ENV-004. Restore = return to recorded cwd and confirm surviving files;
|
|
26
|
+
* files deleted since the snapshot are REPORTED, not resurrected. */
|
|
27
|
+
restore(snapshot: Snapshot): Promise<void>;
|
|
28
|
+
evaluate(): Promise<EnvironmentScore>;
|
|
29
|
+
close(): Promise<void>;
|
|
30
|
+
private guard;
|
|
31
|
+
private inWorkspace;
|
|
32
|
+
private trackFiles;
|
|
33
|
+
private fail;
|
|
34
|
+
}
|
|
35
|
+
export declare function runShell(cmd: string, cwd: string, timeoutMs: number): Promise<{
|
|
36
|
+
code: number;
|
|
37
|
+
stdout: string;
|
|
38
|
+
stderr: string;
|
|
39
|
+
}>;
|
package/dist/terminal.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/environments - Terminal environment (ENV-017/018/019)
|
|
3
|
+
*
|
|
4
|
+
* The native environment: observe = cwd/file tree/processes/output tail;
|
|
5
|
+
* act = run command / write file; evaluate = tests/build/git probes;
|
|
6
|
+
* snapshot/restore = cwd + tracked-file digests (restoration of DELETED
|
|
7
|
+
* files is honestly out of scope and reported, not faked).
|
|
8
|
+
*
|
|
9
|
+
* Windows-first (the host platform) with POSIX command mapping; commands
|
|
10
|
+
* run through cmd/powershell on win32, /bin/sh elsewhere. Every action
|
|
11
|
+
* carries a timeout; every result carries duration + structured errors.
|
|
12
|
+
*/
|
|
13
|
+
import { spawn } from 'node:child_process';
|
|
14
|
+
import { readdir, writeFile, stat, rm, mkdir } from 'node:fs/promises';
|
|
15
|
+
import { join, relative, isAbsolute, resolve } from 'node:path';
|
|
16
|
+
import { existsSync } from 'node:fs';
|
|
17
|
+
import { stableHash } from '@hmharness/cognitive';
|
|
18
|
+
export class TerminalEnvironment {
|
|
19
|
+
opts;
|
|
20
|
+
id = 'terminal';
|
|
21
|
+
version = '1.0.0';
|
|
22
|
+
cwd = '';
|
|
23
|
+
lastOutputs = [];
|
|
24
|
+
actionCounter = 0;
|
|
25
|
+
timeoutMs;
|
|
26
|
+
maxFiles;
|
|
27
|
+
constructor(opts) {
|
|
28
|
+
this.opts = opts;
|
|
29
|
+
this.timeoutMs = opts.timeoutMs ?? 30_000;
|
|
30
|
+
this.maxFiles = opts.maxTrackedFiles ?? 400;
|
|
31
|
+
}
|
|
32
|
+
async capabilities() {
|
|
33
|
+
return [
|
|
34
|
+
{ kind: 'observe', detail: 'cwd, tracked file digests, recent outputs' },
|
|
35
|
+
{ kind: 'act', detail: 'command (shell), writeFile, read briefly' },
|
|
36
|
+
{ kind: 'evaluate', detail: 'exit-code based probes: test/build/git' },
|
|
37
|
+
{ kind: 'snapshot', detail: 'cwd + file digests (deletions not restorable)' },
|
|
38
|
+
{ kind: 'process', detail: `shell commands, ${this.timeoutMs}ms timeout each` },
|
|
39
|
+
{ kind: 'filesystem', detail: `limited to ${this.opts.workspaceDir}` },
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
async reset(opts) {
|
|
43
|
+
this.cwd = opts?.workspaceDir ?? this.opts.workspaceDir;
|
|
44
|
+
await mkdir(this.cwd, { recursive: true });
|
|
45
|
+
this.lastOutputs = [];
|
|
46
|
+
this.actionCounter = 0;
|
|
47
|
+
return this.observe();
|
|
48
|
+
}
|
|
49
|
+
async observe() {
|
|
50
|
+
const files = await this.trackFiles();
|
|
51
|
+
return {
|
|
52
|
+
environmentId: this.id,
|
|
53
|
+
timestamp: new Date().toISOString(),
|
|
54
|
+
state: {
|
|
55
|
+
cwd: this.cwd,
|
|
56
|
+
files,
|
|
57
|
+
recentOutputs: this.lastOutputs.slice(-3),
|
|
58
|
+
actionsTaken: this.actionCounter,
|
|
59
|
+
},
|
|
60
|
+
availableActions: this.actionSpecs(),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
actionSpecs() {
|
|
64
|
+
return [
|
|
65
|
+
{ id: 'command', type: 'command', description: 'run a shell command in the workspace', argsSchema: { cmd: 'string' }, cost: 3 },
|
|
66
|
+
{ id: 'writeFile', type: 'writeFile', description: 'create/overwrite a file in the workspace', argsSchema: { path: 'string', content: 'string' }, cost: 1 },
|
|
67
|
+
{ id: 'deleteFile', type: 'deleteFile', description: 'delete a file in the workspace (irreversible)', argsSchema: { path: 'string' }, cost: 1, irreversible: true },
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
async act(action) {
|
|
71
|
+
this.actionCounter += 1;
|
|
72
|
+
const started = Date.now();
|
|
73
|
+
const guard = this.guard(action);
|
|
74
|
+
if (guard)
|
|
75
|
+
return { ...guard, actionId: action.id, durationMs: Date.now() - started };
|
|
76
|
+
try {
|
|
77
|
+
switch (action.type) {
|
|
78
|
+
case 'command': {
|
|
79
|
+
const cmd = String(action.args.cmd ?? '');
|
|
80
|
+
if (!cmd.trim())
|
|
81
|
+
return this.fail(action.id, 'E_ARGS', 'command.args.cmd required', started);
|
|
82
|
+
if (/(?:^|[|&;])\s*(?:rm\s+-rf\s+\/|format\s+[a-z]:|del\s+\/[fqs])/im.test(cmd)) {
|
|
83
|
+
return this.fail(action.id, 'E_UNSAFE', 'destructive command pattern rejected', started);
|
|
84
|
+
}
|
|
85
|
+
const r = await runShell(cmd, this.cwd, this.timeoutMs);
|
|
86
|
+
this.lastOutputs.push(`$ ${cmd}\n${(r.stdout + r.stderr).slice(0, 2_000)}`);
|
|
87
|
+
return {
|
|
88
|
+
actionId: action.id,
|
|
89
|
+
outcome: r.code === 0 ? 'success' : 'failure',
|
|
90
|
+
error: r.code === 0 ? undefined : { code: 'E_EXIT', message: `exit ${r.code}: ${r.stderr.slice(0, 300)}` },
|
|
91
|
+
output: { code: r.code, stdout: r.stdout.slice(0, 4_000), stderr: r.stderr.slice(0, 1_000) },
|
|
92
|
+
durationMs: Date.now() - started,
|
|
93
|
+
cost: 3,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
case 'writeFile': {
|
|
97
|
+
const p = String(action.args.path ?? '');
|
|
98
|
+
if (!p)
|
|
99
|
+
return this.fail(action.id, 'E_ARGS', 'writeFile.args.path required', started);
|
|
100
|
+
const abs = this.inWorkspace(p);
|
|
101
|
+
if (!abs)
|
|
102
|
+
return this.fail(action.id, 'E_SCOPE', `path escapes workspace: ${p}`, started);
|
|
103
|
+
await mkdir(dirnameOf(abs), { recursive: true });
|
|
104
|
+
await writeFile(abs, String(action.args.content ?? ''), 'utf8');
|
|
105
|
+
return { actionId: action.id, outcome: 'success', output: { path: p, bytes: String(action.args.content ?? '').length }, durationMs: Date.now() - started, cost: 1 };
|
|
106
|
+
}
|
|
107
|
+
case 'deleteFile': {
|
|
108
|
+
const p = String(action.args.path ?? '');
|
|
109
|
+
const abs = this.inWorkspace(p);
|
|
110
|
+
if (!abs || !existsSync(abs))
|
|
111
|
+
return this.fail(action.id, 'E_SCOPE', `path not in workspace or missing: ${p}`, started);
|
|
112
|
+
await rm(abs);
|
|
113
|
+
return { actionId: action.id, outcome: 'success', durationMs: Date.now() - started, cost: 1 };
|
|
114
|
+
}
|
|
115
|
+
default:
|
|
116
|
+
return this.fail(action.id, 'E_UNKNOWN_ACTION', `unknown action type ${action.type}`, started);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
return this.fail(action.id, 'E_IO', err instanceof Error ? err.message : String(err), started);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async snapshot() {
|
|
124
|
+
const files = await this.trackFiles();
|
|
125
|
+
const payload = { cwd: this.cwd, files, outputs: this.lastOutputs.slice(-5) };
|
|
126
|
+
return {
|
|
127
|
+
environmentId: this.id,
|
|
128
|
+
version: 1,
|
|
129
|
+
takenAt: new Date().toISOString(),
|
|
130
|
+
stateHash: stableHash(files),
|
|
131
|
+
payload,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/** ENV-004. Restore = return to recorded cwd and confirm surviving files;
|
|
135
|
+
* files deleted since the snapshot are REPORTED, not resurrected. */
|
|
136
|
+
async restore(snapshot) {
|
|
137
|
+
const payload = snapshot.payload;
|
|
138
|
+
this.cwd = payload.cwd;
|
|
139
|
+
const missing = payload.files.filter((f) => !existsSync(join(this.cwd, f.path)));
|
|
140
|
+
if (missing.length > 0) {
|
|
141
|
+
this.lastOutputs.push(`[restore] ${missing.length} file(s) deleted since snapshot cannot be restored: ${missing.slice(0, 5).map((m) => m.path).join(', ')}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async evaluate() {
|
|
145
|
+
if (!this.cwd)
|
|
146
|
+
return { environmentId: this.id, metrics: { ready: 0 }, details: 'not reset' };
|
|
147
|
+
const probes = [
|
|
148
|
+
['testProbe', 'if exist package.json (node --test --pass-with-no-tests 2>nul) else (exit 0)'],
|
|
149
|
+
['gitProbe', 'git status --porcelain'],
|
|
150
|
+
];
|
|
151
|
+
const metrics = { ready: 1, trackedFiles: (await this.trackFiles()).length };
|
|
152
|
+
for (const [name, cmd] of probes) {
|
|
153
|
+
const r = await runShell(cmd, this.cwd, 15_000).catch(() => ({ code: 1, stdout: '', stderr: 'probe failed' }));
|
|
154
|
+
metrics[name] = r.code === 0 ? 1 : 0;
|
|
155
|
+
}
|
|
156
|
+
return { environmentId: this.id, metrics };
|
|
157
|
+
}
|
|
158
|
+
async close() {
|
|
159
|
+
this.lastOutputs = [];
|
|
160
|
+
}
|
|
161
|
+
guard(_action) {
|
|
162
|
+
if (!this.cwd)
|
|
163
|
+
return this.fail('early', 'E_NOT_RESET', 'call reset() before act()', Date.now());
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
inWorkspace(p) {
|
|
167
|
+
const root = resolve(this.opts.workspaceDir);
|
|
168
|
+
const abs = isAbsolute(p) ? resolve(p) : resolve(this.cwd, p);
|
|
169
|
+
const rel = relative(root, abs);
|
|
170
|
+
return rel.startsWith('..') ? null : abs;
|
|
171
|
+
}
|
|
172
|
+
async trackFiles() {
|
|
173
|
+
if (!this.cwd || !existsSync(this.cwd))
|
|
174
|
+
return [];
|
|
175
|
+
const out = [];
|
|
176
|
+
await walk(this.cwd, this.cwd, out, this.maxFiles);
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
fail(actionId, code, message, started) {
|
|
180
|
+
return { actionId, outcome: 'failure', error: { code, message }, durationMs: Date.now() - started };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function dirnameOf(p) {
|
|
184
|
+
const i = Math.max(p.lastIndexOf('/'), p.lastIndexOf('\\'));
|
|
185
|
+
return i > 0 ? p.slice(0, i) : '.';
|
|
186
|
+
}
|
|
187
|
+
async function walk(root, dir, out, cap) {
|
|
188
|
+
if (out.length >= cap)
|
|
189
|
+
return;
|
|
190
|
+
let entries;
|
|
191
|
+
try {
|
|
192
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
for (const e of entries) {
|
|
198
|
+
if (e.name === 'node_modules' || e.name === '.git')
|
|
199
|
+
continue;
|
|
200
|
+
const full = join(dir, e.name);
|
|
201
|
+
if (e.isDirectory())
|
|
202
|
+
await walk(root, full, out, cap);
|
|
203
|
+
else {
|
|
204
|
+
try {
|
|
205
|
+
const s = await stat(full);
|
|
206
|
+
out.push({ path: relative(root, full).replace(/\\/g, '/'), size: s.size });
|
|
207
|
+
}
|
|
208
|
+
catch { /* raced */ }
|
|
209
|
+
}
|
|
210
|
+
if (out.length >= cap)
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
export function runShell(cmd, cwd, timeoutMs) {
|
|
215
|
+
return new Promise((resolveP) => {
|
|
216
|
+
const isWin = process.platform === 'win32';
|
|
217
|
+
const child = isWin
|
|
218
|
+
? spawn('cmd.exe', ['/d', '/s', '/c', cmd], { cwd, windowsHide: true })
|
|
219
|
+
: spawn('/bin/sh', ['-c', cmd], { cwd });
|
|
220
|
+
let stdout = '';
|
|
221
|
+
let stderr = '';
|
|
222
|
+
let settled = false;
|
|
223
|
+
const timer = setTimeout(() => {
|
|
224
|
+
if (!settled) {
|
|
225
|
+
settled = true;
|
|
226
|
+
child.kill('SIGKILL');
|
|
227
|
+
resolveP({ code: 124, stdout, stderr: stderr + `\n[timeout after ${timeoutMs}ms]` });
|
|
228
|
+
}
|
|
229
|
+
}, timeoutMs);
|
|
230
|
+
child.stdout?.on('data', (d) => (stdout += d.toString()));
|
|
231
|
+
child.stderr?.on('data', (d) => (stderr += d.toString()));
|
|
232
|
+
child.on('error', (err) => {
|
|
233
|
+
if (settled)
|
|
234
|
+
return;
|
|
235
|
+
settled = true;
|
|
236
|
+
clearTimeout(timer);
|
|
237
|
+
resolveP({ code: 127, stdout, stderr: String(err) });
|
|
238
|
+
});
|
|
239
|
+
child.on('close', (code) => {
|
|
240
|
+
if (settled)
|
|
241
|
+
return;
|
|
242
|
+
settled = true;
|
|
243
|
+
clearTimeout(timer);
|
|
244
|
+
resolveP({ code: code ?? 0, stdout, stderr });
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hmharness/environments",
|
|
3
|
+
"version": "0.19.0",
|
|
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
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -p tsconfig.build.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/swsgbl/hmharness.git"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@hmharness/kernel": "0.19.0",
|
|
30
|
+
"@hmharness/cognitive": "0.19.0"
|
|
31
|
+
}
|
|
32
|
+
}
|