harnessed 4.43.0 → 4.44.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.
@@ -13,35 +13,119 @@ function isAblated(env = process.env) {
13
13
  return env.HARNESSED_OFF === '1'
14
14
  }
15
15
 
16
- // src/checkpoint/hookStateRoot.ts
16
+ // src/platform/platform.ts
17
17
  import { existsSync, readFileSync } from 'node:fs'
18
18
  import { homedir } from 'node:os'
19
19
  import { join } from 'node:path'
20
- function hookStateRoot(home = homedir()) {
21
- const override = process.env.HARNESSED_ROOT_OVERRIDE
22
- if (override !== void 0 && override !== '') return override
20
+ function claudeDescriptor(home = homedir()) {
23
21
  const claudeHome = join(home, '.claude')
22
+ return {
23
+ id: 'claude',
24
+ homeDir: claudeHome,
25
+ stateRoot: join(claudeHome, 'harnessed'),
26
+ settingsPath: join(claudeHome, 'settings.json'),
27
+ skillsDir: join(claudeHome, 'skills'),
28
+ commandsDir: join(claudeHome, 'commands'),
29
+ pluginsRegistry: join(claudeHome, 'plugins', 'installed_plugins.json'),
30
+ // mcpConfigPath is a SIBLING of `.claude` (`<home>/.claude.json`), based on
31
+ // the same home base — not a child of homeDir.
32
+ mcpConfigPath: join(home, '.claude.json'),
33
+ // claude writes its env keys into JSON settings.json (capability present).
34
+ supportsEnvKeyWrite: true,
35
+ // Claude Code exposes the active session id to Bash-invoked CLI + hooks.
36
+ sessionIdEnv: 'CLAUDE_CODE_SESSION_ID',
37
+ }
38
+ }
39
+ function codexDescriptor(home = homedir()) {
24
40
  const codexHome = join(home, '.codex')
25
- const rootOf = (id) =>
26
- id === 'claude'
27
- ? join(claudeHome, 'harnessed')
28
- : id === 'codex'
29
- ? join(codexHome, 'harnessed')
30
- : null
31
- const env = process.env.HARNESSED_PLATFORM
32
- if (env !== void 0 && env !== '') {
33
- const r = rootOf(env)
34
- if (r) return r
41
+ const configToml = join(codexHome, 'config.toml')
42
+ return {
43
+ id: 'codex',
44
+ homeDir: codexHome,
45
+ stateRoot: join(codexHome, 'harnessed'),
46
+ settingsPath: null,
47
+ // SHARED convention dir, NOT <homeDir>/skills.
48
+ skillsDir: join(home, '.agents', 'skills'),
49
+ commandsDir: join(codexHome, 'prompts'),
50
+ pluginsRegistry: null,
51
+ mcpConfigPath: configToml,
52
+ supportsEnvKeyWrite: false,
53
+ // Measured on codex-cli 0.154: codex shells carry it, equal to hook session_id.
54
+ sessionIdEnv: 'CODEX_SESSION_ID',
55
+ }
56
+ }
57
+ function descriptorById(id, home) {
58
+ if (id === 'claude') return claudeDescriptor(home)
59
+ if (id === 'codex') return codexDescriptor(home)
60
+ return void 0
61
+ }
62
+ function detectPlatform(home = homedir()) {
63
+ const resolved = resolveHost(home)
64
+ const override = process.env.HARNESSED_ROOT_OVERRIDE
65
+ if (override !== void 0 && override !== '') return { ...resolved, stateRoot: override }
66
+ return resolved
67
+ }
68
+ function resolveHost(home) {
69
+ const base = claudeDescriptor(home)
70
+ const envPlatform = process.env.HARNESSED_PLATFORM
71
+ if (envPlatform !== void 0 && envPlatform !== '') {
72
+ const d = descriptorById(envPlatform, home)
73
+ if (d) return d
74
+ }
75
+ const inClaude = Boolean(process.env.CLAUDE_CODE_SESSION_ID?.trim())
76
+ const inCodex = Boolean(process.env.CODEX_SESSION_ID?.trim())
77
+ if (inClaude !== inCodex) return inClaude ? base : codexDescriptor(home)
78
+ for (const pinRoot of [codexDescriptor(home).stateRoot, base.stateRoot]) {
79
+ try {
80
+ const d = descriptorById(readFileSync(join(pinRoot, '.platform'), 'utf8').trim(), home)
81
+ if (d) return d
82
+ } catch {}
35
83
  }
36
84
  try {
37
- const r = rootOf(readFileSync(join(claudeHome, 'harnessed', '.platform'), 'utf8').trim())
38
- if (r) return r
85
+ if (existsSync(base.homeDir)) return base
86
+ const codex = codexDescriptor(home)
87
+ if (existsSync(codex.homeDir)) return codex
39
88
  } catch {}
89
+ return base
90
+ }
91
+
92
+ // src/checkpoint/hookHost.ts
93
+ function platformArg(argv) {
94
+ const i = argv.indexOf('--platform')
95
+ const v = i >= 0 ? argv[i + 1] : void 0
96
+ return v && !v.startsWith('-') ? v : null
97
+ }
98
+ function applyPlatformArg(argv, env = process.env) {
99
+ const id = platformArg(argv)
100
+ if (id) env.HARNESSED_PLATFORM = id
101
+ return id
102
+ }
103
+ function sessionIdFromPayload(raw) {
40
104
  try {
41
- if (existsSync(claudeHome)) return join(claudeHome, 'harnessed')
42
- if (existsSync(codexHome)) return join(codexHome, 'harnessed')
43
- } catch {}
44
- return join(claudeHome, 'harnessed')
105
+ const v = JSON.parse(raw)?.session_id
106
+ return typeof v === 'string' && v.trim() ? v.trim() : void 0
107
+ } catch {
108
+ return void 0
109
+ }
110
+ }
111
+ function readHookStdin(timeoutMs = 1e3) {
112
+ if (process.stdin.isTTY) return Promise.resolve('')
113
+ return new Promise((resolve2) => {
114
+ let data = ''
115
+ const done = () => {
116
+ clearTimeout(timer)
117
+ process.stdin.removeAllListeners()
118
+ process.stdin.pause()
119
+ resolve2(data)
120
+ }
121
+ const timer = setTimeout(done, timeoutMs)
122
+ process.stdin.setEncoding('utf8')
123
+ process.stdin.on('data', (c) => {
124
+ data += c
125
+ })
126
+ process.stdin.on('end', done)
127
+ process.stdin.on('error', done)
128
+ })
45
129
  }
46
130
 
47
131
  // src/checkpoint/injectCache.ts
@@ -449,14 +533,6 @@ function repoKey(cwd) {
449
533
  }
450
534
  return resolve(cwd)
451
535
  }
