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
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// P14 — Adopsi pekerjaan lintas sesi dengan otoritas eksplisit.
|
|
2
|
+
//
|
|
3
|
+
// Modul ini adalah LAPIS ORKESTRASI, bukan otoritas kedua. Ia hanya:
|
|
4
|
+
//
|
|
5
|
+
// 1. mengambil lease penulis sesi SUMBER (admission + fence epoch),
|
|
6
|
+
// 2. memverifikasi bahwa sesi TUJUAN masih dipegang token pemanggil,
|
|
7
|
+
// 3. menyerahkan SATU task unfinished ke TaskStore lewat
|
|
8
|
+
// `transferTaskAuthority` (satu transaksi: baris task + ledger),
|
|
9
|
+
// 4. melepas lease sumber, termasuk saat transfer ditolak.
|
|
10
|
+
//
|
|
11
|
+
// Semua keputusan status/revisi/lineage tetap milik TaskStore. Modul ini tidak
|
|
12
|
+
// pernah menyalin task, tidak membuat task baru, tidak menandai selesai, dan
|
|
13
|
+
// tidak memicu scheduler. Adopsi yang berhasil hanya berarti "baris task kini
|
|
14
|
+
// berada di namespace sesi tujuan dengan bukti transfer"; apakah pekerjaan itu
|
|
15
|
+
// aman dilanjutkan tetap diputuskan continuity gate + claim baru.
|
|
16
|
+
//
|
|
17
|
+
// Kenapa lease sumber diambil lebih dulu, bukan sekadar membaca baris:
|
|
18
|
+
// penulis lama yang masih hidup bisa menulis di antara pembacaan dan transfer.
|
|
19
|
+
// Lease membuat kita pemilik admission, dan takeover epoch di sessions.db
|
|
20
|
+
// membuat penulis lama basi secara durable (renew/release-nya gagal).
|
|
21
|
+
|
|
22
|
+
import { sanitizeSessionPart } from "../lib/session-id.ts"
|
|
23
|
+
import { isTaskId, type Task } from "../task/model.ts"
|
|
24
|
+
import { TaskStore, type TaskTransfer, type TaskTransferOutcome } from "../task/store.ts"
|
|
25
|
+
import { acquireSessionWriter, releaseSessionWriter, writerTokenFor } from "./authority.ts"
|
|
26
|
+
|
|
27
|
+
/** Spesifikasi CLI `--adopt-task <source>/<taskId>`. */
|
|
28
|
+
export interface AdoptTaskSpec {
|
|
29
|
+
readonly sourceSessionId: string
|
|
30
|
+
readonly taskId: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Parse `<source>/<taskId>`.
|
|
35
|
+
*
|
|
36
|
+
* `lastIndexOf` disengaja: id sesi kanonik tidak mengandung `/`, tetapi input
|
|
37
|
+
* operator bisa saja membawa path-ish prefix. Bagian setelah slash TERAKHIR
|
|
38
|
+
* adalah task id; sisa di kiri adalah id sesi sumber. Task id divalidasi dengan
|
|
39
|
+
* predikat kanonik yang sama dengan TaskStore, jadi bentuk salah tidak pernah
|
|
40
|
+
* sampai ke database.
|
|
41
|
+
*/
|
|
42
|
+
export function parseAdoptTaskArg(raw: string): AdoptTaskSpec | null {
|
|
43
|
+
const slash = raw.lastIndexOf("/")
|
|
44
|
+
if (slash <= 0 || slash === raw.length - 1) return null
|
|
45
|
+
const sourceRaw = raw.slice(0, slash).trim()
|
|
46
|
+
const taskRaw = raw.slice(slash + 1).trim()
|
|
47
|
+
if (sourceRaw.length === 0 || !isTaskId(taskRaw)) return null
|
|
48
|
+
const sourceSessionId = sanitizeSessionPart(sourceRaw)
|
|
49
|
+
if (sourceSessionId.length === 0) return null
|
|
50
|
+
return { sourceSessionId, taskId: taskRaw }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Outcome tingkat adopsi = outcome transfer TaskStore + kegagalan admission
|
|
55
|
+
* yang terjadi SEBELUM transfer sempat dipanggil.
|
|
56
|
+
*
|
|
57
|
+
* `TRANSFERRED` sengaja dipetakan ke `ADOPTED`: operator melihat satu kata
|
|
58
|
+
* yang menyatakan hasil akhir, sementara `storeOutcome` tetap tersedia untuk
|
|
59
|
+
* audit/tes.
|
|
60
|
+
*/
|
|
61
|
+
export type AdoptionOutcome =
|
|
62
|
+
| "ADOPTED"
|
|
63
|
+
| "INVALID_REQUEST"
|
|
64
|
+
| "REFUSED_SOURCE_BUSY"
|
|
65
|
+
| "REFUSED_TARGET_AUTHORITY"
|
|
66
|
+
| Exclude<TaskTransferOutcome, "TRANSFERRED">
|
|
67
|
+
|
|
68
|
+
export interface AdoptionResult {
|
|
69
|
+
readonly outcome: AdoptionOutcome
|
|
70
|
+
readonly detail: string
|
|
71
|
+
/** Task setelah berpindah (revisi baru), atau null bila tidak ada. */
|
|
72
|
+
readonly task: Task | null
|
|
73
|
+
readonly transfer: TaskTransfer | null
|
|
74
|
+
readonly unresolvedRelations: readonly string[]
|
|
75
|
+
/** Epoch sessions.db sesi sumber setelah admission; null bila tak dicoba. */
|
|
76
|
+
readonly sourceEpoch: number | null
|
|
77
|
+
/** True bila token lama kehilangan lease DAN epoch sumber dinaikkan. */
|
|
78
|
+
readonly fencedFormerOwner: boolean
|
|
79
|
+
readonly sourceIncarnation: number | null
|
|
80
|
+
readonly targetIncarnation: number | null
|
|
81
|
+
readonly expectedRevision: number | null
|
|
82
|
+
/** Outcome mentah TaskStore; null bila transfer tidak pernah dipanggil. */
|
|
83
|
+
readonly storeOutcome: TaskTransferOutcome | null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface AdoptUnfinishedTaskRequest {
|
|
87
|
+
readonly sourceSessionId: string
|
|
88
|
+
readonly targetSessionId: string
|
|
89
|
+
readonly taskId: string
|
|
90
|
+
/**
|
|
91
|
+
* Token penulis sesi tujuan yang SEDANG memegang lease. Di produksi ini
|
|
92
|
+
* token admission composition root; tidak pernah dicetak oleh modul ini.
|
|
93
|
+
*/
|
|
94
|
+
readonly targetOwnerToken: string
|
|
95
|
+
readonly bootId: string
|
|
96
|
+
readonly cwd?: string
|
|
97
|
+
readonly now?: number
|
|
98
|
+
/**
|
|
99
|
+
* Nilai yang diharapkan. Bila absen, modul membacanya SETELAH lease sumber
|
|
100
|
+
* dipegang, lalu TaskStore memeriksanya ulang di dalam transaksi. Override
|
|
101
|
+
* eksplisit dipakai tes untuk membuktikan penolakan stale.
|
|
102
|
+
*/
|
|
103
|
+
readonly expectedRevision?: number
|
|
104
|
+
readonly expectedSourceIncarnation?: number
|
|
105
|
+
readonly expectedTargetIncarnation?: number
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Refusal yang boleh ditampilkan sebagai pesan operator, bukan stack trace. */
|
|
109
|
+
export class AdoptionRefusedError extends Error {
|
|
110
|
+
readonly code = "P14_ADOPTION_REFUSED"
|
|
111
|
+
readonly result: AdoptionResult
|
|
112
|
+
constructor(result: AdoptionResult) {
|
|
113
|
+
super(`[adopt] ${result.outcome}: ${result.detail}`)
|
|
114
|
+
this.name = "AdoptionRefusedError"
|
|
115
|
+
this.result = result
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function result(
|
|
120
|
+
outcome: AdoptionOutcome,
|
|
121
|
+
detail: string,
|
|
122
|
+
extra: Partial<AdoptionResult> = {},
|
|
123
|
+
): AdoptionResult {
|
|
124
|
+
return {
|
|
125
|
+
outcome,
|
|
126
|
+
detail,
|
|
127
|
+
task: null,
|
|
128
|
+
transfer: null,
|
|
129
|
+
unresolvedRelations: [],
|
|
130
|
+
sourceEpoch: null,
|
|
131
|
+
fencedFormerOwner: false,
|
|
132
|
+
sourceIncarnation: null,
|
|
133
|
+
targetIncarnation: null,
|
|
134
|
+
expectedRevision: null,
|
|
135
|
+
storeOutcome: null,
|
|
136
|
+
...extra,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Adopsi satu task unfinished dari sesi sumber ke sesi tujuan.
|
|
142
|
+
*
|
|
143
|
+
* Tidak melempar untuk penolakan yang terklasifikasi — semuanya menjadi
|
|
144
|
+
* `AdoptionResult`. Error penyimpanan yang tak terduga tetap dilempar supaya
|
|
145
|
+
* tidak disamarkan sebagai "ditolak" (yang bisa membuat operator mengira
|
|
146
|
+
* datanya aman padahal IO rusak).
|
|
147
|
+
*/
|
|
148
|
+
export function adoptUnfinishedTask(req: AdoptUnfinishedTaskRequest): AdoptionResult {
|
|
149
|
+
if (!isTaskId(req.taskId)) {
|
|
150
|
+
return result("INVALID_REQUEST", `not a canonical task id: ${req.taskId}`)
|
|
151
|
+
}
|
|
152
|
+
if (req.sourceSessionId.length === 0 || req.targetSessionId.length === 0) {
|
|
153
|
+
return result("INVALID_REQUEST", "source and target session ids must be non-empty")
|
|
154
|
+
}
|
|
155
|
+
if (req.sourceSessionId === req.targetSessionId) {
|
|
156
|
+
return result("REFUSED_SELF_TRANSFER", "source and target sessions are identical")
|
|
157
|
+
}
|
|
158
|
+
if (req.targetOwnerToken.length === 0 || req.bootId.length === 0) {
|
|
159
|
+
return result("REFUSED_TARGET_AUTHORITY", "target owner token and boot id are required")
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const store = new TaskStore(req.cwd)
|
|
163
|
+
const now = req.now ?? Date.now()
|
|
164
|
+
const sourceToken = writerTokenFor(req.bootId)
|
|
165
|
+
|
|
166
|
+
// Langkah 1 — admission sumber. Penulis hidup = REFUSED, bukan takeover
|
|
167
|
+
// paksa. Lease kedaluwarsa = takeover, dan epoch sumber dinaikkan sehingga
|
|
168
|
+
// pemilik lama tidak bisa renew/release/menulis lagi. Kegagalan IO yang tak
|
|
169
|
+
// terklasifikasi dibiarkan melempar: jangan menyamarkannya sebagai "busy".
|
|
170
|
+
const admission = acquireSessionWriter({
|
|
171
|
+
sessionId: req.sourceSessionId,
|
|
172
|
+
cwd: req.cwd,
|
|
173
|
+
bootId: req.bootId,
|
|
174
|
+
token: sourceToken,
|
|
175
|
+
reason: "p14-adopt-task",
|
|
176
|
+
now,
|
|
177
|
+
})
|
|
178
|
+
if (!admission.ok) {
|
|
179
|
+
return result("REFUSED_SOURCE_BUSY", admission.error.message)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
const sourceIncarnation =
|
|
184
|
+
req.expectedSourceIncarnation ?? store.getSessionIncarnation(req.sourceSessionId)
|
|
185
|
+
const targetIncarnation =
|
|
186
|
+
req.expectedTargetIncarnation ?? store.getSessionIncarnation(req.targetSessionId)
|
|
187
|
+
|
|
188
|
+
// Langkah 2 — sesi tujuan harus masih milik token yang memanggil. Ini
|
|
189
|
+
// bukan duplikasi cek TaskStore: refusal di sini terjadi SEBELUM baris
|
|
190
|
+
// disentuh, dan pesannya membedakan "target bukan milikmu" dari
|
|
191
|
+
// "sumber basi".
|
|
192
|
+
if (!store.holdsSessionAuthority(req.targetSessionId, req.targetOwnerToken, now)) {
|
|
193
|
+
return result(
|
|
194
|
+
"REFUSED_TARGET_AUTHORITY",
|
|
195
|
+
`target lease for ${req.targetSessionId} is not held by the calling token`,
|
|
196
|
+
{
|
|
197
|
+
sourceEpoch: admission.admission.epoch,
|
|
198
|
+
fencedFormerOwner: admission.admission.tookOver,
|
|
199
|
+
sourceIncarnation,
|
|
200
|
+
targetIncarnation,
|
|
201
|
+
},
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const current = store.getTask(req.sourceSessionId, req.taskId)
|
|
206
|
+
if (current === null) {
|
|
207
|
+
return result("NOT_FOUND", `no task ${req.taskId} in ${req.sourceSessionId}`, {
|
|
208
|
+
sourceEpoch: admission.admission.epoch,
|
|
209
|
+
fencedFormerOwner: admission.admission.tookOver,
|
|
210
|
+
sourceIncarnation,
|
|
211
|
+
targetIncarnation,
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Revisi dibaca SETELAH lease dipegang; transfer tetap memeriksanya ulang
|
|
216
|
+
// di dalam transaksi. Ini menutup jendela antara pembacaan dan mutasi.
|
|
217
|
+
const expectedRevision = req.expectedRevision ?? current.revision
|
|
218
|
+
|
|
219
|
+
const applied = store.transferTaskAuthority({
|
|
220
|
+
taskId: req.taskId,
|
|
221
|
+
fromSessionId: req.sourceSessionId,
|
|
222
|
+
fromIncarnation: sourceIncarnation,
|
|
223
|
+
toSessionId: req.targetSessionId,
|
|
224
|
+
toIncarnation: targetIncarnation,
|
|
225
|
+
expectedRevision,
|
|
226
|
+
sourceOwnerToken: sourceToken,
|
|
227
|
+
targetOwnerToken: req.targetOwnerToken,
|
|
228
|
+
now,
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
return result(applied.outcome === "TRANSFERRED" ? "ADOPTED" : applied.outcome, applied.detail, {
|
|
232
|
+
task: applied.task,
|
|
233
|
+
transfer: applied.transfer,
|
|
234
|
+
unresolvedRelations: applied.unresolvedRelations,
|
|
235
|
+
sourceEpoch: admission.admission.epoch,
|
|
236
|
+
fencedFormerOwner: admission.admission.tookOver,
|
|
237
|
+
sourceIncarnation,
|
|
238
|
+
targetIncarnation,
|
|
239
|
+
expectedRevision,
|
|
240
|
+
storeOutcome: applied.outcome,
|
|
241
|
+
})
|
|
242
|
+
} finally {
|
|
243
|
+
// Langkah 3 — lepas lease sumber SELALU. Bila transfer berhasil, baris
|
|
244
|
+
// task sudah tidak ada di namespace sumber; bila gagal, sesi sumber tetap
|
|
245
|
+
// dapat dipakai operator. Epoch yang sudah dinaikkan tetap menjadi pagar
|
|
246
|
+
// durable terhadap penulis lama.
|
|
247
|
+
releaseSessionWriter(req.sourceSessionId, sourceToken, req.cwd)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// P3.7 — Advanced A+C full-coverage fold renderer (pure, deterministic).
|
|
2
|
+
//
|
|
3
|
+
// Menutup celah yang diidentifikasi audit P3.7: satu-satunya renderer fold
|
|
4
|
+
// deterministik (`mechanicalCompaction`, vendor) SELALU menyisakan ekor
|
|
5
|
+
// (`kept >= 1`), sehingga tak pernah bisa mengklaim cakupan penuh
|
|
6
|
+
// (`base_seq == head+1`, satu-satunya status CURRENT menurut P2.7 rule 9).
|
|
7
|
+
// Modul ini me-render SELURUH riwayat kanonik [0, N) menjadi satu ringkasan —
|
|
8
|
+
// tanpa ekor — sehingga produsen P3.4 dapat menulis proyeksi CURRENT.
|
|
9
|
+
//
|
|
10
|
+
// Batas keras (kontrak ratifikasi P3_ROADMAP.md + HARD RULES P3.7):
|
|
11
|
+
// - MURNI: tanpa SQL/fs/jaringan/LLM/random/clock/state. Input = baris +
|
|
12
|
+
// policy; output = nilai. `built_at` milik primitif P2.7, BUKAN renderer.
|
|
13
|
+
// - DERIVED-ONLY: tak menyentuh `messages`/proyeksi/event; tak memanggil
|
|
14
|
+
// persistence/presentation/selector.
|
|
15
|
+
// - CAKUPAN PENUH ATAU GAGAL: tiap baris sumber diproses dan dipertanggung-
|
|
16
|
+
// jawabkan tepat satu baris ringkasan; baris hilang/tak-kontinu/tak-
|
|
17
|
+
// didukung → throw eksplisit (jangan klaim CURRENT atas yang tak utuh).
|
|
18
|
+
// - SATU baris ringkasan per SATU baris sumber: lines.length === rows.length
|
|
19
|
+
// adalah bukti cakupan menurut konstruksi (bukan heuristik).
|
|
20
|
+
// - Konvensi render mengikuti kernel (`compactLine`: awalan `- role:`, cap
|
|
21
|
+
// 400/200/80) agar konsisten lintas render; PERBEDAAN yang disengaja vs
|
|
22
|
+
// kernel didokumentasikan di bawah (bagian dari semantik A+C P3.7):
|
|
23
|
+
// * hasil tool non-error DI-RENDER (head 200 char), bukan `<result
|
|
24
|
+
// omitted>` — full coverage mewajibkan tiap sekuens dipertanggung-
|
|
25
|
+
// jawabkan; menghilangkan seluruh hasil tool akan melanggar §4.3
|
|
26
|
+
// (tool side effects) kontrak ratifikasi.
|
|
27
|
+
// * `reasoning` (bila ada) dipertahankan sebagai penanda head-capped,
|
|
28
|
+
// bukan dibuang — ketidakpastian/penalaran adalah state bermakna.
|
|
29
|
+
// - Teks akhir lewat `scrubSecrets` yang SUDAH ADA (tanpa scrub paralel).
|
|
30
|
+
|
|
31
|
+
import { contentToText, safeStringify } from "#minicore/core/tokens.ts"
|
|
32
|
+
import { scrubSecrets } from "../policy/scrub.ts"
|
|
33
|
+
|
|
34
|
+
/** Identitas versi renderer — bagian dari kontrak determinisme. */
|
|
35
|
+
export const FULL_HISTORY_FOLD_VERSION = "full-history-fold-v1"
|
|
36
|
+
|
|
37
|
+
/** Batas head per peran — selaras kernel agar konsisten lintas render. */
|
|
38
|
+
const HEAD_USER = 400
|
|
39
|
+
const HEAD_ASSISTANT = 400
|
|
40
|
+
const HEAD_TOOL = 200
|
|
41
|
+
const HEAD_TOOL_ARGS = 80
|
|
42
|
+
const HEAD_REASONING = 400
|
|
43
|
+
|
|
44
|
+
/** Baris sumber kanonik untuk fold (bentuk yang dipakai loader thread). */
|
|
45
|
+
export interface FoldSourceRow {
|
|
46
|
+
readonly seq: number
|
|
47
|
+
readonly message: unknown
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Hasil fold cakupan-penuh — nilai murni, bukan baris DB. */
|
|
51
|
+
export interface FullHistoryFold {
|
|
52
|
+
/** Teks ringkasan (sudah scrubSecrets; TANPA header — konsumen membungkus). */
|
|
53
|
+
readonly summaryText: string
|
|
54
|
+
/** Selalu == jumlah baris sumber (cakupan eksklusif [0, baseSeq)). */
|
|
55
|
+
readonly baseSeq: number
|
|
56
|
+
/** Selalu [[0, baseSeq]] — bentuk kanonik cakupan penuh. */
|
|
57
|
+
readonly includedRanges: readonly (readonly [number, number])[]
|
|
58
|
+
/** Jumlah baris sumber yang diproses. */
|
|
59
|
+
readonly rowCount: number
|
|
60
|
+
/** Jumlah baris ringkasan yang diemisikan (== rowCount menurut konstruksi). */
|
|
61
|
+
readonly lineCount: number
|
|
62
|
+
/** Identitas versi renderer yang menghasilkan output ini. */
|
|
63
|
+
readonly policyVersion: typeof FULL_HISTORY_FOLD_VERSION
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export class FoldError extends Error {
|
|
67
|
+
readonly code = "FOLD_INVALID"
|
|
68
|
+
constructor(detail: string) {
|
|
69
|
+
super(`[fold] FOLD_INVALID: ${detail}`)
|
|
70
|
+
this.name = "FoldError"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function head(text: string, max: number): string {
|
|
75
|
+
return text.length <= max ? text : `${text.slice(0, max)}…`
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface FoldMessageShape {
|
|
79
|
+
role?: unknown
|
|
80
|
+
content?: unknown
|
|
81
|
+
toolCalls?: unknown
|
|
82
|
+
toolCallId?: unknown
|
|
83
|
+
name?: unknown
|
|
84
|
+
reasoning?: unknown
|
|
85
|
+
isError?: unknown
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function asShape(value: unknown): FoldMessageShape {
|
|
89
|
+
return (value ?? {}) as FoldMessageShape
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Render SATU baris kanonik menjadi SATU baris ringkasan. Throw FoldError untuk
|
|
94
|
+
* peran/bentuk tak-didukung — pemanggil tak boleh menebak atau melewatkan diam.
|
|
95
|
+
*/
|
|
96
|
+
function foldLine(seq: number, message: unknown): string {
|
|
97
|
+
const m = asShape(message)
|
|
98
|
+
const role = m.role
|
|
99
|
+
if (role === "user") {
|
|
100
|
+
if (typeof m.content !== "string" && !Array.isArray(m.content))
|
|
101
|
+
throw new FoldError(`seq ${seq}: user message without content`)
|
|
102
|
+
return `- user [seq=${seq}]: ${head(contentToText(m.content as never), HEAD_USER)}`
|
|
103
|
+
}
|
|
104
|
+
if (role === "assistant") {
|
|
105
|
+
if (typeof m.content !== "string" && !Array.isArray(m.content))
|
|
106
|
+
throw new FoldError(`seq ${seq}: assistant message without content`)
|
|
107
|
+
const calls = Array.isArray(m.toolCalls)
|
|
108
|
+
? (m.toolCalls as { name?: unknown; args?: unknown }[])
|
|
109
|
+
.map((call) => {
|
|
110
|
+
const name = typeof call?.name === "string" ? call.name : "unknown-tool"
|
|
111
|
+
return `${name}(${head(safeStringify(call?.args), HEAD_TOOL_ARGS)})`
|
|
112
|
+
})
|
|
113
|
+
.join(", ")
|
|
114
|
+
: ""
|
|
115
|
+
const reasoning =
|
|
116
|
+
typeof m.reasoning === "string" && m.reasoning.length > 0
|
|
117
|
+
? ` [reasoning: ${head(m.reasoning, HEAD_REASONING)}]`
|
|
118
|
+
: ""
|
|
119
|
+
return `- assistant [seq=${seq}]${calls ? ` [calls: ${calls}]` : ""}${reasoning}: ${head(
|
|
120
|
+
contentToText(m.content as never),
|
|
121
|
+
HEAD_ASSISTANT,
|
|
122
|
+
)}`
|
|
123
|
+
}
|
|
124
|
+
if (role === "tool") {
|
|
125
|
+
if (typeof m.name !== "string" || m.name.length === 0)
|
|
126
|
+
throw new FoldError(`seq ${seq}: tool result without name`)
|
|
127
|
+
if (typeof m.toolCallId !== "string" || m.toolCallId.length === 0)
|
|
128
|
+
throw new FoldError(`seq ${seq}: tool result without toolCallId`)
|
|
129
|
+
const content = m.content
|
|
130
|
+
const text = typeof content === "string" ? content : safeStringify(content ?? null)
|
|
131
|
+
const err = m.isError === true || m.isError === 1 ? " ERROR" : ""
|
|
132
|
+
return `- tool(${m.name}) [seq=${seq} call=${m.toolCallId}]${err}: ${head(text, HEAD_TOOL)}`
|
|
133
|
+
}
|
|
134
|
+
throw new FoldError(`seq ${seq}: unsupported role ${JSON.stringify(role) ?? "?"}`)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Render fold cakupan-penuh yang DETERMINISTIK atas SELURUH baris sumber.
|
|
139
|
+
*
|
|
140
|
+
* Kontrak (fail-closed):
|
|
141
|
+
* - rows kosong → throw (tak ada yang bisa diklaim).
|
|
142
|
+
* - seq harus tepat 0..N-1 kontinyu dan terurut — celah/duplikat/tak-urut →
|
|
143
|
+
* throw (seq tertinggi saja TIDAK membuktikan kontinuitas).
|
|
144
|
+
* - tiap baris tepat satu baris ringkasan (lines.length === rows.length);
|
|
145
|
+
* peran/bentuk tak-didukung → throw (jangan lewatkan diam).
|
|
146
|
+
* - teks akhir di-scrub via `scrubSecrets` yang SUDAH ADA.
|
|
147
|
+
* - baseSeq SELALU == rows.length (cakupan [0, N) penuh).
|
|
148
|
+
*/
|
|
149
|
+
export function renderFullHistoryFold(rows: readonly FoldSourceRow[]): FullHistoryFold {
|
|
150
|
+
if (rows.length === 0) throw new FoldError("empty source history — no full-coverage fold")
|
|
151
|
+
for (let i = 0; i < rows.length; i++) {
|
|
152
|
+
const got = rows[i]!.seq
|
|
153
|
+
if (got !== i)
|
|
154
|
+
throw new FoldError(
|
|
155
|
+
`non-contiguous source seq at index ${i} (got ${String(got)}, want ${i}) — refusing partial claim`,
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
const lines = rows.map((r) => foldLine(r.seq, r.message))
|
|
159
|
+
if (lines.length !== rows.length)
|
|
160
|
+
throw new FoldError("line/row count mismatch — refusing partial claim")
|
|
161
|
+
const summaryText = scrubSecrets(lines.join("\n"))
|
|
162
|
+
if (summaryText.length === 0) throw new FoldError("empty fold output — refusing partial claim")
|
|
163
|
+
return {
|
|
164
|
+
summaryText,
|
|
165
|
+
baseSeq: rows.length,
|
|
166
|
+
includedRanges: [[0, rows.length]] as readonly (readonly [number, number])[],
|
|
167
|
+
rowCount: rows.length,
|
|
168
|
+
lineCount: lines.length,
|
|
169
|
+
policyVersion: FULL_HISTORY_FOLD_VERSION,
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// P2.1 — Identitas sesi kanonik: SATU-SATUNYA penentu SessionId durable.
|
|
2
|
+
//
|
|
3
|
+
// Aturan:
|
|
4
|
+
// canonical SessionId = bentuk kanonik (sanitizeSessionPart) yang dipersist.
|
|
5
|
+
// bootId = id volatil per proses (diagnostik saja, tak pernah
|
|
6
|
+
// dipakai sebagai kunci persistensi).
|
|
7
|
+
// alias = input mentah eksak → kanonik (lookup eksak, target
|
|
8
|
+
// imutabel, tanpa rantai, tanpa auto-merge tabrakan).
|
|
9
|
+
// resume tak dikenal = error eksplisit (bukan sesi baru diam-diam).
|
|
10
|
+
//
|
|
11
|
+
// Modul ini murni keputusan + IO alias; fence penulis (writer_epoch) milik P2.2.
|
|
12
|
+
|
|
13
|
+
import { randomUUID } from "node:crypto"
|
|
14
|
+
import { sanitizeSessionPart } from "../lib/session-id.ts"
|
|
15
|
+
import {
|
|
16
|
+
findFsKeyConflicts,
|
|
17
|
+
lookupSessionAlias,
|
|
18
|
+
sessionRowExists,
|
|
19
|
+
tryRecordSessionAlias,
|
|
20
|
+
} from "./persistence.ts"
|
|
21
|
+
|
|
22
|
+
export class SessionNotFoundError extends Error {
|
|
23
|
+
readonly code = "SESSION_NOT_FOUND"
|
|
24
|
+
readonly key: string
|
|
25
|
+
constructor(key: string) {
|
|
26
|
+
super(
|
|
27
|
+
`[resume] session "${key}" not found — refusing to start a new session under a resume id (use --session "${key}" to start fresh)`,
|
|
28
|
+
)
|
|
29
|
+
this.name = "SessionNotFoundError"
|
|
30
|
+
this.key = key
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class SessionAliasError extends Error {
|
|
35
|
+
readonly code: "SESSION_ALIAS_CONFLICT" | "SESSION_ALIAS_HIJACK" | "SESSION_ALIAS_DANGLING"
|
|
36
|
+
readonly alias: string
|
|
37
|
+
constructor(code: SessionAliasError["code"], alias: string, detail: string) {
|
|
38
|
+
super(`[identity] alias "${alias}" refused (${code}): ${detail}`)
|
|
39
|
+
this.name = "SessionAliasError"
|
|
40
|
+
this.code = code
|
|
41
|
+
this.alias = alias
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class SessionKeyCollisionError extends Error {
|
|
46
|
+
readonly code = "SESSION_KEY_COLLISION"
|
|
47
|
+
readonly fsKey: string
|
|
48
|
+
readonly involved: string[]
|
|
49
|
+
constructor(fsKey: string, involved: string[]) {
|
|
50
|
+
super(
|
|
51
|
+
`[identity] canonical key "${fsKey}" is claimed by multiple sessions (${involved.join(", ")}) — refusing auto-merge; disambiguate explicitly`,
|
|
52
|
+
)
|
|
53
|
+
this.name = "SessionKeyCollisionError"
|
|
54
|
+
this.fsKey = fsKey
|
|
55
|
+
this.involved = involved
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface CanonicalSessionIdentity {
|
|
60
|
+
readonly sid: string
|
|
61
|
+
readonly bootId: string
|
|
62
|
+
readonly alias?: string
|
|
63
|
+
readonly resumed: boolean
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// SATU-SATUNYA generator id sesi di CLI. Hex acak sudah aman-sanitizer
|
|
67
|
+
// (sanitize stabil), jadi id fresh tak pernah lossy.
|
|
68
|
+
export function mintSessionId(): string {
|
|
69
|
+
return randomUUID().slice(0, 8)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// SATU-SATUNYA sanitizer identitas sesi: delegasi ke helper kanonik.
|
|
73
|
+
// Jangan tiru regex ini di call-site (drift 64-vs-60 adalah bug P2.1).
|
|
74
|
+
export function sanitizeSessionId(s: string): string {
|
|
75
|
+
return sanitizeSessionPart(s)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function formatSessionIdentityLine(id: CanonicalSessionIdentity): string {
|
|
79
|
+
return `[session sid=${id.sid} boot=${id.bootId}${id.alias ? ` alias=${id.alias}` : ""}${id.resumed ? " resumed" : ""}]`
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function assertNoFsKeyConflict(fsKey: string, resolved: string, cwd?: string): void {
|
|
83
|
+
const conflicts = findFsKeyConflicts(fsKey, resolved, cwd)
|
|
84
|
+
if (conflicts.length > 0) {
|
|
85
|
+
const involved = [resolved, ...conflicts.map((c) => `${c.id}→${c.canonical}`)]
|
|
86
|
+
throw new SessionKeyCollisionError(fsKey, involved)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function recordAliasOrThrow(
|
|
91
|
+
rawAlias: string,
|
|
92
|
+
canonical: string,
|
|
93
|
+
reason: string,
|
|
94
|
+
cwd?: string,
|
|
95
|
+
): boolean {
|
|
96
|
+
const out = tryRecordSessionAlias(rawAlias, sanitizeSessionPart(rawAlias), canonical, reason, cwd)
|
|
97
|
+
if (out.ok) return out.created
|
|
98
|
+
if (out.reason === "alias-conflict") {
|
|
99
|
+
throw new SessionAliasError(
|
|
100
|
+
"SESSION_ALIAS_CONFLICT",
|
|
101
|
+
rawAlias,
|
|
102
|
+
`already maps to "${out.existing}"; alias targets are immutable`,
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
if (out.reason === "key-hijack") {
|
|
106
|
+
throw new SessionAliasError(
|
|
107
|
+
"SESSION_ALIAS_HIJACK",
|
|
108
|
+
rawAlias,
|
|
109
|
+
`filesystem key maps to live session "${out.existing}"`,
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
throw new SessionAliasError(
|
|
113
|
+
"SESSION_ALIAS_DANGLING",
|
|
114
|
+
rawAlias,
|
|
115
|
+
`canonical target "${canonical}" has no session row`,
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Catat provenance input-lossy hanya bila target sudah ada (sesi fresh
|
|
120
|
+
// belum punya baris → tak ada yang perlu didisambiguasi; dicatat saat
|
|
121
|
+
// resolve berikutnya setelah baris tercipta).
|
|
122
|
+
function noteLossyProvenance(raw: string, canonical: string, cwd?: string): void {
|
|
123
|
+
if (raw === canonical) return
|
|
124
|
+
if (!sessionRowExists(canonical, cwd)) return
|
|
125
|
+
recordAliasOrThrow(raw, canonical, "sanitizer-lossy", cwd)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function resolveResumeTarget(rawResume: string, cwd?: string): { sid: string; viaAlias?: string } {
|
|
129
|
+
const exact = lookupSessionAlias(rawResume, cwd)
|
|
130
|
+
if (exact) {
|
|
131
|
+
if (!sessionRowExists(exact.canonical, cwd)) throw new SessionNotFoundError(rawResume)
|
|
132
|
+
assertNoFsKeyConflict(sanitizeSessionPart(rawResume), exact.canonical, cwd)
|
|
133
|
+
return { sid: exact.canonical, viaAlias: rawResume }
|
|
134
|
+
}
|
|
135
|
+
const key = sanitizeSessionPart(rawResume)
|
|
136
|
+
if (!sessionRowExists(key, cwd)) throw new SessionNotFoundError(key)
|
|
137
|
+
assertNoFsKeyConflict(key, key, cwd)
|
|
138
|
+
if (rawResume !== key) noteLossyProvenance(rawResume, key, cwd)
|
|
139
|
+
return { sid: key }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Titik penyelesaian identitas tunggal. Alur:
|
|
143
|
+
// input id → resolveCanonical → canonical path (tak ada jalur khusus alias).
|
|
144
|
+
export function resolveSessionIdentity(opts: {
|
|
145
|
+
sessionFlag?: string
|
|
146
|
+
resumeFlag?: string
|
|
147
|
+
cwd?: string
|
|
148
|
+
bootId?: string
|
|
149
|
+
}): CanonicalSessionIdentity {
|
|
150
|
+
const bootId = opts.bootId ?? mintSessionId()
|
|
151
|
+
const cwd = opts.cwd
|
|
152
|
+
if (opts.resumeFlag) {
|
|
153
|
+
const { sid, viaAlias } = resolveResumeTarget(opts.resumeFlag, cwd)
|
|
154
|
+
const rawSession = opts.sessionFlag ?? ""
|
|
155
|
+
let alias = viaAlias
|
|
156
|
+
if (rawSession && rawSession !== sid) {
|
|
157
|
+
// Pasangan eksplisit operator (--resume X --session Y): catat Y→X.
|
|
158
|
+
// Konflik/hijack = error eksplisit, bukan merge diam-diam. Khusus
|
|
159
|
+
// hijack (Y adalah sesi hidup lain): arahkan operator melepas --session.
|
|
160
|
+
try {
|
|
161
|
+
recordAliasOrThrow(rawSession, sid, "resume-pair", cwd)
|
|
162
|
+
} catch (e) {
|
|
163
|
+
if (e instanceof SessionAliasError && e.code === "SESSION_ALIAS_HIJACK") {
|
|
164
|
+
throw new SessionAliasError(
|
|
165
|
+
e.code,
|
|
166
|
+
rawSession,
|
|
167
|
+
`"${rawSession}" names its own live session — omit --session when using --resume, or resume that session instead`,
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
throw e
|
|
171
|
+
}
|
|
172
|
+
alias = rawSession
|
|
173
|
+
}
|
|
174
|
+
return { sid, bootId, ...(alias ? { alias } : {}), resumed: true }
|
|
175
|
+
}
|
|
176
|
+
const rawSession = opts.sessionFlag ?? ""
|
|
177
|
+
if (rawSession) {
|
|
178
|
+
const sid = sanitizeSessionPart(rawSession)
|
|
179
|
+
if (rawSession !== sid) noteLossyProvenance(rawSession, sid, cwd)
|
|
180
|
+
assertNoFsKeyConflict(sid, sid, cwd)
|
|
181
|
+
return { sid, bootId, resumed: false }
|
|
182
|
+
}
|
|
183
|
+
return { sid: sanitizeSessionPart(mintSessionId()), bootId, resumed: false }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Read-path kompatibel untuk perintah baca (sessions export): alias →
|
|
187
|
+
// kanonik, selain itu bentuk kanonik input. Tak melempar; tak mencatat.
|
|
188
|
+
export function resolveSessionDisplayTarget(input: string, cwd?: string): string {
|
|
189
|
+
const exact = lookupSessionAlias(input, cwd)
|
|
190
|
+
if (exact && sessionRowExists(exact.canonical, cwd)) return exact.canonical
|
|
191
|
+
return sanitizeSessionPart(input)
|
|
192
|
+
}
|