minicode-ai 0.13.1 → 0.14.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/CHANGELOG.md +35 -0
- package/LICENSE +21 -21
- package/README.md +4 -0
- package/cli/args.ts +66 -5
- package/cli/commands/acp.ts +18 -3
- package/cli/commands/daemon.ts +114 -19
- package/cli/commands/exec.ts +104 -7
- package/cli/commands/mcp.ts +66 -5
- package/cli/commands/memory.ts +46 -14
- package/cli/commands.ts +5 -0
- package/cli/index.ts +51 -16
- package/cli/setup.ts +380 -32
- package/cli/tui.ts +115 -50
- package/docs/TERMINAL_CONTRACT.md +91 -24
- package/docs/agents.md +3 -3
- package/docs/choosing-mode.md +20 -13
- package/docs/cli.md +12 -11
- package/docs/environment.md +4 -3
- package/docs/memory-sessions.md +5 -0
- package/package.json +2 -1
- package/src/app/session.ts +159 -35
- package/src/app/tool-layer.ts +69 -62
- package/src/daemon/approvals.ts +25 -1
- package/src/daemon/client.ts +51 -1
- package/src/daemon/control.ts +8 -0
- package/src/daemon/protocol.ts +47 -1
- package/src/daemon/server.ts +45 -9
- package/src/daemon/subscription.ts +25 -1
- package/src/daemon/telemetry.ts +4 -0
- package/src/hooks/run.ts +5 -1
- package/src/mcp/client.ts +349 -90
- package/src/mcp/http-transport.ts +7 -0
- package/src/mcp/transport.ts +5 -0
- package/src/memory/files.ts +69 -3
- package/src/memory/vector.ts +556 -160
- package/src/policy/approval-binding.ts +199 -0
- package/src/policy/context.ts +20 -1
- package/src/policy/executor.ts +44 -5
- package/src/policy/permission.ts +317 -41
- package/src/policy/tool-contract.ts +94 -0
- package/src/policy/usage.ts +3 -1
- package/src/policy/work-mode.ts +99 -17
- package/src/presentation/adapter.ts +157 -7
- package/src/presentation/events.ts +173 -1
- package/src/presentation/model.ts +37 -0
- package/src/presentation/projection.ts +107 -18
- package/src/presentation/reducer.ts +134 -10
- package/src/sandbox/os.ts +18 -1
- package/src/session/persistence.ts +153 -11
- package/src/task/store.ts +43 -3
- package/src/tools/memory.ts +70 -10
- package/src/tools/task.ts +7 -1
- package/src/tools/todo.ts +14 -4
- package/src/ui/approval/prompt.ts +52 -7
- package/src/ui/assistant/simple.ts +160 -57
- package/src/ui/contract.ts +46 -0
- package/src/ui/footer.ts +31 -17
- package/src/ui/i18n/en.ts +12 -3
- package/src/ui/i18n/id.ts +12 -3
- package/src/ui/render/collapse.ts +24 -52
- package/src/ui/render/detail.ts +14 -9
- package/src/ui/render/errors.ts +8 -0
- package/src/ui/render/reasoning.ts +13 -8
- package/src/ui/session-state.ts +243 -0
- package/src/ui/tui/app.ts +13 -7
- package/src/ui/tui/transcript.ts +271 -30
- 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/cli/setup.ts
CHANGED
|
@@ -47,6 +47,7 @@ import {
|
|
|
47
47
|
runVerify,
|
|
48
48
|
runWithSelfHeal,
|
|
49
49
|
} from "../src/policy/verifier.ts"
|
|
50
|
+
import { VerifyWorkModeConflictError } from "../src/policy/work-mode.ts"
|
|
50
51
|
import { createPresentationAdapter, type PresentationAdapter } from "../src/presentation/adapter.ts"
|
|
51
52
|
import { type DomainEvent, DURABILITY } from "../src/presentation/events.ts"
|
|
52
53
|
import { createInitialState, type PresentationState } from "../src/presentation/model.ts"
|
|
@@ -156,10 +157,12 @@ import {
|
|
|
156
157
|
loadPresentationEvents,
|
|
157
158
|
loadSession,
|
|
158
159
|
loadSessionUsageSummary,
|
|
160
|
+
loadSessionWorkMode,
|
|
159
161
|
loadThreadHistoryWithSeq,
|
|
160
162
|
type PresentationPersistStats,
|
|
161
163
|
RefusedHistoryRewriteError,
|
|
162
164
|
saveSession,
|
|
165
|
+
saveSessionWorkMode,
|
|
163
166
|
shrinkThreadHistory,
|
|
164
167
|
ThreadArchivedError,
|
|
165
168
|
terminalizeChildRuns,
|
|
@@ -183,6 +186,7 @@ import {
|
|
|
183
186
|
} from "../src/task/production-scheduler.ts"
|
|
184
187
|
import { SchedulerObservability } from "../src/task/scheduler-observability.ts"
|
|
185
188
|
import { SESSION_RENEW_INTERVAL_MS } from "../src/task/session-authority.ts"
|
|
189
|
+
import { setTaskTransitionSink, TaskStore } from "../src/task/store.ts"
|
|
186
190
|
import { createTaskIdentityResolver, settleSessionVerifyingTasks } from "../src/task/sync.ts"
|
|
187
191
|
import {
|
|
188
192
|
classifyToolResult,
|
|
@@ -221,8 +225,43 @@ import type {
|
|
|
221
225
|
UiTurnSummary,
|
|
222
226
|
} from "../src/ui/contract.ts"
|
|
223
227
|
import { c } from "../src/ui/render/theme.ts"
|
|
228
|
+
// [Area A · PROMPT 06] State UI per-sesi. Satu instance per `createCliSession`.
|
|
229
|
+
import { UiSessionState } from "../src/ui/session-state.ts"
|
|
224
230
|
import { runSetupWizard } from "./wizard.ts"
|
|
225
231
|
|
|
232
|
+
/**
|
|
233
|
+
* P2.3 (Session 4): kontrak runtime single-session ditegakkan di boundary RUN,
|
|
234
|
+
* bukan hanya didokumentasikan. Satu proses hanya boleh menjalankan SATU sesi
|
|
235
|
+
* agent pada satu waktu. Konstruksi dua sesi tetap boleh (mis. UI isolation
|
|
236
|
+
* test), dan daemon multi-consumer tetap berjalan karena hanya ada satu sesi.
|
|
237
|
+
* Sesi berikutnya boleh berjalan setelah sesi aktif selesai/ditutup.
|
|
238
|
+
*/
|
|
239
|
+
let activeAgentSessionId: string | null = null
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Seam uji deterministik untuk memegang guard tanpa menjalankan provider.
|
|
243
|
+
* Produksi TIDAK pernah memanggil ini; hanya test yang menggunakannya agar
|
|
244
|
+
* tidak bergantung pada timing abort provider nyata.
|
|
245
|
+
*/
|
|
246
|
+
export function __setActiveAgentSessionForTest(sessionId: string | null): void {
|
|
247
|
+
activeAgentSessionId = sessionId
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function __getActiveAgentSessionForTest(): string | null {
|
|
251
|
+
return activeAgentSessionId
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export class ConcurrentAgentSessionError extends Error {
|
|
255
|
+
readonly code = "CONCURRENT_SESSION"
|
|
256
|
+
constructor(active: string, attempted: string) {
|
|
257
|
+
super(
|
|
258
|
+
`[runtime] CONCURRENT_SESSION: session ${active} is already running in this process; ` +
|
|
259
|
+
`session ${attempted} refused (one agent session per process)`,
|
|
260
|
+
)
|
|
261
|
+
this.name = "ConcurrentAgentSessionError"
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
226
265
|
export interface CliSessionOptions {
|
|
227
266
|
cwd?: string
|
|
228
267
|
sessionId: string
|
|
@@ -266,6 +305,15 @@ export interface CliSessionOptions {
|
|
|
266
305
|
* setupToolLayer (visibilitas) dan createMinicodeSession (handler ceiling).
|
|
267
306
|
*/
|
|
268
307
|
workMode?: "DISCUSS" | "PLAN" | "EXECUTE"
|
|
308
|
+
/**
|
|
309
|
+
* P12 S2.2 (D-2) — transparansi resume. Benar bila operator memberi sinyal
|
|
310
|
+
* WorkMode eksplisit (--work-mode / MINICODE_WORK_MODE). WorkMode TIDAK
|
|
311
|
+
* dipersist antar-sesi; saat resume tanpa sinyal eksplisit, WorkMode kembali
|
|
312
|
+
* ke EXECUTE. Bila scope ini `false` DAN identitas sesi `resumed`, composition
|
|
313
|
+
* root menulis SATU baris stderr human-facing (bukan output machine) agar
|
|
314
|
+
* transisi tidak terjadi diam-diam.
|
|
315
|
+
*/
|
|
316
|
+
workModeExplicit?: boolean
|
|
269
317
|
/**
|
|
270
318
|
* [PHASE 6U] Opt-in autonomous Scheduler. DEFAULT OFF - `undefined` and
|
|
271
319
|
* `false` behave identically, and for both the composition root is never called.
|
|
@@ -361,14 +409,32 @@ export interface CliSession {
|
|
|
361
409
|
usage: ReturnType<typeof createUsageCollector>
|
|
362
410
|
budget?: number
|
|
363
411
|
budgetStrict?: boolean
|
|
412
|
+
/** F-CTX-1: jendela konteks yang dipakai footer TUI (window %) — flag --context-window. */
|
|
413
|
+
contextWindowTokens?: number
|
|
364
414
|
/** Flag opt-in local config sesi ini — dipakai perintah REPL (/sync,
|
|
365
415
|
* /provider, /model) agar konsisten dengan provider/tool yang aktif. */
|
|
366
416
|
allowLocalConfig: boolean
|
|
367
417
|
/** P2.3: jumlah hit RAG memory yang di-inject ke system prompt sesi ini. */
|
|
368
418
|
memoryHits: number
|
|
369
419
|
detachSimple: () => void
|
|
420
|
+
/**
|
|
421
|
+
* [Area A · PROMPT 06] State UI milik SESI ini (collapse/detail/reasoning +
|
|
422
|
+
* buffer /copy + error tertunda). SATU instance per sesi; TUI dan printer
|
|
423
|
+
* linier sesi yang sama berbagi instance ini, sedangkan sesi lain di proses
|
|
424
|
+
* yang sama punya instance sendiri — dua sesi tak saling menimpa.
|
|
425
|
+
*/
|
|
426
|
+
uiState: UiSessionState
|
|
370
427
|
persistCurrent: (usageData: unknown) => Promise<void>
|
|
371
428
|
runPromptWithVerify: (prompt: string, signal?: AbortSignal) => Promise<void>
|
|
429
|
+
/**
|
|
430
|
+
* P2.3 daemon: identitas/status run LIVE untuk operator control. runId
|
|
431
|
+
* berasal dari jurnal Run kanonik (bukan id sintetis daemon); status
|
|
432
|
+
* RUNNING selama turn belum terminal. Absen = belum ada run di sesi ini.
|
|
433
|
+
*/
|
|
434
|
+
runState: () => {
|
|
435
|
+
runId: string | null
|
|
436
|
+
status: "RUNNING" | "COMPLETED" | "FAILED" | "INTERRUPTED" | null
|
|
437
|
+
}
|
|
372
438
|
/**
|
|
373
439
|
* P11.1 — ringkasan hasil verify terakhir, read-only untuk summary mesin
|
|
374
440
|
* (`exec --json`). Null bila verify belum pernah jalan di sesi ini.
|
|
@@ -448,7 +514,7 @@ export interface CliSession {
|
|
|
448
514
|
adoption: AdoptionResult | null
|
|
449
515
|
}
|
|
450
516
|
|
|
451
|
-
|
|
517
|
+
function mapPresentationEvent(event: DomainEvent): UiPresentationEvent | null {
|
|
452
518
|
switch (event.type) {
|
|
453
519
|
case "user.message":
|
|
454
520
|
return {
|
|
@@ -575,6 +641,7 @@ export function toPresentationEvent(event: DomainEvent): UiPresentationEvent | n
|
|
|
575
641
|
message: event.message,
|
|
576
642
|
cause: event.cause,
|
|
577
643
|
...(event.hint ? { hint: event.hint } : {}),
|
|
644
|
+
...(event.schemaDrift ? { schemaDrift: event.schemaDrift } : {}),
|
|
578
645
|
expandRef: event.expandRef,
|
|
579
646
|
...(event.parentLink ? { parentToolCallId: event.parentLink.parentToolCallId } : {}),
|
|
580
647
|
}
|
|
@@ -610,6 +677,8 @@ export function toPresentationEvent(event: DomainEvent): UiPresentationEvent | n
|
|
|
610
677
|
qualified: event.identity.qualified,
|
|
611
678
|
target: event.argsSummary.target,
|
|
612
679
|
via: event.via,
|
|
680
|
+
...(event.riskTier ? { riskTier: event.riskTier } : {}),
|
|
681
|
+
...(event.diffPreview ? { diffPreview: event.diffPreview } : {}),
|
|
613
682
|
}
|
|
614
683
|
case "approval.settled":
|
|
615
684
|
return {
|
|
@@ -713,6 +782,54 @@ export function toPresentationEvent(event: DomainEvent): UiPresentationEvent | n
|
|
|
713
782
|
checkpointId: event.checkpointId,
|
|
714
783
|
paths: event.paths,
|
|
715
784
|
}
|
|
785
|
+
case "consequence.decided":
|
|
786
|
+
return {
|
|
787
|
+
type: event.type,
|
|
788
|
+
seq: event.eventSeq,
|
|
789
|
+
turnId: event.turnId,
|
|
790
|
+
verdict: event.verdict,
|
|
791
|
+
failureClass: event.failureClass,
|
|
792
|
+
nextAction: event.nextAction,
|
|
793
|
+
message: event.rationale,
|
|
794
|
+
severity: event.verdict === "FAIL" || event.verdict === "BLOCKED" ? "error" : "warning",
|
|
795
|
+
...(event.newEvidenceSinceLastAttempt !== undefined
|
|
796
|
+
? { newEvidenceSinceLastAttempt: event.newEvidenceSinceLastAttempt }
|
|
797
|
+
: {}),
|
|
798
|
+
}
|
|
799
|
+
case "context.window":
|
|
800
|
+
return {
|
|
801
|
+
type: event.type,
|
|
802
|
+
seq: event.eventSeq,
|
|
803
|
+
turnId: event.turnId,
|
|
804
|
+
usedTokens: event.usedTokens,
|
|
805
|
+
windowTokens: event.windowTokens,
|
|
806
|
+
...(event.percent !== undefined ? { percent: event.percent } : {}),
|
|
807
|
+
...(event.costUsd !== undefined ? { costUsd: event.costUsd } : {}),
|
|
808
|
+
...(event.providerModel ? { providerModel: event.providerModel } : {}),
|
|
809
|
+
}
|
|
810
|
+
case "context.cutoff":
|
|
811
|
+
return {
|
|
812
|
+
type: event.type,
|
|
813
|
+
seq: event.eventSeq,
|
|
814
|
+
turnId: event.turnId,
|
|
815
|
+
reason: event.reason,
|
|
816
|
+
message: event.reason,
|
|
817
|
+
severity: "warning",
|
|
818
|
+
...(event.usedTokens !== undefined ? { usedTokens: event.usedTokens } : {}),
|
|
819
|
+
...(event.windowTokens !== undefined ? { windowTokens: event.windowTokens } : {}),
|
|
820
|
+
}
|
|
821
|
+
case "task.status":
|
|
822
|
+
return {
|
|
823
|
+
type: event.type,
|
|
824
|
+
seq: event.eventSeq,
|
|
825
|
+
turnId: event.turnId,
|
|
826
|
+
taskId: event.taskId,
|
|
827
|
+
taskStatus: event.status,
|
|
828
|
+
...(event.parentId ? { parentId: event.parentId } : {}),
|
|
829
|
+
...(event.dependsOn ? { dependsOn: [...event.dependsOn] } : {}),
|
|
830
|
+
...(event.revision !== undefined ? { revision: event.revision } : {}),
|
|
831
|
+
...(event.verification ? { taskVerification: event.verification } : {}),
|
|
832
|
+
}
|
|
716
833
|
default: {
|
|
717
834
|
const exhaustive: never = event
|
|
718
835
|
void exhaustive
|
|
@@ -721,6 +838,23 @@ export function toPresentationEvent(event: DomainEvent): UiPresentationEvent | n
|
|
|
721
838
|
}
|
|
722
839
|
}
|
|
723
840
|
|
|
841
|
+
/**
|
|
842
|
+
* P1.6: proyeksi UI membawa identitas session/child-run tanpa mengulang setiap
|
|
843
|
+
* case. `sessionId` DomainEvent adalah session pemilik event (anak untuk forward),
|
|
844
|
+
* `parentLink` adalah taut ke pemanggil — tiga makna yang tidak boleh disamakan.
|
|
845
|
+
*/
|
|
846
|
+
export function toPresentationEvent(event: DomainEvent): UiPresentationEvent | null {
|
|
847
|
+
const mapped = mapPresentationEvent(event)
|
|
848
|
+
if (!mapped) return null
|
|
849
|
+
const link = (event as { parentLink?: { childSessionId?: string; runId?: string } }).parentLink
|
|
850
|
+
return {
|
|
851
|
+
...mapped,
|
|
852
|
+
sessionId: event.sessionId,
|
|
853
|
+
...(link?.childSessionId ? { childSessionId: link.childSessionId } : {}),
|
|
854
|
+
...(link?.runId ? { runId: link.runId } : {}),
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
|
|
724
858
|
export function parseVerifyTestEvidence(
|
|
725
859
|
command: string,
|
|
726
860
|
output: string,
|
|
@@ -766,6 +900,7 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
766
900
|
adoptTask,
|
|
767
901
|
allowLocalConfig,
|
|
768
902
|
workMode = "EXECUTE",
|
|
903
|
+
workModeExplicit = false,
|
|
769
904
|
maxSteps,
|
|
770
905
|
contextWindowTokens,
|
|
771
906
|
keepRecentTurns,
|
|
@@ -773,6 +908,11 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
773
908
|
rateLimiter,
|
|
774
909
|
sandboxNotice,
|
|
775
910
|
} = opts
|
|
911
|
+
// [Area A · PROMPT 06] State UI milik SESI ini. Dibuat SEKALI per sesi dan
|
|
912
|
+
// dipakai bersama oleh TUI (Transcript) + printer linier sesi yang sama.
|
|
913
|
+
// Env hanya jadi nilai awal; sesi lain di proses yang sama punya state
|
|
914
|
+
// sendiri sehingga tak saling menimpa collapse/compact/reasoning.
|
|
915
|
+
const uiState = new UiSessionState()
|
|
776
916
|
const modelRef = { current: modelOverride }
|
|
777
917
|
// P2.1 — SATU-SATUNYA keputusan identitas sesi. Sesudah baris ini,
|
|
778
918
|
// `sessionId` adalah identitas kanonik durable; id volatil hanya `bootId`
|
|
@@ -836,6 +976,29 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
836
976
|
`${c.dim(`[adopt] ADOPTED ${adoptTask.taskId} ${adoptTask.sourceSessionId} -> ${sessionId} revision=${moved?.revision ?? "?"} status=${moved?.status ?? "?"} exec=${adoption.transfer?.execGeneration ?? "?"}${adoption.fencedFormerOwner ? " (former owner fenced)" : ""}\n`)}`,
|
|
837
977
|
)
|
|
838
978
|
}
|
|
979
|
+
// P0.2 (Q5): saat `--resume` tanpa sinyal WorkMode eksplisit, pulihkan
|
|
980
|
+
// WorkMode tersimpan dari metadata sesi (NULL legacy → default EXECUTE).
|
|
981
|
+
//
|
|
982
|
+
// [mode-architecture] --plan = alias warisan WorkMode PLAN (bukan permission
|
|
983
|
+
// mode). Dilipat di sini (satu titik untuk semua pemanggil: index/exec/acp).
|
|
984
|
+
const persistedWorkMode = identity.resumed ? loadSessionWorkMode(sessionId, cwd) : undefined
|
|
985
|
+
const planAsWorkMode =
|
|
986
|
+
plan && !workModeExplicit && workMode === "EXECUTE" ? ("PLAN" as const) : workMode
|
|
987
|
+
if (plan && !workModeExplicit && workMode === "EXECUTE") {
|
|
988
|
+
process.stderr.write(
|
|
989
|
+
`${c.dim("[deprecated] --plan is now WorkMode PLAN; use --work-mode PLAN\n")}`,
|
|
990
|
+
)
|
|
991
|
+
}
|
|
992
|
+
const effectiveWorkMode: "DISCUSS" | "PLAN" | "EXECUTE" =
|
|
993
|
+
identity.resumed && !workModeExplicit && !plan
|
|
994
|
+
? (persistedWorkMode ?? "EXECUTE")
|
|
995
|
+
: planAsWorkMode
|
|
996
|
+
// P0.2 (Q4): tolak startup (fail-closed) bila `--verify` digabung dengan
|
|
997
|
+
// WorkMode DISCUSS atau PLAN (baik dari flag/env maupun pulihan `--resume`).
|
|
998
|
+
if (verify && effectiveWorkMode !== "EXECUTE") {
|
|
999
|
+
releaseSessionWriter(sessionId, writerToken, cwd)
|
|
1000
|
+
throw new VerifyWorkModeConflictError(effectiveWorkMode)
|
|
1001
|
+
}
|
|
839
1002
|
// P2.6 — kubur residu crash: RUNNING tanpa holder hidup (admission di atas
|
|
840
1003
|
// membuktikan tak ada penulis hidup) → INTERRUPTED/UNKNOWN. BUKAN
|
|
841
1004
|
// auto-continue/replay; tanpa ini satu crash mengganjal RUNNING unik
|
|
@@ -994,15 +1157,10 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
994
1157
|
return rc.signal
|
|
995
1158
|
})()
|
|
996
1159
|
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
: plan
|
|
1002
|
-
? "plan"
|
|
1003
|
-
: allowlist
|
|
1004
|
-
? "allowlist"
|
|
1005
|
-
: "auto"
|
|
1160
|
+
// [mode-architecture] `--plan` legacy bukan lagi permission mode — pemetaannya
|
|
1161
|
+
// ke WorkMode PLAN terjadi di effectiveWorkMode (di atas). Sisa profil warisan
|
|
1162
|
+
// (allow-all/allowlist) tetap tersedia sebagai tool-access profile.
|
|
1163
|
+
const permissionMode = allowAll ? "allow-all" : ask ? "ask" : allowlist ? "allowlist" : "auto"
|
|
1006
1164
|
|
|
1007
1165
|
// Home guard: sesi dibuka di home berisiko (glob rame, write berbahaya).
|
|
1008
1166
|
// Standar industri: percaya-tapi-ingatkan, jangan tolak diam-diam maupun
|
|
@@ -1070,8 +1228,11 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1070
1228
|
prompt,
|
|
1071
1229
|
cwd,
|
|
1072
1230
|
// RAG retrieval tak boleh menulis access_count di mode tanpa-mutasi.
|
|
1073
|
-
//
|
|
1074
|
-
|
|
1231
|
+
// [mode-architecture] PermissionMode readonly/plan tak lagi ada sebagai sumbu
|
|
1232
|
+
// persetujuan; ceiling tanpa-mutasi kini diekspresikan via WorkMode
|
|
1233
|
+
// DISCUSS/PLAN (effectiveWorkMode). Profil warisan allowlist/allow-all
|
|
1234
|
+
// tetap menulis (bukan read-only).
|
|
1235
|
+
trackAccess: effectiveWorkMode === "EXECUTE",
|
|
1075
1236
|
}),
|
|
1076
1237
|
)
|
|
1077
1238
|
|
|
@@ -1106,6 +1267,18 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1106
1267
|
// kanonik — jadi persist harus menyusun ulang: baseline ++ ekor baru.
|
|
1107
1268
|
let contextCanonicalBaseline: readonly unknown[] | undefined
|
|
1108
1269
|
if (identity.resumed) {
|
|
1270
|
+
// P0.2 (Q5 / Invariant #6) — transparansi resume: WorkMode dipulihkan dari
|
|
1271
|
+
// metadata sesi bila operator tidak memberi sinyal eksplisit (NULL legacy
|
|
1272
|
+
// kembali ke default EXECUTE). Satu baris stderr human-facing memastikan
|
|
1273
|
+
// otoritas sesi hasil resume selalu terlihat eksplisit.
|
|
1274
|
+
const resumeNotice = !workModeExplicit
|
|
1275
|
+
? persistedWorkMode !== undefined
|
|
1276
|
+
? `[resume] WorkMode ${effectiveWorkMode} restored from session; pass --work-mode DISCUSS|PLAN|EXECUTE to override\n`
|
|
1277
|
+
: `[resume] WorkMode default EXECUTE (legacy session without saved workMode); pass --work-mode DISCUSS|PLAN to change\n`
|
|
1278
|
+
: persistedWorkMode !== undefined && persistedWorkMode !== effectiveWorkMode
|
|
1279
|
+
? `[resume] WorkMode ${effectiveWorkMode} (overriding saved ${persistedWorkMode})\n`
|
|
1280
|
+
: ""
|
|
1281
|
+
if (resumeNotice) process.stderr.write(resumeNotice)
|
|
1109
1282
|
// P2.4: aktivasi default Thread SEBELUM seed kernel — backfill aditif
|
|
1110
1283
|
// berpagar (konten/seq tak tersentuh). Basi di sini = gagal loud
|
|
1111
1284
|
// (dunia bergerak pasca-admission); warn-swallow di bawah tak berlaku.
|
|
@@ -1463,6 +1636,8 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1463
1636
|
}
|
|
1464
1637
|
: {}),
|
|
1465
1638
|
...(a.parentToolCallId ? { parentToolCallId: a.parentToolCallId } : {}),
|
|
1639
|
+
...(a.childSessionId ? { childSessionId: a.childSessionId } : {}),
|
|
1640
|
+
...(a.runId ? { runId: a.runId } : {}),
|
|
1466
1641
|
...(a.supersedes ? { supersedes: a.supersedes } : {}),
|
|
1467
1642
|
...(a.expandRef
|
|
1468
1643
|
? { expandRef: { toolCallId: a.expandRef.toolCallId, idx: a.expandRef.idx } }
|
|
@@ -1509,6 +1684,11 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1509
1684
|
text: entry.text,
|
|
1510
1685
|
...(entry.reason ? { reason: entry.reason } : {}),
|
|
1511
1686
|
severity: entry.severity,
|
|
1687
|
+
...(entry.taskId ? { taskId: entry.taskId } : {}),
|
|
1688
|
+
...(entry.taskStatus ? { taskStatus: entry.taskStatus } : {}),
|
|
1689
|
+
...(entry.parentId ? { parentId: entry.parentId } : {}),
|
|
1690
|
+
...(entry.dependsOn?.length ? { dependsOn: [...entry.dependsOn] } : {}),
|
|
1691
|
+
...(entry.revision !== undefined ? { revision: entry.revision } : {}),
|
|
1512
1692
|
}))
|
|
1513
1693
|
const plans: UiPresentationPlan[] = [...shadowState.plans.values()].map((entry) => ({
|
|
1514
1694
|
planId: entry.planId,
|
|
@@ -1602,6 +1782,9 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1602
1782
|
inputTokens: u.inputTokens,
|
|
1603
1783
|
outputTokens: u.outputTokens,
|
|
1604
1784
|
totalTokens: u.totalTokens,
|
|
1785
|
+
// P1.4: atribusi biaya compaction memakai model yang benar-benar
|
|
1786
|
+
// dipakai (deepseek-chat), bukan model utama sesi.
|
|
1787
|
+
model: "deepseek-chat",
|
|
1605
1788
|
},
|
|
1606
1789
|
})
|
|
1607
1790
|
} catch {}
|
|
@@ -1612,6 +1795,7 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1612
1795
|
sessionTools,
|
|
1613
1796
|
evidenceMode,
|
|
1614
1797
|
setWorkMode: setToolWorkMode,
|
|
1798
|
+
setPermissionMode: setToolPermissionMode,
|
|
1615
1799
|
} = await startupPhase("tool-layer", () =>
|
|
1616
1800
|
setupToolLayer(
|
|
1617
1801
|
cfg,
|
|
@@ -1625,8 +1809,10 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1625
1809
|
// F-03 (P5 §C2): background bash job diikat ke sid kanonik sesi ini —
|
|
1626
1810
|
// instance yang diikat sid tidak pernah dipasok lewat argumen tool.
|
|
1627
1811
|
sessionId,
|
|
1628
|
-
// P12 S2.2: ceiling WorkMode
|
|
1629
|
-
|
|
1812
|
+
// P12 S2.2 + P0.2: ceiling WorkMode efektif (termasuk hasil pulihan resume).
|
|
1813
|
+
effectiveWorkMode,
|
|
1814
|
+
// P2.3: pin skema MCP per-workspace.
|
|
1815
|
+
cwd,
|
|
1630
1816
|
),
|
|
1631
1817
|
)
|
|
1632
1818
|
|
|
@@ -1663,7 +1849,7 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1663
1849
|
// saat itu — bukan snapshot lama. Tanpa ini ToolContext.permissionMode
|
|
1664
1850
|
// (hanya PM, bukan WorkMode) membuat delegate_task melahirkan anak tanpa
|
|
1665
1851
|
// ceiling WorkMode.
|
|
1666
|
-
setSubAgentWorkMode(() => workModeControl?.getWorkMode() ??
|
|
1852
|
+
setSubAgentWorkMode(() => workModeControl?.getWorkMode() ?? effectiveWorkMode)
|
|
1667
1853
|
|
|
1668
1854
|
let permissions: PermissionControl | undefined
|
|
1669
1855
|
let noProgressDetector: NoProgressDetector | null = null
|
|
@@ -1687,11 +1873,8 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1687
1873
|
cwd,
|
|
1688
1874
|
permissionMode,
|
|
1689
1875
|
allowLocalConfig,
|
|
1690
|
-
// P12 S2.2 — WorkMode efektif sesi ini (
|
|
1691
|
-
|
|
1692
|
-
// yang memakai permissionHandler terinjeksi (6S/autonomous) akan
|
|
1693
|
-
// di-reject oleh fail-closed di sana (tak pernah melewati ceiling).
|
|
1694
|
-
workMode,
|
|
1876
|
+
// P12 S2.2 + P0.2 — WorkMode efektif sesi ini (termasuk pulihan resume).
|
|
1877
|
+
workMode: effectiveWorkMode,
|
|
1695
1878
|
// [Fase 4 / LOOP-13, LOOP-INV-16] Header operasional deterministik di
|
|
1696
1879
|
// system prompt = buffer kedua yang selalu dikirim dan tak pernah
|
|
1697
1880
|
// dikompaksi; tidak pernah masuk histori kanonik. Absen = perilaku lama.
|
|
@@ -1713,19 +1896,27 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1713
1896
|
if (journalHandle !== null) await journalHandle.preflightIntent(call)
|
|
1714
1897
|
},
|
|
1715
1898
|
onPermissions: (ctl) => {
|
|
1716
|
-
permissions =
|
|
1899
|
+
permissions = {
|
|
1900
|
+
setMode: (m) => {
|
|
1901
|
+
ctl.setMode(m)
|
|
1902
|
+
setToolPermissionMode?.(m)
|
|
1903
|
+
},
|
|
1904
|
+
getMode: () => ctl.getMode(),
|
|
1905
|
+
}
|
|
1717
1906
|
},
|
|
1718
1907
|
// P12 S2.2 Slice 5 — handle kontrol WorkMode runtime. Setelah sesi
|
|
1719
1908
|
// dibangun, handle ini di-warisi ke sub-agent/hooks/footer sehingga
|
|
1720
1909
|
// transisi operator berlaku pada sesi yang sedang berjalan.
|
|
1721
1910
|
onWorkMode: (ctl) => {
|
|
1722
|
-
// P12 S2.2 Slice 5 - SATU sumber state: handler WorkMode (ctl).
|
|
1723
|
-
// Visibilitas tool
|
|
1724
|
-
// terpisah) supaya ceiling dan daftar tool tak bisa berbeda.
|
|
1911
|
+
// P12 S2.2 Slice 5 + P0.2 - SATU sumber state: handler WorkMode (ctl).
|
|
1912
|
+
// Visibilitas tool + metadata sesi durable diturunkan dari state sama.
|
|
1725
1913
|
workModeControl = {
|
|
1726
1914
|
setWorkMode: (m) => {
|
|
1727
1915
|
ctl.setWorkMode(m)
|
|
1728
1916
|
setToolWorkMode?.(m)
|
|
1917
|
+
try {
|
|
1918
|
+
saveSessionWorkMode(sessionId, m, cwd, { expectedEpoch })
|
|
1919
|
+
} catch {}
|
|
1729
1920
|
},
|
|
1730
1921
|
getWorkMode: () => ctl.getWorkMode(),
|
|
1731
1922
|
}
|
|
@@ -1772,6 +1963,55 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
1772
1963
|
taskIdentityProvider: createTaskIdentityResolver(cwd ?? process.cwd()),
|
|
1773
1964
|
...(shadowState ? { initialSeq: shadowState.seq, initialTurn, initialTurnStartTs } : {}),
|
|
1774
1965
|
})
|
|
1966
|
+
// P1.6: lifecycle task kanonik dipancarkan pada transisi nyata (bukan hanya
|
|
1967
|
+
// setelah verify-settle). Dedupe per (id,status,revision) agar tidak
|
|
1968
|
+
// double-report saat verify menyelesaikan task yang sama.
|
|
1969
|
+
const taskStatusSeen = new Map<string, string>()
|
|
1970
|
+
const emitTaskStatuses = (): void => {
|
|
1971
|
+
try {
|
|
1972
|
+
// Sinkron: one-shot/exec dapat keluar tepat setelah turn selesai; dynamic
|
|
1973
|
+
// import async bisa tidak sempat berjalan sebelum proses berhenti.
|
|
1974
|
+
const tasks = new TaskStore(cwd ?? process.cwd()).listTasks(sessionId)
|
|
1975
|
+
for (const task of tasks) {
|
|
1976
|
+
const key = `${task.status}:${task.revision}`
|
|
1977
|
+
if (taskStatusSeen.get(task.id) === key) continue
|
|
1978
|
+
taskStatusSeen.set(task.id, key)
|
|
1979
|
+
presentation?.noteTaskStatus({
|
|
1980
|
+
taskId: task.id,
|
|
1981
|
+
status: task.status,
|
|
1982
|
+
...(task.parentId ? { parentId: task.parentId } : {}),
|
|
1983
|
+
...(task.dependsOn?.length ? { dependsOn: [...task.dependsOn] } : {}),
|
|
1984
|
+
revision: task.revision,
|
|
1985
|
+
verification: task.verification
|
|
1986
|
+
? { verdict: task.verification.verdict, checkedAt: task.verification.checkedAt }
|
|
1987
|
+
: { verdict: "unverified", checkedAt: "" },
|
|
1988
|
+
turnId: session.state.turnCount,
|
|
1989
|
+
})
|
|
1990
|
+
}
|
|
1991
|
+
} catch {}
|
|
1992
|
+
}
|
|
1993
|
+
session.events.on("execution:completed", (e) => {
|
|
1994
|
+
const call = (e as { execution?: { call?: { name?: unknown } } }).execution?.call
|
|
1995
|
+
if (call?.name === "todo_write") emitTaskStatuses()
|
|
1996
|
+
})
|
|
1997
|
+
// P2.3: transisi AUTHORITATIVE (claim/verify/merge/cancel/fail) diproyeksikan
|
|
1998
|
+
// setelah commit DB oleh sink TaskStore — bukan ditebak dari perubahan teks.
|
|
1999
|
+
setTaskTransitionSink(({ task }) => {
|
|
2000
|
+
const key = `${task.status}:${task.revision}`
|
|
2001
|
+
if (taskStatusSeen.get(task.id) === key) return
|
|
2002
|
+
taskStatusSeen.set(task.id, key)
|
|
2003
|
+
presentation?.noteTaskStatus({
|
|
2004
|
+
taskId: task.id,
|
|
2005
|
+
status: task.status,
|
|
2006
|
+
...(task.parentId ? { parentId: task.parentId } : {}),
|
|
2007
|
+
...(task.dependsOn?.length ? { dependsOn: [...task.dependsOn] } : {}),
|
|
2008
|
+
revision: task.revision,
|
|
2009
|
+
verification: task.verification
|
|
2010
|
+
? { verdict: task.verification.verdict, checkedAt: task.verification.checkedAt }
|
|
2011
|
+
: { verdict: "unverified", checkedAt: "" },
|
|
2012
|
+
turnId: session.state.turnCount,
|
|
2013
|
+
})
|
|
2014
|
+
})
|
|
1775
2015
|
try {
|
|
1776
2016
|
if (!shadowState) shadowState = createInitialState(sessionId)
|
|
1777
2017
|
if (!shadowDiag) shadowDiag = createReducerDiagnostics()
|
|
@@ -2158,7 +2398,11 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2158
2398
|
exitCode: number
|
|
2159
2399
|
} | null = null
|
|
2160
2400
|
setCompletionEvidence(() => {
|
|
2161
|
-
|
|
2401
|
+
const liveWm = workModeControl?.getWorkMode() ?? effectiveWorkMode
|
|
2402
|
+
const livePm = permissions?.getMode() ?? permissionMode
|
|
2403
|
+
if (!verifyActive || liveWm !== "EXECUTE" || livePm === "readonly" || livePm === "plan") {
|
|
2404
|
+
return { verdict: "unverified" }
|
|
2405
|
+
}
|
|
2162
2406
|
// [Fase 3 / LOOP-INV-12, LOOP-G20] Saat `--verify` aktif tetapi verifier
|
|
2163
2407
|
// belum menghasilkan verdict pertama, tandai `requireVerified: true` agar
|
|
2164
2408
|
// klaim `completed` ditahan di `VERIFYING` (bukan diloloskan fail-open).
|
|
@@ -2260,6 +2504,17 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2260
2504
|
insufficientEvidence: !effectiveOk && effectiveOutput.trim().length === 0,
|
|
2261
2505
|
baselineContentChanged: false,
|
|
2262
2506
|
})
|
|
2507
|
+
// F-CTRL-1/F-CTRL-2: keputusan consequence terstruktur masuk observability
|
|
2508
|
+
// (bukan hanya stderr). VERIFYING / Tamper Guard kini terlihat lewat
|
|
2509
|
+
// `newEvidenceSinceLastAttempt` + `failureClass`/`nextAction`.
|
|
2510
|
+
presentation?.noteConsequenceDecided({
|
|
2511
|
+
verdict: lastAceDecision.verdict,
|
|
2512
|
+
failureClass: lastAceDecision.failureClass,
|
|
2513
|
+
nextAction: lastAceDecision.nextAction,
|
|
2514
|
+
rationale: lastAceDecision.rationale,
|
|
2515
|
+
newEvidenceSinceLastAttempt: lastAceDecision.newEvidenceSinceLastAttempt,
|
|
2516
|
+
turnId: session.state.turnCount,
|
|
2517
|
+
})
|
|
2263
2518
|
if (lastAceDecision && lastAceDecision.verdict === "BLOCKED") {
|
|
2264
2519
|
process.stderr.write(
|
|
2265
2520
|
c.dim(
|
|
@@ -2292,6 +2547,10 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2292
2547
|
summary: effectiveOutput.slice(0, 300) || verifyCommand,
|
|
2293
2548
|
integrityOk: integrityOk && !tampered,
|
|
2294
2549
|
})
|
|
2550
|
+
// F-TASK-1/P1.6: status task kanonik (t<n>) + DAG diamati sebagai event,
|
|
2551
|
+
// bukan hanya efek samping TaskStore. Dedupe yang sama dengan jalur
|
|
2552
|
+
// todo_write agar satu transisi tidak dilaporkan dua kali.
|
|
2553
|
+
emitTaskStatuses()
|
|
2295
2554
|
} catch {}
|
|
2296
2555
|
const evidence = parseVerifyTestEvidence(verifyCommand, effectiveOutput)
|
|
2297
2556
|
if (evidence) {
|
|
@@ -2321,25 +2580,44 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2321
2580
|
// `hooksAllowed` saat session dibuat) supaya transisi WorkMode operator
|
|
2322
2581
|
// berlaku pada sesi yang sedang berjalan.
|
|
2323
2582
|
let hooksSkippedNotice = false
|
|
2583
|
+
let verifySkippedNotice = false
|
|
2324
2584
|
const runHooksGated = async (
|
|
2325
2585
|
phase: "pre" | "post",
|
|
2326
2586
|
ctx: { phase: "pre" | "post"; prompt: string; cwd?: string; result?: unknown },
|
|
2327
2587
|
signal?: AbortSignal,
|
|
2328
2588
|
): Promise<void> => {
|
|
2329
|
-
const liveWorkMode = workModeControl?.getWorkMode() ??
|
|
2330
|
-
|
|
2589
|
+
const liveWorkMode = workModeControl?.getWorkMode() ?? effectiveWorkMode
|
|
2590
|
+
const livePm = permissions?.getMode() ?? permissionMode
|
|
2591
|
+
if (!shouldRunHooks(livePm, liveWorkMode)) {
|
|
2331
2592
|
if (!hooksSkippedNotice) {
|
|
2332
2593
|
hooksSkippedNotice = true
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
)
|
|
2594
|
+
const reasonLabel =
|
|
2595
|
+
liveWorkMode !== "EXECUTE" ? `work-mode ${liveWorkMode}` : `${livePm} mode`
|
|
2596
|
+
process.stderr.write(c.dim(`[hooks] skipped in ${reasonLabel} (read-only contract)\n`))
|
|
2336
2597
|
}
|
|
2337
2598
|
return
|
|
2338
2599
|
}
|
|
2339
2600
|
await runRunHooks(phase, ctx, signal)
|
|
2340
2601
|
}
|
|
2341
2602
|
|
|
2603
|
+
/**
|
|
2604
|
+
* Guard single-session: tolak run kedua dari sesi lain sebelum menyentuh
|
|
2605
|
+
* provider/jurnal. Release di finally — termasuk saat turn melempar.
|
|
2606
|
+
*/
|
|
2342
2607
|
async function runPromptWithVerify(p: string, signal?: AbortSignal): Promise<void> {
|
|
2608
|
+
if (activeAgentSessionId !== null && activeAgentSessionId !== sessionId) {
|
|
2609
|
+
throw new ConcurrentAgentSessionError(activeAgentSessionId, sessionId)
|
|
2610
|
+
}
|
|
2611
|
+
const previous = activeAgentSessionId
|
|
2612
|
+
activeAgentSessionId = sessionId
|
|
2613
|
+
try {
|
|
2614
|
+
return await runPromptWithVerifyImpl(p, signal)
|
|
2615
|
+
} finally {
|
|
2616
|
+
if (activeAgentSessionId === sessionId) activeAgentSessionId = previous ?? null
|
|
2617
|
+
}
|
|
2618
|
+
}
|
|
2619
|
+
|
|
2620
|
+
async function runPromptWithVerifyImpl(p: string, signal?: AbortSignal): Promise<void> {
|
|
2343
2621
|
// [Fase 7 / Live #3] Root wall-clock: kalau timeout CLI sudah lewat saat
|
|
2344
2622
|
// turn mau mulai (setup/retry memakan seluruh budget), tolak langsung
|
|
2345
2623
|
// dengan alasan jelas — jangan biarkan turn baru melebihi deadline total.
|
|
@@ -2435,6 +2713,20 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2435
2713
|
getTokens: () => usage.getSession().totalTokens,
|
|
2436
2714
|
getTurns: () => session.state.turnCount,
|
|
2437
2715
|
onOver: (st, cost) => {
|
|
2716
|
+
// F-CTX-2: pemotongan mi-stream yang durable → masuk transkrip.
|
|
2717
|
+
presentation?.noteContextCutoff({
|
|
2718
|
+
reason:
|
|
2719
|
+
st === "max-turns"
|
|
2720
|
+
? `max turns (${effectiveMaxTurns}) reached`
|
|
2721
|
+
: st === "over" && cost != null && budget != null
|
|
2722
|
+
? `budget exceeded: ${formatUsd(cost)} > ${formatUsd(budget)}`
|
|
2723
|
+
: "budget exceeded: cost unknown (model without pricing)",
|
|
2724
|
+
// Occupancy konteks (bukan spend sesi) — budget cutoff adalah
|
|
2725
|
+
// peristiwa berbeda; field tetap konsisten dengan meter context.
|
|
2726
|
+
usedTokens: session.contextTokens,
|
|
2727
|
+
windowTokens: contextWindowTokens ?? 0,
|
|
2728
|
+
turnId: session.state.turnCount,
|
|
2729
|
+
})
|
|
2438
2730
|
process.stderr.write(
|
|
2439
2731
|
st === "max-turns"
|
|
2440
2732
|
? `[budget] max turns (${effectiveMaxTurns}) reached - stopping turn.\n`
|
|
@@ -2465,6 +2757,26 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2465
2757
|
getInputTokens: () => usage.get().inputTokens,
|
|
2466
2758
|
getOutputTokens: () => usage.get().outputTokens,
|
|
2467
2759
|
onOver: (allowed, actual) => {
|
|
2760
|
+
const windowTokens = contextWindowTokens ?? 0
|
|
2761
|
+
// F-CTX-1 + P1.4: meter occupancy konteks (bukan output cutoff).
|
|
2762
|
+
// `actual` adalah token output yang memicu cutoff; occupancy adalah
|
|
2763
|
+
// ukuran konteks saat ini dari kernel. Dua angka ini tidak boleh
|
|
2764
|
+
// saling menggantikan.
|
|
2765
|
+
const occupancy = session.contextTokens
|
|
2766
|
+
presentation?.noteContextWindow({
|
|
2767
|
+
usedTokens: occupancy,
|
|
2768
|
+
windowTokens,
|
|
2769
|
+
percent: windowTokens > 0 ? Math.min(100, (occupancy / windowTokens) * 100) : undefined,
|
|
2770
|
+
costUsd: usage.getSession().cost,
|
|
2771
|
+
turnId: session.state.turnCount,
|
|
2772
|
+
})
|
|
2773
|
+
// F-CTX-2: pemotongan mi-stream yang durable → masuk transkrip.
|
|
2774
|
+
presentation?.noteContextCutoff({
|
|
2775
|
+
reason: `context window exceeded: output ${actual} > allowance ${allowed} (window ${windowTokens})`,
|
|
2776
|
+
usedTokens: actual,
|
|
2777
|
+
windowTokens,
|
|
2778
|
+
turnId: session.state.turnCount,
|
|
2779
|
+
})
|
|
2468
2780
|
process.stderr.write(
|
|
2469
2781
|
`[context] stream truncated: context-window limit reached (output ${actual} > allowance ${allowed} of window ${contextWindowTokens})\n`,
|
|
2470
2782
|
)
|
|
@@ -2516,7 +2828,19 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2516
2828
|
turnStatus?.endTurn()
|
|
2517
2829
|
}
|
|
2518
2830
|
}
|
|
2519
|
-
|
|
2831
|
+
const liveWm = workModeControl?.getWorkMode() ?? effectiveWorkMode
|
|
2832
|
+
const livePm = permissions?.getMode() ?? permissionMode
|
|
2833
|
+
const verifyRunnableNow =
|
|
2834
|
+
verifyActive && liveWm === "EXECUTE" && livePm !== "readonly" && livePm !== "plan"
|
|
2835
|
+
if (!verifyRunnableNow) {
|
|
2836
|
+
// P0.2 (Q4 / Invariant #8): bila operator menurunkan WorkMode ke DISCUSS/
|
|
2837
|
+
// PLAN (atau PM ke readonly/plan) di tengah sesi TUI, lewati baseline
|
|
2838
|
+
// shell verify, self-heal, dan auto-memory write secara otomatis.
|
|
2839
|
+
if (verifyActive && !verifySkippedNotice) {
|
|
2840
|
+
verifySkippedNotice = true
|
|
2841
|
+
const modeLabel = liveWm !== "EXECUTE" ? `work-mode ${liveWm}` : `${livePm} mode`
|
|
2842
|
+
warnLine(`verify skipped in ${modeLabel} (read-only contract)`)
|
|
2843
|
+
}
|
|
2520
2844
|
await runOnce(p, signal)
|
|
2521
2845
|
await runHooksGated(
|
|
2522
2846
|
"post",
|
|
@@ -2638,6 +2962,9 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2638
2962
|
quiet: enterRepl === true || machineOutput,
|
|
2639
2963
|
getSnapshot: getPresentationSnapshot,
|
|
2640
2964
|
onPresentationEvent,
|
|
2965
|
+
// [Area A · PROMPT 06] State sesi — buffer /copy & error tertunda
|
|
2966
|
+
// bertahan lintas re-attach per turn dan tak bocor ke sesi lain.
|
|
2967
|
+
uiState,
|
|
2641
2968
|
policy: {
|
|
2642
2969
|
describeActivity,
|
|
2643
2970
|
matchTurn: matchTurnBySummary,
|
|
@@ -2714,6 +3041,7 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
2714
3041
|
try {
|
|
2715
3042
|
await saveSession(sessionId, cwd, undefined, durableHistory, usageData, {
|
|
2716
3043
|
expectedEpoch,
|
|
3044
|
+
workMode: workModeControl?.getWorkMode() ?? effectiveWorkMode,
|
|
2717
3045
|
...(currentRunId ? { runId: currentRunId } : {}),
|
|
2718
3046
|
})
|
|
2719
3047
|
} catch (e) {
|
|
@@ -3141,6 +3469,8 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
3141
3469
|
} catch {}
|
|
3142
3470
|
presentation = null
|
|
3143
3471
|
setAskApprovalHook(undefined)
|
|
3472
|
+
// P2.3: lepas sink transisi task milik sesi ini (single-session).
|
|
3473
|
+
setTaskTransitionSink(null)
|
|
3144
3474
|
// background job harus mati bersama CLI — jangan tinggalkan proses yatim
|
|
3145
3475
|
killAllBackgroundJobs()
|
|
3146
3476
|
await mcpCloseAll()
|
|
@@ -3213,7 +3543,9 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
3213
3543
|
effectiveInitialModel,
|
|
3214
3544
|
effectiveTimeoutMs,
|
|
3215
3545
|
permissionMode,
|
|
3216
|
-
workMode
|
|
3546
|
+
get workMode() {
|
|
3547
|
+
return workModeControl?.getWorkMode() ?? effectiveWorkMode
|
|
3548
|
+
},
|
|
3217
3549
|
workModeControl,
|
|
3218
3550
|
allowLocalConfig: allowLocalConfig === true,
|
|
3219
3551
|
sessionTools,
|
|
@@ -3221,10 +3553,26 @@ export async function createCliSession(opts: CliSessionOptions): Promise<CliSess
|
|
|
3221
3553
|
usage,
|
|
3222
3554
|
budget,
|
|
3223
3555
|
budgetStrict,
|
|
3556
|
+
contextWindowTokens,
|
|
3224
3557
|
memoryHits,
|
|
3225
3558
|
detachSimple: () => detachUI(),
|
|
3559
|
+
// [Area A · PROMPT 06] State UI sesi ini (lihat interface CliSession).
|
|
3560
|
+
uiState,
|
|
3226
3561
|
persistCurrent,
|
|
3227
3562
|
runPromptWithVerify,
|
|
3563
|
+
runState: () => ({
|
|
3564
|
+
runId: currentRunId,
|
|
3565
|
+
status:
|
|
3566
|
+
currentRunId === null
|
|
3567
|
+
? null
|
|
3568
|
+
: lastTurnOutcome === "completed"
|
|
3569
|
+
? "COMPLETED"
|
|
3570
|
+
: lastTurnOutcome === "failed"
|
|
3571
|
+
? "FAILED"
|
|
3572
|
+
: lastTurnOutcome === "interrupted"
|
|
3573
|
+
? "INTERRUPTED"
|
|
3574
|
+
: "RUNNING",
|
|
3575
|
+
}),
|
|
3228
3576
|
// P11.1 — baca TERAKHIR-menang: self-heal menimpa lastVerify tiap
|
|
3229
3577
|
// siklus, jadi yang terbaca di sini selalu hasil final. Salin nilai
|
|
3230
3578
|
// (bukan referensi) agar summary mesin tak bisa memutasi state sesi.
|