@auxiora/overseer 0.1.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/LICENSE +191 -0
- package/package.json +10 -0
- package/src/alert-store.ts +100 -0
- package/src/assessment-store.ts +76 -0
- package/src/index.ts +16 -0
- package/src/llm-overseer.ts +59 -0
- package/src/monitor.ts +168 -0
- package/src/types.ts +51 -0
- package/tests/alert-store.test.ts +77 -0
- package/tests/assessment-store.test.ts +79 -0
- package/tests/llm-overseer.test.ts +102 -0
- package/tests/monitor.test.ts +122 -0
- package/tsconfig.json +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2026 Auxiora Contributors
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/package.json
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import type { OverseerAlert } from './types.js';
|
|
3
|
+
|
|
4
|
+
export interface StoredAlert extends OverseerAlert {
|
|
5
|
+
id: number;
|
|
6
|
+
acknowledged: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class AlertStore {
|
|
10
|
+
private db: DatabaseSync;
|
|
11
|
+
private closed = false;
|
|
12
|
+
|
|
13
|
+
constructor(dbPath: string) {
|
|
14
|
+
this.db = new DatabaseSync(dbPath);
|
|
15
|
+
this.db.exec('PRAGMA journal_mode = WAL');
|
|
16
|
+
this.db.exec(`
|
|
17
|
+
CREATE TABLE IF NOT EXISTS alerts (
|
|
18
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
19
|
+
type TEXT NOT NULL,
|
|
20
|
+
agentId TEXT NOT NULL,
|
|
21
|
+
message TEXT NOT NULL,
|
|
22
|
+
severity TEXT NOT NULL,
|
|
23
|
+
detectedAt INTEGER NOT NULL,
|
|
24
|
+
acknowledged INTEGER DEFAULT 0
|
|
25
|
+
)
|
|
26
|
+
`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
record(alert: OverseerAlert): void {
|
|
30
|
+
if (this.closed) return;
|
|
31
|
+
const stmt = this.db.prepare(
|
|
32
|
+
'INSERT INTO alerts (type, agentId, message, severity, detectedAt) VALUES (?, ?, ?, ?, ?)',
|
|
33
|
+
);
|
|
34
|
+
stmt.run(alert.type, alert.agentId, alert.message, alert.severity, alert.detectedAt);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getRecent(limit = 50): StoredAlert[] {
|
|
38
|
+
if (this.closed) return [];
|
|
39
|
+
const stmt = this.db.prepare('SELECT * FROM alerts ORDER BY detectedAt DESC LIMIT ?');
|
|
40
|
+
return (stmt.all(limit) as RawRow[]).map(toStoredAlert);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
getByAgent(agentId: string): StoredAlert[] {
|
|
44
|
+
if (this.closed) return [];
|
|
45
|
+
const stmt = this.db.prepare(
|
|
46
|
+
'SELECT * FROM alerts WHERE agentId = ? ORDER BY detectedAt DESC',
|
|
47
|
+
);
|
|
48
|
+
return (stmt.all(agentId) as RawRow[]).map(toStoredAlert);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getBySeverity(severity: string): StoredAlert[] {
|
|
52
|
+
if (this.closed) return [];
|
|
53
|
+
const stmt = this.db.prepare(
|
|
54
|
+
'SELECT * FROM alerts WHERE severity = ? ORDER BY detectedAt DESC',
|
|
55
|
+
);
|
|
56
|
+
return (stmt.all(severity) as RawRow[]).map(toStoredAlert);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getUnacknowledged(): StoredAlert[] {
|
|
60
|
+
if (this.closed) return [];
|
|
61
|
+
const stmt = this.db.prepare(
|
|
62
|
+
'SELECT * FROM alerts WHERE acknowledged = 0 ORDER BY detectedAt DESC',
|
|
63
|
+
);
|
|
64
|
+
return (stmt.all() as RawRow[]).map(toStoredAlert);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
acknowledge(id: number): void {
|
|
68
|
+
if (this.closed) return;
|
|
69
|
+
const stmt = this.db.prepare('UPDATE alerts SET acknowledged = 1 WHERE id = ?');
|
|
70
|
+
stmt.run(id);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
close(): void {
|
|
74
|
+
if (this.closed) return;
|
|
75
|
+
this.closed = true;
|
|
76
|
+
this.db.close();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface RawRow {
|
|
81
|
+
id: number;
|
|
82
|
+
type: string;
|
|
83
|
+
agentId: string;
|
|
84
|
+
message: string;
|
|
85
|
+
severity: string;
|
|
86
|
+
detectedAt: number;
|
|
87
|
+
acknowledged: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function toStoredAlert(row: RawRow): StoredAlert {
|
|
91
|
+
return {
|
|
92
|
+
id: row.id,
|
|
93
|
+
type: row.type as OverseerAlert['type'],
|
|
94
|
+
agentId: row.agentId,
|
|
95
|
+
message: row.message,
|
|
96
|
+
severity: row.severity as OverseerAlert['severity'],
|
|
97
|
+
detectedAt: row.detectedAt,
|
|
98
|
+
acknowledged: row.acknowledged === 1,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import type { AssessmentResult, OverseerAction, LLMAssessment, OverseerAlert } from './types.js';
|
|
3
|
+
|
|
4
|
+
export class AssessmentStore {
|
|
5
|
+
private readonly db: DatabaseSync;
|
|
6
|
+
private closed = false;
|
|
7
|
+
|
|
8
|
+
constructor(dbPath: string) {
|
|
9
|
+
this.db = new DatabaseSync(dbPath);
|
|
10
|
+
this.db.exec('PRAGMA journal_mode=WAL');
|
|
11
|
+
this.db.exec('PRAGMA busy_timeout=5000');
|
|
12
|
+
this.db.exec(`
|
|
13
|
+
CREATE TABLE IF NOT EXISTS assessments (
|
|
14
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
15
|
+
agentId TEXT NOT NULL,
|
|
16
|
+
heuristicAlertsJson TEXT NOT NULL,
|
|
17
|
+
llmAssessmentJson TEXT,
|
|
18
|
+
action TEXT NOT NULL,
|
|
19
|
+
notification TEXT,
|
|
20
|
+
assessedAt INTEGER NOT NULL
|
|
21
|
+
)
|
|
22
|
+
`);
|
|
23
|
+
this.db.exec('CREATE INDEX IF NOT EXISTS idx_assessments_agent ON assessments (agentId)');
|
|
24
|
+
this.db.exec('CREATE INDEX IF NOT EXISTS idx_assessments_action ON assessments (action)');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
record(result: AssessmentResult): void {
|
|
28
|
+
if (this.closed) return;
|
|
29
|
+
const stmt = this.db.prepare(
|
|
30
|
+
'INSERT INTO assessments (agentId, heuristicAlertsJson, llmAssessmentJson, action, notification, assessedAt) VALUES (?, ?, ?, ?, ?, ?)',
|
|
31
|
+
);
|
|
32
|
+
stmt.run(
|
|
33
|
+
result.agentId,
|
|
34
|
+
JSON.stringify(result.heuristicAlerts),
|
|
35
|
+
result.llmAssessment ? JSON.stringify(result.llmAssessment) : null,
|
|
36
|
+
result.action,
|
|
37
|
+
result.notification ?? null,
|
|
38
|
+
result.assessedAt,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getByAgent(agentId: string): AssessmentResult[] {
|
|
43
|
+
if (this.closed) return [];
|
|
44
|
+
const stmt = this.db.prepare('SELECT * FROM assessments WHERE agentId = ? ORDER BY assessedAt DESC');
|
|
45
|
+
return this.mapRows(stmt.all(agentId) as Record<string, unknown>[]);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getByAction(action: OverseerAction): AssessmentResult[] {
|
|
49
|
+
if (this.closed) return [];
|
|
50
|
+
const stmt = this.db.prepare('SELECT * FROM assessments WHERE action = ? ORDER BY assessedAt DESC');
|
|
51
|
+
return this.mapRows(stmt.all(action) as Record<string, unknown>[]);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getRecent(limit = 50): AssessmentResult[] {
|
|
55
|
+
if (this.closed) return [];
|
|
56
|
+
const stmt = this.db.prepare('SELECT * FROM assessments ORDER BY assessedAt DESC LIMIT ?');
|
|
57
|
+
return this.mapRows(stmt.all(limit) as Record<string, unknown>[]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
close(): void {
|
|
61
|
+
if (this.closed) return;
|
|
62
|
+
this.closed = true;
|
|
63
|
+
this.db.close();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private mapRows(rows: Record<string, unknown>[]): AssessmentResult[] {
|
|
67
|
+
return rows.map((r) => ({
|
|
68
|
+
agentId: r.agentId as string,
|
|
69
|
+
heuristicAlerts: JSON.parse(r.heuristicAlertsJson as string) as OverseerAlert[],
|
|
70
|
+
llmAssessment: r.llmAssessmentJson ? (JSON.parse(r.llmAssessmentJson as string) as LLMAssessment) : undefined,
|
|
71
|
+
action: r.action as OverseerAction,
|
|
72
|
+
notification: (r.notification as string) || undefined,
|
|
73
|
+
assessedAt: r.assessedAt as number,
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { AlertStore } from './alert-store.js';
|
|
2
|
+
export type { StoredAlert } from './alert-store.js';
|
|
3
|
+
export { AssessmentStore } from './assessment-store.js';
|
|
4
|
+
export { OverseerMonitor } from './monitor.js';
|
|
5
|
+
export { ActiveOverseer } from './llm-overseer.js';
|
|
6
|
+
export type {
|
|
7
|
+
AgentSnapshot,
|
|
8
|
+
AlertType,
|
|
9
|
+
AssessmentResult,
|
|
10
|
+
LLMAssessment,
|
|
11
|
+
LLMCallerLike,
|
|
12
|
+
OverseerAction,
|
|
13
|
+
OverseerAlert,
|
|
14
|
+
OverseerConfig,
|
|
15
|
+
ToolCall,
|
|
16
|
+
} from './types.js';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { OverseerMonitor } from './monitor.js';
|
|
2
|
+
import type {
|
|
3
|
+
OverseerConfig,
|
|
4
|
+
AgentSnapshot,
|
|
5
|
+
OverseerAlert,
|
|
6
|
+
AssessmentResult,
|
|
7
|
+
LLMAssessment,
|
|
8
|
+
LLMCallerLike,
|
|
9
|
+
OverseerAction,
|
|
10
|
+
} from './types.js';
|
|
11
|
+
|
|
12
|
+
export class ActiveOverseer {
|
|
13
|
+
private readonly monitor: OverseerMonitor;
|
|
14
|
+
private readonly llmCaller?: LLMCallerLike;
|
|
15
|
+
private readonly history: AssessmentResult[] = [];
|
|
16
|
+
|
|
17
|
+
constructor(config: OverseerConfig, llmCaller?: LLMCallerLike) {
|
|
18
|
+
this.monitor = new OverseerMonitor(config);
|
|
19
|
+
this.llmCaller = llmCaller;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async assess(snapshot: AgentSnapshot): Promise<AssessmentResult> {
|
|
23
|
+
const heuristicAlerts = this.monitor.analyze(snapshot);
|
|
24
|
+
|
|
25
|
+
let llmAssessment: LLMAssessment | undefined;
|
|
26
|
+
let action: OverseerAction = 'none';
|
|
27
|
+
let notification: string | undefined;
|
|
28
|
+
|
|
29
|
+
if (heuristicAlerts.length > 0) {
|
|
30
|
+
action = 'alert';
|
|
31
|
+
|
|
32
|
+
if (this.llmCaller) {
|
|
33
|
+
try {
|
|
34
|
+
llmAssessment = await this.llmCaller.assessWithLLM(heuristicAlerts, snapshot);
|
|
35
|
+
action = llmAssessment.suggestedAction;
|
|
36
|
+
notification = llmAssessment.notification;
|
|
37
|
+
} catch {
|
|
38
|
+
// LLM failed — fall back to heuristic action
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const result: AssessmentResult = {
|
|
44
|
+
agentId: snapshot.agentId,
|
|
45
|
+
heuristicAlerts,
|
|
46
|
+
llmAssessment,
|
|
47
|
+
action,
|
|
48
|
+
notification,
|
|
49
|
+
assessedAt: Date.now(),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
this.history.push(result);
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getAssessmentHistory(): AssessmentResult[] {
|
|
57
|
+
return [...this.history];
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/monitor.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { AgentSnapshot, OverseerAlert, OverseerConfig } from './types.js';
|
|
2
|
+
|
|
3
|
+
export class OverseerMonitor {
|
|
4
|
+
private readonly config: OverseerConfig;
|
|
5
|
+
|
|
6
|
+
constructor(config: OverseerConfig) {
|
|
7
|
+
this.config = config;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
analyze(snapshot: AgentSnapshot): OverseerAlert[] {
|
|
11
|
+
const alerts: OverseerAlert[] = [];
|
|
12
|
+
const now = Date.now();
|
|
13
|
+
|
|
14
|
+
const loopAlerts = this.detectLoops(snapshot, now);
|
|
15
|
+
alerts.push(...loopAlerts);
|
|
16
|
+
|
|
17
|
+
const stallAlert = this.detectStall(snapshot, now);
|
|
18
|
+
if (stallAlert) {
|
|
19
|
+
alerts.push(stallAlert);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const budgetAlert = this.detectBudgetExceeded(snapshot, now);
|
|
23
|
+
if (budgetAlert) {
|
|
24
|
+
alerts.push(budgetAlert);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return alerts;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private detectLoops(snapshot: AgentSnapshot, now: number): OverseerAlert[] {
|
|
31
|
+
const alerts: OverseerAlert[] = [];
|
|
32
|
+
const { toolCalls } = snapshot;
|
|
33
|
+
const { loopThreshold } = this.config;
|
|
34
|
+
|
|
35
|
+
if (toolCalls.length < loopThreshold) {
|
|
36
|
+
return alerts;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Detect single-tool repetition: same tool N times consecutively
|
|
40
|
+
const singleToolAlert = this.detectSingleToolRepetition(snapshot, now);
|
|
41
|
+
if (singleToolAlert) {
|
|
42
|
+
alerts.push(singleToolAlert);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Detect repeating patterns (e.g. A,B,A,B,A,B)
|
|
46
|
+
const patternAlert = this.detectRepeatingPattern(snapshot, now);
|
|
47
|
+
if (patternAlert) {
|
|
48
|
+
alerts.push(patternAlert);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return alerts;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private detectSingleToolRepetition(
|
|
55
|
+
snapshot: AgentSnapshot,
|
|
56
|
+
now: number,
|
|
57
|
+
): OverseerAlert | undefined {
|
|
58
|
+
const { toolCalls } = snapshot;
|
|
59
|
+
const { loopThreshold } = this.config;
|
|
60
|
+
|
|
61
|
+
if (toolCalls.length < loopThreshold) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Check the last N tool calls for same-tool repetition
|
|
66
|
+
const tail = toolCalls.slice(-loopThreshold);
|
|
67
|
+
const allSame = tail.every((tc) => tc.tool === tail[0]!.tool);
|
|
68
|
+
|
|
69
|
+
if (allSame) {
|
|
70
|
+
return {
|
|
71
|
+
type: 'loop_detected',
|
|
72
|
+
agentId: snapshot.agentId,
|
|
73
|
+
message: `Tool "${tail[0]!.tool}" called ${loopThreshold} times consecutively`,
|
|
74
|
+
severity: 'warning',
|
|
75
|
+
detectedAt: now,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private detectRepeatingPattern(
|
|
83
|
+
snapshot: AgentSnapshot,
|
|
84
|
+
now: number,
|
|
85
|
+
): OverseerAlert | undefined {
|
|
86
|
+
const { toolCalls } = snapshot;
|
|
87
|
+
const { loopThreshold } = this.config;
|
|
88
|
+
const tools = toolCalls.map((tc) => tc.tool);
|
|
89
|
+
|
|
90
|
+
// Try pattern lengths from 2 up to half the tool call list
|
|
91
|
+
const maxPatternLen = Math.floor(tools.length / 2);
|
|
92
|
+
|
|
93
|
+
for (let patternLen = 2; patternLen <= maxPatternLen; patternLen++) {
|
|
94
|
+
const repetitions = this.countPatternRepetitions(tools, patternLen);
|
|
95
|
+
if (repetitions >= loopThreshold) {
|
|
96
|
+
const tailStart = tools.length - patternLen * Math.floor(tools.length / patternLen);
|
|
97
|
+
const pattern = tools.slice(tailStart, tailStart + patternLen).join(',');
|
|
98
|
+
return {
|
|
99
|
+
type: 'loop_detected',
|
|
100
|
+
agentId: snapshot.agentId,
|
|
101
|
+
message: `Repeating pattern [${pattern}] detected ${repetitions} times`,
|
|
102
|
+
severity: 'warning',
|
|
103
|
+
detectedAt: now,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private countPatternRepetitions(tools: string[], patternLen: number): number {
|
|
112
|
+
const pattern = tools.slice(-patternLen * Math.floor(tools.length / patternLen));
|
|
113
|
+
if (pattern.length < patternLen * 2) {
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const base = pattern.slice(0, patternLen);
|
|
118
|
+
let count = 0;
|
|
119
|
+
|
|
120
|
+
for (let i = 0; i <= pattern.length - patternLen; i += patternLen) {
|
|
121
|
+
const chunk = pattern.slice(i, i + patternLen);
|
|
122
|
+
const matches = chunk.every((t, idx) => t === base[idx]);
|
|
123
|
+
if (matches) {
|
|
124
|
+
count++;
|
|
125
|
+
} else {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return count;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private detectStall(
|
|
134
|
+
snapshot: AgentSnapshot,
|
|
135
|
+
now: number,
|
|
136
|
+
): OverseerAlert | undefined {
|
|
137
|
+
const idleMs = now - snapshot.lastActivityAt;
|
|
138
|
+
|
|
139
|
+
if (idleMs > this.config.stallTimeoutMs) {
|
|
140
|
+
return {
|
|
141
|
+
type: 'stall_detected',
|
|
142
|
+
agentId: snapshot.agentId,
|
|
143
|
+
message: `Agent idle for ${Math.round(idleMs / 1000)}s (threshold: ${Math.round(this.config.stallTimeoutMs / 1000)}s)`,
|
|
144
|
+
severity: 'critical',
|
|
145
|
+
detectedAt: now,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private detectBudgetExceeded(
|
|
153
|
+
snapshot: AgentSnapshot,
|
|
154
|
+
now: number,
|
|
155
|
+
): OverseerAlert | undefined {
|
|
156
|
+
if (snapshot.tokenUsage > this.config.maxTokenBudget) {
|
|
157
|
+
return {
|
|
158
|
+
type: 'budget_exceeded',
|
|
159
|
+
agentId: snapshot.agentId,
|
|
160
|
+
message: `Token usage ${snapshot.tokenUsage} exceeds budget ${this.config.maxTokenBudget}`,
|
|
161
|
+
severity: 'critical',
|
|
162
|
+
detectedAt: now,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export interface OverseerConfig {
|
|
2
|
+
loopThreshold: number;
|
|
3
|
+
stallTimeoutMs: number;
|
|
4
|
+
maxTokenBudget: number;
|
|
5
|
+
checkIntervalMs: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ToolCall {
|
|
9
|
+
tool: string;
|
|
10
|
+
timestamp: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface AgentSnapshot {
|
|
14
|
+
agentId: string;
|
|
15
|
+
toolCalls: ToolCall[];
|
|
16
|
+
tokenUsage: number;
|
|
17
|
+
lastActivityAt: number;
|
|
18
|
+
startedAt: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type AlertType = 'loop_detected' | 'stall_detected' | 'budget_exceeded';
|
|
22
|
+
|
|
23
|
+
export interface OverseerAlert {
|
|
24
|
+
type: AlertType;
|
|
25
|
+
agentId: string;
|
|
26
|
+
message: string;
|
|
27
|
+
severity: 'warning' | 'critical';
|
|
28
|
+
detectedAt: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type OverseerAction = 'none' | 'alert' | 'notify' | 'cancel';
|
|
32
|
+
|
|
33
|
+
export interface LLMAssessment {
|
|
34
|
+
severity: 'warning' | 'critical';
|
|
35
|
+
reasoning: string;
|
|
36
|
+
suggestedAction: OverseerAction;
|
|
37
|
+
notification?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface AssessmentResult {
|
|
41
|
+
agentId: string;
|
|
42
|
+
heuristicAlerts: OverseerAlert[];
|
|
43
|
+
llmAssessment?: LLMAssessment;
|
|
44
|
+
action: OverseerAction;
|
|
45
|
+
notification?: string;
|
|
46
|
+
assessedAt: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface LLMCallerLike {
|
|
50
|
+
assessWithLLM(alerts: OverseerAlert[], snapshot: AgentSnapshot): Promise<LLMAssessment>;
|
|
51
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
5
|
+
import { AlertStore } from '../src/alert-store.js';
|
|
6
|
+
import type { OverseerAlert } from '../src/types.js';
|
|
7
|
+
|
|
8
|
+
function makeAlert(overrides: Partial<OverseerAlert> = {}): OverseerAlert {
|
|
9
|
+
return {
|
|
10
|
+
type: 'loop_detected',
|
|
11
|
+
agentId: 'agent-1',
|
|
12
|
+
message: 'Loop detected',
|
|
13
|
+
severity: 'warning',
|
|
14
|
+
detectedAt: Date.now(),
|
|
15
|
+
...overrides,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe('AlertStore', () => {
|
|
20
|
+
let dir: string;
|
|
21
|
+
let store: AlertStore;
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
dir = mkdtempSync(join(tmpdir(), 'alert-store-'));
|
|
25
|
+
store = new AlertStore(join(dir, 'alerts.db'));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
store.close();
|
|
30
|
+
rmSync(dir, { recursive: true, force: true });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('stores and retrieves alerts', () => {
|
|
34
|
+
store.record(makeAlert({ message: 'first' }));
|
|
35
|
+
store.record(makeAlert({ message: 'second' }));
|
|
36
|
+
|
|
37
|
+
const recent = store.getRecent(10);
|
|
38
|
+
expect(recent).toHaveLength(2);
|
|
39
|
+
expect(recent[0].id).toBeTypeOf('number');
|
|
40
|
+
expect(recent.map((a) => a.message)).toContain('first');
|
|
41
|
+
expect(recent.map((a) => a.message)).toContain('second');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('filters by agent', () => {
|
|
45
|
+
store.record(makeAlert({ agentId: 'a1' }));
|
|
46
|
+
store.record(makeAlert({ agentId: 'a2' }));
|
|
47
|
+
store.record(makeAlert({ agentId: 'a1' }));
|
|
48
|
+
|
|
49
|
+
const results = store.getByAgent('a1');
|
|
50
|
+
expect(results).toHaveLength(2);
|
|
51
|
+
expect(results.every((a) => a.agentId === 'a1')).toBe(true);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('filters by severity', () => {
|
|
55
|
+
store.record(makeAlert({ severity: 'warning' }));
|
|
56
|
+
store.record(makeAlert({ severity: 'critical' }));
|
|
57
|
+
store.record(makeAlert({ severity: 'critical' }));
|
|
58
|
+
|
|
59
|
+
const results = store.getBySeverity('critical');
|
|
60
|
+
expect(results).toHaveLength(2);
|
|
61
|
+
expect(results.every((a) => a.severity === 'critical')).toBe(true);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('acknowledges alerts', () => {
|
|
65
|
+
store.record(makeAlert());
|
|
66
|
+
store.record(makeAlert());
|
|
67
|
+
|
|
68
|
+
const before = store.getUnacknowledged();
|
|
69
|
+
expect(before).toHaveLength(2);
|
|
70
|
+
|
|
71
|
+
store.acknowledge(before[0].id);
|
|
72
|
+
store.acknowledge(before[1].id);
|
|
73
|
+
|
|
74
|
+
const after = store.getUnacknowledged();
|
|
75
|
+
expect(after).toHaveLength(0);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from 'vitest';
|
|
2
|
+
import { AssessmentStore } from '../src/assessment-store.js';
|
|
3
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import type { AssessmentResult } from '../src/types.js';
|
|
7
|
+
|
|
8
|
+
describe('AssessmentStore', () => {
|
|
9
|
+
let store: AssessmentStore;
|
|
10
|
+
let tmpDir: string;
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
store?.close();
|
|
14
|
+
if (tmpDir) rmSync(tmpDir, { recursive: true, force: true });
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('stores and retrieves assessments', () => {
|
|
18
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'as-'));
|
|
19
|
+
store = new AssessmentStore(join(tmpDir, 'assessments.db'));
|
|
20
|
+
|
|
21
|
+
const assessment: AssessmentResult = {
|
|
22
|
+
agentId: 'agent-1',
|
|
23
|
+
heuristicAlerts: [{ type: 'loop_detected', agentId: 'agent-1', message: 'loop', severity: 'warning', detectedAt: 1000 }],
|
|
24
|
+
action: 'alert',
|
|
25
|
+
assessedAt: Date.now(),
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
store.record(assessment);
|
|
29
|
+
const results = store.getByAgent('agent-1');
|
|
30
|
+
expect(results).toHaveLength(1);
|
|
31
|
+
expect(results[0].action).toBe('alert');
|
|
32
|
+
expect(results[0].heuristicAlerts).toHaveLength(1);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('stores assessments with LLM data', () => {
|
|
36
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'as-'));
|
|
37
|
+
store = new AssessmentStore(join(tmpDir, 'assessments.db'));
|
|
38
|
+
|
|
39
|
+
const assessment: AssessmentResult = {
|
|
40
|
+
agentId: 'agent-2',
|
|
41
|
+
heuristicAlerts: [{ type: 'budget_exceeded', agentId: 'agent-2', message: 'over budget', severity: 'critical', detectedAt: 2000 }],
|
|
42
|
+
llmAssessment: { severity: 'critical', reasoning: 'Agent has used too many tokens', suggestedAction: 'cancel', notification: 'Stop' },
|
|
43
|
+
action: 'cancel',
|
|
44
|
+
notification: 'Stop',
|
|
45
|
+
assessedAt: Date.now(),
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
store.record(assessment);
|
|
49
|
+
const results = store.getByAgent('agent-2');
|
|
50
|
+
expect(results[0].llmAssessment?.reasoning).toBe('Agent has used too many tokens');
|
|
51
|
+
expect(results[0].notification).toBe('Stop');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('filters by action type', () => {
|
|
55
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'as-'));
|
|
56
|
+
store = new AssessmentStore(join(tmpDir, 'assessments.db'));
|
|
57
|
+
|
|
58
|
+
store.record({ agentId: 'a', heuristicAlerts: [], action: 'none', assessedAt: 1000 });
|
|
59
|
+
store.record({ agentId: 'b', heuristicAlerts: [{ type: 'stall_detected', agentId: 'b', message: 'stall', severity: 'warning', detectedAt: 2000 }], action: 'alert', assessedAt: 2000 });
|
|
60
|
+
store.record({ agentId: 'c', heuristicAlerts: [{ type: 'loop_detected', agentId: 'c', message: 'loop', severity: 'critical', detectedAt: 3000 }], action: 'cancel', assessedAt: 3000 });
|
|
61
|
+
|
|
62
|
+
const cancels = store.getByAction('cancel');
|
|
63
|
+
expect(cancels).toHaveLength(1);
|
|
64
|
+
expect(cancels[0].agentId).toBe('c');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('returns recent assessments with limit', () => {
|
|
68
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'as-'));
|
|
69
|
+
store = new AssessmentStore(join(tmpDir, 'assessments.db'));
|
|
70
|
+
|
|
71
|
+
for (let i = 0; i < 5; i++) {
|
|
72
|
+
store.record({ agentId: `a${i}`, heuristicAlerts: [], action: 'none', assessedAt: i * 1000 });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const recent = store.getRecent(2);
|
|
76
|
+
expect(recent).toHaveLength(2);
|
|
77
|
+
expect(recent[0].agentId).toBe('a4');
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { ActiveOverseer } from '../src/llm-overseer.js';
|
|
3
|
+
import type { AgentSnapshot, OverseerConfig } from '../src/types.js';
|
|
4
|
+
|
|
5
|
+
const defaultConfig: OverseerConfig = {
|
|
6
|
+
loopThreshold: 3,
|
|
7
|
+
stallTimeoutMs: 30_000,
|
|
8
|
+
maxTokenBudget: 50_000,
|
|
9
|
+
checkIntervalMs: 5_000,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const makeSnapshot = (overrides: Partial<AgentSnapshot> = {}): AgentSnapshot => ({
|
|
13
|
+
agentId: 'test-agent',
|
|
14
|
+
toolCalls: [],
|
|
15
|
+
tokenUsage: 0,
|
|
16
|
+
lastActivityAt: Date.now(),
|
|
17
|
+
startedAt: Date.now() - 10_000,
|
|
18
|
+
...overrides,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('ActiveOverseer', () => {
|
|
22
|
+
it('returns heuristic alerts when no LLM is provided', async () => {
|
|
23
|
+
const overseer = new ActiveOverseer(defaultConfig);
|
|
24
|
+
|
|
25
|
+
const snapshot = makeSnapshot({
|
|
26
|
+
toolCalls: [
|
|
27
|
+
{ tool: 'read', timestamp: 1 },
|
|
28
|
+
{ tool: 'read', timestamp: 2 },
|
|
29
|
+
{ tool: 'read', timestamp: 3 },
|
|
30
|
+
],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const result = await overseer.assess(snapshot);
|
|
34
|
+
expect(result.heuristicAlerts.length).toBeGreaterThan(0);
|
|
35
|
+
expect(result.llmAssessment).toBeUndefined();
|
|
36
|
+
expect(result.action).toBe('alert');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('calls LLM for assessment when provided and heuristic triggers', async () => {
|
|
40
|
+
const mockLLM = vi.fn().mockResolvedValue({
|
|
41
|
+
severity: 'critical',
|
|
42
|
+
reasoning: 'Agent is stuck in a read loop',
|
|
43
|
+
suggestedAction: 'cancel',
|
|
44
|
+
notification: 'You appear to be repeating the same action. Please try a different approach.',
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const overseer = new ActiveOverseer(defaultConfig, { assessWithLLM: mockLLM });
|
|
48
|
+
|
|
49
|
+
const snapshot = makeSnapshot({
|
|
50
|
+
toolCalls: [
|
|
51
|
+
{ tool: 'read', timestamp: 1 },
|
|
52
|
+
{ tool: 'read', timestamp: 2 },
|
|
53
|
+
{ tool: 'read', timestamp: 3 },
|
|
54
|
+
],
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const result = await overseer.assess(snapshot);
|
|
58
|
+
expect(mockLLM).toHaveBeenCalledTimes(1);
|
|
59
|
+
expect(result.llmAssessment).toBeDefined();
|
|
60
|
+
expect(result.llmAssessment?.suggestedAction).toBe('cancel');
|
|
61
|
+
expect(result.action).toBe('cancel');
|
|
62
|
+
expect(result.notification).toBe('You appear to be repeating the same action. Please try a different approach.');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('skips LLM when heuristic finds no issues', async () => {
|
|
66
|
+
const mockLLM = vi.fn();
|
|
67
|
+
const overseer = new ActiveOverseer(defaultConfig, { assessWithLLM: mockLLM });
|
|
68
|
+
|
|
69
|
+
const snapshot = makeSnapshot();
|
|
70
|
+
const result = await overseer.assess(snapshot);
|
|
71
|
+
|
|
72
|
+
expect(mockLLM).not.toHaveBeenCalled();
|
|
73
|
+
expect(result.heuristicAlerts).toHaveLength(0);
|
|
74
|
+
expect(result.action).toBe('none');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('falls back to heuristic action when LLM fails', async () => {
|
|
78
|
+
const mockLLM = vi.fn().mockRejectedValue(new Error('LLM unavailable'));
|
|
79
|
+
const overseer = new ActiveOverseer(defaultConfig, { assessWithLLM: mockLLM });
|
|
80
|
+
|
|
81
|
+
const snapshot = makeSnapshot({
|
|
82
|
+
tokenUsage: 60_000,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const result = await overseer.assess(snapshot);
|
|
86
|
+
expect(result.heuristicAlerts.length).toBeGreaterThan(0);
|
|
87
|
+
expect(result.llmAssessment).toBeUndefined();
|
|
88
|
+
expect(result.action).toBe('alert');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('records assessments in history', async () => {
|
|
92
|
+
const overseer = new ActiveOverseer(defaultConfig);
|
|
93
|
+
|
|
94
|
+
await overseer.assess(makeSnapshot({ tokenUsage: 60_000 }));
|
|
95
|
+
await overseer.assess(makeSnapshot());
|
|
96
|
+
|
|
97
|
+
const history = overseer.getAssessmentHistory();
|
|
98
|
+
expect(history).toHaveLength(2);
|
|
99
|
+
expect(history[0].action).toBe('alert');
|
|
100
|
+
expect(history[1].action).toBe('none');
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { OverseerMonitor } from '../src/monitor.js';
|
|
4
|
+
import type { AgentSnapshot, OverseerConfig } from '../src/types.js';
|
|
5
|
+
|
|
6
|
+
const baseConfig: OverseerConfig = {
|
|
7
|
+
loopThreshold: 3,
|
|
8
|
+
stallTimeoutMs: 10_000,
|
|
9
|
+
maxTokenBudget: 50_000,
|
|
10
|
+
checkIntervalMs: 5_000,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function makeSnapshot(overrides: Partial<AgentSnapshot> = {}): AgentSnapshot {
|
|
14
|
+
const now = Date.now();
|
|
15
|
+
return {
|
|
16
|
+
agentId: 'agent-1',
|
|
17
|
+
toolCalls: [],
|
|
18
|
+
tokenUsage: 1_000,
|
|
19
|
+
lastActivityAt: now,
|
|
20
|
+
startedAt: now - 5_000,
|
|
21
|
+
...overrides,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('OverseerMonitor', () => {
|
|
26
|
+
it('detects tool call looping (3 same tool = loop with threshold 3)', () => {
|
|
27
|
+
const monitor = new OverseerMonitor(baseConfig);
|
|
28
|
+
const now = Date.now();
|
|
29
|
+
const snapshot = makeSnapshot({
|
|
30
|
+
toolCalls: [
|
|
31
|
+
{ tool: 'search', timestamp: now - 3000 },
|
|
32
|
+
{ tool: 'search', timestamp: now - 2000 },
|
|
33
|
+
{ tool: 'search', timestamp: now - 1000 },
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const alerts = monitor.analyze(snapshot);
|
|
38
|
+
|
|
39
|
+
expect(alerts.length).toBeGreaterThanOrEqual(1);
|
|
40
|
+
const loopAlert = alerts.find(
|
|
41
|
+
(a) => a.type === 'loop_detected' && a.message.includes('search'),
|
|
42
|
+
);
|
|
43
|
+
expect(loopAlert).toBeDefined();
|
|
44
|
+
expect(loopAlert!.severity).toBe('warning');
|
|
45
|
+
expect(loopAlert!.agentId).toBe('agent-1');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('detects stalling agent (15s idle with 10s threshold)', () => {
|
|
49
|
+
const monitor = new OverseerMonitor(baseConfig);
|
|
50
|
+
const now = Date.now();
|
|
51
|
+
vi.spyOn(Date, 'now').mockReturnValue(now);
|
|
52
|
+
|
|
53
|
+
const snapshot = makeSnapshot({
|
|
54
|
+
lastActivityAt: now - 15_000,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const alerts = monitor.analyze(snapshot);
|
|
58
|
+
|
|
59
|
+
vi.restoreAllMocks();
|
|
60
|
+
|
|
61
|
+
const stallAlert = alerts.find((a) => a.type === 'stall_detected');
|
|
62
|
+
expect(stallAlert).toBeDefined();
|
|
63
|
+
expect(stallAlert!.severity).toBe('critical');
|
|
64
|
+
expect(stallAlert!.message).toContain('15s');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('detects token budget exceeded (60k with 50k threshold)', () => {
|
|
68
|
+
const monitor = new OverseerMonitor(baseConfig);
|
|
69
|
+
const snapshot = makeSnapshot({
|
|
70
|
+
tokenUsage: 60_000,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const alerts = monitor.analyze(snapshot);
|
|
74
|
+
|
|
75
|
+
const budgetAlert = alerts.find((a) => a.type === 'budget_exceeded');
|
|
76
|
+
expect(budgetAlert).toBeDefined();
|
|
77
|
+
expect(budgetAlert!.severity).toBe('critical');
|
|
78
|
+
expect(budgetAlert!.message).toContain('60000');
|
|
79
|
+
expect(budgetAlert!.message).toContain('50000');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('returns empty for healthy agent', () => {
|
|
83
|
+
const monitor = new OverseerMonitor(baseConfig);
|
|
84
|
+
const now = Date.now();
|
|
85
|
+
const snapshot = makeSnapshot({
|
|
86
|
+
toolCalls: [
|
|
87
|
+
{ tool: 'search', timestamp: now - 2000 },
|
|
88
|
+
{ tool: 'read', timestamp: now - 1000 },
|
|
89
|
+
],
|
|
90
|
+
tokenUsage: 5_000,
|
|
91
|
+
lastActivityAt: now,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const alerts = monitor.analyze(snapshot);
|
|
95
|
+
|
|
96
|
+
expect(alerts).toEqual([]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('detects repeated consecutive tool pattern (search,read,search,read,search,read)', () => {
|
|
100
|
+
const monitor = new OverseerMonitor(baseConfig);
|
|
101
|
+
const now = Date.now();
|
|
102
|
+
const snapshot = makeSnapshot({
|
|
103
|
+
toolCalls: [
|
|
104
|
+
{ tool: 'search', timestamp: now - 6000 },
|
|
105
|
+
{ tool: 'read', timestamp: now - 5000 },
|
|
106
|
+
{ tool: 'search', timestamp: now - 4000 },
|
|
107
|
+
{ tool: 'read', timestamp: now - 3000 },
|
|
108
|
+
{ tool: 'search', timestamp: now - 2000 },
|
|
109
|
+
{ tool: 'read', timestamp: now - 1000 },
|
|
110
|
+
],
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const alerts = monitor.analyze(snapshot);
|
|
114
|
+
|
|
115
|
+
const patternAlert = alerts.find(
|
|
116
|
+
(a) => a.type === 'loop_detected' && a.message.includes('pattern'),
|
|
117
|
+
);
|
|
118
|
+
expect(patternAlert).toBeDefined();
|
|
119
|
+
expect(patternAlert!.message).toContain('search');
|
|
120
|
+
expect(patternAlert!.message).toContain('read');
|
|
121
|
+
});
|
|
122
|
+
});
|