@code-yeongyu/senpi-agent-core 2026.11.6-1 → 2026.11.6-2
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/dist/durable/session-store.d.ts +18 -1
- package/dist/durable/session-store.js +118 -58
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +3 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type DurableStore, type StoreValue, type StoreWrite, type SyncCommitStore } from "./store.ts";
|
|
2
2
|
export interface StoredEntry {
|
|
3
3
|
readonly key: string;
|
|
4
4
|
readonly line: string;
|
|
@@ -16,6 +16,23 @@ export declare class SessionStoreError extends Error {
|
|
|
16
16
|
readonly code: SessionStoreErrorCode;
|
|
17
17
|
constructor(code: SessionStoreErrorCode, message: string);
|
|
18
18
|
}
|
|
19
|
+
export interface SessionState extends StoredSession {
|
|
20
|
+
readonly divergence: StoreValue;
|
|
21
|
+
/** How many rewrite records the session holds; an append record's id is scoped to it. */
|
|
22
|
+
readonly rewrites: number;
|
|
23
|
+
}
|
|
24
|
+
/** The session as committed: the last rewrite, then every later append; a key seen twice keeps its first record. */
|
|
25
|
+
export declare function readSessionState(store: DurableStore, id: string): Promise<SessionState | undefined>;
|
|
26
|
+
/** A new session's manifest: its id and starting header. Written once, never by a later write of the session. */
|
|
27
|
+
export declare const manifestWrite: (id: string, header: SessionHeader) => StoreWrite;
|
|
28
|
+
/** The session's divergence marker; only the S07 importer writes it, so a handle's write can never clobber it. */
|
|
29
|
+
export declare const divergenceWrite: (id: string, divergence: StoreValue) => StoreWrite;
|
|
30
|
+
/**
|
|
31
|
+
* One record per entry, its id derived from the session, its rewrite count and the key: two processes appending the
|
|
32
|
+
* same key to the same session collide on the id, so the store refuses the second batch (`duplicate-record-id`).
|
|
33
|
+
*/
|
|
34
|
+
export declare const appendRecordWrites: (id: string, rewrites: number, added: readonly StoredEntry[]) => StoreWrite[];
|
|
35
|
+
export declare const rewriteRecordWrite: (store: DurableStore, id: string, header: SessionHeader, entries: readonly StoredEntry[]) => StoreWrite;
|
|
19
36
|
export interface SessionHandle {
|
|
20
37
|
header(): SessionHeader;
|
|
21
38
|
entries(): readonly StoredEntry[];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DurableStoreError, } from "./store.js";
|
|
1
2
|
export class SessionStoreError extends Error {
|
|
2
3
|
constructor(code, message) {
|
|
3
4
|
super(message);
|
|
@@ -5,90 +6,143 @@ export class SessionStoreError extends Error {
|
|
|
5
6
|
this.code = code;
|
|
6
7
|
}
|
|
7
8
|
}
|
|
8
|
-
//
|
|
9
|
+
// One layout for both writers (this store and the S07 JSONL importer, coding-agent session-import/jsonl-import.ts).
|
|
10
|
+
// A session's entries are records in the store's append-only log, one kind per session, so sqlite orders the appends of
|
|
11
|
+
// every process bound to the session and none is lost. A record is an append (one entry) or a rewrite (the full header
|
|
12
|
+
// and entry list, replacing everything before it). The manifest document holds only what is not ordered: the header a
|
|
13
|
+
// new session starts with, and its divergence.
|
|
9
14
|
const SESSION = "session-import/session/";
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
15
|
+
const DIVERGENCE = "session-import/divergence/";
|
|
16
|
+
const sessionManifestKey = (id) => SESSION + encodeURIComponent(id);
|
|
17
|
+
const divergenceKey = (id) => DIVERGENCE + encodeURIComponent(id);
|
|
18
|
+
const sessionRecordKind = (id) => `session/${encodeURIComponent(id)}`;
|
|
13
19
|
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
function readEntry(id, value) {
|
|
21
|
+
if (isRecord(value) && typeof value.key === "string" && typeof value.line === "string") {
|
|
22
|
+
return { key: value.key, line: value.line };
|
|
23
|
+
}
|
|
24
|
+
throw new SessionStoreError("corrupt-session", `session ${id} holds an unreadable entry record`);
|
|
25
|
+
}
|
|
26
|
+
/** The session as committed: the last rewrite, then every later append; a key seen twice keeps its first record. */
|
|
27
|
+
export async function readSessionState(store, id) {
|
|
28
|
+
const manifest = await store.getDocument(sessionManifestKey(id));
|
|
16
29
|
if (manifest === undefined)
|
|
17
30
|
return undefined;
|
|
18
|
-
|
|
19
|
-
if (!isRecord(manifest) || !isRecord(manifest.header) || !Array.isArray(entryIds)) {
|
|
31
|
+
if (!isRecord(manifest) || !isRecord(manifest.header)) {
|
|
20
32
|
throw new SessionStoreError("corrupt-session", `session ${id} has no readable manifest`);
|
|
21
33
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
34
|
+
if ("entryIds" in manifest) {
|
|
35
|
+
throw new SessionStoreError("corrupt-session", `session ${id} is stored in the layout before the record log; re-import it from its JSONL file`);
|
|
36
|
+
}
|
|
37
|
+
let header = manifest.header;
|
|
38
|
+
let entries = [];
|
|
39
|
+
let held = new Set();
|
|
40
|
+
let rewrites = 0;
|
|
41
|
+
for (const record of await store.scanRecords({ kind: sessionRecordKind(id), order: "asc" })) {
|
|
42
|
+
const data = record.data;
|
|
43
|
+
if (isRecord(data) && data.op === "rewrite" && isRecord(data.header) && Array.isArray(data.entries)) {
|
|
44
|
+
header = data.header;
|
|
45
|
+
entries = data.entries.map((entry) => readEntry(id, entry));
|
|
46
|
+
held = new Set(entries.map((entry) => entry.key));
|
|
47
|
+
rewrites++;
|
|
48
|
+
}
|
|
49
|
+
else if (isRecord(data) && data.op === "append") {
|
|
50
|
+
const entry = readEntry(id, data);
|
|
51
|
+
if (held.has(entry.key))
|
|
52
|
+
continue;
|
|
53
|
+
held.add(entry.key);
|
|
54
|
+
entries.push(entry);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
throw new SessionStoreError("corrupt-session", `session ${id} holds an unreadable record ${record.seq}`);
|
|
27
58
|
}
|
|
28
|
-
entries.push({ key, line });
|
|
29
59
|
}
|
|
30
|
-
|
|
60
|
+
const divergence = (await store.getDocument(divergenceKey(id))) ?? null;
|
|
61
|
+
return { id, header, entries, divergence, rewrites };
|
|
31
62
|
}
|
|
32
|
-
|
|
63
|
+
/** A new session's manifest: its id and starting header. Written once, never by a later write of the session. */
|
|
64
|
+
export const manifestWrite = (id, header) => ({
|
|
65
|
+
op: "put",
|
|
66
|
+
key: sessionManifestKey(id),
|
|
67
|
+
data: { id, header },
|
|
68
|
+
});
|
|
69
|
+
/** The session's divergence marker; only the S07 importer writes it, so a handle's write can never clobber it. */
|
|
70
|
+
export const divergenceWrite = (id, divergence) => ({
|
|
33
71
|
op: "put",
|
|
34
|
-
key:
|
|
35
|
-
data:
|
|
36
|
-
id: state.id,
|
|
37
|
-
header: state.header,
|
|
38
|
-
entryIds: state.entries.map((entry) => entry.key),
|
|
39
|
-
divergence: state.divergence,
|
|
40
|
-
},
|
|
72
|
+
key: divergenceKey(id),
|
|
73
|
+
data: divergence,
|
|
41
74
|
});
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
75
|
+
/**
|
|
76
|
+
* One record per entry, its id derived from the session, its rewrite count and the key: two processes appending the
|
|
77
|
+
* same key to the same session collide on the id, so the store refuses the second batch (`duplicate-record-id`).
|
|
78
|
+
*/
|
|
79
|
+
export const appendRecordWrites = (id, rewrites, added) => added.map((entry) => ({
|
|
80
|
+
op: "append",
|
|
81
|
+
id: `${sessionRecordKind(id)}/${rewrites}/${encodeURIComponent(entry.key)}`,
|
|
82
|
+
kind: sessionRecordKind(id),
|
|
83
|
+
data: { op: "append", key: entry.key, line: entry.line },
|
|
84
|
+
}));
|
|
85
|
+
export const rewriteRecordWrite = (store, id, header, entries) => ({
|
|
86
|
+
op: "append",
|
|
87
|
+
id: store.mintId(),
|
|
88
|
+
kind: sessionRecordKind(id),
|
|
89
|
+
data: { op: "rewrite", header, entries: entries.map((entry) => ({ key: entry.key, line: entry.line })) },
|
|
90
|
+
});
|
|
91
|
+
function refuseRepeats(id, entries, held, what) {
|
|
92
|
+
const seen = new Set(held);
|
|
93
|
+
for (const entry of entries) {
|
|
94
|
+
if (seen.has(entry.key))
|
|
95
|
+
throw new SessionStoreError("duplicate-entry", `${what} ${id} repeats ${entry.key}`);
|
|
96
|
+
seen.add(entry.key);
|
|
48
97
|
}
|
|
98
|
+
}
|
|
99
|
+
function planAppend(state, held, added) {
|
|
100
|
+
refuseRepeats(state.id, added, state.entries.map((entry) => entry.key), "session");
|
|
49
101
|
const next = { ...state, entries: [...state.entries, ...added] };
|
|
50
|
-
const
|
|
51
|
-
return { writes: [...
|
|
102
|
+
const create = held ? [] : [manifestWrite(state.id, state.header)];
|
|
103
|
+
return { writes: [...create, ...appendRecordWrites(state.id, state.rewrites, added)], next };
|
|
52
104
|
}
|
|
53
|
-
function planRewrite(state, header, entries) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const puts = entries.map((entry) => ({
|
|
65
|
-
op: "put",
|
|
66
|
-
key: entryKey(state.id, entry.key),
|
|
67
|
-
data: entry.line,
|
|
68
|
-
}));
|
|
69
|
-
return { writes: [...deletes, ...puts, manifestWrite(next)], next };
|
|
105
|
+
function planRewrite(store, state, held, header, entries) {
|
|
106
|
+
refuseRepeats(state.id, entries, [], "rewrite of");
|
|
107
|
+
const next = { ...state, header, entries: [...entries], rewrites: state.rewrites + 1 };
|
|
108
|
+
const create = held ? [] : [manifestWrite(state.id, header)];
|
|
109
|
+
return { writes: [...create, rewriteRecordWrite(store, state.id, header, entries)], next };
|
|
110
|
+
}
|
|
111
|
+
/** Another process committed the same entry key first: the store refused the batch on its record id. */
|
|
112
|
+
function asDuplicateEntry(id, error) {
|
|
113
|
+
return error instanceof DurableStoreError && error.code === "duplicate-record-id"
|
|
114
|
+
? new SessionStoreError("duplicate-entry", `session ${id} already holds an entry this write repeats`)
|
|
115
|
+
: error;
|
|
70
116
|
}
|
|
71
117
|
const stored = ({ id, header, entries }) => ({ id, header, entries });
|
|
72
118
|
/** An existing session keeps its stored header; `header` only seeds a session the store does not hold yet. */
|
|
73
119
|
async function openState(store, id, header) {
|
|
74
|
-
|
|
120
|
+
const read = await readSessionState(store, id);
|
|
121
|
+
return read === undefined
|
|
122
|
+
? { state: { id, header, entries: [], divergence: null, rewrites: 0 }, held: false }
|
|
123
|
+
: { state: read, held: true };
|
|
75
124
|
}
|
|
76
125
|
/** A session on any DurableStore (Durable Object included): every write is one async atomic commit. */
|
|
77
126
|
export function openSessionStore(store, epoch) {
|
|
78
127
|
return {
|
|
79
128
|
load: async (id) => {
|
|
80
|
-
const state = await
|
|
129
|
+
const state = await readSessionState(store, id);
|
|
81
130
|
return state === undefined ? undefined : stored(state);
|
|
82
131
|
},
|
|
83
132
|
open: async (id, header) => {
|
|
84
|
-
let state = await openState(store, id, header);
|
|
133
|
+
let { state, held } = await openState(store, id, header);
|
|
85
134
|
// One write at a time, each planned from the state the previous one committed.
|
|
86
135
|
let tail = Promise.resolve();
|
|
87
136
|
const enqueue = (plan) => {
|
|
88
137
|
const run = tail.then(async () => {
|
|
89
138
|
const next = plan();
|
|
90
|
-
await store
|
|
139
|
+
await store
|
|
140
|
+
.commit(epoch, next.writes, () => { })
|
|
141
|
+
.catch((error) => {
|
|
142
|
+
throw asDuplicateEntry(id, error);
|
|
143
|
+
});
|
|
91
144
|
state = next.next;
|
|
145
|
+
held = true;
|
|
92
146
|
});
|
|
93
147
|
tail = run.catch(() => undefined);
|
|
94
148
|
return run;
|
|
@@ -96,8 +150,8 @@ export function openSessionStore(store, epoch) {
|
|
|
96
150
|
return {
|
|
97
151
|
header: () => state.header,
|
|
98
152
|
entries: () => state.entries,
|
|
99
|
-
append: (entries) => enqueue(() => planAppend(state, entries)),
|
|
100
|
-
rewrite: (header2, entries) => enqueue(() => planRewrite(state, header2, entries)),
|
|
153
|
+
append: (entries) => enqueue(() => planAppend(state, held, entries)),
|
|
154
|
+
rewrite: (header2, entries) => enqueue(() => planRewrite(store, state, held, header2, entries)),
|
|
101
155
|
};
|
|
102
156
|
},
|
|
103
157
|
};
|
|
@@ -107,16 +161,22 @@ export function openSyncSessionStore(store, epoch) {
|
|
|
107
161
|
return {
|
|
108
162
|
load: openSessionStore(store, epoch).load,
|
|
109
163
|
open: async (id, header) => {
|
|
110
|
-
let state = await openState(store, id, header);
|
|
164
|
+
let { state, held } = await openState(store, id, header);
|
|
111
165
|
const commit = (plan) => {
|
|
112
|
-
|
|
166
|
+
try {
|
|
167
|
+
store.commitSync(epoch, plan.writes, () => { });
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
throw asDuplicateEntry(id, error);
|
|
171
|
+
}
|
|
113
172
|
state = plan.next;
|
|
173
|
+
held = true;
|
|
114
174
|
};
|
|
115
175
|
return {
|
|
116
176
|
header: () => state.header,
|
|
117
177
|
entries: () => state.entries,
|
|
118
|
-
appendSync: (entries) => commit(planAppend(state, entries)),
|
|
119
|
-
rewriteSync: (header2, entries) => commit(planRewrite(state, header2, entries)),
|
|
178
|
+
appendSync: (entries) => commit(planAppend(state, held, entries)),
|
|
179
|
+
rewriteSync: (header2, entries) => commit(planRewrite(store, state, held, header2, entries)),
|
|
120
180
|
};
|
|
121
181
|
},
|
|
122
182
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export { decodeRemoteMessage, encodeRemoteMessage, HAND_CONTROL_LANE, type Remot
|
|
|
14
14
|
export { createErrorToolResult, createToolResultMessage, emitToolExecutionEnd, emitToolExecutionUpdate, emitToolResultMessage, shouldTerminateToolBatch, } from "./execution/tool-outcome-events.ts";
|
|
15
15
|
export { createAdmissionLine } from "./durable/store-admission.ts";
|
|
16
16
|
export type { SessionHandle, SessionHeader, StoredEntry, StoredSession, SyncSessionHandle, } from "./durable/session-store.ts";
|
|
17
|
-
export { openSessionStore, openSyncSessionStore, SessionStoreError } from "./durable/session-store.ts";
|
|
17
|
+
export { appendRecordWrites, divergenceWrite, manifestWrite, openSessionStore, openSyncSessionStore, readSessionState, rewriteRecordWrite, SessionStoreError, type SessionState, } from "./durable/session-store.ts";
|
|
18
18
|
export type { SyncCommit, SyncCommitStore } from "./durable/store.ts";
|
|
19
19
|
export { createMemoryStoreMedium, openMemoryStore } from "./durable/store-memory.ts";
|
|
20
20
|
export { createBatchCoordinator } from "./execution/coordinator.ts";
|
package/dist/index.js
CHANGED
|
@@ -12,7 +12,7 @@ export { createHandLedger, } from "./execution/remote-hand-ledger.js";
|
|
|
12
12
|
export { decodeRemoteMessage, encodeRemoteMessage, HAND_CONTROL_LANE, RemoteProtocolError, } from "./execution/remote-protocol.js";
|
|
13
13
|
export { createErrorToolResult, createToolResultMessage, emitToolExecutionEnd, emitToolExecutionUpdate, emitToolResultMessage, shouldTerminateToolBatch, } from "./execution/tool-outcome-events.js";
|
|
14
14
|
export { createAdmissionLine } from "./durable/store-admission.js";
|
|
15
|
-
export { openSessionStore, openSyncSessionStore, SessionStoreError } from "./durable/session-store.js";
|
|
15
|
+
export { appendRecordWrites, divergenceWrite, manifestWrite, openSessionStore, openSyncSessionStore, readSessionState, rewriteRecordWrite, SessionStoreError, } from "./durable/session-store.js";
|
|
16
16
|
export { createMemoryStoreMedium, openMemoryStore } from "./durable/store-memory.js";
|
|
17
17
|
export { createBatchCoordinator } from "./execution/coordinator.js";
|
|
18
18
|
export { createExecutionLedger } from "./execution/ledger.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@code-yeongyu/senpi-agent-core",
|
|
3
|
-
"version": "2026.11.6-
|
|
3
|
+
"version": "2026.11.6-2",
|
|
4
4
|
"description": "General-purpose agent with transport abstraction, state management, and attachment support",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -72,8 +72,8 @@
|
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
74
|
"@earendil-works/chord": "0.99.1",
|
|
75
|
-
"@earendil-works/pi-ai": "npm:@code-yeongyu/senpi-ai@2026.11.6-
|
|
76
|
-
"@earendil-works/pi-telemetry": "npm:@code-yeongyu/senpi-telemetry@2026.11.6-
|
|
75
|
+
"@earendil-works/pi-ai": "npm:@code-yeongyu/senpi-ai@2026.11.6-2",
|
|
76
|
+
"@earendil-works/pi-telemetry": "npm:@code-yeongyu/senpi-telemetry@2026.11.6-2",
|
|
77
77
|
"diff": "9.0.0",
|
|
78
78
|
"ignore": "7.0.9",
|
|
79
79
|
"typebox": "1.3.34",
|