452
- function harnessedRoot() {
453
- return hookStateRoot()
454
- }
455
- function sessionIdEnvName() {
456
- const platform = (process.env.HARNESSED_PLATFORM || 'claude').trim()
457
- if (platform === 'codex') return null
458
- return 'CLAUDE_CODE_SESSION_ID'
459
- }
460
536
  function readWorkflow(root, keys) {
461
537
  let wf = null
462
538
  let intent = null
@@ -505,17 +581,21 @@ function shouldEmitPc(root, repoRoot, sid, pc) {
505
581
  return true
506
582
  }
507
583
  }
508
- function main() {
584
+ async function main() {
509
585
  if (isAblated()) return
510
586
  try {
511
- const root = harnessedRoot()
587
+ const hostArg = applyPlatformArg(process.argv)
588
+ const platform = detectPlatform()
589
+ const root = platform.stateRoot
512
590
  if (process.argv.includes('--invalidate')) {
513
591
  invalidateInjectCache(root)
514
592
  return
515
593
  }
516
594
  const key = repoKey(process.cwd())
517
- const envName = sessionIdEnvName()
518
- const sid = envName ? process.env[envName]?.trim() : void 0
595
+ const envName = platform.sessionIdEnv
596
+ const sid =
597
+ (envName ? process.env[envName]?.trim() : void 0) ||
598
+ (hostArg ? sessionIdFromPayload(await readHookStdin()) : void 0)
519
599
  const { wf, intent, ledgerAgeMs } = readWorkflow(root, sid ? [`${key}::${sid}`, key] : [key])
520
600
  let learningsMd = ''
521
601
  try {
@@ -533,4 +613,4 @@ function main() {
533
613
  `)
534
614
  } catch {}
535
615
  }
536
- main()
616
+ void main()
@@ -18,35 +18,80 @@ function isAblated(env = process.env) {
18
18
  return env.HARNESSED_OFF === '1'
19
19
  }
20
20
 
21
- // src/checkpoint/hookStateRoot.ts
21
+ // src/platform/platform.ts
22
22
  import { existsSync, readFileSync } from 'node:fs'
23
23
  import { homedir } from 'node:os'
24
24
  import { join } from 'node:path'
25
- function hookStateRoot(home = homedir()) {
26
- const override = process.env.HARNESSED_ROOT_OVERRIDE
27
- if (override !== void 0 && override !== '') return override
25
+ function claudeDescriptor(home = homedir()) {
28
26
  const claudeHome = join(home, '.claude')
27
+ return {
28
+ id: 'claude',
29
+ homeDir: claudeHome,
30
+ stateRoot: join(claudeHome, 'harnessed'),
31
+ settingsPath: join(claudeHome, 'settings.json'),
32
+ skillsDir: join(claudeHome, 'skills'),
33
+ commandsDir: join(claudeHome, 'commands'),
34
+ pluginsRegistry: join(claudeHome, 'plugins', 'installed_plugins.json'),
35
+ // mcpConfigPath is a SIBLING of `.claude` (`<home>/.claude.json`), based on
36
+ // the same home base — not a child of homeDir.
37
+ mcpConfigPath: join(home, '.claude.json'),
38
+ // claude writes its env keys into JSON settings.json (capability present).
39
+ supportsEnvKeyWrite: true,
40
+ // Claude Code exposes the active session id to Bash-invoked CLI + hooks.
41
+ sessionIdEnv: 'CLAUDE_CODE_SESSION_ID',
42
+ }
43
+ }
44
+ function codexDescriptor(home = homedir()) {
29
45
  const codexHome = join(home, '.codex')
30
- const rootOf = (id) =>
31
- id === 'claude'
32
- ? join(claudeHome, 'harnessed')
33
- : id === 'codex'
34
- ? join(codexHome, 'harnessed')
35
- : null
36
- const env = process.env.HARNESSED_PLATFORM
37
- if (env !== void 0 && env !== '') {
38
- const r = rootOf(env)
39
- if (r) return r
46
+ const configToml = join(codexHome, 'config.toml')
47
+ return {
48
+ id: 'codex',
49
+ homeDir: codexHome,
50
+ stateRoot: join(codexHome, 'harnessed'),
51
+ settingsPath: null,
52
+ // SHARED convention dir, NOT <homeDir>/skills.
53
+ skillsDir: join(home, '.agents', 'skills'),
54
+ commandsDir: join(codexHome, 'prompts'),
55
+ pluginsRegistry: null,
56
+ mcpConfigPath: configToml,
57
+ supportsEnvKeyWrite: false,
58
+ // Measured on codex-cli 0.154: codex shells carry it, equal to hook session_id.
59
+ sessionIdEnv: 'CODEX_SESSION_ID',
60
+ }
61
+ }
62
+ function descriptorById(id, home) {
63
+ if (id === 'claude') return claudeDescriptor(home)
64
+ if (id === 'codex') return codexDescriptor(home)
65
+ return void 0
66
+ }
67
+ function detectPlatform(home = homedir()) {
68
+ const resolved = resolveHost(home)
69
+ const override = process.env.HARNESSED_ROOT_OVERRIDE
70
+ if (override !== void 0 && override !== '') return { ...resolved, stateRoot: override }
71
+ return resolved
72
+ }
73
+ function resolveHost(home) {
74
+ const base = claudeDescriptor(home)
75
+ const envPlatform = process.env.HARNESSED_PLATFORM
76
+ if (envPlatform !== void 0 && envPlatform !== '') {
77
+ const d = descriptorById(envPlatform, home)
78
+ if (d) return d
79
+ }
80
+ const inClaude = Boolean(process.env.CLAUDE_CODE_SESSION_ID?.trim())
81
+ const inCodex = Boolean(process.env.CODEX_SESSION_ID?.trim())
82
+ if (inClaude !== inCodex) return inClaude ? base : codexDescriptor(home)
83
+ for (const pinRoot of [codexDescriptor(home).stateRoot, base.stateRoot]) {
84
+ try {
85
+ const d = descriptorById(readFileSync(join(pinRoot, '.platform'), 'utf8').trim(), home)
86
+ if (d) return d
87
+ } catch {}
40
88
  }
41
89
  try {
42
- const r = rootOf(readFileSync(join(claudeHome, 'harnessed', '.platform'), 'utf8').trim())
43
- if (r) return r
44
- } catch {}
45
- try {
46
- if (existsSync(claudeHome)) return join(claudeHome, 'harnessed')
47
- if (existsSync(codexHome)) return join(codexHome, 'harnessed')
90
+ if (existsSync(base.homeDir)) return base
91
+ const codex = codexDescriptor(home)
92
+ if (existsSync(codex.homeDir)) return codex
48
93
  } catch {}
49
- return join(claudeHome, 'harnessed')
94
+ return base
50
95
  }
51
96
 
52
97
  // src/checkpoint/modeBDetect.ts
@@ -82,7 +127,7 @@ function detectModeB(content) {
82
127
  // src/checkpoint/stopHookMain.ts
83
128
  var MAX_RETRIES = 2
84
129
  function stateRoot() {
85
- return hookStateRoot()
130
+ return detectPlatform().stateRoot
86
131
  }
87
132
  function messageSig(content) {
88
133
  const text = content