mio-agent-runtime 0.13.4 → 0.14.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.
@@ -1,197 +1,197 @@
1
- 'use strict'
2
-
3
- const fs = require('fs')
4
- const path = require('path')
5
- const os = require('os')
6
- const crypto = require('crypto')
7
-
8
- function configDir() {
9
- return (
10
- process.env.WORKBUDDY_CONFIG_DIR ||
11
- process.env.CODEBUDDY_CONFIG_DIR ||
12
- path.join(os.homedir(), '.workbuddy')
13
- )
14
- }
15
-
16
- function configPath() {
17
- return path.join(configDir(), 'mcp.json')
18
- }
19
-
20
- function legacyConfigPath() {
21
- return path.join(configDir(), '.mcp.json')
22
- }
23
-
24
- function approvalsPath() {
25
- return path.join(configDir(), 'mcp-approvals.json')
26
- }
27
-
28
- // User-level memory rules file. WorkBuddy's codebuddy CLI (a CodeBuddy fork)
29
- // auto-loads CODEBUDDY.md (or CODEBUDDY.mdc) from the config dir into every
30
- // session (MemoryLoader user scope -> system-reminder-md / prompt context).
31
- // Note: PRODUCT_UPPERCASE_NAME in the CLI bundle is "CODEBUDDY", not "WORKBUDDY".
32
- function memoryRulesPath() {
33
- return path.join(configDir(), 'CODEBUDDY.md')
34
- }
35
-
36
- // Project-level memory rules. WorkBuddy walks up from the workspace dir and
37
- // auto-loads CODEBUDDY.md / AGENTS.md (project scope) into every session.
38
- function projectInstructionsPath(workspace) {
39
- return path.join(workspace || process.cwd(), 'AGENTS.md')
40
- }
41
-
42
- function readJson(file) {
43
- if (!fs.existsSync(file)) return null
44
- try {
45
- return JSON.parse(fs.readFileSync(file, 'utf8'))
46
- } catch (_) {
47
- return null
48
- }
49
- }
50
-
51
- const MEMORY_BEGIN = '<!-- MIO_INTELLIGENCE_BEGIN -->'
52
- const MEMORY_END = '<!-- MIO_INTELLIGENCE_END -->'
53
- const MEMORY_BLOCK = `${MEMORY_BEGIN}
54
- ## Mio Intelligence Layer
55
-
56
- Mio MCP tools are available. Treat these as mandatory runtime rules for every workspace.
57
-
58
- - Before designing or implementing a solution, or when unsure about prior decisions, call \`mcp__mio-intelligence__mio.memory.query\` first. Omit \`project\` unless a specific project is required; Mio derives it from the current Git repository.
59
- - After a decision affecting architecture, deployment, dependencies, or long-term behavior, call \`mcp__mio-intelligence__mio.memory.record\` with \`kind\`: \`decision\`, \`context\`, \`problem\`, or \`note\`.
60
- - When a tool call fails, retries, or returns an error, call \`mcp__mio-intelligence__mio.observer.ingest\` with a stable \`trace_id\`, relevant \`event_type\`, \`payload\`, and \`outcome\`.
61
- - Before a high-risk operation such as deletion, migration, credential change, or production change, call \`mcp__mio-intelligence__mio.policy.check\` and follow its suggestion when available.
62
- - At the end of every task, before producing the final answer, call \`mcp__mio-intelligence__mio.observer.ingest\` with \`event_type\` = \`task_outcome\`, a stable \`trace_id\`, \`outcome\` = \`success\` / \`failure\` / \`aborted\`, and a brief non-sensitive \`payload\`. If Mio is unavailable, do not block the final answer.
63
- - When you reuse a prior decision or experience returned by \`mcp__mio-intelligence__mio.memory.query\` and it changes your approach or outcome, call \`mcp__mio-intelligence__mio.experience.reuse\` with \`sourceAgent\`, \`targetAgent\`, \`experienceId\`, \`reuse\`, \`behaviorChanged\`, and \`outcomeImproved\`.
64
- - If Mio MCP is unavailable, do not block the task. Continue normally and note the missed memory or observation.
65
- - Do not store secrets, credentials, or raw sensitive content in Mio memory.
66
- ${MEMORY_END}`
67
-
68
- function ensureBlock(target) {
69
- let content = fs.existsSync(target) ? fs.readFileSync(target, 'utf8') : ''
70
- const original = content
71
- const start = content.indexOf(MEMORY_BEGIN)
72
- if (start !== -1) {
73
- const end = content.indexOf(MEMORY_END, start)
74
- content = end === -1 ? content.slice(0, start) : content.slice(0, start) + content.slice(end + MEMORY_END.length)
75
- content = content.trimEnd()
76
- }
77
- if (content) content += '\n\n'
78
- content += MEMORY_BLOCK + '\n'
79
- if (content === original) return { changed: false, target, backup: null }
80
- const backup = `${target}.bak-mio-${Date.now()}`
81
- if (fs.existsSync(target)) fs.copyFileSync(target, backup)
82
- fs.mkdirSync(path.dirname(target), { recursive: true })
83
- fs.writeFileSync(target, content, 'utf8')
84
- return { changed: true, target, backup }
85
- }
86
-
87
- function isInstalled() {
88
- const config = readJson(configPath())
89
- const entry = config && config.mcpServers && config.mcpServers['mio-intelligence']
90
- if (!entry) return false
91
-
92
- const approvals = readJson(approvalsPath()) || {}
93
- const memory = fs.existsSync(memoryRulesPath()) ? fs.readFileSync(memoryRulesPath(), 'utf8') : ''
94
- return Boolean(
95
- approvals[approvalKey('mio-intelligence', entry)] &&
96
- memory.includes(MEMORY_BEGIN) &&
97
- memory.includes(MEMORY_END)
98
- )
99
- }
100
-
101
- function approvalKey(name, entry) {
102
- let input
103
- if (entry.command) {
104
- input = `${entry.command || ''}|${(entry.args || []).map(String).sort().join(',')}|${Object.keys(entry.env || {}).sort().join(',')}`
105
- } else if (entry.url) {
106
- try {
107
- input = new URL(entry.url).origin
108
- } catch (_) {
109
- input = entry.url
110
- }
111
- } else {
112
- input = JSON.stringify(entry)
113
- }
114
-
115
- const hash = crypto.createHash('sha256').update(input).digest('hex')
116
- return `${hash}::${name}`
117
- }
118
-
119
- function writeApproval(name, entry) {
120
- const target = approvalsPath()
121
- const approvals = readJson(target) || {}
122
- approvals[approvalKey(name, entry)] = Date.now()
123
- fs.mkdirSync(path.dirname(target), { recursive: true })
124
- fs.writeFileSync(target, `${JSON.stringify(approvals, null, 2)}\n`, 'utf8')
125
- }
126
-
127
- function install({ node, serverScript, home, workspace }) {
128
- const target = configPath()
129
- const canonical = readJson(target) || {}
130
- const legacy = readJson(legacyConfigPath()) || {}
131
- const canonicalServers =
132
- canonical.mcpServers && typeof canonical.mcpServers === 'object' ? canonical.mcpServers : {}
133
- const legacyServers =
134
- legacy.mcpServers && typeof legacy.mcpServers === 'object' ? legacy.mcpServers : {}
135
-
136
- const config = {
137
- ...legacy,
138
- ...canonical,
139
- mcpServers: {
140
- ...legacyServers,
141
- ...canonicalServers,
142
- },
143
- }
144
-
145
- // No project/workspace here: the MCP server derives the project from its own
146
- // git cwd. A baked value from install-time cwd overrides that derivation and
147
- // mis-attributes every memory write for the host's lifetime.
148
- const context = JSON.stringify({
149
- agentId: 'workbuddy',
150
- sessionId: 'workbuddy-session',
151
- })
152
-
153
- config.mcpServers = {
154
- ...config.mcpServers,
155
- 'mio-intelligence': {
156
- command: node,
157
- args: [serverScript],
158
- env: {
159
- MIO_DATA_DIR: home,
160
- MIO_CONTEXT: context,
161
- },
162
- },
163
- }
164
-
165
- writeApproval('mio-intelligence', config.mcpServers['mio-intelligence'])
166
-
167
- const backup = `${target}.bak-mio-${Date.now()}`
168
- if (fs.existsSync(target)) {
169
- fs.copyFileSync(target, backup)
170
- }
171
-
172
- fs.mkdirSync(path.dirname(target), { recursive: true })
173
- fs.writeFileSync(target, `${JSON.stringify(config, null, 2)}\n`, 'utf8')
174
-
175
- const userRules = ensureBlock(memoryRulesPath())
176
- let projectRules = null
177
- const workspaceDir = workspace || process.cwd()
178
- if (workspaceDir && path.isAbsolute(workspaceDir)) {
179
- projectRules = ensureBlock(projectInstructionsPath(workspaceDir))
180
- }
181
-
182
- const changed = userRules.changed || Boolean(projectRules && projectRules.changed)
183
-
184
- return {
185
- changed,
186
- configPath: target,
187
- approvalsPath: approvalsPath(),
188
- memoryRulesPath: userRules.target,
189
- projectInstructionsPath: projectRules ? projectRules.target : null,
190
- userRulesChanged: userRules.changed,
191
- projectRulesChanged: projectRules ? projectRules.changed : false,
192
- backup,
193
- message: 'WorkBuddy Mio MCP installed, approved, and Mio rules injected into CODEBUDDY.md (auto-loaded user memory) and the workspace AGENTS.md. Restart WorkBuddy to load them.',
194
- }
195
- }
196
-
197
- module.exports = { configDir, configPath, approvalsPath, memoryRulesPath, projectInstructionsPath, approvalKey, isInstalled, install }
1
+ 'use strict'
2
+
3
+ const fs = require('fs')
4
+ const path = require('path')
5
+ const os = require('os')
6
+ const crypto = require('crypto')
7
+
8
+ function configDir() {
9
+ return (
10
+ process.env.WORKBUDDY_CONFIG_DIR ||
11
+ process.env.CODEBUDDY_CONFIG_DIR ||
12
+ path.join(os.homedir(), '.workbuddy')
13
+ )
14
+ }
15
+
16
+ function configPath() {
17
+ return path.join(configDir(), 'mcp.json')
18
+ }
19
+
20
+ function legacyConfigPath() {
21
+ return path.join(configDir(), '.mcp.json')
22
+ }
23
+
24
+ function approvalsPath() {
25
+ return path.join(configDir(), 'mcp-approvals.json')
26
+ }
27
+
28
+ // User-level memory rules file. WorkBuddy's codebuddy CLI (a CodeBuddy fork)
29
+ // auto-loads CODEBUDDY.md (or CODEBUDDY.mdc) from the config dir into every
30
+ // session (MemoryLoader user scope -> system-reminder-md / prompt context).
31
+ // Note: PRODUCT_UPPERCASE_NAME in the CLI bundle is "CODEBUDDY", not "WORKBUDDY".
32
+ function memoryRulesPath() {
33
+ return path.join(configDir(), 'CODEBUDDY.md')
34
+ }
35
+
36
+ // Project-level memory rules. WorkBuddy walks up from the workspace dir and
37
+ // auto-loads CODEBUDDY.md / AGENTS.md (project scope) into every session.
38
+ function projectInstructionsPath(workspace) {
39
+ return path.join(workspace || process.cwd(), 'AGENTS.md')
40
+ }
41
+
42
+ function readJson(file) {
43
+ if (!fs.existsSync(file)) return null
44
+ try {
45
+ return JSON.parse(fs.readFileSync(file, 'utf8'))
46
+ } catch (_) {
47
+ return null
48
+ }
49
+ }
50
+
51
+ const MEMORY_BEGIN = '<!-- MIO_INTELLIGENCE_BEGIN -->'
52
+ const MEMORY_END = '<!-- MIO_INTELLIGENCE_END -->'
53
+ const MEMORY_BLOCK = `${MEMORY_BEGIN}
54
+ ## Mio Intelligence Layer
55
+
56
+ Mio MCP tools are available. Treat these as mandatory runtime rules for every workspace.
57
+
58
+ - Before designing or implementing a solution, or when unsure about prior decisions, call \`mcp__mio-intelligence__mio.memory.query\` first. Omit \`project\` unless a specific project is required; Mio derives it from the current Git repository.
59
+ - After a decision affecting architecture, deployment, dependencies, or long-term behavior, call \`mcp__mio-intelligence__mio.memory.record\` with \`kind\`: \`decision\`, \`context\`, \`problem\`, or \`note\`.
60
+ - When a tool call fails, retries, or returns an error, call \`mcp__mio-intelligence__mio.observer.ingest\` with a stable \`trace_id\`, relevant \`event_type\`, \`payload\`, and \`outcome\`.
61
+ - Before a high-risk operation such as deletion, migration, credential change, or production change, call \`mcp__mio-intelligence__mio.policy.check\` and follow its suggestion when available.
62
+ - At the end of every task, before producing the final answer, call \`mcp__mio-intelligence__mio.observer.ingest\` with \`event_type\` = \`task_outcome\`, a stable \`trace_id\`, \`outcome\` = \`success\` / \`failure\` / \`aborted\`, and a brief non-sensitive \`payload\`. If Mio is unavailable, do not block the final answer.
63
+ - When you reuse a prior decision or experience returned by \`mcp__mio-intelligence__mio.memory.query\` and it changes your approach or outcome, call \`mcp__mio-intelligence__mio.experience.reuse\` with \`sourceAgent\`, \`targetAgent\`, \`experienceId\`, \`reuse\`, \`behaviorChanged\`, and \`outcomeImproved\`.
64
+ - If Mio MCP is unavailable, do not block the task. Continue normally and note the missed memory or observation.
65
+ - Do not store secrets, credentials, or raw sensitive content in Mio memory.
66
+ ${MEMORY_END}`
67
+
68
+ function ensureBlock(target) {
69
+ let content = fs.existsSync(target) ? fs.readFileSync(target, 'utf8') : ''
70
+ const original = content
71
+ const start = content.indexOf(MEMORY_BEGIN)
72
+ if (start !== -1) {
73
+ const end = content.indexOf(MEMORY_END, start)
74
+ content = end === -1 ? content.slice(0, start) : content.slice(0, start) + content.slice(end + MEMORY_END.length)
75
+ content = content.trimEnd()
76
+ }
77
+ if (content) content += '\n\n'
78
+ content += MEMORY_BLOCK + '\n'
79
+ if (content === original) return { changed: false, target, backup: null }
80
+ const backup = `${target}.bak-mio-${Date.now()}`
81
+ if (fs.existsSync(target)) fs.copyFileSync(target, backup)
82
+ fs.mkdirSync(path.dirname(target), { recursive: true })
83
+ fs.writeFileSync(target, content, 'utf8')
84
+ return { changed: true, target, backup }
85
+ }
86
+
87
+ function isInstalled() {
88
+ const config = readJson(configPath())
89
+ const entry = config && config.mcpServers && config.mcpServers['mio-intelligence']
90
+ if (!entry) return false
91
+
92
+ const approvals = readJson(approvalsPath()) || {}
93
+ const memory = fs.existsSync(memoryRulesPath()) ? fs.readFileSync(memoryRulesPath(), 'utf8') : ''
94
+ return Boolean(
95
+ approvals[approvalKey('mio-intelligence', entry)] &&
96
+ memory.includes(MEMORY_BEGIN) &&
97
+ memory.includes(MEMORY_END)
98
+ )
99
+ }
100
+
101
+ function approvalKey(name, entry) {
102
+ let input
103
+ if (entry.command) {
104
+ input = `${entry.command || ''}|${(entry.args || []).map(String).sort().join(',')}|${Object.keys(entry.env || {}).sort().join(',')}`
105
+ } else if (entry.url) {
106
+ try {
107
+ input = new URL(entry.url).origin
108
+ } catch (_) {
109
+ input = entry.url
110
+ }
111
+ } else {
112
+ input = JSON.stringify(entry)
113
+ }
114
+
115
+ const hash = crypto.createHash('sha256').update(input).digest('hex')
116
+ return `${hash}::${name}`
117
+ }
118
+
119
+ function writeApproval(name, entry) {
120
+ const target = approvalsPath()
121
+ const approvals = readJson(target) || {}
122
+ approvals[approvalKey(name, entry)] = Date.now()
123
+ fs.mkdirSync(path.dirname(target), { recursive: true })
124
+ fs.writeFileSync(target, `${JSON.stringify(approvals, null, 2)}\n`, 'utf8')
125
+ }
126
+
127
+ function install({ node, serverScript, home, workspace }) {
128
+ const target = configPath()
129
+ const canonical = readJson(target) || {}
130
+ const legacy = readJson(legacyConfigPath()) || {}
131
+ const canonicalServers =
132
+ canonical.mcpServers && typeof canonical.mcpServers === 'object' ? canonical.mcpServers : {}
133
+ const legacyServers =
134
+ legacy.mcpServers && typeof legacy.mcpServers === 'object' ? legacy.mcpServers : {}
135
+
136
+ const config = {
137
+ ...legacy,
138
+ ...canonical,
139
+ mcpServers: {
140
+ ...legacyServers,
141
+ ...canonicalServers,
142
+ },
143
+ }
144
+
145
+ // No project/workspace here: the MCP server derives the project from its own
146
+ // git cwd. A baked value from install-time cwd overrides that derivation and
147
+ // mis-attributes every memory write for the host's lifetime.
148
+ const context = JSON.stringify({
149
+ agentId: 'workbuddy',
150
+ sessionId: 'workbuddy-session',
151
+ })
152
+
153
+ config.mcpServers = {
154
+ ...config.mcpServers,
155
+ 'mio-intelligence': {
156
+ command: node,
157
+ args: [serverScript],
158
+ env: {
159
+ MIO_DATA_DIR: home,
160
+ MIO_CONTEXT: context,
161
+ },
162
+ },
163
+ }
164
+
165
+ writeApproval('mio-intelligence', config.mcpServers['mio-intelligence'])
166
+
167
+ const backup = `${target}.bak-mio-${Date.now()}`
168
+ if (fs.existsSync(target)) {
169
+ fs.copyFileSync(target, backup)
170
+ }
171
+
172
+ fs.mkdirSync(path.dirname(target), { recursive: true })
173
+ fs.writeFileSync(target, `${JSON.stringify(config, null, 2)}\n`, 'utf8')
174
+
175
+ const userRules = ensureBlock(memoryRulesPath())
176
+ let projectRules = null
177
+ const workspaceDir = workspace || process.cwd()
178
+ if (workspaceDir && path.isAbsolute(workspaceDir)) {
179
+ projectRules = ensureBlock(projectInstructionsPath(workspaceDir))
180
+ }
181
+
182
+ const changed = userRules.changed || Boolean(projectRules && projectRules.changed)
183
+
184
+ return {
185
+ changed,
186
+ configPath: target,
187
+ approvalsPath: approvalsPath(),
188
+ memoryRulesPath: userRules.target,
189
+ projectInstructionsPath: projectRules ? projectRules.target : null,
190
+ userRulesChanged: userRules.changed,
191
+ projectRulesChanged: projectRules ? projectRules.changed : false,
192
+ backup,
193
+ message: 'WorkBuddy Mio MCP installed, approved, and Mio rules injected into CODEBUDDY.md (auto-loaded user memory) and the workspace AGENTS.md. Restart WorkBuddy to load them.',
194
+ }
195
+ }
196
+
197
+ module.exports = { configDir, configPath, approvalsPath, memoryRulesPath, projectInstructionsPath, approvalKey, isInstalled, install }