edge-impulse-linux 1.26.0 → 1.26.1
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/README.md +7 -3
- package/build/cli/linux/runner-downloader.js +24 -1
- package/build/cli/linux/runner-downloader.js.map +1 -1
- package/build/cli/linux/runner-utils.d.ts +5 -0
- package/build/cli/linux/runner-utils.js +6 -1
- package/build/cli/linux/runner-utils.js.map +1 -1
- package/build/cli/linux/runner.js +244 -65
- package/build/cli/linux/runner.js.map +1 -1
- package/build/library/classifier/audio-classifier.d.ts +12 -3
- package/build/library/classifier/audio-classifier.js +49 -39
- package/build/library/classifier/audio-classifier.js.map +1 -1
- package/build/library/classifier/libcwrapper.d.ts +7 -1
- package/build/library/classifier/libcwrapper.js +10 -2
- package/build/library/classifier/libcwrapper.js.map +1 -1
- package/build/library/classifier/linux-impulse-runner.d.ts +28 -0
- package/build/library/classifier/linux-impulse-runner.js +253 -55
- package/build/library/classifier/linux-impulse-runner.js.map +1 -1
- package/build/library/sensors/gstreamer.js +38 -5
- package/build/library/sensors/gstreamer.js.map +1 -1
- package/build/library/sensors/recorder.js +61 -23
- package/build/library/sensors/recorder.js.map +1 -1
- package/build/sdk/studio/sdk/model/device.d.ts +9 -0
- package/build/sdk/studio/sdk/model/device.js.map +1 -1
- package/build/sdk/studio/sdk/model/deviceDeploymentEvent.d.ts +9 -0
- package/build/sdk/studio/sdk/model/deviceDeploymentEvent.js.map +1 -1
- package/build/sdk/studio/sdk/model/deviceWorkload.d.ts +12 -0
- package/build/sdk/studio/sdk/model/deviceWorkload.js +4 -0
- package/build/sdk/studio/sdk/model/deviceWorkload.js.map +1 -0
- package/build/sdk/studio/sdk/model/deviceWorkloadModel.d.ts +13 -0
- package/build/sdk/studio/sdk/model/deviceWorkloadModel.js +4 -0
- package/build/sdk/studio/sdk/model/deviceWorkloadModel.js.map +1 -0
- package/build/sdk/studio/sdk/model/inferenceHistoryAggregate.d.ts +8 -0
- package/build/sdk/studio/sdk/model/models.d.ts +2 -0
- package/build/sdk/studio/sdk/model/models.js +2 -0
- package/build/sdk/studio/sdk/model/models.js.map +1 -1
- package/build/sdk/studio/sdk/model/monitoringEventContext.d.ts +8 -0
- package/package.json +4 -1
- package/test/agent-bundle-lifecycle.test.ts +445 -0
- package/test/agent-client.test.ts +246 -16
- package/test/agent-command-processor.test.ts +194 -0
- package/test/agent-runner-configuration.test.ts +110 -0
- package/test/agent-runner-integration.test.ts +330 -0
- package/test/utils/agent-runner-fixtures.ts +132 -0
- package/test/agent-runner-metadata.test.ts +0 -31
- package/test/sensor-selection.test.ts +0 -26
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Exercises the client used by an agent-managed runner to register, report inference events and
|
|
3
|
-
* receive commands over the device agent's Unix-socket HTTP API. Each test starts a small mock agent
|
|
4
|
-
* server on a temporary Unix socket, registers only the routes needed for that flow, and asserts the
|
|
5
|
-
* JSON requests sent by the client together with its handling of agent responses and shutdown.
|
|
6
|
-
*/
|
|
7
1
|
import assert from 'node:assert';
|
|
8
2
|
import fs from 'node:fs/promises';
|
|
9
3
|
import http from 'node:http';
|
|
@@ -35,6 +29,13 @@ interface MockAgentRoute {
|
|
|
35
29
|
response: MockAgentResponse | ((request: MockAgentRequest) => MockAgentResponse | Promise<MockAgentResponse>);
|
|
36
30
|
}
|
|
37
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Exercises `AgentClient`, the Unix-socket workload transport for registration, commands and inference
|
|
34
|
+
* events. Normal flows register canonical state, maintain the command stream, post results, apply input
|
|
35
|
+
* policy and send inline or spooled captures. Failure flows cover unavailable or malformed agent
|
|
36
|
+
* responses, registration recovery, command-result retry and expiry, spool failure, request timeout,
|
|
37
|
+
* stream reconnection and graceful shutdown while event requests are still pending.
|
|
38
|
+
*/
|
|
38
39
|
describe('AgentClient', () => {
|
|
39
40
|
let tempDir: string;
|
|
40
41
|
let socketPath: string;
|
|
@@ -162,8 +163,8 @@ describe('AgentClient', () => {
|
|
|
162
163
|
await rejected;
|
|
163
164
|
});
|
|
164
165
|
|
|
165
|
-
//
|
|
166
|
-
it('
|
|
166
|
+
// Rejects an unconfirmed model registration and reconnects with the last confirmed state.
|
|
167
|
+
it('restores the previous registration after an update transport failure', async () => {
|
|
167
168
|
let registrations = 0;
|
|
168
169
|
let commandStreams = 0;
|
|
169
170
|
let recoveredRegistration: InferenceRunnerRegistration | undefined;
|
|
@@ -220,10 +221,13 @@ describe('AgentClient', () => {
|
|
|
220
221
|
}],
|
|
221
222
|
} satisfies InferenceRunnerRegistration;
|
|
222
223
|
|
|
223
|
-
await
|
|
224
|
+
await assert.rejects(
|
|
225
|
+
client.updateRegistration(updatedRegistration),
|
|
226
|
+
/timed out after 20 ms/
|
|
227
|
+
);
|
|
224
228
|
await waitFor(() => registrations === 3 && commandStreams === 2);
|
|
225
229
|
|
|
226
|
-
assert.deepStrictEqual(recoveredRegistration,
|
|
230
|
+
assert.deepStrictEqual(recoveredRegistration, testRegistration());
|
|
227
231
|
await client.close();
|
|
228
232
|
});
|
|
229
233
|
|
|
@@ -309,8 +313,8 @@ describe('AgentClient', () => {
|
|
|
309
313
|
await client.close();
|
|
310
314
|
});
|
|
311
315
|
|
|
312
|
-
// Verifies
|
|
313
|
-
it('
|
|
316
|
+
// Verifies command handlers receive the validated, type-specific command.
|
|
317
|
+
it('delivers workload commands to their registered handler', async () => {
|
|
314
318
|
let delivered: Command | undefined;
|
|
315
319
|
server = await mockAgentServer(socketPath, [
|
|
316
320
|
{
|
|
@@ -329,23 +333,209 @@ describe('AgentClient', () => {
|
|
|
329
333
|
path: '/v1/commands',
|
|
330
334
|
response: {
|
|
331
335
|
headers: { 'content-type': 'text/event-stream' },
|
|
332
|
-
body: 'data: {"id":"
|
|
333
|
-
'"
|
|
336
|
+
body: 'data: {"id":"activate-1","type":"activateBundle",' +
|
|
337
|
+
'"bundle":{"kind":"model","path":"/models/version-2",' +
|
|
338
|
+
'"deploymentVersion":2}}\n\n',
|
|
334
339
|
keepOpen: true,
|
|
335
340
|
},
|
|
336
341
|
},
|
|
342
|
+
{
|
|
343
|
+
method: 'POST',
|
|
344
|
+
path: '/v1/commands/activate-1/result',
|
|
345
|
+
response: { body: {} },
|
|
346
|
+
},
|
|
337
347
|
]);
|
|
338
348
|
|
|
339
349
|
const client = new AgentClient(socketPath);
|
|
340
|
-
|
|
350
|
+
client.commands.on('activateBundle', command => {
|
|
341
351
|
delivered = command;
|
|
352
|
+
return Promise.resolve();
|
|
342
353
|
});
|
|
354
|
+
await client.connect(testRegistration());
|
|
343
355
|
await waitFor(() => delivered !== undefined);
|
|
344
356
|
|
|
345
|
-
assert.strictEqual(delivered?.type, '
|
|
357
|
+
assert.strictEqual(delivered?.type, 'activateBundle');
|
|
358
|
+
await client.close();
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
// An expired command result is final; it must not disrupt a healthy stream or repeat the action.
|
|
362
|
+
it('does not reconnect when the agent refuses an expired command result', async () => {
|
|
363
|
+
let registrations = 0;
|
|
364
|
+
let commandStreams = 0;
|
|
365
|
+
let results = 0;
|
|
366
|
+
server = await mockAgentServer(socketPath, [
|
|
367
|
+
{
|
|
368
|
+
method: 'POST',
|
|
369
|
+
path: '/v1/register',
|
|
370
|
+
response: () => {
|
|
371
|
+
registrations++;
|
|
372
|
+
return {
|
|
373
|
+
body: {
|
|
374
|
+
workloadId: 'vision',
|
|
375
|
+
inputPolicy: { mode: 'none' },
|
|
376
|
+
inlineSizeLimitBytes: 1024,
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
{
|
|
382
|
+
method: 'GET',
|
|
383
|
+
path: '/v1/commands',
|
|
384
|
+
response: () => {
|
|
385
|
+
commandStreams++;
|
|
386
|
+
return {
|
|
387
|
+
headers: { 'content-type': 'text/event-stream' },
|
|
388
|
+
body: 'data: {"id":"policy-expired","type":"updateInputPolicy",' +
|
|
389
|
+
'"inputPolicy":{"mode":"all"}}\n\n',
|
|
390
|
+
keepOpen: true,
|
|
391
|
+
};
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
method: 'POST',
|
|
396
|
+
path: '/v1/commands/policy-expired/result',
|
|
397
|
+
response: () => {
|
|
398
|
+
results++;
|
|
399
|
+
return {
|
|
400
|
+
statusCode: 404,
|
|
401
|
+
body: { code: 'unknown_command', message: 'The command has expired' },
|
|
402
|
+
};
|
|
403
|
+
},
|
|
404
|
+
},
|
|
405
|
+
]);
|
|
406
|
+
|
|
407
|
+
const client = new AgentClient(socketPath, { reconnectDelayMs: 10 });
|
|
408
|
+
await client.connect(testRegistration());
|
|
409
|
+
await waitFor(() => results === 1);
|
|
410
|
+
await client.commands.waitForIdle(100);
|
|
411
|
+
await new Promise(resolve => setTimeout(resolve, 30));
|
|
412
|
+
|
|
413
|
+
assert.strictEqual(registrations, 1);
|
|
414
|
+
assert.strictEqual(commandStreams, 1);
|
|
415
|
+
await client.close();
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
// A request timeout response is ambiguous, so retry the identical result without applying the command twice.
|
|
419
|
+
it('retries a command result after HTTP 408', async () => {
|
|
420
|
+
let handlerCalls = 0;
|
|
421
|
+
const results: CommandResult[] = [];
|
|
422
|
+
server = await mockAgentServer(socketPath, commandResultRetryRoutes('activate-timeout', results, attempt =>
|
|
423
|
+
attempt === 1 ? { statusCode: 408, body: { message: 'Timed out' } } : { body: {} }));
|
|
424
|
+
|
|
425
|
+
const client = new AgentClient(socketPath, { reconnectDelayMs: 10 });
|
|
426
|
+
client.commands.on('activateBundle', () => {
|
|
427
|
+
handlerCalls++;
|
|
428
|
+
return Promise.resolve();
|
|
429
|
+
});
|
|
430
|
+
await client.connect(testRegistration());
|
|
431
|
+
await waitFor(() => results.length === 2);
|
|
432
|
+
|
|
433
|
+
assert.strictEqual(handlerCalls, 1);
|
|
434
|
+
assert.deepStrictEqual(results[1], results[0]);
|
|
435
|
+
await client.close();
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
// An internal contract response requires same-ID delivery retry because the first result may have been accepted.
|
|
439
|
+
it('retries a command result after an internal response', async () => {
|
|
440
|
+
let handlerCalls = 0;
|
|
441
|
+
const results: CommandResult[] = [];
|
|
442
|
+
server = await mockAgentServer(socketPath, commandResultRetryRoutes('activate-internal', results, attempt =>
|
|
443
|
+
attempt === 1 ? {
|
|
444
|
+
statusCode: 500,
|
|
445
|
+
body: { code: 'internal', message: 'Result state is uncertain' },
|
|
446
|
+
} : { body: {} }));
|
|
447
|
+
|
|
448
|
+
const client = new AgentClient(socketPath, { reconnectDelayMs: 10 });
|
|
449
|
+
client.commands.on('activateBundle', () => {
|
|
450
|
+
handlerCalls++;
|
|
451
|
+
return Promise.resolve();
|
|
452
|
+
});
|
|
453
|
+
await client.connect(testRegistration());
|
|
454
|
+
await waitFor(() => results.length === 2);
|
|
455
|
+
|
|
456
|
+
assert.strictEqual(handlerCalls, 1);
|
|
457
|
+
assert.deepStrictEqual(results[1], results[0]);
|
|
346
458
|
await client.close();
|
|
347
459
|
});
|
|
348
460
|
|
|
461
|
+
// A permanently ambiguous result must eventually be logged and released so rollback can run.
|
|
462
|
+
it('bounds ambiguous result retries and processes the next queued command', async () => {
|
|
463
|
+
let handlerCalls = 0;
|
|
464
|
+
let ambiguousResults = 0;
|
|
465
|
+
let nextResults = 0;
|
|
466
|
+
const warnings: string[] = [];
|
|
467
|
+
server = await mockAgentServer(socketPath, [
|
|
468
|
+
{
|
|
469
|
+
method: 'POST',
|
|
470
|
+
path: '/v1/register',
|
|
471
|
+
response: {
|
|
472
|
+
body: {
|
|
473
|
+
workloadId: 'vision',
|
|
474
|
+
inputPolicy: { mode: 'none' },
|
|
475
|
+
inlineSizeLimitBytes: 1024,
|
|
476
|
+
},
|
|
477
|
+
},
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
method: 'GET',
|
|
481
|
+
path: '/v1/commands',
|
|
482
|
+
response: {
|
|
483
|
+
headers: { 'content-type': 'text/event-stream' },
|
|
484
|
+
body: 'data: {"id":"activate-stuck","type":"activateBundle",' +
|
|
485
|
+
'"bundle":{"kind":"model","path":"/models/version-2",' +
|
|
486
|
+
'"deploymentVersion":2}}\n\n' +
|
|
487
|
+
'data: {"id":"policy-next","type":"updateInputPolicy",' +
|
|
488
|
+
'"inputPolicy":{"mode":"none"}}\n\n',
|
|
489
|
+
keepOpen: true,
|
|
490
|
+
},
|
|
491
|
+
},
|
|
492
|
+
{
|
|
493
|
+
method: 'POST',
|
|
494
|
+
path: '/v1/commands/activate-stuck/result',
|
|
495
|
+
response: () => {
|
|
496
|
+
ambiguousResults++;
|
|
497
|
+
return {
|
|
498
|
+
statusCode: 500,
|
|
499
|
+
body: { code: 'internal', message: 'Result state is uncertain' },
|
|
500
|
+
};
|
|
501
|
+
},
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
method: 'POST',
|
|
505
|
+
path: '/v1/commands/policy-next/result',
|
|
506
|
+
response: () => {
|
|
507
|
+
nextResults++;
|
|
508
|
+
return { body: {} };
|
|
509
|
+
},
|
|
510
|
+
},
|
|
511
|
+
]);
|
|
512
|
+
|
|
513
|
+
const client = new AgentClient(socketPath, {
|
|
514
|
+
reconnectDelayMs: 10,
|
|
515
|
+
commandResultRetryTimeoutMs: 25,
|
|
516
|
+
});
|
|
517
|
+
client.commands.on('activateBundle', () => {
|
|
518
|
+
handlerCalls++;
|
|
519
|
+
return Promise.resolve();
|
|
520
|
+
});
|
|
521
|
+
const originalWarn = console.warn;
|
|
522
|
+
console.warn = (...args: unknown[]) => warnings.push(args.map(String).join(' '));
|
|
523
|
+
try {
|
|
524
|
+
await client.connect(testRegistration());
|
|
525
|
+
await waitFor(() => nextResults === 1);
|
|
526
|
+
await client.commands.waitForIdle(100);
|
|
527
|
+
|
|
528
|
+
assert.strictEqual(handlerCalls, 1);
|
|
529
|
+
assert(ambiguousResults >= 2);
|
|
530
|
+
assert(warnings.some(message =>
|
|
531
|
+
message.includes('Stopped retrying result for command "activate-stuck" after 25 ms')));
|
|
532
|
+
}
|
|
533
|
+
finally {
|
|
534
|
+
console.warn = originalWarn;
|
|
535
|
+
await client.close();
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
|
|
349
539
|
// Confirms a selected small input is embedded in the event JSON and acknowledged by the agent.
|
|
350
540
|
it('posts an inference with an inline input selected by the agent policy', async () => {
|
|
351
541
|
let eventBody: EventBatch | undefined;
|
|
@@ -753,6 +943,46 @@ describe('AgentClient', () => {
|
|
|
753
943
|
});
|
|
754
944
|
});
|
|
755
945
|
|
|
946
|
+
/** Creates a complete agent session that initially refuses one command-result delivery. */
|
|
947
|
+
function commandResultRetryRoutes(
|
|
948
|
+
commandId: string,
|
|
949
|
+
results: CommandResult[],
|
|
950
|
+
response: (attempt: number) => MockAgentResponse
|
|
951
|
+
): MockAgentRoute[] {
|
|
952
|
+
return [
|
|
953
|
+
{
|
|
954
|
+
method: 'POST',
|
|
955
|
+
path: '/v1/register',
|
|
956
|
+
response: {
|
|
957
|
+
body: {
|
|
958
|
+
workloadId: 'vision',
|
|
959
|
+
inputPolicy: { mode: 'none' },
|
|
960
|
+
inlineSizeLimitBytes: 1024,
|
|
961
|
+
},
|
|
962
|
+
},
|
|
963
|
+
},
|
|
964
|
+
{
|
|
965
|
+
method: 'GET',
|
|
966
|
+
path: '/v1/commands',
|
|
967
|
+
response: {
|
|
968
|
+
headers: { 'content-type': 'text/event-stream' },
|
|
969
|
+
body: `data: {"id":"${commandId}","type":"activateBundle",` +
|
|
970
|
+
'"bundle":{"kind":"model","path":"/models/version-2",' +
|
|
971
|
+
'"deploymentVersion":2}}\n\n',
|
|
972
|
+
keepOpen: true,
|
|
973
|
+
},
|
|
974
|
+
},
|
|
975
|
+
{
|
|
976
|
+
method: 'POST',
|
|
977
|
+
path: `/v1/commands/${commandId}/result`,
|
|
978
|
+
response: request => {
|
|
979
|
+
results.push(request.body as CommandResult);
|
|
980
|
+
return response(results.length);
|
|
981
|
+
},
|
|
982
|
+
},
|
|
983
|
+
];
|
|
984
|
+
}
|
|
985
|
+
|
|
756
986
|
/**
|
|
757
987
|
* Starts a mock device agent on a Unix socket. Routes declare the response data directly or provide
|
|
758
988
|
* a callback when a test needs to inspect a request, delay a response or record that a stream opened.
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { AgentCommandProcessor } from '../cli-common/agent-client';
|
|
3
|
+
import {
|
|
4
|
+
ActivateBundleCommand,
|
|
5
|
+
Command,
|
|
6
|
+
CommandResult,
|
|
7
|
+
UpdateInputPolicyCommand,
|
|
8
|
+
} from '../shared/device-protocol/workload-contract';
|
|
9
|
+
|
|
10
|
+
type Deferred = {
|
|
11
|
+
promise: Promise<void>;
|
|
12
|
+
resolve: () => void;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Exercises `AgentCommandProcessor`, the serial in-process dispatcher between command delivery and
|
|
17
|
+
* result posting. Successful handlers run in delivery order and produce one acknowledgement each.
|
|
18
|
+
* Missing or failing handlers produce explicit failures, acknowledgement errors do not poison the
|
|
19
|
+
* queue, duplicate ownership is rejected, and redelivery reapplies the command for state recovery.
|
|
20
|
+
*/
|
|
21
|
+
describe('AgentCommandProcessor', () => {
|
|
22
|
+
// A resolved handler produces the success result that lets the agent commit the command.
|
|
23
|
+
it('acknowledges a successful command', async () => {
|
|
24
|
+
const acknowledged: CommandResult[] = [];
|
|
25
|
+
const processor = processorRecording(acknowledged);
|
|
26
|
+
const command = activateBundleCommand('activate-1');
|
|
27
|
+
let delivered: Command | undefined;
|
|
28
|
+
processor.on('activateBundle', received => {
|
|
29
|
+
delivered = received;
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
await processor.process(command);
|
|
34
|
+
|
|
35
|
+
assert.deepStrictEqual(delivered, command);
|
|
36
|
+
assert.deepStrictEqual(acknowledged, [{
|
|
37
|
+
commandId: command.id,
|
|
38
|
+
type: command.type,
|
|
39
|
+
success: true,
|
|
40
|
+
}]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// A handler failure becomes a command failure result so later commands can still run.
|
|
44
|
+
it('acknowledges a handler error as a command failure', async () => {
|
|
45
|
+
const acknowledged: CommandResult[] = [];
|
|
46
|
+
const processor = processorRecording(acknowledged);
|
|
47
|
+
const command = activateBundleCommand('activate-1');
|
|
48
|
+
processor.on('activateBundle', () => Promise.reject(new Error('bundle is incompatible')));
|
|
49
|
+
|
|
50
|
+
await processor.process(command);
|
|
51
|
+
|
|
52
|
+
assert.deepStrictEqual(acknowledged, [{
|
|
53
|
+
commandId: command.id,
|
|
54
|
+
type: command.type,
|
|
55
|
+
success: false,
|
|
56
|
+
reason: 'bundle is incompatible',
|
|
57
|
+
}]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// An unsupported command receives an explicit failure instead of being silently accepted.
|
|
61
|
+
it('acknowledges a command without a registered handler as a failure', async () => {
|
|
62
|
+
const acknowledged: CommandResult[] = [];
|
|
63
|
+
const processor = processorRecording(acknowledged);
|
|
64
|
+
const command = updateInputPolicyCommand('policy-1');
|
|
65
|
+
|
|
66
|
+
await processor.process(command);
|
|
67
|
+
|
|
68
|
+
assert.deepStrictEqual(acknowledged, [{
|
|
69
|
+
commandId: command.id,
|
|
70
|
+
type: command.type,
|
|
71
|
+
success: false,
|
|
72
|
+
reason: 'No handler is registered for command "updateInputPolicy"',
|
|
73
|
+
}]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// A command type has one owner so execution and result handling cannot become ambiguous.
|
|
77
|
+
it('rejects a second handler for the same command type', () => {
|
|
78
|
+
const processor = processorRecording([]);
|
|
79
|
+
processor.on('activateBundle', () => Promise.resolve());
|
|
80
|
+
|
|
81
|
+
assert.throws(
|
|
82
|
+
() => processor.on('activateBundle', () => Promise.resolve()),
|
|
83
|
+
/already registered for command "activateBundle"/
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// The next command waits for the preceding result POST, preserving agent delivery order.
|
|
88
|
+
it('executes and acknowledges commands in delivery order', async () => {
|
|
89
|
+
const events: string[] = [];
|
|
90
|
+
const firstAcknowledgementStarted = deferred();
|
|
91
|
+
const releaseFirstAcknowledgement = deferred();
|
|
92
|
+
const processor = new AgentCommandProcessor(async result => {
|
|
93
|
+
events.push(`acknowledge:${result.commandId}`);
|
|
94
|
+
if (result.commandId === 'activate-1') {
|
|
95
|
+
firstAcknowledgementStarted.resolve();
|
|
96
|
+
await releaseFirstAcknowledgement.promise;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
processor.on('activateBundle', command => {
|
|
100
|
+
events.push(`handle:${command.id}`);
|
|
101
|
+
return Promise.resolve();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const first = processor.process(activateBundleCommand('activate-1'));
|
|
105
|
+
await firstAcknowledgementStarted.promise;
|
|
106
|
+
const second = processor.process(activateBundleCommand('activate-2'));
|
|
107
|
+
assert.deepStrictEqual(events, [ 'handle:activate-1', 'acknowledge:activate-1' ]);
|
|
108
|
+
|
|
109
|
+
releaseFirstAcknowledgement.resolve();
|
|
110
|
+
await Promise.all([ first, second ]);
|
|
111
|
+
assert.deepStrictEqual(events, [
|
|
112
|
+
'handle:activate-1',
|
|
113
|
+
'acknowledge:activate-1',
|
|
114
|
+
'handle:activate-2',
|
|
115
|
+
'acknowledge:activate-2',
|
|
116
|
+
]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// A failed result POST rejects that delivery without permanently poisoning the serial queue.
|
|
120
|
+
it('continues processing after an acknowledgement failure', async () => {
|
|
121
|
+
const handled: string[] = [];
|
|
122
|
+
const acknowledged: string[] = [];
|
|
123
|
+
const processor = new AgentCommandProcessor(result => {
|
|
124
|
+
acknowledged.push(result.commandId);
|
|
125
|
+
return result.commandId === 'activate-1' ?
|
|
126
|
+
Promise.reject(new Error('agent unavailable')) : Promise.resolve();
|
|
127
|
+
});
|
|
128
|
+
processor.on('activateBundle', command => {
|
|
129
|
+
handled.push(command.id);
|
|
130
|
+
return Promise.resolve();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const first = processor.process(activateBundleCommand('activate-1'));
|
|
134
|
+
const second = processor.process(activateBundleCommand('activate-2'));
|
|
135
|
+
|
|
136
|
+
await assert.rejects(first, /agent unavailable/);
|
|
137
|
+
await second;
|
|
138
|
+
assert.deepStrictEqual(handled, [ 'activate-1', 'activate-2' ]);
|
|
139
|
+
assert.deepStrictEqual(acknowledged, [ 'activate-1', 'activate-2' ]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Redelivery reruns the idempotent handler so local state is recovered from the command itself.
|
|
143
|
+
it('reapplies a command when it is redelivered', async () => {
|
|
144
|
+
let handlerCalls = 0;
|
|
145
|
+
const processor = processorRecording([]);
|
|
146
|
+
const command = activateBundleCommand('activate-1');
|
|
147
|
+
processor.on('activateBundle', () => {
|
|
148
|
+
handlerCalls++;
|
|
149
|
+
return Promise.resolve();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
await processor.process(command);
|
|
153
|
+
await processor.process(command);
|
|
154
|
+
|
|
155
|
+
assert.strictEqual(handlerCalls, 2);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
/** Creates a processor whose acknowledgement calls append to the supplied array. */
|
|
160
|
+
function processorRecording(acknowledged: CommandResult[]): AgentCommandProcessor {
|
|
161
|
+
return new AgentCommandProcessor(result => {
|
|
162
|
+
acknowledged.push(result);
|
|
163
|
+
return Promise.resolve();
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Creates a valid model-bundle activation command for processor tests. */
|
|
168
|
+
function activateBundleCommand(id: string): ActivateBundleCommand {
|
|
169
|
+
return {
|
|
170
|
+
id,
|
|
171
|
+
type: 'activateBundle',
|
|
172
|
+
bundle: {
|
|
173
|
+
deploymentVersion: 2,
|
|
174
|
+
kind: 'model',
|
|
175
|
+
path: '/models/version-2',
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Creates an input-policy command used to verify unsupported-command reporting. */
|
|
181
|
+
function updateInputPolicyCommand(id: string): UpdateInputPolicyCommand {
|
|
182
|
+
return {
|
|
183
|
+
id,
|
|
184
|
+
type: 'updateInputPolicy',
|
|
185
|
+
inputPolicy: { mode: 'all' },
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Creates a promise whose completion is controlled by the test. */
|
|
190
|
+
function deferred(): Deferred {
|
|
191
|
+
let resolve: () => void = () => undefined;
|
|
192
|
+
const promise = new Promise<void>(resolvePromise => resolve = resolvePromise);
|
|
193
|
+
return { promise, resolve };
|
|
194
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { EventEmitter } from 'tsee';
|
|
3
|
+
import { agentEngineName, agentRunnerMode } from '../cli/linux/agent-runner-metadata';
|
|
4
|
+
import { AudioClassifier } from '../library/classifier/audio-classifier';
|
|
5
|
+
import {
|
|
6
|
+
LinuxImpulseRunner,
|
|
7
|
+
ModelInformation,
|
|
8
|
+
RunnerHelloInferencingEngine,
|
|
9
|
+
} from '../library/classifier/linux-impulse-runner';
|
|
10
|
+
import { AudioRecorder } from '../library/sensors/recorder';
|
|
11
|
+
import { selectNonInteractiveDevice } from '../library/sensors/sensors-helper';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Exercises the startup decisions that let an agent-managed runner describe and acquire its input
|
|
15
|
+
* without user interaction. It verifies explicit and model-derived serving modes, stable engine
|
|
16
|
+
* names, explicit or unambiguous device selection, and managed handling of silent audio. Ambiguous
|
|
17
|
+
* hardware must fail with configuration guidance instead of blocking on an interactive prompt.
|
|
18
|
+
*/
|
|
19
|
+
describe('agent runner configuration', () => {
|
|
20
|
+
// Empty registration needs a mode derived from startup configuration before a bundle exists.
|
|
21
|
+
it('reports modes from explicit configuration before a model is loaded', () => {
|
|
22
|
+
assert.strictEqual(agentRunnerMode(1337, undefined, undefined, undefined), 'http');
|
|
23
|
+
assert.strictEqual(agentRunnerMode(undefined, '/dev/video0', undefined, undefined), 'camera');
|
|
24
|
+
assert.strictEqual(agentRunnerMode(undefined, undefined, 'default', undefined), 'microphone');
|
|
25
|
+
assert.strictEqual(agentRunnerMode(undefined, undefined, undefined, undefined), undefined);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Model metadata supplies the mode when startup arguments did not select a streaming input.
|
|
29
|
+
it('reports the mode from a loaded model when no input was selected explicitly', () => {
|
|
30
|
+
const model = <ModelInformation>{ modelParameters: { sensorType: 'camera' } };
|
|
31
|
+
assert.strictEqual(agentRunnerMode(undefined, undefined, undefined, model), 'camera');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Registration uses stable names and must tolerate an engine introduced after this CLI version.
|
|
35
|
+
it('uses stable engine names and accepts future engine values', () => {
|
|
36
|
+
assert.strictEqual(agentEngineName(RunnerHelloInferencingEngine.Tflite), 'tflite');
|
|
37
|
+
assert.strictEqual(agentEngineName(RunnerHelloInferencingEngine.TfliteFull), 'tflite');
|
|
38
|
+
assert.strictEqual(agentEngineName(RunnerHelloInferencingEngine.TensorRT), 'tensorrt');
|
|
39
|
+
assert.strictEqual(agentEngineName(18), 'unknown');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Agent mode cannot prompt, so an explicit device must win when several devices are available.
|
|
43
|
+
it('uses an explicitly selected input device', () => {
|
|
44
|
+
assert.strictEqual(selectNonInteractiveDevice(
|
|
45
|
+
[ 'camera-1', 'camera-2' ], 'camera-2', '--camera', 'EI_CAMERA'), 'camera-2');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// A sole device is unambiguous and should start without redundant container configuration.
|
|
49
|
+
it('automatically uses the only available input device', () => {
|
|
50
|
+
assert.strictEqual(selectNonInteractiveDevice(
|
|
51
|
+
[ 'microphone-1' ], undefined, '--microphone', 'EI_MICROPHONE'), 'microphone-1');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Ambiguous hardware must fail rather than blocking a headless workload on an interactive prompt.
|
|
55
|
+
it('rejects ambiguous input device selection', () => {
|
|
56
|
+
assert.throws(
|
|
57
|
+
() => selectNonInteractiveDevice(
|
|
58
|
+
[ 'camera-1', 'camera-2' ], undefined, '--camera', 'EI_CAMERA'),
|
|
59
|
+
/set --camera or EI_CAMERA/
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Managed workloads accept silence because it does not reliably identify a broken microphone.
|
|
64
|
+
it('classifies a silent first audio frame when the legacy diagnostic is disabled', async () => {
|
|
65
|
+
const originalStart = AudioRecorder.prototype.start;
|
|
66
|
+
const audio = new EventEmitter<{ data: (data: Buffer) => void }>();
|
|
67
|
+
AudioRecorder.prototype.start = async () => ({ ee: audio, stop: async () => undefined });
|
|
68
|
+
let classifyCount = 0;
|
|
69
|
+
const classifier = new AudioClassifier(fakeRunner(async () => {
|
|
70
|
+
classifyCount++;
|
|
71
|
+
return classifyResponse();
|
|
72
|
+
}), false, { rejectSilentFirstFrame: false });
|
|
73
|
+
const result = new Promise<void>(resolve => classifier.on('result', () => resolve()));
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
await classifier.start('test');
|
|
77
|
+
audio.emit('data', Buffer.alloc(2));
|
|
78
|
+
await result;
|
|
79
|
+
assert.strictEqual(classifyCount, 1);
|
|
80
|
+
await classifier.stop();
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
AudioRecorder.prototype.start = originalStart;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
/** Creates the minimal initialized runner contract consumed by `AudioClassifier`. */
|
|
89
|
+
function fakeRunner(classify: (values: number[]) => Promise<ReturnType<typeof classifyResponse>>): LinuxImpulseRunner {
|
|
90
|
+
return <LinuxImpulseRunner><unknown>{
|
|
91
|
+
getModel: () => <ModelInformation><unknown>{
|
|
92
|
+
modelParameters: {
|
|
93
|
+
sensorType: 'microphone',
|
|
94
|
+
input_features_count: 1,
|
|
95
|
+
frequency: 1,
|
|
96
|
+
use_continuous_mode: false,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
classify,
|
|
100
|
+
stop: async () => undefined,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Returns the minimal successful inference response consumed by `AudioClassifier`. */
|
|
105
|
+
function classifyResponse() {
|
|
106
|
+
return {
|
|
107
|
+
result: { classification: { silence: 1 } },
|
|
108
|
+
timing: { dsp: 1, classification: 1, anomaly: 0 },
|
|
109
|
+
};
|
|
110
|
+
}
|