@smthrs/journal 1.0.0-rc.7 → 1.0.0-rc.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/cjs/Consensus.d.ts +30 -2
- package/dist/cjs/Consensus.d.ts.map +1 -1
- package/dist/cjs/Consensus.js +5 -1
- package/dist/cjs/Consensus.js.map +2 -2
- package/dist/cjs/Redaction.d.ts.map +1 -1
- package/dist/cjs/Redaction.js +26 -2
- package/dist/cjs/Redaction.js.map +2 -2
- package/dist/cjs/SqlConsensus.d.ts.map +1 -1
- package/dist/cjs/SqlConsensus.js +1 -1
- package/dist/cjs/SqlConsensus.js.map +2 -2
- package/dist/cjs/test/Notifying.js.map +2 -2
- package/dist/esm/Consensus.d.ts +30 -2
- package/dist/esm/Consensus.d.ts.map +1 -1
- package/dist/esm/Consensus.js +32 -3
- package/dist/esm/Consensus.js.map +1 -1
- package/dist/esm/Redaction.d.ts.map +1 -1
- package/dist/esm/Redaction.js +32 -5
- package/dist/esm/Redaction.js.map +1 -1
- package/dist/esm/SqlConsensus.d.ts.map +1 -1
- package/dist/esm/SqlConsensus.js +2 -2
- package/dist/esm/SqlConsensus.js.map +1 -1
- package/dist/esm/test/Notifying.js.map +1 -1
- package/docs/README.md +3 -1
- package/docs/api.md +24 -22
- package/docs/concepts/owner-fence.md +5 -1
- package/docs/concepts/redaction.md +9 -5
- package/docs/installation.md +5 -1
- package/docs/quickstart.md +6 -3
- package/package.json +4 -4
- package/src/Consensus.ts +35 -3
- package/src/Redaction.ts +32 -5
- package/src/SqlConsensus.ts +2 -1
- package/src/test/Notifying.ts +2 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/SqlConsensus.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * The default database-backed consensus strategy.\n *\n * This is the run store's former `flows_runs` ownership compare-and-swap\n * relocated: the owner tuple, the two-phase claim columns, claim recovery, and\n * the grant timestamp are the private schema of one strategy \u2014 the\n * `flows_consensus_leases` table \u2014 instead of a public contract on the run\n * row. The run row still mirrors the outcome, but the lease is what\n * arbitrates.\n *\n * Commit-time admission (R3) is exact: `guard` is a plain SELECT that joins\n * the caller's open write transaction \u2014 `DurableWriter` serializes write\n * transactions, so no reclaim can commit between the guard read and the\n * statements beside it \u2014 and every mutating operation runs through\n * `DurableWriter.write`, joining an enclosing transaction as a savepoint.\n * Prior art: Temporal's shard `rangeID` check\n * (`reference/temporal/service/history/shard/context_impl.go`,\n * `renewRangeLocked`).\n *\n * Governing design: `packages/smithers/flows/journal/docs/concepts/owner-fence.md`.\n *\n * @since 1.0.0\n */\n\nimport * as Dialect from \"@smthrs/database/Dialect\"\nimport { DurableWriter } from \"@smthrs/database/DurableWriter\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport * as SqlClient from \"effect/sql/SqlClient\"\nimport {\n type ActivateOutcome,\n type Claimed,\n type ClaimOutcome,\n Consensus,\n ConsensusError,\n fenceLost,\n type HeartbeatOutcome,\n heartbeatStaleAfter,\n type LivenessEvidence,\n matchesEvidence,\n type RecoverOutcome,\n type Rejected,\n type RejectionReason,\n type Service\n} from \"./Consensus.ts\"\nimport type { OwnerId } from \"./OwnerId.ts\"\n\nconst staleAfterMs = Duration.toMillis(heartbeatStaleAfter)\n\nconst rejected = (reason: RejectionReason): Rejected => ({ _tag: \"Rejected\", reason })\nconst claimed = (grantedAtMs: number): Claimed => ({ _tag: \"Claimed\", grantedAtMs })\nconst lost = { _tag: \"Lost\" } as const\nconst activated = { _tag: \"Activated\" } as const\nconst recovered = { _tag: \"Recovered\" } as const\n\ninterface LeaseRow {\n readonly owner_host_id: string | null\n readonly owner_pid: number | null\n readonly owner_nonce: string | null\n readonly claim_host_id: string | null\n readonly claim_pid: number | null\n readonly claim_nonce: string | null\n}\n\n/**\n * Constructs the database-backed strategy over the context's SQL client and\n * durable writer.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const make: Effect.Effect<Service, never, DurableWriter | SqlClient.SqlClient> = Effect.gen(function*() {\n const sql = yield* Effect.service(SqlClient.SqlClient)\n const writer = yield* DurableWriter\n\n const strategyError = (cause: unknown): ConsensusError =>\n new ConsensusError({\n code: \"persistence_failed\",\n message: \"consensus lease operation failed\",\n cause\n })\n\n // The original cause is preserved through the wrapper so an enclosing\n // `DurableWriter.write`'s retry classification still sees a transient\n // conflict raised by a savepoint-nested lease statement.\n const write = <A>(\n body: Effect.Effect<A, unknown>\n ): Effect.Effect<A, ConsensusError> =>\n writer.write(body).pipe(Effect.mapError((cause) => cause instanceof ConsensusError ? cause : strategyError(cause)))\n\n const leaseOf = (runId: string) =>\n sql<LeaseRow>`\n SELECT owner_host_id, owner_pid, owner_nonce, claim_host_id, claim_pid, claim_nonce\n FROM flows_consensus_leases\n WHERE run_id = ${runId}\n `\n\n const prune = (runId: string) =>\n sql`\n DELETE FROM flows_consensus_leases\n WHERE run_id = ${runId} AND owner_host_id IS NULL AND claim_host_id IS NULL\n `\n\n const claim: Service[\"claim\"] = Effect.fn(\"Consensus.claim\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n INSERT INTO flows_consensus_leases (run_id, claim_host_id, claim_pid, claim_nonce, claimed_at_ms)\n VALUES (${runId}, ${claimant.hostId}, ${claimant.pid}, ${claimant.nonce}, ${nowMs})\n ON CONFLICT (run_id) DO UPDATE SET\n claim_host_id = excluded.claim_host_id,\n claim_pid = excluded.claim_pid,\n claim_nonce = excluded.claim_nonce,\n claimed_at_ms = excluded.claimed_at_ms\n WHERE flows_consensus_leases.claim_host_id IS NULL\n AND flows_consensus_leases.owner_host_id IS NULL\n RETURNING run_id\n `\n if (rows.length > 0) return claimed(nowMs)\n // The upsert only misses on a conflicting row, so the lease exists and\n // holds a claim, an owner, or both.\n const lease = (yield* leaseOf(runId))[0]!\n return lease.claim_host_id !== null ? rejected(\"already_claimed\") : rejected(\"owner_held\")\n }))\n )\n\n const activate: Service[\"activate\"] = Effect.fn(\"Consensus.activate\")((\n runId: string,\n owner: OwnerId,\n grantedAtMs: number,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET\n owner_host_id = ${owner.hostId},\n owner_pid = ${owner.pid},\n owner_nonce = ${owner.nonce},\n granted_at_ms = ${grantedAtMs},\n heartbeat_at_ms = ${nowMs},\n claim_host_id = NULL,\n claim_pid = NULL,\n claim_nonce = NULL,\n claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${owner.hostId}\n AND claim_pid = ${owner.pid}\n AND claim_nonce = ${owner.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n RETURNING run_id\n `\n return (rows.length > 0 ? activated : lost) satisfies ActivateOutcome\n }))\n )\n\n const heartbeat: Service[\"heartbeat\"] = Effect.fn(\"Consensus.heartbeat\")((\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n // Monotonic: a pulse delayed past a newer one from the same owner never\n // moves the lease backwards and never makes a live run look stale.\n const rows = yield* sql<{ readonly heartbeat_at_ms: number }>`\n UPDATE flows_consensus_leases\n SET heartbeat_at_ms = ${Dialect.greatest(sql)}(heartbeat_at_ms, ${nowMs})\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n RETURNING heartbeat_at_ms\n `\n const row = rows[0]\n return (row === undefined\n ? lost\n : { _tag: \"Renewed\", heartbeatAtMs: Number(row.heartbeat_at_ms) }) satisfies HeartbeatOutcome\n }))\n )\n\n const reconfirm: Service[\"reconfirm\"] = Effect.fn(\"Consensus.reconfirm\")((\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n // A pending steal has already reserved the lease, even before activation.\n // Compare both owner and claim in the same update as the renewal.\n const rows = yield* sql<{ readonly heartbeat_at_ms: number }>`\n UPDATE flows_consensus_leases\n SET heartbeat_at_ms = ${Dialect.greatest(sql)}(heartbeat_at_ms, ${nowMs})\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n AND claim_host_id IS NULL\n RETURNING heartbeat_at_ms\n `\n const row = rows[0]\n return (row === undefined\n ? lost\n : { _tag: \"Renewed\", heartbeatAtMs: Number(row.heartbeat_at_ms) }) satisfies HeartbeatOutcome\n }))\n )\n\n const release: Service[\"release\"] = Effect.fn(\"Consensus.release\")((runId: string, owner: OwnerId) =>\n write(Effect.gen(function*() {\n yield* sql`\n UPDATE flows_consensus_leases\n SET claim_host_id = NULL, claim_pid = NULL, claim_nonce = NULL, claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${owner.hostId}\n AND claim_pid = ${owner.pid}\n AND claim_nonce = ${owner.nonce}\n `\n yield* sql`\n UPDATE flows_consensus_leases\n SET owner_host_id = NULL, owner_pid = NULL, owner_nonce = NULL,\n granted_at_ms = NULL, heartbeat_at_ms = NULL\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n `\n yield* prune(runId)\n }))\n )\n\n const steal: Service[\"steal\"] = Effect.fn(\"Consensus.steal\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.suspend((): Effect.Effect<ClaimOutcome, ConsensusError> => {\n if (!matchesEvidence(evidence.expectedOwner, claimant, nowMs, evidence)) {\n return Effect.succeed(rejected(\"evidence_invalid\"))\n }\n return write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET\n claim_host_id = ${claimant.hostId},\n claim_pid = ${claimant.pid},\n claim_nonce = ${claimant.nonce},\n claimed_at_ms = ${nowMs}\n WHERE run_id = ${runId}\n AND owner_host_id = ${evidence.expectedOwner.hostId}\n AND owner_pid = ${evidence.expectedOwner.pid}\n AND owner_nonce = ${evidence.expectedOwner.nonce}\n AND heartbeat_at_ms < ${nowMs - staleAfterMs}\n AND claim_host_id IS NULL\n RETURNING run_id\n `\n if (rows.length > 0) return claimed(nowMs)\n const lease = (yield* leaseOf(runId))[0]\n if (lease !== undefined && lease.claim_host_id !== null) return rejected(\"already_claimed\")\n if (\n lease === undefined ||\n lease.owner_host_id !== evidence.expectedOwner.hostId ||\n Number(lease.owner_pid) !== evidence.expectedOwner.pid ||\n lease.owner_nonce !== evidence.expectedOwner.nonce\n ) {\n // A run nobody owns, or one owned by somebody else, has no owner\n // this evidence could disprove.\n return rejected(\"evidence_invalid\")\n }\n return rejected(\"owner_live\")\n }))\n })\n )\n\n const recover: Service[\"recover\"] = Effect.fn(\"Consensus.recover\")((\n runId: string,\n staleClaimant: OwnerId,\n grantedAtMs: number,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.suspend((): Effect.Effect<RecoverOutcome, ConsensusError> => {\n if (!matchesEvidence(staleClaimant, observer, nowMs, evidence)) {\n return Effect.succeed(rejected(\"evidence_invalid\"))\n }\n return write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET claim_host_id = NULL, claim_pid = NULL, claim_nonce = NULL, claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${staleClaimant.hostId}\n AND claim_pid = ${staleClaimant.pid}\n AND claim_nonce = ${staleClaimant.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n AND claimed_at_ms < ${nowMs - staleAfterMs}\n RETURNING run_id\n `\n if (rows.length > 0) {\n yield* prune(runId)\n return recovered\n }\n const matches = yield* sql<{ readonly run_id: string }>`\n SELECT run_id FROM flows_consensus_leases\n WHERE run_id = ${runId}\n AND claim_host_id = ${staleClaimant.hostId}\n AND claim_pid = ${staleClaimant.pid}\n AND claim_nonce = ${staleClaimant.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n `\n // The exact claim survived the compare-and-swap only because it is\n // not yet stale; anything else means the claim moved on.\n return matches.length > 0 ? rejected(\"claim_fresh\") : rejected(\"claim_changed\")\n }))\n })\n )\n\n const guard: Service[\"guard\"] = Effect.fn(\"Consensus.guard\")((runId: string, owner: OwnerId) =>\n write(Effect.gen(function*() {\n const held = yield* sql<{ readonly ok: number }>`\n SELECT 1 AS ok FROM flows_consensus_leases\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n `\n if (held.length === 0) {\n return yield* Effect.fail(fenceLost(runId, owner))\n }\n }))\n )\n\n return Consensus.of({ claim, activate, heartbeat, reconfirm, release, steal, recover, guard })\n})\n\n/**\n * Provides the database-backed strategy.\n *\n * @since 1.0.0\n * @category layers\n */\nexport const layer: Layer.Layer<Consensus, never, DurableWriter | SqlClient.SqlClient> = Layer.effect(Consensus, make)\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwBA,cAAyB;AACzB,2BAA8B;AAC9B,eAA0B;AAC1B,aAAwB;AACxB,YAAuB;AACvB,gBAA2B;AAC3B,
|
|
4
|
+
"sourcesContent": ["/**\n * The default database-backed consensus strategy.\n *\n * This is the run store's former `flows_runs` ownership compare-and-swap\n * relocated: the owner tuple, the two-phase claim columns, claim recovery, and\n * the grant timestamp are the private schema of one strategy \u2014 the\n * `flows_consensus_leases` table \u2014 instead of a public contract on the run\n * row. The run row still mirrors the outcome, but the lease is what\n * arbitrates.\n *\n * Commit-time admission (R3) is exact: `guard` is a plain SELECT that joins\n * the caller's open write transaction \u2014 `DurableWriter` serializes write\n * transactions, so no reclaim can commit between the guard read and the\n * statements beside it \u2014 and every mutating operation runs through\n * `DurableWriter.write`, joining an enclosing transaction as a savepoint.\n * Prior art: Temporal's shard `rangeID` check\n * (`reference/temporal/service/history/shard/context_impl.go`,\n * `renewRangeLocked`).\n *\n * Governing design: `packages/smithers/flows/journal/docs/concepts/owner-fence.md`.\n *\n * @since 1.0.0\n */\n\nimport * as Dialect from \"@smthrs/database/Dialect\"\nimport { DurableWriter } from \"@smthrs/database/DurableWriter\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport * as SqlClient from \"effect/sql/SqlClient\"\nimport {\n type ActivateOutcome,\n type Claimed,\n type ClaimOutcome,\n Consensus,\n ConsensusError,\n fenceLost,\n type HeartbeatOutcome,\n heartbeatStaleAfter,\n type LivenessEvidence,\n matchesEvidence,\n provesDeath,\n type RecoverOutcome,\n type Rejected,\n type RejectionReason,\n type Service\n} from \"./Consensus.ts\"\nimport type { OwnerId } from \"./OwnerId.ts\"\n\nconst staleAfterMs = Duration.toMillis(heartbeatStaleAfter)\n\nconst rejected = (reason: RejectionReason): Rejected => ({ _tag: \"Rejected\", reason })\nconst claimed = (grantedAtMs: number): Claimed => ({ _tag: \"Claimed\", grantedAtMs })\nconst lost = { _tag: \"Lost\" } as const\nconst activated = { _tag: \"Activated\" } as const\nconst recovered = { _tag: \"Recovered\" } as const\n\ninterface LeaseRow {\n readonly owner_host_id: string | null\n readonly owner_pid: number | null\n readonly owner_nonce: string | null\n readonly claim_host_id: string | null\n readonly claim_pid: number | null\n readonly claim_nonce: string | null\n}\n\n/**\n * Constructs the database-backed strategy over the context's SQL client and\n * durable writer.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const make: Effect.Effect<Service, never, DurableWriter | SqlClient.SqlClient> = Effect.gen(function*() {\n const sql = yield* Effect.service(SqlClient.SqlClient)\n const writer = yield* DurableWriter\n\n const strategyError = (cause: unknown): ConsensusError =>\n new ConsensusError({\n code: \"persistence_failed\",\n message: \"consensus lease operation failed\",\n cause\n })\n\n // The original cause is preserved through the wrapper so an enclosing\n // `DurableWriter.write`'s retry classification still sees a transient\n // conflict raised by a savepoint-nested lease statement.\n const write = <A>(\n body: Effect.Effect<A, unknown>\n ): Effect.Effect<A, ConsensusError> =>\n writer.write(body).pipe(Effect.mapError((cause) => cause instanceof ConsensusError ? cause : strategyError(cause)))\n\n const leaseOf = (runId: string) =>\n sql<LeaseRow>`\n SELECT owner_host_id, owner_pid, owner_nonce, claim_host_id, claim_pid, claim_nonce\n FROM flows_consensus_leases\n WHERE run_id = ${runId}\n `\n\n const prune = (runId: string) =>\n sql`\n DELETE FROM flows_consensus_leases\n WHERE run_id = ${runId} AND owner_host_id IS NULL AND claim_host_id IS NULL\n `\n\n const claim: Service[\"claim\"] = Effect.fn(\"Consensus.claim\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n INSERT INTO flows_consensus_leases (run_id, claim_host_id, claim_pid, claim_nonce, claimed_at_ms)\n VALUES (${runId}, ${claimant.hostId}, ${claimant.pid}, ${claimant.nonce}, ${nowMs})\n ON CONFLICT (run_id) DO UPDATE SET\n claim_host_id = excluded.claim_host_id,\n claim_pid = excluded.claim_pid,\n claim_nonce = excluded.claim_nonce,\n claimed_at_ms = excluded.claimed_at_ms\n WHERE flows_consensus_leases.claim_host_id IS NULL\n AND flows_consensus_leases.owner_host_id IS NULL\n RETURNING run_id\n `\n if (rows.length > 0) return claimed(nowMs)\n // The upsert only misses on a conflicting row, so the lease exists and\n // holds a claim, an owner, or both.\n const lease = (yield* leaseOf(runId))[0]!\n return lease.claim_host_id !== null ? rejected(\"already_claimed\") : rejected(\"owner_held\")\n }))\n )\n\n const activate: Service[\"activate\"] = Effect.fn(\"Consensus.activate\")((\n runId: string,\n owner: OwnerId,\n grantedAtMs: number,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET\n owner_host_id = ${owner.hostId},\n owner_pid = ${owner.pid},\n owner_nonce = ${owner.nonce},\n granted_at_ms = ${grantedAtMs},\n heartbeat_at_ms = ${nowMs},\n claim_host_id = NULL,\n claim_pid = NULL,\n claim_nonce = NULL,\n claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${owner.hostId}\n AND claim_pid = ${owner.pid}\n AND claim_nonce = ${owner.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n RETURNING run_id\n `\n return (rows.length > 0 ? activated : lost) satisfies ActivateOutcome\n }))\n )\n\n const heartbeat: Service[\"heartbeat\"] = Effect.fn(\"Consensus.heartbeat\")((\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n // Monotonic: a pulse delayed past a newer one from the same owner never\n // moves the lease backwards and never makes a live run look stale.\n const rows = yield* sql<{ readonly heartbeat_at_ms: number }>`\n UPDATE flows_consensus_leases\n SET heartbeat_at_ms = ${Dialect.greatest(sql)}(heartbeat_at_ms, ${nowMs})\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n RETURNING heartbeat_at_ms\n `\n const row = rows[0]\n return (row === undefined\n ? lost\n : { _tag: \"Renewed\", heartbeatAtMs: Number(row.heartbeat_at_ms) }) satisfies HeartbeatOutcome\n }))\n )\n\n const reconfirm: Service[\"reconfirm\"] = Effect.fn(\"Consensus.reconfirm\")((\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n // A pending steal has already reserved the lease, even before activation.\n // Compare both owner and claim in the same update as the renewal.\n const rows = yield* sql<{ readonly heartbeat_at_ms: number }>`\n UPDATE flows_consensus_leases\n SET heartbeat_at_ms = ${Dialect.greatest(sql)}(heartbeat_at_ms, ${nowMs})\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n AND claim_host_id IS NULL\n RETURNING heartbeat_at_ms\n `\n const row = rows[0]\n return (row === undefined\n ? lost\n : { _tag: \"Renewed\", heartbeatAtMs: Number(row.heartbeat_at_ms) }) satisfies HeartbeatOutcome\n }))\n )\n\n const release: Service[\"release\"] = Effect.fn(\"Consensus.release\")((runId: string, owner: OwnerId) =>\n write(Effect.gen(function*() {\n yield* sql`\n UPDATE flows_consensus_leases\n SET claim_host_id = NULL, claim_pid = NULL, claim_nonce = NULL, claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${owner.hostId}\n AND claim_pid = ${owner.pid}\n AND claim_nonce = ${owner.nonce}\n `\n yield* sql`\n UPDATE flows_consensus_leases\n SET owner_host_id = NULL, owner_pid = NULL, owner_nonce = NULL,\n granted_at_ms = NULL, heartbeat_at_ms = NULL\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n `\n yield* prune(runId)\n }))\n )\n\n const steal: Service[\"steal\"] = Effect.fn(\"Consensus.steal\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.suspend((): Effect.Effect<ClaimOutcome, ConsensusError> => {\n if (!matchesEvidence(evidence.expectedOwner, claimant, nowMs, evidence)) {\n return Effect.succeed(rejected(\"evidence_invalid\"))\n }\n return write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET\n claim_host_id = ${claimant.hostId},\n claim_pid = ${claimant.pid},\n claim_nonce = ${claimant.nonce},\n claimed_at_ms = ${nowMs}\n WHERE run_id = ${runId}\n AND owner_host_id = ${evidence.expectedOwner.hostId}\n AND owner_pid = ${evidence.expectedOwner.pid}\n AND owner_nonce = ${evidence.expectedOwner.nonce}\n AND ${provesDeath(evidence, claimant) ? sql`TRUE` : sql`heartbeat_at_ms < ${nowMs - staleAfterMs}`}\n AND claim_host_id IS NULL\n RETURNING run_id\n `\n if (rows.length > 0) return claimed(nowMs)\n const lease = (yield* leaseOf(runId))[0]\n if (lease !== undefined && lease.claim_host_id !== null) return rejected(\"already_claimed\")\n if (\n lease === undefined ||\n lease.owner_host_id !== evidence.expectedOwner.hostId ||\n Number(lease.owner_pid) !== evidence.expectedOwner.pid ||\n lease.owner_nonce !== evidence.expectedOwner.nonce\n ) {\n // A run nobody owns, or one owned by somebody else, has no owner\n // this evidence could disprove.\n return rejected(\"evidence_invalid\")\n }\n return rejected(\"owner_live\")\n }))\n })\n )\n\n const recover: Service[\"recover\"] = Effect.fn(\"Consensus.recover\")((\n runId: string,\n staleClaimant: OwnerId,\n grantedAtMs: number,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.suspend((): Effect.Effect<RecoverOutcome, ConsensusError> => {\n if (!matchesEvidence(staleClaimant, observer, nowMs, evidence)) {\n return Effect.succeed(rejected(\"evidence_invalid\"))\n }\n return write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET claim_host_id = NULL, claim_pid = NULL, claim_nonce = NULL, claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${staleClaimant.hostId}\n AND claim_pid = ${staleClaimant.pid}\n AND claim_nonce = ${staleClaimant.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n AND claimed_at_ms < ${nowMs - staleAfterMs}\n RETURNING run_id\n `\n if (rows.length > 0) {\n yield* prune(runId)\n return recovered\n }\n const matches = yield* sql<{ readonly run_id: string }>`\n SELECT run_id FROM flows_consensus_leases\n WHERE run_id = ${runId}\n AND claim_host_id = ${staleClaimant.hostId}\n AND claim_pid = ${staleClaimant.pid}\n AND claim_nonce = ${staleClaimant.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n `\n // The exact claim survived the compare-and-swap only because it is\n // not yet stale; anything else means the claim moved on.\n return matches.length > 0 ? rejected(\"claim_fresh\") : rejected(\"claim_changed\")\n }))\n })\n )\n\n const guard: Service[\"guard\"] = Effect.fn(\"Consensus.guard\")((runId: string, owner: OwnerId) =>\n write(Effect.gen(function*() {\n const held = yield* sql<{ readonly ok: number }>`\n SELECT 1 AS ok FROM flows_consensus_leases\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n `\n if (held.length === 0) {\n return yield* Effect.fail(fenceLost(runId, owner))\n }\n }))\n )\n\n return Consensus.of({ claim, activate, heartbeat, reconfirm, release, steal, recover, guard })\n})\n\n/**\n * Provides the database-backed strategy.\n *\n * @since 1.0.0\n * @category layers\n */\nexport const layer: Layer.Layer<Consensus, never, DurableWriter | SqlClient.SqlClient> = Layer.effect(Consensus, make)\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwBA,cAAyB;AACzB,2BAA8B;AAC9B,eAA0B;AAC1B,aAAwB;AACxB,YAAuB;AACvB,gBAA2B;AAC3B,uBAgBO;AAGP,MAAM,eAAe,SAAS,SAAS,oCAAmB;AAE1D,MAAM,WAAW,CAAC,YAAuC,EAAE,MAAM,YAAY,OAAO;AACpF,MAAM,UAAU,CAAC,iBAAkC,EAAE,MAAM,WAAW,YAAY;AAClF,MAAM,OAAO,EAAE,MAAM,OAAO;AAC5B,MAAM,YAAY,EAAE,MAAM,YAAY;AACtC,MAAM,YAAY,EAAE,MAAM,YAAY;AAkB/B,MAAM,OAA2E,OAAO,IAAI,aAAY;AAC7G,QAAM,MAAM,OAAO,OAAO,QAAQ,UAAU,SAAS;AACrD,QAAM,SAAS,OAAO;AAEtB,QAAM,gBAAgB,CAAC,UACrB,IAAI,gCAAe;AAAA,IACjB,MAAM;AAAA,IACN,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AAKH,QAAM,QAAQ,CACZ,SAEA,OAAO,MAAM,IAAI,EAAE,KAAK,OAAO,S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OAAO,OAAO;AAAA;AAAA;AAAA,2BAGD,KAAK;AAAA,kCACE,cAAc,MAAM;AAAA,8BACxB,cAAc,GAAG;AAAA,gCACf,cAAc,KAAK;AAAA,kCACjB,WAAW;AAAA,kCACX,QAAQ,YAAY;AAAA;AAAA;AAG9C,YAAI,KAAK,SAAS,GAAG;AACnB,iBAAO,MAAM,KAAK;AAClB,iBAAO;AAAA,QACT;AACA,cAAM,UAAU,OAAO;AAAA;AAAA,2BAEJ,KAAK;AAAA,kCACE,cAAc,MAAM;AAAA,8BACxB,cAAc,GAAG;AAAA,gCACf,cAAc,KAAK;AAAA,kCACjB,WAAW;AAAA;AAIrC,eAAO,QAAQ,SAAS,IAAI,SAAS,aAAa,IAAI,SAAS,eAAe;AAAA,MAChF,CAAC,CAAC;AAAA,IACJ,CAAC;AAAA,EACH;AAEA,QAAM,QAA0B,OAAO,GAAG,iBAAiB;AAAA,IAAE,CAAC,OAAe,UAC3E,MAAM,OAAO,IAAI,aAAY;AAC3B,YAAM,OAAO,OAAO;AAAA;AAAA,yBAED,KAAK;AAAA,gCACE,MAAM,MAAM;AAAA,4BAChB,MAAM,GAAG;AAAA,8BACP,MAAM,KAAK;AAAA;AAEnC,UAAI,KAAK,WAAW,GAAG;AACrB,eAAO,OAAO,OAAO,SAAK,4BAAU,OAAO,KAAK,CAAC;AAAA,MACnD;AAAA,IACF,CAAC,CAAC;AAAA,EACJ;AAEA,SAAO,2BAAU,GAAG,EAAE,OAAO,UAAU,WAAW,WAAW,SAAS,OAAO,SAAS,MAAM,CAAC;AAC/F,CAAC;AAQM,MAAM,QAA4E,MAAM,OAAO,4BAAW,IAAI;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/test/Notifying.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Effect-native analogue of Bazel Skyframe's `NotifyingHelper` and\n * `NotifyingInMemoryGraph`: wrap a record-of-Effect-methods service so a listener hook fires around\n * every operation. The hook runs in the calling fiber, so a hook that dies,\n * interrupts, or awaits a `Latch` injects crashes, fence loss, and exact\n * sequencing at any durable transition point, the mechanism that makes\n * interstitial interleaving bugs testable at all.\n *\n * Deliberately NOT ported from Skyframe: `DeterministicHelper`'s\n * deterministic-scheduler machinery, `ChainedFunction`, and `TrackingAwaiter`.\n * Effect already gives us deterministic fibers, `TestClock`, `Latch`, and\n * failure propagation across fiber joins, so those helpers have no residue\n * here.\n *\n * @since 0.1.0\n */\n\nimport type * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\n\n/**\n * Whether the hook is firing before or after the delegated operation.\n * Skyframe's `NotifyingHelper.Order`, minus its `BEFORE_AND_AFTER` (pass the\n * same hook body for both firings instead).\n *\n * @category models\n * @since 0.1.0\n */\nexport type Order = \"before\" | \"after\"\n\n/**\n * Listener invoked around every delegated operation. `op` is the service\n * method name (Skyframe's `EventType`, but open-world), `args` the call\n * arguments. Express crash injection as `Effect.die` or `Effect.interrupt`\n * inside the hook, the hook's type stays `Effect<void>` so the wrapped\n * service is structurally identical to the real one.\n *\n * The `after` firing only happens when the operation succeeds; a failed\n * operation already is the interesting event.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Hook = (\n op: string,\n order: Order,\n args: ReadonlyArray<unknown>\n) => Effect.Effect<void>\n\n/**\n * Wraps a service so `hook(op, \"before\" | \"after\", args)` fires around every\n * Effect-valued operation, delegating to the real implementation in between.\n *\n * Generic over any record-of-Effect-methods service (`Journal`, `CacheStore`,\n * `AttemptStore`, `RunStore`, ...): Effect-returning methods and plain Effect\n * properties are hooked; non-Effect members (e.g. `Stream`-returning methods)\n * delegate untouched.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const wrap = <S extends object>(service: S, hook: Hook): S => {\n const wrapped: Record<string, unknown> = {}\n for (const key of Object.keys(service)) {\n const value = (service as Record<string, unknown>)[key]\n if (Effect.isEffect(value)) {\n wrapped[key] = hook(key, \"before\", []).pipe(\n Effect.andThen(value
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,aAAwB;AACxB,YAAuB;AA2ChB,MAAM,OAAO,CAAmB,SAAY,SAAkB;AACnE,QAAM,UAAmC,CAAC;AAC1C,aAAW,OAAO,OAAO,KAAK,OAAO,GAAG;AACtC,UAAM,QAAS,QAAoC,GAAG;AACtD,QAAI,OAAO,SAAS,KAAK,GAAG;AAC1B,cAAQ,GAAG,IAAI,KAAK,KAAK,UAAU,CAAC,CAAC,EAAE;AAAA,QACrC,OAAO,QAAQ,
|
|
4
|
+
"sourcesContent": ["/**\n * Effect-native analogue of Bazel Skyframe's `NotifyingHelper` and\n * `NotifyingInMemoryGraph`: wrap a record-of-Effect-methods service so a listener hook fires around\n * every operation. The hook runs in the calling fiber, so a hook that dies,\n * interrupts, or awaits a `Latch` injects crashes, fence loss, and exact\n * sequencing at any durable transition point, the mechanism that makes\n * interstitial interleaving bugs testable at all.\n *\n * Deliberately NOT ported from Skyframe: `DeterministicHelper`'s\n * deterministic-scheduler machinery, `ChainedFunction`, and `TrackingAwaiter`.\n * Effect already gives us deterministic fibers, `TestClock`, `Latch`, and\n * failure propagation across fiber joins, so those helpers have no residue\n * here.\n *\n * @since 0.1.0\n */\n\nimport type * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\n\n/**\n * Whether the hook is firing before or after the delegated operation.\n * Skyframe's `NotifyingHelper.Order`, minus its `BEFORE_AND_AFTER` (pass the\n * same hook body for both firings instead).\n *\n * @category models\n * @since 0.1.0\n */\nexport type Order = \"before\" | \"after\"\n\n/**\n * Listener invoked around every delegated operation. `op` is the service\n * method name (Skyframe's `EventType`, but open-world), `args` the call\n * arguments. Express crash injection as `Effect.die` or `Effect.interrupt`\n * inside the hook, the hook's type stays `Effect<void>` so the wrapped\n * service is structurally identical to the real one.\n *\n * The `after` firing only happens when the operation succeeds; a failed\n * operation already is the interesting event.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Hook = (\n op: string,\n order: Order,\n args: ReadonlyArray<unknown>\n) => Effect.Effect<void>\n\n/**\n * Wraps a service so `hook(op, \"before\" | \"after\", args)` fires around every\n * Effect-valued operation, delegating to the real implementation in between.\n *\n * Generic over any record-of-Effect-methods service (`Journal`, `CacheStore`,\n * `AttemptStore`, `RunStore`, ...): Effect-returning methods and plain Effect\n * properties are hooked; non-Effect members (e.g. `Stream`-returning methods)\n * delegate untouched.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const wrap = <S extends object>(service: S, hook: Hook): S => {\n const wrapped: Record<string, unknown> = {}\n for (const key of Object.keys(service)) {\n const value = (service as Record<string, unknown>)[key]\n if (Effect.isEffect(value)) {\n wrapped[key] = hook(key, \"before\", []).pipe(\n Effect.andThen(value),\n Effect.tap(() => hook(key, \"after\", []))\n )\n } else if (typeof value === \"function\") {\n const method = value as (...args: Array<unknown>) => unknown\n wrapped[key] = (...args: Array<unknown>) => {\n const result = method.apply(service, args)\n return Effect.isEffect(result)\n ? hook(key, \"before\", args).pipe(\n Effect.andThen(result),\n Effect.tap(() => hook(key, \"after\", args))\n )\n : result\n }\n } else {\n wrapped[key] = value\n }\n }\n return wrapped as S\n}\n\n/**\n * Layer form of {@link wrap}: reads the real service from the environment and\n * re-provides the notifying wrapper under the same key, so an existing stack\n * gains a listener without touching its construction.\n *\n * @category layers\n * @since 0.1.0\n */\nexport const layer = <Id, S extends object>(\n tag: Context.Key<Id, S>,\n hook: Hook\n): Layer.Layer<Id, never, Id> => Layer.effect(tag)(Effect.map(tag, (service) => wrap(service, hook)))\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,aAAwB;AACxB,YAAuB;AA2ChB,MAAM,OAAO,CAAmB,SAAY,SAAkB;AACnE,QAAM,UAAmC,CAAC;AAC1C,aAAW,OAAO,OAAO,KAAK,OAAO,GAAG;AACtC,UAAM,QAAS,QAAoC,GAAG;AACtD,QAAI,OAAO,SAAS,KAAK,GAAG;AAC1B,cAAQ,GAAG,IAAI,KAAK,KAAK,UAAU,CAAC,CAAC,EAAE;AAAA,QACrC,OAAO,QAAQ,KAAK;AAAA,QACpB,OAAO,IAAI,MAAM,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC;AAAA,MACzC;AAAA,IACF,WAAW,OAAO,UAAU,YAAY;AACtC,YAAM,SAAS;AACf,cAAQ,GAAG,IAAI,IAAI,SAAyB;AAC1C,cAAM,SAAS,OAAO,MAAM,SAAS,IAAI;AACzC,eAAO,OAAO,SAAS,MAAM,IACzB,KAAK,KAAK,UAAU,IAAI,EAAE;AAAA,UAC1B,OAAO,QAAQ,MAAM;AAAA,UACrB,OAAO,IAAI,MAAM,KAAK,KAAK,SAAS,IAAI,CAAC;AAAA,QAC3C,IACE;AAAA,MACN;AAAA,IACF,OAAO;AACL,cAAQ,GAAG,IAAI;AAAA,IACjB;AAAA,EACF;AACA,SAAO;AACT;AAUO,MAAM,QAAQ,CACnB,KACA,SAC+B,MAAM,OAAO,GAAG,EAAE,OAAO,IAAI,KAAK,CAAC,YAAY,KAAK,SAAS,IAAI,CAAC,CAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/Consensus.d.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* The journal owns the consensus rules — single writer per run, fenced and
|
|
5
5
|
* unfenced appends, commit-time admission, the generation fence, and steal
|
|
6
|
-
* only on staleness plus liveness evidence
|
|
6
|
+
* only on staleness plus liveness evidence, or on a same-host probe that
|
|
7
|
+
* proves the owner's process gone — and this service is how a deployment chooses *how* those rules are
|
|
7
8
|
* arbitrated: where the lease lives, how liveness is probed, and how the
|
|
8
9
|
* fence is checked at commit time. `Consensus` lives here beside `OwnerId`
|
|
9
10
|
* for the same reason `OwnerId` does: the journal is what it fences.
|
|
@@ -105,7 +106,8 @@ export declare const heartbeatWriteTolerance: Duration.Duration;
|
|
|
105
106
|
* strategy can check for itself, and `steal` checks it: the takeover refuses
|
|
106
107
|
* any lease whose heartbeat is still inside the window. It is therefore
|
|
107
108
|
* accepted from a claimant on any host, while the other two stay bound to the
|
|
108
|
-
* host relation that makes them meaningful.
|
|
109
|
+
* host relation that makes them meaningful. `same-host-pid-dead` alone also
|
|
110
|
+
* admits a steal inside the window; see {@link provesDeath}. The observation must carry the
|
|
109
111
|
* exact instant supplied to the consuming operation: `checkedAtMs` must equal
|
|
110
112
|
* that operation's `nowMs`. A caller that probes at T and calls at T+1 is
|
|
111
113
|
* refused and must build fresh evidence.
|
|
@@ -149,6 +151,32 @@ export declare const sameOwner: (left: OwnerId, right: OwnerId) => boolean;
|
|
|
149
151
|
* @category predicates
|
|
150
152
|
*/
|
|
151
153
|
export declare const matchesEvidence: (expectedOwner: OwnerId, observer: OwnerId, nowMs: number, evidence: LivenessEvidence) => boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Whether evidence proves the owner gone, so a steal need not wait for its
|
|
156
|
+
* lease to go stale.
|
|
157
|
+
*
|
|
158
|
+
* The lease stands in for a question a peer cannot ask directly: is the owner
|
|
159
|
+
* still running? A `same-host-pid-dead` probe asks it. The claimant read its
|
|
160
|
+
* own host's process table and found no process behind the owner's pid, and a
|
|
161
|
+
* process that does not exist renews no lease and writes nothing, so waiting
|
|
162
|
+
* out `heartbeatStaleAfter` adds no safety. A run whose owner was killed with
|
|
163
|
+
* `SIGKILL` is therefore reclaimed by the next process on the same host at
|
|
164
|
+
* once.
|
|
165
|
+
*
|
|
166
|
+
* The evidence must still pass {@link matchesEvidence}, which binds the kind
|
|
167
|
+
* to the same host. One more case is excluded here: an owner recorded with the
|
|
168
|
+
* claimant's own pid names the claimant's process, which is running, so a
|
|
169
|
+
* probe of that pid cannot report it gone. Such evidence keeps the staleness
|
|
170
|
+
* cutoff.
|
|
171
|
+
*
|
|
172
|
+
* The other kinds never waive the cutoff. `cross-host-unreachable-stale` is a
|
|
173
|
+
* judgement about a host this process cannot inspect, and `lease-expired` is
|
|
174
|
+
* the cutoff itself.
|
|
175
|
+
*
|
|
176
|
+
* @since 1.0.0
|
|
177
|
+
* @category predicates
|
|
178
|
+
*/
|
|
179
|
+
export declare const provesDeath: (evidence: LivenessEvidence, claimant: OwnerId) => boolean;
|
|
152
180
|
/**
|
|
153
181
|
* Why a strategy refused a claim, steal, or recovery.
|
|
154
182
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Consensus.d.ts","sourceRoot":"","sources":["../../src/Consensus.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"Consensus.d.ts","sourceRoot":"","sources":["../../src/Consensus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,QAA8B,CAAA;AAEvE;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,QAA+B,CAAA;AAE1E;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAAC,QAA+B,CAAA;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,QAI9C,CAAA;AAID;;;;;;;;;;;;;;;;;;;;;;;G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,KAAG,cAItD,CAAA;AAEJ;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,YAAW,OAAO,CAAC,OAAO,CAAM,KAAG,OAWxD,CAAA;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,YAAW,OAAO,CAAC,OAAO,CAAM,KAAG,KAAK,CAAC,KAAK,CAAC,SAAS,CACnC,CAAA;AAO/C;;;;;GAKG;AACH,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,CAoI3C,CAAA;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,UAAU,EAAE,KAAK,CAAC,KAAK,CAAC,SAAS,CAAsC,CAAA"}
|
package/dist/esm/Consensus.js
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* The journal owns the consensus rules — single writer per run, fenced and
|
|
5
5
|
* unfenced appends, commit-time admission, the generation fence, and steal
|
|
6
|
-
* only on staleness plus liveness evidence
|
|
6
|
+
* only on staleness plus liveness evidence, or on a same-host probe that
|
|
7
|
+
* proves the owner's process gone — and this service is how a deployment chooses *how* those rules are
|
|
7
8
|
* arbitrated: where the lease lives, how liveness is probed, and how the
|
|
8
9
|
* fence is checked at commit time. `Consensus` lives here beside `OwnerId`
|
|
9
10
|
* for the same reason `OwnerId` does: the journal is what it fences.
|
|
@@ -108,7 +109,8 @@ const staleAfterMs = Duration.toMillis(heartbeatStaleAfter);
|
|
|
108
109
|
* strategy can check for itself, and `steal` checks it: the takeover refuses
|
|
109
110
|
* any lease whose heartbeat is still inside the window. It is therefore
|
|
110
111
|
* accepted from a claimant on any host, while the other two stay bound to the
|
|
111
|
-
* host relation that makes them meaningful.
|
|
112
|
+
* host relation that makes them meaningful. `same-host-pid-dead` alone also
|
|
113
|
+
* admits a steal inside the window; see {@link provesDeath}. The observation must carry the
|
|
112
114
|
* exact instant supplied to the consuming operation: `checkedAtMs` must equal
|
|
113
115
|
* that operation's `nowMs`. A caller that probes at T and calls at T+1 is
|
|
114
116
|
* refused and must build fresh evidence.
|
|
@@ -157,6 +159,32 @@ export const matchesEvidence = (expectedOwner, observer, nowMs, evidence) => {
|
|
|
157
159
|
return true;
|
|
158
160
|
}
|
|
159
161
|
};
|
|
162
|
+
/**
|
|
163
|
+
* Whether evidence proves the owner gone, so a steal need not wait for its
|
|
164
|
+
* lease to go stale.
|
|
165
|
+
*
|
|
166
|
+
* The lease stands in for a question a peer cannot ask directly: is the owner
|
|
167
|
+
* still running? A `same-host-pid-dead` probe asks it. The claimant read its
|
|
168
|
+
* own host's process table and found no process behind the owner's pid, and a
|
|
169
|
+
* process that does not exist renews no lease and writes nothing, so waiting
|
|
170
|
+
* out `heartbeatStaleAfter` adds no safety. A run whose owner was killed with
|
|
171
|
+
* `SIGKILL` is therefore reclaimed by the next process on the same host at
|
|
172
|
+
* once.
|
|
173
|
+
*
|
|
174
|
+
* The evidence must still pass {@link matchesEvidence}, which binds the kind
|
|
175
|
+
* to the same host. One more case is excluded here: an owner recorded with the
|
|
176
|
+
* claimant's own pid names the claimant's process, which is running, so a
|
|
177
|
+
* probe of that pid cannot report it gone. Such evidence keeps the staleness
|
|
178
|
+
* cutoff.
|
|
179
|
+
*
|
|
180
|
+
* The other kinds never waive the cutoff. `cross-host-unreachable-stale` is a
|
|
181
|
+
* judgement about a host this process cannot inspect, and `lease-expired` is
|
|
182
|
+
* the cutoff itself.
|
|
183
|
+
*
|
|
184
|
+
* @since 1.0.0
|
|
185
|
+
* @category predicates
|
|
186
|
+
*/
|
|
187
|
+
export const provesDeath = (evidence, claimant) => evidence.kind === "same-host-pid-dead" && evidence.expectedOwner.pid !== claimant.pid;
|
|
160
188
|
/**
|
|
161
189
|
* Why a strategy refused a claim, steal, or recovery.
|
|
162
190
|
*
|
|
@@ -403,8 +431,9 @@ export const makeLocal = Effect.sync(() => {
|
|
|
403
431
|
if (lease?.owner === undefined || !sameOwner(lease.owner.id, evidence.expectedOwner)) {
|
|
404
432
|
return rejected("evidence_invalid");
|
|
405
433
|
}
|
|
406
|
-
if (lease.owner.heartbeatAtMs >= nowMs - staleAfterMs)
|
|
434
|
+
if (!provesDeath(evidence, claimant) && lease.owner.heartbeatAtMs >= nowMs - staleAfterMs) {
|
|
407
435
|
return rejected("owner_live");
|
|
436
|
+
}
|
|
408
437
|
lease.claim = { id: claimant, claimedAtMs: nowMs };
|
|
409
438
|
return claimed(nowMs);
|
|
410
439
|
})),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Consensus.js","sourceRoot":"","sources":["../../src/Consensus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAsB,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;AAEvE;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAsB,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;AAE1E;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAsB,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAsB,QAAQ,CAAC,MAAM,CACvE,QAAQ,CAAC,QAAQ,CAAC,mBAAmB,CAAC;IACpC,QAAQ,CAAC,QAAQ,CAAC,sBAAsB,CAAC;IACzC,QAAQ,CAAC,QAAQ,CAAC,iB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MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;YAC/B,OAAO,KAAK,EAAE,KAAK,KAAK,SAAS,IAAI,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC;gBACnE,CAAC,CAAC,MAAM,CAAC,IAAI;gBACb,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;QAC1C,CAAC,CAAC,CACH;KACF,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,UAAU,GAA2B,KAAK,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA","sourcesContent":["/**\n * The injectable consensus strategy arbitrating the journal's ownership rules.\n *\n * The journal owns the consensus rules — single writer per run, fenced and\n * unfenced appends, commit-time admission, the generation fence, and steal\n * only on staleness plus liveness evidence — and this service is how a deployment chooses *how* those rules are\n * arbitrated: where the lease lives, how liveness is probed, and how the\n * fence is checked at commit time. `Consensus` lives here beside `OwnerId`\n * for the same reason `OwnerId` does: the journal is what it fences.\n * `@smthrs/run-store`'s `Ownership` keeps its public API and rules but\n * delegates arbitration to this strategy.\n *\n * Every operation that reasons about staleness takes the caller's clock\n * reading (`nowMs`) rather than reading a clock itself: staleness (R5) and\n * the monotonic lease stamp are defined against the arbitrating caller's\n * reading, exactly as `RunStore`'s compare-and-swap operations always were,\n * so the recorded lease never differs from the outcome the caller observed.\n *\n * Governing design: `packages/smithers/flows/journal/docs/concepts/owner-fence.md`.\n * Rule constants and their provenance:\n * `packages/smithers/flows/run-store/docs/concepts/leases.md`.\n *\n * Prior art: Effect `EventJournal` (`reference/effect` `unstable/eventlog`)\n * resolves multi-writer by merge with conflict surfacing; driving a run is not\n * mergeable, so this surface deviates to exclusive-writer admission. The\n * fencing and generation semantics follow Temporal's shard `rangeID`\n * (`reference/temporal/service/history/shard/context_impl.go`).\n *\n * @since 1.0.0\n */\n\nimport * as Context from \"effect/Context\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport * as Schema from \"effect/Schema\"\nimport { OwnerId } from \"./OwnerId.ts\"\n\n/**\n * How often the supervision loop pulses to renew the owner's lease.\n *\n * @since 1.0.0\n * @category constants\n */\nexport const heartbeatInterval: Duration.Duration = Duration.seconds(1)\n\n/**\n * How old a persisted heartbeat must be before a peer may steal the run.\n * Every strategy judges R5 staleness against this one definition.\n *\n * @since 1.0.0\n * @category constants\n */\nexport const heartbeatStaleAfter: Duration.Duration = Duration.seconds(30)\n\n/**\n * How far the owner's wall clock may run behind a peer's before the lease\n * reasoning stops holding.\n *\n * The owner stamps its heartbeat from *its* clock and a would-be stealer\n * compares that stamp against *its own*, so the two hosts' clock offset is\n * subtracted directly from the owner's real safety margin. This constant names\n * that allowance instead of leaving it implicit in a \"two ticks\" arithmetic\n * accident, and it is a budgeted allowance, not a guarantee: see\n * {@link heartbeatWriteTolerance} for what happens once it is exceeded.\n *\n * @since 1.0.0\n * @category constants\n */\nexport const heartbeatSkewAllowance: Duration.Duration = Duration.seconds(10)\n\n/**\n * How long heartbeat writes may go unconfirmed before ownership must be reconfirmed.\n *\n * A peer judges staleness by the persisted heartbeat and may steal the run the\n * instant it is {@link heartbeatStaleAfter} old, so an owner that tolerated\n * write failures for exactly that long would still be executing side effects\n * when the steal is admitted. The budget is therefore\n * {@link heartbeatStaleAfter} minus {@link heartbeatSkewAllowance} (the peer's\n * clock may already read that much later than the owner's) minus one\n * {@link heartbeatInterval} (reserved for one bounded compare-and-swap\n * reconfirmation before the owner interrupts).\n *\n * This bounds, but does not eliminate, overlap. Beyond\n * {@link heartbeatSkewAllowance} of clock offset a peer can be admitted while\n * the old owner is still running. *Durable* writes stay safe regardless — they\n * are fenced by the strategy's commit-time admission, so the displaced owner's\n * writes fail rather than corrupt. Non-durable external side effects (an HTTP\n * call, a spawned process) can genuinely overlap. That is inherent to any\n * wall-clock lease and is stated here rather than asserted away; a caller that\n * cannot tolerate any overlap needs an external fencing token at the side\n * effect itself, not a larger timeout.\n *\n * @since 1.0.0\n * @category constants\n */\nexport const heartbeatWriteTolerance: Duration.Duration = Duration.millis(\n Duration.toMillis(heartbeatStaleAfter) -\n Duration.toMillis(heartbeatSkewAllowance) -\n Duration.toMillis(heartbeatInterval)\n)\n\nconst staleAfterMs = Duration.toMillis(heartbeatStaleAfter)\n\n/**\n * Evidence that the owner in an exact run snapshot is no longer live.\n *\n * Defined here because R5 — steal requires staleness plus liveness evidence —\n * is a consensus rule every strategy validates. `@smthrs/run-store`'s\n * `Ownership` re-exports it alongside the probes that mint it.\n *\n * Two of the three kinds are collected outside the strategy:\n * `same-host-pid-dead` is a local process probe, and\n * `cross-host-unreachable-stale` is a reachability judgement the deployment\n * makes. `lease-expired` is different — it asserts only that the persisted\n * heartbeat is older than the staleness cutoff, which is the one claim a\n * strategy can check for itself, and `steal` checks it: the takeover refuses\n * any lease whose heartbeat is still inside the window. It is therefore\n * accepted from a claimant on any host, while the other two stay bound to the\n * host relation that makes them meaningful. The observation must carry the\n * exact instant supplied to the consuming operation: `checkedAtMs` must equal\n * that operation's `nowMs`. A caller that probes at T and calls at T+1 is\n * refused and must build fresh evidence.\n *\n * @since 1.0.0\n * @category models\n */\nexport const LivenessEvidence = Schema.Struct({\n expectedOwner: OwnerId,\n checkedAtMs: Schema.Number,\n kind: Schema.Literals([\"same-host-pid-dead\", \"cross-host-unreachable-stale\", \"lease-expired\"])\n})\n\n/**\n * Evidence that the owner in an exact run snapshot is no longer live.\n *\n * @since 1.0.0\n * @category models\n */\nexport type LivenessEvidence = typeof LivenessEvidence.Type\n\n/**\n * Structural equality on the process identity tuple.\n *\n * @since 1.0.0\n * @category predicates\n */\nexport const sameOwner = (left: OwnerId, right: OwnerId): boolean =>\n left.hostId === right.hostId && left.pid === right.pid && left.nonce === right.nonce\n\n/**\n * Validates liveness evidence against the owner it claims to disprove.\n *\n * The evidence must name the expected owner, carry the observer's current\n * clock reading, and respect the host rule: a `same-host-pid-dead` probe is\n * only meaningful from the same host, a `cross-host-unreachable-stale`\n * observation only from a different one, and `lease-expired` is host-neutral\n * because the strategy verifies the cutoff it names for itself.\n *\n * @since 1.0.0\n * @category predicates\n */\nexport const matchesEvidence = (\n expectedOwner: OwnerId,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n): boolean => {\n if (!sameOwner(expectedOwner, evidence.expectedOwner) || evidence.checkedAtMs !== nowMs) return false\n switch (evidence.kind) {\n // A pid means nothing outside the host that owns the process namespace.\n case \"same-host-pid-dead\":\n return expectedOwner.hostId === observer.hostId\n // Unreachability is what a peer host observes; on the owner's own host it\n // would be an unprobed guess dressed as evidence.\n case \"cross-host-unreachable-stale\":\n return expectedOwner.hostId !== observer.hostId\n // The lease is host-neutral, and the strategy verifies the same\n // caller-clock cutoff named by this evidence.\n case \"lease-expired\":\n return true\n }\n}\n\n/**\n * Why a strategy refused a claim, steal, or recovery.\n *\n * @since 1.0.0\n * @category models\n */\nexport const RejectionReason = Schema.Literals([\n \"already_claimed\",\n \"owner_held\",\n \"owner_live\",\n \"evidence_invalid\",\n \"claim_fresh\",\n \"claim_changed\",\n \"unavailable\"\n])\n\n/**\n * Why a strategy refused a claim, steal, or recovery.\n *\n * @since 1.0.0\n * @category models\n */\nexport type RejectionReason = typeof RejectionReason.Type\n\n/**\n * A granted claim, carrying the grant timestamp the later activation must\n * present (R4 — the generation fence).\n *\n * @since 1.0.0\n * @category models\n */\nexport const Claimed = Schema.TaggedStruct(\"Claimed\", {\n grantedAtMs: Schema.Number\n})\n\n/**\n * A granted claim.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Claimed = typeof Claimed.Type\n\n/**\n * A refused claim, steal, or recovery, with the strategy's typed reason.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Rejected = Schema.TaggedStruct(\"Rejected\", {\n reason: RejectionReason\n})\n\n/**\n * A refused claim, steal, or recovery.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Rejected = typeof Rejected.Type\n\n/**\n * Outcome of `claim` and `steal`.\n *\n * @since 1.0.0\n * @category models\n */\nexport const ClaimOutcome = Schema.Union([Claimed, Rejected])\n\n/**\n * Outcome of `claim` and `steal`.\n *\n * @since 1.0.0\n * @category models\n */\nexport type ClaimOutcome = typeof ClaimOutcome.Type\n\n/**\n * A claim promoted to ownership.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Activated = Schema.TaggedStruct(\"Activated\", {})\n\n/**\n * A claim promoted to ownership.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Activated = typeof Activated.Type\n\n/**\n * The claim or lease the caller presented no longer exists.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Lost = Schema.TaggedStruct(\"Lost\", {})\n\n/**\n * The claim or lease the caller presented no longer exists.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Lost = typeof Lost.Type\n\n/**\n * Outcome of `activate`.\n *\n * @since 1.0.0\n * @category models\n */\nexport const ActivateOutcome = Schema.Union([Activated, Lost])\n\n/**\n * Outcome of `activate`.\n *\n * @since 1.0.0\n * @category models\n */\nexport type ActivateOutcome = typeof ActivateOutcome.Type\n\n/**\n * A renewed lease, carrying the monotonic heartbeat stamp the strategy\n * recorded — the maximum of the stored stamp and the caller's reading, so a\n * late pulse never moves the lease backwards.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Renewed = Schema.TaggedStruct(\"Renewed\", {\n heartbeatAtMs: Schema.Number\n})\n\n/**\n * A renewed lease.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Renewed = typeof Renewed.Type\n\n/**\n * Outcome of `heartbeat` and `reconfirm`.\n *\n * @since 1.0.0\n * @category models\n */\nexport const HeartbeatOutcome = Schema.Union([Renewed, Lost])\n\n/**\n * Outcome of `heartbeat` and `reconfirm`.\n *\n * @since 1.0.0\n * @category models\n */\nexport type HeartbeatOutcome = typeof HeartbeatOutcome.Type\n\n/**\n * A stale claim cleared after its claimant was proven dead.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Recovered = Schema.TaggedStruct(\"Recovered\", {})\n\n/**\n * A stale claim cleared after its claimant was proven dead.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Recovered = typeof Recovered.Type\n\n/**\n * Outcome of `recover`.\n *\n * @since 1.0.0\n * @category models\n */\nexport const RecoverOutcome = Schema.Union([Recovered, Rejected])\n\n/**\n * Outcome of `recover`.\n *\n * @since 1.0.0\n * @category models\n */\nexport type RecoverOutcome = typeof RecoverOutcome.Type\n\n/**\n * Failure raised by a consensus strategy.\n *\n * `fence_lost` is `guard`'s refusal: the supplied owner no longer holds the\n * run, so the fenced write it joins must not commit. `persistence_failed`\n * wraps a strategy's storage failure; the original cause is preserved so the\n * durable writer's retry classification still sees a transient conflict\n * through the wrapper.\n *\n * @since 1.0.0\n * @category errors\n */\nexport class ConsensusError extends Schema.TaggedError<ConsensusError>()(\"@smthrs/journal/ConsensusError\", {\n code: Schema.Literals([\"fence_lost\", \"persistence_failed\"]),\n message: Schema.String,\n cause: Schema.optional(Schema.Unknown)\n}) {}\n\n/**\n * Consensus operations.\n *\n * The rules being arbitrated are strategy-independent; a strategy\n * chooses where the lease lives and how the fence is checked at commit time.\n * The claim lifecycle is two-phase, exactly as `RunStore`'s always was:\n * `claim` (or `steal`, its evidence-gated form) reserves the run and returns\n * the grant timestamp, `activate` presents that grant to take ownership, and\n * `release` clears whichever of the caller's claim or ownership still stands.\n * `recover` clears a *third party's* stale claim after its claimant was proven\n * dead — the relocated `recoverClaim` mechanic. `guard` is the commit-time\n * admission check (R3) `Journal.emitDurable`, `checkpoint`, and `compact`\n * join: it succeeds while the owner holds the run and fails with `fence_lost`\n * otherwise.\n *\n * Heartbeats and lease state are the strategy's private evidence and never\n * enter the journal.\n *\n * @since 1.0.0\n * @category models\n */\nexport interface Service {\n readonly claim: (\n runId: string,\n claimant: OwnerId,\n nowMs: number\n ) => Effect.Effect<ClaimOutcome, ConsensusError>\n readonly activate: (\n runId: string,\n owner: OwnerId,\n grantedAtMs: number,\n nowMs: number\n ) => Effect.Effect<ActivateOutcome, ConsensusError>\n readonly heartbeat: (\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) => Effect.Effect<HeartbeatOutcome, ConsensusError>\n /** Renews only while this owner holds the lease and no takeover claim is pending. */\n readonly reconfirm: (\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) => Effect.Effect<HeartbeatOutcome, ConsensusError>\n readonly release: (runId: string, owner: OwnerId) => Effect.Effect<void, ConsensusError>\n readonly steal: (\n runId: string,\n claimant: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) => Effect.Effect<ClaimOutcome, ConsensusError>\n readonly recover: (\n runId: string,\n staleClaimant: OwnerId,\n grantedAtMs: number,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) => Effect.Effect<RecoverOutcome, ConsensusError>\n readonly guard: (runId: string, owner: OwnerId) => Effect.Effect<void, ConsensusError>\n}\n\n/**\n * Context service for the injected consensus strategy.\n *\n * @since 1.0.0\n * @category services\n */\nexport class Consensus extends Context.Service<Consensus, Service>()(\"@smthrs/journal/Consensus\") {}\n\n/**\n * Constructs a consensus service from an implementation.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const make = (implementation: Service): Service => Consensus.of(implementation)\n\nconst rejected = (reason: RejectionReason): Rejected => ({ _tag: \"Rejected\", reason })\nconst claimed = (grantedAtMs: number): Claimed => ({ _tag: \"Claimed\", grantedAtMs })\nconst lost: Lost = { _tag: \"Lost\" }\nconst activated: Activated = { _tag: \"Activated\" }\nconst recovered: Recovered = { _tag: \"Recovered\" }\n\n/**\n * The refusal `guard` raises for an owner that no longer holds the run.\n *\n * @since 1.0.0\n * @category errors\n */\nexport const fenceLost = (runId: string, owner: OwnerId): ConsensusError =>\n new ConsensusError({\n code: \"fence_lost\",\n message: `run ${runId} is no longer owned by ${owner.hostId}:${owner.pid}:${owner.nonce}`\n })\n\n/**\n * Constructs a strategy that arbitrates nothing: claims are refused, leases\n * are never held, and every fenced write is rejected.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const makeNoop = (overrides: Partial<Service> = {}): Service =>\n Consensus.of({\n claim: Effect.fn(\"Consensus.claim\")(() => Effect.succeed(rejected(\"unavailable\"))),\n activate: Effect.fn(\"Consensus.activate\")(() => Effect.succeed(lost)),\n heartbeat: Effect.fn(\"Consensus.heartbeat\")(() => Effect.succeed(lost)),\n reconfirm: Effect.fn(\"Consensus.reconfirm\")(() => Effect.succeed(lost)),\n release: Effect.fn(\"Consensus.release\")(() => Effect.void),\n steal: Effect.fn(\"Consensus.steal\")(() => Effect.succeed(rejected(\"unavailable\"))),\n recover: Effect.fn(\"Consensus.recover\")(() => Effect.succeed(rejected(\"unavailable\"))),\n guard: Effect.fn(\"Consensus.guard\")((runId: string, owner: OwnerId) => Effect.fail(fenceLost(runId, owner))),\n ...overrides\n })\n\n/**\n * Provides the refusing strategy stub.\n *\n * @since 1.0.0\n * @category layers\n */\nexport const layerNoop = (overrides: Partial<Service> = {}): Layer.Layer<Consensus> =>\n Layer.succeed(Consensus)(makeNoop(overrides))\n\ninterface LocalLease {\n owner: { readonly id: OwnerId; readonly grantedAtMs: number; heartbeatAtMs: number } | undefined\n claim: { readonly id: OwnerId; readonly claimedAtMs: number } | undefined\n}\n\n/**\n * Constructs the in-memory single-process strategy.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const makeLocal: Effect.Effect<Service> = Effect.sync(() => {\n const leases = new Map<string, LocalLease>()\n\n const leaseOf = (runId: string): LocalLease => {\n const existing = leases.get(runId)\n if (existing !== undefined) return existing\n const fresh: LocalLease = { owner: undefined, claim: undefined }\n leases.set(runId, fresh)\n return fresh\n }\n\n const prune = (runId: string, lease: LocalLease): void => {\n if (lease.owner === undefined && lease.claim === undefined) {\n leases.delete(runId)\n }\n }\n\n return Consensus.of({\n claim: Effect.fn(\"Consensus.claim\")((runId: string, claimant: OwnerId, nowMs: number) =>\n Effect.sync(() => {\n const lease = leaseOf(runId)\n if (lease.claim !== undefined) return rejected(\"already_claimed\")\n if (lease.owner !== undefined) return rejected(\"owner_held\")\n lease.claim = { id: claimant, claimedAtMs: nowMs }\n return claimed(nowMs)\n })\n ),\n activate: Effect.fn(\"Consensus.activate\")((\n runId: string,\n owner: OwnerId,\n grantedAtMs: number,\n nowMs: number\n ) =>\n Effect.sync(() => {\n const lease = leaseOf(runId)\n if (\n lease.claim === undefined ||\n !sameOwner(lease.claim.id, owner) ||\n lease.claim.claimedAtMs !== grantedAtMs\n ) {\n prune(runId, lease)\n return lost\n }\n lease.claim = undefined\n lease.owner = { id: owner, grantedAtMs, heartbeatAtMs: nowMs }\n return activated\n })\n ),\n heartbeat: Effect.fn(\"Consensus.heartbeat\")((runId: string, owner: OwnerId, nowMs: number) =>\n Effect.sync(() => {\n const lease = leases.get(runId)\n if (lease?.owner === undefined || !sameOwner(lease.owner.id, owner)) return lost\n lease.owner.heartbeatAtMs = Math.max(lease.owner.heartbeatAtMs, nowMs)\n return ({ _tag: \"Renewed\", heartbeatAtMs: lease.owner.heartbeatAtMs } satisfies Renewed)\n })\n ),\n reconfirm: Effect.fn(\"Consensus.reconfirm\")((runId: string, owner: OwnerId, nowMs: number) =>\n Effect.sync(() => {\n const lease = leases.get(runId)\n if (lease?.owner === undefined || lease.claim !== undefined || !sameOwner(lease.owner.id, owner)) return lost\n lease.owner.heartbeatAtMs = Math.max(lease.owner.heartbeatAtMs, nowMs)\n return ({ _tag: \"Renewed\", heartbeatAtMs: lease.owner.heartbeatAtMs } satisfies Renewed)\n })\n ),\n release: Effect.fn(\"Consensus.release\")((runId: string, owner: OwnerId) =>\n Effect.sync(() => {\n const lease = leases.get(runId)\n if (lease === undefined) return\n if (lease.claim !== undefined && sameOwner(lease.claim.id, owner)) {\n lease.claim = undefined\n }\n if (lease.owner !== undefined && sameOwner(lease.owner.id, owner)) {\n lease.owner = undefined\n }\n prune(runId, lease)\n })\n ),\n steal: Effect.fn(\"Consensus.steal\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.sync(() => {\n if (!matchesEvidence(evidence.expectedOwner, claimant, nowMs, evidence)) {\n return rejected(\"evidence_invalid\")\n }\n const lease = leases.get(runId)\n if (lease?.claim !== undefined) return rejected(\"already_claimed\")\n if (lease?.owner === undefined || !sameOwner(lease.owner.id, evidence.expectedOwner)) {\n return rejected(\"evidence_invalid\")\n }\n if (lease.owner.heartbeatAtMs >= nowMs - staleAfterMs) return rejected(\"owner_live\")\n lease.claim = { id: claimant, claimedAtMs: nowMs }\n return claimed(nowMs)\n })\n ),\n recover: Effect.fn(\"Consensus.recover\")((\n runId: string,\n staleClaimant: OwnerId,\n grantedAtMs: number,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.sync(() => {\n if (!matchesEvidence(staleClaimant, observer, nowMs, evidence)) return rejected(\"evidence_invalid\")\n const lease = leases.get(runId)\n if (\n lease?.claim === undefined ||\n !sameOwner(lease.claim.id, staleClaimant) ||\n lease.claim.claimedAtMs !== grantedAtMs\n ) {\n return rejected(\"claim_changed\")\n }\n if (lease.claim.claimedAtMs >= nowMs - staleAfterMs) return rejected(\"claim_fresh\")\n lease.claim = undefined\n prune(runId, lease)\n return recovered\n })\n ),\n guard: Effect.fn(\"Consensus.guard\")((runId: string, owner: OwnerId) =>\n Effect.suspend(() => {\n const lease = leases.get(runId)\n return lease?.owner !== undefined && sameOwner(lease.owner.id, owner)\n ? Effect.void\n : Effect.fail(fenceLost(runId, owner))\n })\n )\n })\n})\n\n/**\n * Provides the in-memory single-process strategy.\n *\n * The lease lives in process memory, so R3 is exact within the process — the\n * strategy's transitions and the serialized append transactions interleave\n * atomically on one event loop — and browser support needs nothing beyond\n * this layer plus the browser-safe journal. State is per layer build: provide\n * one instance to every service that must agree on it, which is what\n * `SqlJournal.layerWith` and `RunStore.layerWith` exist for.\n *\n * The lease is not part of any database transaction: a transition made inside\n * a `Journal.transact` that later rolls back stays applied in memory, so the\n * lease and a rolled-back run row can disagree until the next transition.\n * `SqlConsensus` keeps the two in one transaction; prefer it wherever a\n * database is present.\n *\n * @since 1.0.0\n * @category layers\n */\nexport const layerLocal: Layer.Layer<Consensus> = Layer.effect(Consensus, makeLocal)\n"]}
|
|
1
|
+
{"version":3,"file":"Consensus.js","sourceRoot":"","sources":["../../src/Consensus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAsB,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;AAEvE;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAsB,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;AAE1E;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAsB,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAsB,QAAQ,CAAC,MAAM,CACvE,QAAQ,CAAC,QAAQ,CAAC,mBAAmB,CAAC;IACpC,QAAQ,CAAC,QAAQ,CAAC,sBAAsB,CAAC;IACzC,QAAQ,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CACvC,CAAA;AAED,MAAM,YAAY,GAAG,QAAQ,CAAC,QAAQ,CAAC,mBAAmB,CAAC,CAAA;AAE3D;;;;;;;;;;;;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QAClB,CAAC,CAAC,CACH;QACD,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC,iBAAiB,CAAC,CAAC,CAAC,KAAa,EAAE,KAAc,EAAE,EAAE,CACpE,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE;YAClB,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;YAC/B,OAAO,KAAK,EAAE,KAAK,KAAK,SAAS,IAAI,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC;gBACnE,CAAC,CAAC,MAAM,CAAC,IAAI;gBACb,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;QAC1C,CAAC,CAAC,CACH;KACF,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,UAAU,GAA2B,KAAK,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA","sourcesContent":["/**\n * The injectable consensus strategy arbitrating the journal's ownership rules.\n *\n * The journal owns the consensus rules — single writer per run, fenced and\n * unfenced appends, commit-time admission, the generation fence, and steal\n * only on staleness plus liveness evidence, or on a same-host probe that\n * proves the owner's process gone — and this service is how a deployment chooses *how* those rules are\n * arbitrated: where the lease lives, how liveness is probed, and how the\n * fence is checked at commit time. `Consensus` lives here beside `OwnerId`\n * for the same reason `OwnerId` does: the journal is what it fences.\n * `@smthrs/run-store`'s `Ownership` keeps its public API and rules but\n * delegates arbitration to this strategy.\n *\n * Every operation that reasons about staleness takes the caller's clock\n * reading (`nowMs`) rather than reading a clock itself: staleness (R5) and\n * the monotonic lease stamp are defined against the arbitrating caller's\n * reading, exactly as `RunStore`'s compare-and-swap operations always were,\n * so the recorded lease never differs from the outcome the caller observed.\n *\n * Governing design: `packages/smithers/flows/journal/docs/concepts/owner-fence.md`.\n * Rule constants and their provenance:\n * `packages/smithers/flows/run-store/docs/concepts/leases.md`.\n *\n * Prior art: Effect `EventJournal` (`reference/effect` `unstable/eventlog`)\n * resolves multi-writer by merge with conflict surfacing; driving a run is not\n * mergeable, so this surface deviates to exclusive-writer admission. The\n * fencing and generation semantics follow Temporal's shard `rangeID`\n * (`reference/temporal/service/history/shard/context_impl.go`).\n *\n * @since 1.0.0\n */\n\nimport * as Context from \"effect/Context\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport * as Schema from \"effect/Schema\"\nimport { OwnerId } from \"./OwnerId.ts\"\n\n/**\n * How often the supervision loop pulses to renew the owner's lease.\n *\n * @since 1.0.0\n * @category constants\n */\nexport const heartbeatInterval: Duration.Duration = Duration.seconds(1)\n\n/**\n * How old a persisted heartbeat must be before a peer may steal the run.\n * Every strategy judges R5 staleness against this one definition.\n *\n * @since 1.0.0\n * @category constants\n */\nexport const heartbeatStaleAfter: Duration.Duration = Duration.seconds(30)\n\n/**\n * How far the owner's wall clock may run behind a peer's before the lease\n * reasoning stops holding.\n *\n * The owner stamps its heartbeat from *its* clock and a would-be stealer\n * compares that stamp against *its own*, so the two hosts' clock offset is\n * subtracted directly from the owner's real safety margin. This constant names\n * that allowance instead of leaving it implicit in a \"two ticks\" arithmetic\n * accident, and it is a budgeted allowance, not a guarantee: see\n * {@link heartbeatWriteTolerance} for what happens once it is exceeded.\n *\n * @since 1.0.0\n * @category constants\n */\nexport const heartbeatSkewAllowance: Duration.Duration = Duration.seconds(10)\n\n/**\n * How long heartbeat writes may go unconfirmed before ownership must be reconfirmed.\n *\n * A peer judges staleness by the persisted heartbeat and may steal the run the\n * instant it is {@link heartbeatStaleAfter} old, so an owner that tolerated\n * write failures for exactly that long would still be executing side effects\n * when the steal is admitted. The budget is therefore\n * {@link heartbeatStaleAfter} minus {@link heartbeatSkewAllowance} (the peer's\n * clock may already read that much later than the owner's) minus one\n * {@link heartbeatInterval} (reserved for one bounded compare-and-swap\n * reconfirmation before the owner interrupts).\n *\n * This bounds, but does not eliminate, overlap. Beyond\n * {@link heartbeatSkewAllowance} of clock offset a peer can be admitted while\n * the old owner is still running. *Durable* writes stay safe regardless — they\n * are fenced by the strategy's commit-time admission, so the displaced owner's\n * writes fail rather than corrupt. Non-durable external side effects (an HTTP\n * call, a spawned process) can genuinely overlap. That is inherent to any\n * wall-clock lease and is stated here rather than asserted away; a caller that\n * cannot tolerate any overlap needs an external fencing token at the side\n * effect itself, not a larger timeout.\n *\n * @since 1.0.0\n * @category constants\n */\nexport const heartbeatWriteTolerance: Duration.Duration = Duration.millis(\n Duration.toMillis(heartbeatStaleAfter) -\n Duration.toMillis(heartbeatSkewAllowance) -\n Duration.toMillis(heartbeatInterval)\n)\n\nconst staleAfterMs = Duration.toMillis(heartbeatStaleAfter)\n\n/**\n * Evidence that the owner in an exact run snapshot is no longer live.\n *\n * Defined here because R5 — steal requires staleness plus liveness evidence —\n * is a consensus rule every strategy validates. `@smthrs/run-store`'s\n * `Ownership` re-exports it alongside the probes that mint it.\n *\n * Two of the three kinds are collected outside the strategy:\n * `same-host-pid-dead` is a local process probe, and\n * `cross-host-unreachable-stale` is a reachability judgement the deployment\n * makes. `lease-expired` is different — it asserts only that the persisted\n * heartbeat is older than the staleness cutoff, which is the one claim a\n * strategy can check for itself, and `steal` checks it: the takeover refuses\n * any lease whose heartbeat is still inside the window. It is therefore\n * accepted from a claimant on any host, while the other two stay bound to the\n * host relation that makes them meaningful. `same-host-pid-dead` alone also\n * admits a steal inside the window; see {@link provesDeath}. The observation must carry the\n * exact instant supplied to the consuming operation: `checkedAtMs` must equal\n * that operation's `nowMs`. A caller that probes at T and calls at T+1 is\n * refused and must build fresh evidence.\n *\n * @since 1.0.0\n * @category models\n */\nexport const LivenessEvidence = Schema.Struct({\n expectedOwner: OwnerId,\n checkedAtMs: Schema.Number,\n kind: Schema.Literals([\"same-host-pid-dead\", \"cross-host-unreachable-stale\", \"lease-expired\"])\n})\n\n/**\n * Evidence that the owner in an exact run snapshot is no longer live.\n *\n * @since 1.0.0\n * @category models\n */\nexport type LivenessEvidence = typeof LivenessEvidence.Type\n\n/**\n * Structural equality on the process identity tuple.\n *\n * @since 1.0.0\n * @category predicates\n */\nexport const sameOwner = (left: OwnerId, right: OwnerId): boolean =>\n left.hostId === right.hostId && left.pid === right.pid && left.nonce === right.nonce\n\n/**\n * Validates liveness evidence against the owner it claims to disprove.\n *\n * The evidence must name the expected owner, carry the observer's current\n * clock reading, and respect the host rule: a `same-host-pid-dead` probe is\n * only meaningful from the same host, a `cross-host-unreachable-stale`\n * observation only from a different one, and `lease-expired` is host-neutral\n * because the strategy verifies the cutoff it names for itself.\n *\n * @since 1.0.0\n * @category predicates\n */\nexport const matchesEvidence = (\n expectedOwner: OwnerId,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n): boolean => {\n if (!sameOwner(expectedOwner, evidence.expectedOwner) || evidence.checkedAtMs !== nowMs) return false\n switch (evidence.kind) {\n // A pid means nothing outside the host that owns the process namespace.\n case \"same-host-pid-dead\":\n return expectedOwner.hostId === observer.hostId\n // Unreachability is what a peer host observes; on the owner's own host it\n // would be an unprobed guess dressed as evidence.\n case \"cross-host-unreachable-stale\":\n return expectedOwner.hostId !== observer.hostId\n // The lease is host-neutral, and the strategy verifies the same\n // caller-clock cutoff named by this evidence.\n case \"lease-expired\":\n return true\n }\n}\n\n/**\n * Whether evidence proves the owner gone, so a steal need not wait for its\n * lease to go stale.\n *\n * The lease stands in for a question a peer cannot ask directly: is the owner\n * still running? A `same-host-pid-dead` probe asks it. The claimant read its\n * own host's process table and found no process behind the owner's pid, and a\n * process that does not exist renews no lease and writes nothing, so waiting\n * out `heartbeatStaleAfter` adds no safety. A run whose owner was killed with\n * `SIGKILL` is therefore reclaimed by the next process on the same host at\n * once.\n *\n * The evidence must still pass {@link matchesEvidence}, which binds the kind\n * to the same host. One more case is excluded here: an owner recorded with the\n * claimant's own pid names the claimant's process, which is running, so a\n * probe of that pid cannot report it gone. Such evidence keeps the staleness\n * cutoff.\n *\n * The other kinds never waive the cutoff. `cross-host-unreachable-stale` is a\n * judgement about a host this process cannot inspect, and `lease-expired` is\n * the cutoff itself.\n *\n * @since 1.0.0\n * @category predicates\n */\nexport const provesDeath = (evidence: LivenessEvidence, claimant: OwnerId): boolean =>\n evidence.kind === \"same-host-pid-dead\" && evidence.expectedOwner.pid !== claimant.pid\n\n/**\n * Why a strategy refused a claim, steal, or recovery.\n *\n * @since 1.0.0\n * @category models\n */\nexport const RejectionReason = Schema.Literals([\n \"already_claimed\",\n \"owner_held\",\n \"owner_live\",\n \"evidence_invalid\",\n \"claim_fresh\",\n \"claim_changed\",\n \"unavailable\"\n])\n\n/**\n * Why a strategy refused a claim, steal, or recovery.\n *\n * @since 1.0.0\n * @category models\n */\nexport type RejectionReason = typeof RejectionReason.Type\n\n/**\n * A granted claim, carrying the grant timestamp the later activation must\n * present (R4 — the generation fence).\n *\n * @since 1.0.0\n * @category models\n */\nexport const Claimed = Schema.TaggedStruct(\"Claimed\", {\n grantedAtMs: Schema.Number\n})\n\n/**\n * A granted claim.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Claimed = typeof Claimed.Type\n\n/**\n * A refused claim, steal, or recovery, with the strategy's typed reason.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Rejected = Schema.TaggedStruct(\"Rejected\", {\n reason: RejectionReason\n})\n\n/**\n * A refused claim, steal, or recovery.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Rejected = typeof Rejected.Type\n\n/**\n * Outcome of `claim` and `steal`.\n *\n * @since 1.0.0\n * @category models\n */\nexport const ClaimOutcome = Schema.Union([Claimed, Rejected])\n\n/**\n * Outcome of `claim` and `steal`.\n *\n * @since 1.0.0\n * @category models\n */\nexport type ClaimOutcome = typeof ClaimOutcome.Type\n\n/**\n * A claim promoted to ownership.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Activated = Schema.TaggedStruct(\"Activated\", {})\n\n/**\n * A claim promoted to ownership.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Activated = typeof Activated.Type\n\n/**\n * The claim or lease the caller presented no longer exists.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Lost = Schema.TaggedStruct(\"Lost\", {})\n\n/**\n * The claim or lease the caller presented no longer exists.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Lost = typeof Lost.Type\n\n/**\n * Outcome of `activate`.\n *\n * @since 1.0.0\n * @category models\n */\nexport const ActivateOutcome = Schema.Union([Activated, Lost])\n\n/**\n * Outcome of `activate`.\n *\n * @since 1.0.0\n * @category models\n */\nexport type ActivateOutcome = typeof ActivateOutcome.Type\n\n/**\n * A renewed lease, carrying the monotonic heartbeat stamp the strategy\n * recorded — the maximum of the stored stamp and the caller's reading, so a\n * late pulse never moves the lease backwards.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Renewed = Schema.TaggedStruct(\"Renewed\", {\n heartbeatAtMs: Schema.Number\n})\n\n/**\n * A renewed lease.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Renewed = typeof Renewed.Type\n\n/**\n * Outcome of `heartbeat` and `reconfirm`.\n *\n * @since 1.0.0\n * @category models\n */\nexport const HeartbeatOutcome = Schema.Union([Renewed, Lost])\n\n/**\n * Outcome of `heartbeat` and `reconfirm`.\n *\n * @since 1.0.0\n * @category models\n */\nexport type HeartbeatOutcome = typeof HeartbeatOutcome.Type\n\n/**\n * A stale claim cleared after its claimant was proven dead.\n *\n * @since 1.0.0\n * @category models\n */\nexport const Recovered = Schema.TaggedStruct(\"Recovered\", {})\n\n/**\n * A stale claim cleared after its claimant was proven dead.\n *\n * @since 1.0.0\n * @category models\n */\nexport type Recovered = typeof Recovered.Type\n\n/**\n * Outcome of `recover`.\n *\n * @since 1.0.0\n * @category models\n */\nexport const RecoverOutcome = Schema.Union([Recovered, Rejected])\n\n/**\n * Outcome of `recover`.\n *\n * @since 1.0.0\n * @category models\n */\nexport type RecoverOutcome = typeof RecoverOutcome.Type\n\n/**\n * Failure raised by a consensus strategy.\n *\n * `fence_lost` is `guard`'s refusal: the supplied owner no longer holds the\n * run, so the fenced write it joins must not commit. `persistence_failed`\n * wraps a strategy's storage failure; the original cause is preserved so the\n * durable writer's retry classification still sees a transient conflict\n * through the wrapper.\n *\n * @since 1.0.0\n * @category errors\n */\nexport class ConsensusError extends Schema.TaggedError<ConsensusError>()(\"@smthrs/journal/ConsensusError\", {\n code: Schema.Literals([\"fence_lost\", \"persistence_failed\"]),\n message: Schema.String,\n cause: Schema.optional(Schema.Unknown)\n}) {}\n\n/**\n * Consensus operations.\n *\n * The rules being arbitrated are strategy-independent; a strategy\n * chooses where the lease lives and how the fence is checked at commit time.\n * The claim lifecycle is two-phase, exactly as `RunStore`'s always was:\n * `claim` (or `steal`, its evidence-gated form) reserves the run and returns\n * the grant timestamp, `activate` presents that grant to take ownership, and\n * `release` clears whichever of the caller's claim or ownership still stands.\n * `recover` clears a *third party's* stale claim after its claimant was proven\n * dead — the relocated `recoverClaim` mechanic. `guard` is the commit-time\n * admission check (R3) `Journal.emitDurable`, `checkpoint`, and `compact`\n * join: it succeeds while the owner holds the run and fails with `fence_lost`\n * otherwise.\n *\n * Heartbeats and lease state are the strategy's private evidence and never\n * enter the journal.\n *\n * @since 1.0.0\n * @category models\n */\nexport interface Service {\n readonly claim: (\n runId: string,\n claimant: OwnerId,\n nowMs: number\n ) => Effect.Effect<ClaimOutcome, ConsensusError>\n readonly activate: (\n runId: string,\n owner: OwnerId,\n grantedAtMs: number,\n nowMs: number\n ) => Effect.Effect<ActivateOutcome, ConsensusError>\n readonly heartbeat: (\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) => Effect.Effect<HeartbeatOutcome, ConsensusError>\n /** Renews only while this owner holds the lease and no takeover claim is pending. */\n readonly reconfirm: (\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) => Effect.Effect<HeartbeatOutcome, ConsensusError>\n readonly release: (runId: string, owner: OwnerId) => Effect.Effect<void, ConsensusError>\n readonly steal: (\n runId: string,\n claimant: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) => Effect.Effect<ClaimOutcome, ConsensusError>\n readonly recover: (\n runId: string,\n staleClaimant: OwnerId,\n grantedAtMs: number,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) => Effect.Effect<RecoverOutcome, ConsensusError>\n readonly guard: (runId: string, owner: OwnerId) => Effect.Effect<void, ConsensusError>\n}\n\n/**\n * Context service for the injected consensus strategy.\n *\n * @since 1.0.0\n * @category services\n */\nexport class Consensus extends Context.Service<Consensus, Service>()(\"@smthrs/journal/Consensus\") {}\n\n/**\n * Constructs a consensus service from an implementation.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const make = (implementation: Service): Service => Consensus.of(implementation)\n\nconst rejected = (reason: RejectionReason): Rejected => ({ _tag: \"Rejected\", reason })\nconst claimed = (grantedAtMs: number): Claimed => ({ _tag: \"Claimed\", grantedAtMs })\nconst lost: Lost = { _tag: \"Lost\" }\nconst activated: Activated = { _tag: \"Activated\" }\nconst recovered: Recovered = { _tag: \"Recovered\" }\n\n/**\n * The refusal `guard` raises for an owner that no longer holds the run.\n *\n * @since 1.0.0\n * @category errors\n */\nexport const fenceLost = (runId: string, owner: OwnerId): ConsensusError =>\n new ConsensusError({\n code: \"fence_lost\",\n message: `run ${runId} is no longer owned by ${owner.hostId}:${owner.pid}:${owner.nonce}`\n })\n\n/**\n * Constructs a strategy that arbitrates nothing: claims are refused, leases\n * are never held, and every fenced write is rejected.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const makeNoop = (overrides: Partial<Service> = {}): Service =>\n Consensus.of({\n claim: Effect.fn(\"Consensus.claim\")(() => Effect.succeed(rejected(\"unavailable\"))),\n activate: Effect.fn(\"Consensus.activate\")(() => Effect.succeed(lost)),\n heartbeat: Effect.fn(\"Consensus.heartbeat\")(() => Effect.succeed(lost)),\n reconfirm: Effect.fn(\"Consensus.reconfirm\")(() => Effect.succeed(lost)),\n release: Effect.fn(\"Consensus.release\")(() => Effect.void),\n steal: Effect.fn(\"Consensus.steal\")(() => Effect.succeed(rejected(\"unavailable\"))),\n recover: Effect.fn(\"Consensus.recover\")(() => Effect.succeed(rejected(\"unavailable\"))),\n guard: Effect.fn(\"Consensus.guard\")((runId: string, owner: OwnerId) => Effect.fail(fenceLost(runId, owner))),\n ...overrides\n })\n\n/**\n * Provides the refusing strategy stub.\n *\n * @since 1.0.0\n * @category layers\n */\nexport const layerNoop = (overrides: Partial<Service> = {}): Layer.Layer<Consensus> =>\n Layer.succeed(Consensus)(makeNoop(overrides))\n\ninterface LocalLease {\n owner: { readonly id: OwnerId; readonly grantedAtMs: number; heartbeatAtMs: number } | undefined\n claim: { readonly id: OwnerId; readonly claimedAtMs: number } | undefined\n}\n\n/**\n * Constructs the in-memory single-process strategy.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const makeLocal: Effect.Effect<Service> = Effect.sync(() => {\n const leases = new Map<string, LocalLease>()\n\n const leaseOf = (runId: string): LocalLease => {\n const existing = leases.get(runId)\n if (existing !== undefined) return existing\n const fresh: LocalLease = { owner: undefined, claim: undefined }\n leases.set(runId, fresh)\n return fresh\n }\n\n const prune = (runId: string, lease: LocalLease): void => {\n if (lease.owner === undefined && lease.claim === undefined) {\n leases.delete(runId)\n }\n }\n\n return Consensus.of({\n claim: Effect.fn(\"Consensus.claim\")((runId: string, claimant: OwnerId, nowMs: number) =>\n Effect.sync(() => {\n const lease = leaseOf(runId)\n if (lease.claim !== undefined) return rejected(\"already_claimed\")\n if (lease.owner !== undefined) return rejected(\"owner_held\")\n lease.claim = { id: claimant, claimedAtMs: nowMs }\n return claimed(nowMs)\n })\n ),\n activate: Effect.fn(\"Consensus.activate\")((\n runId: string,\n owner: OwnerId,\n grantedAtMs: number,\n nowMs: number\n ) =>\n Effect.sync(() => {\n const lease = leaseOf(runId)\n if (\n lease.claim === undefined ||\n !sameOwner(lease.claim.id, owner) ||\n lease.claim.claimedAtMs !== grantedAtMs\n ) {\n prune(runId, lease)\n return lost\n }\n lease.claim = undefined\n lease.owner = { id: owner, grantedAtMs, heartbeatAtMs: nowMs }\n return activated\n })\n ),\n heartbeat: Effect.fn(\"Consensus.heartbeat\")((runId: string, owner: OwnerId, nowMs: number) =>\n Effect.sync(() => {\n const lease = leases.get(runId)\n if (lease?.owner === undefined || !sameOwner(lease.owner.id, owner)) return lost\n lease.owner.heartbeatAtMs = Math.max(lease.owner.heartbeatAtMs, nowMs)\n return ({ _tag: \"Renewed\", heartbeatAtMs: lease.owner.heartbeatAtMs } satisfies Renewed)\n })\n ),\n reconfirm: Effect.fn(\"Consensus.reconfirm\")((runId: string, owner: OwnerId, nowMs: number) =>\n Effect.sync(() => {\n const lease = leases.get(runId)\n if (lease?.owner === undefined || lease.claim !== undefined || !sameOwner(lease.owner.id, owner)) return lost\n lease.owner.heartbeatAtMs = Math.max(lease.owner.heartbeatAtMs, nowMs)\n return ({ _tag: \"Renewed\", heartbeatAtMs: lease.owner.heartbeatAtMs } satisfies Renewed)\n })\n ),\n release: Effect.fn(\"Consensus.release\")((runId: string, owner: OwnerId) =>\n Effect.sync(() => {\n const lease = leases.get(runId)\n if (lease === undefined) return\n if (lease.claim !== undefined && sameOwner(lease.claim.id, owner)) {\n lease.claim = undefined\n }\n if (lease.owner !== undefined && sameOwner(lease.owner.id, owner)) {\n lease.owner = undefined\n }\n prune(runId, lease)\n })\n ),\n steal: Effect.fn(\"Consensus.steal\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.sync(() => {\n if (!matchesEvidence(evidence.expectedOwner, claimant, nowMs, evidence)) {\n return rejected(\"evidence_invalid\")\n }\n const lease = leases.get(runId)\n if (lease?.claim !== undefined) return rejected(\"already_claimed\")\n if (lease?.owner === undefined || !sameOwner(lease.owner.id, evidence.expectedOwner)) {\n return rejected(\"evidence_invalid\")\n }\n if (!provesDeath(evidence, claimant) && lease.owner.heartbeatAtMs >= nowMs - staleAfterMs) {\n return rejected(\"owner_live\")\n }\n lease.claim = { id: claimant, claimedAtMs: nowMs }\n return claimed(nowMs)\n })\n ),\n recover: Effect.fn(\"Consensus.recover\")((\n runId: string,\n staleClaimant: OwnerId,\n grantedAtMs: number,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.sync(() => {\n if (!matchesEvidence(staleClaimant, observer, nowMs, evidence)) return rejected(\"evidence_invalid\")\n const lease = leases.get(runId)\n if (\n lease?.claim === undefined ||\n !sameOwner(lease.claim.id, staleClaimant) ||\n lease.claim.claimedAtMs !== grantedAtMs\n ) {\n return rejected(\"claim_changed\")\n }\n if (lease.claim.claimedAtMs >= nowMs - staleAfterMs) return rejected(\"claim_fresh\")\n lease.claim = undefined\n prune(runId, lease)\n return recovered\n })\n ),\n guard: Effect.fn(\"Consensus.guard\")((runId: string, owner: OwnerId) =>\n Effect.suspend(() => {\n const lease = leases.get(runId)\n return lease?.owner !== undefined && sameOwner(lease.owner.id, owner)\n ? Effect.void\n : Effect.fail(fenceLost(runId, owner))\n })\n )\n })\n})\n\n/**\n * Provides the in-memory single-process strategy.\n *\n * The lease lives in process memory, so R3 is exact within the process — the\n * strategy's transitions and the serialized append transactions interleave\n * atomically on one event loop — and browser support needs nothing beyond\n * this layer plus the browser-safe journal. State is per layer build: provide\n * one instance to every service that must agree on it, which is what\n * `SqlJournal.layerWith` and `RunStore.layerWith` exist for.\n *\n * The lease is not part of any database transaction: a transition made inside\n * a `Journal.transact` that later rolls back stays applied in memory, so the\n * lease and a rolled-back run row can disagree until the next transition.\n * `SqlConsensus` keeps the two in one transaction; prefer it wherever a\n * database is present.\n *\n * @since 1.0.0\n * @category layers\n */\nexport const layerLocal: Layer.Layer<Consensus> = Layer.effect(Consensus, makeLocal)\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Redaction.d.ts","sourceRoot":"","sources":["../../src/Redaction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;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AAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,aAAa,CAAC,MAAM,CAAC,CAAA;IACtD;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IACrC,0GAA0G;IAC1G,QAAQ,CAAC,IAAI,EAAE,MAAM,aAAa,CAAC,MAAM,CAAC,CAAA;IAC1C,iFAAiF;IACjF,QAAQ,CAAC,KAAK,EAAE,MAAM,aAAa,CAAC,MAAM,CAAC,CAAA;CAC5C;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,YAAY,MAAM,CAAA;AAe/B;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,QAAY,CAAA;AAEvC;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,4BAA4B,CAAA;AAiBpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,eAAO,MAAM,YAAY,GAAI,QAAO,aAAa,CAAC,IAAI,CAAmB,KAAG,YA6H3E,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,QAAO,QAA4B,CAAA"}
|
package/dist/esm/Redaction.js
CHANGED
|
@@ -661,7 +661,14 @@ export const defaultRules = [
|
|
|
661
661
|
// of `aaaaaaaa-` cost 11 seconds, on the path every journal write and every
|
|
662
662
|
// log line takes. A scheme longer than 30 characters is not one this net is
|
|
663
663
|
// for, and the cap makes each start position cost a constant.
|
|
664
|
-
|
|
664
|
+
//
|
|
665
|
+
// The username may be empty (`redis://:password@host`), and the password
|
|
666
|
+
// runs to the LAST `@` before the authority ends, as a URL parser reads
|
|
667
|
+
// it: RFC 3986 allows `:` in a password and a raw `@` is common in
|
|
668
|
+
// connection strings. Stopping at the first `:` or `@` left the tail of
|
|
669
|
+
// such a password in the row. Quotes and backslashes end the scan so a
|
|
670
|
+
// URL inside JSON text never swallows the next string.
|
|
671
|
+
pattern: /\b([a-z][a-z0-9+.-]{0,30}:\/\/[^\s:@/]*):(?!\[REDACTED\]@)[^\s/?#"'`\\]*@/gi,
|
|
665
672
|
replace: "$1:[REDACTED]@"
|
|
666
673
|
},
|
|
667
674
|
{
|
|
@@ -717,6 +724,23 @@ export const defaultRules = [
|
|
|
717
724
|
pattern: /\bAIza[0-9A-Za-z_-]{35,}(?![0-9A-Za-z_-])/g,
|
|
718
725
|
replace: "[REDACTED]"
|
|
719
726
|
},
|
|
727
|
+
{
|
|
728
|
+
id: "gitlab-token",
|
|
729
|
+
pattern: /\bglpat-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g,
|
|
730
|
+
replace: "[REDACTED]"
|
|
731
|
+
},
|
|
732
|
+
{
|
|
733
|
+
id: "npm-token",
|
|
734
|
+
pattern: /\bnpm_[A-Za-z0-9]{36}(?![A-Za-z0-9])/g,
|
|
735
|
+
replace: "[REDACTED]"
|
|
736
|
+
},
|
|
737
|
+
{
|
|
738
|
+
// The webhook path is the whole credential: anyone holding it can post
|
|
739
|
+
// to the channel. The host stays so a failed delivery is still readable.
|
|
740
|
+
id: "slack-webhook",
|
|
741
|
+
pattern: /(\bhooks\.slack\.com\/services\/)T[A-Z0-9]+\/B[A-Z0-9]+\/[A-Za-z0-9]+/g,
|
|
742
|
+
replace: "$1[REDACTED]"
|
|
743
|
+
},
|
|
720
744
|
{
|
|
721
745
|
id: "assignment",
|
|
722
746
|
// A credential name, `:`, `=` or `=>`, and its whole value: quoted at any
|
|
@@ -925,12 +949,15 @@ const tokenCounterKeys = new Set([
|
|
|
925
949
|
"totaltoken"
|
|
926
950
|
]);
|
|
927
951
|
/**
|
|
928
|
-
*
|
|
929
|
-
*
|
|
930
|
-
*
|
|
952
|
+
* Any name ending in a plural `tokens` holds a count too, the rule the textual
|
|
953
|
+
* pass applies in {@link namesCredential}: `Envelope.budget.tokens` is the
|
|
954
|
+
* run's token ceiling, and `usage.cacheWriteTokens`, `maxTokens`, and
|
|
955
|
+
* `contextWindowTokens` are accounting; redacting them erased every journaled
|
|
956
|
+
* budget and cache-write count (#3846). A bare singular `token` stays bearer
|
|
957
|
+
* material even when it is numeric.
|
|
931
958
|
*/
|
|
932
959
|
const isTokenCount = (key, value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0 &&
|
|
933
|
-
(tokenCounterKeys.has(canonicalKey(key)) ||
|
|
960
|
+
(tokenCounterKeys.has(canonicalKey(key)) || /tokens$/i.test(key));
|
|
934
961
|
/**
|
|
935
962
|
* Whether a field name names a credential.
|
|
936
963
|
*
|