|
1 | | -// Memory Core owns admission-ordered sync outcome reporting. |
| 1 | +// Memory Core owns process-local sync outcome reporting. |
2 | 2 | import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime"; |
3 | 3 | import { redactSensitiveText } from "openclaw/plugin-sdk/security-runtime"; |
4 | 4 |
|
5 | 5 | export class MemorySyncOutcomeLedger { |
6 | | - private nextGeneration = 0; |
7 | | - private latestSuccessfulGeneration = 0; |
8 | | - private latestFailure?: { generation: number; reason: string }; |
9 | | - private activeGeneration?: number; |
| 6 | + private failureRevision = 0; |
| 7 | + private latestFailure?: { revision: number; reason: string }; |
| 8 | + private active = false; |
10 | 9 |
|
11 | 10 | async track(operation: () => Promise<string | undefined | void>, active = false): Promise<void> { |
12 | | - const generation = ++this.nextGeneration; |
| 11 | + const failureAtStart = this.latestFailure?.revision; |
13 | 12 | if (active) { |
14 | | - this.activeGeneration = generation; |
| 13 | + this.active = true; |
15 | 14 | } |
16 | 15 | try { |
17 | 16 | const incompleteReason = await operation(); |
18 | 17 | if (incompleteReason) { |
19 | | - this.recordFailure(generation, incompleteReason); |
20 | | - } else if (this.latestFailure?.generation !== generation) { |
21 | | - this.latestSuccessfulGeneration = Math.max(this.latestSuccessfulGeneration, generation); |
| 18 | + this.recordFailure(incompleteReason); |
| 19 | + } else if (failureAtStart !== undefined && this.latestFailure?.revision === failureAtStart) { |
| 20 | + this.latestFailure = undefined; |
22 | 21 | } |
23 | 22 | } catch (error) { |
24 | | - this.recordFailure(generation, error); |
| 23 | + this.recordFailure(error); |
25 | 24 | throw error; |
26 | 25 | } finally { |
27 | | - if (this.activeGeneration === generation) { |
28 | | - this.activeGeneration = undefined; |
| 26 | + if (active) { |
| 27 | + this.active = false; |
29 | 28 | } |
30 | 29 | } |
31 | 30 | } |
32 | 31 |
|
33 | 32 | recordActiveFailure(error: unknown): void { |
34 | | - if (this.activeGeneration !== undefined) { |
35 | | - this.recordFailure(this.activeGeneration, error); |
| 33 | + if (this.active) { |
| 34 | + this.recordFailure(error); |
36 | 35 | } |
37 | 36 | } |
38 | 37 |
|
39 | 38 | get lastError(): string | undefined { |
40 | | - return this.latestFailure && this.latestFailure.generation > this.latestSuccessfulGeneration |
41 | | - ? this.latestFailure.reason |
42 | | - : undefined; |
| 39 | + return this.latestFailure?.reason; |
43 | 40 | } |
44 | 41 |
|
45 | | - private recordFailure(generation: number, error: unknown): void { |
46 | | - if (generation <= (this.latestFailure?.generation ?? 0)) { |
47 | | - return; |
48 | | - } |
| 42 | + private recordFailure(error: unknown): void { |
49 | 43 | this.latestFailure = { |
50 | | - generation, |
| 44 | + revision: ++this.failureRevision, |
51 | 45 | reason: redactSensitiveText(formatErrorMessage(error), { mode: "tools" }), |
52 | 46 | }; |
53 | 47 | } |
|
0 commit comments