|
1 | 1 | import { randomInt } from 'node:crypto'; |
| 2 | +import type { SettleObservation } from '@agent-device/contracts/interaction'; |
2 | 3 | import type { SnapshotState } from '@agent-device/kernel/snapshot'; |
3 | 4 | import { activatePartialRefFrame, refFrameEpoch, refFrameState } from './ref-frame.ts'; |
4 | 5 | import type { SessionState } from './types.ts'; |
@@ -120,6 +121,40 @@ export function markSessionPartialRefsIssued(session: SessionState, refs: Iterab |
120 | 121 | activatePartialRefFrame(session, scope); |
121 | 122 | } |
122 | 123 |
|
| 124 | +/** |
| 125 | + * #1101 `--settle`: a settle observation carrying a diff hands the client refs |
| 126 | + * minted from the freshly stored settled tree (added lines carry them), which |
| 127 | + * makes the response ref-issuing like snapshot/find: it activates a PARTIAL |
| 128 | + * frame (ADR 0014) authorizing exactly those bodies, and returns the stored |
| 129 | + * tree's generation to ride inside the settle payload for MCP per-ref pinning. |
| 130 | + * Without a diff — never captured, or a sparse-quality capture that was not |
| 131 | + * stored — nothing was issued and the frame is left as the action's own |
| 132 | + * side-effect seam expired it. |
| 133 | + * |
| 134 | + * Every route that can attach a settle observation goes through here (the |
| 135 | + * touch commands and the generic scroll/back route, #1638), so the issuance |
| 136 | + * rule has one implementation beside the partial-frame primitive it wraps. |
| 137 | + */ |
| 138 | +export function issueSettleRefs( |
| 139 | + session: SessionState, |
| 140 | + settle: SettleObservation | undefined, |
| 141 | +): number | undefined { |
| 142 | + if (!settle?.diff) return undefined; |
| 143 | + markSessionPartialRefsIssued(session, collectSettleIssuedRefBodies(settle)); |
| 144 | + return session.snapshotGeneration; |
| 145 | +} |
| 146 | + |
| 147 | +/** The reusable refs a settled diff exposed: added diff lines, `refs`, `tail`. */ |
| 148 | +function collectSettleIssuedRefBodies(settle: SettleObservation): string[] { |
| 149 | + const bodies: string[] = []; |
| 150 | + for (const line of settle.diff?.lines ?? []) { |
| 151 | + if (line.ref) bodies.push(line.ref); |
| 152 | + } |
| 153 | + for (const entry of settle.refs ?? []) bodies.push(entry.ref); |
| 154 | + for (const entry of settle.tail ?? []) bodies.push(entry.ref); |
| 155 | + return bodies; |
| 156 | +} |
| 157 | + |
123 | 158 | /** |
124 | 159 | * Warning for a ref pinned to a generation (`@e12~s3`) whose epoch no longer |
125 | 160 | * matches the session's current ref-frame epoch (`refFrameEpoch`) — NOT the |
|
0 commit comments