@indra.ai/deva 1.64.7 → 1.64.9
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/index.js +12 -4
- package/package.json +1 -1
- package/tests/suite/06-shutdown.test.js +123 -0
package/index.js
CHANGED
|
@@ -224,6 +224,7 @@ class Deva {
|
|
|
224
224
|
for (const pending of this._pending) {
|
|
225
225
|
clearTimeout(pending.timer);
|
|
226
226
|
if (this.events) this.ignore(pending.evt, pending.handler);
|
|
227
|
+
pending.resolve(this._messages.offline); // the Deva is going offline so answer the waiting caller.
|
|
227
228
|
}
|
|
228
229
|
this._pending.clear();
|
|
229
230
|
}
|
|
@@ -767,7 +768,7 @@ class Deva {
|
|
|
767
768
|
this.action('ask', `${key}:${id.uid}`);
|
|
768
769
|
this.intent('good', `ask:${key}:${id.uid}`);
|
|
769
770
|
const evt = `${key}:ask:${packet.id.uid}`; // the once event keyed to this packet.
|
|
770
|
-
const pending = {evt, handler: false, timer: false}; // tracked so shutdown can clean up.
|
|
771
|
+
const pending = {evt, handler: false, timer: false, resolve}; // tracked so shutdown can clean up.
|
|
771
772
|
|
|
772
773
|
// single place to release the listener, the timer, and the pending record.
|
|
773
774
|
const release = () => {
|
|
@@ -1007,11 +1008,14 @@ class Deva {
|
|
|
1007
1008
|
this.state('valid', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1008
1009
|
this.intent('good', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1009
1010
|
this.talk(`${agent.key}:ask:${packet.id.uid}`, packet_stamped);
|
|
1011
|
+
|
|
1010
1012
|
}).catch(err => {
|
|
1011
1013
|
this.action('talk', `${agent.key}:${key}:${method}:err:${packet.id.uid}`);
|
|
1012
1014
|
this.state('catch', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1013
1015
|
this.intent('bad', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1016
|
+
// reply on the asker's event so the asker hears the error right away.
|
|
1014
1017
|
this.talk(`${agent.key}:${key}:${packet.id.uid}`, {error:err});
|
|
1018
|
+
// log the error here; the asker owns the reject.
|
|
1015
1019
|
return this.err(err, packet, () => {});
|
|
1016
1020
|
})
|
|
1017
1021
|
}
|
|
@@ -1020,7 +1024,7 @@ class Deva {
|
|
|
1020
1024
|
this.state('catch', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1021
1025
|
this.intent('bad', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1022
1026
|
this.talk(`${agent.key}:${key}:${packet.id.uid}`, {error:e});
|
|
1023
|
-
return this.err(e, packet, () => {})
|
|
1027
|
+
return this.err(e, packet, () => {});
|
|
1024
1028
|
}
|
|
1025
1029
|
// now when we ask the meta params[0] should be the method
|
|
1026
1030
|
}
|
|
@@ -1276,12 +1280,16 @@ class Deva {
|
|
|
1276
1280
|
Recursion Framework.
|
|
1277
1281
|
***************/
|
|
1278
1282
|
stop() {
|
|
1279
|
-
if (!this._active) return this._messages.offline; // check for active status
|
|
1283
|
+
if (!this._active) return Promise.resolve(this._messages.offline); // check for active status
|
|
1280
1284
|
const key = 'stop'; // set the stop key
|
|
1281
1285
|
const id = this.uid(); // generate the stop uid
|
|
1282
1286
|
let data;
|
|
1283
|
-
return new Promise((resolve, reject) => {
|
|
1287
|
+
return new Promise(async (resolve, reject) => {
|
|
1284
1288
|
try {
|
|
1289
|
+
// stop the child Devas first so none are left running when the parent closes. ready() loads them last.
|
|
1290
|
+
this.state('loop', `${key}:devas:${id.uid}`);
|
|
1291
|
+
for (const deva in this.devas) await this.devas[deva].stop();
|
|
1292
|
+
|
|
1285
1293
|
this.state('set', `${key}:agent:${id.uid}`); // state stop agent
|
|
1286
1294
|
const agentData = this.agent();
|
|
1287
1295
|
const agent = agentData.sha256; // get the current agent
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "31711622713594414221",
|
|
3
3
|
"name": "@indra.ai/deva",
|
|
4
|
-
"version": "1.64.
|
|
4
|
+
"version": "1.64.9",
|
|
5
5
|
"description": "Deva Core a Vedic-inspired Event Based Context Aware Feature, Zone, Action, and State Machine integrated Artificial Intelligence Framework",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"license": "VLA:31711622713594414221 LICENSE.md",
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Deva Core Test Suite: step 6, stop and the shutdown chain
|
|
3
|
+
// Copyright ©2000-2026 Quinn Michaels; All rights reserved.
|
|
4
|
+
// Owner Signature Required For Lawful Use.
|
|
5
|
+
// Distributed under VLA:31711622713594414221 LICENSE.md
|
|
6
|
+
|
|
7
|
+
import test from 'node:test';
|
|
8
|
+
import assert from 'node:assert/strict';
|
|
9
|
+
import {build, client, agent} from './_deva.js';
|
|
10
|
+
|
|
11
|
+
const TIERS = ['md5', 'sha256', 'sha512'];
|
|
12
|
+
const CHAIN = ['stop', 'close', 'leave', 'exit', 'shutdown'];
|
|
13
|
+
const fresh = () => structuredClone(client);
|
|
14
|
+
|
|
15
|
+
test('stop() answers offline before init()', async () => {
|
|
16
|
+
const deva = build();
|
|
17
|
+
const stopped = deva.stop();
|
|
18
|
+
assert.ok(stopped instanceof Promise, 'offline stop still returns a Promise');
|
|
19
|
+
assert.equal(await stopped, deva._messages.offline);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('stop() runs the full chain and resolves the stamped stop data', async () => {
|
|
23
|
+
const deva = build();
|
|
24
|
+
await deva.init(fresh());
|
|
25
|
+
const agent = deva.agent();
|
|
26
|
+
const clientHash = deva.client().sha256;
|
|
27
|
+
const data = await deva.stop();
|
|
28
|
+
|
|
29
|
+
assert.equal(data.key, 'shutdown', 'the last step sets the key');
|
|
30
|
+
assert.equal(data.value, agent.key);
|
|
31
|
+
assert.equal(data.agent, agent.sha256);
|
|
32
|
+
assert.equal(data.client, clientHash);
|
|
33
|
+
for (const t of TIERS) assert.equal(typeof data[t], 'string', `stop data ${t}`);
|
|
34
|
+
// every step stamps its time, in chain order.
|
|
35
|
+
for (let i = 0; i < CHAIN.length; i++) {
|
|
36
|
+
assert.equal(typeof data[CHAIN[i]], 'number', `${CHAIN[i]} time`);
|
|
37
|
+
if (i) assert.ok(data[CHAIN[i]] >= data[CHAIN[i - 1]], `${CHAIN[i]} after ${CHAIN[i - 1]}`);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('each step talks its devacore event in chain order', async () => {
|
|
42
|
+
const deva = build();
|
|
43
|
+
await deva.init(fresh());
|
|
44
|
+
const heard = [];
|
|
45
|
+
for (const key of CHAIN) deva.events.on(deva._events[key], packet => heard.push(packet.key));
|
|
46
|
+
await deva.stop();
|
|
47
|
+
assert.deepEqual(heard, CHAIN);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('the on hooks run in chain order and the Deva is cleared by onShutdown', async () => {
|
|
51
|
+
const order = [];
|
|
52
|
+
const next = {onStop: 'close', onClose: 'leave', onLeave: 'exit', onExit: 'shutdown'};
|
|
53
|
+
const hooks = {};
|
|
54
|
+
for (const name of [...Object.keys(next), 'onShutdown']) {
|
|
55
|
+
hooks[name] = function (data, resolve) {
|
|
56
|
+
order.push([name, Boolean(this._active)]);
|
|
57
|
+
return next[name] ? this[next[name]](data, resolve) : resolve(data);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const deva = build(hooks);
|
|
61
|
+
await deva.init(fresh());
|
|
62
|
+
await deva.stop();
|
|
63
|
+
assert.deepEqual(order, [
|
|
64
|
+
['onStop', true], ['onClose', true], ['onLeave', true], ['onExit', true],
|
|
65
|
+
['onShutdown', false], // shutdown clears _active before its hook runs.
|
|
66
|
+
]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('after stop the Deva is offline and its cleared features are false', async () => {
|
|
70
|
+
const deva = build();
|
|
71
|
+
await deva.init(fresh());
|
|
72
|
+
await deva.stop();
|
|
73
|
+
assert.equal(deva._active, false);
|
|
74
|
+
for (const item of ['_license', '_security', '_support', '_services', '_systems', '_config', 'events']) {
|
|
75
|
+
assert.equal(deva[item], false, `${item} cleared`);
|
|
76
|
+
}
|
|
77
|
+
assert.equal(await deva.question('/hello'), deva._messages.offline);
|
|
78
|
+
assert.equal(await deva.stop(), deva._messages.offline, 'a second stop is offline');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('stop removes every listener this Deva put on the event bus', async () => {
|
|
82
|
+
const deva = build();
|
|
83
|
+
await deva.init(fresh());
|
|
84
|
+
const bus = deva.events;
|
|
85
|
+
assert.ok(bus.eventNames().length > 0, 'init added listeners');
|
|
86
|
+
await deva.stop();
|
|
87
|
+
assert.deepEqual(bus.eventNames(), [], 'nothing left on the bus');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('a pending ask at shutdown answers offline and leaves nothing behind', async () => {
|
|
91
|
+
const deva = build({config: {hash: {}, timeout: {ask: 0}}}); // no timeout, so only shutdown can end it.
|
|
92
|
+
await deva.init(fresh());
|
|
93
|
+
const bus = deva.events;
|
|
94
|
+
const silent = () => {};
|
|
95
|
+
bus.on('silent:ask', silent);
|
|
96
|
+
const asked = deva.question('#silent shout hi');
|
|
97
|
+
await new Promise(r => setImmediate(r)); // let the ask get on the bus.
|
|
98
|
+
assert.equal(deva._pending.size, 1, 'the ask is waiting');
|
|
99
|
+
await deva.stop();
|
|
100
|
+
assert.equal(await asked, deva._messages.offline, 'the waiting caller is answered');
|
|
101
|
+
assert.equal(deva._pending.size, 0);
|
|
102
|
+
bus.removeListener('silent:ask', silent);
|
|
103
|
+
assert.deepEqual(bus.eventNames(), []);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('stop() stops the child Devas first and leaves nothing on the bus', async () => {
|
|
107
|
+
const order = [];
|
|
108
|
+
const helper = build({
|
|
109
|
+
agent: {...agent, id: 'helper', key: 'helper', profile: {...agent.profile, name: 'Helper'}},
|
|
110
|
+
onShutdown(data, resolve) { order.push('helper:shutdown'); return resolve(data); },
|
|
111
|
+
});
|
|
112
|
+
const deva = build({devas: {helper}});
|
|
113
|
+
await deva.init(fresh());
|
|
114
|
+
assert.ok(helper._active, 'the child is running after init');
|
|
115
|
+
const bus = deva.events;
|
|
116
|
+
const heard = packet => order.push(`${packet.value}:stop`);
|
|
117
|
+
bus.on(deva._events.stop, heard);
|
|
118
|
+
await deva.stop();
|
|
119
|
+
bus.removeListener(deva._events.stop, heard);
|
|
120
|
+
assert.equal(helper._active, false, 'the child is offline');
|
|
121
|
+
assert.deepEqual(order, ['helper:stop', 'helper:shutdown', 'test:stop'], 'the child finishes before the parent starts');
|
|
122
|
+
assert.deepEqual(bus.eventNames(), [], 'nothing left on the bus');
|
|
123
|
+
});
|