@howaboua/pi-durable-context 0.0.0-stage → 0.9.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.
- package/LICENSE +21 -0
- package/NOTICE +63 -0
- package/README.md +168 -2
- package/dist/history-search-worker.js +85 -0
- package/dist/index.js +2628 -0
- package/dist/internal/context-visibility.d.ts +6 -0
- package/dist/node.js +124 -0
- package/dist/packages/context/src/admission.d.ts +23 -0
- package/dist/packages/context/src/history-archive.d.ts +24 -0
- package/dist/packages/context/src/history-items.d.ts +17 -0
- package/dist/packages/context/src/history-ranker.d.ts +27 -0
- package/dist/packages/context/src/history-search-worker.d.ts +15 -0
- package/dist/packages/context/src/history.d.ts +80 -0
- package/dist/packages/context/src/index.d.ts +72 -0
- package/dist/packages/context/src/instruction-run.d.ts +13 -0
- package/dist/packages/context/src/lifecycle.d.ts +40 -0
- package/dist/packages/context/src/model-roles.d.ts +26 -0
- package/dist/packages/context/src/node.d.ts +10 -0
- package/dist/packages/context/src/note-path.d.ts +2 -0
- package/dist/packages/context/src/note-store.d.ts +53 -0
- package/dist/packages/context/src/notes.d.ts +76 -0
- package/dist/packages/context/src/policy.d.ts +24 -0
- package/dist/packages/context/src/presentation.d.ts +52 -0
- package/dist/packages/context/src/query-input.d.ts +3 -0
- package/dist/packages/context/src/remote.d.ts +34 -0
- package/dist/packages/context/src/rollover-preparation.d.ts +16 -0
- package/dist/packages/context/src/rollover.d.ts +32 -0
- package/dist/packages/context/src/tool-contract.d.ts +85 -0
- package/dist/packages/context/src/tool-rollover.d.ts +41 -0
- package/dist/packages/context/src/usage.d.ts +24 -0
- package/dist/packages/context/src/window-state.d.ts +37 -0
- package/dist/presentation.js +93 -0
- package/package.json +50 -5
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { ConversationId, Storage, Tx } from "@earendil-works/pi-durable";
|
|
3
|
+
/** Persist an immutable group for a newly created child before dispatching its first task. */
|
|
4
|
+
export declare function assignContextVisibility(tx: Tx, parent: ConversationId, child: ConversationId, shareContext: boolean | undefined): Promise<void>;
|
|
5
|
+
/** Foreign Context reads require exact group equality; absent records mean legacy shared storage. */
|
|
6
|
+
export declare function canReadContextTarget(storage: Storage, caller: ConversationId, target: ConversationId, context: Context): Promise<boolean>;
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// packages/context/src/node.ts
|
|
2
|
+
import { isAbsolute } from "node:path";
|
|
3
|
+
import { Worker } from "node:worker_threads";
|
|
4
|
+
function createNodeHistoryRanker(options) {
|
|
5
|
+
if (!isAbsolute(options.path))
|
|
6
|
+
throw new Error("History index requires an absolute host-granted path");
|
|
7
|
+
const workers = new Set;
|
|
8
|
+
let pending = Promise.resolve();
|
|
9
|
+
let closed = false;
|
|
10
|
+
return {
|
|
11
|
+
async refresh(request) {
|
|
12
|
+
if (closed)
|
|
13
|
+
throw new Error("History ranker is closed");
|
|
14
|
+
request.signal?.throwIfAborted();
|
|
15
|
+
await schedule(request);
|
|
16
|
+
},
|
|
17
|
+
async search(request) {
|
|
18
|
+
request.signal?.throwIfAborted();
|
|
19
|
+
if (closed)
|
|
20
|
+
throw new Error("History ranker is closed");
|
|
21
|
+
const words = [
|
|
22
|
+
...new Set(request.query.toLowerCase().match(/[\p{L}\p{N}\p{M}\p{Co}]+/gu))
|
|
23
|
+
];
|
|
24
|
+
if (!words.length)
|
|
25
|
+
throw new Error("Supply at least one word or number to search history");
|
|
26
|
+
return schedule(request, words.map((word) => `"${word}"`).join(" AND "));
|
|
27
|
+
},
|
|
28
|
+
async close() {
|
|
29
|
+
closed = true;
|
|
30
|
+
await Promise.all([...workers].map((worker) => worker.terminate()));
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
function schedule(request, match) {
|
|
34
|
+
const operation = pending.then(() => run(request, match));
|
|
35
|
+
pending = operation.then(() => {}, () => {});
|
|
36
|
+
if (!request.signal)
|
|
37
|
+
return operation;
|
|
38
|
+
const signal = request.signal;
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
const abort = () => reject(signal.reason ?? new Error("History search cancelled"));
|
|
41
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
42
|
+
operation.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
43
|
+
if (signal.aborted)
|
|
44
|
+
abort();
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function run(request, match) {
|
|
48
|
+
request.signal?.throwIfAborted();
|
|
49
|
+
if (closed)
|
|
50
|
+
return Promise.reject(new Error("History ranker is closed"));
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
const iterator = async function* () {
|
|
53
|
+
yield* request.items;
|
|
54
|
+
}();
|
|
55
|
+
const worker = new Worker(new URL(import.meta.url.endsWith(".ts") ? "./history-search-worker.ts" : "./history-search-worker.js", import.meta.url), {
|
|
56
|
+
workerData: {
|
|
57
|
+
path: options.path,
|
|
58
|
+
scope: request.scope,
|
|
59
|
+
limit: request.limit,
|
|
60
|
+
recentFirst: request.recentFirst,
|
|
61
|
+
match
|
|
62
|
+
},
|
|
63
|
+
execArgv: []
|
|
64
|
+
});
|
|
65
|
+
workers.add(worker);
|
|
66
|
+
let settled = false;
|
|
67
|
+
const finish = (error, ids) => {
|
|
68
|
+
if (settled)
|
|
69
|
+
return;
|
|
70
|
+
settled = true;
|
|
71
|
+
request.signal?.removeEventListener("abort", abort);
|
|
72
|
+
iterator.return(undefined).catch(() => {});
|
|
73
|
+
worker.terminate().then(() => {
|
|
74
|
+
workers.delete(worker);
|
|
75
|
+
if (error)
|
|
76
|
+
reject(error);
|
|
77
|
+
else
|
|
78
|
+
resolve(ids ?? []);
|
|
79
|
+
}, reject);
|
|
80
|
+
};
|
|
81
|
+
const abort = () => finish(new Error("History search cancelled; retry the query", {
|
|
82
|
+
cause: request.signal?.reason
|
|
83
|
+
}));
|
|
84
|
+
request.signal?.addEventListener("abort", abort, { once: true });
|
|
85
|
+
worker.on("message", async (value) => {
|
|
86
|
+
if (settled)
|
|
87
|
+
return;
|
|
88
|
+
if (value && typeof value === "object" && "ready" in value) {
|
|
89
|
+
try {
|
|
90
|
+
const batch = [];
|
|
91
|
+
let chars = 0;
|
|
92
|
+
while (batch.length < 128 && chars < 1e6) {
|
|
93
|
+
request.signal?.throwIfAborted();
|
|
94
|
+
const next = await iterator.next();
|
|
95
|
+
if (settled)
|
|
96
|
+
return;
|
|
97
|
+
if (next.done)
|
|
98
|
+
break;
|
|
99
|
+
batch.push(next.value);
|
|
100
|
+
chars += next.value.content.length;
|
|
101
|
+
}
|
|
102
|
+
worker.postMessage(batch.length ? { items: batch } : { finish: true });
|
|
103
|
+
} catch (error) {
|
|
104
|
+
finish(error instanceof Error ? error : new Error("History index input failed", { cause: error }));
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (value && typeof value === "object" && "ids" in value && Array.isArray(value.ids) && value.ids.every((id) => typeof id === "string"))
|
|
109
|
+
finish(undefined, value.ids);
|
|
110
|
+
else
|
|
111
|
+
finish(new Error("Ranked history search is unavailable; list and read remain available. Check the native index capability or disable ranked history.", {
|
|
112
|
+
cause: value && typeof value === "object" && "error" in value ? value.error : value
|
|
113
|
+
}));
|
|
114
|
+
});
|
|
115
|
+
worker.on("error", (error) => finish(new Error("Ranked history search is unavailable; check the native index capability or disable ranked history", { cause: error })));
|
|
116
|
+
worker.on("exit", () => finish(new Error("History worker stopped; retry the query")));
|
|
117
|
+
if (request.signal?.aborted)
|
|
118
|
+
abort();
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export {
|
|
123
|
+
createNodeHistoryRanker
|
|
124
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { SubmissionId } from "@earendil-works/pi-durable";
|
|
2
|
+
import { type ContextHost, type InputResult } from "./lifecycle.ts";
|
|
3
|
+
import { type createRollover } from "./rollover.ts";
|
|
4
|
+
type InputState = {
|
|
5
|
+
phase: "admit";
|
|
6
|
+
checkpointRequired?: true;
|
|
7
|
+
} | {
|
|
8
|
+
phase: "blocked";
|
|
9
|
+
retry: number;
|
|
10
|
+
reason: string;
|
|
11
|
+
} | {
|
|
12
|
+
phase: "observe";
|
|
13
|
+
submission: SubmissionId;
|
|
14
|
+
};
|
|
15
|
+
export declare function createInput(host: () => ContextHost, rollover: ReturnType<typeof createRollover>, idleMs: number): import("@earendil-works/pi-durable").Task<{
|
|
16
|
+
readonly requestId?: string;
|
|
17
|
+
} & {
|
|
18
|
+
readonly type: "input";
|
|
19
|
+
readonly content: import("@earendil-works/pi-durable").UserInput;
|
|
20
|
+
readonly whenBusy?: "steer" | "followUp" | "reject";
|
|
21
|
+
readonly entry?: import("@earendil-works/pi-durable").InputEntry;
|
|
22
|
+
}, InputState, InputResult, object>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import { type Conversation, type ConversationId, type EntryRecord, type Storage } from "@earendil-works/pi-durable";
|
|
3
|
+
export type HistoryEntry = {
|
|
4
|
+
windowId: string;
|
|
5
|
+
entry: EntryRecord;
|
|
6
|
+
boundary: boolean;
|
|
7
|
+
summary: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type HistoryReader = (id: ConversationId, context: Context) => Promise<Conversation>;
|
|
10
|
+
/** Paged catalogue and transcript reads. Only the current ancestry stays in memory. */
|
|
11
|
+
export declare function historyEntries(options: {
|
|
12
|
+
conversation: Conversation;
|
|
13
|
+
activeWindow: string;
|
|
14
|
+
caller: ConversationId;
|
|
15
|
+
storage: Storage;
|
|
16
|
+
reader: HistoryReader;
|
|
17
|
+
activeWindowFor: (id: ConversationId) => Promise<string>;
|
|
18
|
+
archive: boolean;
|
|
19
|
+
familyTargets?: readonly ConversationId[] | undefined;
|
|
20
|
+
windowId?: string | null | undefined;
|
|
21
|
+
context: Context;
|
|
22
|
+
}): AsyncGenerator<HistoryEntry>;
|
|
23
|
+
/** Opaque recovery references only, never summary or transcript contents in the prompt. */
|
|
24
|
+
export declare function renderArchiveHistoryHint(conversation: Conversation, activeWindow: string, context: Context): Promise<string>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type EntryRecord, type JsonObject } from "@earendil-works/pi-durable";
|
|
2
|
+
import { type HistoryArguments } from "./tool-contract.ts";
|
|
3
|
+
export type HistoryItem = {
|
|
4
|
+
window_id: string;
|
|
5
|
+
item_id: string;
|
|
6
|
+
role: string;
|
|
7
|
+
tool_name?: string;
|
|
8
|
+
tool_namespace?: string;
|
|
9
|
+
content: string;
|
|
10
|
+
};
|
|
11
|
+
export declare function historyItem(entry: EntryRecord, windowId: string): HistoryItem | undefined;
|
|
12
|
+
export declare function matchesHistoryItem(item: HistoryItem, args: HistoryArguments): boolean;
|
|
13
|
+
export declare function itemIdentity(item: HistoryItem): JsonObject;
|
|
14
|
+
export declare function previewHistoryItem(item: HistoryItem, maxChars: number): JsonObject;
|
|
15
|
+
export declare function boundedPreviews(items: readonly JsonObject[]): JsonObject[];
|
|
16
|
+
/** Ranking excludes reasoning, tool calls/results and system content entirely. */
|
|
17
|
+
export declare function rankedHistoryText(entry: EntryRecord): string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ConversationId } from "@earendil-works/pi-durable";
|
|
2
|
+
export type HistoryIndexRequest = {
|
|
3
|
+
scope: string;
|
|
4
|
+
items: Iterable<{
|
|
5
|
+
id: string;
|
|
6
|
+
content: string;
|
|
7
|
+
}> | AsyncIterable<{
|
|
8
|
+
id: string;
|
|
9
|
+
content: string;
|
|
10
|
+
}>;
|
|
11
|
+
signal?: AbortSignal | undefined;
|
|
12
|
+
};
|
|
13
|
+
/** A host-granted derived terms index. Contents must come only from retired visible text. */
|
|
14
|
+
export interface HistoryRanker {
|
|
15
|
+
/** Replace the complete eligible corpus for this scope, removing stale rows. */
|
|
16
|
+
refresh(request: HistoryIndexRequest): Promise<void>;
|
|
17
|
+
search(request: HistoryIndexRequest & {
|
|
18
|
+
query: string;
|
|
19
|
+
limit: number;
|
|
20
|
+
recentFirst: boolean;
|
|
21
|
+
}): Promise<string[]>;
|
|
22
|
+
}
|
|
23
|
+
/** Omitted means off. The callback is read on every tool call for live settings. */
|
|
24
|
+
export type RankedHistory = {
|
|
25
|
+
ranker: HistoryRanker;
|
|
26
|
+
enabled?: (conversationId: ConversationId) => boolean;
|
|
27
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type HistoryWorkerOptions = {
|
|
2
|
+
path: string;
|
|
3
|
+
scope: string;
|
|
4
|
+
match: string | undefined;
|
|
5
|
+
limit: number | undefined;
|
|
6
|
+
recentFirst: boolean | undefined;
|
|
7
|
+
};
|
|
8
|
+
export type HistoryWorkerMessage = {
|
|
9
|
+
items: readonly {
|
|
10
|
+
id: string;
|
|
11
|
+
content: string;
|
|
12
|
+
}[];
|
|
13
|
+
} | {
|
|
14
|
+
finish: true;
|
|
15
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import { type Conversation, type ConversationId, type Storage } from "@earendil-works/pi-durable";
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
import type { RankedHistory } from "./history-ranker.ts";
|
|
5
|
+
import { type ContextRemoteCatalog } from "./remote.ts";
|
|
6
|
+
export declare function createHistoryTool(options: {
|
|
7
|
+
conversation: (id: ConversationId, context: Context) => Promise<Conversation>;
|
|
8
|
+
storage: () => Storage;
|
|
9
|
+
remote?: ContextRemoteCatalog | undefined;
|
|
10
|
+
archiveHistory?: boolean;
|
|
11
|
+
rankedHistory?: RankedHistory | undefined;
|
|
12
|
+
}): {
|
|
13
|
+
name: string;
|
|
14
|
+
description: string;
|
|
15
|
+
parameters: Type.TObject<{
|
|
16
|
+
action: Type.TOptional<Type.TUnion<Type.TLiteral<"help" | "list" | "list_windows" | "read" | "search">[]>>;
|
|
17
|
+
target: Type.TOptional<Type.TString>;
|
|
18
|
+
item_id: Type.TOptional<Type.TString>;
|
|
19
|
+
agent_name: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
20
|
+
limit: Type.TOptional<Type.TInteger>;
|
|
21
|
+
limit_chars: Type.TOptional<Type.TInteger>;
|
|
22
|
+
max_chars_per_item: Type.TOptional<Type.TInteger>;
|
|
23
|
+
offset_chars: Type.TOptional<Type.TInteger>;
|
|
24
|
+
query: Type.TOptional<Type.TString>;
|
|
25
|
+
recent_first: Type.TOptional<Type.TBoolean>;
|
|
26
|
+
role: Type.TOptional<Type.TUnion<[Type.TLiteral<"user">, Type.TLiteral<"assistant">, Type.TLiteral<"tool">, Type.TLiteral<"system">, Type.TLiteral<"developer">, Type.TNull]>>;
|
|
27
|
+
tool_name: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
28
|
+
tool_namespace: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
29
|
+
window_id: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
30
|
+
}>;
|
|
31
|
+
constrainedSampling?: false | import("@earendil-works/pi-ai").ConstrainedSamplingConfig;
|
|
32
|
+
replay?: "safe" | "unsafe";
|
|
33
|
+
executionMode?: import("@earendil-works/pi-durable").ToolExecutionMode;
|
|
34
|
+
prepareArguments?(args: unknown): {
|
|
35
|
+
action?: never;
|
|
36
|
+
target?: string;
|
|
37
|
+
item_id?: string;
|
|
38
|
+
agent_name?: string | null;
|
|
39
|
+
limit?: number;
|
|
40
|
+
limit_chars?: number;
|
|
41
|
+
max_chars_per_item?: number;
|
|
42
|
+
offset_chars?: number;
|
|
43
|
+
query?: string;
|
|
44
|
+
recent_first?: boolean;
|
|
45
|
+
role?: "assistant" | "developer" | "system" | "tool" | "user" | null;
|
|
46
|
+
tool_name?: string | null;
|
|
47
|
+
tool_namespace?: string | null;
|
|
48
|
+
window_id?: string | null;
|
|
49
|
+
};
|
|
50
|
+
structuredOutputSchema?: Type.TSchema;
|
|
51
|
+
callers?: readonly ("model" | "tools")[];
|
|
52
|
+
deferred?: true;
|
|
53
|
+
outputLimits?: {
|
|
54
|
+
readonly maxBytes?: number;
|
|
55
|
+
readonly maxLines?: number;
|
|
56
|
+
readonly retain?: "head" | "tail";
|
|
57
|
+
};
|
|
58
|
+
execute(args: {
|
|
59
|
+
action?: never;
|
|
60
|
+
target?: string;
|
|
61
|
+
item_id?: string;
|
|
62
|
+
agent_name?: string | null;
|
|
63
|
+
limit?: number;
|
|
64
|
+
limit_chars?: number;
|
|
65
|
+
max_chars_per_item?: number;
|
|
66
|
+
offset_chars?: number;
|
|
67
|
+
query?: string;
|
|
68
|
+
recent_first?: boolean;
|
|
69
|
+
role?: "assistant" | "developer" | "system" | "tool" | "user" | null;
|
|
70
|
+
tool_name?: string | null;
|
|
71
|
+
tool_namespace?: string | null;
|
|
72
|
+
window_id?: string | null;
|
|
73
|
+
}, api: import("@earendil-works/pi-durable").ToolExecutionApi<import("@earendil-works/chord").JsonValue>, context: Context): Promise<import("@earendil-works/pi-durable").ToolExecutionResult<import("@earendil-works/chord").JsonValue>>;
|
|
74
|
+
executionHints: {
|
|
75
|
+
discovery: "help";
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
/** Awaited lifecycle refresh via public APIs. Optional index failures are reported
|
|
79
|
+
* by the next ranked search, never by interrupting ordinary generation. Abort propagates. */
|
|
80
|
+
export declare function refreshRankedHistory(conversation: Conversation, ranked: RankedHistory, context: Context): Promise<void>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { Models } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { ConversationId, Harness, InputSubmissionDraft, Storage, TaskId } from "@earendil-works/pi-durable";
|
|
4
|
+
import type { RankedHistory } from "./history-ranker.ts";
|
|
5
|
+
import { type InputResult } from "./lifecycle.ts";
|
|
6
|
+
import type { ContextModelRoles } from "./model-roles.ts";
|
|
7
|
+
import type { ContextRemoteCatalog } from "./remote.ts";
|
|
8
|
+
import type { BeforeRollover } from "./rollover-preparation.ts";
|
|
9
|
+
export type { HistoryRanker, RankedHistory } from "./history-ranker.ts";
|
|
10
|
+
export type { InputResult } from "./lifecycle.ts";
|
|
11
|
+
export type { ContextModelRole, ContextModelRoles } from "./model-roles.ts";
|
|
12
|
+
export { readSavedNotes, type SavedNote } from "./note-store.ts";
|
|
13
|
+
export type { ContextRemoteCatalog } from "./remote.ts";
|
|
14
|
+
export type { BeforeRollover } from "./rollover-preparation.ts";
|
|
15
|
+
export type { TransitionResult, WindowIdentity } from "./window-state.ts";
|
|
16
|
+
/** Policy for model-requested context windows. */
|
|
17
|
+
export type NewContextRequests = "off" | "pi-limits" | "unrestricted";
|
|
18
|
+
/** Install the extension before opening. Bind the same Harness and storage before resuming it. */
|
|
19
|
+
export declare function createContextManagement(options: {
|
|
20
|
+
models: Models;
|
|
21
|
+
/** Include retained fork branches and prioritise native summary search matches. Off by default. */
|
|
22
|
+
archiveHistory?: boolean;
|
|
23
|
+
/** Opt in to retired visible-text ranking with an explicit host-granted index. */
|
|
24
|
+
rankedHistory?: RankedHistory;
|
|
25
|
+
/** Await connected-service refresh before a window cut. Interrupted effects are not replayed. */
|
|
26
|
+
beforeRollover?: BeforeRollover;
|
|
27
|
+
/** User-configured roles, authorised for this conversation. Global paths remain host-owned. */
|
|
28
|
+
modelRoles?: ContextModelRoles;
|
|
29
|
+
/** Opt in to a host-owned stable family and agent namespace. No local fallback. */
|
|
30
|
+
remote?: ContextRemoteCatalog;
|
|
31
|
+
now?: () => number;
|
|
32
|
+
/** Agent-requested rollover policy. `pi-limits` requires the host's history-eligibility signal below. */
|
|
33
|
+
newContextRequests?: NewContextRequests;
|
|
34
|
+
/** Read-only host signal for history eligible for a new window; errors fail closed. */
|
|
35
|
+
hasEligibleContextHistory?: (conversationId: ConversationId, context: Context) => boolean | Promise<boolean>;
|
|
36
|
+
automaticIdleRollover?: boolean;
|
|
37
|
+
/** Minutes of observed idle time. Explicit policy overrides the legacy boolean. */
|
|
38
|
+
idleRolloverMinutes?: 0 | 5 | 15 | 25 | 55 | 115 | 355 | 715;
|
|
39
|
+
/** Once-per-window suggestion for contexts larger than 272k. Zero disables it. */
|
|
40
|
+
taskBoundaryReminderTokens?: number;
|
|
41
|
+
}): {
|
|
42
|
+
extension: import("@earendil-works/pi-durable").Extension<import("@earendil-works/pi-durable").ToolRegistration>;
|
|
43
|
+
bind(harness: Harness, storage: Storage): void;
|
|
44
|
+
/** Release only after draining host calls and closing this Harness and its task invocations. */
|
|
45
|
+
unbind(harness: Harness): void;
|
|
46
|
+
submit(id: ConversationId, draft: InputSubmissionDraft, context: Context): Promise<TaskId<InputResult>>;
|
|
47
|
+
newContext(id: ConversationId, context: Context): Promise<TaskId<import("./window-state.ts").TransitionResult>>;
|
|
48
|
+
retry(id: ConversationId, context: Context): Promise<void>;
|
|
49
|
+
status(id: ConversationId, context: Context): Promise<{
|
|
50
|
+
window: Readonly<{
|
|
51
|
+
window?: import("./window-state.ts").WindowIdentity;
|
|
52
|
+
entry?: import("@earendil-works/pi-durable").EntryId;
|
|
53
|
+
settlement?: {
|
|
54
|
+
observedAt: number;
|
|
55
|
+
userEntry: import("@earendil-works/pi-durable").EntryId | null;
|
|
56
|
+
completed?: boolean;
|
|
57
|
+
};
|
|
58
|
+
transition?: TaskId<import("./window-state.ts").TransitionResult>;
|
|
59
|
+
reminded: number;
|
|
60
|
+
taskBoundaryReminded?: string;
|
|
61
|
+
}> | undefined;
|
|
62
|
+
admission: Readonly<{
|
|
63
|
+
queue: TaskId[];
|
|
64
|
+
cancelled: TaskId[];
|
|
65
|
+
retry: number;
|
|
66
|
+
blocked?: {
|
|
67
|
+
task: TaskId;
|
|
68
|
+
reason: string;
|
|
69
|
+
};
|
|
70
|
+
}> | undefined;
|
|
71
|
+
}>;
|
|
72
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import { type ConversationId, type TaskId, type Tx } from "@earendil-works/pi-durable";
|
|
3
|
+
import { type ContextHost } from "./lifecycle.ts";
|
|
4
|
+
/** Start trusted component instructions only, never user or peer content. */
|
|
5
|
+
export declare function startInstruction(tx: Tx, id: ConversationId, owner: TaskId, kind: string, content: string, now: number): Promise<TaskId<import("@earendil-works/pi-durable").GenerationResult> | undefined>;
|
|
6
|
+
export declare function waitInstruction(host: ContextHost, id: ConversationId, generation: TaskId, context: Context): Promise<void>;
|
|
7
|
+
/** The native run can hand over and fail before its observer runs, including on reopen.
|
|
8
|
+
* Task receipts, not the first generation or an earlier assistant entry, prove settlement.
|
|
9
|
+
* Reject failures throughout the claimed interval, also when direct input bypassed admission.
|
|
10
|
+
*/
|
|
11
|
+
export declare function instructionOutcome(tx: Tx, id: ConversationId, first: TaskId): Promise<"aborted" | "completed" | "failed" | undefined>;
|
|
12
|
+
/** Recovery can find the first generation done but a successor failed or aborted. */
|
|
13
|
+
export declare function instructionAnswer(tx: Tx, id: ConversationId): Promise<import("@earendil-works/pi-durable").EntryId | undefined>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { Conversation, ConversationId, DocumentWatch, Harness, JsonObject, Storage, SubmissionId, SubmissionSettlement, TaskId, Tx } from "@earendil-works/pi-durable";
|
|
3
|
+
import { type TransitionResult } from "./window-state.ts";
|
|
4
|
+
export declare const INPUT_TASK = "howaboua.context.input";
|
|
5
|
+
export declare const ROLLOVER_TASK = "howaboua.context.rollover";
|
|
6
|
+
export type ContextHost = {
|
|
7
|
+
harness: Harness;
|
|
8
|
+
storage: Storage;
|
|
9
|
+
};
|
|
10
|
+
export type InputResult = SubmissionSettlement & {
|
|
11
|
+
submission: SubmissionId;
|
|
12
|
+
};
|
|
13
|
+
export declare const Admission: import("@earendil-works/pi-durable").ConversationDocToken<{
|
|
14
|
+
queue: TaskId[];
|
|
15
|
+
cancelled: TaskId[];
|
|
16
|
+
retry: number;
|
|
17
|
+
blocked?: {
|
|
18
|
+
task: TaskId;
|
|
19
|
+
reason: string;
|
|
20
|
+
};
|
|
21
|
+
}>;
|
|
22
|
+
export declare const InputReceipt: import("@earendil-works/pi-durable").ConversationDocFamilyToken<{
|
|
23
|
+
task?: TaskId<InputResult>;
|
|
24
|
+
}, null>;
|
|
25
|
+
export declare const Continuation: import("@earendil-works/pi-durable").ConversationDocFamilyToken<{
|
|
26
|
+
task?: TaskId<TransitionResult>;
|
|
27
|
+
}, null>;
|
|
28
|
+
/** Cancellation must wake every held input before the rollover releases its claim. */
|
|
29
|
+
export declare function cancelHeldInputs(tx: Tx, id: ConversationId): Promise<void>;
|
|
30
|
+
export declare function releaseAdmission(tx: Tx, id: ConversationId, task: TaskId): Promise<void>;
|
|
31
|
+
export declare function conversation(host: ContextHost, id: ConversationId, context: Context): Promise<Conversation>;
|
|
32
|
+
export declare function initializeWindow(tx: Tx, id: ConversationId, now: number): Promise<void>;
|
|
33
|
+
/** Foreground cells also have to settle; waiting on the managed input itself would deadlock. */
|
|
34
|
+
export declare function foreignTasks(tx: Tx, id: ConversationId): Promise<TaskId[]>;
|
|
35
|
+
export declare function nativeIdle(tx: Tx, id: ConversationId): Promise<boolean>;
|
|
36
|
+
/** Own the watch through cancellation, including a change between acquisition and start. */
|
|
37
|
+
export declare function until<T extends JsonObject>(watch: DocumentWatch<T> | undefined, predicate: (value: Readonly<T>) => boolean, signal: AbortSignal): Promise<void>;
|
|
38
|
+
/** Withdraw only the owned input. A placed input requires stopping its native generation. */
|
|
39
|
+
export declare function cancelInput(host: ContextHost, id: ConversationId, requestId: string, context: Context): Promise<void>;
|
|
40
|
+
export declare function cancelPlacedInput(host: ContextHost, conv: Conversation, id: SubmissionId, context: Context): Promise<void>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { Models, ModelThinkingLevel } from "@earendil-works/pi-ai";
|
|
3
|
+
import { type Agent, type ConversationId, type ModelRef, type Tx } from "@earendil-works/pi-durable";
|
|
4
|
+
/** User-owned role preferences. The host owns configuration paths and access policy. */
|
|
5
|
+
export type ContextModelRole = {
|
|
6
|
+
id: string;
|
|
7
|
+
description: string;
|
|
8
|
+
provider: string;
|
|
9
|
+
model: string;
|
|
10
|
+
reasoning: ModelThinkingLevel;
|
|
11
|
+
};
|
|
12
|
+
/** Read-only catalogue. Preparation snapshots definitions before awaiting provider access. */
|
|
13
|
+
export type ContextModelRoles = (conversationId: ConversationId, context: Context) => readonly ContextModelRole[] | Promise<readonly ContextModelRole[]>;
|
|
14
|
+
export type PreparedRole = {
|
|
15
|
+
id: string;
|
|
16
|
+
model: ModelRef;
|
|
17
|
+
thinkingLevel: ModelThinkingLevel;
|
|
18
|
+
source: {
|
|
19
|
+
model: ModelRef | null;
|
|
20
|
+
thinkingLevel: ModelThinkingLevel;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
export declare function renderModelRoles(catalog: ContextModelRoles | undefined, id: ConversationId, context: Context): Promise<string>;
|
|
24
|
+
export declare function prepareRole(catalog: ContextModelRoles | undefined, id: string, conversationId: ConversationId, source: Agent, models: Models, context: Context): Promise<PreparedRole>;
|
|
25
|
+
/** Checked in the same transaction as the model selection and window cut. */
|
|
26
|
+
export declare function roleSourceMatches(tx: Tx, id: ConversationId, role: PreparedRole): Promise<boolean>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { HistoryRanker } from "./history-ranker.ts";
|
|
2
|
+
/** Native capability: path must be an explicitly granted absolute host index path.
|
|
3
|
+
* Requires Node >=22.19 with node:sqlite/FTS5; never imported by the portable entry.
|
|
4
|
+
* Close after draining tool calls. No worker survives process restart.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createNodeHistoryRanker(options: {
|
|
7
|
+
path: string;
|
|
8
|
+
}): HistoryRanker & {
|
|
9
|
+
close(): Promise<void>;
|
|
10
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { ConversationId, DocumentReader, EntryId, Storage, TaskId, Tx } from "@earendil-works/pi-durable";
|
|
3
|
+
export type NoteMetadata = {
|
|
4
|
+
path: string;
|
|
5
|
+
bytes: number;
|
|
6
|
+
lines: number;
|
|
7
|
+
createdAt: number;
|
|
8
|
+
updatedAt: number;
|
|
9
|
+
};
|
|
10
|
+
type NoteFreshness = {
|
|
11
|
+
windowId: string;
|
|
12
|
+
userEntry: EntryId | null;
|
|
13
|
+
taskId: TaskId;
|
|
14
|
+
updatedAt: number;
|
|
15
|
+
};
|
|
16
|
+
type NotesCatalog = {
|
|
17
|
+
files: NoteMetadata[];
|
|
18
|
+
totalBytes: number;
|
|
19
|
+
lastWrite?: NoteFreshness;
|
|
20
|
+
lastAttempt?: TaskId;
|
|
21
|
+
};
|
|
22
|
+
/** The sole authority for metadata and freshness. Bodies live in per-path documents. */
|
|
23
|
+
export declare const NotesState: import("@earendil-works/pi-durable").RewindableConversationDocToken<NotesCatalog>;
|
|
24
|
+
export type SavedNote = {
|
|
25
|
+
path: string;
|
|
26
|
+
text: string;
|
|
27
|
+
createdAt: number;
|
|
28
|
+
updatedAt: number;
|
|
29
|
+
};
|
|
30
|
+
/** Supply an immutable storage view for coherent catalog/body reads. Storage stays caller-owned. */
|
|
31
|
+
export declare function readSavedNotes(storage: Storage, conversationId: ConversationId, context: Context): Promise<SavedNote[]>;
|
|
32
|
+
export declare function readNoteText(tx: Tx, id: ConversationId, path: string): Promise<string>;
|
|
33
|
+
export declare function notesAreFresh(tx: Tx, id: ConversationId): Promise<boolean>;
|
|
34
|
+
/** Commit before validation/mutation so a rejected replacement cannot reuse an older save. */
|
|
35
|
+
export declare function beginNoteWrite(tx: Tx, id: ConversationId, taskId: TaskId): Promise<void>;
|
|
36
|
+
/** Receipt, body, catalog and freshness commit together under the original task identity. */
|
|
37
|
+
export declare function mutateNote(tx: Tx, input: {
|
|
38
|
+
conversationId: ConversationId;
|
|
39
|
+
taskId: TaskId;
|
|
40
|
+
path: string;
|
|
41
|
+
text: string;
|
|
42
|
+
append: boolean;
|
|
43
|
+
now: () => number;
|
|
44
|
+
}): Promise<NoteMetadata>;
|
|
45
|
+
export declare function noteMetadata(file: NoteMetadata): {
|
|
46
|
+
path: string;
|
|
47
|
+
bytes: number;
|
|
48
|
+
created_at: string;
|
|
49
|
+
updated_at: string;
|
|
50
|
+
};
|
|
51
|
+
/** Bootstrap lists paths, never loads bodies. */
|
|
52
|
+
export declare function renderNotesThreadHint(reader: DocumentReader, id: ConversationId, context: Context, maxBytes?: number): Promise<string | undefined>;
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { type Storage } from "@earendil-works/pi-durable";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import { type ContextRemoteCatalog } from "./remote.ts";
|
|
4
|
+
export declare function createNotesTool(options: {
|
|
5
|
+
now: () => number;
|
|
6
|
+
storage: () => Storage;
|
|
7
|
+
remote?: ContextRemoteCatalog | undefined;
|
|
8
|
+
}): {
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
parameters: Type.TObject<{
|
|
12
|
+
action: Type.TOptional<Type.TUnion<Type.TLiteral<"append" | "help" | "list" | "read" | "search" | "write">[]>>;
|
|
13
|
+
target: Type.TOptional<Type.TString>;
|
|
14
|
+
agent_name: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
15
|
+
file_order: Type.TOptional<Type.TUnion<[Type.TLiteral<"ascending">, Type.TLiteral<"descending">]>>;
|
|
16
|
+
file_order_by: Type.TOptional<Type.TUnion<[Type.TLiteral<"name">, Type.TLiteral<"created_at">, Type.TLiteral<"updated_at">]>>;
|
|
17
|
+
limit: Type.TOptional<Type.TInteger>;
|
|
18
|
+
max_matches_per_file: Type.TOptional<Type.TInteger>;
|
|
19
|
+
path: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
20
|
+
query: Type.TOptional<Type.TString>;
|
|
21
|
+
recent_file_first: Type.TOptional<Type.TBoolean>;
|
|
22
|
+
start_line: Type.TOptional<Type.TUnion<[Type.TInteger, Type.TNull]>>;
|
|
23
|
+
stop_line: Type.TOptional<Type.TUnion<[Type.TInteger, Type.TNull]>>;
|
|
24
|
+
limit_chars: Type.TOptional<Type.TInteger>;
|
|
25
|
+
offset_chars: Type.TOptional<Type.TInteger>;
|
|
26
|
+
text: Type.TOptional<Type.TString>;
|
|
27
|
+
}>;
|
|
28
|
+
constrainedSampling?: false | import("@earendil-works/pi-ai").ConstrainedSamplingConfig;
|
|
29
|
+
replay?: "safe" | "unsafe";
|
|
30
|
+
executionMode?: import("@earendil-works/pi-durable").ToolExecutionMode;
|
|
31
|
+
prepareArguments?(args: unknown): {
|
|
32
|
+
action?: never;
|
|
33
|
+
target?: string;
|
|
34
|
+
agent_name?: string | null;
|
|
35
|
+
file_order?: "ascending" | "descending";
|
|
36
|
+
file_order_by?: "created_at" | "name" | "updated_at";
|
|
37
|
+
limit?: number;
|
|
38
|
+
max_matches_per_file?: number;
|
|
39
|
+
path?: string | null;
|
|
40
|
+
query?: string;
|
|
41
|
+
recent_file_first?: boolean;
|
|
42
|
+
start_line?: number | null;
|
|
43
|
+
stop_line?: number | null;
|
|
44
|
+
limit_chars?: number;
|
|
45
|
+
offset_chars?: number;
|
|
46
|
+
text?: string;
|
|
47
|
+
};
|
|
48
|
+
structuredOutputSchema?: Type.TSchema;
|
|
49
|
+
callers?: readonly ("model" | "tools")[];
|
|
50
|
+
deferred?: true;
|
|
51
|
+
outputLimits?: {
|
|
52
|
+
readonly maxBytes?: number;
|
|
53
|
+
readonly maxLines?: number;
|
|
54
|
+
readonly retain?: "head" | "tail";
|
|
55
|
+
};
|
|
56
|
+
execute(args: {
|
|
57
|
+
action?: never;
|
|
58
|
+
target?: string;
|
|
59
|
+
agent_name?: string | null;
|
|
60
|
+
file_order?: "ascending" | "descending";
|
|
61
|
+
file_order_by?: "created_at" | "name" | "updated_at";
|
|
62
|
+
limit?: number;
|
|
63
|
+
max_matches_per_file?: number;
|
|
64
|
+
path?: string | null;
|
|
65
|
+
query?: string;
|
|
66
|
+
recent_file_first?: boolean;
|
|
67
|
+
start_line?: number | null;
|
|
68
|
+
stop_line?: number | null;
|
|
69
|
+
limit_chars?: number;
|
|
70
|
+
offset_chars?: number;
|
|
71
|
+
text?: string;
|
|
72
|
+
}, api: import("@earendil-works/pi-durable").ToolExecutionApi<import("@earendil-works/chord").JsonValue>, context: import("@earendil-works/chord").Context): Promise<import("@earendil-works/pi-durable").ToolExecutionResult<import("@earendil-works/chord").JsonValue>>;
|
|
73
|
+
executionHints: {
|
|
74
|
+
discovery: "help";
|
|
75
|
+
};
|
|
76
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { Models } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { ConversationId, Harness, Storage } from "@earendil-works/pi-durable";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
import type { RankedHistory } from "./history-ranker.ts";
|
|
6
|
+
import { type ContextModelRoles } from "./model-roles.ts";
|
|
7
|
+
export declare function createContextPolicy(options: {
|
|
8
|
+
models: Models;
|
|
9
|
+
taskBoundaryReminderTokens: number;
|
|
10
|
+
newContextRequests: "off" | "pi-limits" | "unrestricted";
|
|
11
|
+
modelRoles?: ContextModelRoles | undefined;
|
|
12
|
+
archiveHistory?: boolean;
|
|
13
|
+
rankedHistory?: RankedHistory | undefined;
|
|
14
|
+
host: () => {
|
|
15
|
+
harness: Harness;
|
|
16
|
+
storage: Storage;
|
|
17
|
+
};
|
|
18
|
+
now: () => number;
|
|
19
|
+
ensureWindow: (id: ConversationId, context: Context) => Promise<void>;
|
|
20
|
+
}): {
|
|
21
|
+
sections: import("@earendil-works/pi-durable").PromptSection<import("@earendil-works/pi-durable").ToolRegistration>[];
|
|
22
|
+
hooks: import("@earendil-works/pi-durable").HookRegistration[];
|
|
23
|
+
remainingTool: import("@earendil-works/pi-durable").ToolRegistration<Type.TObject<{}>, import("@earendil-works/chord").JsonValue>;
|
|
24
|
+
};
|