@tpsdev-ai/flair 0.62.0 → 0.63.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 +14 -13
- package/SECURITY.md +73 -43
- package/config.yaml +11 -11
- package/dist/bridges/runtime/execute.js +1 -0
- package/dist/bridges/runtime/import-runner.js +4 -0
- package/dist/bridges/types.js +1 -0
- package/dist/build-info.json +3 -3
- package/dist/cli.js +309 -205
- package/dist/commands/agent.js +325 -137
- package/dist/commands/attention.js +2 -0
- package/dist/commands/backup-approval.js +218 -0
- package/dist/commands/backup.js +37 -33
- package/dist/commands/bootstrap.js +5 -1
- package/dist/commands/bridge.js +10 -0
- package/dist/commands/deploy.js +37 -7
- package/dist/commands/doctor.js +206 -42
- package/dist/commands/export.js +144 -15
- package/dist/commands/federation.js +653 -68
- package/dist/commands/grant.js +5 -6
- package/dist/commands/help.js +25 -0
- package/dist/commands/idp.js +14 -19
- package/dist/commands/import.js +167 -34
- package/dist/commands/init.js +380 -164
- package/dist/commands/integrity.js +5 -6
- package/dist/commands/keys.js +12 -8
- package/dist/commands/mcp.js +157 -79
- package/dist/commands/memory.js +38 -17
- package/dist/commands/migrate-harness-memory.js +9 -3
- package/dist/commands/orgevent.js +5 -10
- package/dist/commands/principal.js +32 -39
- package/dist/commands/quality.js +26 -3
- package/dist/commands/reembed.js +468 -111
- package/dist/commands/rem.js +484 -65
- package/dist/commands/restore.js +352 -44
- package/dist/commands/search.js +2 -0
- package/dist/commands/service.js +109 -2
- package/dist/commands/soul.js +0 -6
- package/dist/commands/status.js +165 -124
- package/dist/commands/test.js +4 -1
- package/dist/commands/upgrade.js +53 -22
- package/dist/commands/workspace.js +7 -8
- package/dist/deploy.js +59 -3
- package/dist/doctor-client.js +9 -25
- package/dist/fabric-upgrade.js +12 -2
- package/dist/federation/presence-send.js +139 -0
- package/dist/federation/scheduler.js +148 -12
- package/dist/federation-verify.js +269 -177
- package/dist/install/clients.js +171 -35
- package/dist/keystore.js +77 -10
- package/dist/lib/agent-id-rule.js +44 -2
- package/dist/lib/agent-key-file.js +509 -0
- package/dist/lib/agent-rotation-lock.js +249 -0
- package/dist/lib/atomic-write.js +3 -3
- package/dist/lib/auth-resolve.js +1040 -64
- package/dist/lib/backup-approval-store.js +612 -0
- package/dist/lib/backup-artifact.js +343 -0
- package/dist/lib/config-critical-section.js +212 -25
- package/dist/lib/connect-phase.js +33 -0
- package/dist/lib/daemon-liveness.js +2 -1
- package/dist/lib/federation-pair-access.js +4 -0
- package/dist/lib/federation-retry-queue.js +283 -0
- package/dist/lib/federation-retry-row.js +101 -0
- package/dist/lib/instruction-event-contract.js +17 -0
- package/dist/lib/instruction-event-doctor.js +102 -0
- package/dist/lib/key-dir.js +203 -0
- package/dist/lib/launchd-management.js +21 -6
- package/dist/lib/local-instance-pair.js +8 -1
- package/dist/lib/mcp-enable.js +14 -15
- package/dist/lib/memory-attributes.js +2 -0
- package/dist/lib/memory-provider-timeout.js +33 -0
- package/dist/lib/mqtt-config.js +132 -0
- package/dist/lib/mqtt-listener-doctor.js +104 -0
- package/dist/lib/node-alias-path.js +50 -2
- package/dist/lib/open-live-row.js +20 -0
- package/dist/lib/ops-url.js +52 -0
- package/dist/lib/promotion-content-hash.js +20 -0
- package/dist/lib/remote-admin-pass.js +157 -0
- package/dist/lib/restore-archive.js +760 -0
- package/dist/lib/restore-verify.js +1 -1
- package/dist/lib/scheduler-platform.js +13 -0
- package/dist/lib/service-repoint.js +13 -4
- package/dist/lib/skill-assignment-preflight.js +159 -0
- package/dist/lib/skill-seed.js +125 -10
- package/dist/lib/skill-tombstone-doctor.js +133 -0
- package/dist/lib/soul-scope-fields.js +26 -0
- package/dist/lib/terminal-text.js +32 -0
- package/dist/lib/uninstall-purge.js +266 -11
- package/dist/lib/upgrade-prior-liveness.js +2 -1
- package/dist/mcp-client-assertion.js +7 -14
- package/dist/memory-id-checkpoint.js +28 -7
- package/dist/mqtt-off-deploy.js +550 -0
- package/dist/rem/promote-policy.js +3 -3
- package/dist/rem/restore.js +302 -26
- package/dist/rem/runner.js +220 -58
- package/dist/rem/scheduler.js +21 -4
- package/dist/resources/A2AAdapter.js +131 -178
- package/dist/resources/AdminMemory.js +2 -0
- package/dist/resources/AdminMemoryCandidates.js +116 -0
- package/dist/resources/Agent.js +155 -45
- package/dist/resources/AgentCard.js +4 -0
- package/dist/resources/AgentKeyRegister.js +56 -22
- package/dist/resources/AgentReadPosition.js +7 -0
- package/dist/resources/AgentSeed.js +232 -107
- package/dist/resources/AutoPromoteCandidates.js +413 -84
- package/dist/resources/Credential.js +6 -8
- package/dist/resources/Federation.js +219 -57
- package/dist/resources/FederationRetry.js +4 -4
- package/dist/resources/InstructionVersion.js +1 -1
- package/dist/resources/MCPClientMetadata.js +6 -3
- package/dist/resources/Memory.js +1348 -271
- package/dist/resources/MemoryArchive.js +149 -64
- package/dist/resources/MemoryBootstrap.js +12 -22
- package/dist/resources/MemoryCandidate.js +16 -2
- package/dist/resources/MemoryConsolidate.js +2 -2
- package/dist/resources/MemoryFeed.js +360 -147
- package/dist/resources/MemoryMaintenance.js +290 -72
- package/dist/resources/MemoryPurge.js +138 -22
- package/dist/resources/MemoryReflect.js +68 -14
- package/dist/resources/MemoryReindex.js +91 -13
- package/dist/resources/Message.js +11 -2
- package/dist/resources/OAuth.js +156 -39
- package/dist/resources/OAuthClient.js +1 -1
- package/dist/resources/OrgEvent.js +292 -29
- package/dist/resources/OrgEventCatchup.js +7 -0
- package/dist/resources/OrgEventMaintenance.js +48 -10
- package/dist/resources/PairingToken.js +1 -1
- package/dist/resources/Presence.js +31 -11
- package/dist/resources/PromoteMemoryCandidate.js +8 -2
- package/dist/resources/RecordUsage.js +2 -4
- package/dist/resources/Soul.js +2 -0
- package/dist/resources/SoulFeed.js +2 -1
- package/dist/resources/WorkspaceLatest.js +4 -0
- package/dist/resources/XAA.js +36 -10
- package/dist/resources/a2a-stream.js +313 -0
- package/dist/resources/a2a-url.js +4 -5
- package/dist/resources/agent-admin.js +10 -1
- package/dist/resources/agent-auth.js +98 -68
- package/dist/resources/agent-read-position-lib.js +4 -1
- package/dist/resources/agent-status-guard.js +9 -7
- package/dist/resources/auth-middleware.js +166 -42
- package/dist/resources/authority-field-guard.js +9 -11
- package/dist/resources/auto-promote-lib.js +8 -5
- package/dist/resources/basic-auth-header.js +21 -0
- package/dist/resources/bm25-index-service.js +4 -5
- package/dist/resources/dedup-cluster.js +6 -1
- package/dist/resources/embeddings-provider.js +25 -0
- package/dist/resources/federation-classify.js +89 -0
- package/dist/resources/federation-retry-lib.js +6 -81
- package/dist/resources/health.js +75 -19
- package/dist/resources/host-pointer/registry.js +29 -0
- package/dist/resources/host-pointer-adapter.js +1 -0
- package/dist/resources/in-process-api.js +74 -11
- package/dist/resources/in-process.js +24 -22
- package/dist/resources/instruction-version-record.js +251 -7
- package/dist/resources/internal-authority.js +13 -0
- package/dist/resources/mcp-client-metadata-fields.js +4 -0
- package/dist/resources/mcp-handler.js +146 -13
- package/dist/resources/mcp-oauth-flag.js +12 -14
- package/dist/resources/mcp-oauth.js +446 -53
- package/dist/resources/mcp-tool-scopes.js +69 -0
- package/dist/resources/mcp-tools.js +70 -22
- package/dist/resources/memory-declared-attributes.js +1 -0
- package/dist/resources/memory-embed-hash.js +56 -0
- package/dist/resources/memory-host-source.js +5 -2
- package/dist/resources/memory-reflect-lib.js +1 -1
- package/dist/resources/memory-write-lock.js +202 -23
- package/dist/resources/memory-write-response.js +1 -1
- package/dist/resources/memory-write-transaction.js +243 -0
- package/dist/resources/migration-boot.js +17 -12
- package/dist/resources/migrations/boot-progress.js +25 -0
- package/dist/resources/migrations/data-dir.js +16 -3
- package/dist/resources/migrations/dir-safety.js +60 -2
- package/dist/resources/migrations/embedding-stamp.js +315 -49
- package/dist/resources/migrations/graph-heal.js +9 -2
- package/dist/resources/migrations/ledger.js +5 -2
- package/dist/resources/migrations/lock.js +867 -80
- package/dist/resources/migrations/progress.js +71 -10
- package/dist/resources/migrations/runner.js +212 -18
- package/dist/resources/migrations/state.js +36 -1
- package/dist/resources/migrations/status.js +2 -13
- package/dist/resources/migrations/synthetic-test-migration.js +78 -12
- package/dist/resources/migrations/types.js +26 -1
- package/dist/resources/migrations/visibility-backfill.js +111 -70
- package/dist/resources/oauth-discovery.js +0 -5
- package/dist/resources/object-body-guard.js +12 -5
- package/dist/resources/org-event-reserved.js +92 -0
- package/dist/resources/org-event-write-fault.js +19 -0
- package/dist/resources/owner-field-guard.js +1 -1
- package/dist/resources/owner-scoped-write-policy.js +65 -21
- package/dist/resources/owner-scoped-writes.js +37 -6
- package/dist/resources/path-id-rewrite.js +76 -0
- package/dist/resources/presence-internal.js +5 -5
- package/dist/resources/promotion-binding.js +43 -0
- package/dist/resources/promotion-stamp.js +124 -33
- package/dist/resources/protocol-context.js +189 -0
- package/dist/resources/record-type-kit.js +2 -4
- package/dist/resources/request-target.js +28 -1
- package/dist/resources/request-transaction.js +11 -1
- package/dist/resources/seed-reservation.js +1 -2
- package/dist/resources/semantic-retrieval-core.js +7 -6
- package/dist/resources/server-row-validation.js +71 -0
- package/dist/resources/skill-manifest.js +4 -3
- package/dist/resources/skill-version-write.js +400 -27
- package/dist/resources/skill-write-fault.js +6 -3
- package/dist/resources/skill-write-policy.js +2 -1
- package/dist/resources/soul-write-policy.js +26 -7
- package/dist/resources/table-helpers.js +1 -1
- package/dist/resources/table-subscription-policy.js +518 -85
- package/dist/resources/table-subscriptions.js +20 -7
- package/dist/resources/txn-pause-point.js +122 -5
- package/dist/resources/usage-recording.js +54 -46
- package/dist/resources/write-back.js +176 -11
- package/dist/resources/write-journal-context.js +5 -0
- package/dist/resources/write-journal.js +289 -0
- package/dist/src/keystore.js +77 -10
- package/dist/src/lib/agent-id-rule.js +44 -2
- package/dist/src/lib/atomic-write.js +3 -3
- package/dist/src/lib/federation-retry-row.js +101 -0
- package/dist/src/lib/instruction-event-contract.js +17 -0
- package/dist/src/lib/key-dir.js +203 -0
- package/dist/src/lib/memory-attributes.js +2 -0
- package/dist/src/lib/open-live-row.js +20 -0
- package/dist/src/lib/scheduler-platform.js +13 -0
- package/dist/src/lib/soul-scope-fields.js +26 -0
- package/dist/src/rem/promote-policy.js +3 -3
- package/dist/src/rem/scheduler.js +21 -4
- package/docs/access-control.md +39 -21
- package/docs/api-reference.md +161 -52
- package/docs/auth.md +69 -38
- package/docs/claude-code.md +3 -3
- package/docs/deploying-on-fabric.md +118 -2
- package/docs/deployment.md +17 -0
- package/docs/embedding-in-a-harper-app.md +23 -21
- package/docs/entity-vocabulary.md +7 -11
- package/docs/federation.md +29 -2
- package/docs/hosted-on-fabric.md +1 -1
- package/docs/integrations.md +11 -11
- package/docs/mcp-clients.md +7 -5
- package/docs/notes/mcp-agent-auth-consumer.md +4 -9
- package/docs/notes/mcp-oauth-model2.md +6 -6
- package/docs/openclaw.md +1 -1
- package/docs/quickstart-fabric.md +2 -2
- package/docs/quickstart.md +3 -3
- package/docs/releasing.md +13 -13
- package/docs/rem.md +24 -3
- package/docs/secrets-and-keys.md +9 -14
- package/docs/standalone-local.md +29 -2
- package/docs/supply-chain-policy.md +28 -20
- package/docs/the-team.md +2 -2
- package/docs/upgrade.md +29 -5
- package/package.json +18 -11
- package/schemas/agent.graphql +3 -3
- package/schemas/federation.graphql +10 -7
- package/schemas/memory.graphql +7 -2
- package/templates/bin/flair-rem-nightly.sh.tmpl +7 -1
- package/templates/launchd/start-flair-with-admin-pass.sh +188 -10
- package/dist/resources/soul-adk-guard.js +0 -68
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { readMigrationState } from "./state.js";
|
|
1
2
|
let cycleStatus = { phase: "idle" };
|
|
2
3
|
const progressById = new Map();
|
|
4
|
+
/** Ids whose entry `seedProgressFromState` built from the state file and
|
|
5
|
+
* `setMigrationProgress` has not written since (flair#2840). */
|
|
6
|
+
const seededFromState = new Set();
|
|
3
7
|
let stateFileStatus = { path: null, lastWriteError: null };
|
|
4
8
|
/** Records the resolved state path on each write ATTEMPT (success or failure). */
|
|
5
9
|
export function noteStateWriteAttempt(path) {
|
|
@@ -24,6 +28,7 @@ export function getCycleStatus() {
|
|
|
24
28
|
}
|
|
25
29
|
export function setMigrationProgress(p) {
|
|
26
30
|
progressById.set(p.id, p);
|
|
31
|
+
seededFromState.delete(p.id);
|
|
27
32
|
}
|
|
28
33
|
export function getMigrationProgress(id) {
|
|
29
34
|
return progressById.get(id);
|
|
@@ -41,9 +46,57 @@ export function seedIdleProgress(ids) {
|
|
|
41
46
|
}
|
|
42
47
|
}
|
|
43
48
|
}
|
|
49
|
+
/** The progress row for one durable state entry. Shared with status.ts's
|
|
50
|
+
* fallback so a boot-seeded row and a fallback row read the same. */
|
|
51
|
+
export function progressFromStateEntry(id, entry) {
|
|
52
|
+
const derivedState = entry.lastOutcome === "success" ? "completed" : entry.lastOutcome === "halted" ? "halted" : "failed";
|
|
53
|
+
const row = {
|
|
54
|
+
id,
|
|
55
|
+
rowsDone: entry.rowsProcessed ?? 0,
|
|
56
|
+
rowsRemaining: entry.rowsRemaining ?? 0,
|
|
57
|
+
state: derivedState,
|
|
58
|
+
};
|
|
59
|
+
if (entry.reason)
|
|
60
|
+
row.reason = entry.reason;
|
|
61
|
+
if (entry.rowsUnconfirmed) {
|
|
62
|
+
row.rowsUnconfirmed = entry.rowsUnconfirmed;
|
|
63
|
+
if (entry.unconfirmedIds?.length)
|
|
64
|
+
row.unconfirmedIds = entry.unconfirmedIds;
|
|
65
|
+
}
|
|
66
|
+
return row;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Seeds the in-process progress store from the state file at boot
|
|
70
|
+
* (flair#2840). When the file parses as a JSON object, a migration with an
|
|
71
|
+
* entry is seeded from it, including unconfirmed ids, their count and the
|
|
72
|
+
* status its last outcome maps to. A migration without one, or every migration
|
|
73
|
+
* when the file is missing, unreadable, invalid or not an object, is seeded
|
|
74
|
+
* `idle`. An existing entry is never overwritten. A seeded entry counts as not started
|
|
75
|
+
* this boot until `setMigrationProgress` writes its id (see
|
|
76
|
+
* {@link markUnstartedMigrationsFailed}).
|
|
77
|
+
*/
|
|
78
|
+
export function seedProgressFromState(statePath, ids) {
|
|
79
|
+
const state = readMigrationState(statePath);
|
|
80
|
+
for (const id of ids) {
|
|
81
|
+
if (progressById.has(id))
|
|
82
|
+
continue;
|
|
83
|
+
const entry = state[id];
|
|
84
|
+
if (entry) {
|
|
85
|
+
progressById.set(id, progressFromStateEntry(id, entry));
|
|
86
|
+
seededFromState.add(id);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
progressById.set(id, { id, rowsDone: 0, rowsRemaining: 0, state: "idle" });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
44
93
|
/**
|
|
45
|
-
* Marks every migration that
|
|
46
|
-
*
|
|
94
|
+
* Marks every migration that did not start this boot as `failed` with
|
|
95
|
+
* `reason` (flair#812): an id with no entry, an `idle` entry, or an entry
|
|
96
|
+
* `seedProgressFromState` built from the state file that `setMigrationProgress`
|
|
97
|
+
* has not written since (flair#2840). A seeded entry keeps its recorded
|
|
98
|
+
* `rowsUnconfirmed` and `unconfirmedIds`; the rest of its row is replaced as
|
|
99
|
+
* for an idle entry.
|
|
47
100
|
*
|
|
48
101
|
* The boot path calls this when a cycle couldn't run, so that a condition
|
|
49
102
|
* which is really instance-wide (no writable data dir, tables never ready,
|
|
@@ -51,31 +104,39 @@ export function seedIdleProgress(ids) {
|
|
|
51
104
|
* `flair doctor` and `flair quality`'s `instance.migrationsClean` actually
|
|
52
105
|
* read.
|
|
53
106
|
*
|
|
54
|
-
*
|
|
55
|
-
* can fail AFTER some migrations already reached a terminal state —
|
|
56
|
-
* pre-hash path, for instance, halts each candidate with its own precise
|
|
107
|
+
* Skipping an entry the runner wrote this boot is load-bearing, not tidiness:
|
|
108
|
+
* a cycle can fail AFTER some migrations already reached a terminal state —
|
|
109
|
+
* the pre-hash path, for instance, halts each candidate with its own precise
|
|
57
110
|
* reason and THEN reports cycle failure. Overwriting those would replace a
|
|
58
111
|
* specific `halted` (which the runner retries on the next boot, per its
|
|
59
112
|
* documented contract) with a blanket `failed`, and would flip migrations
|
|
60
|
-
* that genuinely completed this cycle to failed.
|
|
61
|
-
* is one nothing has said anything about yet.
|
|
113
|
+
* that genuinely completed this cycle to failed.
|
|
62
114
|
*
|
|
63
115
|
* Returns the ids actually marked, so a caller can tell "nothing had
|
|
64
116
|
* started" from "the cycle died partway".
|
|
65
117
|
*/
|
|
66
|
-
export function
|
|
118
|
+
export function markUnstartedMigrationsFailed(ids, reason) {
|
|
67
119
|
const marked = [];
|
|
68
120
|
for (const id of ids) {
|
|
69
121
|
const current = progressById.get(id);
|
|
70
|
-
|
|
122
|
+
const seeded = seededFromState.has(id);
|
|
123
|
+
if (current && current.state !== "idle" && !seeded)
|
|
71
124
|
continue;
|
|
72
|
-
|
|
125
|
+
const row = { id, rowsDone: 0, rowsRemaining: 0, state: "failed", reason };
|
|
126
|
+
if (seeded && current?.rowsUnconfirmed) {
|
|
127
|
+
row.rowsUnconfirmed = current.rowsUnconfirmed;
|
|
128
|
+
if (current.unconfirmedIds?.length)
|
|
129
|
+
row.unconfirmedIds = current.unconfirmedIds;
|
|
130
|
+
}
|
|
131
|
+
progressById.set(id, row);
|
|
132
|
+
seededFromState.delete(id);
|
|
73
133
|
marked.push(id);
|
|
74
134
|
}
|
|
75
135
|
return marked;
|
|
76
136
|
}
|
|
77
137
|
export function _resetProgressForTests() {
|
|
78
138
|
progressById.clear();
|
|
139
|
+
seededFromState.clear();
|
|
79
140
|
cycleStatus = { phase: "idle" };
|
|
80
141
|
stateFileStatus = { path: null, lastWriteError: null };
|
|
81
142
|
}
|
|
@@ -26,7 +26,9 @@ import { readMigrationState, writeMigrationStateEntry, isShortCircuited, default
|
|
|
26
26
|
import { writeLedgerEvent } from "./ledger.js";
|
|
27
27
|
import { setCyclePhase, setMigrationProgress, seedIdleProgress, noteStateWriteAttempt, noteStateWriteSuccess, noteStateWriteFailure } from "./progress.js";
|
|
28
28
|
import { shouldRegisterSyntheticMigration } from "./synthetic-test-migration.js";
|
|
29
|
+
import { UNCONFIRMED_IDS_LIMIT, BatchOutcomeError } from "./types.js";
|
|
29
30
|
import { join } from "node:path";
|
|
31
|
+
import { txnPausePoint } from "../txn-pause-point.js";
|
|
30
32
|
/**
|
|
31
33
|
* Test-only batch-throttle override, mirroring space.ts's
|
|
32
34
|
* FLAIR_MIGRATION_TEST_FREE_BYTES pattern (real Harper is a spawned child
|
|
@@ -96,10 +98,10 @@ function resolveDeps(deps) {
|
|
|
96
98
|
* `/HealthDetail` (progress.ts's state-file status, cleared on the next
|
|
97
99
|
* successful write).
|
|
98
100
|
*/
|
|
99
|
-
function recordMigrationStateEntry(path, id, entry) {
|
|
101
|
+
async function recordMigrationStateEntry(path, id, entry, lock) {
|
|
100
102
|
noteStateWriteAttempt(path);
|
|
101
103
|
try {
|
|
102
|
-
writeMigrationStateEntry(path, id, entry);
|
|
104
|
+
await writeMigrationStateEntry(path, id, entry, lock);
|
|
103
105
|
noteStateWriteSuccess();
|
|
104
106
|
}
|
|
105
107
|
catch (err) {
|
|
@@ -154,7 +156,9 @@ export async function runMigrationCycle(rawDeps) {
|
|
|
154
156
|
return { ran: false, reason: `lock error: ${err?.message ?? String(err)}` };
|
|
155
157
|
}
|
|
156
158
|
if (!lock.acquired) {
|
|
157
|
-
return
|
|
159
|
+
return lock.kind === "held"
|
|
160
|
+
? { ran: false, reason: `single-flight: ${lock.reason}` }
|
|
161
|
+
: { ran: false, reason: `lock error: ${lock.reason}` };
|
|
158
162
|
}
|
|
159
163
|
try {
|
|
160
164
|
return await runCycleLocked(deps, lock);
|
|
@@ -171,9 +175,29 @@ export async function runMigrationCycle(rawDeps) {
|
|
|
171
175
|
}
|
|
172
176
|
}
|
|
173
177
|
async function runCycleLocked(deps, lock) {
|
|
178
|
+
// Test-only hold (flair#2876): this cycle has taken the lock and has not yet
|
|
179
|
+
// read the state file, so a test can observe the progress row boot seeding
|
|
180
|
+
// produced. Inert unless the fault-injection opt-in and an arm file in the
|
|
181
|
+
// test pause dir are present.
|
|
182
|
+
const pause = txnPausePoint("migration-cycle-start");
|
|
183
|
+
if (pause)
|
|
184
|
+
await pause;
|
|
174
185
|
const state = readMigrationState(deps.statePath);
|
|
175
186
|
const candidates = [];
|
|
187
|
+
/** The re-verified unconfirmed record per migration, carried into its run. */
|
|
188
|
+
const records = new Map();
|
|
176
189
|
for (const migration of deps.registry.list()) {
|
|
190
|
+
const entry = state[migration.id];
|
|
191
|
+
// flair#2840: re-read the ids a previous run recorded as unconfirmed on
|
|
192
|
+
// each cycle that acquires the lock (this function), pending or not. A
|
|
193
|
+
// listed id is cleared here when the re-read answers `at-target` (the row
|
|
194
|
+
// needs no further write from this migration) or `no-row`; a later batch
|
|
195
|
+
// that verifies the row's write clears it too.
|
|
196
|
+
const record = await reverifyUnconfirmed(migration, entry);
|
|
197
|
+
records.set(migration.id, record);
|
|
198
|
+
const survived = record && record.count > 0 ? record : null;
|
|
199
|
+
const recordChanged = !!entry && !!record && ((survived?.count ?? 0) !== (entry.rowsUnconfirmed ?? entry.unconfirmedIds?.length ?? 0) ||
|
|
200
|
+
(survived?.ids.length ?? 0) !== (entry.unconfirmedIds?.length ?? 0));
|
|
177
201
|
if (isShortCircuited(state, migration.id, deps.runningVersion) && !migration.alwaysDetect) {
|
|
178
202
|
// flair#812: a short-circuit is an ASSERTION READ OFF A FILE, not an
|
|
179
203
|
// observation of the corpus — and that file is hand-editable (the
|
|
@@ -194,10 +218,14 @@ async function runCycleLocked(deps, lock) {
|
|
|
194
218
|
rowsRemaining: 0,
|
|
195
219
|
state: "completed",
|
|
196
220
|
reason: `recorded complete at ${deps.runningVersion} in ${deps.statePath} — not re-verified this boot`,
|
|
221
|
+
...unconfirmedProgress(survived),
|
|
197
222
|
});
|
|
223
|
+
if (entry && record && recordChanged) {
|
|
224
|
+
await recordMigrationStateEntry(deps.statePath, migration.id, withUnconfirmedRecord(entry, record), lock);
|
|
225
|
+
}
|
|
198
226
|
continue;
|
|
199
227
|
}
|
|
200
|
-
setMigrationProgress({ id: migration.id, rowsDone: 0, rowsRemaining: 0, state: "checking" });
|
|
228
|
+
setMigrationProgress({ id: migration.id, rowsDone: 0, rowsRemaining: 0, state: "checking", ...unconfirmedProgress(survived) });
|
|
201
229
|
let pending;
|
|
202
230
|
try {
|
|
203
231
|
pending = await migration.detect();
|
|
@@ -215,11 +243,40 @@ async function runCycleLocked(deps, lock) {
|
|
|
215
243
|
rowsRemaining: 0,
|
|
216
244
|
state: "failed",
|
|
217
245
|
reason,
|
|
246
|
+
...unconfirmedProgress(survived),
|
|
218
247
|
});
|
|
219
248
|
continue;
|
|
220
249
|
}
|
|
221
250
|
if (!pending) {
|
|
251
|
+
if (survived) {
|
|
252
|
+
// No pending work, but the record is not empty: keep it — with its
|
|
253
|
+
// count, its ids and the last outcome's status — instead of replacing
|
|
254
|
+
// it with a clean `completed` (flair#2840).
|
|
255
|
+
setMigrationProgress({
|
|
256
|
+
id: migration.id,
|
|
257
|
+
rowsDone: entry?.rowsProcessed ?? 0,
|
|
258
|
+
rowsRemaining: 0,
|
|
259
|
+
state: stateFromOutcome(entry),
|
|
260
|
+
...(entry?.reason ? { reason: entry.reason } : {}),
|
|
261
|
+
...unconfirmedProgress(survived),
|
|
262
|
+
});
|
|
263
|
+
if (entry && recordChanged) {
|
|
264
|
+
await recordMigrationStateEntry(deps.statePath, migration.id, withUnconfirmedRecord(entry, survived), lock);
|
|
265
|
+
}
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
222
268
|
setMigrationProgress({ id: migration.id, rowsDone: 0, rowsRemaining: 0, state: "completed" });
|
|
269
|
+
if (entry && record) {
|
|
270
|
+
// The record's count reached zero and no work is pending: the record
|
|
271
|
+
// can now say this migration completed.
|
|
272
|
+
await recordMigrationStateEntry(deps.statePath, migration.id, {
|
|
273
|
+
completedAtVersion: deps.runningVersion,
|
|
274
|
+
completedAt: deps.now().toISOString(),
|
|
275
|
+
lastOutcome: "success",
|
|
276
|
+
rowsProcessed: entry.rowsProcessed ?? 0,
|
|
277
|
+
rowsRemaining: 0,
|
|
278
|
+
}, lock);
|
|
279
|
+
}
|
|
223
280
|
continue;
|
|
224
281
|
}
|
|
225
282
|
candidates.push(migration);
|
|
@@ -247,7 +304,10 @@ async function runCycleLocked(deps, lock) {
|
|
|
247
304
|
catch (err) {
|
|
248
305
|
const reason = `pre-flight integrity check failed: ${err?.message ?? String(err)}`;
|
|
249
306
|
for (const migration of candidates) {
|
|
250
|
-
setMigrationProgress({
|
|
307
|
+
setMigrationProgress({
|
|
308
|
+
id: migration.id, rowsDone: 0, rowsRemaining: 0, state: "halted", reason,
|
|
309
|
+
...unconfirmedProgress(records.get(migration.id) ?? null),
|
|
310
|
+
});
|
|
251
311
|
}
|
|
252
312
|
setCyclePhase("done", reason);
|
|
253
313
|
return { ran: false, reason };
|
|
@@ -289,11 +349,12 @@ async function runCycleLocked(deps, lock) {
|
|
|
289
349
|
rowsRemaining: 0,
|
|
290
350
|
state: "halted",
|
|
291
351
|
reason: `pre-flight integrity check failed: ${err?.message ?? String(err)}`,
|
|
352
|
+
...unconfirmedProgress(records.get(migration.id) ?? null),
|
|
292
353
|
});
|
|
293
354
|
continue;
|
|
294
355
|
}
|
|
295
356
|
}
|
|
296
|
-
const wroteRows = await runOneMigration(migration, envelope, deps, lock);
|
|
357
|
+
const wroteRows = await runOneMigration(migration, envelope, deps, lock, records.get(migration.id) ?? null);
|
|
297
358
|
if (wroteRows)
|
|
298
359
|
envelopeStale = true;
|
|
299
360
|
}
|
|
@@ -303,7 +364,10 @@ async function runCycleLocked(deps, lock) {
|
|
|
303
364
|
async function haltMigration(migration, reason, ctx) {
|
|
304
365
|
const { deps, startedAt, rowsDone, rowsRemaining, hashEnvelopeMatch, state } = ctx;
|
|
305
366
|
const endedAt = deps.now().toISOString();
|
|
306
|
-
setMigrationProgress({
|
|
367
|
+
setMigrationProgress({
|
|
368
|
+
id: migration.id, rowsDone, rowsRemaining, state: "halted", reason,
|
|
369
|
+
...unconfirmedProgress(ctx.unconfirmed),
|
|
370
|
+
});
|
|
307
371
|
const fromVersion = state[migration.id]?.completedAtVersion ?? "unknown";
|
|
308
372
|
const evt = {
|
|
309
373
|
migrationId: migration.id,
|
|
@@ -331,12 +395,91 @@ async function haltMigration(migration, reason, ctx) {
|
|
|
331
395
|
// on lastOutcome === "success", so a halted migration is retried on every
|
|
332
396
|
// subsequent boot until it clears, never permanently stuck. A failed write
|
|
333
397
|
// is logged + surfaced (flair#1800), still best-effort.
|
|
334
|
-
recordMigrationStateEntry(deps.statePath, migration.id, {
|
|
398
|
+
await recordMigrationStateEntry(deps.statePath, migration.id, {
|
|
335
399
|
lastOutcome: "halted",
|
|
336
400
|
reason,
|
|
337
401
|
rowsProcessed: rowsDone,
|
|
338
402
|
rowsRemaining,
|
|
339
|
-
|
|
403
|
+
...unconfirmedState(ctx.unconfirmed),
|
|
404
|
+
}, ctx.lock);
|
|
405
|
+
}
|
|
406
|
+
/** The progress fields for a run's unconfirmed record (flair#2840). Empty when
|
|
407
|
+
* the record counts nothing. */
|
|
408
|
+
function unconfirmedProgress(record) {
|
|
409
|
+
if (!record || record.count <= 0)
|
|
410
|
+
return {};
|
|
411
|
+
return {
|
|
412
|
+
rowsUnconfirmed: record.count,
|
|
413
|
+
...(record.ids.length > 0 ? { unconfirmedIds: record.ids.slice(0, UNCONFIRMED_IDS_LIMIT) } : {}),
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
/** The state-file fields, same shape as {@link unconfirmedProgress}. */
|
|
417
|
+
function unconfirmedState(record) {
|
|
418
|
+
if (!record || record.count <= 0)
|
|
419
|
+
return {};
|
|
420
|
+
return {
|
|
421
|
+
rowsUnconfirmed: record.count,
|
|
422
|
+
...(record.ids.length > 0 ? { unconfirmedIds: record.ids.slice(0, UNCONFIRMED_IDS_LIMIT) } : {}),
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
/** The record a state entry carries, or null when it carries none. */
|
|
426
|
+
function recordedUnconfirmed(entry) {
|
|
427
|
+
const ids = entry?.unconfirmedIds ?? [];
|
|
428
|
+
if (ids.length === 0 && !entry?.rowsUnconfirmed)
|
|
429
|
+
return null;
|
|
430
|
+
return { ids: [...ids], count: entry?.rowsUnconfirmed ?? ids.length };
|
|
431
|
+
}
|
|
432
|
+
/** `halted`/`completed`/`failed` as status.ts derives it from an outcome. */
|
|
433
|
+
function stateFromOutcome(entry) {
|
|
434
|
+
if (entry?.lastOutcome === "success")
|
|
435
|
+
return "completed";
|
|
436
|
+
if (entry?.lastOutcome === "failed")
|
|
437
|
+
return "failed";
|
|
438
|
+
return "halted";
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Re-reads the ids a state entry records as unconfirmed (flair#2840) and
|
|
442
|
+
* returns the record that survives: an id is cleared when `checkUnconfirmed`
|
|
443
|
+
* answers `at-target` (the row needs no further write from this migration) or
|
|
444
|
+
* `no-row`.
|
|
445
|
+
*
|
|
446
|
+
* Any other re-check answer keeps the id (one that is not `pending` is logged),
|
|
447
|
+
* and a read that throws keeps it in this re-check. A record whose count is
|
|
448
|
+
* larger than its id list keeps the unlisted remainder as a count with no ids —
|
|
449
|
+
* those rows were never named, so no read can clear them.
|
|
450
|
+
*/
|
|
451
|
+
async function reverifyUnconfirmed(migration, entry) {
|
|
452
|
+
const recorded = recordedUnconfirmed(entry);
|
|
453
|
+
if (!recorded)
|
|
454
|
+
return null;
|
|
455
|
+
if (typeof migration.checkUnconfirmed !== "function")
|
|
456
|
+
return recorded;
|
|
457
|
+
const kept = [];
|
|
458
|
+
for (const id of recorded.ids) {
|
|
459
|
+
try {
|
|
460
|
+
const verdict = await migration.checkUnconfirmed(id);
|
|
461
|
+
if (verdict === "at-target" || verdict === "no-row")
|
|
462
|
+
continue;
|
|
463
|
+
if (verdict !== "pending") {
|
|
464
|
+
const answer = typeof verdict === "string" ? `string (length ${verdict.length})` : typeof verdict;
|
|
465
|
+
console.warn("[flair-migrations] %s: checkUnconfirmed(%s) answered %s; re-check kept id", migration.id, id, answer);
|
|
466
|
+
}
|
|
467
|
+
kept.push(id);
|
|
468
|
+
}
|
|
469
|
+
catch {
|
|
470
|
+
kept.push(id);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
const unlisted = Math.max(0, recorded.count - recorded.ids.length);
|
|
474
|
+
return { ids: kept, count: kept.length + unlisted };
|
|
475
|
+
}
|
|
476
|
+
/** The entry with its unconfirmed fields replaced by `record` (or dropped when
|
|
477
|
+
* the record counts nothing). */
|
|
478
|
+
function withUnconfirmedRecord(entry, record) {
|
|
479
|
+
const next = { ...entry };
|
|
480
|
+
delete next.rowsUnconfirmed;
|
|
481
|
+
delete next.unconfirmedIds;
|
|
482
|
+
return { ...next, ...unconfirmedState(record) };
|
|
340
483
|
}
|
|
341
484
|
/**
|
|
342
485
|
* Returns whether this migration MAY have written any rows — the cycle
|
|
@@ -347,11 +490,11 @@ async function haltMigration(migration, reason, ctx) {
|
|
|
347
490
|
* gate, while a false `false` would let that gate compare against a stale
|
|
348
491
|
* baseline and spuriously halt.
|
|
349
492
|
*/
|
|
350
|
-
async function runOneMigration(migration, envelope, deps, lock) {
|
|
493
|
+
async function runOneMigration(migration, envelope, deps, lock, carried) {
|
|
351
494
|
const startedAt = deps.now().toISOString();
|
|
352
495
|
const state = readMigrationState(deps.statePath);
|
|
353
496
|
const posture = postureFor(migration.riskClass);
|
|
354
|
-
setMigrationProgress({ id: migration.id, rowsDone: 0, rowsRemaining: 0, state: "preflight" });
|
|
497
|
+
setMigrationProgress({ id: migration.id, rowsDone: 0, rowsRemaining: 0, state: "preflight", ...unconfirmedProgress(carried) });
|
|
355
498
|
// ── Ladder step 1: space check (with HNSW/working-set overhead) ──
|
|
356
499
|
let initialRemaining;
|
|
357
500
|
try {
|
|
@@ -360,11 +503,13 @@ async function runOneMigration(migration, envelope, deps, lock) {
|
|
|
360
503
|
catch (err) {
|
|
361
504
|
await haltMigration(migration, `countPending() threw: ${err?.message ?? String(err)}`, {
|
|
362
505
|
deps,
|
|
506
|
+
lock,
|
|
363
507
|
startedAt,
|
|
364
508
|
rowsDone: 0,
|
|
365
509
|
rowsRemaining: 0,
|
|
366
510
|
hashEnvelopeMatch: null,
|
|
367
511
|
state,
|
|
512
|
+
unconfirmed: carried ?? undefined,
|
|
368
513
|
});
|
|
369
514
|
return false; // halted before any write
|
|
370
515
|
}
|
|
@@ -380,17 +525,19 @@ async function runOneMigration(migration, envelope, deps, lock) {
|
|
|
380
525
|
// ── Ladder step 5: no safe path → halt-don't-brick ──
|
|
381
526
|
await haltMigration(migration, `blocked on disk: ${space.reason}`, {
|
|
382
527
|
deps,
|
|
528
|
+
lock,
|
|
383
529
|
startedAt,
|
|
384
530
|
rowsDone: 0,
|
|
385
531
|
rowsRemaining: initialRemaining,
|
|
386
532
|
hashEnvelopeMatch: null,
|
|
387
533
|
state,
|
|
534
|
+
unconfirmed: carried ?? undefined,
|
|
388
535
|
});
|
|
389
536
|
return false; // halted before any write
|
|
390
537
|
}
|
|
391
538
|
// ── Ladder step 3 (+ step 4 fallback): risk-scoped snapshot, or a
|
|
392
539
|
// content-only export if the snapshot mechanism itself fails. ──
|
|
393
|
-
setMigrationProgress({ id: migration.id, rowsDone: 0, rowsRemaining: initialRemaining, state: "snapshotting" });
|
|
540
|
+
setMigrationProgress({ id: migration.id, rowsDone: 0, rowsRemaining: initialRemaining, state: "snapshotting", ...unconfirmedProgress(carried) });
|
|
394
541
|
try {
|
|
395
542
|
createMigrationSnapshot({
|
|
396
543
|
migrationId: migration.id,
|
|
@@ -410,19 +557,34 @@ async function runOneMigration(migration, envelope, deps, lock) {
|
|
|
410
557
|
createContentOnlyExport({ migrationId: migration.id, table, rows, fromVersion: state[migration.id]?.completedAtVersion ?? "unknown" }, { exportRoot: deps.exportRoot, now: deps.now });
|
|
411
558
|
}
|
|
412
559
|
catch (expErr) {
|
|
413
|
-
await haltMigration(migration, `snapshot failed (${snapErr?.message ?? String(snapErr)}) and the content-only export fallback also failed (${expErr?.message ?? String(expErr)})`, { deps, startedAt, rowsDone: 0, rowsRemaining: initialRemaining, hashEnvelopeMatch: null, state });
|
|
560
|
+
await haltMigration(migration, `snapshot failed (${snapErr?.message ?? String(snapErr)}) and the content-only export fallback also failed (${expErr?.message ?? String(expErr)})`, { deps, lock, startedAt, rowsDone: 0, rowsRemaining: initialRemaining, hashEnvelopeMatch: null, state, unconfirmed: carried ?? undefined });
|
|
414
561
|
return false; // halted before any write
|
|
415
562
|
}
|
|
416
563
|
}
|
|
417
564
|
// ── Execution: throttled batches, resumable via per-row markers ──
|
|
418
|
-
setMigrationProgress({ id: migration.id, rowsDone: 0, rowsRemaining: initialRemaining, state: "running" });
|
|
565
|
+
setMigrationProgress({ id: migration.id, rowsDone: 0, rowsRemaining: initialRemaining, state: "running", ...unconfirmedProgress(carried) });
|
|
419
566
|
let rowsDone = 0;
|
|
567
|
+
// flair#2840: rows recorded as unconfirmed — the ids this run's batches
|
|
568
|
+
// report (a write the migration made but could not verify, or a loopback
|
|
569
|
+
// re-embed that answered 503 `write_outcome_unknown`, which can reflect an
|
|
570
|
+
// earlier writer's unresolved write), plus the listed ids an earlier cycle
|
|
571
|
+
// recorded and no later read has cleared. A batch that verifies an id
|
|
572
|
+
// removes it here.
|
|
573
|
+
const unconfirmed = new Set(carried?.ids ?? []);
|
|
574
|
+
// Ids the durable record counted but never listed cannot be re-read; carry
|
|
575
|
+
// their number (flair#2840).
|
|
576
|
+
const unconfirmedUnlisted = carried ? Math.max(0, carried.count - carried.ids.length) : 0;
|
|
577
|
+
const unconfirmedRecord = () => ({ ids: [...unconfirmed], count: unconfirmed.size + unconfirmedUnlisted });
|
|
420
578
|
const oldRowHashes = new Map();
|
|
421
579
|
const newRowIds = [];
|
|
422
580
|
try {
|
|
423
581
|
for (;;) {
|
|
424
582
|
const result = await migration.run(posture.batchSize);
|
|
425
583
|
rowsDone += result.processed;
|
|
584
|
+
for (const id of result.touchedIds)
|
|
585
|
+
unconfirmed.delete(id);
|
|
586
|
+
for (const id of result.unconfirmedIds ?? [])
|
|
587
|
+
unconfirmed.add(id);
|
|
426
588
|
if (result.oldRowSourceHashes) {
|
|
427
589
|
for (const [k, v] of Object.entries(result.oldRowSourceHashes))
|
|
428
590
|
oldRowHashes.set(k, v);
|
|
@@ -434,6 +596,7 @@ async function runOneMigration(migration, envelope, deps, lock) {
|
|
|
434
596
|
rowsDone,
|
|
435
597
|
rowsRemaining: Math.max(0, initialRemaining - rowsDone),
|
|
436
598
|
state: "running",
|
|
599
|
+
...unconfirmedProgress(unconfirmedRecord()),
|
|
437
600
|
});
|
|
438
601
|
lock.touch();
|
|
439
602
|
if (result.processed === 0)
|
|
@@ -442,18 +605,36 @@ async function runOneMigration(migration, envelope, deps, lock) {
|
|
|
442
605
|
}
|
|
443
606
|
}
|
|
444
607
|
catch (err) {
|
|
608
|
+
// flair#2840: a batch that throws from the synthetic write-back, the
|
|
609
|
+
// visibility backfill or embedding-stamp's `.content`-suffix retry read
|
|
610
|
+
// carries those outcomes on the error, because there is no RunBatchResult
|
|
611
|
+
// to read. Merge them before the halt writes the record; a throw from a
|
|
612
|
+
// path that does not wrap its batch carries nothing.
|
|
613
|
+
if (err instanceof BatchOutcomeError) {
|
|
614
|
+
// The batch verified and stamped these rows before it threw; count them.
|
|
615
|
+
rowsDone += err.touchedIds.length;
|
|
616
|
+
for (const id of err.touchedIds)
|
|
617
|
+
unconfirmed.delete(id);
|
|
618
|
+
for (const id of err.unconfirmedIds)
|
|
619
|
+
unconfirmed.add(id);
|
|
620
|
+
}
|
|
445
621
|
await haltMigration(migration, `run() threw mid-batch: ${err?.message ?? String(err)}`, {
|
|
446
622
|
deps,
|
|
623
|
+
lock,
|
|
447
624
|
startedAt,
|
|
448
625
|
rowsDone,
|
|
449
626
|
rowsRemaining: Math.max(0, initialRemaining - rowsDone),
|
|
450
627
|
hashEnvelopeMatch: null,
|
|
451
628
|
state,
|
|
629
|
+
unconfirmed: unconfirmedRecord(),
|
|
452
630
|
});
|
|
453
631
|
return true; // conservative: a partial batch may have landed before the throw
|
|
454
632
|
}
|
|
455
633
|
// ── Completion gate — strictness per risk class ──
|
|
456
|
-
setMigrationProgress({
|
|
634
|
+
setMigrationProgress({
|
|
635
|
+
id: migration.id, rowsDone, rowsRemaining: 0, state: "completing",
|
|
636
|
+
...unconfirmedProgress(unconfirmedRecord()),
|
|
637
|
+
});
|
|
457
638
|
let finalRemaining;
|
|
458
639
|
try {
|
|
459
640
|
finalRemaining = await migration.countPending();
|
|
@@ -461,11 +642,13 @@ async function runOneMigration(migration, envelope, deps, lock) {
|
|
|
461
642
|
catch (err) {
|
|
462
643
|
await haltMigration(migration, `post-batch countPending() threw: ${err?.message ?? String(err)}`, {
|
|
463
644
|
deps,
|
|
645
|
+
lock,
|
|
464
646
|
startedAt,
|
|
465
647
|
rowsDone,
|
|
466
648
|
rowsRemaining: 0,
|
|
467
649
|
hashEnvelopeMatch: null,
|
|
468
650
|
state,
|
|
651
|
+
unconfirmed: unconfirmedRecord(),
|
|
469
652
|
});
|
|
470
653
|
return rowsDone > 0;
|
|
471
654
|
}
|
|
@@ -515,11 +698,13 @@ async function runOneMigration(migration, envelope, deps, lock) {
|
|
|
515
698
|
gateOk = false;
|
|
516
699
|
await haltMigration(migration, `post-hash computation failed: ${err?.message ?? String(err)}`, {
|
|
517
700
|
deps,
|
|
701
|
+
lock,
|
|
518
702
|
startedAt,
|
|
519
703
|
rowsDone,
|
|
520
704
|
rowsRemaining: finalRemaining,
|
|
521
705
|
hashEnvelopeMatch,
|
|
522
706
|
state,
|
|
707
|
+
unconfirmed: unconfirmedRecord(),
|
|
523
708
|
});
|
|
524
709
|
return rowsDone > 0;
|
|
525
710
|
}
|
|
@@ -557,11 +742,13 @@ async function runOneMigration(migration, envelope, deps, lock) {
|
|
|
557
742
|
reasonParts.push("hash envelope mismatch");
|
|
558
743
|
await haltMigration(migration, `completion gate failed: ${reasonParts.join(", ")}`, {
|
|
559
744
|
deps,
|
|
745
|
+
lock,
|
|
560
746
|
startedAt,
|
|
561
747
|
rowsDone,
|
|
562
748
|
rowsRemaining: finalRemaining,
|
|
563
749
|
hashEnvelopeMatch,
|
|
564
750
|
state,
|
|
751
|
+
unconfirmed: unconfirmedRecord(),
|
|
565
752
|
});
|
|
566
753
|
return rowsDone > 0;
|
|
567
754
|
}
|
|
@@ -590,14 +777,21 @@ async function runOneMigration(migration, envelope, deps, lock) {
|
|
|
590
777
|
// /HealthDetail. Worst case this migration's detect() runs (cheaply) again
|
|
591
778
|
// next boot — and /HealthDetail still says "completed" while state.json
|
|
592
779
|
// never got the entry. That failure must be visible, not swallowed.
|
|
593
|
-
recordMigrationStateEntry(deps.statePath, migration.id, {
|
|
780
|
+
await recordMigrationStateEntry(deps.statePath, migration.id, {
|
|
594
781
|
completedAtVersion: deps.runningVersion,
|
|
595
782
|
completedAt: endedAt,
|
|
596
783
|
lastOutcome: "success",
|
|
597
784
|
rowsProcessed: rowsDone,
|
|
598
785
|
rowsRemaining: 0,
|
|
599
|
-
|
|
786
|
+
...unconfirmedState(unconfirmedRecord()),
|
|
787
|
+
}, lock);
|
|
600
788
|
pruneMigrationSnapshots(deps.snapshotRoot);
|
|
601
|
-
|
|
789
|
+
// State stays "completed" when the gate passes, even with a non-empty
|
|
790
|
+
// unconfirmed record (flair#2840): the record, not the state, carries the
|
|
791
|
+
// unconfirmed rows.
|
|
792
|
+
setMigrationProgress({
|
|
793
|
+
id: migration.id, rowsDone, rowsRemaining: 0, state: "completed",
|
|
794
|
+
...unconfirmedProgress(unconfirmedRecord()),
|
|
795
|
+
});
|
|
602
796
|
return rowsDone > 0;
|
|
603
797
|
}
|
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
23
23
|
import { dirname, join } from "node:path";
|
|
24
24
|
import { ensureSecureDir, writeSecureFile } from "./dir-safety.js";
|
|
25
|
+
import { migrationLockHandleStatus, migrationLockIdentity, migrationLockStillHeld } from "./lock.js";
|
|
26
|
+
import { txnPausePoint } from "../txn-pause-point.js";
|
|
25
27
|
export function defaultStatePath(dataDir) {
|
|
26
28
|
return join(dataDir, ".migrations", "state.json");
|
|
27
29
|
}
|
|
@@ -49,11 +51,44 @@ export function readMigrationState(path) {
|
|
|
49
51
|
return {};
|
|
50
52
|
}
|
|
51
53
|
}
|
|
52
|
-
|
|
54
|
+
/** The named refusal a state write makes without a live handle for the state
|
|
55
|
+
* file's migration lock (flair#2866). */
|
|
56
|
+
export class MigrationStateWriteRefusedError extends Error {
|
|
57
|
+
constructor(message) {
|
|
58
|
+
super(message);
|
|
59
|
+
this.name = "MigrationStateWriteRefusedError";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export async function writeMigrationStateEntry(path, id, entry, lock) {
|
|
53
63
|
const dir = dirname(path);
|
|
64
|
+
const designatedLockPath = join(dir, "lock");
|
|
65
|
+
const status = migrationLockHandleStatus(lock);
|
|
66
|
+
const identity = migrationLockIdentity(lock);
|
|
67
|
+
if (status !== "live" || identity === null) {
|
|
68
|
+
throw new MigrationStateWriteRefusedError(status === "released"
|
|
69
|
+
? `refusing to write ${path}: the migration lock handle was already released`
|
|
70
|
+
: `refusing to write ${path}: no held migration lock handle was supplied`);
|
|
71
|
+
}
|
|
72
|
+
if (identity.lockPath !== designatedLockPath) {
|
|
73
|
+
throw new MigrationStateWriteRefusedError(`refusing to write ${id} in ${path}: the lock handle is for ${identity.lockPath}, not the state file's lock at ${designatedLockPath}`);
|
|
74
|
+
}
|
|
54
75
|
ensureSecureDir(dir);
|
|
55
76
|
const current = readMigrationState(path);
|
|
56
77
|
current[id] = entry;
|
|
78
|
+
// Test-only pause between the read and the rename (flair#2866).
|
|
79
|
+
const pause = txnPausePoint("migration-state-write");
|
|
80
|
+
if (pause)
|
|
81
|
+
await pause;
|
|
82
|
+
// This re-check reads the lock pathname and detects a replacement that
|
|
83
|
+
// landed before the read. It inspects the file it opened, so a pathname
|
|
84
|
+
// replacement during that descriptor read can occur before the check
|
|
85
|
+
// returns.
|
|
86
|
+
// writeSecureFile then creates its temporary file, flushes it and renames it
|
|
87
|
+
// over the state file; a replacement that lands during that write is not
|
|
88
|
+
// detected again (flair#2866).
|
|
89
|
+
if (migrationLockHandleStatus(lock) !== "live" || !migrationLockStillHeld(lock)) {
|
|
90
|
+
throw new MigrationStateWriteRefusedError(`refusing to write ${id} in ${path}: the migration lock at ${identity.lockPath} is no longer held by pid ${identity.holder.pid} (started ${identity.holder.startedAt})`);
|
|
91
|
+
}
|
|
57
92
|
writeSecureFile(path, JSON.stringify(current, null, 2) + "\n", dir);
|
|
58
93
|
}
|
|
59
94
|
/**
|
|
@@ -10,25 +10,14 @@
|
|
|
10
10
|
* completed/halted outcome is still genuinely useful in that window instead
|
|
11
11
|
* of reporting nothing.
|
|
12
12
|
*/
|
|
13
|
-
import { listMigrationProgress, getCycleStatus, getStateFileStatus } from "./progress.js";
|
|
13
|
+
import { listMigrationProgress, getCycleStatus, getStateFileStatus, progressFromStateEntry } from "./progress.js";
|
|
14
14
|
import { readMigrationState, defaultStatePath } from "./state.js";
|
|
15
15
|
export function getMigrationStatusSnapshot(dataDir) {
|
|
16
16
|
const cycle = getCycleStatus();
|
|
17
17
|
let migrations = listMigrationProgress();
|
|
18
18
|
if (migrations.length === 0) {
|
|
19
19
|
const state = readMigrationState(defaultStatePath(dataDir));
|
|
20
|
-
migrations = Object.entries(state).map(([id, entry]) =>
|
|
21
|
-
const derivedState = entry.lastOutcome === "success" ? "completed" : entry.lastOutcome === "halted" ? "halted" : "failed";
|
|
22
|
-
const row = {
|
|
23
|
-
id,
|
|
24
|
-
rowsDone: entry.rowsProcessed ?? 0,
|
|
25
|
-
rowsRemaining: entry.rowsRemaining ?? 0,
|
|
26
|
-
state: derivedState,
|
|
27
|
-
};
|
|
28
|
-
if (entry.reason)
|
|
29
|
-
row.reason = entry.reason;
|
|
30
|
-
return row;
|
|
31
|
-
});
|
|
20
|
+
migrations = Object.entries(state).map(([id, entry]) => progressFromStateEntry(id, entry));
|
|
32
21
|
}
|
|
33
22
|
return {
|
|
34
23
|
cyclePhase: cycle.phase,
|