minicode-ai 0.11.0 → 0.13.0
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/CHANGELOG.md +98 -0
- package/LICENSE +21 -21
- package/README.md +4 -4
- package/cli/args.ts +46 -0
- package/cli/commands/acp.ts +163 -10
- package/cli/commands/config.ts +33 -18
- package/cli/commands/daemon.ts +515 -0
- package/cli/commands/exec.ts +145 -53
- package/cli/commands/openwork.ts +986 -0
- package/cli/commands/sessions.ts +4 -1
- package/cli/commands/stats.ts +34 -4
- package/cli/commands.ts +217 -2
- package/cli/index.ts +146 -14
- package/cli/recovery-warning.ts +115 -0
- package/cli/router.ts +32 -1
- package/cli/setup.ts +2332 -171
- package/cli/tui.ts +113 -7
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +489 -0
- package/docs/OUTPUT_EVENT_MODEL.md +289 -0
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +391 -0
- package/docs/OUTPUT_PROTOCOL_SPEC.md +285 -0
- package/docs/OUTPUT_RENDERING_SPEC.md +248 -0
- package/docs/OUTPUT_UX_RULES.md +449 -0
- package/docs/PLAN_UIUX_V6.md +2 -2
- package/docs/README.md +4 -3
- package/docs/TERMINAL_CONTRACT.md +278 -66
- package/docs/architecture.md +89 -7
- package/docs/changelog.md +1 -1
- package/docs/choosing-mode.md +2 -2
- package/docs/cli.md +9 -5
- package/docs/concepts.md +1 -1
- package/docs/contributing.md +9 -2
- package/docs/environment.md +6 -2
- package/docs/exec.md +46 -5
- package/docs/memory-sessions.md +2 -0
- package/docs/policy-sandbox.md +1 -1
- package/docs/pricing-budget.md +1 -1
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +6 -2
- package/docs/security-model.md +1 -1
- package/docs/security.md +1 -1
- package/docs/skills.md +2 -2
- package/docs/terminal.md +9 -5
- package/docs/tools.md +1 -1
- package/package.json +35 -5
- package/src/app/session.ts +261 -15
- package/src/app/tool-layer.ts +94 -7
- package/src/daemon/approvals.ts +361 -0
- package/src/daemon/capability.ts +145 -0
- package/src/daemon/children.ts +142 -0
- package/src/daemon/client.ts +267 -0
- package/src/daemon/control.ts +350 -0
- package/src/daemon/discovery.ts +234 -0
- package/src/daemon/feed.ts +155 -0
- package/src/daemon/framing.ts +94 -0
- package/src/daemon/host.ts +316 -0
- package/src/daemon/protocol.ts +217 -0
- package/src/daemon/resource.ts +170 -0
- package/src/daemon/sanitize.ts +57 -0
- package/src/daemon/server.ts +627 -0
- package/src/daemon/store.ts +317 -0
- package/src/daemon/subscription.ts +270 -0
- package/src/daemon/telemetry.ts +81 -0
- package/src/hooks/run.ts +12 -1
- package/src/lib/atomic-write.ts +29 -3
- package/src/lib/db-path.ts +26 -0
- package/src/lib/keystore.ts +544 -42
- package/src/lib/net.ts +43 -1
- package/src/lib/safe-open.ts +116 -1
- package/src/lib/session-id.ts +15 -0
- package/src/lsp/client.ts +123 -5
- package/src/mcp/client.ts +139 -17
- package/src/mcp/server.ts +177 -50
- package/src/mcp/transport.ts +16 -19
- package/src/memory/vector.ts +770 -55
- package/src/policy/allowlist.ts +14 -2
- package/src/policy/bash-guard.ts +105 -10
- package/src/policy/compaction.ts +82 -5
- package/src/policy/consequence-engine.ts +220 -0
- package/src/policy/context.ts +132 -0
- package/src/policy/executor.ts +187 -121
- package/src/policy/no-progress.ts +559 -0
- package/src/policy/permission.ts +4 -8
- package/src/policy/pricing.ts +17 -3
- package/src/policy/scrub.ts +69 -2
- package/src/policy/tool-contract.ts +515 -0
- package/src/policy/usage.ts +310 -24
- package/src/policy/verifier.ts +6 -3
- package/src/policy/work-mode.ts +182 -0
- package/src/presentation/adapter.ts +533 -26
- package/src/presentation/events.ts +151 -0
- package/src/presentation/model.ts +180 -27
- package/src/presentation/projection.ts +858 -0
- package/src/presentation/reducer.ts +645 -310
- package/src/presentation/store.ts +12 -7
- package/src/providers/anthropic.ts +18 -10
- package/src/providers/responses.ts +17 -7
- package/src/runtime/capability.ts +409 -0
- package/src/runtime/child-execution.ts +274 -0
- package/src/runtime/composition.ts +958 -0
- package/src/runtime/dispatch.ts +591 -0
- package/src/runtime/execution-backend.ts +632 -0
- package/src/runtime/execution-events.ts +272 -0
- package/src/runtime/execution-id.ts +230 -0
- package/src/runtime/execution-journal.ts +926 -0
- package/src/runtime/execution-kernel.ts +456 -0
- package/src/runtime/execution-registry.ts +379 -0
- package/src/runtime/process-supervision.ts +276 -0
- package/src/runtime/production-execution.ts +551 -0
- package/src/runtime/production-runtime.ts +217 -0
- package/src/runtime/recovery-safety.ts +70 -0
- package/src/runtime/recovery.ts +477 -0
- package/src/runtime/runtime-capabilities.ts +179 -0
- package/src/runtime/runtime-diagnostics.ts +141 -0
- package/src/runtime/runtime-host.ts +365 -0
- package/src/runtime/runtime-status-facts.ts +222 -0
- package/src/runtime/runtime-status-provider.ts +74 -0
- package/src/runtime/runtime-status.ts +459 -0
- package/src/runtime/runtime-task-admission.ts +101 -0
- package/src/runtime/supervisor.ts +444 -0
- package/src/session/authority.ts +168 -0
- package/src/session/checkpoint.ts +88 -1
- package/src/session/context-adapter.ts +251 -0
- package/src/session/context-assembly.ts +242 -0
- package/src/session/context-identity.ts +389 -0
- package/src/session/context-projection.ts +248 -0
- package/src/session/context-selector.ts +500 -0
- package/src/session/cross-session-adoption.ts +249 -0
- package/src/session/full-history-fold.ts +171 -0
- package/src/session/identity.ts +192 -0
- package/src/session/journal.ts +805 -102
- package/src/session/open-work-intent.ts +732 -0
- package/src/session/open-work.ts +767 -0
- package/src/session/pending-tools.ts +94 -0
- package/src/session/persistence.ts +3569 -127
- package/src/session/runtime-header.ts +208 -0
- package/src/session/shadow-git.ts +13 -5
- package/src/session/verification.ts +1623 -0
- package/src/task/assignment.ts +136 -0
- package/src/task/autonomous-adapter.ts +168 -0
- package/src/task/autonomous-context.ts +630 -0
- package/src/task/autonomous-policy.ts +490 -0
- package/src/task/continuity.ts +179 -0
- package/src/task/execution-cancellation.ts +165 -0
- package/src/task/graph-validate.ts +379 -0
- package/src/task/graph.ts +305 -0
- package/src/task/identity.ts +230 -0
- package/src/task/model.ts +417 -0
- package/src/task/production-scheduler.ts +293 -0
- package/src/task/readiness.ts +229 -0
- package/src/task/scheduler-observability.ts +387 -0
- package/src/task/scheduler.ts +1197 -0
- package/src/task/scheduling-policy.ts +165 -0
- package/src/task/session-authority.ts +148 -0
- package/src/task/session-ownership.ts +211 -0
- package/src/task/store.ts +2887 -0
- package/src/task/sync.ts +343 -0
- package/src/task/trigger.ts +260 -0
- package/src/tools/bash.ts +377 -211
- package/src/tools/code_run.ts +1 -1
- package/src/tools/delete_file.ts +1 -1
- package/src/tools/edit.ts +33 -6
- package/src/tools/evidence.ts +30 -0
- package/src/tools/git.ts +4 -4
- package/src/tools/glob.ts +12 -3
- package/src/tools/grep.ts +1 -1
- package/src/tools/lsp.ts +4 -4
- package/src/tools/mcp_call.ts +11 -4
- package/src/tools/memory.ts +153 -18
- package/src/tools/move_file.ts +1 -1
- package/src/tools/patch.ts +1 -1
- package/src/tools/read_file.ts +1 -1
- package/src/tools/read_image.ts +1 -1
- package/src/tools/submit_result.ts +1 -1
- package/src/tools/task.ts +437 -41
- package/src/tools/todo.ts +662 -35
- package/src/tools/web_fetch.ts +4 -2
- package/src/tools/web_search.ts +9 -3
- package/src/tools/write_file.ts +1 -1
- package/src/ui/assistant/simple.ts +123 -50
- package/src/ui/assistant/turn-status.ts +17 -3
- package/src/ui/contract.ts +184 -0
- package/src/ui/footer.ts +54 -18
- package/src/ui/i18n/en.ts +62 -6
- package/src/ui/i18n/id.ts +62 -6
- package/src/ui/input/prompt-engine.ts +120 -25
- package/src/ui/render/errors.ts +39 -1
- package/src/ui/render/markdown-table.ts +455 -0
- package/src/ui/render/markdown.ts +22 -1
- package/src/ui/render/table-grid.ts +184 -0
- package/src/ui/render/table.ts +15 -76
- package/src/ui/runtime/screen.ts +7 -2
- package/src/ui/runtime/statusline.ts +25 -6
- package/src/ui/tui/app.ts +598 -133
- package/src/ui/tui/transcript.ts +531 -89
- package/vendor/minicore/VENDOR.md +1 -1
- package/vendor/minicore/src/core/budget.ts +25 -25
- package/vendor/minicore/src/core/compact.ts +75 -75
- package/vendor/minicore/src/core/errors.ts +59 -59
- package/vendor/minicore/src/core/events.ts +73 -73
- package/vendor/minicore/src/core/executor.ts +139 -139
- package/vendor/minicore/src/core/history.ts +31 -31
- package/vendor/minicore/src/core/index.ts +41 -41
- package/vendor/minicore/src/core/loop.ts +355 -355
- package/vendor/minicore/src/core/permission.ts +22 -22
- package/vendor/minicore/src/core/provider.ts +39 -39
- package/vendor/minicore/src/core/recovery.ts +35 -35
- package/vendor/minicore/src/core/session.ts +327 -327
- package/vendor/minicore/src/core/snapshot.ts +146 -146
- package/vendor/minicore/src/core/tokens.ts +107 -107
- package/vendor/minicore/src/core/tool.ts +115 -115
- package/vendor/minicore/src/core/types.ts +50 -50
- package/vendor/minicore/src/providers/openai-compat.ts +417 -417
- package/AGENTS.md +0 -45
- package/PLAN.md +0 -491
- package/docs/ARCHITECTURE.html +0 -1248
- package/docs/CONTROL-PLANE-MAP.md +0 -139
- package/docs/HARNESS.md +0 -150
- package/docs/SUMMARY.md +0 -52
- package/docs/UI_RENDER_PIPELINE.md +0 -165
- package/docs/USAGE.md +0 -11
|
@@ -62,6 +62,37 @@ export interface CheckpointManifest {
|
|
|
62
62
|
checkpoints: Checkpoint[]
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
/**
|
|
66
|
+
* [Audit #2 A.2] Normalisasi manifest korup: `JSON.parse` yang sukses belumlah
|
|
67
|
+
* berarti strukturnya valid. `checkpoints` yang bukan array atau `currentIndex`
|
|
68
|
+
* di luar batas akan memicu `TypeError` saat undo/redo mengindeks
|
|
69
|
+
* `manifest.checkpoints[manifest.currentIndex]`. Bentuk tak valid → jatuh ke
|
|
70
|
+
* state kosong yang aman; pointer di luar batas di-clamp ke `[-1, len-1]`.
|
|
71
|
+
*/
|
|
72
|
+
function normalizeCheckpointManifest(
|
|
73
|
+
parsed: unknown,
|
|
74
|
+
fallbackSessionId: string,
|
|
75
|
+
): CheckpointManifest {
|
|
76
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
77
|
+
return { sessionId: sanitizeSessionId(fallbackSessionId), currentIndex: -1, checkpoints: [] }
|
|
78
|
+
}
|
|
79
|
+
const m = parsed as Partial<CheckpointManifest>
|
|
80
|
+
const checkpoints = Array.isArray(m.checkpoints) ? m.checkpoints : []
|
|
81
|
+
let currentIndex =
|
|
82
|
+
typeof m.currentIndex === "number" && Number.isInteger(m.currentIndex) ? m.currentIndex : -1
|
|
83
|
+
if (currentIndex < -1) currentIndex = -1
|
|
84
|
+
if (currentIndex >= checkpoints.length) currentIndex = checkpoints.length - 1
|
|
85
|
+
if (checkpoints.length === 0) currentIndex = -1
|
|
86
|
+
return {
|
|
87
|
+
sessionId:
|
|
88
|
+
typeof m.sessionId === "string" && m.sessionId.trim() !== ""
|
|
89
|
+
? m.sessionId
|
|
90
|
+
: sanitizeSessionId(fallbackSessionId),
|
|
91
|
+
currentIndex,
|
|
92
|
+
checkpoints,
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
65
96
|
function getCheckpointDir(sessionId: string, cwd: string = process.cwd()): string {
|
|
66
97
|
return resolve(cwd, ".minicode", "checkpoints", sanitizeSessionId(sessionId))
|
|
67
98
|
}
|
|
@@ -70,6 +101,62 @@ function getManifestPath(sessionId: string, cwd: string = process.cwd()): string
|
|
|
70
101
|
return join(getCheckpointDir(sessionId, cwd), "manifest.json")
|
|
71
102
|
}
|
|
72
103
|
|
|
104
|
+
/**
|
|
105
|
+
* [P14.0] Ringkasan manifest checkpoint, SYNC dan NON-MENULIS.
|
|
106
|
+
*
|
|
107
|
+
* `loadCheckpointManifest` yang async menulis backup `.corrupt.*` saat JSON
|
|
108
|
+
* rusak — itu benar untuk jalur pemulihan sesi, tapi PROYEKSI read-only tidak
|
|
109
|
+
* boleh meninggalkan jejak tulis. Pembaca ini hanya melaporkan: manifest
|
|
110
|
+
* hilang = `exists: false` (kondisi normal sesi tanpa checkpoint), JSON rusak
|
|
111
|
+
* = `corrupt: true` (dilabel jujur, tidak dibackup, tidak dihapus).
|
|
112
|
+
*/
|
|
113
|
+
export interface CheckpointInventoryEntry {
|
|
114
|
+
readonly sessionId: string
|
|
115
|
+
readonly exists: boolean
|
|
116
|
+
readonly corrupt: boolean
|
|
117
|
+
readonly checkpointCount: number
|
|
118
|
+
readonly currentIndex: number
|
|
119
|
+
readonly latestTurn: number | null
|
|
120
|
+
readonly latestTimestamp: string | null
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function readCheckpointInventorySync(
|
|
124
|
+
sessionId: string,
|
|
125
|
+
cwd?: string,
|
|
126
|
+
): CheckpointInventoryEntry {
|
|
127
|
+
const empty: CheckpointInventoryEntry = {
|
|
128
|
+
sessionId,
|
|
129
|
+
exists: false,
|
|
130
|
+
corrupt: false,
|
|
131
|
+
checkpointCount: 0,
|
|
132
|
+
currentIndex: -1,
|
|
133
|
+
latestTurn: null,
|
|
134
|
+
latestTimestamp: null,
|
|
135
|
+
}
|
|
136
|
+
let raw: string
|
|
137
|
+
try {
|
|
138
|
+
raw = require("node:fs").readFileSync(getManifestPath(sessionId, cwd ?? process.cwd()), "utf8")
|
|
139
|
+
} catch {
|
|
140
|
+
return empty
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
const parsed = JSON.parse(raw) as CheckpointManifest
|
|
144
|
+
const checkpoints = Array.isArray(parsed.checkpoints) ? parsed.checkpoints : []
|
|
145
|
+
const latest = checkpoints.length > 0 ? checkpoints[checkpoints.length - 1] : undefined
|
|
146
|
+
return {
|
|
147
|
+
sessionId,
|
|
148
|
+
exists: true,
|
|
149
|
+
corrupt: false,
|
|
150
|
+
checkpointCount: checkpoints.length,
|
|
151
|
+
currentIndex: typeof parsed.currentIndex === "number" ? parsed.currentIndex : -1,
|
|
152
|
+
latestTurn: typeof latest?.turn === "number" ? latest.turn : null,
|
|
153
|
+
latestTimestamp: typeof latest?.timestamp === "string" ? latest.timestamp : null,
|
|
154
|
+
}
|
|
155
|
+
} catch {
|
|
156
|
+
return { ...empty, exists: true, corrupt: true }
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
73
160
|
export async function loadCheckpointManifest(
|
|
74
161
|
sessionId: string,
|
|
75
162
|
cwd?: string,
|
|
@@ -78,7 +165,7 @@ export async function loadCheckpointManifest(
|
|
|
78
165
|
try {
|
|
79
166
|
const raw = await readFile(path, "utf8")
|
|
80
167
|
try {
|
|
81
|
-
return JSON.parse(raw)
|
|
168
|
+
return normalizeCheckpointManifest(JSON.parse(raw), sessionId)
|
|
82
169
|
} catch (e) {
|
|
83
170
|
if (e instanceof SyntaxError) {
|
|
84
171
|
const backup = `${path}.corrupt.${Date.now()}`
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// P3.5 — Runtime Context Adapter (host-side metadata propagation + lifecycle bridge).
|
|
2
|
+
//
|
|
3
|
+
// Menutup dua celah yang diidentifikasi audit P3.5:
|
|
4
|
+
//
|
|
5
|
+
// N1 — Runtime metadata propagation.
|
|
6
|
+
// Pada batas seed runtime, hanya `ContextSelection.messages` yang disalin ke
|
|
7
|
+
// `SessionConfig.initialMessages`; identity/frontier/revision/selectionBasis/
|
|
8
|
+
// freshness/coverage/ModelContextProvenance tidak terbawa. Modul ini menyediakan
|
|
9
|
+
// carrier metadata host-side (RAM, lifecycle-scoped) yang MENGANGKUT metadata itu
|
|
10
|
+
// melewati batas seed — tanpa memasukkannya sebagai pesan model palsu.
|
|
11
|
+
//
|
|
12
|
+
// N2 — Headless compaction bridge.
|
|
13
|
+
// Jembatan durable `context.compacted` hanya ada di presentation adapter.
|
|
14
|
+
// Sesi otonom yang dibuat lewat factory otonom (tanpa adapter) membuat event
|
|
15
|
+
// `context:compacted` tetap in-memory sehingga revision undercount. Modul ini
|
|
16
|
+
// menyediakan jembatan tipis yang MENGAMBIL event bus kernel dan menuliskannya
|
|
17
|
+
// ke jalur durable YANG SUDAH ADA (appendPresentationEvents) — idempoten,
|
|
18
|
+
// berpagar epoch bila epoch dipegang, tanpa store kedua.
|
|
19
|
+
//
|
|
20
|
+
// Batas keras (roadmap ratifikasi + HARD RULES):
|
|
21
|
+
// - TANPA store/cache/sidecar baru; TANPA perubahan vendor/kernel; TANPA schema.
|
|
22
|
+
// - TANPA tulis ke messages/turns/head/run/canonical history.
|
|
23
|
+
// - TANPA fabricate revision; TANPA promosi UNKNOWN→fresh.
|
|
24
|
+
// - ContextStore tetap buffer runtime; canonical tetap satu-satunya authority;
|
|
25
|
+
// P3.1/P2.7 tetap batas publikasi.
|
|
26
|
+
// - Satu-satunya tulis adapter = jembatan presentation-event yang SUDAH ADA
|
|
27
|
+
// (saluran yang sama dipakai presentation adapter hari ini).
|
|
28
|
+
//
|
|
29
|
+
// Catatan jangkauan (audit §11): jalur anak delegate_task SUDAH di-bridge oleh
|
|
30
|
+
// presentation adapter anak (cli/index.ts memasang adapter + flush). Jembatan di
|
|
31
|
+
// sini HANYA untuk sesi tanpa presentation adapter (factory otonom / headless);
|
|
32
|
+
// memasangnya di jalur yang sudah punya adapter akan menggandakan marker.
|
|
33
|
+
|
|
34
|
+
import type { DomainEvent } from "../presentation/events.ts"
|
|
35
|
+
import type {
|
|
36
|
+
ContextFreshness,
|
|
37
|
+
ContextFrontier,
|
|
38
|
+
ContextIdentity,
|
|
39
|
+
ModelContextProvenance,
|
|
40
|
+
} from "./context-identity.ts"
|
|
41
|
+
import type { ContextSelection } from "./context-selector.ts"
|
|
42
|
+
import { appendPresentationEvents, presentationHead } from "./persistence.ts"
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Sumber event kompaksi kernel minimal — bentuk struktural yang dipenuhi baik
|
|
46
|
+
* oleh EventBus kernel (`Session.events`) maupun `SubAgentSession.events`
|
|
47
|
+
* (tanpa mengimpor kernel di modul ini; tanpa melebar ke tipe sesi penuh).
|
|
48
|
+
*/
|
|
49
|
+
export interface CompactionEventSource {
|
|
50
|
+
on(type: "context:compacted", handler: (event: { reason?: unknown }) => void): () => void
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Metadata runtime yang dibawa melewati batas seed (host-side, RAM, lifecycle-
|
|
55
|
+
* scoped). BUKAN store: objek ini hidup pada handle sesi; tidak diserialisasi,
|
|
56
|
+
* tidak menjadi otoritas, tidak mengubah keputusan publikasi. Bila frontier tak
|
|
57
|
+
* tersedia (seleksi kosong) → identity/frontier/provenance absen + freshness
|
|
58
|
+
* unknown (jangan mengarang).
|
|
59
|
+
*/
|
|
60
|
+
export interface RuntimeContextMetadata {
|
|
61
|
+
readonly sessionId: string
|
|
62
|
+
readonly threadId: string
|
|
63
|
+
/** Identitas view (dari frontier seleksi); null bila tak ada baris kanonik. */
|
|
64
|
+
readonly identity: ContextIdentity | null
|
|
65
|
+
/** Frontier view terpilih (provenance mengikat isi); null bila tak ada. */
|
|
66
|
+
readonly frontier: ContextFrontier | null
|
|
67
|
+
/**
|
|
68
|
+
* Revisi kanonis yang diamati (dari frontier seleksi, atau canonicalRevision
|
|
69
|
+
* bila frontier absen). Nilai observasi — TIDAK pernah di-fabricate.
|
|
70
|
+
*/
|
|
71
|
+
readonly revision: number
|
|
72
|
+
/** Basis pemilihan (dari seleksi). */
|
|
73
|
+
readonly selectionBasis: string
|
|
74
|
+
/** Kesegaran view (dari seleksi; unknown tetap unknown). */
|
|
75
|
+
readonly freshness: ContextFreshness
|
|
76
|
+
/** Cakupan ringkasan [0, coveredSeq); 0 = tanpa ringkasan. */
|
|
77
|
+
readonly coveredSeq: number
|
|
78
|
+
/** Provenance model-visible; absen bila frontier absen (jangan mengarang). */
|
|
79
|
+
readonly provenance?: ModelContextProvenance
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Bangun metadata runtime dari ContextSelection. MURNI; tak menyentuh DB/runtime.
|
|
84
|
+
* `canonicalRevision` = revisi kanonis yang diamati pemanggil (mis. dari
|
|
85
|
+
* countDurableCompactions); dipakai HANYA bila frontier absen — bila frontier
|
|
86
|
+
* ada, revision frontier yang dipakai (sumber tunggal, tak ditebak).
|
|
87
|
+
*/
|
|
88
|
+
export function runtimeMetadataFromSelection(
|
|
89
|
+
selection: ContextSelection,
|
|
90
|
+
canonicalRevision = 0,
|
|
91
|
+
): RuntimeContextMetadata {
|
|
92
|
+
const frontier = selection.frontier
|
|
93
|
+
const identity: ContextIdentity | null = frontier
|
|
94
|
+
? {
|
|
95
|
+
sessionId: selection.sessionId,
|
|
96
|
+
threadId: selection.threadId,
|
|
97
|
+
baseSeq: frontier.baseSeq,
|
|
98
|
+
anchorEventId: frontier.anchorEventId,
|
|
99
|
+
}
|
|
100
|
+
: null
|
|
101
|
+
const provenance = provenanceFromSelection(selection)
|
|
102
|
+
return {
|
|
103
|
+
sessionId: selection.sessionId,
|
|
104
|
+
threadId: selection.threadId,
|
|
105
|
+
identity,
|
|
106
|
+
frontier,
|
|
107
|
+
revision: frontier?.revision ?? canonicalRevision,
|
|
108
|
+
selectionBasis: selection.selectionBasis,
|
|
109
|
+
freshness: selection.freshness,
|
|
110
|
+
coveredSeq: selection.coveredSeq,
|
|
111
|
+
...(provenance !== undefined ? { provenance } : {}),
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Bangun ModelContextProvenance dari ContextSelection (tipe P3.2 yang sudah ada —
|
|
117
|
+
* sebelumnya TIDAK pernah dikonstruksi di mana pun; P3.5 mengisinya). Provenance
|
|
118
|
+
* mencatat ASAL + transformasi; TIDAK memberi otoritas kanonik.
|
|
119
|
+
*
|
|
120
|
+
* Mengembalikan undefined bila frontier absen (tak ada yang bisa dibuktikan).
|
|
121
|
+
* `projectionRevision` SENGAJA dibiarkan absen: ContextSelection tidak membawa
|
|
122
|
+
* versi proyeksi berversi, dan kontrak P3.2 melarang mengarangnya.
|
|
123
|
+
*/
|
|
124
|
+
export function provenanceFromSelection(
|
|
125
|
+
selection: ContextSelection,
|
|
126
|
+
): ModelContextProvenance | undefined {
|
|
127
|
+
if (!selection.frontier) return undefined
|
|
128
|
+
return {
|
|
129
|
+
contextIdentity: {
|
|
130
|
+
sessionId: selection.sessionId,
|
|
131
|
+
threadId: selection.threadId,
|
|
132
|
+
baseSeq: selection.frontier.baseSeq,
|
|
133
|
+
anchorEventId: selection.frontier.anchorEventId,
|
|
134
|
+
},
|
|
135
|
+
canonicalFrontier: selection.frontier,
|
|
136
|
+
selectionBasis: selection.selectionBasis,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Baca event_seq berikutnya untuk jalur jembatan (presentationHead + 1).
|
|
142
|
+
* Baca-saja; memakai helper P2.11 yang SUDAH ADA (tanpa memuat seluruh riwayat
|
|
143
|
+
* — murah untuk kompaksi yang jarang).
|
|
144
|
+
*/
|
|
145
|
+
export function nextPresentationEventSeq(sessionId: string, cwd?: string): number {
|
|
146
|
+
return presentationHead(sessionId, cwd) + 1
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Jembatan kompaksi headless: tulis event durable `context.compacted` lewat
|
|
151
|
+
* primitif yang SUDAH ADA (appendPresentationEvents — idempoten via dedup payload
|
|
152
|
+
* identik, berpagar epoch, tabrakan payload-beda ditolak eksplisit). TIDAK
|
|
153
|
+
* menulis kanonik; TIDAK membuat store kedua.
|
|
154
|
+
*
|
|
155
|
+
* Best-effort: kegagalan jembatan dilaporkan (stderr, konvensi src/session yang
|
|
156
|
+
* sudah ada) dan TIDAK pernah menyentuh canonical history. `expectedEpoch`
|
|
157
|
+
* opsional — bila absen, tulis tanpa pagar (sama seperti flush jalur yang tak
|
|
158
|
+
* memegang epoch; mis. adapter anak di cli/index.ts yang juga unfenced).
|
|
159
|
+
* `eventSeq`/`ts` opsional agar deterministik di test; produksi membiarkannya
|
|
160
|
+
* dihitung (seq berikutnya; waktu saat tulis).
|
|
161
|
+
*/
|
|
162
|
+
export async function bridgeCompactionToDurable(opts: {
|
|
163
|
+
sessionId: string
|
|
164
|
+
cwd?: string
|
|
165
|
+
expectedEpoch?: number
|
|
166
|
+
reason: string
|
|
167
|
+
turnId?: number
|
|
168
|
+
ts?: number
|
|
169
|
+
eventSeq?: number
|
|
170
|
+
}): Promise<{ bridged: boolean; detail: string }> {
|
|
171
|
+
const event: DomainEvent = {
|
|
172
|
+
type: "context.compacted",
|
|
173
|
+
eventSeq: opts.eventSeq ?? nextPresentationEventSeq(opts.sessionId, opts.cwd),
|
|
174
|
+
ts: opts.ts ?? Date.now(),
|
|
175
|
+
sessionId: opts.sessionId,
|
|
176
|
+
turnId: opts.turnId ?? 0,
|
|
177
|
+
reason: typeof opts.reason === "string" ? opts.reason : "",
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
const stats = await appendPresentationEvents(
|
|
181
|
+
opts.sessionId,
|
|
182
|
+
opts.cwd,
|
|
183
|
+
[event],
|
|
184
|
+
opts.expectedEpoch !== undefined ? { expectedEpoch: opts.expectedEpoch } : undefined,
|
|
185
|
+
)
|
|
186
|
+
return {
|
|
187
|
+
bridged: stats.written > 0,
|
|
188
|
+
detail: `written=${stats.written} dup=${stats.duplicates} col=${stats.collisions}`,
|
|
189
|
+
}
|
|
190
|
+
} catch (e) {
|
|
191
|
+
const detail = `context.compacted bridge failed: ${(e as Error).message}`
|
|
192
|
+
process.stderr.write(`[warn] ${detail}\n`)
|
|
193
|
+
return { bridged: false, detail }
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Pasang jembatan kompaksi pada bus event sesi TANPA presentation adapter (jalur
|
|
199
|
+
* otonom/headless). Berlangganan `context:compacted` dan menuliskan marker durable
|
|
200
|
+
* via `bridgeCompactionToDurable`. Mengembalikan fungsi detach.
|
|
201
|
+
*
|
|
202
|
+
* Desain (mengapa begini):
|
|
203
|
+
* - `epochOf` dievaluasi SAAT event tiba (bukan saat attach) supaya nilai epoch
|
|
204
|
+
* selalu terkini; kegagalan membaca epoch → tulis tanpa pagar (best-effort, sama
|
|
205
|
+
* seperti konvensi flush anak yang tak memegang epoch).
|
|
206
|
+
* - `seq` dialokasikan dari SATU counter lokal yang di-seed presentationHead saat
|
|
207
|
+
* attach (bukan MAX+1 per event) supaya dua kompaksi berurutan tak berebut seq
|
|
208
|
+
* yang sama dalam satu proses; lintas-proses tetap aman via INSERT OR IGNORE +
|
|
209
|
+
* deteksi tabrakan primitif yang sudah ada.
|
|
210
|
+
* - Handler TIDAK melempar (bus kernel mengisolasi error handler, tapi jembatan
|
|
211
|
+
* tetap fire-and-forget + catch agar tak ada efek samping ke turn).
|
|
212
|
+
*
|
|
213
|
+
* JANGAN pasang di sesi yang SUDAH punya presentation adapter (jalur anak
|
|
214
|
+
* delegate_task di cli/index.ts) — marker akan ganda per kompaksi.
|
|
215
|
+
*/
|
|
216
|
+
export function attachHeadlessCompactionBridge(opts: {
|
|
217
|
+
sessionId: string
|
|
218
|
+
cwd?: string
|
|
219
|
+
events: CompactionEventSource
|
|
220
|
+
epochOf?: () => number | undefined
|
|
221
|
+
turnIdOf?: () => number
|
|
222
|
+
}): () => void {
|
|
223
|
+
let seq: number
|
|
224
|
+
try {
|
|
225
|
+
seq = nextPresentationEventSeq(opts.sessionId, opts.cwd)
|
|
226
|
+
} catch {
|
|
227
|
+
// Baca gagal = mulai dari 0; tulis pertama menentukan nasib (diterima atau
|
|
228
|
+
// ditolak eksplisit oleh primitif). Jangan mengarang seq acak.
|
|
229
|
+
seq = 0
|
|
230
|
+
}
|
|
231
|
+
const handler = (e: unknown): void => {
|
|
232
|
+
const reason = (e as { reason?: unknown } | null | undefined)?.reason
|
|
233
|
+
const reasonText = typeof reason === "string" ? reason : ""
|
|
234
|
+
let expectedEpoch: number | undefined
|
|
235
|
+
try {
|
|
236
|
+
expectedEpoch = opts.epochOf?.()
|
|
237
|
+
} catch {
|
|
238
|
+
expectedEpoch = undefined
|
|
239
|
+
}
|
|
240
|
+
const eventSeq = seq++
|
|
241
|
+
void bridgeCompactionToDurable({
|
|
242
|
+
sessionId: opts.sessionId,
|
|
243
|
+
cwd: opts.cwd,
|
|
244
|
+
...(expectedEpoch !== undefined ? { expectedEpoch } : {}),
|
|
245
|
+
reason: reasonText,
|
|
246
|
+
eventSeq,
|
|
247
|
+
...(opts.turnIdOf ? { turnId: opts.turnIdOf() } : {}),
|
|
248
|
+
})
|
|
249
|
+
}
|
|
250
|
+
return opts.events.on("context:compacted", handler)
|
|
251
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// P2.8 — Context Assembly (read-only, derived, RAM-only).
|
|
2
|
+
//
|
|
3
|
+
// Perakitan konteks = lapisan TUNGGAL antara histori kanonik/proyeksi turunan
|
|
4
|
+
// dan buffer vendor. Aturan“P2.8” yang bersifat hukum:
|
|
5
|
+
//
|
|
6
|
+
// HANYA proyeksi CURRENT yang boleh ikut. State lain (STALE / INCOMPLETE /
|
|
7
|
+
// CORRUPT / UNKNOWN / absen) → fallback ke histori kanonik persis seperti
|
|
8
|
+
// hari ini. “Tak ada proyeksi” berarti “tak ada proyeksi yang bisa dipakai”,
|
|
9
|
+
// BUKAN “histori kosong”.
|
|
10
|
+
//
|
|
11
|
+
// Batas keras modul ini:
|
|
12
|
+
// - BACA SAJA. Nol tulis ke messages / history_projections / runs / sessions.
|
|
13
|
+
// - Tanpa reads atas runs / run_id / last_persisted_seq (Run bukan input konteks).
|
|
14
|
+
// - Tanpa kebijakan budget / pemicu kompaksi (milik vendor).
|
|
15
|
+
// - Tanpa ringkasan baru (summary berasal dari pemanggil P2.7), tanpa rebuild.
|
|
16
|
+
// - Tanpa state durable: ContextView hidup hanya di RAM per proses.
|
|
17
|
+
//
|
|
18
|
+
// Bentuk ringkasan sintetis mengikuti konvensi vendor yang sudah ada
|
|
19
|
+
// (vendor/minicore/src/core/compact.ts:44 — {role:"user", content:"Previous
|
|
20
|
+
// context:\n..."}): pesan user biasa, TANPA field persistence-only (event_id,
|
|
21
|
+
// seq, run_id, thread_id, migrated, …) dan tidak pernah ditulis balik ke histori.
|
|
22
|
+
|
|
23
|
+
import type { Message } from "#minicore/core/types.ts"
|
|
24
|
+
import {
|
|
25
|
+
getProjection,
|
|
26
|
+
getProjectionStatus,
|
|
27
|
+
loadThreadHistoryWithSeq,
|
|
28
|
+
type ProjectionState,
|
|
29
|
+
SUMMARY_PROJECTION_ID,
|
|
30
|
+
} from "./persistence.ts"
|
|
31
|
+
import { buildRuntimeStateHeader, type OperationalStateSnapshot } from "./runtime-header.ts"
|
|
32
|
+
|
|
33
|
+
/** Asal isi view: proyeksi turunan, atau histori kanonik (fallback). */
|
|
34
|
+
export type ContextSource = "projection" | "messages"
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* View konteks turunan — NILAI SESAJA, bukan state persistensi.
|
|
38
|
+
* Tidak pernah diserialisasi, tidak pernah jadi otoritas resume.
|
|
39
|
+
*/
|
|
40
|
+
export interface ContextView {
|
|
41
|
+
/** Ringkasan cakupan [0, coveredSeq); absen bila fallback penuh. */
|
|
42
|
+
summary?: string
|
|
43
|
+
/** Cakupan eksklusif yang diwakili ringkasan; 0 bila tak ada ringkasan. */
|
|
44
|
+
coveredSeq: number
|
|
45
|
+
source: ContextSource
|
|
46
|
+
status: ProjectionState
|
|
47
|
+
/** Materialisasi untuk di-seed ke ContextStore vendor (appendAll). */
|
|
48
|
+
messages: Message[]
|
|
49
|
+
/**
|
|
50
|
+
* Artefak context-only yang Tertanam di `messages[0]` (bila proyeksi
|
|
51
|
+
* dipakai). Wajib dipROID oleh jalur persist agar artefak turunan tak
|
|
52
|
+
* pernah menjadi histori kanonik. `undefined` pada fallback.
|
|
53
|
+
*/
|
|
54
|
+
contextOnly?: ContextOnlyArtifact
|
|
55
|
+
/**
|
|
56
|
+
* [Fase 4 / LOOP-13, LOOP-INV-16] Header operasional deterministik yang
|
|
57
|
+
* disuntikkan ke `messages[0]` (di depan ringkasan) saat state tersedia.
|
|
58
|
+
* Disimpan terpisah agar jalur persist bisa membuangnya bersama artifact.
|
|
59
|
+
*/
|
|
60
|
+
runtimeHeader?: ContextOnlyArtifact
|
|
61
|
+
/** Alasan diagnostik kenapa proyeksi tidak dipakai (kosong bila CURRENT). */
|
|
62
|
+
detail: string
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Bentuk vendor minimum yang valid untuk ringkasan sintetis: UserMessage
|
|
67
|
+
* (role+content). Field persistence-only DILARANG di sini.
|
|
68
|
+
*/
|
|
69
|
+
export function syntheticSummaryMessage(summary: string, coveredSeq: number): Message {
|
|
70
|
+
return {
|
|
71
|
+
role: "user",
|
|
72
|
+
// Penanda cakupan eksplisit supaya model (dan pembaca log) tahu ini
|
|
73
|
+
// ringkasan prefix [0,N), bukan pesan kanonik.
|
|
74
|
+
content: `Previous context [0,${coveredSeq}):\n${summary}`,
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Batas pemotongan aman. Memotong antara assistant(toolCalls) dan hasil
|
|
80
|
+
* tool-nya menghasilkan tool result yatim pada konteks — pasangan rusak yang
|
|
81
|
+
* kernel/provider tak boleh terima. Border tak aman → fallback penuh
|
|
82
|
+
* (deterministik; bukan heuristik).
|
|
83
|
+
*
|
|
84
|
+
* Diekspor untuk dipakai bersama P3.3 selector (satu sumber kebenaran batas;
|
|
85
|
+
* P3.3 memilih segmen kanonik, ia wajib memakai batas yang SAMA).
|
|
86
|
+
*/
|
|
87
|
+
export function boundaryIsSafe(
|
|
88
|
+
rows: { seq: number; message: unknown }[],
|
|
89
|
+
boundarySeq: number,
|
|
90
|
+
): boolean {
|
|
91
|
+
if (boundarySeq <= 0) return true
|
|
92
|
+
const lastCovered = rows.find((r) => r.seq === boundarySeq - 1)
|
|
93
|
+
const firstTail = rows.find((r) => r.seq === boundarySeq)
|
|
94
|
+
const hasCalls = (m: unknown): boolean => {
|
|
95
|
+
const toolCalls = (m as { toolCalls?: unknown } | undefined)?.toolCalls
|
|
96
|
+
return Array.isArray(toolCalls) && toolCalls.length > 0
|
|
97
|
+
}
|
|
98
|
+
if (lastCovered && hasCalls(lastCovered.message)) return false
|
|
99
|
+
if (firstTail && (firstTail.message as { role?: string }).role === "tool") return false
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Identitas artefak context-only yang HANYA boleh hidup di ContextStore.
|
|
105
|
+
*
|
|
106
|
+
* Mengapa field penanda tak bisa di-ekspresikan di pesan: vendor
|
|
107
|
+
* `snapshotMessage` (vendor/minicore/src/core/snapshot.ts:17) untuk
|
|
108
|
+
* role "user" HANYA mempertahankan `{role, content}` — field tambahan
|
|
109
|
+
* DIBUANG sebelum `session.state.history` terlihat aplikasi. Jadi penanda
|
|
110
|
+
* struktural mustahil tanpa mengubah vendor (dilarang). Kanal yang selamat
|
|
111
|
+
* hanyalah content itu sendiri — maka kanal ini adalah FINGERPRINT TEBAK
|
|
112
|
+
* PERSIS dari artefak yang kita buat sendiri, bukan pola tekstual heuristik.
|
|
113
|
+
*
|
|
114
|
+
* Batasnya jelas: hanya cocok untuk satu pesan (index 0) dan hanya bila
|
|
115
|
+
* role+content identik_byte dengan artefak yang dicatat. Kalau kernel
|
|
116
|
+
* sudah mengompak (artefak terlipat ke summary vendor), fingerprint tak
|
|
117
|
+
* cocok lagi → tak ada yang dibuang.
|
|
118
|
+
*/
|
|
119
|
+
export interface ContextOnlyArtifact {
|
|
120
|
+
role: "user"
|
|
121
|
+
content: string
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Buang artefak context-only dari array yang akan dipersist.
|
|
126
|
+
* Fungsi MURNI, gagal-closed: tanpa fingerprint → tak mengubah apa pun.
|
|
127
|
+
*/
|
|
128
|
+
export function stripContextOnly<T>(
|
|
129
|
+
messages: readonly T[],
|
|
130
|
+
artifact: ContextOnlyArtifact | undefined,
|
|
131
|
+
): T[] {
|
|
132
|
+
if (!artifact) return messages as T[]
|
|
133
|
+
const first = messages[0] as { role?: unknown; content?: unknown } | undefined
|
|
134
|
+
if (!first) return messages as T[]
|
|
135
|
+
if (first.role !== artifact.role) return messages as T[]
|
|
136
|
+
if (first.content !== artifact.content) return messages as T[]
|
|
137
|
+
return messages.slice(1)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Gabungkan artifact header + summary + canonical tail menjadi messages. */
|
|
141
|
+
function assembleViewMessages(
|
|
142
|
+
messages: Message[],
|
|
143
|
+
operationalState?: OperationalStateSnapshot | null,
|
|
144
|
+
summaryArtifact?: ContextOnlyArtifact,
|
|
145
|
+
): { messages: Message[]; contextOnly?: ContextOnlyArtifact; runtimeHeader?: ContextOnlyArtifact } {
|
|
146
|
+
// Kasus tanpa header: perilaku lama persis.
|
|
147
|
+
if (!operationalState) {
|
|
148
|
+
return {
|
|
149
|
+
messages,
|
|
150
|
+
...(summaryArtifact ? { contextOnly: summaryArtifact } : {}),
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const headerText = buildRuntimeStateHeader(operationalState)
|
|
155
|
+
// Bila ada artifact ringkasan (proyeksi), GABUNG header + ringkasan menjadi
|
|
156
|
+
// SATU pesan context-only di index 0. Ini menjaga invariant stripContextOnly
|
|
157
|
+
// (yang hanya memeriksa index 0) tetap benar: tidak ada artefak turunan yang
|
|
158
|
+
// bergeser ke index 1 lalu bocor ke histori kanonik.
|
|
159
|
+
if (summaryArtifact) {
|
|
160
|
+
const combinedContent = `${headerText}\n\n${summaryArtifact.content}`
|
|
161
|
+
const combined: ContextOnlyArtifact = { role: "user", content: combinedContent }
|
|
162
|
+
const rest = messages.slice(1) // buang synthetic lama (index 0)
|
|
163
|
+
return {
|
|
164
|
+
messages: [combined as Message, ...rest],
|
|
165
|
+
contextOnly: combined,
|
|
166
|
+
runtimeHeader: { role: "user", content: headerText },
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Fallback (tanpa ringkasan): header berdiri sendiri di index 0.
|
|
171
|
+
const runtimeHeader: ContextOnlyArtifact = { role: "user", content: headerText }
|
|
172
|
+
return {
|
|
173
|
+
messages: [runtimeHeader as Message, ...messages],
|
|
174
|
+
runtimeHeader,
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Rakit view konteks untuk satu thread.
|
|
180
|
+
*
|
|
181
|
+
* Fallback = histori kanonik utuh, tanpa filter/urutan/transformasi — identik
|
|
182
|
+
* dengan replay `loadSession` hari ini.
|
|
183
|
+
*/
|
|
184
|
+
export function assembleContext(
|
|
185
|
+
sessionId: string,
|
|
186
|
+
threadId: string,
|
|
187
|
+
cwd?: string,
|
|
188
|
+
operationalState?: OperationalStateSnapshot | null,
|
|
189
|
+
): ContextView {
|
|
190
|
+
const status = getProjectionStatus(sessionId, threadId, SUMMARY_PROJECTION_ID, cwd)
|
|
191
|
+
const rows = loadThreadHistoryWithSeq(sessionId, threadId, cwd)
|
|
192
|
+
const all: Message[] = rows.map((r) => r.message as Message)
|
|
193
|
+
|
|
194
|
+
// Fallback: tak ada proyeksi yang bisa dipakai. Histori kanonik utuh.
|
|
195
|
+
const fallback = (detail: string, state: ProjectionState = status.state): ContextView => {
|
|
196
|
+
const assembled = assembleViewMessages(all, operationalState)
|
|
197
|
+
return {
|
|
198
|
+
coveredSeq: 0,
|
|
199
|
+
source: "messages",
|
|
200
|
+
status: state,
|
|
201
|
+
messages: assembled.messages,
|
|
202
|
+
...(assembled.runtimeHeader ? { runtimeHeader: assembled.runtimeHeader } : {}),
|
|
203
|
+
detail,
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// HANYA CURRENT boleh ikut. State lain → fallback, tanpa blend.
|
|
208
|
+
if (status.state !== "CURRENT") return fallback(`projection ${status.state}: ${status.detail}`)
|
|
209
|
+
|
|
210
|
+
const projection = getProjection(sessionId, threadId, SUMMARY_PROJECTION_ID, cwd)
|
|
211
|
+
// Baris CURRENT tapi hilang saat dibaca = tak dapat dibuktikan → fallback.
|
|
212
|
+
if (!projection) return fallback("projection CURRENT but unreadable", "UNKNOWN")
|
|
213
|
+
const baseSeq = projection.base_seq
|
|
214
|
+
// base_seq=0: cakupan vakum → tak ada ringkasan; histori utuh apa adanya.
|
|
215
|
+
if (baseSeq <= 0) return fallback("empty coverage (base_seq=0)", "CURRENT")
|
|
216
|
+
|
|
217
|
+
if (!boundaryIsSafe(rows, baseSeq)) {
|
|
218
|
+
return fallback("unsafe boundary: tool call/result pair would split", "CURRENT")
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const tail = rows.filter((r) => r.seq >= baseSeq).map((r) => r.message as Message)
|
|
222
|
+
const summary = projection.summary_text
|
|
223
|
+
if (typeof summary !== "string" || summary.length === 0) {
|
|
224
|
+
return fallback("CURRENT projection carries no summary text", "CURRENT")
|
|
225
|
+
}
|
|
226
|
+
const synthetic = syntheticSummaryMessage(summary, baseSeq)
|
|
227
|
+
const artifact: ContextOnlyArtifact = {
|
|
228
|
+
role: "user",
|
|
229
|
+
content: synthetic.content as string,
|
|
230
|
+
}
|
|
231
|
+
const assembled = assembleViewMessages([synthetic, ...tail], operationalState, artifact)
|
|
232
|
+
return {
|
|
233
|
+
summary,
|
|
234
|
+
coveredSeq: baseSeq,
|
|
235
|
+
source: "projection",
|
|
236
|
+
status: "CURRENT",
|
|
237
|
+
messages: assembled.messages,
|
|
238
|
+
...(assembled.contextOnly ? { contextOnly: assembled.contextOnly } : {}),
|
|
239
|
+
...(assembled.runtimeHeader ? { runtimeHeader: assembled.runtimeHeader } : {}),
|
|
240
|
+
detail: `summary covers [0,${baseSeq}) + canonical tail`,
|
|
241
|
+
}
|
|
242
|
+
}
|