agent-working-memory 0.7.16 → 0.8.5
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 +156 -15
- package/dist/adapters/claude-code.d.ts.map +1 -1
- package/dist/adapters/claude-code.js +2 -16
- package/dist/adapters/claude-code.js.map +1 -1
- package/dist/adapters/codex.d.ts.map +1 -1
- package/dist/adapters/codex.js +2 -11
- package/dist/adapters/codex.js.map +1 -1
- package/dist/adapters/common.d.ts +18 -0
- package/dist/adapters/common.d.ts.map +1 -1
- package/dist/adapters/common.js +254 -141
- package/dist/adapters/common.js.map +1 -1
- package/dist/adapters/cursor.d.ts.map +1 -1
- package/dist/adapters/cursor.js +2 -15
- package/dist/adapters/cursor.js.map +1 -1
- package/dist/adapters/http.d.ts.map +1 -1
- package/dist/adapters/http.js +28 -34
- package/dist/adapters/http.js.map +1 -1
- package/dist/api/routes.d.ts +1 -1
- package/dist/api/routes.d.ts.map +1 -1
- package/dist/api/routes.js +280 -113
- package/dist/api/routes.js.map +1 -1
- package/dist/cli/migrate.d.ts +42 -0
- package/dist/cli/migrate.d.ts.map +1 -0
- package/dist/cli/migrate.js +268 -0
- package/dist/cli/migrate.js.map +1 -0
- package/dist/cli.js +45 -1
- package/dist/cli.js.map +1 -1
- package/dist/coordination/circuit-breaker.d.ts +24 -0
- package/dist/coordination/circuit-breaker.d.ts.map +1 -0
- package/dist/coordination/circuit-breaker.js +70 -0
- package/dist/coordination/circuit-breaker.js.map +1 -0
- package/dist/coordination/failure-modes.d.ts +21 -0
- package/dist/coordination/failure-modes.d.ts.map +1 -0
- package/dist/coordination/failure-modes.js +49 -0
- package/dist/coordination/failure-modes.js.map +1 -0
- package/dist/coordination/peer-decisions.js +7 -7
- package/dist/coordination/routes.d.ts.map +1 -1
- package/dist/coordination/routes.js +44 -1
- package/dist/coordination/routes.js.map +1 -1
- package/dist/coordination/schema.d.ts.map +1 -1
- package/dist/coordination/schema.js +18 -0
- package/dist/coordination/schema.js.map +1 -1
- package/dist/coordination/schemas.d.ts +9 -9
- package/dist/coordination/stale.d.ts +9 -1
- package/dist/coordination/stale.d.ts.map +1 -1
- package/dist/coordination/stale.js +53 -9
- package/dist/coordination/stale.js.map +1 -1
- package/dist/core/embeddings.d.ts +16 -10
- package/dist/core/embeddings.d.ts.map +1 -1
- package/dist/core/embeddings.js +54 -38
- package/dist/core/embeddings.js.map +1 -1
- package/dist/core/ml-worker-entry.d.ts +17 -0
- package/dist/core/ml-worker-entry.d.ts.map +1 -0
- package/dist/core/ml-worker-entry.js +193 -0
- package/dist/core/ml-worker-entry.js.map +1 -0
- package/dist/core/ml-worker.d.ts +59 -0
- package/dist/core/ml-worker.d.ts.map +1 -0
- package/dist/core/ml-worker.js +253 -0
- package/dist/core/ml-worker.js.map +1 -0
- package/dist/core/query-expander.d.ts +13 -15
- package/dist/core/query-expander.d.ts.map +1 -1
- package/dist/core/query-expander.js +48 -54
- package/dist/core/query-expander.js.map +1 -1
- package/dist/core/reranker.d.ts +9 -19
- package/dist/core/reranker.d.ts.map +1 -1
- package/dist/core/reranker.js +33 -35
- package/dist/core/reranker.js.map +1 -1
- package/dist/core/salience.d.ts +32 -5
- package/dist/core/salience.d.ts.map +1 -1
- package/dist/core/salience.js +201 -28
- package/dist/core/salience.js.map +1 -1
- package/dist/core/write-pipeline.d.ts +128 -0
- package/dist/core/write-pipeline.d.ts.map +1 -0
- package/dist/core/write-pipeline.js +405 -0
- package/dist/core/write-pipeline.js.map +1 -0
- package/dist/engine/activation.d.ts +2 -2
- package/dist/engine/activation.d.ts.map +1 -1
- package/dist/engine/activation.js +238 -115
- package/dist/engine/activation.js.map +1 -1
- package/dist/engine/confidence.d.ts +62 -0
- package/dist/engine/confidence.d.ts.map +1 -0
- package/dist/engine/confidence.js +100 -0
- package/dist/engine/confidence.js.map +1 -0
- package/dist/engine/connections.d.ts +47 -8
- package/dist/engine/connections.d.ts.map +1 -1
- package/dist/engine/connections.js +83 -22
- package/dist/engine/connections.js.map +1 -1
- package/dist/engine/consolidation-scheduler.d.ts +46 -10
- package/dist/engine/consolidation-scheduler.d.ts.map +1 -1
- package/dist/engine/consolidation-scheduler.js +170 -50
- package/dist/engine/consolidation-scheduler.js.map +1 -1
- package/dist/engine/consolidation.d.ts +16 -2
- package/dist/engine/consolidation.d.ts.map +1 -1
- package/dist/engine/consolidation.js +143 -44
- package/dist/engine/consolidation.js.map +1 -1
- package/dist/engine/eval.d.ts +2 -2
- package/dist/engine/eval.d.ts.map +1 -1
- package/dist/engine/eval.js +9 -9
- package/dist/engine/eval.js.map +1 -1
- package/dist/engine/eviction.d.ts +4 -4
- package/dist/engine/eviction.d.ts.map +1 -1
- package/dist/engine/eviction.js +15 -15
- package/dist/engine/eviction.js.map +1 -1
- package/dist/engine/retraction.d.ts +93 -5
- package/dist/engine/retraction.d.ts.map +1 -1
- package/dist/engine/retraction.js +238 -19
- package/dist/engine/retraction.js.map +1 -1
- package/dist/engine/staging.d.ts +1 -1
- package/dist/engine/staging.d.ts.map +1 -1
- package/dist/engine/staging.js +3 -3
- package/dist/engine/staging.js.map +1 -1
- package/dist/hooks/sidecar.d.ts +1 -1
- package/dist/hooks/sidecar.d.ts.map +1 -1
- package/dist/hooks/sidecar.js +4 -4
- package/dist/hooks/sidecar.js.map +1 -1
- package/dist/index.js +48 -31
- package/dist/index.js.map +1 -1
- package/dist/mcp.js +110 -155
- package/dist/mcp.js.map +1 -1
- package/dist/storage/factory.d.ts +55 -0
- package/dist/storage/factory.d.ts.map +1 -0
- package/dist/storage/factory.js +135 -0
- package/dist/storage/factory.js.map +1 -0
- package/dist/storage/pglite-schema.d.ts +20 -0
- package/dist/storage/pglite-schema.d.ts.map +1 -0
- package/dist/storage/pglite-schema.js +165 -0
- package/dist/storage/pglite-schema.js.map +1 -0
- package/dist/storage/pglite.d.ts +187 -0
- package/dist/storage/pglite.d.ts.map +1 -0
- package/dist/storage/pglite.js +1114 -0
- package/dist/storage/pglite.js.map +1 -0
- package/dist/storage/sqlite.d.ts +117 -0
- package/dist/storage/sqlite.d.ts.map +1 -1
- package/dist/storage/sqlite.js +357 -8
- package/dist/storage/sqlite.js.map +1 -1
- package/dist/storage/store.d.ts +63 -0
- package/dist/storage/store.d.ts.map +1 -0
- package/dist/storage/store.js +27 -0
- package/dist/storage/store.js.map +1 -0
- package/dist/types/engram.d.ts +92 -2
- package/dist/types/engram.d.ts.map +1 -1
- package/dist/types/engram.js.map +1 -1
- package/package.json +3 -1
- package/src/adapters/claude-code.ts +218 -234
- package/src/adapters/codex.ts +252 -262
- package/src/adapters/common.ts +411 -284
- package/src/adapters/cursor.ts +135 -150
- package/src/adapters/http.ts +93 -100
- package/src/adapters/index.ts +31 -31
- package/src/adapters/types.ts +75 -75
- package/src/api/index.ts +3 -3
- package/src/api/routes.ts +367 -120
- package/src/cli/migrate.ts +307 -0
- package/src/cli.ts +37 -1
- package/src/coordination/circuit-breaker.ts +83 -0
- package/src/coordination/events.ts +90 -90
- package/src/coordination/failure-modes.ts +50 -0
- package/src/coordination/peer-decisions.ts +105 -105
- package/src/coordination/plugin-loader.ts +60 -60
- package/src/coordination/plugin.ts +44 -44
- package/src/coordination/routes.ts +56 -1
- package/src/coordination/schema.ts +13 -0
- package/src/coordination/stale.ts +80 -11
- package/src/coordination/types.ts +311 -311
- package/src/coordination/write-mutex.ts +69 -69
- package/src/core/auto-tagger.ts +168 -168
- package/src/core/decay.ts +63 -63
- package/src/core/embeddings.ts +110 -93
- package/src/core/index.ts +5 -5
- package/src/core/logger.ts +36 -36
- package/src/core/ml-worker-entry.ts +194 -0
- package/src/core/ml-worker.ts +281 -0
- package/src/core/query-expander.ts +122 -128
- package/src/core/reranker.ts +119 -125
- package/src/core/salience.ts +200 -33
- package/src/core/write-pipeline.ts +521 -0
- package/src/engine/activation.ts +235 -109
- package/src/engine/confidence.ts +120 -0
- package/src/engine/connections.ts +162 -103
- package/src/engine/consolidation-scheduler.ts +242 -125
- package/src/engine/consolidation.ts +138 -45
- package/src/engine/eval.ts +102 -102
- package/src/engine/eviction.ts +101 -101
- package/src/engine/index.ts +8 -8
- package/src/engine/retraction.ts +366 -116
- package/src/engine/staging.ts +74 -74
- package/src/hooks/sidecar.ts +5 -5
- package/src/index.ts +42 -28
- package/src/mcp.ts +105 -152
- package/src/storage/factory.ts +147 -0
- package/src/storage/index.ts +3 -3
- package/src/storage/pglite-schema.ts +166 -0
- package/src/storage/pglite.ts +1363 -0
- package/src/storage/sqlite.ts +402 -7
- package/src/storage/store.ts +80 -0
- package/src/types/agent.ts +67 -67
- package/src/types/checkpoint.ts +46 -46
- package/src/types/engram.ts +101 -3
- package/src/types/eval.ts +100 -100
- package/src/types/index.ts +6 -6
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type Database from 'better-sqlite3';
|
|
8
|
+
import { classifyFailure, FailureMode, MUTATION_HINTS } from './failure-modes.js';
|
|
9
|
+
import { recordFailure as circuitRecordFailure } from './circuit-breaker.js';
|
|
8
10
|
|
|
9
11
|
interface StaleAgent {
|
|
10
12
|
id: string;
|
|
@@ -26,22 +28,89 @@ export function detectStale(db: Database.Database, thresholdSeconds: number): St
|
|
|
26
28
|
).all(thresholdSeconds) as StaleAgent[];
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
/**
|
|
31
|
+
/** Maximum retry attempts before an assignment is permanently failed. */
|
|
32
|
+
const MAX_RETRY_ATTEMPTS = 3;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Retry or permanently fail a single assignment.
|
|
36
|
+
* Called from cleanupStale (stale path) and POST /assignment/:id/fail (voluntary path).
|
|
37
|
+
*
|
|
38
|
+
* @returns 'retried' | 'failed'
|
|
39
|
+
*/
|
|
40
|
+
export function retryOrFailAssignment(
|
|
41
|
+
db: Database.Database,
|
|
42
|
+
assignmentId: string,
|
|
43
|
+
agentId: string,
|
|
44
|
+
failureResult: string,
|
|
45
|
+
mode?: FailureMode,
|
|
46
|
+
): 'retried' | 'failed' {
|
|
47
|
+
const row = db.prepare(
|
|
48
|
+
`SELECT attempt_count, description FROM coord_assignments WHERE id = ?`
|
|
49
|
+
).get(assignmentId) as { attempt_count: number; description: string | null } | undefined;
|
|
50
|
+
|
|
51
|
+
const attemptCount = row?.attempt_count ?? 0;
|
|
52
|
+
const failureMode = mode ?? classifyFailure(failureResult);
|
|
53
|
+
|
|
54
|
+
if (attemptCount < MAX_RETRY_ATTEMPTS) {
|
|
55
|
+
const hint = MUTATION_HINTS[failureMode];
|
|
56
|
+
const nextAttempt = attemptCount + 1;
|
|
57
|
+
const hintBlock = `\n\n--- RETRY HINT (attempt ${nextAttempt}) ---\n${hint}`;
|
|
58
|
+
const newDescription = (row?.description ?? '') + hintBlock;
|
|
59
|
+
|
|
60
|
+
db.prepare(
|
|
61
|
+
`UPDATE coord_assignments
|
|
62
|
+
SET status = 'pending', agent_id = NULL, started_at = NULL, result = NULL,
|
|
63
|
+
attempt_count = ?, last_failure_mode = ?, description = ?
|
|
64
|
+
WHERE id = ?`
|
|
65
|
+
).run(nextAttempt, failureMode, newDescription, assignmentId);
|
|
66
|
+
|
|
67
|
+
db.prepare(
|
|
68
|
+
`INSERT INTO coord_events (agent_id, event_type, detail) VALUES (?, 'assignment_retry', ?)`
|
|
69
|
+
).run(agentId, `attempt ${nextAttempt}: ${failureMode} — ${failureResult.slice(0, 200)}`);
|
|
70
|
+
|
|
71
|
+
return 'retried';
|
|
72
|
+
} else {
|
|
73
|
+
db.prepare(
|
|
74
|
+
`UPDATE coord_assignments
|
|
75
|
+
SET status = 'failed', result = ?, last_failure_mode = ?, completed_at = datetime('now')
|
|
76
|
+
WHERE id = ?`
|
|
77
|
+
).run(failureResult, failureMode, assignmentId);
|
|
78
|
+
|
|
79
|
+
db.prepare(
|
|
80
|
+
`INSERT INTO coord_events (agent_id, event_type, detail) VALUES (?, 'assignment_failed', ?)`
|
|
81
|
+
).run(agentId, `permanently failed after ${attemptCount} attempt(s): ${failureResult.slice(0, 200)}`);
|
|
82
|
+
|
|
83
|
+
return 'failed';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Clean up stale agents: retry or fail assignments, release locks, mark dead. */
|
|
30
88
|
export function cleanupStale(db: Database.Database, thresholdSeconds: number): { stale: StaleAgent[]; cleaned: number } {
|
|
31
89
|
const stale = detectStale(db, thresholdSeconds);
|
|
32
90
|
let cleaned = 0;
|
|
33
91
|
|
|
34
92
|
for (const agent of stale) {
|
|
35
|
-
//
|
|
36
|
-
const
|
|
37
|
-
`
|
|
38
|
-
|
|
39
|
-
|
|
93
|
+
// Handle each orphaned assignment with retry logic
|
|
94
|
+
const orphanedRows = db.prepare(
|
|
95
|
+
`SELECT id FROM coord_assignments WHERE agent_id = ? AND status IN ('assigned', 'in_progress')`
|
|
96
|
+
).all(agent.id) as Array<{ id: string }>;
|
|
97
|
+
|
|
98
|
+
let retried = 0;
|
|
99
|
+
let failed = 0;
|
|
100
|
+
|
|
101
|
+
for (const row of orphanedRows) {
|
|
102
|
+
const outcome = retryOrFailAssignment(db, row.id, agent.id, 'agent disconnected (stale)');
|
|
103
|
+
if (outcome === 'retried') retried++;
|
|
104
|
+
else failed++;
|
|
105
|
+
}
|
|
40
106
|
|
|
41
|
-
if (
|
|
107
|
+
if (orphanedRows.length > 0) {
|
|
42
108
|
db.prepare(
|
|
43
109
|
`INSERT INTO coord_events (agent_id, event_type, detail) VALUES (?, 'assignment_failed', ?)`
|
|
44
|
-
).run(agent.id, `
|
|
110
|
+
).run(agent.id, `stale cleanup: retried ${retried}, permanently failed ${failed}`);
|
|
111
|
+
|
|
112
|
+
// Record failure in circuit breaker for each orphaned assignment
|
|
113
|
+
circuitRecordFailure(db, agent.id);
|
|
45
114
|
}
|
|
46
115
|
|
|
47
116
|
// Release locks
|
|
@@ -50,12 +119,12 @@ export function cleanupStale(db: Database.Database, thresholdSeconds: number): {
|
|
|
50
119
|
// Mark dead
|
|
51
120
|
db.prepare(`UPDATE coord_agents SET status = 'dead', current_task = NULL WHERE id = ?`).run(agent.id);
|
|
52
121
|
|
|
53
|
-
cleaned +=
|
|
122
|
+
cleaned += orphanedRows.length + locks.changes;
|
|
54
123
|
|
|
55
|
-
if (
|
|
124
|
+
if (orphanedRows.length > 0 || locks.changes > 0) {
|
|
56
125
|
db.prepare(
|
|
57
126
|
`INSERT INTO coord_events (agent_id, event_type, detail) VALUES (?, 'stale_cleanup', ?)`
|
|
58
|
-
).run(agent.id, `failed ${
|
|
127
|
+
).run(agent.id, `failed ${orphanedRows.length} assignment(s), released ${locks.changes} lock(s)`);
|
|
59
128
|
}
|
|
60
129
|
}
|
|
61
130
|
|