@@ -44,7 +44,16 @@ export type ObserverClaim = |
44 | 44 |
|
45 | 45 | export interface Scope extends AttachmentContract.Scope { |
46 | 46 | readonly sessionID: SessionID |
47 | | - readonly own: (messageID: MessageID) => Effect.Effect<void> |
| 47 | + /** |
| 48 | + * Claims a message for this scope, invalidating the turn's prior evidence. |
| 49 | + * |
| 50 | + * Returns whether ownership was taken. `false` means the scope had already published its |
| 51 | + * resolution and can no longer accept one — the admission boundary converts that into the typed |
| 52 | + * `SessionScopeOwnRefused`, while non-admission callers ignore it and keep the historical no-op. |
| 53 | + * A degraded or cancelled scope that has NOT resolved still throws, which is CP-031's existing |
| 54 | + * recoverable admission failure and is deliberately unchanged. |
| 55 | + */ |
| 56 | + readonly own: (messageID: MessageID) => Effect.Effect<boolean> |
48 | 57 | readonly owns: (messageID: MessageID) => boolean |
49 | 58 | readonly reserve: (jobID: SessionID) => Effect.Effect<Reservation> |
50 | 59 | readonly reject: (reservation: Reservation) => Effect.Effect<void> |
@@ -75,6 +84,20 @@ export interface TaskInterface { |
75 | 84 | * is necessarily an effectful read. |
76 | 85 | */ |
77 | 86 | readonly locate: (sessionID: SessionID) => Effect.Effect<Scope | undefined> |
| 87 | + /** |
| 88 | + * CP-032 R-08: the BORROW lookup, deliberately distinct from `locate`. |
| 89 | + * |
| 90 | + * A scope that has published its resolution stays registered until `closeNow` unregisters it, and |
| 91 | + * in that window it is unusable: `reserve` throws, `own` returns silently on the `closed` guard |
| 92 | + * (so no typed refusal is minted), and `result` hands back the EARLIER resolution and fallback. A |
| 93 | + * supplement that borrowed one would therefore file the earlier position, which the filing guard |
| 94 | + * already holds, and lose its own answer with no note and no error. |
| 95 | + * |
| 96 | + * `locate` keeps returning it on purpose. `tool/task.ts` reconciles the carried parent scope |
| 97 | + * against the registry by object identity and treats disagreement as a coordination fault, so |
| 98 | + * hiding a resolved scope there would fail delegated calls rather than protect them. |
| 99 | + */ |
| 100 | + readonly locateBorrowable: (sessionID: SessionID) => Effect.Effect<Scope | undefined> |
78 | 101 | readonly claim: (sessionID: SessionID) => Effect.Effect<Claim> |
79 | 102 | readonly settleClaim: (claim: Claim, active: boolean) => Effect.Effect<void> |
80 | 103 | readonly awaitClaim: (claim: Claim) => Effect.Effect<boolean> |
@@ -164,6 +187,16 @@ type RegistryState = { |
164 | 187 | { |
165 | 188 | readonly scope: Scope |
166 | 189 | readonly captureFence: (ref: Ports.ParticipantFenceRef) => boolean |
| 190 | + /** |
| 191 | + * CP-032 R-08: this scope has published its resolution, which is the ONLY replaceable state. |
| 192 | + * Kept as a private thunk over the owning scope's `state` rather than exposed on `Current`, |
| 193 | + * so no public contract widens and no existing `current()` consumer changes. |
| 194 | + * |
| 195 | + * Deliberately NOT `closed`: `closeNow` already unregisters, so a closed scope is absent from |
| 196 | + * this map. Deliberately NOT `degraded`/`cancelled` while unresolved — those still throw from |
| 197 | + * `own()` and remain CP-031's recoverable admission failure, which R-23 preserves. |
| 198 | + */ |
| 199 | + readonly resolved: () => boolean |
167 | 200 | } |
168 | 201 | > |
169 | 202 | /** Core owns each ref's strong fence-lifetime root; historical snapshots therefore key weakly. */ |
@@ -296,12 +329,26 @@ export const make = Effect.gen(function* () { |
296 | 329 |
|
297 | 330 | const own: Scope["own"] = (messageID) => |
298 | 331 | apply(() => { |
299 | | - if (state.closed) return |
| 332 | + // CP-032 R-08. Ownership is REFUSED here, not silently dropped, and the caller learns which. |
| 333 | + // |
| 334 | + // A resolved scope reaches this branch (`gate()` sets `closed` at every resolution), and a |
| 335 | + // borrow check performed earlier cannot have caught it: the admission path yields through |
| 336 | + // `revert.cleanup` and `createUserMessage` between discovery and this call, so a scope that |
| 337 | + // was live at lookup can resolve before ownership. Returning `false` — rather than the |
| 338 | + // former bare `return` — is what lets the admission boundary convert that into the typed |
| 339 | + // refusal instead of proceeding onto a dead scope and replaying its earlier answer. |
| 340 | + // |
| 341 | + // The no-op itself is preserved and load-bearing: taking ownership here would `invalidate()` |
| 342 | + // a settled candidate and let a later `result()` fall through to a different fallback, so a |
| 343 | + // completed scope's selected answer must stay immutable. Non-admission callers ignore the |
| 344 | + // result and keep exactly that behaviour. |
| 345 | + if (state.closed) return false |
300 | 346 | if (state.degraded || state.cancelled || state.resolution) { |
301 | 347 | throw new Error(`Attachment scope ${state.scopeID} cannot own another message`) |
302 | 348 | } |
303 | 349 | state.messages.add(messageID) |
304 | 350 | invalidate() |
| 351 | + return true |
305 | 352 | }) |
306 | 353 |
|
307 | 354 | const owns: Scope["owns"] = (messageID) => transition(() => state.messages.has(messageID)) |
@@ -536,9 +583,36 @@ export const make = Effect.gen(function* () { |
536 | 583 | close, |
537 | 584 | } |
538 | 585 |
|
| 586 | + const entry = { |
| 587 | + scope: handle, |
| 588 | + captureFence, |
| 589 | + resolved: () => state.resolution !== undefined, |
| 590 | + } |
| 591 | + |
| 592 | + // CP-032 R-08. One synchronous critical section, so check-and-replace is atomic by |
| 593 | + // construction: `Effect.sync` cannot yield, and `closeNow`'s delete runs inside `transition`, |
| 594 | + // which cannot either. No new lock is needed. |
| 595 | + // |
| 596 | + // A LIVE incumbent still refuses the open — CP-031's exclusivity, unchanged, and the path that |
| 597 | + // keeps producing the typed `SessionScopeOwnRefused` note for a degraded-but-unresolved scope. |
| 598 | + // A RESOLVED incumbent is replaced in place: it can no longer serve a borrower, and leaving it |
| 599 | + // registered is exactly what made a later supplement's distinct answer vanish into the earlier |
| 600 | + // filed position with no note and no error. |
| 601 | + // |
| 602 | + // The predecessor is not closed here; its own finalizer still runs and is identity-safe. |
| 603 | + // `closeNow` deletes only while the registered scope id is still its own, and `scopeID` is a |
| 604 | + // fresh UUID per open, so the predecessor cannot evict the successor. |
539 | 605 | const registered = yield* Effect.sync(() => { |
540 | | - if (registry.scopes.has(sessionID)) return false |
541 | | - registry.scopes.set(sessionID, { scope: handle, captureFence }) |
| 606 | + const incumbent = registry.scopes.get(sessionID) |
| 607 | + if (incumbent && !incumbent.resolved()) return false |
| 608 | + // The predecessor's fence snapshot is deliberately left bound to the predecessor. A fence ref |
| 609 | + // authorizes the exact scope generation it was captured on, never the session's future |
| 610 | + // successor — the standing contract proved by "captureFence and claimCancellationAtFence bind |
| 611 | + // exact scope generations". Deleting or forwarding it here would manufacture authority over a |
| 612 | + // generation the ref never named. The successor stays fence-capable because `captureFence` |
| 613 | + // resolves an unretained ref through `registry.scopes.get(sessionID)`, which is now the |
| 614 | + // successor. |
| 615 | + registry.scopes.set(sessionID, entry) |
542 | 616 | return true |
543 | 617 | }) |
544 | 618 | if (!registered) return yield* Effect.fail(new Error(`Attachment scope already open for session ${sessionID}`)) |
@@ -579,6 +653,20 @@ export const make = Effect.gen(function* () { |
579 | 653 | return registry.scopes.get(sessionID)?.scope |
580 | 654 | }) |
581 | 655 |
|
| 656 | + // CP-032 R-08. Raw `locate` stays raw because two of its three production consumers need registry |
| 657 | + // truth rather than usability: `tool/task.ts` reconciles the carried parent scope by object |
| 658 | + // identity and fails the call on disagreement, and `attachment/participant.ts` reports covered |
| 659 | + // edges for closure proof and must not narrow the proven set. Only `executeSupplement`'s borrow |
| 660 | + // needs the qualified answer, so the qualification lives here rather than inside `locate`. |
| 661 | + const locateBorrowable: Interface["locateBorrowable"] = Effect.fn("AttachmentCoordinator.locateBorrowable")( |
| 662 | + function* (sessionID) { |
| 663 | + const registry = yield* InstanceState.get(registries) |
| 664 | + const entry = registry.scopes.get(sessionID) |
| 665 | + if (!entry || entry.resolved()) return undefined |
| 666 | + return entry.scope |
| 667 | + }, |
| 668 | + ) |
| 669 | + |
582 | 670 | const captureFence: Interface["captureFence"] = Effect.fn("AttachmentCoordinator.captureFence")( |
583 | 671 | function* (sessionID, ref) { |
584 | 672 | const registry = yield* InstanceState.get(registries) |
@@ -601,6 +689,7 @@ export const make = Effect.gen(function* () { |
601 | 689 | return Service.of({ |
602 | 690 | open, |
603 | 691 | locate, |
| 692 | + locateBorrowable, |
604 | 693 | captureFence, |
605 | 694 | claimCancellationAtFence, |
606 | 695 | claim, |
|