@openlucaskaka/kagent 0.1.7

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 (117) hide show
  1. package/README.md +353 -0
  2. package/npm/bin/kagent-serve.js +6 -0
  3. package/npm/bin/kagent.js +6 -0
  4. package/npm/lib/App.js +524 -0
  5. package/npm/lib/app-state.js +224 -0
  6. package/npm/lib/approval-choice.js +25 -0
  7. package/npm/lib/commands.js +59 -0
  8. package/npm/lib/editor.js +188 -0
  9. package/npm/lib/ink-runner.js +41 -0
  10. package/npm/lib/kagent-home.js +39 -0
  11. package/npm/lib/launcher.js +221 -0
  12. package/npm/lib/protocol.js +33 -0
  13. package/npm/lib/provider-setup.js +139 -0
  14. package/npm/lib/python-runner.js +892 -0
  15. package/npm/lib/runtime-client.js +390 -0
  16. package/npm/lib/terminal-input.js +127 -0
  17. package/npm/lib/terminal-text.js +19 -0
  18. package/npm/lib/terminal-width.js +14 -0
  19. package/npm/lib/transcript.js +227 -0
  20. package/npm/lib/ui-components.js +247 -0
  21. package/npm/lib/update-manager.js +334 -0
  22. package/package.json +39 -0
  23. package/pyproject.toml +55 -0
  24. package/src/kagent/__init__.py +90 -0
  25. package/src/kagent/cli/__init__.py +5 -0
  26. package/src/kagent/cli/__main__.py +6 -0
  27. package/src/kagent/cli/commands.py +112 -0
  28. package/src/kagent/cli/conversation.py +127 -0
  29. package/src/kagent/cli/interactive.py +841 -0
  30. package/src/kagent/cli/main.py +685 -0
  31. package/src/kagent/cli/memory.py +460 -0
  32. package/src/kagent/cli/pending_approval.py +169 -0
  33. package/src/kagent/cli/provider.py +27 -0
  34. package/src/kagent/cli/session_commands.py +401 -0
  35. package/src/kagent/cli/stdio_runtime.py +784 -0
  36. package/src/kagent/cli/trace.py +67 -0
  37. package/src/kagent/cli/ui.py +931 -0
  38. package/src/kagent/core/__init__.py +0 -0
  39. package/src/kagent/core/agent.py +296 -0
  40. package/src/kagent/core/faults.py +11 -0
  41. package/src/kagent/core/invariants.py +47 -0
  42. package/src/kagent/core/normalization.py +81 -0
  43. package/src/kagent/core/planning.py +48 -0
  44. package/src/kagent/core/state.py +73 -0
  45. package/src/kagent/core/summary.py +64 -0
  46. package/src/kagent/core/tools.py +196 -0
  47. package/src/kagent/core/trace.py +57 -0
  48. package/src/kagent/eval/__init__.py +11 -0
  49. package/src/kagent/eval/cases.py +229 -0
  50. package/src/kagent/eval/evaluator.py +216 -0
  51. package/src/kagent/integrations/__init__.py +3 -0
  52. package/src/kagent/integrations/audit.py +95 -0
  53. package/src/kagent/integrations/backends.py +131 -0
  54. package/src/kagent/integrations/memory.py +301 -0
  55. package/src/kagent/ops/__init__.py +0 -0
  56. package/src/kagent/ops/batch.py +156 -0
  57. package/src/kagent/ops/doctor.py +255 -0
  58. package/src/kagent/ops/metrics.py +214 -0
  59. package/src/kagent/ops/release_evidence.py +877 -0
  60. package/src/kagent/ops/release_manifest.py +142 -0
  61. package/src/kagent/ops/trace_replay.py +285 -0
  62. package/src/kagent/providers/__init__.py +7 -0
  63. package/src/kagent/providers/embeddings.py +187 -0
  64. package/src/kagent/providers/llm.py +770 -0
  65. package/src/kagent/py.typed +1 -0
  66. package/src/kagent/runtime/__init__.py +28 -0
  67. package/src/kagent/runtime/action_graph.py +543 -0
  68. package/src/kagent/runtime/agent.py +2089 -0
  69. package/src/kagent/runtime/approval.py +64 -0
  70. package/src/kagent/runtime/cancellation.py +64 -0
  71. package/src/kagent/runtime/checkpoint_state.py +146 -0
  72. package/src/kagent/runtime/checkpoint_storage.py +270 -0
  73. package/src/kagent/runtime/context.py +65 -0
  74. package/src/kagent/runtime/file_transaction.py +195 -0
  75. package/src/kagent/runtime/hooks.py +74 -0
  76. package/src/kagent/runtime/metadata.py +116 -0
  77. package/src/kagent/runtime/patch_checkpoints.py +385 -0
  78. package/src/kagent/runtime/policy.py +50 -0
  79. package/src/kagent/runtime/presentation.py +205 -0
  80. package/src/kagent/runtime/redaction.py +130 -0
  81. package/src/kagent/runtime/sandbox.py +331 -0
  82. package/src/kagent/runtime/skills.py +66 -0
  83. package/src/kagent/runtime/steering.py +56 -0
  84. package/src/kagent/runtime/steps.py +255 -0
  85. package/src/kagent/runtime/task_state.py +40 -0
  86. package/src/kagent/runtime/tools.py +3532 -0
  87. package/src/kagent/runtime/types.py +240 -0
  88. package/src/kagent/runtime/workspace.py +597 -0
  89. package/src/kagent/service/__init__.py +26 -0
  90. package/src/kagent/service/__main__.py +6 -0
  91. package/src/kagent/service/active_runs.py +178 -0
  92. package/src/kagent/service/cli.py +737 -0
  93. package/src/kagent/service/contract.py +2571 -0
  94. package/src/kagent/service/errors.py +71 -0
  95. package/src/kagent/service/idempotency.py +584 -0
  96. package/src/kagent/service/router.py +884 -0
  97. package/src/kagent/service/run.py +150 -0
  98. package/src/kagent/service/runtime.py +1915 -0
  99. package/src/kagent/service/runtime_approval.py +20 -0
  100. package/src/kagent/service/runtime_cancel.py +192 -0
  101. package/src/kagent/service/runtime_lifecycle.py +229 -0
  102. package/src/kagent/service/runtime_metadata.py +21 -0
  103. package/src/kagent/service/runtime_policy.py +115 -0
  104. package/src/kagent/service/runtime_recovery.py +573 -0
  105. package/src/kagent/service/runtime_resume.py +382 -0
  106. package/src/kagent/service/runtime_resume_claim.py +116 -0
  107. package/src/kagent/service/runtime_run.py +350 -0
  108. package/src/kagent/service/runtime_status.py +2007 -0
  109. package/src/kagent/service/safety.py +139 -0
  110. package/src/kagent/service/server.py +114 -0
  111. package/src/kagent/service/status.py +233 -0
  112. package/src/kagent/service/trace_store.py +322 -0
  113. package/src/kagent/service/transport.py +24 -0
  114. package/src/kagent/utils/__init__.py +0 -0
  115. package/src/kagent/utils/config_validation.py +21 -0
  116. package/src/kagent/utils/json_output.py +23 -0
  117. package/src/kagent/utils/paths.py +473 -0
