@cotal-ai/pi 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,108 @@
1
+ const TOMBSTONE_CAP = 4096;
2
+ /**
3
+ * Pi-local acknowledgement ledger over MeshAgent's public prefix-drain API.
4
+ *
5
+ * A provider-confirmed batch is drained only when its still-buffered ids are the current front
6
+ * prefix. Missing ids are allowed only at the older edge, where MeshAgent's bounded inbox may have
7
+ * force-evicted them. Anything else is tombstoned and left unacked rather than risking a positional
8
+ * drain of unrelated traffic. A late duplicate is discarded only when it reaches the front.
9
+ */
10
+ export class InboxTurn {
11
+ source;
12
+ tombstones = new Set();
13
+ previousTombstones = new Set();
14
+ constructor(source) {
15
+ this.source = source;
16
+ }
17
+ peek() {
18
+ return this.source.peekInbox();
19
+ }
20
+ /** Remove already-confirmed late duplicates from the front, one exact item at a time. */
21
+ discardTombstonedFront() {
22
+ let discarded = 0;
23
+ while (true) {
24
+ const front = this.source.peekInbox()[0];
25
+ if (!front || !this.hasTombstone(front.id))
26
+ return discarded;
27
+ this.source.drainInbox(1);
28
+ discarded++;
29
+ }
30
+ }
31
+ /** Prefix-only discard for adapter-local traffic such as own channel echoes. */
32
+ discardFront(match) {
33
+ let discarded = 0;
34
+ while (true) {
35
+ const front = this.source.peekInbox()[0];
36
+ if (!front || !match(front))
37
+ return discarded;
38
+ this.source.drainInbox(1);
39
+ discarded++;
40
+ }
41
+ }
42
+ /**
43
+ * Select the next FIFO batch after already-reserved ids. `boundary` is never skipped: a buried
44
+ * echo or other local boundary waits until older reserved work reaches a terminal decision.
45
+ */
46
+ select(reserved, boundary, limit) {
47
+ const selected = [];
48
+ for (const item of this.source.peekInbox()) {
49
+ if (reserved.has(item.id))
50
+ continue;
51
+ if (boundary(item))
52
+ break;
53
+ selected.push(item);
54
+ if (selected.length >= limit)
55
+ break;
56
+ }
57
+ return selected;
58
+ }
59
+ /**
60
+ * Commit provider-confirmed ids without ever calling drainInbox(0), which means "drain all" in
61
+ * MeshAgent. On an invariant mismatch, acknowledge nothing and retain tombstones for exact later
62
+ * discard.
63
+ */
64
+ commitConfirmed(ids) {
65
+ if (ids.length === 0)
66
+ return { drained: 0, tombstoned: 0 };
67
+ this.discardTombstonedFront();
68
+ const pending = this.source.peekInbox();
69
+ const pendingIds = new Set(pending.map((item) => item.id));
70
+ const firstPresent = ids.findIndex((id) => pendingIds.has(id));
71
+ if (firstPresent < 0) {
72
+ for (const id of ids)
73
+ this.addTombstone(id);
74
+ return { drained: 0, tombstoned: ids.length };
75
+ }
76
+ const remaining = ids.slice(firstPresent);
77
+ const missingAfterFront = remaining.find((id) => !pendingIds.has(id));
78
+ const actualPrefix = pending.slice(0, remaining.length).map((item) => item.id);
79
+ const prefixMatches = !missingAfterFront && actualPrefix.length === remaining.length && actualPrefix.every((id, i) => id === remaining[i]);
80
+ if (!prefixMatches) {
81
+ for (const id of ids)
82
+ this.addTombstone(id);
83
+ const expected = remaining.join(", ");
84
+ const actual = actualPrefix.join(", ");
85
+ return {
86
+ drained: 0,
87
+ tombstoned: ids.length,
88
+ error: `confirmed inbox prefix mismatch: expected [${expected}], found [${actual}]`,
89
+ };
90
+ }
91
+ if (remaining.length > 0)
92
+ this.source.drainInbox(remaining.length);
93
+ for (const id of ids.slice(0, firstPresent))
94
+ this.addTombstone(id);
95
+ return { drained: remaining.length, tombstoned: firstPresent };
96
+ }
97
+ hasTombstone(id) {
98
+ return this.tombstones.has(id) || this.previousTombstones.has(id);
99
+ }
100
+ addTombstone(id) {
101
+ this.tombstones.add(id);
102
+ if (this.tombstones.size >= TOMBSTONE_CAP) {
103
+ this.previousTombstones = this.tombstones;
104
+ this.tombstones = new Set();
105
+ }
106
+ }
107
+ }
108
+ //# sourceMappingURL=inbox-turn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inbox-turn.js","sourceRoot":"","sources":["../src/inbox-turn.ts"],"names":[],"mappings":"AAaA,MAAM,aAAa,GAAG,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,kBAAkB,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACpE,CAAC;IAEO,YAAY,CAAC,EAAU;QAC7B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACxB,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,IAAI,aAAa,EAAE,CAAC;YAC1C,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC,UAAU,CAAC;YAC1C,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;QAC9B,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,5 @@
1
+ import "./connector.js";
2
+ export { piConnector } from "./connector.js";
3
+ export { default as cotalMeshExtension } from "./extension.js";
4
+ export { InboxTurn, type InboxSource } from "./inbox-turn.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import "./connector.js"; // self-registers the "pi" connector
2
+ export { piConnector } from "./connector.js";
3
+ export { default as cotalMeshExtension } from "./extension.js";
4
+ export { InboxTurn } from "./inbox-turn.js"; // for SDK embedders driving their own session
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,CAAC,CAAC,oCAAoC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAoB,MAAM,iBAAiB,CAAC,CAAC,8CAA8C"}
@@ -0,0 +1,2 @@
1
+ export { default } from "./extension.js";
2
+ //# sourceMappingURL=standalone.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"standalone.d.ts","sourceRoot":"","sources":["../src/standalone.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC"}