UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

95 lines • 4.3 kB
import { frozenBlock } from "./compact-seeded-block.js"; import { CompactStartIdentity } from "./compact-start-identity.js"; import { CompactStateStorage } from "./compact-state-storage.js"; import { CompactTransitionError } from "./compact-transition.js"; function rowOf(messageId, message) { return { messageId, ...(message.root.ref ? { blocks: message.root.ref } : {}), ...(message.fields ? { fields: message.fields } : {}), ...(message.correlation ? { correlation: message.correlation } : {}), }; } /** Live messages and their persistent block roots; every publication runs inside one synchronous acceptance. */ export class CompactActiveMessages extends CompactStateStorage { constructor() { super(...arguments); this.messages = { header: undefined, ref: undefined }; this.active = new Map(); this.identity = new CompactStartIdentity(this.epoch); this.sequence = 0; this.residentTailBytes = 0; this.hotWitnessBytes = 0; } isActiveKey(key) { return this.active.has(key); } publish(address, block, fields) { const message = this.active.get(address.messageId) ?? { ...this.fresh(), epoch: block.epoch }; if (fields) message.fields = fields; this.put(message, address.contentIndex, block); this.publishRow(address.messageId, message); } /** An authoritative whole message: a fresh block root, so no index the message no longer names survives. */ install(messageId, parts, blocks) { const old = this.active.get(messageId); const message = { ...this.fresh(old?.order), ...parts }; for (const [index, block] of blocks) this.put(message, index, { ...block.body, seeded: block, epoch: parts.epoch }); this.publishRow(messageId, message); if (old) this.discard(old); } settle(messageId, persistence) { const message = this.active.get(messageId); this.settled.set(messageId, { ...(message ? rowOf(messageId, message) : { messageId }), persistence }); this.dropActive(messageId); } dropActive(messageId) { const message = this.active.get(messageId); if (!message) return; this.messages = this.messageRoots.set(this.messages, message.order, undefined); this.discard(message); this.active.delete(messageId); this.identity.release(messageId); } block(address) { const block = this.active.get(address.messageId)?.blocks.get(address.contentIndex); if (!block) throw new CompactTransitionError("unknown_block", `${address.messageId}#${address.contentIndex}`); return block; } fresh(order = String(this.sequence).padStart(16, "0")) { return { order, blocks: new Map(), root: { header: undefined, ref: undefined }, tailBytes: 0, witnessBytes: 0 }; } put(message, contentIndex, block) { const old = message.blocks.get(contentIndex); const tails = (block.tail.length + (block.seeded?.signature.tail.length ?? 0) - (old?.tail.length ?? 0) - (old?.seeded?.signature.tail.length ?? 0)) * 2; const witness = (block.witness.length - (old?.witness.length ?? 0)) * 2; message.root = this.blockRoots.set(message.root, String(contentIndex).padStart(16, "0"), frozenBlock(contentIndex, block)); message.blocks.set(contentIndex, block); message.tailBytes += tails; message.witnessBytes += witness; this.residentTailBytes += tails; this.hotWitnessBytes += witness; } publishRow(messageId, message) { this.messages = this.messageRoots.set(this.messages, message.order, rowOf(messageId, message)); // The only entry into `active`, so every start path voids cold work queued while the key was settled. if (!this.active.has(messageId)) this.cold.activate(messageId); this.active.set(messageId, message); } discard(message) { this.blockRoots.release(message.root); this.residentTailBytes -= message.tailBytes; this.hotWitnessBytes -= message.witnessBytes; } } //# sourceMappingURL=compact-active-messages.js.map