pantheon-opencode 1.5.0-beta.2 → 1.5.0-beta.20

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.
Files changed (107) hide show
  1. package/.pantheon/code-mode/manifest.json +21 -0
  2. package/AGENTS.md +14 -63
  3. package/CHANGELOG.md +1260 -0
  4. package/README.md +162 -23
  5. package/README.pt-BR.md +104 -19
  6. package/bin/pantheon-init.mjs +178 -21
  7. package/commands/pantheon.md +1 -35
  8. package/package.json +23 -15
  9. package/plugin.json +1 -1
  10. package/pyproject.toml +62 -0
  11. package/scripts/code_mode_server.py +431 -55
  12. package/scripts/doctor.mjs +207 -9
  13. package/scripts/install/cli-ui.mjs +17 -11
  14. package/scripts/install/config-migration.mjs +4 -6
  15. package/scripts/install/opencode.mjs +137 -35
  16. package/scripts/install/plugin.mjs +12 -45
  17. package/scripts/install/shared.mjs +43 -2
  18. package/scripts/install/strings.mjs +129 -0
  19. package/scripts/install-mcp.mjs +1 -0
  20. package/scripts/manifest-inventory.mjs +410 -0
  21. package/scripts/mcp_persistence_server.py +536 -164
  22. package/scripts/package-evidence.mjs +242 -0
  23. package/scripts/probe-pantheon-cost.mjs +10 -1
  24. package/scripts/release-beta-version.mjs +56 -60
  25. package/scripts/sync-artifacts.mjs +169 -0
  26. package/scripts/sync-tui.mjs +129 -64
  27. package/scripts/test-opencode-v1-v2-sandbox.sh +79 -161
  28. package/scripts/validation-policy.mjs +7 -7
  29. package/scripts/version-check.mjs +113 -108
  30. package/scripts/versioning.mjs +230 -86
  31. package/src/agents/apollo.md +1 -0
  32. package/src/agents/gaia.md +1 -0
  33. package/src/agents/mnemosyne.md +1 -1
  34. package/src/agents/zeus.md +11 -54
  35. package/src/instructions/memory-protocol.instructions.md +6 -3
  36. package/src/instructions/zeus-anti-stall.instructions.md +2 -4
  37. package/src/instructions/zeus-council-synthesis.instructions.md +3 -28
  38. package/src/instructions/zeus-timeout-retry.instructions.md +3 -28
  39. package/src/mcp/code_mode_server.py +431 -55
  40. package/src/mcp/mcp_persistence_server.py +536 -164
  41. package/src/mcp/toon_codec.py +2 -4
  42. package/src/pantheon/background-job-board.ts +47 -5
  43. package/src/pantheon/{delegation-compaction.ts → compaction-context.ts} +10 -100
  44. package/src/pantheon/cost-command.ts +309 -44
  45. package/src/pantheon/delegation-enforce.ts +59 -14
  46. package/src/pantheon/file-persistence-salvage.ts +64 -0
  47. package/src/pantheon/file-persistence.ts +194 -45
  48. package/src/pantheon/goal-loop.ts +1 -1
  49. package/src/pantheon/goal-store.ts +1 -1
  50. package/src/pantheon/idle-continuation.ts +4 -6
  51. package/src/pantheon/model-command.ts +1 -2
  52. package/src/pantheon/native-task-status.ts +139 -0
  53. package/src/pantheon/presets.d.mts +0 -20
  54. package/src/pantheon/presets.mjs +0 -64
  55. package/src/pantheon/session-guard.ts +2 -2
  56. package/src/pantheon/session-id.ts +30 -0
  57. package/src/pantheon/shared-board.ts +37 -0
  58. package/src/pantheon/task-result-guard.ts +106 -0
  59. package/src/pantheon/todo-enforcer.ts +3 -3
  60. package/src/pantheon/tool-ceiling.ts +159 -0
  61. package/src/pantheon/tool-context.ts +23 -0
  62. package/src/pantheon/v2-bridge.ts +1 -4
  63. package/src/plugin-v2.ts +8 -21
  64. package/src/plugin.ts +83 -251
  65. package/src/plugins/pantheon-hooks.ts +51 -0
  66. package/src/plugins/tui/dist/server.d.ts +1 -1
  67. package/src/plugins/tui/dist/server.d.ts.map +1 -1
  68. package/src/plugins/tui/dist/server.js +1 -1
  69. package/src/plugins/tui/dist/server.js.map +1 -1
  70. package/src/plugins/tui/dist/tui.d.ts +261 -77
  71. package/src/plugins/tui/dist/tui.d.ts.map +1 -1
  72. package/src/plugins/tui/dist/tui.js +640 -234
  73. package/src/plugins/tui/dist/tui.js.map +1 -1
  74. package/src/plugins/tui/package-lock.json +2 -2
  75. package/src/plugins/tui/package.json +2 -2
  76. package/src/plugins/tui/src/index.tsx +872 -226
  77. package/src/routing.yml +0 -16
  78. package/src/skills/context-compression/SKILL.md +1 -1
  79. package/scripts/generate-preset-docs.mjs +0 -300
  80. package/scripts/generate-prompts.sh +0 -222
  81. package/scripts/hash_verify.py +0 -192
  82. package/scripts/install/component.mjs +0 -24
  83. package/scripts/install/migrate-config.mjs +0 -302
  84. package/scripts/test-context-compression.sh +0 -169
  85. package/src/pantheon/auto-wake.ts +0 -89
  86. package/src/pantheon/chat-reminders.ts +0 -9
  87. package/src/pantheon/comment-checker.ts +0 -48
  88. package/src/pantheon/compaction-assert.ts +0 -160
  89. package/src/pantheon/cost-tracker.ts +0 -108
  90. package/src/pantheon/delegate-manager.ts +0 -669
  91. package/src/pantheon/delegation-classifier.ts +0 -307
  92. package/src/pantheon/delegation-finalize.ts +0 -374
  93. package/src/pantheon/delegation-notify.ts +0 -212
  94. package/src/pantheon/delegation-toast.ts +0 -73
  95. package/src/pantheon/delegation.ts +0 -1120
  96. package/src/pantheon/dispatch-guard.ts +0 -109
  97. package/src/pantheon/permission-eval.ts +0 -284
  98. package/src/pantheon/preemptive-compact.ts +0 -76
  99. package/src/pantheon/retry-policy.ts +0 -212
  100. package/src/pantheon/step-cap.ts +0 -110
  101. package/src/pantheon/token-opt.ts +0 -781
  102. package/src/pantheon/toon-codec.ts +0 -481
  103. package/src/pantheon/v2-events.ts +0 -131
  104. package/src/pantheon/v2-hooks.ts +0 -287
  105. package/src/pantheon/v2-tool-definitions.ts +0 -331
  106. package/src/pantheon/zeus-delegate-with-retry.ts +0 -406
  107. package/src/plugins/tui/dist/tui.tsx +0 -2605
