ds4-context-engine 0.1.2 → 0.2.0-beta.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,72 @@
1
+ import type { SessionEntry } from "@earendil-works/pi-coding-agent";
2
+ import {
3
+ RANKING_FEEDBACK_CUSTOM_ENTRY_TYPE,
4
+ parseRankingFeedback,
5
+ rankingTrainingSample,
6
+ type RankingFeedbackEntry,
7
+ type RankingTrainingSample,
8
+ } from "ds4-context-core/ranking/learned-ranker";
9
+
10
+ const MAX_RANKING_LABEL_WARNINGS = 100;
11
+
12
+ export interface RankingLabelProjection {
13
+ entries: RankingFeedbackEntry[];
14
+ samples: RankingTrainingSample[];
15
+ malformedEntries: number;
16
+ duplicateEntries: number;
17
+ warnings: string[];
18
+ }
19
+
20
+ export function projectRankingLabels(
21
+ entries: readonly SessionEntry[],
22
+ maxSamples: number,
23
+ ): RankingLabelProjection {
24
+ if (!Number.isSafeInteger(maxSamples) || maxSamples < 1) {
25
+ throw new Error("Ranking label projection limit must be a positive integer");
26
+ }
27
+ const accepted: RankingFeedbackEntry[] = [];
28
+ let acceptedCursor = 0;
29
+ const seen = new Set<string>();
30
+ const warnings: string[] = [];
31
+ let malformedEntries = 0;
32
+ let duplicateEntries = 0;
33
+
34
+ for (const entry of entries) {
35
+ if (entry.type !== "custom" || entry.customType !== RANKING_FEEDBACK_CUSTOM_ENTRY_TYPE) continue;
36
+ const parsed = parseRankingFeedback(entry.data);
37
+ if (!parsed) {
38
+ malformedEntries++;
39
+ if (warnings.length < MAX_RANKING_LABEL_WARNINGS) {
40
+ warnings.push(`Ignored malformed ranking feedback custom entry ${entry.id}`);
41
+ }
42
+ continue;
43
+ }
44
+ if (seen.has(parsed.feedbackId)) {
45
+ duplicateEntries++;
46
+ if (warnings.length < MAX_RANKING_LABEL_WARNINGS) {
47
+ warnings.push(`Ignored duplicate ranking feedback ${parsed.feedbackId} at ${entry.id}`);
48
+ }
49
+ continue;
50
+ }
51
+ if (accepted.length < maxSamples) {
52
+ seen.add(parsed.feedbackId);
53
+ accepted.push(parsed);
54
+ } else {
55
+ const replaced = accepted[acceptedCursor];
56
+ if (replaced) seen.delete(replaced.feedbackId);
57
+ seen.add(parsed.feedbackId);
58
+ accepted[acceptedCursor] = parsed;
59
+ acceptedCursor = (acceptedCursor + 1) % maxSamples;
60
+ }
61
+ }
62
+
63
+ const bounded = accepted
64
+ .sort((left, right) => left.createdAt - right.createdAt || left.feedbackId.localeCompare(right.feedbackId));
65
+ return {
66
+ entries: bounded,
67
+ samples: bounded.map(rankingTrainingSample),
68
+ malformedEntries,
69
+ duplicateEntries,
70
+ warnings,
71
+ };
72
+ }
@@ -0,0 +1,86 @@
1
+ import { realpathSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ import {
4
+ RUNTIME_CAPABILITY_IDS,
5
+ RUNTIME_HISTORY_SCHEMA_VERSION,
6
+ buildCanonicalToolAtomicGroups,
7
+ negotiateRuntimeCapabilities,
8
+ validateRuntimeHistorySnapshot,
9
+ type RuntimeCapabilityDeclaration,
10
+ type RuntimeCapabilityNegotiation,
11
+ type RuntimeCapabilityRequest,
12
+ type RuntimeHistorySnapshot,
13
+ } from "ds4-context-core/adapter/runtime-adapter";
14
+ import { sha256 } from "ds4-context-core/shared/hash";
15
+ import { stableStringify } from "ds4-context-core/shared/stable-json";
16
+ import {
17
+ sessionEntryToContextMessages,
18
+ type ExtensionContext,
19
+ } from "@earendil-works/pi-coding-agent";
20
+ import { toCanonicalMessage } from "./message-converter.ts";
21
+
22
+ export const PI_RUNTIME_ID = "pi";
23
+
24
+ export const PI_RUNTIME_CAPABILITIES: readonly RuntimeCapabilityDeclaration[] = [
25
+ { id: "compaction", supported: true, version: "pi-session-compact-v1" },
26
+ { id: "provider-continuation", supported: true, version: "pi-openai-responses-continuation-v1" },
27
+ { id: "embeddings", supported: true, version: "embedding-port-v1" },
28
+ {
29
+ id: "local-kv-reuse",
30
+ supported: false,
31
+ reason: "Pi does not expose reusable local prefix or KV state",
32
+ },
33
+ ];
34
+
35
+ function canonicalProjectRoot(path: string): string {
36
+ const absolute = resolve(path);
37
+ try {
38
+ return realpathSync.native(absolute);
39
+ } catch {
40
+ return absolute;
41
+ }
42
+ }
43
+
44
+ export function negotiatePiRuntimeCapabilities(
45
+ requested: readonly RuntimeCapabilityRequest[] = RUNTIME_CAPABILITY_IDS.map((id) => ({ id })),
46
+ ): RuntimeCapabilityNegotiation {
47
+ return negotiateRuntimeCapabilities(PI_RUNTIME_CAPABILITIES, requested);
48
+ }
49
+
50
+ /**
51
+ * Projects Pi's active, compaction-aware branch without modifying canonical JSONL.
52
+ * The snapshot is suitable for shared adapter-contract validation and diagnostics.
53
+ */
54
+ export function snapshotPiRuntimeHistory(
55
+ ctx: Pick<ExtensionContext, "cwd" | "isProjectTrusted" | "sessionManager">,
56
+ ): RuntimeHistorySnapshot {
57
+ if (!ctx.isProjectTrusted()) {
58
+ throw new Error("Pi project root is not trusted");
59
+ }
60
+ const sessionId = ctx.sessionManager.getSessionId();
61
+ const entries = ctx.sessionManager.buildContextEntries();
62
+ const messages = entries.flatMap((entry) => sessionEntryToContextMessages(entry).map((message) =>
63
+ toCanonicalMessage({
64
+ sessionId,
65
+ entryId: entry.id,
66
+ entryTimestamp: entry.timestamp,
67
+ message,
68
+ })));
69
+ const snapshot: RuntimeHistorySnapshot = {
70
+ schemaVersion: RUNTIME_HISTORY_SCHEMA_VERSION,
71
+ runtimeId: PI_RUNTIME_ID,
72
+ sessionId,
73
+ revision: sha256(stableStringify({
74
+ leafId: ctx.sessionManager.getLeafId(),
75
+ entries,
76
+ })),
77
+ projectRoot: canonicalProjectRoot(ctx.cwd),
78
+ messages,
79
+ toolAtomicGroups: buildCanonicalToolAtomicGroups(messages),
80
+ };
81
+ const issues = validateRuntimeHistorySnapshot(snapshot);
82
+ if (issues.length > 0) {
83
+ throw new Error(`Pi canonical history snapshot failed validation: ${issues.length} issue(s)`);
84
+ }
85
+ return snapshot;
86
+ }
@@ -1,5 +1,5 @@
1
- export const EXTENSION_VERSION = "0.1.2";
1
+ export const EXTENSION_VERSION = "0.2.0-beta.1";
2
2
  export const SUPPORTED_PI_VERSION = "0.84.3";
3
3
  export const OBSERVER_PLANNER_VERSION = "observer-model-aware-v1";
4
- export const PLANNER_VERSION = "managed-native-continuation-v1";
5
- export const POLICY_VERSION = "8";
4
+ export const PLANNER_VERSION = "managed-learned-ranking-v1";
5
+ export const POLICY_VERSION = "9";