package/README.md ADDED
@@ -0,0 +1,353 @@
1
+ # kagent
2
+
3
+ Production-shaped LangGraph agent runtime for internal, non-coding workflows.
4
+
5
+ It provides two execution paths:
6
+
7
+ - deterministic graph runs for local tests, smoke checks, and regression checks;
8
+ - an agent runtime that plans with a configured LLM provider,
9
+ executes policy-gated tools, records structured observations, and can replan
10
+ after failures.
11
+
12
+ The project is intentionally conservative: bounded iterations, strict JSON
13
+ plan parsing, explicit tool schemas, approval gates for risky actions, compact
14
+ operator output, and redacted production evidence.
15
+
16
+ ## Quick Start
17
+
18
+ npm install:
19
+
20
+ ```sh
21
+ npm install -g @openlucaskaka/kagent@latest
22
+ kagent
23
+ ```
24
+
25
+ The default `kagent` command opens an Ink-based terminal UI and keeps the
26
+ Python LangGraph runtime as the execution engine behind it. The first run
27
+ prepares a private Python runtime under your user cache, installs kagent there,
28
+ and then opens the terminal agent. If no provider is configured yet, kagent
29
+ starts a first-time setup flow. The setup first asks you to choose Qwen,
30
+ DeepSeek, Ollama, or OpenAI-compatible/custom from a provider menu, then asks
31
+ for that provider's Base URL, model, and API key. Without a home override, the
32
+ local provider config is stored at `~/.kagent/config/provider.json` with
33
+ owner-only permissions. Use `kagent --classic` to bypass the Ink UI and run the
34
+ Python CLI directly.
35
+
36
+ Published stable releases use the npm `latest` tag (`stable/latest` channel);
37
+ prereleases such as beta builds use the `next` tag (`beta/next` channel). Set
38
+ `KAGENT_UPDATE_CHANNEL=beta` or `next` to follow prereleases. Interactive TTY
39
+ launches check the selected registry channel at most once every 24 hours and
40
+ prompt before upgrading. Set `KAGENT_NO_SELF_UPDATE=1` to disable automatic
41
+ checks. Run `kagent update --check` for an immediate read-only check or
42
+ `kagent upgrade` to install the selected channel explicitly. Check metadata is
43
+ stored at `~/.kagent/cache/npm-self-update.json` by default.
44
+
45
+ The npm launcher supports macOS and Linux (platform identifiers `darwin` and `linux`)
46
+ and stores each immutable Python runtime under
47
+ `~/.kagent/cache/npm-python`. A new runtime is downloaded and
48
+ prepared only when the Python dependency or ABI identity changes; a package
49
+ release with unchanged dependencies and ABI reuses the existing runtime.
50
+
51
+ To reconfigure later:
52
+
53
+ ```sh
54
+ kagent --configure
55
+ ```
56
+
57
+ Environment variables still override the local config for CI or temporary
58
+ operator sessions: `KAGENT_LLM_PROVIDER`, `KAGENT_LLM_BASE_URL`,
59
+ `KAGENT_LLM_API_KEY`, and `KAGENT_LLM_MODEL`.
60
+
61
+ ### User and project data
62
+
63
+ The following is the default layout under `~/.kagent`:
64
+
65
+ ```text
66
+ ~/.kagent/config/provider.json
67
+ ~/.kagent/state/session-memory.json
68
+ ~/.kagent/state/history
69
+ ~/.kagent/state/pending-approvals/
70
+ ~/.kagent/state/patches/
71
+ ~/.kagent/cache/npm-python/
72
+ ~/.kagent/cache/npm-self-update.json
73
+ ~/.kagent/.migration-v1-complete
74
+ ```
75
+
76
+ Set `KAGENT_HOME` to move that shared root. Existing explicit override
77
+ variables, such as `KAGENT_LLM_CONFIG_PATH`, `KAGENT_SESSION_MEMORY_PATH`,
78
+ `KAGENT_HISTORY_PATH`, `KAGENT_PENDING_APPROVAL_PATH`,
79
+ `KAGENT_PATCH_STATE_DIR`, and `KAGENT_NODE_VENV`, retain higher precedence than
80
+ `KAGENT_HOME`. The default is used only when neither an explicit override nor
81
+ `KAGENT_HOME` is set. `KAGENT_HOME` relocates the entire user-level layout:
82
+ config, state, cache, and migration marker all move beneath the resolved
83
+ `KAGENT_HOME` root.
84
+
85
+ On first use of the default home, Kagent safely copies durable provider and
86
+ state data from the former XDG locations. Existing destinations are never
87
+ overwritten, legacy sources remain untouched, owner-only permissions and
88
+ symlink checks are enforced, and disposable cache data is rebuilt under
89
+ `cache/npm-python` in the resolved root instead of migrated. XDG variables are legacy
90
+ migration sources, not current defaults. An explicit `KAGENT_HOME` skips legacy
91
+ discovery so a custom home cannot import unrelated state. The
92
+ `.migration-v1-complete` marker in the resolved root is written only after all
93
+ eligible durable data has migrated successfully; npm update metadata lives at
94
+ `cache/npm-self-update.json` beneath that root.
95
+
96
+ The project-local `$PWD/.kagent` directory remains separate from the resolved
97
+ user-level Kagent root (default `~/.kagent`): runtime workspaces and project
98
+ skills continue to live under `$PWD/.kagent/runtime-workspace` and
99
+ `$PWD/.kagent/skills`.
100
+
101
+ One-shot runs use the same command:
102
+
103
+ ```sh
104
+ kagent "draft an internal rollout checklist"
105
+ ```
106
+
107
+ Start the local HTTP service after npm installation:
108
+
109
+ ```sh
110
+ kagent-serve --host 127.0.0.1 --port 8000
111
+ ```
112
+
113
+ Local source checkout:
114
+
115
+ ```sh
116
+ python3 -m venv .venv
117
+ .venv/bin/python -m pip install -e '.[dev]'
118
+ scripts/run_checks.sh
119
+ ```
120
+
121
+ Start the LLM-backed terminal agent after setting provider environment
122
+ variables:
123
+
124
+ ```sh
125
+ # Set KAGENT_LLM_PROVIDER, KAGENT_LLM_BASE_URL, KAGENT_LLM_API_KEY,
126
+ # and KAGENT_LLM_MODEL in your shell or secret manager.
127
+
128
+ kagent
129
+ ```
130
+
131
+ Run a one-shot runtime goal through the same default agent path:
132
+
133
+ ```sh
134
+ kagent "draft an internal rollout checklist"
135
+ ```
136
+
137
+ Run the deterministic regression graph explicitly when you need local,
138
+ LLM-free checks:
139
+
140
+ ```sh
141
+ .venv/bin/python -m kagent.cli --deterministic "calculate 2 + 3"
142
+ ```
143
+
144
+ Inspect the default Codex-style runtime LangGraph topology:
145
+
146
+ ```sh
147
+ kagent --runtime --graph
148
+ ```
149
+
150
+ After package installation, the console entrypoint is the same daily-use
151
+ interface:
152
+
153
+ ```sh
154
+ kagent
155
+ ```
156
+
157
+ TTY sessions show live progress and a compact operator transcript by default.
158
+ Final answers stream into one stable transcript entry as provider chunks arrive.
159
+ The Ink editor supports wrapped Chinese and emoji input, pasted graphemes,
160
+ Backspace and forward Delete, Home/End, prompt history, and a responsive layout
161
+ tested at 40 and 100 columns. PageUp/PageDown browse older conversation pages
162
+ without moving the prompt cursor. Multiline paste keeps line breaks; use
163
+ Shift+Enter, Option+Enter, or Ctrl+J to insert a line break and Enter to run.
164
+ Type `/` to open the command palette, use Up/Down to select, and Tab to
165
+ complete. Ctrl-C cooperatively cancels an active run without restarting the
166
+ Python session. While kagent is working, the prompt remains editable and Enter
167
+ queues the latest additional instruction for the next planner or tool boundary;
168
+ Escape also requests cancellation. Permission prompts show the user-facing
169
+ action and target while keeping internal tool identifiers out of the normal
170
+ transcript.
171
+ Completed file changes, artifacts, browser actions, HTTP fetches, workspace
172
+ diffs, and bounded command results appear as concise outcome lines without
173
+ internal tool names. Ctrl+O expands or collapses the latest outcome content.
174
+
175
+ Use `/pwd`, `/cd PATH`, `/status`, `/config`, `/tools`, `/memory`,
176
+ `/compact-memory`, `/clear`, `/reset`, and `/help` in the Ink terminal. The
177
+ classic Python terminal additionally provides `/doctor`, `/json`, `/compact`,
178
+ `/last`, `/trace`, and `/save-trace PATH`. Unknown slash commands and known
179
+ commands with invalid arguments are handled locally with suggestions or usage
180
+ hints and are not sent to the model as goals.
181
+ Persisted session memory is owner-only on read and write,
182
+ uses `0700` parent directories, rejects symlink memory files or parent
183
+ directories, and redacts common API keys, bearer tokens, and URL credentials
184
+ before reusing memory in later turns or writing it to disk. Memory uses a v2
185
+ compact layout with durable summary, durable facts, open items, and recent
186
+ turns. Long sessions automatically compact older turns before they are reused
187
+ in prompts; `/compact-memory` forces compaction immediately and `/memory`
188
+ shows the current summary/facts/open-items/recent-turns view. The CLI
189
+ defaults to the runtime for both `kagent` and `kagent "goal"`; use
190
+ `--deterministic` only for the legacy regression graph. Runtime turns use three
191
+ planning iterations by default. Without a home override, TTY sessions persist
192
+ memory at `~/.kagent/state/session-memory.json`; set
193
+ `KAGENT_SESSION_MEMORY_PATH` to override that path or to an empty value to
194
+ disable default persistence. Without a home override, prompt history is stored
195
+ owner-only at `~/.kagent/state/history`; set `KAGENT_HISTORY_PATH`
196
+ to override it or to an empty value to disable persisted prompt history. Both
197
+ memory and prompt history redact common API keys, bearer tokens, and URL
198
+ credentials before writing to disk. Use `--max-iterations` to override the iteration
199
+ budget, `--session-memory PATH` for an explicit memory file, `--runtime-plan`
200
+ for deterministic runtime tests, and `--interactive-json` when you need full
201
+ traces. Runtime and service config values must use JSON integers, not strings
202
+ or booleans.
203
+
204
+ ## What It Can Do
205
+
206
+ The runtime currently includes tools for:
207
+
208
+ - notes and structured artifacts;
209
+ - task lists, rubrics, text transforms, and decision matrices;
210
+ - approved HTTP GET requests with SSRF protections;
211
+ - opening URLs in the local browser;
212
+ - opening local macOS applications by app name;
213
+ - approved bounded local shell commands for internal CLI checks, with
214
+ destructive, secret-exposing, and network shell commands rejected;
215
+ - creating, updating, moving, and deleting workspace files through an audited
216
+ `apply_patch` flow with transactional rollback and approval-gated undo/redo.
217
+ - maintaining versioned virtual-workspace assets with reviewed diff and
218
+ approval-gated current/revision SHA-locked restore and redo.
219
+
220
+ Risky tools are policy-gated. Runs expose structured events, observations,
221
+ approval state, artifacts, and metrics so internal dashboards can inspect what
222
+ happened without reading raw traces by default. Runtime summaries include
223
+ compact fields such as `progress_event_count` for timeline-oriented UIs.
224
+ Interactive approvals accept `d` to inspect the pending action JSON before
225
+ answering `y` or `n`.
226
+
227
+ ## Service
228
+
229
+ Start the local HTTP service:
230
+
231
+ ```sh
232
+ kagent-serve --host 127.0.0.1 --port 8000
233
+ ```
234
+
235
+ Useful endpoints include:
236
+
237
+ - `GET /health`, `HEAD /health`, `GET /ready`, `HEAD /ready`
238
+ - `GET /config`, `GET /version`, `GET /tools`, `GET /runtime/graph`, `GET /runtime/tools`
239
+ - `POST /run`, `POST /runtime/run`, `POST /runtime/resume`
240
+ - `GET /runtime/runs`, `GET /runtime/runs/summary`
241
+ - `GET /runtime/approvals`, `GET /runtime/approvals/summary`
242
+ - `GET /metrics`, `GET /metrics.prom`, `GET /openapi.json`
243
+
244
+ Use `deploy/env.example` for environment variable names and
245
+ `docs/deployment.md` for deployment defaults, auth, trace persistence, runtime
246
+ tool policy, diagnostics protection, and production preflight checks.
247
+
248
+ ## Code Organization
249
+
250
+ - `src/kagent/core/`: deterministic LangGraph loop.
251
+ - `src/kagent/runtime/`: LLM runtime, policies, tools,
252
+ and typed runtime data.
253
+ - `src/kagent/service/`: stdlib HTTP service, routing,
254
+ status, approvals, resume/cancel, trace store, and transport helpers.
255
+ - `src/kagent/cli/`: command line and interactive
256
+ terminal UI.
257
+ - `src/kagent/providers/`: provider detection, OpenAI-compatible protocol
258
+ adapter, and fake provider test support.
259
+ - `src/kagent/eval/`: evaluation cases and runner.
260
+ - `src/kagent/ops/`: doctor, metrics, release
261
+ evidence, release manifest, batch, and trace replay commands.
262
+
263
+ ## Console Scripts
264
+
265
+ Installed entry points:
266
+
267
+ ```sh
268
+ kagent --version
269
+ kagent-batch /tmp/goals.jsonl /tmp/results.jsonl
270
+ kagent-doctor --production --require-runtime-provider
271
+ kagent-eval --list-cases
272
+ kagent-metrics /tmp/kagent-continuous.jsonl
273
+ kagent-release-evidence --help
274
+ kagent-release-manifest --help
275
+ kagent-serve --host 127.0.0.1 --port 8000
276
+ kagent-trace-prune /tmp/kagent-traces --max-age-days 7 --runtime-only --fail-on-errors
277
+ kagent-trace-replay /tmp/kagent-traces/RUN_ID.json
278
+ ```
279
+
280
+ ## Release And Operations
281
+
282
+ Common local gates:
283
+
284
+ ```sh
285
+ make check
286
+ make smoke-service
287
+ make readiness-audit
288
+ make production-approval-bundle
289
+ ```
290
+
291
+ Release and rollout scripts:
292
+
293
+ - `scripts/production_readiness_audit.py`
294
+ - `scripts/staging_acceptance.sh`
295
+ - `scripts/observability_acceptance.sh`
296
+ - `scripts/internal_rollout_acceptance.py`
297
+ - `scripts/production_approval_bundle.sh`
298
+ - `scripts/smoke_real_llm_runtime.sh`
299
+
300
+ These scripts emit redacted evidence and are documented in
301
+ `docs/operations.md`, `docs/production-readiness.md`, and
302
+ `docs/internal-rollout.md`.
303
+
304
+ ## Python API
305
+
306
+ Stable package-level imports are available for automation:
307
+
308
+ ```python
309
+ from kagent import (
310
+ FakeLLMProvider,
311
+ evaluate_agent,
312
+ preview_plan,
313
+ registered_evaluation_cases,
314
+ registered_tool_metadata,
315
+ run_agent,
316
+ run_runtime_agent,
317
+ summarize_run,
318
+ )
319
+ ```
320
+
321
+ Example deterministic runtime test:
322
+
323
+ ```python
324
+ from kagent import FakeLLMProvider, run_runtime_agent
325
+
326
+ provider = FakeLLMProvider(
327
+ '{"actions":[{"id":"step-1","tool":"note","input":{"text":"hello"}}]}'
328
+ )
329
+ result = run_runtime_agent("capture hello", provider=provider)
330
+ ```
331
+
332
+ The package ships a `py.typed` marker for downstream type checkers.
333
+
334
+ ## Documentation
335
+
336
+ - Architecture: `docs/architecture.md`
337
+ - Operations runbook: `docs/operations.md`
338
+ - Deployment guide: `docs/deployment.md`
339
+ - Internal rollout: `docs/internal-rollout.md`
340
+ - Production readiness: `docs/production-readiness.md`
341
+ - Internal client example: `examples/internal_runtime_client.py`
342
+ - Release notes: `CHANGELOG.md`
343
+
344
+ ## Current Graph
345
+
346
+ ```text
347
+ planner -> executor -> verifier -> END
348
+ verifier -> reflector -> executor
349
+ ```
350
+
351
+ The deterministic graph retries failed verification through the reflector until
352
+ the step budget is exhausted. The runtime path uses bounded plan-act-observe
353
+ iterations with strict plan validation and policy-gated tool execution.
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const { runPythonEntrypoint } = require("../lib/python-runner");
5
+
6
+ runPythonEntrypoint("kagent-serve", process.argv.slice(2));
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const { launchKagent } = require("../lib/launcher");
5
+
6
+ void launchKagent(process.argv.slice(2));