@@ -22,7 +22,8 @@
22
22
  */
23
23
 
24
24
  import { spawn as spawnAsync, spawnSync } from 'node:child_process'
25
- import { existsSync, mkdirSync, readdirSync, readFileSync } from 'node:fs'
25
+ import { createHash } from 'node:crypto'
26
+ import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, statSync } from 'node:fs'
26
27
  import { homedir } from 'node:os'
27
28
  import { basename, dirname, join, resolve } from 'node:path'
28
29
  import { fileURLToPath } from 'node:url'
@@ -389,7 +390,7 @@ function checkMcpConfig(args) {
389
390
  // ENOENT-on-spawn failure class (dead hardcoded path, ambiguous relative
390
391
  // path) before opencode tries to spawn the server.
391
392
  for (const [name, mcpEntry] of Object.entries(mcp)) {
392
- if (!mcpEntry || mcpEntry.type !== 'local') continue
393
+ if (mcpEntry?.type !== 'local') continue
393
394
  const cmd = mcpEntry.command
394
395
  if (!Array.isArray(cmd) || cmd.length === 0) {
395
396
  warn(`${cfg.label}: MCP "${name}" has no command array`)
@@ -530,7 +531,7 @@ async function checkMcpRuntimeSmoke(args) {
530
531
  const local = []
531
532
  for (const cfg of collectMcpConfigs(args)) {
532
533
  for (const [name, entry] of Object.entries(cfg.data.mcp ?? {})) {
533
- if (!entry || entry.type !== 'local') continue
534
+ if (entry?.type !== 'local') continue
534
535
  if (!Array.isArray(entry.command) || entry.command.length === 0) continue
535
536
  const cwd = resolveMcpCwd(entry, cfg.path)
536
537
  const command = entry.command.map((part, index) =>
@@ -958,6 +959,35 @@ function checkVenvLayer(args) {
958
959
  // Check F.2: Code-Mode Scripts Directory
959
960
  // ---------------------------------------------------------------------------
960
961
 
962
+ /**
963
+ * Return whether *dir* contains an executable code-mode target.
964
+ *
965
+ * Mirrors `_has_usable_scripts` in `scripts/code_mode_server.py`: a directory
966
+ * is only a valid resolver candidate when it is a real (non-symlink) directory
967
+ * containing a regular `.py`/`.sh` direct child. This prevents an empty project
968
+ * overlay from masking a lower-priority installation that actually ships
969
+ * scripts. Keep both implementations in sync (see tests/test_doctor_layers.mjs).
970
+ */
971
+ function hasUsableScripts(dir) {
972
+ try {
973
+ if (!existsSync(dir)) return false
974
+ const stat = lstatSync(dir)
975
+ if (stat.isSymbolicLink() || !stat.isDirectory()) return false
976
+ return readdirSync(dir).some((name) => {
977
+ if (name.startsWith('.')) return false
978
+ const lower = name.toLowerCase()
979
+ if (!lower.endsWith('.py') && !lower.endsWith('.sh')) return false
980
+ try {
981
+ return statSync(join(dir, name)).isFile()
982
+ } catch {
983
+ return false
984
+ }
985
+ })
986
+ } catch {
987
+ return false
988
+ }
989
+ }
990
+
961
991
  /**
962
992
  * Verify the code-mode scripts directory exists for the resolved runtime
963
993
  * root (project → <target>/.opencode, otherwise the global config dir).
@@ -966,12 +996,20 @@ function checkVenvLayer(args) {
966
996
  */
967
997
  export function checkCodeModeDir(args) {
968
998
  section('F.2 Code-Mode Scripts')
969
- const target = resolve(args.target)
970
- const isProject = existsSync(join(target, '.opencode'))
971
- const runtimeRoot = isProject ? target : resolveOpenCodeConfigDir(args.env ?? process.env)
972
- const codeModeDir = isProject
973
- ? join(runtimeRoot, '.opencode', '.pantheon', 'code-mode')
974
- : join(runtimeRoot, '.pantheon', 'code-mode')
999
+ const env = args.env ?? process.env
1000
+ const target = resolve(env.PANTHEON_PROJECT ?? args.target)
1001
+ const projectCandidates = [
1002
+ join(target, '.opencode', '.pantheon', 'code-mode'),
1003
+ join(target, '.pantheon', 'code-mode'),
1004
+ ]
1005
+ // Seeding semantics: an existing project candidate wins; a project
1006
+ // install (.opencode present) gets its runtime dir created when missing.
1007
+ // Only targets with no project layout fall back to the global config dir.
1008
+ const codeModeDir =
1009
+ projectCandidates.find(hasUsableScripts) ??
1010
+ (existsSync(join(target, '.opencode'))
1011
+ ? projectCandidates[0]
1012
+ : join(resolveOpenCodeConfigDir(env), '.pantheon', 'code-mode'))
975
1013
  if (existsSync(codeModeDir)) {
976
1014
  pass(`Code-mode scripts directory exists: ${codeModeDir}`)
977
1015
  return codeModeDir
@@ -986,6 +1024,83 @@ export function checkCodeModeDir(args) {
986
1024
  return codeModeDir
987
1025
  }
988
1026
 
1027
+ /** Resolve code-mode using the same project-first rule as the MCP server. */
1028
+ export function resolveCodeModeDir(args) {
1029
+ const env = args.env ?? process.env
1030
+ const target = resolve(env.PANTHEON_PROJECT ?? args.target)
1031
+ const projectCandidates = [
1032
+ join(target, '.opencode', '.pantheon', 'code-mode'),
1033
+ join(target, '.pantheon', 'code-mode'),
1034
+ ]
1035
+ const projectDir = projectCandidates.find(hasUsableScripts)
1036
+ if (projectDir) return projectDir
1037
+ return join(resolveOpenCodeConfigDir(env), '.pantheon', 'code-mode')
1038
+ }
1039
+
1040
+ /** Validate a code-mode manifest without modifying it. */
1041
+ export function validateCodeModeManifest(codeModeDir) {
1042
+ const manifestPath = join(codeModeDir, 'manifest.json')
1043
+ if (!existsSync(manifestPath)) {
1044
+ return { ok: false, message: `Code-mode manifest missing: ${manifestPath}` }
1045
+ }
1046
+
1047
+ let data
1048
+ try {
1049
+ data = JSON.parse(readFileSync(manifestPath, 'utf8'))
1050
+ } catch {
1051
+ return { ok: false, message: `Code-mode manifest malformed: ${manifestPath}` }
1052
+ }
1053
+ if (!data || typeof data !== 'object' || Array.isArray(data)) {
1054
+ return { ok: false, message: 'Code-mode manifest malformed: root must be an object' }
1055
+ }
1056
+ if (data.version !== 1) {
1057
+ return { ok: false, message: 'Code-mode manifest malformed: version must be 1' }
1058
+ }
1059
+ if (!data.scripts || typeof data.scripts !== 'object' || Array.isArray(data.scripts)) {
1060
+ return { ok: false, message: 'Code-mode manifest malformed: scripts must be an object' }
1061
+ }
1062
+
1063
+ const listed = Object.keys(data.scripts)
1064
+ const invalid = listed.filter(
1065
+ (name) => typeof data.scripts[name] !== 'string' || !/^[a-f0-9]{64}$/i.test(data.scripts[name]),
1066
+ )
1067
+ if (invalid.length) {
1068
+ return { ok: false, message: `Code-mode manifest invalid digest: ${invalid.join(', ')}` }
1069
+ }
1070
+
1071
+ const actualScripts = readdirSync(codeModeDir).filter(
1072
+ (name) =>
1073
+ (name.endsWith('.py') || name.endsWith('.sh')) && statSync(join(codeModeDir, name)).isFile(),
1074
+ )
1075
+ const missing = listed.filter((name) => !existsSync(join(codeModeDir, name)))
1076
+ const extra = actualScripts.filter((name) => !Object.hasOwn(data.scripts, name))
1077
+ const differing = listed.filter((name) => {
1078
+ const path = join(codeModeDir, name)
1079
+ if (!existsSync(path)) return false
1080
+ const digest = createHash('sha256').update(readFileSync(path)).digest('hex')
1081
+ return digest !== data.scripts[name].toLowerCase()
1082
+ })
1083
+ if (missing.length || extra.length || differing.length) {
1084
+ const names = [...new Set([...missing, ...extra, ...differing])]
1085
+ return {
1086
+ ok: false,
1087
+ message: `Code-mode manifest mismatch: ${differing.length + missing.length + extra.length}/${listed.length} scripts differ — ${names.join(', ')}`,
1088
+ }
1089
+ }
1090
+ return { ok: true, count: listed.length, total: actualScripts.length }
1091
+ }
1092
+
1093
+ /** Run the diagnostic manifest check after directory validation. */
1094
+ export function checkCodeModeManifest(args) {
1095
+ const codeModeDir = resolveCodeModeDir(args)
1096
+ const result = validateCodeModeManifest(codeModeDir)
1097
+ if (!result.ok) {
1098
+ error(result.message)
1099
+ return
1100
+ }
1101
+ pass(`✅ Code-mode manifest valid: ${result.count}/${result.total} scripts verified`)
1102
+ }
1103
+
989
1104
  /** Resolve the venv created by init for either a project or global install. */
990
1105
  export function resolveRuntimePython(args) {
991
1106
  const target = resolve(args.target)
@@ -1018,6 +1133,87 @@ export function resolveMcpCwd(entry, configPath = '') {
1018
1133
  return dirname(configPath)
1019
1134
  }
1020
1135
 
1136
+ // ---------------------------------------------------------------------------
1137
+ // Check H2: installed-version drift (beta.5)
1138
+ // ---------------------------------------------------------------------------
1139
+
1140
+ /**
1141
+ * Prerelease-aware comparison for pantheon versions (X.Y.Z or X.Y.Z-beta.N).
1142
+ * A stable release outranks any beta of the same core version. Returns
1143
+ * <0, 0, >0 (a - b). The shared compareVersions/parseVersion above ignores
1144
+ * the `-beta.N` suffix (it only reads 4 numeric groups), which would make
1145
+ * every beta pair compare equal.
1146
+ */
1147
+ function comparePantheonVersions(a, b) {
1148
+ const parse = (v) => {
1149
+ const m = String(v)
1150
+ .trim()
1151
+ .match(/^(\d+)\.(\d+)\.(\d+)(?:-beta\.(\d+))?$/)
1152
+ if (!m) return null
1153
+ return {
1154
+ core: [+m[1], +m[2], +m[3]],
1155
+ beta: m[4] === undefined ? Number.POSITIVE_INFINITY : +m[4],
1156
+ }
1157
+ }
1158
+ const pa = parse(a)
1159
+ const pb = parse(b)
1160
+ if (!pa || !pb) return 0
1161
+ for (let i = 0; i < 3; i++) {
1162
+ if (pa.core[i] !== pb.core[i]) return pa.core[i] - pb.core[i]
1163
+ }
1164
+ return pa.beta - pb.beta
1165
+ }
1166
+
1167
+ /**
1168
+ * Compare the version recorded in the installation marker
1169
+ * (<config>/.pantheon/install-state.json, written by the postinstall sync)
1170
+ * against this package's version. When the package is newer, the config
1171
+ * merge/venv/MCP wiring may be stale — point the user at `update`.
1172
+ * Advisory only: a missing marker (pre-beta.5 install) is not an error.
1173
+ */
1174
+ export function checkInstallVersionDrift(args) {
1175
+ section('H2 Installed Version')
1176
+ const env = args.env ?? process.env
1177
+ let pkgVersion = null
1178
+ try {
1179
+ pkgVersion = JSON.parse(readFileSync(join(ROOT, 'package.json'), 'utf8')).version
1180
+ } catch {
1181
+ return 'no-marker'
1182
+ }
1183
+ if (!pkgVersion) return 'no-marker'
1184
+
1185
+ const statePath = join(resolveOpenCodeConfigDir(env), '.pantheon', 'install-state.json')
1186
+ let installed = null
1187
+ if (existsSync(statePath)) {
1188
+ try {
1189
+ installed = JSON.parse(readFileSync(statePath, 'utf8')).pantheon_version ?? null
1190
+ } catch {
1191
+ installed = null
1192
+ }
1193
+ }
1194
+ if (!installed) {
1195
+ info('No installation version marker found (pre-beta.5 install) — skipping drift check')
1196
+ return 'no-marker'
1197
+ }
1198
+ const drift = comparePantheonVersions(pkgVersion, installed)
1199
+ if (drift > 0) {
1200
+ warn(
1201
+ `Package v${pkgVersion} is newer than the last-synced installation (v${installed}) — ` +
1202
+ 'run `npx pantheon-opencode update` (or re-run init) to refresh config, venv and MCP entries',
1203
+ )
1204
+ return 'package-newer'
1205
+ }
1206
+ if (drift < 0) {
1207
+ warn(
1208
+ `Installation marker (v${installed}) is newer than this package (v${pkgVersion}) — ` +
1209
+ 'the doctor is running from a different install than the last sync',
1210
+ )
1211
+ return 'install-newer'
1212
+ }
1213
+ pass(`Installation in sync with package v${pkgVersion}`)
1214
+ return 'sync'
1215
+ }
1216
+
1021
1217
  // ---------------------------------------------------------------------------
1022
1218
  // Check G: AGENTS.md freshness
1023
1219
  // ---------------------------------------------------------------------------
@@ -1314,6 +1510,8 @@ async function main() {
1314
1510
  checkMcpConfig(args)
1315
1511
  checkVenvLayer(args)
1316
1512
  checkCodeModeDir(args)
1513
+ checkCodeModeManifest(args)
1514
+ checkInstallVersionDrift(args)
1317
1515
  await checkMcpRuntimeSmoke(args)
1318
1516
  checkPermissionMismatches(args)
1319
1517
  checkSyncStatus(args)
@@ -1,3 +1,5 @@
1
+ import { strings } from './strings.mjs'
2
+
1
3
  export const icons = {
2
4
  success: '\u2705',
3
5
  warning: '\u26a0\ufe0f ',
@@ -8,9 +10,9 @@ export const icons = {
8
10
  arrow: '\u2192',
9
11
  bullet: '\u2022',
10
12
  star: '\u2b50',
11
- wrench: '\U0001f527',
12
- rocket: '\U0001f680',
13
- package: '\U0001f4e6',
13
+ wrench: '\u{1F527}',
14
+ rocket: '\u{1F680}',
15
+ package: '\u{1F4E6}',
14
16
  gear: '\u2699\ufe0f',
15
17
  }
16
18
 
@@ -160,28 +162,32 @@ export function spinner(message) {
160
162
  export function printSummary(target, platforms, stats) {
161
163
  if (_quiet) return
162
164
 
165
+ const S = strings()
163
166
  const line = '\u2500'.repeat(Math.min(process.stdout.columns || 60, 60))
164
167
  const hasErrors = stats && stats.errors > 0
165
168
  console.log(
166
169
  tag(
167
170
  hasErrors
168
- ? `${icons.warning} Instalação concluída com erros em ${target}`
169
- : `${icons.rocket} OpenCode instalado em ${target}`,
171
+ ? `${icons.warning} ${S.summaryWithErrors(target)}`
172
+ : `${icons.rocket} ${S.summaryInstalled(target)}`,
170
173
  ),
171
174
  )
172
175
  console.log(tag(line))
173
176
  console.log('')
174
177
 
175
178
  if (stats) {
176
- console.log(tag(` Componentes:\t${stats.created} instalados, ${stats.skipped} pulados`))
179
+ console.log(tag(S.summaryComponents(stats.created, stats.skipped)))
177
180
  if (stats.errors > 0) {
178
- console.warn(tag(` ${icons.warning}${stats.errors} erro(s) encontrados`))
181
+ console.warn(tag(S.summaryErrors(stats.errors)))
182
+ }
183
+ if (stats.warnings > 0) {
184
+ console.warn(tag(S.summaryWarnings(stats.warnings)))
179
185
  }
180
186
  console.log('')
181
187
  }
182
188
 
183
- console.log(tag(` ${icons.star} Próximos passos:`))
184
- console.log(tag(` ${icons.bullet} Configure seus agentes em opencode.json`))
185
- console.log(tag(` ${icons.bullet} Adicione MCP servers em mcp.json`))
186
- console.log(tag(` ${icons.bullet} Rode 'opencode doctor' para verificar a instalação`))
189
+ console.log(tag(` ${icons.star} ${S.summaryNext.trim()}`))
190
+ console.log(tag(S.summaryNextAgents))
191
+ console.log(tag(S.summaryNextMcp))
192
+ console.log(tag(S.summaryNextDoctor))
187
193
  }
@@ -73,7 +73,7 @@ function convertPermissionsV2toV1(permArray) {
73
73
  const result = {}
74
74
  // Reverse action mapping: V2 action → V1 key
75
75
  const ACTION_TO_V1 = Object.fromEntries(
76
- Object.entries(V1_PERMISSION_TO_ACTION).map(([k, v]) => [v, k])
76
+ Object.entries(V1_PERMISSION_TO_ACTION).map(([k, v]) => [v, k]),
77
77
  )
78
78
 
79
79
  for (const { action, resource, effect } of permArray) {
@@ -104,7 +104,7 @@ const PROVIDER_RENAME = {
104
104
 
105
105
  /** Reverse: V2 → V1 */
106
106
  const PROVIDER_RENAME_REVERSE = Object.fromEntries(
107
- Object.entries(PROVIDER_RENAME).map(([k, v]) => [v, k])
107
+ Object.entries(PROVIDER_RENAME).map(([k, v]) => [v, k]),
108
108
  )
109
109
 
110
110
  /**
@@ -176,9 +176,7 @@ function convertProviderV2toV1(name, config) {
176
176
 
177
177
  // package → npm (strip aisdk: prefix)
178
178
  if (result.package !== undefined) {
179
- result.npm = result.package.startsWith('aisdk:')
180
- ? result.package.slice(6)
181
- : result.package
179
+ result.npm = result.package.startsWith('aisdk:') ? result.package.slice(6) : result.package
182
180
  delete result.package
183
181
  }
184
182
 
@@ -303,7 +301,7 @@ const V1_TO_V2_RENAMES = {
303
301
 
304
302
  /** Build reverse map */
305
303
  const V2_TO_V1_RENAMES = Object.fromEntries(
306
- Object.entries(V1_TO_V2_RENAMES).map(([k, v]) => [v, k])
304
+ Object.entries(V1_TO_V2_RENAMES).map(([k, v]) => [v, k]),
307
305
  )
308
306
 
309
307
  // ---------------------------------------------------------------------------
@@ -12,6 +12,7 @@
12
12
  * isolation is handled at runtime via OPENCODE_DB.
13
13
  */
14
14
 
15
+ import { createHash } from 'node:crypto'
15
16
  import { chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync } from 'node:fs'
16
17
  import { homedir } from 'node:os'
17
18
  import { dirname, isAbsolute, join, resolve } from 'node:path'
@@ -34,6 +35,7 @@ import { detectVersion, runMigrations } from './migrate.mjs'
34
35
  import { resolveOpenCodeVersion } from './opencode-version.mjs'
35
36
  import { copyPluginFiles, installPlugin, registerPlugin, unregisterPlugin } from './plugin.mjs'
36
37
  import {
38
+ checkRuntimePrerequisites,
37
39
  collectSkillNames,
38
40
  copyFiles,
39
41
  installSkills,
@@ -42,8 +44,10 @@ import {
42
44
  sourceDirValid,
43
45
  summary,
44
46
  syncDir,
47
+ writeConfigWithBackup,
45
48
  writeIfChanged,
46
49
  } from './shared.mjs'
50
+ import { strings } from './strings.mjs'
47
51
 
48
52
  import { setupVenv, venvPythonPath } from './venv.mjs'
49
53
 
@@ -61,6 +65,34 @@ function deepClone(value) {
61
65
  return JSON.parse(JSON.stringify(value))
62
66
  }
63
67
 
68
+ /**
69
+ * (Re)generate the code-mode manifest with SHA-256 digests for every bundled
70
+ * script. This is the explicit installation opt-in: the MCP server only
71
+ * executes scripts listed here whose on-disk hash matches. Missing or empty
72
+ * directories approve nothing (0) — execution is never implicit.
73
+ */
74
+ function writeCodeModeManifest(dir, dryRun) {
75
+ if (!existsSync(dir)) return 0
76
+ let names
77
+ try {
78
+ names = readdirSync(dir)
79
+ .filter((name) => /\.(sh|py)$/i.test(name) && !name.startsWith('.'))
80
+ .sort()
81
+ } catch {
82
+ return 0
83
+ }
84
+ if (names.length === 0) return 0
85
+ const scripts = {}
86
+ for (const name of names) {
87
+ scripts[name] = createHash('sha256')
88
+ .update(readFileSync(join(dir, name)))
89
+ .digest('hex')
90
+ }
91
+ const payload = `${JSON.stringify({ version: 1, scripts }, null, 2)}\n`
92
+ writeIfChanged(join(dir, 'manifest.json'), payload, dryRun)
93
+ return names.length
94
+ }
95
+
64
96
  function readJsonConfig(filePath) {
65
97
  if (!existsSync(filePath)) return {}
66
98
 
@@ -185,6 +217,21 @@ function managedPluginIdentity(ref) {
185
217
  if (absolutePath === normalizePluginPath(resolve(ROOT, PANTHEON_V2_LEGACY_FILE))) {
186
218
  return PANTHEON_V2_PLUGIN
187
219
  }
220
+ // beta.5: stale refs from OTHER installed copies of this package (older
221
+ // nvm prefixes, npx cache hashes). Only paths inside
222
+ // `node_modules/pantheon-opencode/` count as installed copies — a dev
223
+ // checkout elsewhere (e.g. a vendor folder named pantheon-opencode) remains
224
+ // protected by the third-party rule above.
225
+ const installedMarker = '/node_modules/pantheon-opencode/'
226
+ const withSlashes = normalizePluginPath(normalized)
227
+ const markerIndex = withSlashes.lastIndexOf(installedMarker)
228
+ if (markerIndex !== -1) {
229
+ const suffix = withSlashes.slice(markerIndex + installedMarker.length)
230
+ if (suffix === PANTHEON_V1_PLUGIN) return PANTHEON_V1_PLUGIN
231
+ if (suffix === PANTHEON_V1_HOOKS) return PANTHEON_V1_HOOKS
232
+ if (suffix === PANTHEON_V2_PLUGIN) return PANTHEON_V2_PLUGIN
233
+ if (suffix === PANTHEON_V2_LEGACY_FILE) return PANTHEON_V2_PLUGIN
234
+ }
188
235
  }
189
236
 
190
237
  return null
@@ -343,6 +390,45 @@ export function syncTuiRegistration(target, { isGlobal = false, dryRun = false }
343
390
  return registerPlugin(targetTuiConfigPath, tuiCopyDir, { dryRun })
344
391
  }
345
392
 
393
+ /**
394
+ * beta.5: set up the Python venv + run the health check. Never throws —
395
+ * failures are reported as warnings and return false so the caller omits
396
+ * MCP entries from the config (entries pointing at a missing venv would
397
+ * make every MCP fail to launch). `deps` is injectable for tests.
398
+ */
399
+ export function setupRuntimePhase(target, { dryRun, clean, isGlobal }, stats, deps = {}) {
400
+ const S = strings()
401
+ const setup = deps.setupVenv ?? setupVenv
402
+ const check = deps.healthCheck ?? healthCheck
403
+ try {
404
+ const venvSpinner = spinner(S.settingUpRuntime)
405
+ setup(target, { dryRun, force: clean })
406
+ venvSpinner(true)
407
+ const runtimeTarget = isGlobal ? target : join(target, '.opencode')
408
+ const health = check(runtimeTarget, { dryRun, pythonTarget: target })
409
+
410
+ section(`\uD83D\uDD0D ${S.healthSection}`)
411
+ const healthStep = step(S.runningHealthChecks)
412
+ for (const p of health.passed) success(`${p.check}: ${p.detail}`)
413
+ for (const w of health.warnings) warning(`${w.check}: ${w.detail}`)
414
+ for (const f of health.failed) error(`${f.check}: ${f.detail}`)
415
+
416
+ if (health.failed.length > 0) {
417
+ healthStep(false)
418
+ warning(`${health.failed.length} check(s) failed — review above`)
419
+ stats.warnings += health.failed.length
420
+ return false
421
+ }
422
+ healthStep(true)
423
+ return true
424
+ } catch (err) {
425
+ error(S.runtimeFailed(err.message))
426
+ warning(S.runtimeSkippedMcp)
427
+ stats.warnings += 1
428
+ return false
429
+ }
430
+ }
431
+
346
432
  export async function installOpenCode(
347
433
  target,
348
434
  dryRun = false,
@@ -357,6 +443,7 @@ export async function installOpenCode(
357
443
 
358
444
  const componentSet = new Set(components)
359
445
  const stats = summary.opencode
446
+ const S = strings()
360
447
 
361
448
  // V1/V2 dual-version awareness (Phase 3): the installer builds a unified
362
449
  // config, then applies V2-native migration (migrateV1toV2) when
@@ -405,6 +492,17 @@ export async function installOpenCode(
405
492
  const config = readJsonConfig(targetConfigPath)
406
493
  const pantheonConfig = readJsonConfig(pantheonConfigPath)
407
494
 
495
+ // beta.5 preflight: fail BEFORE any files are written when the runtime
496
+ // component needs toolchain pieces that are missing. Nothing has been
497
+ // copied yet at this point, so the failure is clean.
498
+ if (componentSet.has('runtime') && !dryRun) {
499
+ const missing = checkRuntimePrerequisites()
500
+ if (missing.length > 0) {
501
+ for (const problem of missing) error(problem)
502
+ throw new Error(S.prereqMissing)
503
+ }
504
+ }
505
+
408
506
  // Determine layout based on install scope.
409
507
  // Global config dir (~/.opencode or ~/.config/opencode) uses a flat layout:
410
508
  // agents/ skills/ commands/ plugins/
@@ -417,7 +515,7 @@ export async function installOpenCode(
417
515
  configure({ dryRun })
418
516
 
419
517
  if (isGlobal) {
420
- info('Global config directory detected — using flat layout (agents/, skills/, commands/)')
518
+ info(S.globalLayout)
421
519
  }
422
520
 
423
521
  // -----------------------------------------------------------------------
@@ -430,7 +528,7 @@ export async function installOpenCode(
430
528
  warning(`Agent source directory not found: ${srcDir}`)
431
529
  stats.errors++
432
530
  } else {
433
- const agentStep = step('Installing agents')
531
+ const agentStep = step(S.installingAgents)
434
532
  const dstDir = isGlobal ? join(target, 'agents') : join(target, '.opencode', 'agents')
435
533
  if (!dryRun) mkdirSync(dstDir, { recursive: true })
436
534
  if (clean && existsSync(dstDir) && !dryRun) {
@@ -665,6 +763,13 @@ export async function installOpenCode(
665
763
  stats.created++
666
764
  }
667
765
 
766
+ // B3-08: explicit installation mode — seed the manifest with SHA-256
767
+ // digests so the server never executes a script implicitly.
768
+ const approvedScripts = writeCodeModeManifest(dstCodeModeDir, dryRun)
769
+ if (approvedScripts > 0) {
770
+ info(`Code-mode manifest: ${approvedScripts} script(s) approved`)
771
+ }
772
+
668
773
  // ── tiers.json ──
669
774
  const srcTiers = join(ROOT, '.pantheon', 'tiers.json')
670
775
  const dstTiers = join(runtimeTarget, '.pantheon', 'tiers.json')
@@ -676,6 +781,17 @@ export async function installOpenCode(
676
781
  }
677
782
  }
678
783
 
784
+ // -----------------------------------------------------------------------
785
+ // 2.11 Setup virtual environment + health check (--components runtime)
786
+ // beta.5: runs BEFORE the config write and is no longer fatal — on failure
787
+ // the install completes without MCP entries (which would point at a venv
788
+ // that does not exist) instead of aborting a half-done installation.
789
+ // -----------------------------------------------------------------------
790
+ let runtimeHealthy = !componentSet.has('runtime')
791
+ if (componentSet.has('runtime')) {
792
+ runtimeHealthy = setupRuntimePhase(target, { dryRun, clean, isGlobal }, stats)
793
+ }
794
+
679
795
  // -----------------------------------------------------------------------
680
796
  // 3. Create/update opencode.json (always runs)
681
797
  // Reads TARGET's existing config first, then merges Pantheon settings
@@ -1017,8 +1133,10 @@ export async function installOpenCode(
1017
1133
 
1018
1134
  // --------------------------------------------------------------------
1019
1135
  // F.5 MCP server entries (for runtime-deployed scripts)
1136
+ // beta.5: skipped when the Python runtime setup failed — entries pointing
1137
+ // at a venv that does not exist would make every MCP fail to launch.
1020
1138
  // --------------------------------------------------------------------
1021
- if (componentSet.has('runtime')) {
1139
+ if (componentSet.has('runtime') && runtimeHealthy) {
1022
1140
  config.mcp = config.mcp || {}
1023
1141
  // MCP stays a flat named-server map for BOTH V1 and V2: OpenCode 1.18.x
1024
1142
  // has no `mcp.servers` wrapper and rejects one with
@@ -1102,12 +1220,12 @@ export async function installOpenCode(
1102
1220
  // before writing. V1 path is untouched (zero regression).
1103
1221
  let configToWrite = config
1104
1222
  if (version === 'v2') {
1105
- info('Migrating config to V2 native format...')
1223
+ info(S.v2Migrating)
1106
1224
  configToWrite = migrateV1toV2(config)
1107
1225
  }
1108
1226
 
1109
1227
  const configContent = `${JSON.stringify(configToWrite, null, 2)}\n`
1110
- const status = writeIfChanged(targetConfigPath, configContent, dryRun)
1228
+ const status = writeConfigWithBackup(targetConfigPath, configContent, dryRun)
1111
1229
  if (status === 'created') stats.created++
1112
1230
  else stats.skipped++
1113
1231
 
@@ -1128,40 +1246,15 @@ export async function installOpenCode(
1128
1246
  }
1129
1247
 
1130
1248
  // -----------------------------------------------------------------------
1131
- // 4. Setup virtual environment + health check (--components runtime)
1249
+ // 4. (runtime setup moved to 2.11 — before the config write — so MCP
1250
+ // entries are only added when the Python runtime is actually healthy)
1132
1251
  // -----------------------------------------------------------------------
1133
- if (componentSet.has('runtime')) {
1134
- try {
1135
- const venvSpinner = spinner('Setting up Python virtual environment')
1136
- setupVenv(target, { dryRun, force: clean })
1137
- venvSpinner(true)
1138
- const runtimeTarget = isGlobal ? target : join(target, '.opencode')
1139
- const health = healthCheck(runtimeTarget, { dryRun, pythonTarget: target })
1140
-
1141
- // Print health summary
1142
- section('\uD83D\uDD0D Health Check')
1143
- const healthStep = step('Running health checks')
1144
- for (const p of health.passed) success(`${p.check}: ${p.detail}`)
1145
- for (const w of health.warnings) warning(`${w.check}: ${w.detail}`)
1146
- for (const f of health.failed) error(`${f.check}: ${f.detail}`)
1147
-
1148
- if (health.failed.length > 0) {
1149
- healthStep(false)
1150
- warning(`${health.failed.length} check(s) failed — review above`)
1151
- stats.errors += health.failed.length
1152
- } else {
1153
- healthStep(true)
1154
- }
1155
- } catch (err) {
1156
- error(`Setup failed: ${err.message}`)
1157
- throw err // Fatal — abort installation
1158
- }
1159
- }
1160
1252
 
1161
1253
  if (interactive && !dryRun) {
1162
1254
  const created = stats.created
1163
1255
  const skipped = stats.skipped
1164
1256
  const errors = stats.errors
1257
+ const warnings = stats.warnings ?? 0
1165
1258
 
1166
1259
  process.stdout.write('\n')
1167
1260
  process.stdout.write(
@@ -1179,11 +1272,14 @@ export async function installOpenCode(
1179
1272
  process.stdout.write(` ${colors.dim('\u2014')} ${skipped} already up-to-date\n`)
1180
1273
  if (errors > 0)
1181
1274
  process.stdout.write(` ${colors.yellow('\u26a0')} ${errors} error(s) encountered\n`)
1275
+ if (warnings > 0)
1276
+ process.stdout.write(
1277
+ ` ${colors.yellow('\u26a0')} ${warnings} warning(s) — run 'pantheon-opencode doctor' for details\n`,
1278
+ )
1182
1279
  process.stdout.write('\n')
1183
1280
  process.stdout.write(` ${colors.bold('Next steps:')}\n`)
1184
1281
  process.stdout.write(` ${colors.dim('\u2022')} Configure agents in opencode.json\n`)
1185
- process.stdout.write(` ${colors.dim('\u2022')} Add MCP servers in mcp.json\n`)
1186
- process.stdout.write(` ${colors.dim('\u2022')} Run 'opencode doctor' to verify\n`)
1282
+ process.stdout.write(` ${colors.dim('\u2022')} Run 'pantheon-opencode doctor' to verify\n`)
1187
1283
  process.stdout.write(
1188
1284
  ` ${colors.dim('\u2022')} Run 'opencode' to start using Pantheon agents\n`,
1189
1285
  )
@@ -1207,4 +1303,10 @@ export async function installOpenCode(
1207
1303
  const { writeActivePreset } = await import('./model-picker.mjs')
1208
1304
  writeActivePreset(target, opts.preset, { source: 'cli' })
1209
1305
  }
1306
+
1307
+ // Fail-closed: accumulated stats.errors (bad sources, missing requirements,
1308
+ // failed health checks) must never masquerade as a successful installation.
1309
+ if (stats.errors > 0) {
1310
+ throw new Error(`installation completed with ${stats.errors} error(s); see output above`)
1311
+ }
1210
1312
  }