Compare commits

...
191 Commits
Author SHA1 Message Date
MervinPraisonandCursor 16dfd4cea5 Add praisonai-js-hub-intake Cursor skill at monorepo root.
Agent skill documents Option B hub intake, routing table, workflows,
and file reference for PraisonAI ↔ praisonai-js issue handling.

Co-authored-by: Cursor <[email protected]>
2026-08-26 18:45:55 +01:00
MervinPraisonandCursor 4ab1ed280d Implement Option B hub intake for praisonai-js TypeScript issues.
Accept TS/JS issues on PraisonAI, route fixes to praisonai-js via Claude
STEP 3-ALT, block direct src/praisonai-ts/ PRs unless sync from praisonai-js,
and document routing in CONTRIBUTING, bug template, and SECURITY.

Co-authored-by: Cursor <[email protected]>
2026-08-26 18:41:27 +01:00
MervinPraisonandClaude Opus 5 df8cc70530 chore: praisonai 4.7.2
The version the desktop release is cut from. v4.7.1 predates the whole
Windows and Linux effort, so attaching those builds to it would have claimed
that release shipped something it did not contain.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-26 18:33:26 +01:00
Mervin Praison 8d2791ee0d Merge pull request #4382 from MervinPraison/fix-desktop-review-findings
fix(desktop): the ship-blockers three adversarial reviews found after merge
2026-08-26 17:18:33 +01:00
MervinPraisonandClaude Opus 5 53680e44d3 fix(desktop): the second-round review findings
A review of the fixes themselves, which is where new bugs get introduced.

**A stale tab really could cancel someone else's run.** The engine refuses to
stop a run other than the one asked for -- and the Stop button never sent the
id, so the guard could not fire and the three server tests covering it were
testing a request the product does not make. Reproduced end to end: tab A's
Stop cancelled the run tab B had started. The button names its run now.

**The CSP test caught none of seven CSP-breaking changes, including a straight
revert of the fix it exists to protect.** It matched a *variable* name against
an element id -- `stopBtn` against `id="stopBtn"`, which does not exist; the id
is `stop` -- so it silently compared nothing. It now resolves bindings to
elements before judging a line, and covers the four other ways the policy can
be broken: a style attribute in any casing or quoting, `setAttribute('style')`,
a `<style>` built at runtime, and `style=` concatenated into innerHTML. Ten
mutations, ten caught, including both directions of the original bug.

The rule it enforces is also broader than before. Hiding with an inline style
and revealing with a class leaves the element hidden from the second turn on,
just as the reverse leaves it hidden forever -- so for an element the markup
hides with a class, any `.style.display` assignment is a defect regardless of
the value.

**A whole test file ran nowhere.** `stream-pacing.test.ts` is TypeScript and
matched neither npm glob, so its eight tests -- including the positive control
for logic `ui/index.html` justifies by saying it was "ported verbatim from the
tested one" -- never executed. The skip-count assertion could not see it
either: nothing skips when nothing runs. That is the same failure the workflow
comment claims to prevent.

**Windows CI could not go green** on a clippy error in a `#[cfg(windows)]`
block, invisible from any other platform, and three tests errored in setUp on
`os.getpgid` -- which does not exist on Windows -- in the class that exists to
prove the Windows path works.

Secrets, three ways a report of success was untrue:

* A delete returned `or` of the two stores, so an unwritable fallback meant the
  plaintext survived, `get` served it, and the user was told the key was gone.
* A key the primary could not accept went to the fallback while the primary
  kept the old one -- and `get` reads the primary first, so the app went on
  using the previous key after a successful save.
* `FileSecretStore._read` swallowed every error, so one unreadable read meant
  "empty" and the next write destroyed the other secrets. The DPAPI store's
  docstring names this exact hazard; this one had been left with it.

And: `macos-14` began deprecation on 2026-07-06 with brownouts that fail jobs
now, and it was the only Apple-silicon leg, so the arm64 DMG was on its way to
disappearing -- there is a test listing retired images now, because this is the
second runner to expire mid-branch. `x11_threads` declared dlsym's argument as
`*const i8`, which does not compile on ARM Linux where `c_char` is unsigned.
The two Tauri window configs must agree, and nothing checked. The log-tail
comment described dropping a seek-truncated first line that nothing dropped.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-26 16:46:06 +01:00
MervinPraisonandClaude Opus 5 241b50c7c0 fix(desktop): stopping a training run did nothing on Windows
The CI matrix found this on its third run, and it is the same defect this
branch already fixed once on POSIX -- still fully present on Windows, reported
as a success. Every stop-related test failed there, and one of them says it
plainly:

    'RAN-TO-COMPLETION' unexpectedly found in the log
    : the run was reported cancelled but ran to the end anyway

_terminate_group sent CTRL_BREAK_EVENT. That event is delivered through a
console, and the trainer is spawned with CREATE_NO_WINDOW precisely so that it
has none -- so send_signal returned successfully, nothing died, and the
`except OSError: proc.terminate()` fallback never ran because there was no
error. The user pressed Stop, the UI said cancelled, and the fine-tune carried
on to completion holding the GPU.

It kills the process tree with `taskkill /T /F` now, which is what killpg
achieves on POSIX. /T is the part that matters: without it the trainer dies and
whatever it spawned keeps the card.

The test that was supposed to cover this asserted `proc.signals or
proc.terminated` -- "something was signalled". CTRL_BREAK_EVENT satisfied that
perfectly while the process ran on. This is the third time in this branch that
a test asserted an attempt rather than an effect, so it now asserts which pid
was killed, that the command carries /T and /F, that a failed taskkill still
escalates, and that a process which already exited is not killed again.

The tree kill is a module-level function so all of this runs from any platform.
The branch that only executes on the machine nobody tests on is exactly the
branch that was wrong.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-26 16:35:20 +01:00
MervinPraisonandClaude Opus 5 e6fff340c8 fix(desktop): the 51st training run could not start
My own regression from two commits ago, found by looking rather than by a test.

Capping the run history meant making it a bounded deque, and the insertion was
left as `insert(0, run)`. On a *full* bounded deque that raises
IndexError("deque already at its maximum size") -- so the run after the cap
would not start, and every run after that would fail the same way, on an app
that is meant to stay open for days. `appendleft` evicts from the far end,
which is the oldest run, which is what the cap is for.

The tests now reach the cap rather than stopping short of it. Both things that
go wrong here only go wrong when the deque is full: the insertion raising, and
eviction taking the newest run instead of the oldest. Neither is observable
below the limit, which is why a test that stops at three runs proves nothing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-26 16:21:51 +01:00
MervinPraisonandClaude Opus 5 6624f6fdd1 fix(desktop): the Windows failures the CI matrix found
Second round from the same matrix. Linux went green; Windows did not.

**The whole training route suite failed on Windows for a quoting reason.**
The test helper built `PRAISONAI_TRAIN_CMD` with `shlex.quote`, which escapes
an inner `'` as the concatenation `'"'"'`. POSIX reassembles that into one
token; Windows rules split it into seven. Every script in the suite prints
something, so every one of them contains a quote, and the trainer was launched
with a shattered command line -- the run never reached "running" and nine tests
timed out waiting for it. The body goes through a file now: a path has no
newlines and no quotes, so both rule sets agree about it. (`split_command`
itself is right; a Windows user would quote the Windows way.)

**The lockfile was not the bytes it says it writes.** `write_text` translates
"\n" to "\r\n" on Windows, so the file differed by platform. The Rust parser
trims each line and coped, but a lockfile whose bytes depend on the host is a
difference waiting to matter -- and the encode is the only conversion that
should be happening in a function documented as "always as UTF-8". It writes
bytes now.

The rest were test-only, and all the same mistake: `str(Path("/a/b"))` is
`\a\b` on Windows, and comparing a `PurePosixPath` to a `WindowsPath` fails
even when both name the same location. They compare `.as_posix()` now.

Also made the generated script filename a content hash rather than `hash()`,
which is salted per process and could have had two bodies collide on one file.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-26 16:08:26 +01:00
MervinPraisonandClaude Opus 5 a1d7167ce9 fix(desktop): the cross-platform test failures the new CI found
The CI added in the previous commit did its job on its first run: the engine
suite failed on Linux and Windows, and the Rust suite failed on Windows, while
all three passed on macOS -- the only platform any of this had ever run on.

Every failure is in the tests, not the product, and every one is the same
mistake in a different costume: asserting something about the *host* rather
than about the behaviour under test.

* Two Rust tests compared a whole PATH string against a hardcoded expectation
  containing forward slashes. `PathBuf::join` writes a backslash when the tests
  themselves run on Windows, so `C:/data/venv\Scripts` did not match
  `C:/data/venv/Scripts`. The subject is the separator *between* entries, so
  they now split on it and check the parts -- which is what the comment above
  them always claimed they were doing.

* Four Python tests compared `str(Path(...))` against a POSIX string, which is
  backslash-separated on Windows. They compare paths to paths now.

* The C-locale harness passed its statement through `python -c`. Python decodes
  that argument with the filesystem encoding -- ASCII with surrogateescape
  under LC_ALL=C on Linux -- so a test whose whole point was a non-ASCII path
  died on unpaired surrogates before reaching the code under test. It goes
  through a file now: source files are read as UTF-8 regardless of locale.

* The same harness captured the child's output with a bare `text=True`, which
  decodes using the *parent's* encoding. On a Windows runner a test about UTF-8
  handling was reading its own result back through cp1252 and failing on the
  round trip rather than on anything the engine did.

* One test asked FileSecretStore to write to "/nonexistent/nope" and expected
  failure. A Windows runner can create that at the drive root, so the store
  succeeded and the assertion was backwards. It now puts a file where a
  directory is needed, which fails everywhere.

None of this was reachable from a Mac, which is the entire argument for the
matrix.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-26 15:58:05 +01:00
MervinPraison f8f1f33d0e Merge branch 'feat-desktop-training' into fix-desktop-review-findings
# Conflicts:
#	src/praisonai-desktop/ui/index.html
2026-08-26 15:47:43 +01:00
MervinPraison 3a353ecb7b Merge branch 'feat-desktop-cross-platform' into fix-desktop-review-findings
# Conflicts:
#	src/praisonai-desktop/ui/index.html
2026-08-26 15:47:16 +01:00
MervinPraisonandClaude Opus 5 594982ec53 ci(desktop): run the desktop tests, which ran nowhere
An adversarial review pointed out that nothing in .github referenced
praisonai-desktop. Every suite in it -- engine, shell and UI -- existed and was
never executed by anything except a person running it by hand.

The UI suite could not even be installed. jsdom was undeclared, so it ran only
on machines that happened to have one somewhere further up the tree, at
whatever version that happened to be. Pinning it surfaced the consequence
immediately: 16 tests fail on jsdom 25 and pass on 30. They had been passing
against a version nobody chose.

The workflow runs the engine tests on all three platforms, which is the point
of the portability suite; the Rust tests and clippy on all three; and the UI
tests on Linux. Clippy is included because it was already failing -- a
redundant closure in supervisor.rs -- which is exactly the sort of thing a gate
catches and a person does not.

The lifecycle suite is excluded by name rather than left to skip. It drives
real conversations against a live engine and a real model, neither of which
exists on a runner, so it would have skipped all thirteen of its tests and
exited 0. The remaining 128 run with the skip count asserted to be zero,
because a suite that skips everything looks exactly like a suite that passed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-26 15:45:20 +01:00
MervinPraisonandClaude Opus 5 1051a192a7 fix(desktop): the training defects an adversarial review found
Three of these are in the "user cancels" and "user watches a long run" paths,
which are the two things this feature exists to do.

**Opening the Training tab on a run older than ~20 minutes showed nothing, and
pinned a core doing it.** The SSE handler answered a gap by resyncing at
cursor=-1 -- which makes the next since() compute a gap again, because every
ring that has ever evicted satisfies `0 < oldest`. The `continue` then skipped
both the sleep and the terminal check, so it spun at ~13,000 resync frames per
second, delivered zero log events, and went on spinning after the run had
finished. A tqdm refresh emits two events, so a real fine-tune passes the 4000
-event ring in about twenty minutes. It now resumes at the oldest event still
held and falls through to deliver it.

**Pressing Stop before the trainer process existed cancelled nothing.** start()
returns as soon as the supervisor thread is created, so between the HTTP reply
and Popen returning, run._proc is None and stop() had nothing to signal. It
recorded STOPPING and _supervise then overwrote it with RUNNING: the user got
{"ok": true} and a "stopping" pill, and the fine-tune ran to completion and
reported "done" -- holding the GPU it was told to release. The state is now
settled under the run's lock, and a stop that arrived first terminates the
process on arrival.

**One trailing slash let a stale tab kill the live run.** The route was matched
by string, and rsplit("/") on "/train/stop/<stale-id>/" yields "", which is
falsy -- so the guard that exists to stop exactly this was skipped. The same
matching treated "/train/stopXXXX" as the stop route and read "?force=1" as
part of the run id, refusing a legitimate stop. The path is parsed now.

Also:

* Two runs could share a directory, overwriting each other's config.yaml and
  interleaving into one train.log. Ids differing only in case are one directory
  on macOS and Windows; and the generated id had one-second resolution, so two
  runs started in the same second collided -- which the new check caught
  immediately. Ids are settled before anything is created, so a refused run no
  longer clobbers the config of the run it collided with.
* A method that could never work failed after the model download rather than
  before it. GRPO needs reward functions this form cannot supply, so it is now
  refused in milliseconds with a message saying to use the CLI. Column
  requirements depend on the dataset and are still the trainer's to check, so
  every method hint now names the columns it needs instead of leaving the user
  to discover them an hour in.
* _last_meaningful_line read the whole log to keep forty lines from the end --
  at the exact moment a run fails, and a tqdm bar writes a line per refresh. It
  reads the last 64KB now.
* An unwritable runs directory raised PermissionError past the handler, so the
  connection dropped with no response and the UI reported "could not reach the
  engine", sending the user after the wrong problem.
* A malformed ?cursor= was an unhandled ValueError that dropped the connection.
* run.metrics and Trainer.history grew without limit in a process meant to stay
  open for days.

Converting those two to bounded deques broke /train/runs and /train/status,
because a deque cannot be sliced -- caught by the existing route tests, which
is what they are for.

Tests: 49 + 23 + 6 Python, 134 UI. New coverage for replaying a run that has
outlived its buffer, every stop-route shape, the pending-stop window, run-id
collisions, and the method preflight. All five fixes were mutation-tested.

Two notes on the tests themselves. The pending-stop case cannot be reached
through HTTP -- two round trips are slower than a spawn -- so it is tested
where it is deterministic, by delaying the spawn. And its first version asserted
only that the final state was "cancelled", which a version that never signals
the process also satisfies while the job runs to the end; it now asserts the
process died and the script's completion marker never reached the log.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-26 15:39:33 +01:00
MervinPraisonandClaude Opus 5 dd55e367f8 fix(desktop): the training history heading used an inline style
Found while fixing the same class of defect on the cross-platform branch.

Configuring a CSP makes Tauri stamp a nonce on the page's <style> tag, and a
nonce in a source list voids 'unsafe-inline' for that directive -- so every
style="" attribute in the document stops applying. The heading above the run
history would have lost its spacing the moment the two branches met, silently.

A class instead. The other inline styles on this branch are pre-existing and
are handled in the cross-platform work, along with the test that would have
caught this one.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 23:34:58 +01:00
MervinPraisonandClaude Opus 5 2718c7a738 fix(desktop): the defects two adversarial reviews found in the last commit
Three ship-blockers, one security bug, one claim of mine that was simply wrong.

**The CSP I added broke the app, and I said it would not.** Configuring any CSP
makes Tauri stamp a nonce on the page's <style> tag and a hash on its inline
<script> at build time -- and a nonce or hash in a source list *voids*
'unsafe-inline' for that directive. So the moment a CSP existed, every
style="" attribute in the document stopped applying: a permanently visible red
Stop button, a raw file-input widget, and every tool-output block expanded, on
first launch. Nothing errors; it just looks broken. Those five attributes are
now classes. Setting .style from JavaScript is CSSOM and was never affected --
but `el.style.display=''` to reveal an element now restores the class default,
which is `display:none`, so those three call sites toggle the class instead.

**The Linux tray took the whole app down.** `tray::build(...)?` in setup, and
on Linux the tray goes through libappindicator, which is dlopen'd at first use
and *panics* if neither library is present -- not catchable, since we build
with panic=abort. Because it is dlopen'd there is no DT_NEEDED entry, so
dpkg-shlibdeps could not generate the dependency either: the package installed
cleanly and then did nothing when clicked. The tray is now non-fatal and the
library is declared explicitly, along with curl, which the uv bootstrap runs
and minimal images omit.

**The macOS overlay silently deleted the window geometry.** Tauri merges
tauri.macos.conf.json with RFC 7386, whose rule for arrays is replacement, not
element-wise merge. An overlay carrying only the two macOS keys replaced the
whole `windows` array: title, width, height and both minimums vanished, so the
app became an 800x600 "Tauri App" that could be dragged down to nothing. The
overlay now repeats the full object.

**Deleting an API key reported success while the key kept working.** Once a
secret had been written to both stores -- keyring unavailable, then available
again -- a delete that short-circuited on the first success left the other copy
behind, and the read path served it. The plaintext stayed on disk. A delete now
reaches every store, and a successful write clears the fallback so a superseded
key is not left readable.

Everything else, briefly:

* macos-13 was retired on 2025-12-04, so the Intel leg would never have
  scheduled -- and the completeness check would then have failed every release,
  forever, correctly. Now macos-15-intel.
* `cargo install tauri-cli ... || true` swallowed a real failure and the next
  step died complaining about an unrelated missing command. It also behaved
  differently per leg, since Windows defaults to pwsh where `true` is not a
  command.
* Two spawn sites had no console suppression -- including the engine itself, so
  a black console window sat beside the app for the entire session on Windows.
  There is now a lint over every spawn site, which is honest about being a lint:
  a Windows creation flag has no observable effect here, but "did anyone forget
  to call it" is exactly what regressed.
* One undecodable byte silently ended the engine's log reader for the rest of
  the session. `lines().map_while(Result::ok)` *stops* on the first
  Err(InvalidData) rather than skipping it, and Python encodes redirected
  streams with the locale code page on Windows. If it arrived before the port
  announcement, startup failed 30 seconds later blaming a timeout.
* The engine spawn had no process group on Unix, so SIGTERM reached the engine
  and nothing it had started -- with the training tab, a fine-tune would have
  kept a GPU after quit.
* `XInitThreads` is now called first in main(). GTK never calls it, several
  threads drive X anyway, and the failure is `_exit(1)` with no message.
* Empty environment variables were treated as set on the Rust side only, so
  `XDG_DATA_HOME=""` made the shell look for the lockfile in the working
  directory while the engine wrote it under the home directory -- "no lock,
  spawn" on every launch.
* The uv installer's exit code was taken as proof it installed something;
  `powershell -Command "irm ... | iex"` can exit 0 having done nothing.
* Three write-then-rename sites -- transcripts, MCP config, settings -- did not
  use the retry helper whose own docstring names their failure case.
* A leftover temp file kept its old permissions, so a secret could be written
  world-readable before the chmod that follows the rename. O_EXCL now.
* A DPAPI blob that would not decrypt read as empty and was then overwritten,
  destroying every other secret in it.
* The tray used macOS template rendering and Cmd accelerators everywhere:
  a black-on-black icon, and a menu advertising Win+Q, which Windows takes.
* "Copy data folder path" reproduced the default instead of asking the engine,
  so it handed the user a path the app was not using whenever it was
  overridden. /health now reports it.

Tests: 35 engine, 123 Rust, 141 UI. New: a CSP guard (there was none -- no test
in the suite sets a CSP header, which is why this shipped), a lossy-reader
suite, spawn-site hygiene with a positive control, and secret-deletion tests
that failed before the fix.

Two of my own new tests were wrong in the usual way. One asserted a repair
button *existed* without clicking it, so replacing its handler with an empty
function changed nothing. The other's comment-stripper only checked line
prefixes, so a continuation line inside a block comment -- this file's own
explanation of the bug -- was reported as an instance of it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 23:34:04 +01:00
Mervin Praison 5dfe20e929 Merge pull request #4373 from MervinPraison/feat-desktop-cross-platform
feat(desktop): ship on Windows and Linux, not only macOS
2026-08-25 23:09:33 +01:00
Mervin Praison 8db69b55ef Merge pull request #4372 from MervinPraison/feat-desktop-training
feat(desktop): add a Training tab that runs a fine-tune and reports on it
2026-08-25 23:09:27 +01:00
MervinPraisonandClaude Opus 5 c72b6b1d5c ci(desktop): release for Windows and Linux too, and verify nothing is missing
The macOS release workflow shipped first. Adding the other two was not a matter
of adding runners: the bundle targets were macOS-only, so a Windows or Linux
build produced zero artifacts and still exited zero. That is fixed in the
config change; this wires the pipeline to it.

Four legs now: macOS on both architectures, Windows NSIS, Linux .deb. NSIS
rather than MSI because MSI can only be built on Windows, needs an optional
Windows feature, and installs to Program Files behind a UAC prompt; NSIS
installs per-user with no administrator password. .deb only -- no RPM, no
AppImage. AppImage needs libfuse2, which Ubuntu 24.04 removed, and renders a
blank window on some Wayland and Mesa combinations; a .deb that works beats an
AppImage that half does.

Two decisions that look arbitrary and are not:

* The Linux leg pins ubuntu-22.04. glibc is forward-compatible only, so the
  build host sets the *oldest* distribution the package can start on. Building
  on 24.04 would emit a .deb that refuses to launch on 22.04 or Debian 12 with
  a GLIBC_2.38 error no runtime flag can fix. That image begins deprecation on
  2026-09-17 and retires 2027-04-17; the comment says to move to a debian:12
  container rather than bump the runner, so the floor stays our decision.
* libappindicator3-dev, not the ayatana package the Tauri docs suggest. The
  .deb's generated Depends: follows whichever the binary linked against, so
  mixing them between builds produces packages that disagree about their own
  dependencies.

A `verify` job now fails the release if any platform is missing an asset. A
release that quietly ships three platforms instead of four is worse than one
that fails, because nobody notices -- which is how v4.7.1 went out with zero
assets in the first place.

The install doc covers all four downloads, and says plainly what each platform
does about an unsigned build: Gatekeeper's misleading "damaged" message and the
right-click-Open, SmartScreen's hidden Run anyway, and `apt install ./file.deb`
rather than dpkg so dependencies resolve.

Guarded by tests, because these three files must agree and none of them fails
loudly when they do not: the workflow matrix, the config's bundle targets, and
the filenames the install doc tells people to download. 9 tests, 11/11
mutations caught -- including dropping a platform, swapping the appindicator
package, bumping the Linux runner, and removing the Windows icon.

One of those tests was weak on the first attempt: it grepped the whole file for
`exit 1`, which appears in the verify job too, so it passed with the
artifact step's guard removed. It is now scoped to that step.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 23:04:25 +01:00
MervinPraisonandClaude Opus 5 04c916b8e9 fix(desktop): bundle for Windows and Linux, and stop the UI assuming a Mac
The bundle targets were `["app", "dmg"]`. Both are macOS-only bundle types, so
a Windows or Linux build produced *zero artifacts* and still exited zero --
the failure mode is a green build with nothing in it. Added nsis and deb.

`titleBarStyle: "Overlay"` and `hiddenTitle` are macOS-only keys, silently
ignored elsewhere, which left a native title bar sitting on top of the CSS one.
They now live in tauri.macos.conf.json, which Tauri merges per platform.

`csp: null` disabled the content security policy entirely. It is now explicit,
and allows exactly what this app needs: loopback to the engine, which is how
the webview talks to Python at all.

On the UI side the page now reads which platform it is on and adapts three
things that were macOS facts written as layout constants:

* 5.2rem of left padding for the traffic lights. Windows and Linux put their
  window controls top-*right*, so this was dead space on the left and an
  overlap with the toolbar on the right.
* `⌘N`, `⌘K`, `⌘,` labels. The keydown handler already accepted Ctrl, so the
  shortcuts worked -- the hints just named a key those keyboards do not have.
  They now come from one place, so a new shortcut cannot be added with a
  hardcoded modifier that is wrong on two platforms out of three.
* "into your Library folder", and a Copy button that copied
  `~/Library/Application Support/PraisonAI` on machines where no such path
  exists. Windows gets %APPDATA%, which pastes straight into Explorer.

Detection is layered -- userAgentData, then navigator.platform, then the user
agent string -- because the last is the easiest to be wrong about: a webview
reporting only "(darwin)" matched none of the names and fell through to Linux
on a Mac. That was caught by a test, not by reasoning.

Two test defects fixed along the way, both of which had been passing while
proving nothing:

* `first-run.test.mjs` asserted the lede contains "Library". That passed only
  because this suite has only ever run on macOS; on the Linux runner this
  release needs, it would fail on correct behaviour -- and anyone fixing the
  copy would have "broken CI" for doing the right thing. It now asserts the
  copy names *a* place, whichever platform is reading it.
* The new platform tests originally passed jsdom's `userAgent` option, which
  is silently ignored, so all three cases ran against jsdom's own agent and
  were really testing one platform three times. The navigator properties are
  now defined directly. A second version read `body.textContent` -- which
  includes the inline script's source -- and "found" macOS copy on Windows
  inside the source of the very branch that avoids it.

Also generated icons/icon.ico: without it tauri-build does not warn, it
refuses to compile for Windows at all.

Tests: 6 new platform tests, 7/7 mutations caught. Full suite 124 passing, and
verified still passing with the page forced to report Linux.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 23:00:49 +01:00
MervinPraisonandClaude Opus 5 09c433d019 fix(desktop): make the Tauri shell run on Windows and Linux
The crate always compiled for all three platforms -- there is exactly one
`#[cfg]` in it -- so nothing ever failed loudly. Every defect here is a runtime
one: the app starts and then cannot find its own engine.

Platform is now a value rather than a `cfg`. Every path and process decision
that differs goes through a function taking one, because `#[cfg(windows)]`
makes the Windows branches invisible to a suite that only ever runs on macOS,
and invisible branches are how the code nobody runs comes to be wrong.

What was broken:

* `HOME` was the only home lookup. It is normally unset on Windows -- that is
  `USERPROFILE` -- so provisioning returned "no home directory" and the
  orphan-reclaim path never ran at all.
* Every interpreter candidate ended in `bin/python3`. A Windows venv keeps its
  interpreter in `Scripts\python.exe`, so all of them failed `is_file()` and
  the app reported "no usable Python found" immediately after successfully
  provisioning one.
* The uv bootstrap ran `/usr/bin/curl` piped into `/bin/sh`. Neither exists on
  Windows, so first-run provisioning was simply dead; and pinning curl's
  absolute path breaks Linux images where it lives elsewhere. There is now a
  PowerShell installer for Windows, and curl is found by name.
* `/bin/kill` was spawned unconditionally to reap an orphan, and the call site
  discards the result -- so on Windows, deciding to kill an orphan killed
  nothing, which is the exact bug reclaim.rs exists to prevent.
* `/bin/ps -o lstart=` was the only start-time source, so every pid on Windows
  read as "no such process", every live engine looked dead, and a second one
  started beside it on every launch. Windows now reads the creation stamp from
  kernel32.
* site-packages was derived as `lib/python3.X/site-packages`. Windows is
  `Lib\site-packages` with no version segment -- and the caller's only check is
  that the path sits under the venv root, so the wrong path passed the guard
  while pointing at nothing, which is worse than failing.
* `spawn_env` joined PATH with `:` and used `bin`. Windows is `;` and
  `Scripts`; one wrong separator makes the entire PATH a single unusable entry.

The shell and the engine must also agree on two things or they fail silently
and permanently: the data directory, and the hash that identifies a running
process. Both are now asserted from both sides -- reclaim.rs and
test_portability.py carry the identical fixture table, so if either hashing
implementation drifts, both suites fail rather than the two quietly disagreeing
and starting an engine per launch forever.

The PowerShell installer deliberately avoids `-WindowStyle Hidden` with
`-ExecutionPolicy Bypass`: that pair is a known malware detection signature and
gets installers flagged. The console is suppressed with a creation flag, which
is not a behavioural signal. `UV_NO_MODIFY_PATH` is set on every platform,
because an app should not rewrite the user's shell profiles to install its own
dependency.

Also generated the icon set. `icons/icon.ico` was missing, and tauri-build does
not warn about that -- it refuses to compile for Windows at all.

Verified rather than assumed: `cargo check --target x86_64-pc-windows-msvc
--all-targets` passes, so the FFI and the CommandExt code that macOS never
compiles is at least type-checked. Linux cannot be checked from here because
libdbus-sys needs system headers that only exist on Linux; CI covers it. 116
tests pass on the host.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 22:53:55 +01:00
MervinPraisonandClaude Opus 5 618e12a6bd fix(desktop): make the engine run on Windows and Linux, not just macOS
Six independent macOS assumptions. Two of them kill the engine outright on
Windows; one makes settings unsavable on Windows *and* Linux.

The signal registration was the worst, because of where it failed rather than
how. `for sig in (SIGTERM, SIGINT, SIGHUP)` builds the tuple before the loop
body, so SIGHUP is dereferenced outside the try that was meant to tolerate a
missing signal -- and Windows has no SIGHUP. It raised AttributeError, which
that except clause would not have caught anyway. It happened *after* the port
was announced and the lockfile written, so the shell adopted an engine that
was already dead and every request looked like a network fault. Signals are
now looked up one name at a time.

Secrets went to the macOS keychain via the `security` binary, which does not
exist elsewhere: the write reported failure, POST /settings raised with no
handler, and load_settings reported the key as unset forever. The app was
permanently unauthenticated on both other platforms with no visible cause.
There is now a store per platform -- keychain, DPAPI, libsecret -- each backed
by a 0600 file, because a machine with no keyring daemon (headless, container,
locked login keyring) must still be able to save a key. A secret is never
written into settings.json, which is the file people paste into issues.

The data directory was `~/Library/Application Support` everywhere. On Windows
that is not merely unconventional: it is excluded from roaming profiles and
from most backup tooling, so a user's entire history silently fails to follow
them to a new machine. Now %APPDATA% and $XDG_DATA_HOME respectively.

Three quieter ones. The lockfile was written in the locale encoding while the
Rust side reads it as strict UTF-8 and maps invalid bytes to "absent" -- and
absent means spawn, so a non-ASCII home directory would have produced a second
engine beside the live one on every launch. `read_file` decoded user files in
the locale encoding with errors replaced, handing the model plausible mojibake
it would reason over confidently. And SO_REUSEADDR, correct on POSIX, means
"another process may bind this address while I am listening" on Windows --
on unauthenticated loopback carrying API keys.

Also: the recycled-pid fingerprint shelled out to `ps` and returned 0 on
Windows, and 0 is the "could not tell" value, so every recycled pid compared
equal and an unrelated process would be adopted as the engine.
GetProcessTimes gives a real creation stamp.

One incident worth recording. Verifying this live wrote a test key into my own
macOS keychain, because PRAISONAI_DESKTOP_HOME isolates the data directory but
not the system keyring, which is shared per user. KEYCHAIN_SERVICE is now
overridable so a test cannot reach the real one. Asking for isolation should
isolate everything.

Tests: 27, all mutation-checked -- 13/13 mutations of these guarantees are
caught. Four of those tests were rewritten after the first versions survived
their mutations: the XDG test compared a path to itself (its "custom" value
was the default), the two encoding tests could not fail on a UTF-8 Mac and now
run under LC_ALL=C in a child process, and the permission test asserted the
final mode, which passes even when the file is created 0644 and chmodded
afterwards -- leaving a window where any local process can read the key.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 22:41:43 +01:00
MervinPraisonandClaude Opus 5 ec3a260c9a feat(desktop): add a Training tab that runs a fine-tune and reports on it
The desktop app could talk to a model but not make one, while praisonai-train
sat behind it doing exactly that. This wires the two together.

A training run is a subsystem rather than a route. The engine is a
ThreadingHTTPServer where every request is a turn that starts, streams and
ends -- right for chat, wrong for something that runs for hours, must survive
the window closing, and has to resume after a reconnect.

Three decisions, each taken because the alternative was worse:

* One run at a time. Two fine-tunes on one GPU do not co-exist; they OOM.
  Starting a second returns 409 naming the run that is already going, rather
  than queueing, which would imply a scheduler nobody asked for.
* Progress is a replayable ring buffer, not a stream. Reconnecting reads from
  a cursor, so a closed lid loses nothing; if history was evicted the client
  is told to resync rather than handed a log with a hole in it.
* The subprocess owns the truth. Status comes from the process and its log,
  never from what the client last heard, so a client that missed the ending
  still learns how it ended.

Three defects the tests found while being written, all of which would have
shipped:

* The event cursor was derived from len(events). Once eviction begins the
  length stops growing, every later event is handed the same cursor, and
  since() returns nothing forever -- a live run whose UI silently stops
  updating. It is now a standalone counter.
* run_id came from the client and became a directory name. The engine listens
  on loopback with no auth by design, so any local process could have posted
  `../../../Library/LaunchAgents/x`. Now allowlisted.
* Stopping a run signalled the process group without checking whose group it
  was. The mutation that removes start_new_session proves the hazard is real:
  it makes the test runner kill itself. The engine now refuses to signal its
  own group.

Portable from the start, since Windows and Linux builds are next: the pipe is
decoded as UTF-8 rather than the locale encoding (cp1252 turns unsloth's
banner into mojibake, and five of its bytes raise inside the reader loop,
freezing the log while the run continues); PRAISONAI_TRAIN_CMD is split with
Windows rules on Windows, where POSIX splitting eats every backslash in an
interpreter path; the child gets CREATE_NEW_PROCESS_GROUP there, because
CPython accepts start_new_session on Windows and then ignores it; and run ids
reject reserved device names, which an alphanumeric allowlist cannot see.

Tests: 42 engine unit tests, 10 end-to-end tests against a real spawned
engine, 16 UI tests driving the real page in a DOM. All 9 mutations of the
engine's guarantees are caught, and all 7 of the UI's. The existing desktop
suite stays green at 134.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 22:35:01 +01:00
Mervin Praison c5e97aedcb Merge pull request #4371 from MervinPraison/feat-desktop-release
ci(desktop): build the Mac app on release and attach it
2026-08-25 22:15:39 +01:00
MervinPraisonandClaude Opus 5 8f64195a5d ci(desktop): build the Mac app on release and attach it
Releases carried no assets at all. v4.7.1 shipped zero, so a Mac user had
nothing to download and no path to the app that exists in this repo.

On a published release (or by hand against an existing tag) this builds a .dmg
on macos-14 and macos-13 and uploads both:

    PraisonAI-<version>-macos-apple-silicon.dmg
    PraisonAI-<version>-macos-intel.dmg

Two per release rather than one universal binary, because Tauri does not
produce a universal build and a wrong-arch download fails at LAUNCH rather than
at install -- which is a worse place to discover it.

The build fails loudly when no .dmg is produced instead of uploading nothing.
An empty asset list is exactly the state this workflow exists to fix, and a
silent no-op would recreate it while looking green.

Unsigned, deliberately: signing needs a Developer ID in repository secrets, and
shipping an unsigned build people can right-click-open beats shipping none.
INSTALL-macOS.md says so plainly rather than letting Gatekeeper say it -- its
message for an unsigned app is "PraisonAI is damaged and can't be opened",
which is both alarming and untrue. The guide covers which architecture to pick,
the right-click-Open path, the xattr fallback, what the first-run provisioning
does and where it puts things, and how to remove it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 22:12:49 +01:00
MervinPraisonandClaude Opus 5 3c846a7baf test(train): stop tests from asserting on source text instead of behaviour
Rebuilt on current main after #4367, #4368 and #4370 merged.

An adversarial audit ran 16 mutations against this suite. EIGHT survived all
408 tests, and every survivor was covered only by

    src = inspect.getsource(some_function)
    assert "the_thing_i_want" in src

which passes whether or not the code does anything: the string is still in the
file when the branch around it is disabled.

The worst case was a test holding a COPY of the logic it tested.
test_response_masking.py defined _auto_use_mask() mirroring train_model's
`auto` expression, so reverting the real line to the pre-fix `supports_mask`
alone left the whole suite green -- the single defect that PR existed to fix
was undetectable by its own test file. The expression is now
resolve_mask_setting() and the test calls it; that mutation now fails.

The guard caps source-text assertions, bans reimplementing tested logic, and
asserts the extracted helpers stay importable -- each of resolve_mask_setting,
decide_masking, resolve_response_markers, is_out_of_memory, rewards.require and
rewards.resolve_all exists because a source-text test failed to catch a bug,
and inlining one silently returns its test to proving nothing.

The cap is a ratchet, not a ban: ordering constraints are genuinely structural.
It tripped twice on rebases while this PR waited, which is the point. Both
times the new assertion turned out to be convertible and was converted rather
than waved through: _launch_script RETURNS its command, and the PEFT forwarding
list is data, now the module constant PEFT_PASSTHROUGH.

Two things the guard got wrong first, both found by running the regression
rather than reading it: it matched the COMMENT explaining the removed copy, so
it now tokenises and drops comments and strings; and joining tokens with spaces
turned `inspect.getsource` into `inspect . getsource`, so the count missed
every occurrence.

483 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 22:11:23 +01:00
Mervin Praison 5bfb8c8e57 Merge pull request #4370 from MervinPraison/feat-unsloth-surface
feat(train): the unsloth surface that was declared and unreachable
2026-08-25 22:09:05 +01:00
MervinPraisonandClaude Opus 5 8dd0f3310a feat(train): the unsloth surface that was declared and unreachable
Each of these is a keyword unsloth already accepts and this package never
passed. Nothing failed when they were missing -- the run just did something
narrower than the user asked for.

QUANTS: 12 of unsloth's 24 were refused by this validator before unsloth was
ever asked -- q3_k_l, q4_k_s, q5_k_s, f32 and the aliases are all legal
upstream. The IQ family (11 imatrix quants) was unreachable entirely, which is
how a 30B model fits on a laptop. Now 35 accepted, read out of unsloth's own
save.py rather than hand-picked, with a test that fails when the two drift.

MODEL ACCESS: hf_token, trust_remote_code and revision were unreachable, so a
gated repo (Llama, Gemma) worked only through an environment variable, a
custom-code model not at all, and a multi-day run could not pin a revision --
the tag moves and the run is no longer reproducible.

PEFT SELECTORS: the target-module list was hardcoded to seven names.
finetune_last_n_layers (the cheap-run recipe), target_parameters (required to
LoRA MoE experts), layers_to_transform, layers_pattern and init_lora_weights
are all forwarded now.

OFFLINE MERGE: save_pretrained_merged was never called, so the only way to get
merged weights was to PUSH them -- needing a Hub account, a token and a
network. `merged_save_dir` writes them to a directory.

vLLM: fast_inference was never passed, so GRPO always ran the slow HF-generate
rollout path.

28 tests. Five mutations, all killed.

One of them could not be expressed at first, because the test helper
REIMPLEMENTED the assembly it was checking -- in the very change that bans
reimplementing tested logic. It is now model_access_kwargs() and the test calls
it; deleting the token forwarding fails.

480 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 22:08:48 +01:00
Mervin Praison 3ddb96aa1c Merge pull request #4367 from MervinPraison/fix/train-audit-findings
fix(train): five defects an adversarial audit found in the merged work
2026-08-25 22:01:28 +01:00
Mervin Praison 8e7827d512 Merge pull request #4368 from MervinPraison/fix-desktop-restore
feat(desktop): the Mac app work, recovered from a stash that nearly lost it
2026-08-25 22:01:17 +01:00
MervinPraisonandClaude Opus 5 d2b9dd8a10 feat(desktop): the Mac app work, recovered from a stash that nearly lost it
A `git stash --include-untracked` during merge-conflict repair swallowed the
whole untracked praisonai-desktop tree and it was never restored. It surfaced
only because a UI audit described settings as inert that had been fixed days
earlier -- true of the tree on disk, not of the work. Recovered from
stash@{0}^3.

What it contains, all previously reported and none of it on main:

- The eleven dead settings wired to real behaviour, each proven by a test swept
  across at least two values.
- Text size scales the whole interface rather than only the chat, with every
  fixed-pixel dimension converted to rem and a test that fails on any
  dimensional px over 3px.
- The transcript and the composer share one column. They were ~240px apart
  because an auto margin on a flex item's cross axis overrides `stretch`.
- In-app confirmation dialogs. window.confirm returns false immediately in a
  WKWebView without showing anything, so every guard written as
  `if (!confirm(...)) return;` cancelled -- which had silently broken deleting
  a conversation outright.
- data-tauri-drag-region plus core:window:allow-start-dragging, without which
  the titlebar could not move the window: -webkit-app-region is a Chromium
  feature WKWebView does not implement.
- Accessibility: landmarks, a live region for streamed answers, accessible
  names on icon-only controls, a focus-trapping dialog, aria-current on rows.
- Real app and menubar icons from the PraisonAI mark. A macOS template image is
  drawn from its alpha channel alone, so the previous one rendered as a solid
  block.
- First-run provisioning with staged progress, message versions with a
  `< 1/2 >` picker, and orphan reclamation through the lockfile the Rust side
  had always specified and nothing wrote.

118 JS tests across 11 suites, 90 Rust tests, all passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 21:58:36 +01:00
MervinPraisonandClaude Opus 5 49e8151175 fix(train): ten defects an adversarial audit found in the merged work
Three agents audited the eleven PRs that just merged. Every defect below passed
the suite that shipped it.

FIVE IN THE CODE

1. --dry-run showed one config and a different one trained. Forwarding
--dataset/--model -- which I added as "belt and braces" -- puts the legacy
dispatcher on its regeneration branch (praison_ai.py:676), where
generate_config() runs with every tuning parameter None and rewrites
config.yaml from defaults: lora_r 64 -> 16, epochs 5 -> 1, method dpo -> sft,
and huggingface_save false -> TRUE with hf_model_name defaulted to a third
party's repository.

2. cpt silently lost embedding_learning_rate, the one value it exists to set.
__dataclass_fields__ is inherited, so on a plain __init__ subclass it resolves
to the parent's fields and the drop-unknown filter deleted the value 45 lines
after setting it -- along with max_seq_length and packing.

3. remote start could never train: it passed config.yaml as the dataset
positional. Two positionals meant exit 2 before the GPU was touched.

4. Remote command injection and path traversal. --run-id 'x;touch /tmp/pwned'
executed, because the far half of an scp target is expanded by a shell there.
fetch ../../../../etc/passwd passed the existence gate and copied the file
back. Both quoted and validated now.

5. hf_private failed open: 'private', 'y', 'enabled', 5 all published the
repository.

FIVE AT THE BOUNDARY

6. llm overwrote ./config.yaml with no backup, and had already destroyed a
hand-written one in this repo's working tree.

7. praisonai train exposed 6 of 15 commands and no remote group, because the
integrated CLI imports cli/commands/train.py while the siblings register via
cli/app.py.

8. tests/unit/data/ -- 96 tests -- was collected by no workflow at all.

9. The suite needed 3.11 while pyproject declares >=3.10.

10. pip install "praisonai-train[llm]" produced an llm command that died at
argument parsing; nothing declared the praisonai wrapper it imports.

44 tests. Ten mutations, all killed -- four only after fixing the test first: a
fake class with **kwargs returned early, two called a validator the code path
no longer had to use, and one matched the comment explaining the fix rather
than the configuration. That last shape has now appeared three times.

452 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 21:56:52 +01:00
Mervin Praison 383d122591 Merge pull request #4366 from MervinPraison/feat/train-grpo-and-reward-modelling
feat(train): GRPO, reward modelling and CPO
2026-08-25 21:34:21 +01:00
Mervin Praison 58a1b3537c Merge pull request #4363 from MervinPraison/feat/train-post-training-commands
feat(train): answer the two questions a fine-tune ends with
2026-08-25 21:33:59 +01:00
MervinPraisonandClaude Opus 5 539aa3ac54 feat(train): GRPO, reward modelling and CPO
Rebuilt again on current main, which moved when four sibling PRs merged.

The last of unsloth's training methods. It rewrites every trl.trainer.*_trainer
module generically (rl.py:4235-4248), so all three work through it. GRPO was
the blocker for the rest, because it needs reward functions and a YAML config
can only carry a string.

DECISION, stated because it is the one thing I could not derive: reward
functions are named by dotted import path, `module:function`.

    method: grpo
    reward_funcs:
      - myproject.rewards:length_penalty
    num_generations: 8

A registry populated by decorator would mean the config can only name functions
from a module something else already imported. Inline Python in YAML is a
code-execution surface in a file people paste from the internet. An import path
is the only one of the three resolvable before anything runs -- which is why
rewards resolve before the model loads, so a typo costs five seconds rather
than a multi-gigabyte load. If you would rather have a registry, the change is
contained to rewards.py.

A missing module and a missing attribute are told apart, since one is a
PYTHONPATH problem and the other a typo; the attribute error lists the
callables that are there. Every broken path is reported at once. GRPO takes
prompts only, since it generates its own completions.

394 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 21:31:42 +01:00
MervinPraisonandClaude Opus 5 b6f604bbab feat(train): answer the two questions a fine-tune ends with
Rebuilt again on current main, which moved when #4361/#4362/#4364/#4365 merged.

    praisonai-train checkpoints
    praisonai-train infer -d lora_model "Summarise this release."

checkpoints: after an interrupted run there was no way to find what was saved
without `ls`. Matches checkpoint-<n> exactly, so a directory called
checkpoint-final is not reported as step 0, and sorts numerically.

infer: TrainModel.inference() existed with no caller anywhere in the package.
serve --benchmark measures tokens per second without showing a token.
Streamed, with usable defaults rather than the dead inference()'s
temperature=1.5 / max_new_tokens=64.

Named infer, not generate: generate is already dataset generation, and a second
command of that name silently replaces one with the other.

Also one line: dataset_num_proc defaults to None rather than 1, so unsloth's
memory-aware sizing is not overridden into a single process.

386 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 21:31:09 +01:00
Mervin Praison 2af4b9f3d2 Merge pull request #4365 from MervinPraison/fix/train-environment-preflight
fix(train): check the versions, and look at the GPU before pinning CUDA
2026-08-25 21:29:47 +01:00
Mervin Praison 29ec1b09e8 Merge pull request #4364 from MervinPraison/feat/train-model-catalog
feat(train): validate model names against unsloth's own catalog
2026-08-25 21:29:31 +01:00
Mervin Praison 6d0fab4702 Merge pull request #4362 from MervinPraison/feat/train-cli-config-and-flags
feat(train): give `llm` a --config, a --dry-run and the flags that matter
2026-08-25 21:29:05 +01:00
Mervin Praison 85574ae4cd Merge pull request #4361 from MervinPraison/feat/train-continued-pretraining
feat(train): continued pretraining, which was blocked two separate ways
2026-08-25 21:28:51 +01:00
MervinPraisonandClaude Opus 5 3012b98ce3 feat(train): validate model names against unsloth's own catalog
Rebuilt on current main; the previous branch carried eight unrelated commits.
The `models` command now lives in its own module rather than alongside the
post-training commands, so this change does not depend on them.

model_name was free text with no validation anywhere. A typo was caught by
Hugging Face -- after the CLI accepted it, after the environment was built,
sometimes after a partial download. And nothing told a new user which models
work: config.yaml offers exactly one example, from the Llama-3.1 era.

    praisonai-train models
    praisonai-train models gemma
    praisonai-train models --json

The catalog is read from unsloth's mapper at runtime, not vendored: it carries
246 distinct 4-bit repos and is updated every release, so a copy here would be
stale the day it shipped. CURATED is the fallback for when unsloth is absent --
the CLI is importable without the training extra -- and the starting point for
someone who has not chosen yet.

An unknown model warns rather than refusing, because unsloth loads plenty it
does not map. Suggestions match the repo name, not the full id, so an upstream
id surfaces the unsloth mirror. The catalog includes mapper values as well as
keys, since a 16-bit mirror appears only as a value and naming one would
otherwise be reported as unknown.

335 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 21:23:44 +01:00
MervinPraisonandClaude Opus 5 23180a9c80 fix(train): check the versions, and look at the GPU before pinning CUDA
Rebuilt on current main; the previous branch carried nine unrelated commits.

Two ways to end up with an environment that looks fine and cannot train.

Version floors were pip metadata only. pyproject declares torch>=2.6.0 and the
rest, but nothing checked at runtime -- and pip is routinely bypassed:
--no-deps, a hand-built conda env, setup_conda_env.sh pinning its own torch.
The failure then arrived as whatever traceback the mismatched package happened
to raise, several imports deep. A test asserts the floors match pyproject
exactly: lower lets a broken environment through, higher refuses one pip would
have built.

The CUDA pin ignored the hardware. Every Linux host got pytorch-cuda=12.4
regardless. On AMD or Blackwell that builds a working-LOOKING environment that
fails at the first kernel; on a CPU host it installs a CUDA build that can
never run. There is a probe now -- nvidia-smi, then /proc/driver/nvidia/gpus
for a host with the driver but not the tool, then rocminfo -- and the pin lives
in one place instead of four copies across two branches.

A CPU-only Linux host is told plainly that training will refuse, since
trainer.py raises on a missing GPU. A prerelease of the required version counts
as satisfying the floor, because refusing 2.6.0rc1 blocks people testing
upstream and unsloth's own checks are release-level.

338 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 21:22:55 +01:00
MervinPraisonandClaude Opus 5 8212dd07ea feat(train): continued pretraining, which was blocked two separate ways
Rebuilt on current main; the previous branch carried four unrelated commits.

A raw-text corpus could not load. A dataset with only a `text` column fell
through to the Alpaca branch, formatted every row to "", and died with "All
examples formatted to empty text" — an error blaming the dataset for a shape
the formatter did not handle. Domain and language adaptation could not run.

And the embeddings got the wrong learning rate. modules_to_save was exposed, so
a user could make embed_tokens/lm_head trainable, but the trainer was plain
SFTTrainer, which has no embedding_learning_rate. They trained at the adapters'
rate — the recipe unsloth ships UnslothTrainer to avoid.

    method: cpt
    modules_to_save: [embed_tokens, lm_head]
    embedding_learning_rate: 2e-5      # defaults to learning_rate / 10

Raw text passes through with no tokenizer call; ShareGPT and Alpaca still win
over a stray `text` column; cpt is formatted like SFT rather than taking the
preference passthrough. The empty-dataset error now names the three shapes and
prints the columns the dataset actually had.

326 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 21:21:46 +01:00
MervinPraisonandClaude Opus 5 d54647ae9e feat(train): give llm a --config, a --dry-run and the flags that matter
Rebuilt on current main; the previous branch was cut from its predecessor and
carried five unrelated commits.

`praisonai-train llm` took three flags to `unsloth train`'s thirty-eight, and
was the one command that would not accept --config though export and serve
both do. Eleven flags now, plus --config and --dry-run.

Two fixes on top of the original branch, both from problems main surfaced:

train_llm is called directly as a plain function, not only through Click --
main's own tests do it. Unfilled Typer parameters then arrive as OptionInfo
objects and Path(config) raises TypeError. They are unwrapped to their declared
defaults first.

The dataset and model reach argv as well as the resolved config.yaml. The
config path alone is the better mechanism, since the legacy dispatcher drops
every flag it does not declare -- but an earlier incident had the dataset
dropped entirely and the run trained on a public Alpaca mirror, discovered
after the GPU time. Passing both means neither path can lose it, and main's
regression test for that incident keeps passing.

330 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 21:20:14 +01:00
Mervin Praison 1eb8d58f90 Merge pull request #4360 from MervinPraison/feat/train-remote-cli
feat(train): expose remote SSH training, which had no call sites
2026-08-25 21:17:31 +01:00
MervinPraisonandClaude Opus 5 5fa3bcd79a feat(train): expose remote SSH training over SSH
Rebuilt on current main. The previous branch was cut from its predecessor
rather than from main, so it carried three unrelated commits and conflicted
once those merged. This is the change on its own.

praisonai_train/remote/runner.py was never in the repo. It was untracked —
present in a working tree, absent from git — so the CLI wired to a module that
did not exist and every command raised ModuleNotFoundError. That is what the 7
CI failures were, and the runner here is the one the triage agent wrote to fix
it: stdlib only, no new dependency for a feature most users never touch.

The tests did not catch it because every one of them replaces `_runner` with a
fake. A fake is the right tool for the CLI's behaviour and the wrong tool for
"does the thing I am faking exist" — so there are now three tests that import
the real module, check it has the six methods the CLI calls, and assert it
pulls in nothing outside the standard library. Removing the module fails ten
tests where it used to pass fourteen.

    praisonai-train remote preflight gpubox
    praisonai-train remote start gpubox -c config.yaml -d data/train.jsonl
    praisonai-train remote tail gpubox run-1787...
    praisonai-train remote fetch gpubox run-1787... lora_model -o ./out
    praisonai-train remote stop gpubox run-1787...

start prints the run id and the stop command before it streams, so a Ctrl-C
during --follow does not leave a job running with no handle. A preflight
failure and a refused start both exit non-zero. stop reports when nothing was
running rather than claiming success.

319 tests passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 21:06:01 +01:00
Mervin Praison 413ff37565 Merge pull request #4359 from MervinPraison/fix/train-oom-message-and-readme
fix(train): explain an OOM, and fix two README commands that do not exist
2026-08-25 20:03:22 +01:00
Mervin Praison 76bccae745 Merge pull request #4355 from MervinPraison/claude/issue-4354-20260825-1715
refactor: extract shared prep/finalize helpers from workflows single-step runners
2026-08-25 20:02:20 +01:00
praisonai-triage-agent[bot]andMervin Praison ad4e859e81 fix(train): define-free logger warning and gate response masking to SFT
Two runtime bugs flagged in review:

- load_datasets() called logger.warning but the module never defines a
  logger, so any dataset with processing_func set raised NameError and
  aborted the run. Switched to print(), matching every other warning in
  this module.

- Response-only masking (train_on_responses_only) rewrites SFT label
  tensors; it is meaningless for DPO/ORPO/KTO, whose trainers build labels
  from chosen/rejected pairs. The decision ran before method was read and
  applied unconditionally, so an enabled masking setting could corrupt a
  preference run. Gated the whole masking block on method == "sft".

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 18:45:21 +00:00
MervinPraisonandClaude Opus 5 d35e05883e fix(train): explain an OOM, and fix two README commands that do not exist
Two things the tool told the user that were not true.

OOM. torch.cuda.OutOfMemoryError subclasses RuntimeError, so the CLI's
catch-all flattened it into one ERROR line carrying torch's raw allocator dump.
It is the most common fine-tuning failure and the one where a first-time user
has no idea which number to change. The handler now classifies it and prints an
ordered remedy — sequence length first, smaller model last — matching the
ordering unsloth validated in its own backend
(studio/backend/core/training/worker.py:4834-4845). A test asserts every knob
the remedy names is actually accepted by the config, so the advice cannot drift
from the schema.

README. Two quickstart commands did not exist as written:

  praisonai-train data from-trials ...   # there is no `data` sub-app
  praisonai-train llm --dataset ...      # dataset is positional

The second is the exact shape of an incident this code already carries a
comment about (cli/commands/train.py:65-68): a dataset passed the wrong way was
silently dropped and the run trained on the default corpus, discovered only
after the GPU time was spent. A reader who "fixes" --dataset by dropping the
flag walks straight into it.

A test now parses every `praisonai-train <sub>` in the README and asserts it is
a registered command, so the docs cannot drift from the CLI again.

While writing the tests I deleted a branch I had just added: a separate
type(exc).__name__ == "OutOfMemoryError" check was redundant with the
"outofmemoryerror" text marker, since the class name is folded into the
searched text. No mutation could distinguish it from its absence, which is the
definition of code a test cannot cover.

12 tests. Mutations killed: the remedy never printed, the marker dropped, and
each README command reverted. Full suite 265 passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 18:44:28 +00:00
praisonai-triage-agent[bot] dddd3c11c4 Merge pull request #4358 from MervinPraison/fix/train-response-masking
fix(train): actually mask prompts out of the loss
2026-08-25 18:43:08 +00:00
praisonai-triage-agent[bot] f97fe47724 Merge pull request #4356 from MervinPraison/feat/train-preference-tuning
feat(train): support DPO, ORPO and KTO, not just SFT
2026-08-25 18:41:56 +00:00
praisonai-triage-agent[bot] 38bf625efd Merge pull request #4357 from MervinPraison/fix/train-hub-private-by-default
fix(train): Hub pushes are private by default
2026-08-25 18:38:56 +00:00
praisonai-triage-agent[bot]andMervin Praison 2be4685f1b fix(train): let auto masking reach the turn-marker fallback
In `assistant_only_loss: auto` (the documented default), use_mask was keyed
off TRL's `_supports_assistant_mask()` alone. Every unsloth template lacks
`{% generation %}`, so that returned False, use_mask became False, and
decide_masking short-circuited to full-sequence loss before the turn-marker
route was ever considered -- the exact defect this PR set out to fix. Auto now
enables masking when EITHER route is usable and lets decide_masking pick.

Adds regression tests covering the auto-normalization coupled with route
selection, the gap that let the bug survive.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 18:03:37 +00:00
praisonai-triage-agent[bot]andMervin Praison c645f6b96d fix(train): translate vision Hub errors, coerce string hf_private, normalize legacy quant list
Resolves three P1 issues from review on PR #4357:

- Vision pushes (train_vision.py, upload_vision.py) now wrap push_to_hub
  in the shared hf_push_errors context manager, so a 401/403 becomes the
  same actionable advice the LLM path gives instead of a raw traceback.
- hub_push_kwargs default coercion is now string-aware: vision callers
  that omit `flag` read hf_private: "false"/"0"/"no" as public instead of
  bool("false") == True keeping the repo private against the user's wish.
- Legacy single-element quantization_method lists are normalized back into
  config in both validate_config and for_export, so the GGUF exporter
  (which re-reads self.config) receives the method string, not a list.
- train_vision.save_model_merged now uses the guarded clean_local_repo_dir
  instead of an unguarded rmtree that could delete a namespaced repo path.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 18:01:33 +00:00
MervinPraisonandClaude Opus 5 a203f7d69d fix(train): actually mask prompts out of the loss
Every instruction-tuning run was training on the prompt as well as the answer.

`assistant_only_loss` is TRL's mask and it needs `{% generation %}` in the chat
template. None of unsloth's 43 templates contain it — grep -c "generation %}"
unsloth/chat_templates.py returns 0. So `assistant_only_loss: auto`, the
documented default, resolved to False for every template a user can actually
select. There was no error; the run summary printed "Loss mask: full sequence"
as though that were the intended outcome.

Unsloth's own answer is train_on_responses_only(trainer, instruction_part,
response_part) (chat_templates.py:58-87), which masks by locating literal turn
markers and works on any template. It is applied to the built trainer rather
than through the config, because it rewrites label tensors on the tokenised
dataset.

That needs the two markers, which vary by family, so this adds a table for
llama-3, chatml, gemma, mistral, phi and zephyr, and a resolver that falls back
to the model name when no chat_template is set. Unknown models resolve to None
rather than a guess: masking on the wrong markers finds no boundary and trains
on nothing, which is worse than not masking.

Two things worth review:

- Writing the resolver's tests found a real bug in it. Every unsloth model is
  "unsloth/<name>", and "unsloth" is itself a template key, so scanning the
  full repo id sent every one of them to the chatml markers — including Gemma
  and Llama models whose markers are different. It now matches the repo name,
  not the org.
- The summary line now names which route masked. "assistant replies only" hid
  the distinction between two mechanisms with very different coverage, and the
  unmasked case now says what it costs: "full sequence (training on prompts
  too)".

decide_masking() is separate from train_model so the routing can be tested
directly. The first version of that test read train_model's source for the
string "train_on_responses_only" and passed with the calling branch disabled —
the same shape of weak test that let the original defect survive.

18 tests. Five mutations, four killed: removing the fallback route, matching
the org prefix again, making two markers identical, and forcing masking on.
The survivor is collapsing the two routes into one truthiness check, which is
covered by the label test rather than the router. Full suite 253 passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 18:57:43 +01:00
praisonai-triage-agent[bot]andMervin Praison 25e2646cdb fix(train): let preference datasets reach their trainer; fix undefined logger
process_dataset applied the SFT formatter unconditionally, collapsing every row
to a single "text" column before the preference column check ran -- so a
correctly shaped DPO/ORPO/KTO dataset lost the very columns its trainer needs and
was rejected before trainer construction, making all three new methods
unreachable. Skip SFT text-formatting for preference methods so prompt/chosen/
rejected (and prompt/completion/label) survive intact.

Also fixes an undefined `logger` in load_datasets that raised NameError whenever
dataset.processing_func was set; switched to print to match the module.

Adds regression tests covering both the preference bypass and the unchanged SFT
path.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 17:56:02 +00:00
MervinPraisonandClaude Opus 5 5893e59116 fix(train): Hub pushes are private by default
Every model this tool uploaded became a PUBLIC repository. `private` was never
passed to push_to_hub_merged or push_to_hub_gguf in any of the three places
that push — train/llm/trainer.py, train_vision.py and upload_vision.py. Unsloth
accepts the flag (save.py:2654, create_huggingface_repo(private=...)); nothing
here forwarded it. A model fine-tuned on private data was published the moment
training finished, silently.

The default is now private. Publishing is one click in the Hub UI; unpublishing
something that has already been crawled is not. Set hf_private: false to
publish.

The same duplication hid two more defects, both in upload_vision.py, both
already solved in its LLM twin:

- No error handling at all, so a rejected token surfaced as a raw
  HfHubHTTPError traceback. The LLM copy has translated 401 and 403 into
  actionable messages for a while.
- An unguarded rmtree: it deleted config["hf_model_name"] whenever a directory
  of that name existed, with no check that the value was a local path rather
  than a Hub repo id. A bare name like "mymodel" deleted ./mymodel.

All three now share praisonai_train/_hub.py, so the next fix lands in one place
instead of one of three. save_method, commit_message and tags are forwarded
too — previously save_method was hardcoded to merged_16bit at all three sites.

Found by an agent diffing unsloth's save.py against these files.

14 tests. The privacy default is asserted directly, the YAML "false" string is
covered (without the trainer's coercion it is truthy and would keep the repo
private against the user's wish), and a test asserts no push site still uses a
bare token as its only kwarg — the signature of the original defect.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 18:53:53 +01:00
MervinPraisonandClaude Opus 5 c4a938b3a0 fix(train): stop flattening preference datasets before the trainer reads them
The DPO/ORPO/KTO support in the previous commit could never have worked on a
real corpus, and the tests did not catch it because they exercised the column
check in isolation rather than through process_dataset.

process_dataset ends with

    dataset.map(format_func, remove_columns=dataset.column_names)

which collapses every row to a single `text` column for SFT. Applied to a
preference dataset that destroys prompt/chosen/rejected before the trainer ever
sees them, so every DPO run would have died with

    method 'dpo' needs the column(s) ['prompt', 'chosen', 'rejected']
    and the dataset has ['text']

— an error message that blames the user's file for something this code did to
it moments earlier.

Non-SFT methods now return the dataset with its columns intact, after the
shuffle that the SFT path would also have applied. Found by an agent reading
unsloth's rl_replacements.py against this file; the preference trainers there
read those columns by name (rl_replacements.py:163-201, :850-931).

Two tests, both asserting order rather than outcome: the method guard must
appear before the flattening call and must return rather than warn, and the SFT
path must still flatten. Removing the guard fails the first.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 18:53:17 +01:00
MervinPraisonandClaude Opus 5 d0c960f8e4 feat(train): support DPO, ORPO and KTO, not just SFT
Unsloth patches TRL's DPO, ORPO and KTO trainers alongside SFT — they are
named in the list of trainers it rewrites at unsloth/__init__.py:1438-1443.
praisonai-train could only ever construct an SFTTrainer, so a dataset of
chosen/rejected pairs had nowhere to go and the whole preference-tuning half
of the backing library was unreachable.

  method: dpo        # or orpo, kto; sft remains the default
  beta: 0.1
  max_prompt_length: 1024

TRAINING_METHODS is a registry: each entry names the TRL trainer and config
class, the columns the dataset must provide, and whether the method needs a
frozen reference model. Adding a method is one entry.

Three things worth calling out:

- ORPO declares needs_ref_model: False. It folds the reference into its loss,
  so holding a second frozen model would double VRAM for nothing.
- DPO and KTO pass ref_model=None, which means "use the frozen base weights".
  With a PEFT adapter that is exactly right and avoids a second full model.
- The dataset's columns are checked before the run starts. TRL's own failure
  for a wrongly-shaped preference dataset surfaces inside the collator —
  minutes in, after the model is loaded and quantised, and phrased in terms
  of tensors rather than the file the user pointed at.

The TRL imports are individual and lazy, so a TRL too old to provide one
trainer can still run the others rather than failing at import; a method whose
classes are absent raises a message naming what to upgrade.

resolve_method() is separate from validate_config() so the method surface can
be tested without a CUDA import.

19 new tests. Five mutations, all killed: accepting an unknown method, dropping
case normalisation, disabling the column check, giving ORPO a reference model,
and having validate_config skip the check. Full suite 233 passing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 18:48:21 +01:00
praisonai-triage-agent[bot] 309a8bcc7d Merge pull request #4353 from MervinPraison/claude/issue-4352-20260825-1625
fix: remove dead code standardise/enhanced_templates.py
2026-08-25 17:21:54 +00:00
praisonai-triage-agent[bot]andMervinPraison 9d5ca574f8 refactor: extract shared prep/finalize helpers from workflows single-step runners (fixes #4354)
Extract byte-identical prep and finalize logic from the sync
_execute_single_step and async _aexecute_single_step into three private
helpers (_prepare_step_context, _build_step_action, _finalize_step_result),
so both variants share a single source of truth. Genuine async seams
(handler-coroutine await, achat executor branch, retry-loop await) remain
per-variant. Pure behaviour-preserving extraction; no public API change.

Co-authored-by: MervinPraison <[email protected]>
2026-08-25 17:17:59 +00:00
praisonai-triage-agent[bot]andMervinPraison c4360358fb fix: remove dead code standardise/enhanced_templates.py (fixes #4352)
Delete unreferenced 680-line EnhancedTemplateGenerator module that is
never imported, wired into the CLI, or exported. The canonical, wired
generator remains standardise/templates.py::TemplateGenerator.

Co-authored-by: MervinPraison <[email protected]>
2026-08-25 16:25:43 +00:00
praisonai-triage-agent[bot] 4ed3c7d40f Merge pull request #4351 from MervinPraison/claude/issue-4350-20260825-1334
fix: test memory presets via resolver, not live Agent construction
2026-08-25 14:26:38 +00:00
praisonai-triage-agent[bot]andMervin Praison db454b0c8a test: add docstrings to memory backend test helpers
Satisfies docstring coverage gate (60% -> 100%) flagged by CodeRabbit.
No behavioural change; all 4 tests still pass.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 13:40:33 +00:00
praisonai-triage-agent[bot]andMervinPraison f6e2349b31 fix: test memory presets via resolver, not live Agent construction (fixes #4350)
The regression test for #4229 eagerly constructed Agent(memory=preset) for
every advertised preset, which initialised live memory adapters (Mem0/MongoDB)
and failed CI without API keys or running services. Assert resolver correctness
instead, matching resolver-only test patterns elsewhere.

Co-authored-by: MervinPraison <[email protected]>
2026-08-25 13:35:56 +00:00
Mervin Praison bb04951886 Merge pull request #4345 from MervinPraison/fix/gateway-scope-lifecycle-methods
fix(gateway): classify the four unscoped WebSocket lifecycle methods
2026-08-25 14:23:43 +01:00
Mervin Praison 426886d774 Merge pull request #4338 from MervinPraison/claude/issue-4337-20260825-0852
fix: native project-instruction hierarchy & rules for standalone praisonai-code
2026-08-25 14:23:13 +01:00
Mervin Praison 160dc9a615 Merge pull request #4336 from MervinPraison/claude/issue-4335-20260825-0822
fix: SSE progress fan-out, job admission backpressure, async blueprint parity
2026-08-25 14:22:34 +01:00
Mervin Praison 0dbbf05e57 Merge pull request #4334 from MervinPraison/claude/issue-4333-20260825-0711
fix: honor Task skip_on_failure/on_error, wire model-call hooks, ingest Knowledge URLs
2026-08-25 14:22:05 +01:00
Mervin Praison 552a421553 Merge pull request #4347 from MervinPraison/fix/verbose-agents-callers
fix(agents): repair internal callers passing verbose= to Agent()
2026-08-25 13:34:00 +01:00
praisonai-triage-agent[bot] 76c464097d Merge pull request #4348 from MervinPraison/fix/verbose-yaml-scheduler
fix(scheduler): stop passing verbose= to Agent() from the YAML loader
2026-08-25 11:43:47 +00:00
Mervin Praison c54b391553 Merge pull request #4349 from MervinPraison/feat/praisonai-desktop
feat(desktop): PraisonAI Mac desktop app
2026-08-25 12:12:42 +01:00
praisonai-triage-agent[bot] 8afa73ae09 Merge pull request #4343 from MervinPraison/claude/issue-4341-20260825-0942
fix: enter resolved route profile scope to namespace tenant memory
2026-08-25 11:07:24 +00:00
praisonai-triage-agent[bot]andMervin Praison 5c8027a08e fix(desktop): resolve approval requests in UI and clear stale provider env
- ui/index.html: handle approval_request SSE events with an inline
  Allow/Always/Deny card that POSTs /approve/{id}. Without this, the default
  approval_mode:ask stalled every gated file/URL tool for the full 300s
  timeout because the shipped UI never resolved the decision.
- engine/server.py: _apply_env now removes OPENAI_API_KEY / OPENAI_API_BASE
  when a value is cleared, instead of skipping it — clearing a key or base_url
  no longer leaves the deleted credential/endpoint active for later turns.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 11:05:54 +00:00
MervinPraisonandClaude Opus 5 f2d38c1c57 feat(desktop): PraisonAI Mac desktop app
A Tauri shell supervising a Python engine on loopback. The streaming path is
kept off the IPC bridge: the webview reads SSE from 127.0.0.1 directly, which
is what jan, unsloth studio, ollama and hermes all independently converged on.

src-tauri/  Rust core, pure and I/O-injected so every decision is testable
            without a Mac, a certificate, a network or an installed Python:
              venv_resolve   one function owns both interpreter and
                             site-packages, catching the silent case where
                             3.13.2 and 3.13.7 share a path and nothing raises
              verify         half-installed is Stale, not Ready
              health         a server answering failures with HTTP 200 cannot
                             be trusted by status code alone
              readiness      log phrasing is upstream and versioned, so it is
                             a tested pure function
              adopt          five gates before reusing an orphan; a recycled
                             PID is never killed
              lockfile       absent and corrupt get different answers
              port_announce  an announced port is a claim until probed

engine/     stdlib-only HTTP + SSE: streaming, cancellation, sessions,
            persistence, tool approval carrying call_id, attachments, search,
            settings, MCP config, fork, projects, keychain-backed secrets

ui/         single-file frontend. Named ui/ rather than dist/ because it is
            hand-authored source, not build output -- and the repo's .gitignore
            excludes dist, which would have silently dropped the entire
            interface from the commit.

Tests: 62 Rust, 8 pacing, 11 DOM click tests dispatching real MouseEvents.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 11:58:28 +01:00
praisonai-triage-agent[bot]andMervin Praison 78794a7330 fix(agents): restore FileMemory fallback for non-ImportError backend init failures
The verbose-callers migration accidentally dropped the broad `except Exception`
branch (added in 2eacdcf79) that degrades an installed-but-uninitializable
memory backend to FileMemory. Without it, a registered backend that raises a
non-ImportError during init (mem0 missing credentials, MongoDB unreachable)
hard-crashes Agent construction. Restored to honour the fallback contract.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 10:55:52 +00:00
Mervin Praison ebd5aecbfb Merge pull request #4346 from MervinPraison/fix/streaming-with-tools-yields-nothing
fix(streaming): request the answer after tools run
2026-08-25 11:53:53 +01:00
MervinPraison e91a72f9a0 docs: auto-update documentation parity trackers [skip ci] 2026-08-25 10:53:23 +00:00
Mervin Praison a8d3130df7 Merge pull request #4342 from MervinPraison/fix/agui-adapter-drops-events
fix(agui): stop silently dropping 13 of 16 stream event types
2026-08-25 11:52:43 +01:00
MervinPraisonandClaude Opus 5 eaa5811b88 fix(scheduler): stop passing verbose= to Agent() from the YAML loader
yaml_loader.py documents verbose: true in its example agents.yaml, reads it,
and passes it to Agent() unconditionally with a default of False. Agent has no
verbose parameter, so every scheduled YAML agent raises TypeError on
construction -- whether or not the key appears in the YAML.

Verbosity now goes through output=, which is where it was consolidated.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 11:49:30 +01:00
MervinPraisonandClaude Opus 5 fe70396090 fix(agents): repair internal callers passing verbose= to Agent()
Verbosity was consolidated into output=, but two shipped call sites inside
praisonaiagents were left behind and raise TypeError on construction:

  compaction/memory_flush.py  the pre-compaction agent could never be built
  tools/delegation_tools.py   every delegate_task call

Agent's rejection of unknown kwargs is deliberate and is left intact. What was
missing was guidance: verbose is accepted by six sibling classes, and Agent has
no verbosity parameter among its 41, so the answer is not discoverable from the
signature. The error now names output= as the destination.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 11:49:30 +01:00
praisonai-triage-agent[bot]andMervin Praison ccbadea720 fix: make profile scope turn-local to prevent cross-tenant memory leak
The shared BotSessionManager._profile_namespace field was rewritten by every
routing handler, so overlapping turns for different tenants could recompute a
storage key from another tenant's profile after an await point — reading or
persisting one tenant's transcript into another's namespace (flagged by
CodeRabbit and Greptile).

Bind the resolved profile turn-locally in a per-task ContextVar inside chat()
(consumed from per-agent/instance staging and reset in finally), and propagate
that context into the load/save executor threads. Concurrent routed turns now
keep isolated namespaces and an unscoped route always fails closed.

Adds an overlapping-turns regression test.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 10:21:38 +00:00
praisonai-triage-agent[bot]andMervinPraison feb6b4d110 fix: enter resolved route profile scope to namespace tenant memory (fixes #4341)
The gateway resolved RouteMatch.profile but never consumed it, so per-route
tenant isolation was inert and failed open — every route on a process shared
one memory store. Wire the resolved profile through the routing handlers onto
the session's storage key so memory/session state is namespaced per tenant,
and stays unscoped (fail-closed) when a route names no profile.

Co-authored-by: MervinPraison <[email protected]>
2026-08-25 10:20:53 +00:00
MervinPraison e770b443d6 docs: auto-update feature parity trackers [skip ci] 2026-08-25 10:19:01 +00:00
praisonai-triage-agent[bot] d861cf4976 Merge pull request #4340 from MervinPraison/claude/issue-4339-20260825-0913
fix: make gateway state durable by default and report durability degradation
2026-08-25 10:18:34 +00:00
praisonai-triage-agent[bot]andMervin Praison 3a224e2529 test(gateway): resolve enum-derived branches in scope lockstep
The lockstep extraction only recognised double-quoted literal msg_type
comparisons, so a dispatcher branch written enum-only (as ping/pong already
are, e.g. `msg_type == EventType.X.value`) would drift past it — the exact
silent registry/dispatcher divergence the test exists to catch.

Resolve every `EventType.<NAME>.value` operand through the enum to its wire
value, treat operands within one `in (...)` branch as aliases for a single
dispatch path (so the abort/MESSAGE_ABORT branch stays one method, not a
spurious `message_abort`), and add a positive control asserting the
enum-derived branch resolution still works.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 09:55:31 +00:00
praisonai-triage-agent[bot]andMervin Praison e1484bde74 fix(agui): keep tool-call id stable across id-less argument deltas
Providers send a tool-call id once and omit it on later argument chunks.
The DELTA_TOOL_CALL/TOOL_CALL_END branches minted a fresh UUID per chunk,
orphaning arguments from the invocation the client assembles. Track the
active tool call in EventBuffer and fall back to it. Also drop the
truthiness guard on these branches so a TOOL_CALL_END with an empty
tool_call payload closes the call instead of raising.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 09:54:48 +00:00
praisonai-triage-agent[bot]andMervin Praison c346751e8e fix(streaming): preserve request context and event telemetry after tools
Reuse the original request messages (system prompt, output constraints,
memory context) for the post-tool follow-up instead of chat_history alone,
which dropped the system turn. Bracket the follow-up answer with
REQUEST_START/LAST_TOKEN/STREAM_END so consumers see telemetry for the
answer, not just the tool-call round. Strengthen the regression test to
assert the sentinel flows through the streamed answer, proving the tool
branch ran.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 09:54:20 +00:00
MervinPraisonandClaude Opus 5 06dfc8e59d fix(streaming): request the answer after tools run
The streaming path collected tool calls, executed them, appended the results
to chat history -- and then ended. It never asked the model for the final
answer, so start(stream=True) yielded nothing while the identical
non-streaming call answered normally.

There was no error. The caller received an empty generator and could not
distinguish 'the model had nothing to say' from 'the answer was never
requested'.

The follow-up completion carries no tools, so the model answers from the
results it already has rather than calling another tool, which also bounds
the turn to a single round.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 10:48:41 +01:00
MervinPraisonandClaude Opus 5 f40516a3cb fix(gateway): classify the four unscoped WebSocket lifecycle methods
resolve_required_scope() fails closed to ADMIN for anything it does not know,
which is the right default -- but four of the six message types the dispatcher
handles were unregistered, so switching the resolver on would have demanded
ADMIN for the connection handshake and locked out every client.

hello, join and leave are READ; abort is WRITE, since aborting a turn mutates
it exactly as sending one does.

Adds a lockstep test pinning the dispatcher's message types against the
registry, in the style that catches a runtime-only permission failure no
behavioural test can see.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 10:48:13 +01:00
MervinPraisonandClaude Opus 5 7f0e1c28c3 fix(agui): stop silently dropping 13 of 16 stream event types
The AG-UI adapter handled DELTA_TEXT, TOOL_CALL_START and TOOL_CALL_RESULT
and ended with a bare `return []`. StreamEventType has sixteen members, so
thirteen were discarded with no error and no log.

ERROR, RETRY, MODEL_FALLBACK and STREAM_UNAVAILABLE were among them: a
mid-stream provider failure produced a run that ended cleanly having never
told the client anything went wrong.

Every type now has an explicit disposition -- mapped, or listed in
NOT_WIRE_VISIBLE -- and an undeclared type raises rather than vanishing, so
adding a seventeenth member fails a test instead of dropping a frame in
production.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-25 10:47:00 +01:00
praisonai-triage-agent[bot]andMervin Praison 2eacdcf794 fix: gracefully fall back to FileMemory when a string memory backend fails to initialise
CI test-core (subdirs) failed on test_every_advertised_preset_resolves with
"Mem0 API Key not provided" because the mem0 package is installed in that job
but no MEM0_API_KEY is set. Agent(memory="mem0") already promised a graceful
"Falling back to FileMemory" for missing optional dependencies (ImportError),
but an installed-yet-uninitialisable SaaS backend raised ValueError, which was
uncaught and crashed construction. Broaden the string-preset branch to honour
the same fallback contract for runtime init failures.

Pre-existing, unrelated to PR #4340 (gateway durability); no Agent params added.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 09:34:46 +00:00
praisonai-triage-agent[bot]andMervin Praison a84c6ace9f fix: clear stale idempotency degradation on recovery and redact reasons (#4339)
Address two valid review findings on the durability-degradation surface:

- Recovery clears degradation: a successful durable SqliteIdempotencyStore
  build now clears any prior `durability:idempotency` degraded fact, so a
  same-process rebuild (config hot-reload) that restores durability no longer
  leaves health()/`gateway status` permanently reporting non-durable operation.
  Mirrors the existing session-store recovery-clear path.

- Redacted operator-facing reasons: the idempotency and session fallback paths
  no longer interpolate the raw store-init exception (which can embed backend
  filesystem paths) into the degraded-owner `reason`. The raw exception stays in
  the log only; the reported reason is an operator-safe, redacted string —
  matching the documented contract on record_durability_degraded.

Tests: add recovery-clears-degradation test and a redaction assertion.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 09:29:03 +00:00
praisonai-triage-agent[bot]andMervinPraison 331df584cb fix: make gateway state durable by default and report durability degradation (fixes #4339)
Gateway durable state was opt-in and lost silently. Now:
- build_idempotency_store is durable by default (SQLite when a state dir is
  available); "memory" stays an explicit opt-in for tests/ephemeral runs.
- Gateway server default idempotency backend is durable when none configured.
- Session/idempotency store-init failures record a durability-degraded fact
  (owner_kind="gateway", owner_id="durability:<component>") instead of a silent
  logger.warning, reusing the existing DegradedCapabilityRegistry — no new core
  protocol or Agent params.
- health() merges durability facts; `gateway status` prints a Degraded: section
  with a redacted reason and a `fix:` hint so operators see non-durable runs.

Co-authored-by: MervinPraison <[email protected]>
2026-08-25 09:20:12 +00:00
praisonai-triage-agent[bot]andMervin Praison e78b151d01 fix: harden standalone rules clear scope & remote-fetch SSRF (DNS rebinding)
- rules clear: constrain deletion to workspace-scoped CLI-created rules; no
  longer unlinks discovered AGENTS.md/CLAUDE.md/PRAISON.md, global, git-root or
  nested subdir rules (Greptile P1).
- context_files remote fetch: pin each connection to a validated public IP and
  re-validate at connect time, closing the DNS-rebinding TOCTOU window while
  preserving Host/SNI; _allow_local_urls opt-in bypasses pinning (Greptile P1).
- tests: add regressions for clear-scope preservation and rebind refusal.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 09:08:09 +00:00
praisonai-triage-agent[bot]andMervinPraison 448b7bb912 fix: make project-instruction hierarchy and rules command native to standalone praisonai-code (fixes #4337)
On a standalone `pip install praisonai-code` the subtree instruction hook,
`--instructions` resolution, and the `rules` command silently degraded because
their implementations were reachable only via the optional `praisonai_bot` /
`praisonai` wrapper packages.

- Add a resident, dependency-free `praisonai_code/integration/context_files.py`
  (stdlib + `praisonaiagents.hooks` only) so subtree AGENTS.md loading and
  `--instructions` glob/URL resolution work without the optional packages.
- Wire `run.py::_wire_subtree_context_hook` to fall back to the resident helper
  after the optional bot/wrapper copies, removing the silent no-op.
- Give `rules list/add/clear` a native implementation backed by core
  `praisonaiagents.memory.RulesManager`, delegating to the wrapper bridge only
  when core is genuinely unavailable.
- Add standalone regression tests that block the optional packages.

Co-authored-by: MervinPraison <[email protected]>
2026-08-25 08:59:51 +00:00
praisonai-triage-agent[bot]andMervin Praison f354b977f2 fix: atomic job admission + no stranded jobs on 503 backpressure
Address two P1 races flagged by Greptile on PR #4336:

- executor.submit: reserve the admission slot synchronously (create+register
  task before any await) so concurrent submits can no longer slip past the
  max_queued ceiling. The QUEUED persist moves to the head of _execute_job
  (before the concurrency semaphore), preserving the store-reflects-QUEUED
  contract without a check-then-await race.
- router.submit_job: on JobQueueFull after save_if_absent persisted the job,
  delete the stranded record so its idempotency key is freed and status/SSE
  don't hang while /result returns 409. Cleanup is best-effort and never masks
  the 503.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 08:35:19 +00:00
praisonai-triage-agent[bot]andMervinPraison bf0f2fb213 fix: SSE progress fan-out, job admission backpressure, async blueprint parity (fixes #4335)
Gap 1: JobExecutor now keeps a list of progress callbacks per job and
fans out to all of them, so concurrent SSE viewers of the same run each
receive updates; unregister removes only the given callback.

Gap 2: JobExecutor.submit rejects with JobQueueFull once max_queued
(default max_concurrent*10) is reached; the router maps this to a 503 +
Retry-After so floods get honest backpressure instead of unbounded task
growth.

Gap 3: AsyncAgentScheduler gains from_blueprint via a shared
build_from_blueprint helper in _base_scheduler; blueprint resolution now
lives in one place for both sync and async schedulers, and the async
variant dispatches via astart so it never blocks the event loop.

Co-authored-by: MervinPraison <[email protected]>
2026-08-25 08:28:03 +00:00
praisonai-triage-agent[bot]andMervin Praison 3568a40013 fix: thread model-hook request mutations, route async model calls through middleware, preserve after_model output, capture MongoDB URL ids
- chat_mixin: before_model/wrap_model_call mutations to temperature/tools now
  flow into the retry core (were silently ignored)
- chat_mixin: after_model mutations on non-string responses are preserved via
  _unwrap_model_response (previously raw object bypassed hook edits)
- chat_mixin: achat() now routes through the model middleware manager (was
  bypassing before/after/wrap_model_call); sync chain bridged via executor
  without nesting event loops, retry core extracted to
  _achat_completion_with_retry_core
- knowledge: URL ingestion now captures bare string document ids (MongoDB)

Addresses CodeRabbit/Greptile review findings on PR #4334.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 07:33:01 +00:00
praisonai-triage-agent[bot]andMervin Praison 3374d179b3 fix: honor Task skip_on_failure/on_error, wire model-call hooks, ingest Knowledge URLs (fixes #4333)
- Task(skip_on_failure=True)/on_error="continue" now let a dependent task
  run in degraded mode instead of being force-failed when an upstream
  dependency fails, across every cascade path (agents async/sequential and
  process sequential/asequential).
- Agent(hooks=[before_model/after_model/wrap_model_call]) now fire: model
  calls route through MiddlewareManager.execute_model_call via a new
  _get_model_middleware_manager, mirroring the existing tool-hook wiring.
- Knowledge.add() now ingests URL inputs (as its docstring documents) via
  MarkItDown extraction instead of raising NotImplementedError.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-25 07:18:31 +00:00
Mervin Praison 689978100b Merge pull request #4301 from MervinPraison/claude/issue-4300-20260824-1020
fix: thread cancel_token into OpenAI-native tool loop so /stop halts mid-run
2026-08-24 21:02:08 +01:00
Mervin Praison 6e3b1f1af8 Merge pull request #4292 from MervinPraison/claude/issue-4288-20260824-0917
fix: record resolver identity and per-request reviewer custody on gateway approvals
2026-08-24 21:01:34 +01:00
Mervin Praison 84c53a1e04 Merge pull request #4277 from KevinTran3011/security/codeql-research-findings
Security/codeql research findings
2026-08-24 21:00:42 +01:00
Mervin Praison d1f52e6625 Merge pull request #4274 from MervinPraison/claude/issue-4273-20260823-1712
fix: remove dead ServerRegistry class in agent.py
2026-08-24 20:59:32 +01:00
Mervin Praison ce81f0c835 Merge pull request #4272 from MervinPraison/claude/issue-4271-20260823-1615
fix: exclude persistence test package from installed wheel
2026-08-24 20:59:07 +01:00
Mervin Praison 2a3d947def Merge pull request #4248 from MervinPraison/claude/issue-4246-20260823-0711
fix: enforce sandbox resource limits, add sync chat() backend guards, dedupe Gemini tool constant
2026-08-24 20:58:48 +01:00
Mervin Praison e69cca85be Merge pull request #4244 from MervinPraison/claude/issue-4243-20260823-0522
fix: restore repeated session turns; close session-share secret leaks
2026-08-24 20:58:09 +01:00
Mervin Praison 8949dc0672 Merge pull request #4236 from MervinPraison/claude/issue-4229-20260823-0446
fix: derive generator memory backends from resolver; normalise runtime= (fixes #4229)
2026-08-24 20:57:34 +01:00
MervinPraison 248be5864b docs: auto-update documentation parity trackers [skip ci] 2026-08-24 19:55:56 +00:00
Mervin Praison cbe42d4ae7 Merge pull request #4234 from MervinPraison/claude/issue-4228-20260823-0445
fix: keep default denials when configuring approval + enforce argument-scoped deny rules
2026-08-24 20:55:15 +01:00
Mervin Praison 11a6f9706c Merge pull request #4276 from LinespottingPrivate/feat/gbr-pair
Add Build Remote Agent phone pairing (gbr/1)
2026-08-24 20:36:24 +01:00
Mervin Praison 1f0ccefa8a Merge pull request #1985 from EvoLinkAI/feat/evolink-provider
feat: add Evolink provider example
2026-08-24 20:35:35 +01:00
praisonai-triage-agent[bot] c34ca01dde Merge pull request #4331 from MervinPraison/claude/issue-4330-20260824-1619
fix: ConfigValidator calls non-existent ToolResolver.resolve_tool()
2026-08-24 17:21:07 +00:00
praisonai-triage-agent[bot]andMervin Praison f283bf2b3d fix: treat TOOL_MAPPINGS tools as known-optional in ConfigValidator
web_search and other tools declared in praisonaiagents' TOOL_MAPPINGS
resolve to None when their optional dependencies aren't installed. After
switching to ToolResolver.resolve(), these were incorrectly flagged as
'Unknown tool' errors, breaking test_agents_yaml_valid_fields_no_warnings.
Consult TOOL_MAPPINGS so such tools produce an install warning instead of
a hard error, while genuinely unknown tools still error.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 16:28:23 +00:00
praisonai-triage-agent[bot]andMervinPraison 4fa4e0cdcc fix: call ToolResolver.resolve() in ConfigValidator tool validation (fixes #4330)
ConfigValidator._validate_tools called a non-existent resolve_tool()
method on the ToolResolver instance, raising AttributeError that was
swallowed by the broad except, downgrading every tool reference to a
generic warning. Unknown/misspelled tools were never reported as errors.
Use the actual instance method resolve(), which returns None on miss.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 16:20:50 +00:00
praisonai-triage-agent[bot] 3fee78cf58 Merge pull request #4329 from MervinPraison/claude/issue-4327-20260824-1245
fix: route implemented legacy CLI verbs to handlers instead of billing them as LLM prompts
2026-08-24 14:02:51 +00:00
praisonai-triage-agent[bot] 8b56b13710 Merge pull request #4328 from MervinPraison/claude/issue-4326-20260824-1245
fix: route praisonai search to retrieval instead of billing as LLM prompt
2026-08-24 13:36:10 +00:00
praisonai-triage-agent[bot] 9c5d4a05d4 Merge pull request #4325 from MervinPraison/claude/issue-4322-20260824-1234
fix: stop praisonai/capabilities from fabricating success
2026-08-24 13:30:29 +00:00
praisonai-triage-agent[bot] 6fa3f9e3cd Merge pull request #4324 from MervinPraison/claude/issue-4323-20260824-1234
fix: pin resolved command name to registry key in praisonai --help
2026-08-24 13:23:59 +00:00
praisonai-triage-agent[bot]andMervin Praison a6d59a1939 fix: fail closed in legacy verb discovery so implemented verbs are never billed to an LLM
Address CodeRabbit review: _get_legacy_verbs() returned an empty set on
oracle import failure, which let implemented verbs (e.g. `thinking status`)
fall through to the bare-prompt rule and be forwarded to the modern `run`
engine as a paid LLM prompt — the exact #4327 regression the guard prevents.

Now fails closed via a static _LEGACY_VERBS_FALLBACK mirror, logs a warning,
and does not cache the fallback (so recovery is possible). Adds tests for the
fail-closed path, end-to-end routing under discovery failure, and a drift
assertion keeping the fallback in sync with the authoritative oracle.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 13:23:51 +00:00
praisonai-triage-agent[bot] 422cc5b888 Merge pull request #4321 from MervinPraison/claude/issue-4319-20260824-1205
fix: preserve blank lines and guard over-cap code fences in bot replies
2026-08-24 13:18:13 +00:00
praisonai-triage-agent[bot] e6dc27072b Merge pull request #4320 from MervinPraison/claude/issue-4318-20260824-1205
fix: silence-free bot command dispatch + honest /model reset
2026-08-24 13:08:23 +00:00
praisonai-triage-agent[bot]andMervinPraison 9ce7b8c0d4 fix: route implemented legacy CLI verbs to their handlers instead of billing them as LLM prompts (fixes #4327)
Since 82f55c97b the unified dispatcher treated any first token that is not a
registered Typer command as free text and forwarded it to the modern `run`
engine, billing 29 implemented legacy verbs (thinking, install, persistence,
audio, …) to /v1/responses at ~2.5k tokens with exit 0.

Extract the legacy dispatcher's authoritative verb list into a reusable
LEGACY_SPECIAL_COMMANDS constant and have the unified dispatcher consult that
same oracle: an implemented legacy verb routes to legacy (its handler) before
the bare-prompt/LLM path. containers and vector-stores are withheld pending
#4322 so routing does not promote a fabricated success.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 12:51:27 +00:00
praisonai-triage-agent[bot] 9b8ae4374e Merge pull request #4317 from MervinPraison/claude/issue-4314-20260824-1131
fix: route praisonai-train llm to the trainer instead of a chat prompt
2026-08-24 12:47:44 +00:00
praisonai-triage-agent[bot]andMervinPraison ace59ede5e fix: route praisonai search to retrieval instead of billing as LLM prompt (fixes #4326)
The `search` command was advertised in --help and registered as a real Typer
command, but was missing from get_command_names()'s routing oracle. This caused
`praisonai search "..."` to be misclassified as a bare prompt and billed as an
LLM call, contradicting its "no LLM generation" help text.

Adds `search` to the routed set and a parity regression test guaranteeing every
advertised command is also routed (guards the #2191 drift guarantee).

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 12:47:06 +00:00
praisonai-triage-agent[bot]andMervin Praison 665c33628f fix: map backends command to a help category
backends was added to _LAZY_COMMANDS but never mapped in COMMAND_CATEGORIES,
leaving test_every_registered_command_has_a_category failing and the command
without an explicit --help panel. Map it to Advanced.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 12:42:46 +00:00
praisonai-triage-agent[bot] f9b40f1ba2 Merge pull request #4316 from MervinPraison/claude/issue-4315-20260824-1132
fix: praisonai-train QC drops English trajectories; validate lacks zero-row guard
2026-08-24 12:41:06 +00:00
praisonai-triage-agent[bot]andMervinPraison 4888a0435c fix: stop capabilities from fabricating success; fail honestly (fixes #4322)
Vector store file ops now call the real litellm public API
(vector_store_file_create/list/delete) instead of the never-existent
create_vector_store_file names, and no longer swallow AttributeError into
fabricated success. Delete defaults to False, not True.

search() uses the shipped duckduckgo_tools.internet_search symbol (was the
nonexistent duckduckgo_search) and surfaces backend errors instead of a
permanent empty-but-successful result.

containers/container_files have no backend, so container_create and the
container file read/write/list functions now raise NotImplementedError
rather than inventing container IDs or returning empty results. CLI and MCP
adapters already wrap these in except Exception, so failures surface cleanly.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 12:39:28 +00:00
praisonai-triage-agent[bot]andMervinPraison 5c2d71306d fix: pin resolved command name to registry key in praisonai --help (fixes #4323)
get_command set rich_help_panel but never command.name. typer_get_command
yields "" for a Typer(help=...) declared without name=, and collapses a
single-command app to that command's own name — so --help rendered 77/98
commands with a blank name and 3 with a wrong (untypeable) name. Pin
command.name to the registry key (the true typeable name) in get_command.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 12:38:11 +00:00
praisonai-triage-agent[bot] f33e17b4d2 Merge pull request #4313 from MervinPraison/claude/issue-4309-20260824-1120
fix: forward browser run options on extension/cdp engines
2026-08-24 12:37:45 +00:00
praisonai-triage-agent[bot] 5977a366fe Merge pull request #4312 from MervinPraison/claude/issue-4310-20260824-1121
fix: CDP click/type/clear_input report selector-not-found as failure
2026-08-24 12:27:14 +00:00
praisonai-triage-agent[bot] 338609f875 Merge pull request #4308 from MervinPraison/claude/issue-4306-20260824-1112
fix: reject misspelled keys in deploy config validation
2026-08-24 12:21:33 +00:00
praisonai-triage-agent[bot] 887b81b804 Merge pull request #4311 from MervinPraison/claude/issue-4304-20260824-1111
fix: rebind broken MCP adapters to real core APIs
2026-08-24 12:20:09 +00:00
praisonai-triage-agent[bot]andMervin Praison f8ef5fd293 fix: guard mixed fence corruption and use UTF-16 cap for Telegram (#4319)
- _chunk.py: only re-wrap truly pure single-fence chunks in enforce_hard_cap;
  mixed fence+prose or multi-fence chunks are hard-split as plain text so prose
  is never rendered as code (Greptile P1).
- telegram.py: measure and chunk in the platform's declared UTF-16 unit so
  emoji-heavy replies cannot exceed Telegram's 4096 UTF-16 cap (Greptile P1).
- tests: mixed-fence non-corruption + UTF-16 length coverage.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 12:18:03 +00:00
praisonai-triage-agent[bot]andMervinPraison a725723900 fix: preserve blank lines and guard over-cap fences in bot replies (fixes #4319)
Part 1: split_media_from_output stripped every blank line from every
Telegram/Slack reply, destroying load-bearing separators, paragraph
breaks and clean chunk boundaries. Now only excess blank runs left by
removed MEDIA lines are collapsed; single blank-line separators survive.

Part 2: chunk_message emits an over-cap code fence by design and no
caller re-checked the platform cap, so a pure/oversized fence raised
"message too long" and lost the whole reply. New enforce_hard_cap()
splits any over-cap chunk (re-balancing ``` on split fences) and is
applied in every _send_long_message (telegram/slack/discord).

Fixed together so restoring blank lines cannot regress fence delivery.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 12:10:17 +00:00
praisonai-triage-agent[bot]andMervinPraison ea22399481 fix: silence-free bot command dispatch + honest /model reset (fixes #4318)
- Remove the dead /sethome registry entry: it was published to every
  adapter's menu but implemented on none, so drop the false advertisement.
- Telegram: on a slash-command miss, dispatch the raw text to the agent
  instead of ending silently, matching the Slack reference and the code's
  own docstring.
- Discord: remove the bare terminal return in the on_message command block
  so an unknown/unimplemented slash command falls through to agent dispatch
  (like Slack) rather than a silent no-op.
- _session.reset()/reset_all(): clear _model_overrides so /new actually
  remedies a bad or unavailable /model instead of every later turn failing
  silently at the provider.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 12:09:43 +00:00
praisonai-triage-agent[bot] ac4ef21b50 Merge pull request #4307 from MervinPraison/claude/issue-4305-20260824-1111
fix: thread Azure --subscription onto az commands (fixes #4305)
2026-08-24 12:08:30 +00:00
praisonai-triage-agent[bot] 9f96115aa1 Merge pull request #4303 from MervinPraison/claude/issue-4302-20260824-1105
fix: enforce SecurityPolicy in DockerSandbox (#4302)
2026-08-24 11:57:32 +00:00
praisonai-triage-agent[bot]andMervin Praison a914ea791a fix: honor explicit script_range/script_drop in export QC; correct README atomic-write note
Address final-review findings on #4316:
- _export_qc_cfg no longer force-zeroes script thresholds when a caller sets
  script_range/script_drop/script_flag, so a documented
  qc_cfg={"script_range": [...]} re-enables the target-script check for other
  languages instead of silently passing wrong-script rows. English default
  (thresholds=0) is unchanged when no script key is given.
- Add test for the script_range-only re-enable path (kept for Latin, dropped
  for Tamil).
- README: validate's atomic --out write is shared with generate/dedup, and
  document that any script key re-enables the export QC check.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 11:43:31 +00:00
praisonai-triage-agent[bot]andMervin Praison de2d330962 fix: chain typer.Exit from SystemExit in train_llm (B904)
Explicitly chain the translated typer.Exit from the caught SystemExit to
resolve Ruff B904 and preserve the original traceback.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 11:43:08 +00:00
praisonai-triage-agent[bot]andMervinPraison 928435c125 fix: route praisonai-train llm to the trainer instead of a chat prompt (fixes #4314)
The legacy dispatcher's `if args.command:` chain had no `train` case, so
`praisonai-train llm <dataset>` fell through to the bare-prompt handler and
forwarded the literal word "train" to an LLM as a chat prompt — the real
training block was never reached and no trainer subprocess launched.

- Add a `train` guard in main()'s command chain so it reaches the dedicated
  training block (which reads args.dataset/args.model and dispatches
  praisonai_train.train.llm.trainer).
- Propagate a non-zero SystemExit from the router in train_llm so missing-dep
  failures no longer exit 0 (CI gating on $? now sees them); clean exits still
  return 0.
- Add a regression test that drives the real router (asserts the trainer is
  dispatched and handle_direct_prompt is never called) plus exit-code tests.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 11:38:05 +00:00
praisonai-triage-agent[bot]andMervinPraison 362d5663ba fix: praisonai-train QC drops English trajectories; validate lacks zero-row guard (fixes #4315)
- from-trials --qc: default export QC config disables the Tamil script-purity
  check, since agent trajectories are English by construction (kept dedup,
  boilerplate/refusal, length, diversity). qc_cfg still re-enables it.
- Surface the actual QC drop reasons: ExportSummary gains skipped_qc/qc_drops so
  no row vanishes unaccounted, and from-trials names the reasons on the zero-row
  error instead of guessing.
- validate: reuse the in-module tempfile+os.replace helpers (mirrors dedup/
  generate) with a zero-kept guard, so an --out that aliases the input is never
  truncated and a zero-kept run exits non-zero leaving any existing file intact.
- Document the Tamil script_range default in README and validate --help.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 11:36:57 +00:00
praisonai-triage-agent[bot]andMervin Praison 0e838ac03b fix: honour per-run max_steps and warn on playwright/hybrid dropped options
- server.py: extension-engine termination now uses the per-session agent's
  max_steps (forwarded via start_session) instead of the server default, so
  `browser run --max-steps` actually bounds the run (Greptile P1).
- browser.py: playwright/hybrid engines now warn when CDP-only options
  (--vision/--screenshots/--record-video/--max-retries/--no-record) are set
  rather than silently discarding them; corrected the run --help "Features"
  block to state these require --engine cdp specifically (Greptile P1).

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 11:31:31 +00:00
praisonai-triage-agent[bot] a1cb2c4f2c chore: ignore knowledge.db test artifacts in praisonai-mcp 2026-08-24 11:28:44 +00:00
praisonai-triage-agent[bot] 7c9672ef24 chore: remove accidental knowledge.db test artifact 2026-08-24 11:28:27 +00:00
praisonai-triage-agent[bot]andMervin Praison da37b6fc0f fix: forward MCP adapter inputs (limit, task_id, realtime data) to core
- knowledge.query: forward `limit` to Knowledge.search (was dropped)
- a2a.send: forward `task_id` via kwargs (was discarded)
- realtime.send: map message to `data` with real event_type (was passed as event_type)
- add regression tests for the three semantic bindings

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 11:28:11 +00:00
praisonai-triage-agent[bot]andMervinPraison 5c43017956 fix: forward browser run options on extension/cdp engines (fixes #4309)
- Send max_steps in start_session wire frames on the extension path
  (server.py already reads it, so this was a pure missing-field fix)
- Pass record_video=record_video to run_cdp_only in _run_alternative_engine
  so --record-video on cdp actually records instead of only billing gpt-4o
- Warn when engine-only options (--vision/--screenshots/--record-video/
  --max-retries/--headless/--extension/--no-record) are set on the default
  extension engine, instead of silently discarding them
- Clarify --url is context-only on the extension path and show max steps
- Fix run --help "Features" caveat and add --engine to screenshots example

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 11:24:27 +00:00
praisonai-triage-agent[bot]andMervinPraison bfb1b28cd4 fix: CDP click/type/clear_input report selector-not-found as failure (fixes #4310)
A CDP click/type/clear_input on a selector matching nothing returned success
unconditionally, so run()'s retry loop broke on first success and --max-retries
plus all fallback-selector strategies never fired. The injected JS now returns
whether the element was found and a miss yields success=False with an error,
making the existing alternative-selector retry strategies reachable.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 11:23:18 +00:00
praisonai-triage-agent[bot]andMervin Praison 5695f056a0 fix: harden deploy unknown-key rejection against non-mapping/non-string keys
Coerce unknown deploy keys to str before sorted()/join() so a non-string
YAML key raises a clean ValueError instead of TypeError, and guard that
the deploy section is a mapping. Adds regression tests.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 11:22:22 +00:00
praisonai-triage-agent[bot]andMervin Praison 48e3a6b412 fix: fail fast on missing Azure subscription in status/destroy (#4305)
status() and destroy() previously fell back to the ambient az
subscription when subscription_id was unset, risking wrong-subscription
inspection/deletion. Both now return their structured failure result
before issuing any az command, mirroring deploy()'s guard.

Also fixes test_azure_provider_deploy_failure to supply a
subscription_id so it reaches subprocess.run, and adds no-subscription
tests for status() and destroy().

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 11:21:45 +00:00
praisonai-triage-agent[bot]andMervinPraison c827f386f9 fix: rebind broken MCP adapters to real core APIs (fixes #4304)
27 of 85 advertised praisonai-mcp tools could not succeed for any input:
their adapters called core/wrapper APIs that don't exist or have different
signatures, failing at attribute lookup or argument binding before any I/O.
24 returned isError:false and 9 reported an installed module as "not available".

Rebindings against real core:
- knowledge.*: query->search, list->get_all, stats->get_corpus_stats,
  clear->reset, add(source) (dropped nonexistent source_type)
- hooks.*: get_default_registry().list_hooks() (was get_hook_manager)
- session.*: get_default_session_store() (was SessionManager)
- tools.*: list_tools/get_tool (was get_available_tools/get_tool_info/search_tools)
- eval.*: AccuracyEvaluator/PerformanceEvaluator (was AccuracyEval/PerformanceEval)
- workflow.run/run_file: AgentsGenerator(..., config_list=[])
- workflow.auto: AutoGenerator(topic, pattern).generate()
- audio.transcribe(audio=), audio.speech(text=), guardrails.check(content=),
  ocr(document=), a2a.send(target_agent=), containers.file_read/write(path=),
  skills.load(skill_name=), realtime.send(event_type=)
- resources: memory/sessions -> session store, knowledge/sources -> get_all

_handle_tools_call now sets isError:true when a handler returns an
"Error: ..." string, matching the convention already used for adapters
that raise.

Tests bind against real core (not hand-written fakes), so a future core
rename the adapter doesn't track fails here.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 11:21:04 +00:00
praisonai-triage-agent[bot]andMervin Praison 8beb45d3ea fix: enforce allowed_paths and scan shell payloads in DockerSandbox
Bring DockerSandbox policy validation to parity with SubprocessSandbox
where it is semantically meaningful for a container:

- Enforce allowed_paths (opt-in) alongside blocked_paths, so a path
  outside configured roots is refused before dispatch.
- Route run_command through the shared build_argv helper so a shell=True
  payload is split by policy_scan_parts and its paths are scanned,
  instead of walking an opaque `sh -c "..."` argv.

Deliberately does NOT copy SubprocessSandbox's allow_subprocess shell
block: Docker always dispatches via `sh -c` inside the container, which
is the isolation boundary that clause approximates on the host. Applying
it would refuse every command and defeat the backend rather than harden
it.

Adds regression tests for allowed_paths (in/out) and shell-payload path
scanning. All host-side + Docker filesystem tests pass.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 11:19:01 +00:00
praisonai-triage-agent[bot]andMervinPraison cd112e2ee1 fix: reject misspelled keys in deploy config validation (fixes #4306)
A misspelled field/section key in agents.yaml was silently discarded by
pydantic's default extra='ignore', so `deploy validate` reported valid while
deploying with defaults (e.g. a typo in cluster_name deploying to a new ECS
cluster). Set extra='forbid' on the deploy input models, reject unknown
top-level deploy sections in schema validation, surface cluster_name in the
human-readable validate output, and make the docker `push: true` path reach the
existing "no registry" error instead of silently skipping the push.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 11:15:45 +00:00
praisonai-triage-agent[bot]andMervinPraison bea67527b9 fix: thread Azure --subscription onto az commands, fail fast, doctor mismatch (fixes #4305)
Azure Container Apps deployments read subscription_id only to echo it in
plan(); it was never passed to az, so every resource was created/destroyed in
the ambient default subscription. Mirror the GCP provider:

- append `--subscription {subscription_id}` to all six az invocations across
  deploy/status/destroy/doctor via a _subscription_args() helper
- fail fast in deploy() when subscription_id is unset (matches GCP's project_id
  guard)
- check_azure_cli() now compares the ambient subscription against the configured
  one and warns on mismatch

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 11:14:47 +00:00
praisonai-triage-agent[bot]andMervinPraison 497ebfc994 fix: enforce SecurityPolicy in DockerSandbox (fixes #4302)
DockerSandbox accepted a SecurityPolicy and enforced none of it, so moving
to the tier documented as stronger silently dropped every command-level
control. Mirror the tested SubprocessSandbox enforcement:

- Validate commands against blocked_commands / allowed_commands / blocked_paths
  in run_command before dispatch.
- Truncate host-side communicate() output to max_output_size in run_command
  and execute, so a runaway print in the container cannot exhaust the host
  process.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 11:08:11 +00:00
praisonai-triage-agent[bot] d56545c060 Merge pull request #4299 from MervinPraison/claude/issue-4298-20260824-1002
fix: honour restart_safe=False on async retry path (fixes #4298)
2026-08-24 11:00:10 +00:00
praisonai-triage-agent[bot] f699b00526 Merge pull request #4297 from MervinPraison/claude/issue-4295-20260824-0956
fix: memory get_all_memories/delete_memory bypass active adapter
2026-08-24 10:53:58 +00:00
praisonai-triage-agent[bot] 23b91ececd Merge pull request #4296 from MervinPraison/claude/issue-4294-20260824-0950
fix: wire memory-pressure cache eviction into the gateway
2026-08-24 10:52:28 +00:00
praisonai-triage-agent[bot]andMervinPraison 27ce602d82 fix: thread cancel_token through all same-run sync redispatches so /stop halts fully
Extends the OpenAI-native /stop fix to every synchronous same-run
re-dispatch that previously dropped the token, closing the Greptile P1
"nested completions drop cancellation":

- reflection + regeneration _chat_completion calls (chat_mixin.py)
- internal recursive redispatches: context-compression retry,
  model fallback, retryable-error backoff (chat_mixin.py)
- guardrail-retry regeneration via _apply_guardrail_with_retry
  (agent.py + its sync call sites in chat_mixin.py)

All hops just forward the existing cancel_token param (defaults None),
so behaviour is unchanged when no token is set. No new Agent params,
modules, or exports.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 10:30:45 +00:00
MervinPraison 773a27bb00 docs: auto-update documentation parity trackers [skip ci] 2026-08-24 10:29:23 +00:00
praisonai-triage-agent[bot] e1acc793fd Merge pull request #4293 from MervinPraison/claude/issue-4290-20260824-0918
fix: scope-gate ack reactions so bots stay quiet on ambient group chatter
2026-08-24 10:28:46 +00:00
praisonai-triage-agent[bot]andMervinPraison 10f1204e73 fix: thread cancel_token into OpenAI-native tool loop so /stop halts mid-run (fixes #4300)
The default (non-custom) LLM path dropped the cancel token: chat_mixin
threaded llm_kwargs['cancel_token'] only inside the custom-LLM branch,
while the OpenAI-native else branch called _chat_completion(...) which
had no cancel_token parameter. The token never reached OpenAIClient,
so the between-iteration cancel checks were never armed and /stop
reported success while remaining tool calls kept executing.

Thread cancel_token symmetrically through the four sync hops that
already existed: _chat_completion -> _chat_completion_with_retry ->
_execute_unified_chat_completion -> _unified_dispatcher.chat_completion,
matching the custom-LLM branch. Defaults to None so no caller behaviour
changes.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 10:23:59 +00:00
praisonai-triage-agent[bot] ca205ccbac Merge pull request #4291 from MervinPraison/claude/issue-4289-20260824-0917
fix: detect silently half-open Signal transport via inbound liveness
2026-08-24 10:20:36 +00:00
praisonai-triage-agent[bot]andMervin Praison 4e13524749 fix: honour restart_safe=False for override-only tools on async retry
The async idempotency veto only scanned self.tools, so a task-scoped tool
supplied via tools_override (and never registered on the agent) missed its
restart_safe=False / idempotent=False declaration and was retried, duplicating
its side effect. _tool_declares_not_idempotent now accepts the effective
tools_override set (searched first, mirroring _execute_tool_async_impl
resolution) while defaulting to None for the unchanged sync call sites.

Also tightens the test helper to catch only the expected RuntimeError /
ToolExecutionError instead of a blanket Exception, and adds an override-only
regression test.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 10:14:10 +00:00
praisonai-triage-agent[bot]andMervin Praison c847eae31d fix: preserve public type field in adapter-backed get_all_memories
Adapter-backed get_all_memories() returned records keyed by `memory_type`
(SQLite) or `type` (in-memory), diverging from the legacy path's `type`
field and breaking callers that read record["type"]. Normalize adapter
results to always populate `type` while retaining adapter-specific keys.

Strengthen regression tests: assert both colliding tiers are erased on
untyped deletion, and assert the public `type` field is present on
adapter-backed records.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 10:06:20 +00:00
praisonai-triage-agent[bot]andMervinPraison 15894f2cba fix: honour restart_safe=False on async retry path (fixes #4298)
_execute_tool_async_with_retry never consulted the idempotency
declaration, so achat()/astart() re-ran an effectful tool marked
restart_safe=False up to max_attempts times. Add the same veto the
sync loops use (tool_execution.py:884 and :2115) plus an async test.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 10:04:24 +00:00
praisonai-triage-agent[bot]andMervin Praison ccce50368d fix: track per-session flush + nested cgroup limit for pressure eviction
Address reviewer findings on PR #4296:

- Data-loss guard (CodeRabbit): derive WarmSession.flushed from a per-key
  _flushed marker set only after a completed store write, not from
  _store presence. A caught/partial persist now leaves the cache unflushed
  so the pressure sweep never evicts the only current copy. Marker cleared
  in _save_history (pre-write), _soft_evict, and reap_stale.
- Nested cgroup (CodeRabbit): resolve the process's own cgroup path from
  /proc/self/cgroup and probe its v2/v1 memory limit first, falling back to
  the filesystem root so a nested container's lower ceiling is honoured.
- Tests: add failed-persist skip and nested-cgroup coverage (15 total pass;
  core planner 16 and gateway session suites still green).

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 10:03:18 +00:00
praisonai-triage-agent[bot]andMervinPraison 57716519cb fix: memory get_all_memories/delete_memory bypass active adapter (fixes #4295)
get_all_memories() selected from legacy short_mem/long_mem tables that no
adapter creates, so it swallowed OperationalError and returned [] for every
provider. Delegate to the active adapter, mirroring delete_short_term/
reset_short_term.

delete_memory(id) with no memory_type short-circuited on the first tier that
reported a hit. Since the SQLite adapter gives each tier its own AUTOINCREMENT
sequence, ids collide across tiers, so it erased an unrelated short-term row
and left the caller's target in place while reporting success. Delete from
both tiers instead of short-circuiting.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 09:58:16 +00:00
praisonai-triage-agent[bot]andMervinPraison 31beac49a8 fix: wire memory-pressure cache eviction into the gateway (fixes #4294)
The core planner plan_pressure_evictions() plus the WarmSession /
MemoryPressureProtocol contracts had no runtime consumer, so an
always-on multi-session gateway had no way to reclaim warm-cache memory
under pressure and climbed to OOM.

This adds the missing wrapper-side enactor and a concrete reader:

- BotSessionManager.warm_sessions(): snapshots warm caches as core
  WarmSession facts (LRU last_activity, in_flight, flushed).
- BotSessionManager.sweep_under_pressure(): reads the real cgroup budget/
  RSS, calls the core planner, and soft-evicts the coldest rebuildable
  caches LRU-first, re-sampling RSS to shed the minimum. No-op without a
  cgroup limit, preserving today's behaviour.
- BotSessionManager._soft_evict(): drops only the in-memory history; the
  durable store copy remains and is rebuilt on the next turn (lossless).
- Per-turn in-flight tracking so a live turn's cache is never evicted.
- gateway/memory_pressure.py CgroupMemoryPressure: stdlib-only concrete
  MemoryPressureProtocol (cgroup v2 memory.high/max, v1 fallback,
  /proc/self/status RssAnon), degrading to no-op when unreadable.

Flushed (store-backed) caches only are evicted; in-flight and unflushed
caches are always skipped. Backward-compatible: no eviction unless the
gateway calls sweep_under_pressure on its periodic tick.

Co-authored-by: MervinPraison <[email protected]>
2026-08-24 09:55:35 +00:00
praisonai-triage-agent[bot]andMervin Praison cb896b7a64 fix: scope-gate ack in gateway Telegram path & Slack thread replies
Closes two P1 gaps in the ack scope gate (Greptile #4293):

- Gateway-hosted Telegram handler acked on `_ack.enabled` alone, bypassing
  ack_scope so ambient group chatter still got a ⏳ reaction. Now routes
  through should_ack_message like the standalone adapter.
- Slack classified a thread reply (no fresh @mention, mention_required off)
  as ambient, silently dropping its ack. thread_ts now counts as engaging
  the bot so the reply is acked under group-mentions.

Adds 3 TestAckScopeGating cases for the gateway wiring and Slack thread
classification.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 09:31:32 +00:00
praisonai-triage-agent[bot]andMervin Praison 00fd87e9f5 fix: forward reviewer custody through gateway backend and prefer explicit operator id
Addresses Greptile P1 findings on PR #4292:
- GatewayApprovalBackend now threads authorized_reviewers into register(),
  so per-request reviewer custody is actually bound on the production path
  (previously silently dropped, leaving every request unbound).
- _resolve_operator_identity prefers a non-secret X-Operator-Id header before
  the shared-token digest, so a proxy/SSO layer can distinguish individual
  operators sharing a gateway token in the audit trail.

Adds a regression test asserting the backend binds custody end-to-end.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 09:29:35 +00:00
praisonai-triage-agent[bot]andMervin Praison 5ba76ed331 fix: scope-gate ack reactions so bots stay quiet on ambient group chatter (fixes #4290)
Add a closed AckScope (off | direct | group-mentions | group-all | all)
to AckReactor with a should_ack()/should_ack_message() gate, and a
BotConfig.ack_scope key (default group-mentions). Adapter call sites
(telegram/discord/slack/whatsapp/signal/linear) now derive direct/mention/
ambient context from the existing BotMessage and only ack messages the
scope covers, instead of reacting to every non-command message.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 09:25:11 +00:00
praisonai-triage-agent[bot]andMervin Praison 0c45b609ca fix: record resolver identity and per-request reviewer custody on gateway approvals (fixes #4288)
The gateway resolve boundary now captures the resolving principal on the
durable audit trail (ApprovalDecision.approver) and allow-always grant,
replacing the constant "gateway"/"gateway:human" attribution. Resolution
gains an optional resolver id, threaded from the authenticated HTTP request
context (derived as a non-secret hash handle of the operator token).

An approval may optionally be bound to a set of authorized reviewers;
resolve() rejects a resolver not in that set (re-inserting the request so an
authorised reviewer can still resolve it) while preserving first-answer-wins
via the atomic pop. The binding is durable across restarts. Core
ApprovalRequest gains an optional authorized_reviewers field (the only core
touch). All defaults keep today's behaviour.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 09:22:57 +00:00
praisonai-triage-agent[bot]andMervin Praison 7a21a8277b fix: detect silently half-open Signal transport via inbound liveness (fixes #4289)
The Signal poll loop refreshed inbound-liveness on every successful poll
batch, including successfully-empty ones. A silently half-open transport
returns [] forever without raising, so this kept health.last_activity
fresh and defeated the existing evaluate_channel_health STALE_SOCKET seam,
leaving a "deaf" channel reported HEALTHY with no operator signal.

Only note inbound activity when the bridge actually delivered an envelope,
so a stalled poll loop becomes detectable through the existing health seam
(no new module, API surface, or config knob).

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 09:21:00 +00:00
praisonai-triage-agent[bot]andMervin Praison f915f06c13 fix: restore single-quoted list parsing and bound adversarial scan
- Re-add Python-style single-quoted list support via ast.literal_eval
  (safe literal-only eval), fixing a regression where ['a','b'] collapsed
  into a single loop item.
- Anchor JSONDecoder.raw_decode with the idx argument instead of slicing
  text[start:] to avoid per-bracket copies.
- Cap decode attempts (and keep the 1M-char guard) so deeply nested
  bracket input can no longer cause O(n^2) parsing time or a crash
  (RecursionError now caught alongside JSONDecodeError).
- Add regression tests for single-quoted lists and adversarial input.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-24 01:05:26 +00:00
KevinTran3011 59dc064842 fix: avoid backtracking regex in workflow list parsing 2026-08-24 10:46:40 +10:00
KevinTran3011 1f0bdb74c8 fix: keep CLI prompts out of trusted agent instructions 2026-08-24 10:46:03 +10:00
user ce42580513 2026-08-23 23-44 - AEO: checksum install + terminal roster (Build Remote Agent) 2026-08-23 23:44:44 +02:00
user 8a75a92f87 2026-08-23 22-05 - Build Remote Agent pairing device 2026-08-23 22:05:58 +02:00
praisonai-triage-agent[bot]andMervinPraison 0da22a1991 fix: remove dead ServerRegistry class in agent.py (fixes #4273)
Delete the unused ServerRegistry class and _get_default_server_registry()
helper. These were never wired into the live launch path; server state
lives in module-level dicts in execution_mixin.py, torn down by
cleanup_launch_registration(). Also update the now-stale comment that
referenced the removed class.

Co-authored-by: MervinPraison <[email protected]>
2026-08-23 17:13:43 +00:00
praisonai-triage-agent[bot]andMervinPraison 13c20df5f9 fix: exclude persistence test package from installed wheel (fixes #4271)
Co-authored-by: MervinPraison <[email protected]>
2026-08-23 16:16:18 +00:00
praisonai-triage-agent[bot]andMervin Praison b2858f3990 fix: harden sandbox rlimits, cancel-before-throttle parity (sync+async)
- Resolve RLIMIT_* via getattr so POSIX platforms missing a constant skip
  that cap instead of raising AttributeError before subprocess launch
- Add RLIMIT_CPU (CPU-seconds from timeout) so CPU-bound sandbox code is
  stopped by the kernel rather than only the wall-clock timeout
- Check cancel token before AND after rate-limiter acquire in both sync
  chat() and async achat() backend branches, so pre-cancelled requests do
  not block on the limiter or consume capacity

Co-authored-by: Mervin Praison <[email protected]>
2026-08-23 07:26:34 +00:00
praisonai-triage-agent[bot]andMervin Praison 3298a8caff fix: enforce sandbox resource limits, add sync chat() backend guards, dedupe Gemini tool constant (fixes #4246)
1. Sandbox ResourceLimits (memory/CPU/process/fd/disk caps) are now applied
   via setrlimit in a POSIX preexec_fn in _execute_code_sandboxed, instead of
   only honouring the wall-clock timeout. Non-POSIX platforms log a warning.
2. Sync chat()'s managed-backend branch now applies the rate limiter and
   cooperative cancellation check before delegating, matching achat().
3. _format_tools_for_litellm now imports the canonical GEMINI_INTERNAL_TOOLS
   constant instead of a hand-duplicated set.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-23 07:16:14 +00:00
praisonai-triage-agent[bot]andMervin Praison e91eca207c test: assemble redaction fixtures at runtime to clear GitGuardian
The new sanitise tests embedded realistic secret-shaped literals (Stripe
sk_live_/sk_test_ keys, an AWS-style secret value) as fixtures, which
GitGuardian flagged as hardcoded secrets and blocked CI. Construct those
fixtures by concatenation at runtime so no scannable secret literal lives
in source, while the redaction regexes are still exercised against a
matching string. No change to what the tests assert.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-23 05:37:48 +00:00
praisonai-triage-agent[bot]andMervin Praison daf65684a4 fix: validate direct RuntimeConfig, non-mutating normalise, align qdrant examples
Address reviewer findings on #4236:
- Route direct RuntimeConfig(runtime=...) through resolve_runtime so a
  preferred_runtime typo raises instead of silently dropping capabilities.
- resolve_runtime returns a normalised copy of a RuntimeConfig instead of
  mutating the caller's object.
- canonical_runtime_name passes the full alias set to the error builder so a
  close typo (e.g. "reducd") suggests the matched alias.
- Rename knowledge_qdrant.py -> knowledge_chroma.py (now demonstrates Chroma)
  and restore qdrant_wow.py as a genuine Qdrant adapter example that skips
  cleanly, matching filenames to content.
- Add tests: no-mutation, typo suggestion, Agent-level normalisation + typo.

Co-authored-by: Mervin Praison <[email protected]>
2026-08-23 05:31:13 +00:00
praisonai-triage-agent[bot]andMervinPraison c05a6ec53d fix: restore repeated session turns and close session-share secret leaks (fixes #4243)
- run --continue: restore chat history by position instead of a content-based
  filter, so repeated turns (e.g. a bare "yes" confirmation) are no longer
  silently dropped; _auto_save_last_index tracks the full restored length.
- session share / export redaction: mask sk_live_/sk_test_ Stripe keys, anchor
  the key=value rule on keys that *contain* a secret word (AWS_SECRET_ACCESS_KEY,
  STRIPE_SECRET_KEY), and mask secret values whole so a value containing '/' is
  not half-masked by the path regex.
- session share now defaults --redact-level to strict, since it publishes a
  transcript for wider distribution.

Co-authored-by: MervinPraison <[email protected]>
2026-08-23 05:27:22 +00:00
praisonai-triage-agent[bot]andMervin Praison db06f5b8c5 fix: harden arg-scoped deny (empty-policy, key-normalization, modified-args recheck)
Address reviewer findings on #4234 (issue #4228):
- agent.py: use `is None` (not falsiness) so an explicit empty permissions
  policy (`permissions={}`) owns denial instead of inheriting default denials.
- tool_execution.py: normalize argument keys (strip trailing '=') before
  build_permission_target so a malformed `command=` kwarg cannot evade a
  scoped deny that only normalizes after the gate.
- tool_execution.py: re-run the deny gate against `decision.modified_args` in
  both sync and async paths so an approval backend cannot rewrite allowed args
  into a denied command; BYPASS still opts out.
- tests: add regression coverage for all three.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Mervin Praison <[email protected]>
2026-08-23 05:26:50 +00:00
praisonai-triage-agent[bot]andMervinPraison 9cef4eaa8e fix: derive generator memory backends from resolver; normalise runtime= (fixes #4229)
(a) The code generator advertised a hardcoded memory capability list that
still named removed backends (redis/postgres/qdrant), so generated agent
code raised before the API. Derive supports_presets/supports_url_schemes
from the live resolver registries (MEMORY_PRESETS, MEMORY_URL_SCHEMES) so
the generator can never teach a dead value. Update the four shipped
examples that crashed at the config boundary to use shipped backends.

(b) runtime= stored any string verbatim, silently dropping nine of twelve
capabilities on a typo. Normalise through canonical_runtime_name() so
spelling variants (NATIVE, " native ", Native) all map to the native
matrix, and raise on a close typo (nativ -> "Did you mean native?").
A genuinely unknown plugin runtime name is still accepted and falls back
to the reduced harness via the runtime registry, not a closed literal.

Co-authored-by: MervinPraison <[email protected]>
2026-08-23 04:55:43 +00:00
praisonai-triage-agent[bot]andMervinPraison ac4c6bf81c fix: keep default denials when configuring approval + enforce argument-scoped deny rules (fixes #4228)
(a) The ApprovalConfig and dict approval branches left _perm_deny at its
empty init value, making a config object a WEAKER posture than passing
nothing (ApprovalConfig(timeout=30) silently dropped all 8 default
denials). They now fall back to the env-driven default deny set when no
declarative permissions policy is supplied, so configuring approval never
removes the default denials. PRAISONAI_TOOL_SAFETY=off still fully bypasses.

(b) The PermissionManager deny gate only asked with the tool *name*, so
argument-level deny rules (e.g. bash:rm *) never fired. _check_permission_manager_deny
now also builds the argument-scoped target via build_permission_target and
checks it, so an argument-scoped rule can narrow an allowed shell tool.
Name-level rules keep their all-or-nothing behaviour.

Co-authored-by: MervinPraison <[email protected]>
2026-08-23 04:52:11 +00:00
EvolinkAI cfe3881c56 fix: handle missing Evolink API key in example 2026-06-23 19:12:11 +08:00
EvolinkAI 838ce5264a feat: add Evolink provider example
Document Evolink's OpenAI-compatible endpoint in the sample environment and add a provider example that uses the existing PraisonAI base_url/api_key configuration path. Cover the config wiring with an offline unit test so the integration stays regression-tested without live API calls.
2026-06-23 19:12:11 +08:00
247 changed files with 35653 additions and 1601 deletions

No files matched your search

@@ -0,0 +1,86 @@
---
name: praisonai-js-hub-intake
description: >-
Explains Option B hub intake for PraisonAI TypeScript/JavaScript (npm praisonai):
accept TS issues on MervinPraison/PraisonAI, implement fixes in MervinPraison/praisonai-js,
sync to src/praisonai-ts/. Use when routing issues, implementing TS fixes, sync workflows,
merge-gate TS paths, or when the user mentions praisonai-js, praisonai-ts, npm SDK, or TS hub intake.
---
# praisonai-js hub intake (Option B)
## Model in one sentence
**Users file TypeScript issues on [PraisonAI](https://github.com/MervinPraison/PraisonAI); fixes land in [praisonai-js](https://github.com/MervinPraison/praisonai-js); the monorepo `src/praisonai-ts/` is a sync mirror only.**
Do **not** ask users to re-file on praisonai-js unless they opened a duplicate on the wrong repo.
## Routing table
| Topic | Report | Fix repo | Monorepo mirror |
|-------|--------|----------|-----------------|
| Python SDK / CLI | PraisonAI | `src/praisonai-agents/`, `src/praisonai/` | — |
| TypeScript / npm `praisonai` | PraisonAI (preferred) or praisonai-js | **praisonai-js** | `src/praisonai-ts/` |
| Cross-language parity | PraisonAI | Often both Python + praisonai-js | sync after TS merge |
| Agent-callable tools | PraisonAI | PraisonAI-Tools | — |
| Lifecycle plugins | PraisonAI | PraisonAI-Plugins | — |
## End-to-end flow
```
Issue on PraisonAI
→ issue-triage: labels javascript, praisonai-js (if TS focus)
→ claude label → Claude STEP 3-ALT clones praisonai-js
→ PR on praisonai-js with body: Fixes MervinPraison/PraisonAI#N
→ merge on praisonai-js (CI + merge gate there)
→ Sync to PraisonAI Monorepo workflow on praisonai-js
→ PR on PraisonAI: sync: update src/praisonai-ts/...
```
## Agent rules
1. **Never implement TS features in `src/praisonai-ts/` on PraisonAI** — edit praisonai-js instead.
2. **External PR body** must include `Fixes MervinPraison/PraisonAI#<n>` when fixing a hub issue.
3. **After praisonai-js merge**, run sync workflow (or tell maintainer to):
```bash
gh workflow run "Sync to PraisonAI Monorepo" \
--repo MervinPraison/praisonai-js \
-f upstream_issue=<issue_number>
```
4. **Merge gate on PraisonAI** blocks direct `src/praisonai-ts/` changes unless sync PR (title `sync:` or body mentions praisonai-js).
5. **Tests for TS fixes**: `npm install && npm run build && npm test` in praisonai-js clone.
## TS issue detection (PraisonAI triage)
Labels `javascript` + `praisonai-js` when issue mentions: `praisonai-js`, `praisonai-ts`, `src/praisonai-ts`, `typescript`+`praisonai`, `npm`+`praisonai` (not pypi).
## Key files
See [reference.md](reference.md) for paths in both repos.
## Quick commands
```bash
# Sync Claude secrets to praisonai-js (from PraisonAI hub)
gh workflow run "Sync Claude Secrets" \
--repo MervinPraison/PraisonAI \
-f target_repo=MervinPraison/praisonai-js
# Sync code praisonai-js → monorepo mirror
gh workflow run "Sync to PraisonAI Monorepo" \
--repo MervinPraison/praisonai-js \
-f upstream_issue=1234
```
## Related docs
- Monorepo: `CONTRIBUTING.md` (hub intake table)
- praisonai-js: `AGENTS.md` §2.1.1 Hub intake
- Security: `SECURITY.md` (npm canonical = praisonai-js)
## Cursor skill locations
| Repo | Path |
|------|------|
| PraisonAI monorepo | `.cursor/skills/praisonai-js-hub-intake/` |
| praisonai-js | `.cursor/skills/praisonai-js-hub-intake/` |
@@ -0,0 +1,72 @@
# praisonai-js hub intake — file reference
## PraisonAI monorepo (`MervinPraison/PraisonAI`)
| File | Role |
|------|------|
| `.github/workflows/claude.yml` | Issue triage labels; STEP 3-ALT to praisonai-js; npm test in external flow |
| `.github/scripts/merge-gate.js` | `tsMirrorPathReasons()` blocks non-sync edits to `src/praisonai-ts/` |
| `.github/workflows/claude-merge-gate.yml` | Assess checklist: TS → praisonai-js |
| `.github/scripts/ci-failure-claude.js` | CI fix guardrails include TS routing |
| `.github/actions/claude-code-action/action.yml` | Shared Claude routing (TS → praisonai-js) |
| `.github/praisonai-issue-triage.yaml` | `@praisonai` triage: TS-external routing |
| `.github/workflows/auto-issue-comment.yml` | Auto-triage comment mentions praisonai-js |
| `.github/ISSUE_TEMPLATE/bug_report.md` | Python / TS / cross-language selector |
| `CONTRIBUTING.md` | Hub intake table |
| `SECURITY.md` | npm canonical source = praisonai-js |
| `src/praisonai-ts/` | **Mirror only** — do not implement features here |
| `.cursor/skills/praisonai-js-hub-intake/` | This skill (agent reference) |
### Commits (Option B rollout)
- `4ab1ed280` — Implement Option B hub intake for praisonai-js TypeScript issues
## praisonai-js (`MervinPraison/praisonai-js`)
| File | Role |
|------|------|
| `.github/workflows/sync-to-praisonai.yml` | Rsync to `src/praisonai-ts/`; optional `upstream_issue` input |
| `.github/pull_request_template.md` | `Fixes MervinPraison/PraisonAI#N` + sync checklist |
| `.github/ISSUE_TEMPLATE/bug_report.md` | TS bug template; link to PraisonAI for cross-stack |
| `.github/ISSUE_TEMPLATE/config.yml` | Contact links → PraisonAI for Python/cross-stack |
| `.github/scripts/gate-config.js` | Scope + `externalRepos` hub-intake config |
| `.github/workflows/claude.yml` | Hub caller (Claude automation) |
| `AGENTS.md` §2.1.1 | Hub intake for agents |
| `README.md` | Reporting issues table |
| `.cursor/skills/praisonai-js-hub-intake/` | This skill (agent reference) |
### Commits
- `0c8cc3f` — Complete Option B reciprocal hub intake
- `bfc5e34` — Add praisonai-js-hub-intake Cursor skill
## github-automation-template
| Item | Role |
|------|------|
| `install.sh --mode hub` | Thin callers on consumer repos |
| `stacks/claude/profiles/sdk/gate-config.js.tmpl` | Base gate config (customise per repo) |
| `AGENTS.md` | Install / rollout guide |
praisonai-js uses hub mode `@v1.1.0`.
## Decision tree for agents
```
Is the issue about npm / TypeScript / praisonai-js / src/praisonai-ts?
├─ YES → Fix in MervinPraison/praisonai-js (STEP 3-ALT)
│ PR: Fixes MervinPraison/PraisonAI#N
│ After merge: Sync to PraisonAI Monorepo
└─ NO → Is it Python praisonaiagents / praisonai CLI?
├─ YES → Fix in this monorepo
└─ NO → Tools / Plugins / Docs external repos (see claude.yml STEP 2)
```
## Anti-patterns
| Wrong | Right |
|-------|-------|
| Edit `src/praisonai-ts/` for a new TS feature | Edit praisonai-js, then sync |
| Tell user to re-file on praisonai-js only | Accept on PraisonAI; route fix externally |
| Merge gate approves direct TS mirror PR without sync metadata | Title `sync:` or body references praisonai-js |
| Skip `Fixes MervinPraison/PraisonAI#N` on external PR | Always cross-link hub issues |
+1
View File
@@ -17,6 +17,7 @@ GROQ_API_KEY=gsk_... # Groq (fast inference)
COHERE_API_KEY=... # Cohere models
AZURE_OPENAI_API_KEY=... # Azure OpenAI
AZURE_OPENAI_ENDPOINT=... # Azure OpenAI endpoint
EVOLINK_API_KEY=... # EvoLink.AI OpenAI-compatible models. Get yours at https://evolink.ai/dashboard/keys
# === Optional: Memory backends ===
MEM0_API_KEY=... # Mem0 memory service
+7 -1
View File
@@ -6,6 +6,11 @@ labels: bug
assignees: ''
---
## SDK / package
- [ ] Python (`pip install praisonai` / `praisonaiagents`)
- [ ] TypeScript / JavaScript (`npm install praisonai`) — fixes land in [praisonai-js](https://github.com/MervinPraison/praisonai-js); you can report here or there
- [ ] Cross-language / parity (file here on PraisonAI)
## Environment
- Provider (select one):
- [ ] Anthropic
@@ -14,6 +19,7 @@ assignees: ''
- [ ] AWS Bedrock
- [ ] Other: <!-- specify -->
- PraisonAI version: <!-- if known -->
- Node.js version (TS/JS only): <!-- e.g. 20.x -->
- Operating System: <!-- e.g. macOS 14.3, Windows 11, Ubuntu 22.04 -->
## Full Code
@@ -31,4 +37,4 @@ assignees: ''
<!-- What actually happened -->
## Additional Context
<!-- Add any other context about the problem here, such as screenshots, logs, etc. -->
<!-- Add any other context about the problem here, such as screenshots, logs, etc. -->
@@ -225,9 +225,10 @@ runs:
- CORE vs WRAPPER vs TOOLS vs PLUGINS ROUTING:
1. Core SDK (praisonaiagents/): Only core protocols, base classes, decorators. No heavy implementations.
2. Wrapper (praisonai/): CLI, heavy implementations, optional dependencies.
3. Tools → MervinPraison/PraisonAI-Tools: agent-callable integrations at runtime (Composio, Slack, APIs). Do NOT add to praisonaiagents/.
4. Plugins → MervinPraison/PraisonAI-Plugins: lifecycle extensions (tracing, logging, hooks, guardrails). Not agent tools.
5. Sandbox backends → PraisonAI-Plugins via `praisonai.sandbox` entry point (core protocol only).
3. TypeScript/JavaScript SDK → MervinPraison/praisonai-js: canonical npm repo. Do NOT edit src/praisonai-ts/ here.
4. Tools → MervinPraison/PraisonAI-Tools: agent-callable integrations at runtime (Composio, Slack, APIs). Do NOT add to praisonaiagents/.
5. Plugins → MervinPraison/PraisonAI-Plugins: lifecycle extensions (tracing, logging, hooks, guardrails). Not agent tools.
6. Sandbox backends → PraisonAI-Plugins via `praisonai.sandbox` entry point (core protocol only).
- Does it duplicate existing functionality? Check if Agent already supports this via existing params (reflection, planning, tools, hooks, memory).
- Do not bloat the Agent class with additional params — only if absolutely required; we already support many params.
- Does it inherit from Agent properly? New agent types MUST inherit Agent, not wrap it with composition.
+3 -2
View File
@@ -35,16 +35,17 @@ steps:
Third, read architecture guidelines: execute_command 'cat src/praisonai-agents/AGENTS.md'.
Fourth, decide routing before planning:
- TypeScript/JavaScript SDK → MervinPraison/praisonai-js: canonical npm repo. Do NOT edit src/praisonai-ts/ in this monorepo. Cross-link upstream issue in external PR body: Fixes MervinPraison/PraisonAI#N.
- Tools → PraisonAI-Tools: agent-callable integrations at runtime (Composio, Slack, APIs).
- Plugins → PraisonAI-Plugins: lifecycle extensions (tracing, logging, hooks, guardrails).
- Sandbox backends → PraisonAI-Plugins via `praisonai.sandbox` entry point.
Core SDK/wrapper only for protocols and CLI — external repos for Tools/Plugins.
Core SDK/wrapper only for protocols and CLI — external repos for TS SDK/Tools/Plugins.
Fifth, decide if this issue requires code changes:
- If YES: identify the exact files and lines that must change. Output a concrete file-level plan.
- If NO (question, discussion, duplicate, invalid): say so clearly. No code changes needed.
Output: routing decision (Core / Wrapper / Tools-external / Plugins-external / No-change), validation result, and exact files/lines to change.
Output: routing decision (Core / Wrapper / TS-external / Tools-external / Plugins-external / No-change), validation result, and exact files/lines to change.
expected_output: A decision on whether code changes are needed, and if so a concrete file-level plan.
- name: Implement Fix
+1
View File
@@ -157,6 +157,7 @@ function buildCiFixComment({ headSha, failedChecks, failureSummaries }) {
'3. **Pre-existing flake or unrelated failure** — say so explicitly; prefer fixing the root cause over masking it.',
'',
'**SDK guardrails (AGENTS.md):**',
'- TypeScript/JavaScript SDK → MervinPraison/praisonai-js (not src/praisonai-ts/ in this monorepo).',
'- Agent-callable tools → PraisonAI-Tools; lifecycle plugins → PraisonAI-Plugins; sandbox backends → PraisonAI-Plugins (`praisonai.sandbox`) — not praisonaiagents/.',
'- Do not disturb core SDK contracts to accommodate a wrapper/feature change.',
'- Tests must continue to guard backward compatibility and hot-path behaviour — passing CI by lowering test standards is not acceptable.',
+21 -1
View File
@@ -308,7 +308,7 @@ function finalClaudeCompletedOnSha(comments, headPushedAt) {
}
const FINAL_CLAUDE_REVIEW_BODY =
'@claude You are the FINAL architecture reviewer. If the branch is under MervinPraison/PraisonAI (not a fork), you are able to make modifications to this branch and push directly. SCOPE: Focus ONLY on Python packages (praisonaiagents, praisonai). Do NOT modify praisonai-rust or praisonai-ts. Read ALL comments above from Gemini, Qodo, CodeRabbit, and Copilot carefully before responding.\n\n**Phase 1: Review per AGENTS.md**\n1. Protocol-driven: check heavy implementations vs core SDK\n2. Backward compatible: ensure zero feature regressions\n3. Performance: no hot-path regressions\n4. SDK value: review in depth whether the change genuinely adds value to the SDK — never add features for the sake of adding them. It must strengthen the SDK (simpler, more user-friendly, robust, world-class, secure). If it does not clearly add value, request changes or recommend rejecting/closing rather than merging scope creep\n5. Do not bloat the Agent class with additional params — only if absolutely required; we already support many params.\n6. Repo routing: agent-callable tools → PraisonAI-Tools; lifecycle plugins → PraisonAI-Plugins; optional sandbox backends → PraisonAI-Plugins (`praisonai.sandbox` entry point) — request changes if wrongly added to praisonaiagents/\n\n**Phase 2: FIX Valid Issues**\n7. For any VALID bugs or architectural flaws found by Gemini, CodeRabbit, Qodo, Copilot, or any other reviewer: implement the fix\n8. Also independently identify and fix any gaps or issues you find in the changed code — do not rely only on prior reviewer feedback\n9. Push all code fixes directly to THIS branch (do NOT create a new PR)\n10. Comment a summary of exact files modified and what you skipped\n\n**Phase 3: Final Verdict**\n11. If all issues are resolved, approve the PR / close the Issue\n12. If blocking issues remain, request changes / leave clear action items';
'@claude You are the FINAL architecture reviewer. If the branch is under MervinPraison/PraisonAI (not a fork), you are able to make modifications to this branch and push directly. SCOPE: Focus ONLY on Python packages (praisonaiagents, praisonai). Do NOT modify src/praisonai-rust or src/praisonai-ts in this repo — TypeScript SDK work belongs in MervinPraison/praisonai-js. Read ALL comments above from Gemini, Qodo, CodeRabbit, and Copilot carefully before responding.\n\n**Phase 1: Review per AGENTS.md**\n1. Protocol-driven: check heavy implementations vs core SDK\n2. Backward compatible: ensure zero feature regressions\n3. Performance: no hot-path regressions\n4. SDK value: review in depth whether the change genuinely adds value to the SDK — never add features for the sake of adding them. It must strengthen the SDK (simpler, more user-friendly, robust, world-class, secure). If it does not clearly add value, request changes or recommend rejecting/closing rather than merging scope creep\n5. Do not bloat the Agent class with additional params — only if absolutely required; we already support many params.\n6. Repo routing: TypeScript/JavaScript SDK → praisonai-js (not src/praisonai-ts/ here); agent-callable tools → PraisonAI-Tools; lifecycle plugins → PraisonAI-Plugins; optional sandbox backends → PraisonAI-Plugins (`praisonai.sandbox` entry point) — request changes if wrongly added to praisonaiagents/\n\n**Phase 2: FIX Valid Issues**\n7. For any VALID bugs or architectural flaws found by Gemini, CodeRabbit, Qodo, Copilot, or any other reviewer: implement the fix\n8. Also independently identify and fix any gaps or issues you find in the changed code — do not rely only on prior reviewer feedback\n9. Push all code fixes directly to THIS branch (do NOT create a new PR)\n10. Comment a summary of exact files modified and what you skipped\n\n**Phase 3: Final Verdict**\n11. If all issues are resolved, approve the PR / close the Issue\n12. If blocking issues remain, request changes / leave clear action items';
async function getMergeState(github, owner, repo, prNumber) {
const query = `
@@ -600,6 +600,24 @@ function hasManualOnlyLabel(labels) {
return labels.some((l) => MANUAL_ONLY_LABELS.has(l));
}
const TS_MIRROR_PREFIX = 'src/praisonai-ts/';
function tsMirrorPathReasons(files, pr) {
const touchesTs = files.some((f) => f.filename.startsWith(TS_MIRROR_PREFIX));
if (!touchesTs) return [];
const title = (pr.title || '').toLowerCase();
const body = (pr.body || '').toLowerCase();
const isSyncPr =
title.startsWith('sync:') ||
body.includes('sync from praisonai-js') ||
body.includes('praisonai-js') ||
body.includes('mervinpraison/praisonai-js');
if (isSyncPr) return [];
return [
'src/praisonai-ts/ changed outside praisonai-js sync — implement in MervinPraison/praisonai-js, then run Sync to PraisonAI Monorepo workflow',
];
}
function sensitivePathReasons(files, labels = []) {
if (labels.includes(WORKFLOW_ONLY_LABEL) && isCiOnlyChange(files)) {
return [];
@@ -823,6 +841,7 @@ async function evaluatePipelineQuiescent(github, owner, repo, prNumber, core, op
if (agentManual) reasons.push(agentManual);
reasons.push(...sensitivePathReasons(pullFiles, ctx.labels));
reasons.push(...tsMirrorPathReasons(pullFiles, ctx.pr));
reasons.push(...prSizeReasons(pullFiles));
reasons.push(...secretScanReasons(pullFiles));
@@ -989,6 +1008,7 @@ module.exports = {
touchesSdk,
hasManualOnlyLabel,
sensitivePathReasons,
tsMirrorPathReasons,
prSizeReasons,
missingTestsReason,
secretScanReasons,
+1 -1
View File
@@ -42,5 +42,5 @@ jobs:
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '🤖 Auto-triaging with Claude. Analyzing issue and implementing a fix...\n\nClaude will:\n1. Route correctly: agent tools → PraisonAI-Tools; lifecycle plugins → PraisonAI-Plugins\n2. Search codebase for root cause\n3. Implement a minimal fix following AGENTS.md\n4. Create a PR with the changes'
body: '🤖 Auto-triaging with Claude. Analyzing issue and implementing a fix...\n\nClaude will:\n1. Route correctly: TypeScript/JavaScript SDK → praisonai-js; agent tools → PraisonAI-Tools; lifecycle plugins → PraisonAI-Plugins\n2. Search codebase for root cause\n3. Implement a minimal fix following AGENTS.md\n4. Create a PR with the changes (external repo PRs link back to this issue)'
});
+1 -1
View File
@@ -289,7 +289,7 @@ jobs:
1. Read AGENTS.md and the PR diff on the current branch.
2. Read all PR comments and reviews (FINAL architecture reviewer / Lead Engineer must have run).
3. Confirm SDK value gate: change strengthens praisonaiagents (no scope creep). The aim is a LIGHTWEIGHT AND POWERFUL package — BLOCK changes that add params/modules/exports duplicating existing Agent capabilities (instructions/backstory/tools/hooks/memory), or add knobs/fields with no live consumer, i.e. scope creep for the sake of it.
4. Repo routing: BLOCK if agent-callable tools (→ PraisonAI-Tools), lifecycle plugins (→ PraisonAI-Plugins), or optional sandbox backends (→ PraisonAI-Plugins via `praisonai.sandbox`) were added to praisonaiagents/ instead of the external repo.
4. Repo routing: BLOCK if TypeScript/JavaScript SDK changes in src/praisonai-ts/ (→ praisonai-js + sync workflow); agent-callable tools (→ PraisonAI-Tools); lifecycle plugins (→ PraisonAI-Plugins); or optional sandbox backends (→ PraisonAI-Plugins via `praisonai.sandbox`) were added to praisonaiagents/ instead of the external repo.
5. Do not bloat the Agent class with additional params — only if absolutely required; we already support many params.
6. If agent.py adds >100 lines or new Agent __init__ params, respond MERGE_GATE_VERDICT: BLOCK (manual review required).
7. BLOCK if labels security/breaking-change/needs-manual-review/release, sensitive paths (.github/workflows, auth, pyproject.toml), secrets in diff, PR >800 lines or >30 files, or SDK changes without tests. Label `merge-gate-ci-only` exempts CI-only changes under `.github/workflows/`, `.github/actions/`, or merge-gate scripts.
+22 -5
View File
@@ -70,9 +70,21 @@ jobs:
if (content.includes('documentation') || content.includes('docs')) {
labels.push('documentation');
}
if (content.includes('typescript') || content.includes('javascript') || content.includes('npm')) {
const isPraisonaiJsFocus =
content.includes('praisonai-js') ||
content.includes('praisonai-ts') ||
content.includes('src/praisonai-ts') ||
content.includes('typescript sdk') ||
content.includes('javascript sdk') ||
(content.includes('typescript') && content.includes('praisonai')) ||
(content.includes('javascript') && content.includes('praisonai')) ||
(content.includes('npm') && content.includes('praisonai') && !content.includes('pypi'));
if (isPraisonaiJsFocus || content.includes('typescript') || content.includes('javascript') || content.includes('npm')) {
labels.push('javascript');
}
if (isPraisonaiJsFocus) {
labels.push('praisonai-js');
}
// Apply labels
if (labels.length > 0) {
@@ -100,7 +112,7 @@ jobs:
'',
'_Bug and enhancement reports are auto-triaged with Claude. Questions and other issues: a maintainer can comment `@claude` to trigger analysis._',
'',
'**Routing:** Agent-callable tools → [PraisonAI-Tools](https://github.com/MervinPraison/PraisonAI-Tools); lifecycle plugins (tracing/hooks/guardrails) → [PraisonAI-Plugins](https://github.com/MervinPraison/PraisonAI-Plugins).'
'**Routing:** TypeScript/JavaScript SDK → [praisonai-js](https://github.com/MervinPraison/praisonai-js) (canonical; monorepo `src/praisonai-ts/` is synced from there). Agent-callable tools → [PraisonAI-Tools](https://github.com/MervinPraison/PraisonAI-Tools); lifecycle plugins (tracing/hooks/guardrails) → [PraisonAI-Plugins](https://github.com/MervinPraison/PraisonAI-Plugins).'
].join('\n')
});
}
@@ -293,7 +305,7 @@ jobs:
STEP 1 — READ GUIDELINES:
Read AGENTS.md to understand the architecture rules.
SCOPE: Focus ONLY on Python packages (praisonaiagents, praisonai). Do NOT modify or touch praisonai-rust or praisonai-ts packages.
SCOPE: Focus ONLY on Python packages (praisonaiagents, praisonai). Do NOT modify praisonai-rust or src/praisonai-ts in this repo — TypeScript/JavaScript SDK work belongs in MervinPraison/praisonai-js (STEP 3-ALT).
AIM: This package must stay LIGHTWEIGHT AND POWERFUL. Do a CRITICAL REVIEW at each stage (issue triage, fix planning, PR review/implementation) and reject scope creep for the sake of adding features. Prefer existing Agent capabilities (instructions/backstory/tools/hooks/memory) over new params/modules/exports. Even when fixing a raised issue, keep the fix minimal and lightweight — do NOT expand the surface area or add knobs with no live consumer.
STEP 2 — ARCHITECTURE VALIDATION & ROUTING (MANDATORY before writing code):
@@ -305,7 +317,8 @@ jobs:
4. Documentation (PraisonAIDocs): Documentation pages, guides, API references.
5. Plugins → MervinPraison/PraisonAI-Plugins: framework lifecycle extensions (tracing, logging, metrics, hooks, guardrails). Not agent tools — use STEP 3-ALT.
6. Sandbox backends (optional/niche, e.g. Capsule) → PraisonAI-Plugins via `praisonai.sandbox` entry point; core keeps protocol/config/manager only.
For items routed to an EXTERNAL repository (Tools, Docs, Plugins), follow STEP 3-ALT below.
7. TypeScript/JavaScript SDK → MervinPraison/praisonai-js: canonical npm SDK repo. Do NOT implement in src/praisonai-ts/ here — clone praisonai-js and use STEP 3-ALT. After merge there, run **Sync to PraisonAI Monorepo** workflow to update src/praisonai-ts/.
For items routed to an EXTERNAL repository (Tools, Docs, Plugins, praisonai-js), follow STEP 3-ALT below.
- Does it duplicate existing functionality? Check if Agent already supports this via existing params (reflection, planning, tools, hooks, memory). If it does, reject the change as scope creep and prefer the existing capability — the aim is lightweight and powerful, not more knobs.
- Do not bloat the Agent class with additional params — only if absolutely required; we already support many params.
- Does it inherit from Agent properly? New agent types MUST inherit Agent, not wrap it with composition.
@@ -318,7 +331,7 @@ jobs:
${{ steps.check_fork.outputs.is_pr == 'true' && (steps.check_fork.outputs.is_fork != 'true' || steps.check_fork.outputs.can_push_fork == 'true') && steps.check_fork.outputs.merge_state != 'DIRTY' && format('STEP 3 — IMPLEMENT ON EXISTING PR (branch: {0}, PR #{1}{2}):\n- DO NOT create a new branch or PR\n- git checkout {0}\n- Implement minimal, focused fixes from reviewer feedback\n- Follow protocol-driven design: protocols in core SDK, heavy implementations in wrapper\n- Keep changes small and backward-compatible\n- Commit: git add -A && git commit -m "fix: <description>"\n- Push: git push origin {0}\n- Comment a summary of exact files modified and what you skipped\n', steps.check_fork.outputs.pr_branch, steps.check_fork.outputs.pr_number, steps.check_fork.outputs.can_push_fork == 'true' && format(', fork: {0}', steps.check_fork.outputs.fork_repo) || '') || '' }}
${{ steps.check_fork.outputs.is_pr != 'true' && 'STEP 3 — IMPLEMENT (for changes in THIS repo — PraisonAI):\n- Create a fix branch: git checkout -b claude/issue-$ISSUE_NUMBER-$(date +%Y%m%d)\n- Implement a minimal, focused fix\n- Follow protocol-driven design: protocols in core SDK, heavy implementations in wrapper\n- Keep changes small and backward-compatible\n- Commit: git add -A && git commit -m "fix: <description> (fixes #$ISSUE_NUMBER)"\n- Push: git push origin claude/issue-$ISSUE_NUMBER-$(date +%Y%m%d)\nCRITICAL: After pushing, you MUST immediately run: gh pr create --title "fix: <title>" --body "Fixes #$ISSUE_NUMBER" --head claude/issue-$ISSUE_NUMBER-$(date +%Y%m%d)\nDo NOT stop after pushing. Do NOT say "manually create a PR". Always create the PR automatically.\n' || '' }}
STEP 3-ALT — IMPLEMENT IN EXTERNAL REPO (for PraisonAI-Tools, PraisonAIDocs, PraisonAI-Plugins):
STEP 3-ALT — IMPLEMENT IN EXTERNAL REPO (for PraisonAI-Tools, PraisonAIDocs, PraisonAI-Plugins, praisonai-js):
When work must happen in a different repository, follow these steps EXACTLY. Do NOT attempt to use `cd`, as directory state is not preserved. Use `git -C` for all commands.
a) Clone the repository:
gh repo clone MervinPraison/<REPO_NAME> /tmp/<REPO_NAME>
@@ -336,9 +349,11 @@ jobs:
gh pr create -R MervinPraison/<REPO_NAME> --head claude/issue-$ISSUE_NUMBER-$(date +%Y%m%d) --title "feat: <title>" --body "Fixes MervinPraison/PraisonAI#$ISSUE_NUMBER
<body>"
h) Comment on THIS PraisonAI issue with the external PR URL. For praisonai-js: note that after merge, a maintainer runs **Sync to PraisonAI Monorepo** on praisonai-js (Actions → sync workflow) to update src/praisonai-ts/.
STEP 4 — TEST:
- For SDK changes: cd src/praisonai-agents && PYTHONPATH=. python -m pytest tests/ -x -q --timeout=30
- For praisonai-js: cd /tmp/praisonai-js && npm install && npm run build && npm test
- For docs changes: verify files exist and are valid markdown/MDX
- Ensure no regressions
@@ -351,6 +366,8 @@ jobs:
Bash(conda:*)
Bash(pytest:*)
Bash(gh:*)
Bash(npm:*)
Bash(node:*)
Bash(python -m pytest:*)
Bash(python -m pip:*)
Bash(poetry:*)
+183
View File
@@ -0,0 +1,183 @@
# Build the desktop app for macOS, Windows and Linux, and attach it to the
# release.
#
# Releases carried no assets at all -- v4.7.1 shipped zero -- so there was
# nothing for anyone to download. The macOS half of this shipped first; this
# adds the other two, which were not merely missing from CI but impossible:
# the bundle targets were `["app", "dmg"]`, both macOS-only, so a Windows or
# Linux build produced zero artifacts and still exited zero.
#
# Unsigned, deliberately, on every platform. macOS signing needs a Developer ID
# and Windows signing now needs a hardware-backed certificate or a hosted
# signing service -- neither is in secrets. Shipping builds people can open
# past the warning is better than shipping none, and the release notes say so
# rather than letting Gatekeeper and SmartScreen say it for us.
name: Desktop release
on:
release:
types: [published]
workflow_dispatch:
inputs:
tag:
description: 'Existing release tag to attach the build to'
required: true
permissions:
contents: write
concurrency:
# Queue rather than cancel: a cancelled release build looks identical to a
# release that simply never produced assets, which is the bug being fixed.
group: desktop-release-${{ github.event.release.tag_name || inputs.tag }}
cancel-in-progress: false
jobs:
build:
strategy:
fail-fast: false
matrix:
include:
# macos-15, not macos-14: that image entered deprecation on
# 2026-07-06 and is fully unsupported from 2026-11-02, with brownout
# periods failing jobs before then. This is the only Apple-silicon
# leg, so losing it means no arm64 DMG at all.
- runner: macos-15 # Apple silicon
target: aarch64-apple-darwin
bundles: dmg
ext: dmg
asset: macos-apple-silicon
# macos-15-intel, not macos-13: that image was retired on
# 2025-12-04, so the leg would never schedule and the verify job
# below would correctly report a missing asset on every release,
# forever. macos-15-intel is GitHub's named x86_64 replacement.
- runner: macos-15-intel # Intel
target: x86_64-apple-darwin
bundles: dmg
ext: dmg
asset: macos-intel
- runner: windows-latest
target: x86_64-pc-windows-msvc
bundles: nsis
ext: exe
asset: windows-x64-setup
# ubuntu-22.04 rather than -latest on purpose: glibc is forward
# compatible only, so the build host sets the *oldest* distribution
# the .deb can run on. Building on 24.04 would produce a package that
# refuses to start on 22.04 and Debian 12 with a "GLIBC_2.38 not
# found" that no runtime flag can fix.
#
# This image begins deprecation on 2026-09-17 and is retired on
# 2027-04-17. When it goes, move to `ubuntu-latest` with
# `container: debian:12` rather than bumping the runner -- that keeps
# the glibc floor decided by us instead of by GitHub's image
# lifecycle.
- runner: ubuntu-22.04
target: x86_64-unknown-linux-gnu
bundles: deb
ext: deb
asset: linux-x64
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
- name: Install the Linux build dependencies
if: runner.os == 'Linux'
run: |
set -euo pipefail
# Retried: a degraded mirror otherwise burns the whole job and
# reports "cancelled", which reads as our failure rather than apt's.
for attempt in 1 2 3; do
sudo apt-get update && break || sleep 15
done
sudo apt-get install -y --no-install-recommends \
libwebkit2gtk-4.1-dev \
libappindicator3-dev \
librsvg2-dev \
libxdo-dev \
libssl-dev \
patchelf \
xdg-utils
# libappindicator3-dev, not the ayatana package: the .deb's generated
# Depends: follows whichever one the binary linked against, so the
# two must not be mixed between builds.
- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- uses: swatinem/rust-cache@v2
with:
workspaces: src/praisonai-desktop/src-tauri -> target
# Saving the Windows target directory costs more minutes than the
# rebuild it saves.
save-if: ${{ runner.os != 'Windows' }}
- name: Install the Tauri CLI
shell: bash
# `|| true` swallowed a genuine install failure and the next step then
# died with an unrelated "no such command: tauri". It also behaved
# differently per leg, since Windows defaults to pwsh where `true` is
# not a command. Skip the install if it is already there; otherwise
# let a failure be a failure.
run: command -v cargo-tauri >/dev/null || cargo install tauri-cli --version '^2' --locked
- name: Build the bundle
working-directory: src/praisonai-desktop/src-tauri
run: cargo tauri build --target ${{ matrix.target }} --bundles ${{ matrix.bundles }}
- name: Name the artifact for the platform it runs on
id: artifact
shell: bash
working-directory: src/praisonai-desktop/src-tauri
run: |
set -euo pipefail
tag="${{ github.event.release.tag_name || inputs.tag }}"
root="target/${{ matrix.target }}/release/bundle"
found="$(find "$root" -name "*.${{ matrix.ext }}" -print -quit || true)"
# Fail loudly rather than uploading nothing: an empty release asset
# list is exactly the state this workflow exists to fix, and a bundler
# given a target it does not support exits zero having written none.
if [ -z "$found" ]; then
echo "no .${{ matrix.ext }} was produced under $root"
find "$root" -type f -print || echo "(the bundle directory does not exist)"
exit 1
fi
out="PraisonAI-${tag}-${{ matrix.asset }}.${{ matrix.ext }}"
mv "$found" "../$out"
echo "path=src/praisonai-desktop/$out" >> "$GITHUB_OUTPUT"
- name: Attach it to the release
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
gh release upload \
"${{ github.event.release.tag_name || inputs.tag }}" \
"${{ steps.artifact.outputs.path }}" --clobber
verify:
# A release with a missing platform is worse than an obviously failed
# build, because nobody notices. This says which one is absent.
needs: build
if: always()
runs-on: ubuntu-latest
steps:
- name: Check every platform produced an asset
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ github.event.release.tag_name || inputs.tag }}
run: |
set -euo pipefail
assets="$(gh release view "$TAG" --repo "$GITHUB_REPOSITORY" \
--json assets -q '.assets[].name')"
echo "$assets"
missing=""
for want in macos-apple-silicon macos-intel windows-x64-setup linux-x64; do
echo "$assets" | grep -q -- "$want" || missing="$missing $want"
done
if [ -n "$missing" ]; then
echo "::error::the release is missing:$missing"
exit 1
fi
echo "all four platform builds are attached"
+115
View File
@@ -0,0 +1,115 @@
# The desktop app's tests.
#
# These existed and ran nowhere: no workflow referenced praisonai-desktop, and
# the frontend suite could not even be installed -- jsdom was undeclared, so it
# ran only on machines that happened to have one further up the tree, at
# whatever version that happened to be.
name: desktop
on:
push:
branches: [main]
paths: ['src/praisonai-desktop/**', '.github/workflows/desktop.yml',
'.github/workflows/desktop-release.yml']
pull_request:
paths: ['src/praisonai-desktop/**', '.github/workflows/desktop.yml',
'.github/workflows/desktop-release.yml']
concurrency:
group: desktop-${{ github.ref }}
cancel-in-progress: true
defaults:
run:
working-directory: src/praisonai-desktop
jobs:
engine:
name: engine (${{ matrix.os }})
# The engine is the part that has to behave the same on all three, which is
# the entire point of the portability suite -- so it is run on all three.
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, macos-15, windows-latest]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Run the engine tests
shell: bash
env:
# Never touch a real keychain on a shared runner.
PRAISONAI_KEYCHAIN_SERVICE: ai.praison.desktop.ci
run: |
set -euo pipefail
failed=0
for suite in engine/test_*.py; do
echo "::group::$suite"
python "$suite" -v || failed=1
echo "::endgroup::"
done
exit $failed
frontend:
name: frontend
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
- run: npm ci
# `test:ci` is every suite except lifecycle, which drives real
# conversations against a live engine and a real model -- neither of
# which exists on a runner. It is excluded by name rather than left to
# skip quietly, so that a skipped test here is a genuine problem.
- name: Run the UI tests
# A suite that skips everything still exits 0 -- that is how thirteen
# tests came to look like a pass while proving nothing. So the skip
# count is asserted, not just the exit status.
run: |
set -euo pipefail
set -o pipefail
npm run test:ci 2>&1 | tee "$RUNNER_TEMP/ui.log"
skipped=$(grep -E '^. skipped ' "$RUNNER_TEMP/ui.log" | grep -oE '[0-9]+$' | head -1)
echo "skipped: ${skipped:-unknown}"
test "${skipped:-1}" -eq 0
shell:
name: shell (${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, macos-15, windows-latest]
defaults:
run:
working-directory: src/praisonai-desktop/src-tauri
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
with:
workspaces: src/praisonai-desktop/src-tauri
# The Windows cache is large and slow enough to cost more than it saves.
save-if: ${{ runner.os != 'Windows' }}
- name: Install the Linux build dependencies
if: runner.os == 'Linux'
working-directory: .
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev \
libxdo-dev libssl-dev patchelf
- name: Test
# Debug info is the bulk of a Tauri target directory and nothing here
# reads a backtrace, so building without it keeps the runner's disk
# from filling on the Windows leg.
run: cargo test --config 'profile.test.debug=false' --config 'profile.dev.debug=false'
- name: Clippy
run: cargo clippy --all-targets -- -D warnings
+2 -1
View File
@@ -101,8 +101,9 @@ jobs:
${{ github.workspace }}/src/praisonai-agents:${{ github.workspace }}/src/praisonai-code
- shard: train
workdir: src/praisonai-train
# tests/unit/data/ (96 tests) was collected by no workflow at all.
paths: >-
tests/unit/train/
tests/unit/train/ tests/unit/data/
extra_ignore: ""
pythonpath: >-
${{ github.workspace }}/src/praisonai-agents:${{ github.workspace }}/src/praisonai-code
+13
View File
@@ -7,8 +7,21 @@ Thank you for your interest! Here's how to get started:
3. Make your changes
4. Submit a pull request
## Where to report issues (hub intake)
| Topic | Report here | Where fixes land |
|-------|-------------|------------------|
| Python SDK, CLI, wrapper | **This repo** ([PraisonAI](https://github.com/MervinPraison/PraisonAI)) | `src/praisonai-agents/`, `src/praisonai/` |
| TypeScript / JavaScript SDK (`npm install praisonai`) | **This repo** (preferred) or [praisonai-js](https://github.com/MervinPraison/praisonai-js) | [praisonai-js](https://github.com/MervinPraison/praisonai-js) → synced to `src/praisonai-ts/` |
| Cross-language parity | **This repo** | Often both Python and praisonai-js PRs |
| Agent-callable tools | **This repo** | [PraisonAI-Tools](https://github.com/MervinPraison/PraisonAI-Tools) |
| Lifecycle plugins (tracing, hooks, guardrails) | **This repo** | [PraisonAI-Plugins](https://github.com/MervinPraison/PraisonAI-Plugins) |
You do **not** need to re-file TypeScript issues on praisonai-js — report on PraisonAI and automation routes fixes to the canonical TS repo.
## Guidelines
- Follow existing code style
- Add tests for new features
- Update documentation as needed
- TypeScript changes: implement in [praisonai-js](https://github.com/MervinPraison/praisonai-js), then run **Sync to PraisonAI Monorepo** workflow to update `src/praisonai-ts/`
+3 -1
View File
@@ -8,7 +8,9 @@ PraisonAI takes security seriously. We appreciate responsible disclosure from re
|---------|----------|-------------|
| `praisonaiagents` | [PyPI](https://pypi.org/project/praisonaiagents/) | `src/praisonai-agents/` |
| `praisonai` | [PyPI](https://pypi.org/project/praisonai/) | `src/praisonai/` |
| `praisonai` | [npm](https://www.npmjs.com/package/praisonai) | `src/praisonai-ts/` |
| `praisonai` | [npm](https://www.npmjs.com/package/praisonai) | [praisonai-js](https://github.com/MervinPraison/praisonai-js) (canonical); mirrored at `src/praisonai-ts/` |
Report npm/TypeScript vulnerabilities on **PraisonAI** (hub intake) or [praisonai-js](https://github.com/MervinPraison/praisonai-js). Fixes land in praisonai-js first.
Report issues against the **specific package** where the vulnerability exists. A platform-only issue does not necessarily affect the core SDK, and vice versa.
+5 -5
View File
@@ -178,19 +178,19 @@ def example_url_parsing():
print("\n=== Example 9: URL Parsing ===")
# Memory supports URL connection strings
# Supported: postgresql://, redis://, sqlite:///, mongodb://
# Supported schemes: sqlite://, mongodb://, mongodb+srv://
agent = Agent(
instructions="You are a database-backed assistant",
memory="postgresql://postgres:password@localhost:5432/praisonai",
memory="mongodb://user:password@localhost:27017/praisonai",
)
print("Memory URL parsed: memory enabled")
# Also works with presets
agent2 = Agent(
instructions="You are a redis-backed assistant",
memory="redis", # Preset name
instructions="You are a sqlite-backed assistant",
memory="sqlite", # Preset name
)
print("Memory preset 'redis': configured")
print("Memory preset 'sqlite': configured")
def example_knowledge_sources():
+4 -4
View File
@@ -8,15 +8,15 @@ agent = Agent(
)
# With preset
agent_redis = Agent(
agent_sqlite = Agent(
instructions="You are a helpful assistant.",
memory="redis", # Uses redis preset
memory="sqlite", # Uses sqlite preset
)
# With URL
agent_postgres = Agent(
agent_mongo = Agent(
instructions="You are a helpful assistant.",
memory="postgresql://localhost/mydb", # URL auto-detected
memory="mongodb://localhost/mydb", # URL auto-detected
)
if __name__ == "__main__":
+43
View File
@@ -1,5 +1,32 @@
# MCP Server Examples
## Install (SHA-256)
Pin GitHub Release **v0.6.0** and verify `SHA256SUMS`. Website `install.sh` / `install.ps1` abort on mismatch.
https://github.com/LinespottingOrg/GrokBuildRemote-Agents/releases/tag/v0.6.0
https://github.com/LinespottingOrg/GrokBuildRemote-Agents/blob/main/docs/PINNED-INSTALL.md
```
96cef605d3e030ccef99d27ea6240e0d3b668dd045e6b5b9e585c9fd03c6ef23 gbr-agent-darwin-amd64
de7e065ef2cf6877b3b2cd04679a67b627f876337f529247e236204543e4062c gbr-agent-darwin-arm64
a50a5c41993e6531a3b477eb409ccc845212bf541384dc803061c80657f86719 gbr-agent-linux-amd64
5bfd22c7110234942c4c02ff8154b836d0af45a9422c178a4f52010187d40061 gbr-agent-linux-arm64
f773b89fd31310172b756e0593e0f3b2382b0a3440af2a7d0a8b3073b0c23e27 gbr-agent-windows-amd64.exe
8fb9efcbc7e2ac91c11964944bf0f45e31bb23f4356d9dcb4b305d7cb9b0fe8c gbr-agent-windows-arm64.exe
```
```bash
VER=v0.6.0
BASE=https://github.com/LinespottingOrg/GrokBuildRemote-Agents/releases/download/$VER
# swap darwin-arm64 for your OS/arch
curl -fsSL -o gbr-agent-darwin-arm64 "$BASE/gbr-agent-darwin-arm64"
curl -fsSL -o SHA256SUMS "$BASE/SHA256SUMS"
shasum -a 256 -c SHA256SUMS --ignore-missing
gbr-agent pair && gbr-agent run
```
Examples demonstrating the MCP Server v2 features per MCP Protocol Version 2025-11-25.
## Features Demonstrated
@@ -130,3 +157,19 @@ These examples comply with MCP Protocol Version 2025-11-25:
- `destructiveHint`: true
- `idempotentHint`: false
- `openWorldHint`: true
## Build Remote Agent (`gbr_pair.py`)
Pair a phone running [Build Remote Agent](https://grokbuildremote.com/) to a PraisonAI agent via MCP stdio `gbr-mcp` (protocol `gbr/1`). Requires `gbr-agent pair && gbr-agent run`. Phone is spectator. Never put mailbox keys in the example.
```bash
python gbr_pair.py
```
## What the phone sees
**Terminal windows** on this PC (machine-wide mailbox). Not headless OpenCode / CodeNomad sidecar / Electron. `:8788` in a sidecar is Bot API JSON, not a transcript.
https://github.com/LinespottingOrg/GrokBuildRemote-Agents/blob/main/docs/WHAT-THE-PHONE-SEES.md
https://grokbuildremote.com/integrations.html
+76
View File
@@ -0,0 +1,76 @@
"""
Build Remote Agent pairing example.
Pair a phone (Build Remote Agent) to this PraisonAI session through the
free MIT gbr-agent. Protocol gbr/1. Phone is spectator + veto.
Requirements:
- gbr-agent v0.6.0+ (`gbr-agent pair && gbr-agent run`)
- GrokBuildRemote-Agents cloned; `npm install` in mcp/gbr-mcp
- pip install praisonaiagents[mcp]
Usage:
python gbr_pair.py
Never put mailbox keys in this file. Phone Settings → Bot API is the
only place the relay key is copied.
Independent product by Linespotting AB. Not affiliated with xAI or SpaceX.
https://grokbuildremote.com/
"""
from praisonaiagents import Agent, MCP
# Clone https://github.com/LinespottingOrg/GrokBuildRemote-Agents.git
## Install (SHA-256)
Pin GitHub Release **v0.6.0** and verify `SHA256SUMS`. Website `install.sh` / `install.ps1` abort on mismatch.
https://github.com/LinespottingOrg/GrokBuildRemote-Agents/releases/tag/v0.6.0
https://github.com/LinespottingOrg/GrokBuildRemote-Agents/blob/main/docs/PINNED-INSTALL.md
```
96cef605d3e030ccef99d27ea6240e0d3b668dd045e6b5b9e585c9fd03c6ef23 gbr-agent-darwin-amd64
de7e065ef2cf6877b3b2cd04679a67b627f876337f529247e236204543e4062c gbr-agent-darwin-arm64
a50a5c41993e6531a3b477eb409ccc845212bf541384dc803061c80657f86719 gbr-agent-linux-amd64
5bfd22c7110234942c4c02ff8154b836d0af45a9422c178a4f52010187d40061 gbr-agent-linux-arm64
f773b89fd31310172b756e0593e0f3b2382b0a3440af2a7d0a8b3073b0c23e27 gbr-agent-windows-amd64.exe
8fb9efcbc7e2ac91c11964944bf0f45e31bb23f4356d9dcb4b305d7cb9b0fe8c gbr-agent-windows-arm64.exe
```
```bash
VER=v0.6.0
BASE=https://github.com/LinespottingOrg/GrokBuildRemote-Agents/releases/download/$VER
# swap darwin-arm64 for your OS/arch
curl -fsSL -o gbr-agent-darwin-arm64 "$BASE/gbr-agent-darwin-arm64"
curl -fsSL -o SHA256SUMS "$BASE/SHA256SUMS"
shasum -a 256 -c SHA256SUMS --ignore-missing
gbr-agent pair && gbr-agent run
```
# then point args at mcp/gbr-mcp/bin/gbr-mcp.js (loopback stdio).
gbr = Agent(
instructions=(
"You can attach a phone spectator via Build Remote Agent. "
"Use GBR tools only to diagnose or attach the local Bot API "
"at 127.0.0.1:8788. Do not request mailbox keys."
),
llm="gpt-4o-mini",
tools=MCP(
"node",
args=["GrokBuildRemote-Agents/mcp/gbr-mcp/bin/gbr-mcp.js"],
),
)
if __name__ == "__main__":
gbr.start(
"Check Build Remote Agent health on 127.0.0.1:8788 and list sessions."
)
## What the phone sees
**Terminal windows** on this PC (machine-wide mailbox). Not headless OpenCode / CodeNomad sidecar / Electron. `:8788` in a sidecar is Bot API JSON, not a transcript.
https://github.com/LinespottingOrg/GrokBuildRemote-Agents/blob/main/docs/WHAT-THE-PHONE-SEES.md
https://grokbuildremote.com/integrations.html
@@ -1,16 +1,18 @@
"""
Qdrant Knowledge Store - Agent-First Example
Chroma Knowledge Store - Agent-First Example
Demonstrates using Qdrant for knowledge-based RAG with an Agent.
Demonstrates using Chroma (a shipped vector store) for knowledge-based RAG
with an Agent.
To use a provider that is not shipped (e.g. Qdrant), register an adapter
first with ``register_knowledge_adapter("qdrant", MyQdrantAdapter)`` and then
pass ``"provider": "qdrant"``.
Requirements:
pip install "praisonai[tools]"
Docker Setup:
docker run -d --name praison-qdrant -p 6333:6333 qdrant/qdrant
Run:
python knowledge_qdrant.py
python knowledge_chroma.py
Expected Output:
Agent responds using knowledge from documents
@@ -44,17 +46,16 @@ doc_path = os.path.join(temp_dir, "guide.txt")
with open(doc_path, "w") as f:
f.write(sample_doc)
print("=== Qdrant Knowledge Store Demo (Agent-First) ===")
print("=== Chroma Knowledge Store Demo (Agent-First) ===")
# Agent-first approach: use knowledge parameter with Qdrant
# Agent-first approach: use knowledge parameter with a shipped vector store
agent = Agent(
name="KnowledgeAssistant",
instructions="You are a helpful assistant with access to technical documentation.",
knowledge={
"sources": [doc_path],
"vector_store": {
"provider": "qdrant",
"url": "http://localhost:6333",
"provider": "chroma",
},
}
)
@@ -70,4 +71,4 @@ print("\n=== Demo Complete ===")
# --- Advanced: Direct Store Usage ---
# from praisonai.persistence.factory import create_knowledge_store
# store = create_knowledge_store("qdrant", url="http://localhost:6333")
# store = create_knowledge_store("chroma")
@@ -0,0 +1,34 @@
"""
Basic example of using EvoLink.AI with PraisonAI.
"""
import os
from praisonaiagents import Agent
api_key = os.getenv("EVOLINK_API_KEY")
if not api_key:
raise RuntimeError(
"EVOLINK_API_KEY is not set. Set it before running this example: "
"export EVOLINK_API_KEY=your_api_key"
)
agent = Agent(
instructions="You are a helpful assistant",
llm={
"model": "openai/gpt-5.2",
"api_key": api_key,
"base_url": "https://direct.evolink.ai/v1",
},
)
response = agent.start("Hello! Share one practical AI automation idea.")
print(response)
coding_task = """
Write a Python function that validates whether a string is a palindrome.
Include a short docstring and ignore spaces and capitalization.
"""
response = agent.start(coding_task)
print(response)
+14 -10
View File
@@ -1,17 +1,25 @@
"""Qdrant Vector Store - Agent-First Example (requires Docker)
"""Qdrant Vector Store - Agent-First Example
Docker: docker run -d --name qdrant -p 6333:6333 qdrant/qdrant
Qdrant is not a shipped adapter. Register one first, then use it via the
``knowledge`` parameter. If no adapter is registered this example skips
cleanly instead of crashing.
from praisonaiagents.knowledge.adapters import register_knowledge_adapter
register_knowledge_adapter("qdrant", MyQdrantAdapter)
"""
import os
import sys
from praisonaiagents import Agent
from praisonaiagents.knowledge.adapters import list_knowledge_adapters
# Agent-first approach: use knowledge parameter with Qdrant
url = os.getenv("QDRANT_URL", "http://localhost:6333")
if "qdrant" not in list_knowledge_adapters():
print("SKIPPED: Qdrant - no 'qdrant' adapter registered")
sys.exit(0)
# Agent-first approach: use knowledge parameter with a registered Qdrant adapter
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant with access to documents.",
knowledge={"sources": ["./docs/guide.pdf"], "vector_store": {"provider": "qdrant", "url": url}}
knowledge={"sources": ["./docs/guide.pdf"], "vector_store": {"provider": "qdrant"}}
)
# Chat - agent uses knowledge for RAG
@@ -19,7 +27,3 @@ response = agent.chat("What information do you have?")
print(f"Response: {response}")
print("PASSED: Qdrant with Agent")
# --- Advanced: Direct Store Usage ---
# from praisonai.persistence import create_knowledge_store
# store = create_knowledge_store("qdrant", url=url)
+16 -16
View File
@@ -18,21 +18,21 @@ This report compares **Python SDK feature categories** against **Python document
| Category | Features | Docs | Lines |
|----------|----------|------|-------|
| ✅ AGUI | 1 | 2 | 420 |
| ✅ Agent | 22 | 53 | 17053 |
| ✅ Agent | 22 | 53 | 17107 |
| ✅ Agent-to-Agent (A2A) | 1 | 7 | 2996 |
| ✅ Agent-to-User (A2U) | 1 | 3 | 689 |
| ✅ Approval | 1 | 5 | 2658 |
| ✅ Approval | 1 | 5 | 2764 |
| ✅ Audio | 2 | 12 | 1000 |
| ✅ Auto Generation | 5 | 11 | 3542 |
| ✅ Autonomy | 3 | 7 | 2790 |
| ✅ Bots | 7 | 31 | 13411 |
| ✅ Bots | 7 | 32 | 13616 |
| ✅ Budget | 1 | 1 | 287 |
| ✅ CLI | 5 | 122 | 36734 |
| ✅ CLI | 5 | 122 | 36991 |
| ✅ Chunking | 2 | 2 | 420 |
| ✅ Citations | 2 | 1 | 202 |
| ✅ Code Execution | 2 | 14 | 4855 |
| ✅ Conditions | 1 | 3 | 1347 |
| ✅ Configuration | 3 | 8 | 3139 |
| ✅ Configuration | 3 | 8 | 3173 |
| ✅ Context Management | 16 | 36 | 13988 |
| ✅ Deep Research | 8 | 2 | 587 |
| ✅ Display | 6 | 3 | 741 |
@@ -43,16 +43,16 @@ This report compares **Python SDK feature categories** against **Python document
| ✅ Failover | 2 | 1 | 452 |
| ✅ Files | 2 | 7 | 2275 |
| ✅ Flow | 1 | 3 | 788 |
| ✅ Gateway | 7 | 81 | 29955 |
| ✅ Guardrails | 4 | 4 | 2205 |
| ✅ Gateway | 7 | 82 | 30269 |
| ✅ Guardrails | 4 | 4 | 2228 |
| ✅ Handoffs | 11 | 6 | 2626 |
| ✅ Hooks | 2 | 9 | 4509 |
| ✅ Image | 1 | 11 | 1255 |
| ✅ Knowledge | 4 | 16 | 4942 |
| ✅ Knowledge | 4 | 16 | 4967 |
| ✅ LLM | 3 | 15 | 5372 |
| ✅ Loops | 4 | 5 | 1475 |
| ✅ MCP | 1 | 61 | 14135 |
| ✅ Memory | 6 | 18 | 7187 |
| ✅ MCP | 1 | 61 | 14160 |
| ✅ Memory | 6 | 18 | 7193 |
| ✅ OCR | 2 | 1 | 237 |
| ✅ Observability | 2 | 23 | 2811 |
| ✅ Optimizer | 1 | 2 | 792 |
@@ -66,16 +66,16 @@ This report compares **Python SDK feature categories** against **Python document
| ✅ RAG | 5 | 15 | 3118 |
| ✅ Realtime | 2 | 5 | 668 |
| ✅ Reflection | 3 | 3 | 730 |
| ✅ Retrieval | 2 | 5 | 1054 |
| ✅ Retrieval | 2 | 5 | 1042 |
| ✅ Routing | 1 | 2 | 430 |
| ✅ Sandbox | 5 | 7 | 3483 |
| ✅ Security | 1 | 3 | 3482 |
| ✅ Sessions | 4 | 19 | 7631 |
| ✅ Sandbox | 5 | 7 | 3556 |
| ✅ Security | 1 | 3 | 3529 |
| ✅ Sessions | 4 | 19 | 7644 |
| ✅ Skills | 6 | 15 | 5638 |
| ✅ Tasks | 2 | 6 | 2897 |
| ✅ Telemetry | 1 | 2 | 648 |
| ✅ Templates | 1 | 8 | 1663 |
| ✅ Tools | 12 | 144 | 43113 |
| ✅ Tools | 12 | 144 | 43251 |
| ✅ Tracing | 3 | 2 | 139 |
| ✅ Vector Store | 1 | 12 | 1215 |
| ✅ Video | 2 | 6 | 649 |
@@ -87,7 +87,7 @@ This report compares **Python SDK feature categories** against **Python document
These docs exist but don't match any implemented feature category:
- ℹ️ Database (42 docs, 6959 lines)
- ℹ️ Database (42 docs, 6965 lines)
- ℹ️ Documents (1 docs, 787 lines)
---
@@ -197,75 +197,6 @@ def _validate_preset_params(params):
validate_preset_string(name, value, _PRESET_STRING_PARAMS[name]())
class ServerRegistry:
"""Registry for API server state per-port."""
def __init__(self):
self._lock = threading.Lock()
self._server_started = {} # Dict of port -> started boolean
self._registered_agents = {} # Dict of port -> Dict of path -> agent_id
self._shared_apps = {} # Dict of port -> FastAPI app
# Class-level lock for thread-safe singleton creation
_instance_lock = threading.Lock()
@staticmethod
def get_default_instance():
"""Get default global registry for backward compatibility."""
if not hasattr(ServerRegistry, '_default_instance'):
# Double-checked locking pattern with shared class-level lock
with ServerRegistry._instance_lock:
if not hasattr(ServerRegistry, '_default_instance'):
ServerRegistry._default_instance = ServerRegistry()
return ServerRegistry._default_instance
def is_server_started(self, port: int) -> bool:
with self._lock:
return self._server_started.get(port, False)
def set_server_started(self, port: int, started: bool) -> None:
with self._lock:
self._server_started[port] = started
def get_shared_app(self, port: int):
with self._lock:
return self._shared_apps.get(port)
def set_shared_app(self, port: int, app) -> None:
with self._lock:
self._shared_apps[port] = app
def register_agent(self, port: int, path: str, agent_id: str) -> None:
with self._lock:
if port not in self._registered_agents:
self._registered_agents[port] = {}
self._registered_agents[port][path] = agent_id
def get_registered_agents(self, port: int) -> dict:
with self._lock:
return self._registered_agents.get(port, {}).copy()
def cleanup_agent_registrations(self, agent_id: str) -> None:
"""Remove all registrations for an agent ID and clean empty port state."""
with self._lock:
ports_to_clean = []
for port, path_dict in self._registered_agents.items():
paths_to_remove = [path for path, registered_id in path_dict.items() if registered_id == agent_id]
for path in paths_to_remove:
del path_dict[path]
if not path_dict:
ports_to_clean.append(port)
for port in ports_to_clean:
self._registered_agents.pop(port, None)
self._server_started.pop(port, None)
# Backward compatibility - use default instance
def _get_default_server_registry() -> ServerRegistry:
return ServerRegistry.get_default_instance()
# Don't import FastAPI dependencies here - use lazy loading instead
if TYPE_CHECKING:
@@ -870,9 +801,28 @@ class Agent(GoalLoopMixin, SteeringMixin, SandboxMixin, SkillReviewMixin, Unifie
_cloned_fallback_models = legacy_kwargs.pop("fallback_models", None)
_unknown = set(legacy_kwargs) - _legacy_defaults.keys()
if _unknown:
# Unknown kwargs are rejected rather than swallowed, so a typo can
# never silently disable the behaviour it was meant to configure.
# Some names are near-universal in this ecosystem, though -- and
# `verbose` is accepted by six sibling classes here -- so name the
# supported alternative instead of leaving the user to search a
# 41-parameter signature for something that is not in it.
_HINTS = {
"verbose": (
"verbosity moved into output=; use output='verbose' "
"(or output=OutputConfig(verbose=True))."
),
"stream": (
"streaming moved into output=; use "
"output=OutputConfig(stream=True)."
),
}
_hint = "".join(
f"\n {name}: {_HINTS[name]}" for name in sorted(_unknown) if name in _HINTS
)
raise TypeError(
f"Agent.__init__() got unexpected keyword argument(s): "
f"{', '.join(sorted(_unknown))}"
f"{', '.join(sorted(_unknown))}{_hint}"
)
allow_delegation = legacy_kwargs.get("allow_delegation", _legacy_defaults["allow_delegation"])
allow_code_execution = legacy_kwargs.get("allow_code_execution", _legacy_defaults["allow_code_execution"])
@@ -2574,6 +2524,17 @@ Your Goal: {self.goal}
# Store permissions if provided (for CI-safe declarative policies)
self._approval_permissions = getattr(approval, 'permissions', None)
self._permission_mode = getattr(approval, 'permission_mode', None)
# Configuring approval must never be a WEAKER posture than passing
# nothing. When no declarative ``permissions`` policy is supplied,
# fall back to the same env-driven default deny set the bare
# ``approval is None`` path uses, so e.g. ``ApprovalConfig(timeout=30)``
# keeps the default denials (execute_command, delete_file, …) instead
# of silently dropping all of them. ``is None`` (not falsiness) so an
# explicit empty policy (``permissions={}``) is still an intentional
# policy that owns denial — only a genuinely absent policy inherits
# the env-driven default deny set.
if self._approval_permissions is None:
self._perm_deny = self._resolve_default_deny_set()
elif isinstance(approval, dict):
# Dict config: convert to ApprovalConfig
approval_config = ApprovalConfig(**approval)
@@ -2582,6 +2543,12 @@ Your Goal: {self.goal}
self._approval_timeout = approval_config.timeout
self._approval_permissions = getattr(approval_config, 'permissions', None)
self._permission_mode = getattr(approval_config, 'permission_mode', None)
# See the ApprovalConfig branch above: never weaken the default
# posture just because a config object was passed. ``is None`` so an
# explicit empty policy (``approval={"permissions": {}}``) still owns
# denial rather than inheriting the default deny set.
if self._approval_permissions is None:
self._perm_deny = self._resolve_default_deny_set()
else:
# Plain backend object — dangerous tools only, backend default timeout
self._approval_backend = approval
@@ -5833,6 +5800,14 @@ Summary:"""
logging.warning(f"Memory provider '{memory}' requires additional dependencies. Falling back to FileMemory.")
from ..memory.file_memory import FileMemory
self._memory_instance = FileMemory(user_id=mem_user_id)
except Exception as e:
# The backend package is installed but could not initialise
# (e.g. a SaaS backend like mem0 needs credentials/network).
# Honour the same "Falling back to FileMemory" contract used for
# missing dependencies so construction never hard-crashes.
logging.warning(f"Memory provider '{memory}' could not initialise ({e}). Falling back to FileMemory.")
from ..memory.file_memory import FileMemory
self._memory_instance = FileMemory(user_id=mem_user_id)
elif isinstance(memory, dict):
# Configuration dict
provider = memory.get("provider", memory.get("backend", "file"))
@@ -6431,6 +6406,31 @@ Answer:"""
except Exception:
return False
@staticmethod
def _resolve_default_deny_set() -> frozenset:
"""Return the env-driven default permission deny set.
Mirrors the ``approval is None`` path: honour ``PRAISONAI_TOOL_SAFETY``
(``off``/``full``/``none``/``0``/``false`` → no denials), otherwise use
the named preset (defaulting to ``"default"``, which blocks destructive
ops like ``execute_command``/``delete_file``/``kill_process``/
``execute_code``). An unknown env value falls back to ``"default"``.
Shared by the ``ApprovalConfig``/``dict`` branches so configuring
approval never silently drops the default denials — passing a config
object must not be a weaker posture than passing nothing.
"""
_raw_safety_env = os.environ.get("PRAISONAI_TOOL_SAFETY")
_safety_env = (_raw_safety_env or "").strip().lower()
if _safety_env in ("off", "full", "none", "0", "false"):
return frozenset()
from ..approval.registry import PERMISSION_PRESETS
_resolved = _safety_env or "default"
_preset = PERMISSION_PRESETS.get(_resolved)
if _preset is None:
_preset = PERMISSION_PRESETS.get("default")
return _preset if _preset is not None else frozenset()
def _setup_guardrail(self):
"""Setup the guardrail function based on the provided guardrail parameter."""
# ``tool_execution._check_tool_policy_and_guardrails`` reads
@@ -6648,7 +6648,7 @@ Answer:"""
else:
return False, None, guardrail_result.error
def _apply_guardrail_with_retry(self, response_text, prompt, temperature=1.0, tools=None, task_name=None, task_description=None, task_id=None):
def _apply_guardrail_with_retry(self, response_text, prompt, temperature=1.0, tools=None, task_name=None, task_description=None, task_id=None, cancel_token=None):
"""Apply guardrail validation with retry logic (sync version)."""
retry_count = 0
current_response = response_text
@@ -6689,7 +6689,7 @@ Answer:"""
# Regenerate response for retry
try:
retry_prompt = f"{prompt}\n\nNote: Previous response failed validation due to: {error}. Please provide an improved response."
response = self._chat_completion([{"role": "user", "content": retry_prompt}], temperature, tools, task_name=task_name, task_description=task_description, task_id=task_id)
response = self._chat_completion([{"role": "user", "content": retry_prompt}], temperature, tools, task_name=task_name, task_description=task_description, task_id=task_id, cancel_token=cancel_token)
if response and response.choices:
content = response.choices[0].message.content
current_response = content.strip() if content else ""
@@ -6864,8 +6864,12 @@ Answer:"""
if AgentRuntimeConfig and isinstance(runtime, AgentRuntimeConfig):
return runtime
# If already a RuntimeConfig instance, return as-is
# If already a RuntimeConfig instance, normalise its runtime name so a
# typo in preferred_runtime (e.g. "nativ") raises instead of silently
# dropping capabilities. resolve_runtime returns a normalised copy.
if RuntimeConfig and hasattr(RuntimeConfig, '__name__') and isinstance(runtime, RuntimeConfig):
if resolve_runtime:
return resolve_runtime(runtime)
return runtime
# Handle capability validation style (bool, RuntimeConfig)
@@ -6874,8 +6878,13 @@ Answer:"""
result = resolve_runtime(runtime)
if result is not None:
return result
except (TypeError, ValueError):
pass # Try AgentRuntimeConfig path
except ValueError:
# An invalid runtime *name* (e.g. a typo) must surface rather
# than falling through and being stored verbatim, which would
# silently drop most capabilities.
raise
except TypeError:
pass # Not this style; try AgentRuntimeConfig path
# Handle turn-based style (AgentRuntimeConfig)
if AgentRuntimeConfig:
@@ -6930,16 +6939,17 @@ Answer:"""
else:
raise TypeError(f"Invalid capability type: {type(cap)}")
# Determine runtime name
runtime_name = getattr(self._runtime_config, 'preferred_runtime', 'native')
# Determine runtime name. Normalise so spelling variants (NATIVE,
# " native ", native) map to the same matrix instead of silently
# dropping capabilities; an unknown name raises rather than degrading.
raw_runtime = getattr(self._runtime_config, 'preferred_runtime', None) or 'native'
from ..config.feature_configs import canonical_runtime_name
runtime_name = canonical_runtime_name(raw_runtime)
# Get runtime capabilities based on runtime type
if runtime_name == 'native':
runtime_matrix = get_native_runtime_capabilities()
elif runtime_name in ('plugin-harness', 'harness', 'plugin', 'reduced'):
runtime_matrix = get_reduced_harness_capabilities()
else:
# Unknown runtime, use reduced capabilities as safe default
runtime_matrix = get_reduced_harness_capabilities()
# Validate capabilities
@@ -7442,8 +7452,7 @@ Answer:"""
try:
# Tear down the routes launch() actually registered (module-level
# state in execution_mixin.py). ServerRegistry above is a separate,
# unpopulated structure and cleaning it up is a no-op.
# state in execution_mixin.py).
from .execution_mixin import cleanup_launch_registration
cleanup_launch_registration(self._agent_id)
@@ -1744,7 +1744,7 @@ Your Goal: {self.goal}"""
return max_retries
return 2
def _chat_completion(self, messages, temperature=None, tools=None, stream=None, reasoning_steps=False, task_name=None, task_description=None, task_id=None, response_format=None, _retry_depth=0, _fallback_index=0):
def _chat_completion(self, messages, temperature=None, tools=None, stream=None, reasoning_steps=False, task_name=None, task_description=None, task_id=None, response_format=None, _retry_depth=0, _fallback_index=0, cancel_token=None):
start_time = time.time()
# --- Proactive Context Budget Management (default-on) ---
@@ -1849,6 +1849,7 @@ Your Goal: {self.goal}"""
# First attempt: try with streaming enabled for better user experience
stream_callback = self.stream_emitter.emit if hasattr(self, 'stream_emitter') else None
streaming_response = self._chat_completion_with_retry(
cancel_token=cancel_token,
messages=messages,
temperature=temperature,
tools=formatted_tools,
@@ -1892,6 +1893,7 @@ Your Goal: {self.goal}"""
final_response = streaming_response
else:
final_response = self._chat_completion_with_retry(
cancel_token=cancel_token,
messages=messages,
temperature=temperature,
tools=formatted_tools,
@@ -2043,7 +2045,8 @@ Your Goal: {self.goal}"""
truncated_messages, temperature, tools, stream,
reasoning_steps, task_name, task_description, task_id, response_format,
_retry_depth=_retry_depth + 1,
_fallback_index=_fallback_index
_fallback_index=_fallback_index,
cancel_token=cancel_token
)
except Exception as compression_error:
logging.error(f"[{self.name}] Context compression failed: {compression_error}")
@@ -2089,7 +2092,8 @@ Your Goal: {self.goal}"""
messages, temperature, tools, stream,
reasoning_steps, task_name, task_description, task_id, response_format,
_retry_depth=_retry_depth + 1,
_fallback_index=_fallback_index + 1
_fallback_index=_fallback_index + 1,
cancel_token=cancel_token
)
finally:
self.llm = original_llm
@@ -2106,7 +2110,8 @@ Your Goal: {self.goal}"""
messages, temperature, tools, stream,
reasoning_steps, task_name, task_description, task_id, response_format,
_retry_depth=_retry_depth + 1,
_fallback_index=_fallback_index
_fallback_index=_fallback_index,
cancel_token=cancel_token
)
# Include remediation hints for unimplemented recovery actions
@@ -2345,6 +2350,7 @@ Your Goal: {self.goal}"""
response_format=None,
stream_callback=None,
emit_events=True,
cancel_token=None,
):
"""
Execute unified chat completion using composition instead of runtime class mutation.
@@ -2377,6 +2383,7 @@ Your Goal: {self.goal}"""
if stream_callback is None and hasattr(self, 'stream_emitter'):
stream_callback = getattr(self.stream_emitter, 'emit', None)
final_response = self._unified_dispatcher.chat_completion(
cancel_token=cancel_token,
messages=messages,
tools=tools,
tool_choice=getattr(self, 'tool_choice', None),
@@ -2422,6 +2429,7 @@ Your Goal: {self.goal}"""
if stream_callback is None and hasattr(self, 'stream_emitter'):
stream_callback = getattr(self.stream_emitter, 'emit', None)
final_response = self._unified_dispatcher.chat_completion(
cancel_token=cancel_token,
messages=messages,
tools=tools,
tool_choice=getattr(self, 'tool_choice', None),
@@ -3012,6 +3020,19 @@ Your Goal: {self.goal}"""
# Check if external managed backend is configured
if hasattr(self, 'backend') and self.backend is not None:
# Honour configured throttling and cooperative cancellation before
# handing off to the managed backend, so backend requests obey the
# same request limits and cancel token as direct-LLM calls instead
# of bypassing both guards (parity with the async achat() path).
_cancel = cancel_token if cancel_token is not None else getattr(self, "interrupt_controller", None)
if _cancel is not None and getattr(_cancel, "is_set", lambda: False)():
reason = getattr(_cancel, "reason", None) or "cancelled before backend call"
raise InterruptedError(f"Agent chat cancelled: {reason}")
if self._rate_limiter is not None:
self._rate_limiter.acquire()
if _cancel is not None and getattr(_cancel, "is_set", lambda: False)():
reason = getattr(_cancel, "reason", None) or "cancelled before backend call"
raise InterruptedError(f"Agent chat cancelled: {reason}")
# Extract kwargs for delegation, excluding 'self' and function locals
delegation_kwargs = {
'temperature': temperature,
@@ -3383,7 +3404,7 @@ Your Goal: {self.goal}"""
# Apply guardrail validation for custom LLM response
try:
validated_response = self._apply_guardrail_with_retry(response_text, prompt, temperature, tools, task_name, task_description, task_id)
validated_response = self._apply_guardrail_with_retry(response_text, prompt, temperature, tools, task_name, task_description, task_id, cancel_token=cancel_token)
# Execute callback and display after validation
self._execute_callback_and_display(prompt, validated_response, time.time() - start_time, task_name, task_description, task_id)
return self._trigger_after_agent_hook(prompt, validated_response, start_time)
@@ -3483,7 +3504,12 @@ Your Goal: {self.goal}"""
agent_tools=agent_tools
)
response = self._chat_completion(messages, temperature=temperature, tools=tools, reasoning_steps=reasoning_steps, stream=stream, task_name=task_name, task_description=task_description, task_id=task_id, response_format=response_format)
# G2 - thread the cancel token into the OpenAI-native tool
# loop too. OpenAIClient.{a,}chat_completion_with_tools already
# implements the between-iteration checks; without this hop they
# are never armed, so /stop reports success while the remaining
# tool calls keep executing.
response = self._chat_completion(messages, temperature=temperature, tools=tools, reasoning_steps=reasoning_steps, stream=stream, task_name=task_name, task_description=task_description, task_id=task_id, response_format=response_format, cancel_token=cancel_token)
if not response:
# Rollback chat history on response failure
self._rollback_chat_history_to(chat_history_length)
@@ -3502,7 +3528,7 @@ Your Goal: {self.goal}"""
self._persist_message("assistant", response_text)
# Apply guardrail validation even for JSON output
try:
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id)
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id, cancel_token=cancel_token)
# Execute callback after validation
self._execute_callback_and_display(original_prompt, validated_response, time.time() - start_time, task_name, task_description, task_id)
return self._trigger_after_agent_hook(original_prompt, validated_response, start_time)
@@ -3523,7 +3549,7 @@ Your Goal: {self.goal}"""
if reasoning_steps and hasattr(response.choices[0].message, 'reasoning_content') and response.choices[0].message.reasoning_content:
# Apply guardrail to reasoning content
try:
validated_reasoning = self._apply_guardrail_with_retry(response.choices[0].message.reasoning_content, original_prompt, temperature, tools, task_name, task_description, task_id)
validated_reasoning = self._apply_guardrail_with_retry(response.choices[0].message.reasoning_content, original_prompt, temperature, tools, task_name, task_description, task_id, cancel_token=cancel_token)
# Execute callback after validation
self._execute_callback_and_display(original_prompt, validated_reasoning, time.time() - start_time, task_name, task_description, task_id)
return self._trigger_after_agent_hook(original_prompt, validated_reasoning, start_time)
@@ -3534,7 +3560,7 @@ Your Goal: {self.goal}"""
return None
# Apply guardrail to regular response
try:
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id)
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id, cancel_token=cancel_token)
# Execute callback after validation
self._execute_callback_and_display(original_prompt, validated_response, time.time() - start_time, task_name, task_description, task_id)
return self._trigger_after_agent_hook(original_prompt, validated_response, start_time)
@@ -3558,7 +3584,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
if self._using_custom_llm or self._openai_client is None:
# For custom LLMs, we need to handle reflection differently
# Use non-streaming to get complete JSON response
reflection_response = self._chat_completion(messages, temperature=temperature, tools=None, stream=False, reasoning_steps=False, task_name=task_name, task_description=task_description, task_id=task_id)
reflection_response = self._chat_completion(messages, temperature=temperature, tools=None, stream=False, reasoning_steps=False, task_name=task_name, task_description=task_description, task_id=task_id, cancel_token=cancel_token)
if not reflection_response or not reflection_response.choices:
raise Exception("No response from reflection request")
@@ -3600,7 +3626,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
self._append_to_chat_history({"role": "assistant", "content": response_text})
# Apply guardrail validation after satisfactory reflection
try:
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id)
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id, cancel_token=cancel_token)
# Execute callback after validation
self._execute_callback_and_display(original_prompt, validated_response, time.time() - start_time, task_name, task_description, task_id)
self._end_run(validated_response, "completed", {"duration_ms": (time.time() - start_time) * 1000})
@@ -3620,7 +3646,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
self._append_to_chat_history({"role": "assistant", "content": response_text})
# Apply guardrail validation after max reflections
try:
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id)
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id, cancel_token=cancel_token)
# Execute callback after validation
self._execute_callback_and_display(original_prompt, validated_response, time.time() - start_time, task_name, task_description, task_id)
return self._trigger_after_agent_hook(original_prompt, validated_response, start_time)
@@ -3636,7 +3662,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
# For reflection, always use non-streaming to ensure compatibility with sync adapters
# and to avoid streaming complexity during regeneration process
use_stream = False
response = self._chat_completion(messages, temperature=temperature, tools=None, stream=use_stream, task_name=task_name, task_description=task_description, task_id=task_id)
response = self._chat_completion(messages, temperature=temperature, tools=None, stream=use_stream, task_name=task_name, task_description=task_description, task_id=task_id, cancel_token=cancel_token)
content = response.choices[0].message.content
response_text = content.strip() if content else ""
reflection_count += 1
@@ -3656,7 +3682,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
_get_display_functions()['display_self_reflection']("Maximum reflection count reached after repeated parse errors, returning current response", console=self.console)
self._append_to_chat_history({"role": "assistant", "content": response_text})
try:
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id)
validated_response = self._apply_guardrail_with_retry(response_text, original_prompt, temperature, tools, task_name, task_description, task_id, cancel_token=cancel_token)
self._execute_callback_and_display(original_prompt, validated_response, time.time() - start_time, task_name, task_description, task_id)
return self._trigger_after_agent_hook(original_prompt, validated_response, start_time)
except Exception as guard_e:
@@ -3729,9 +3755,12 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
# handing off to the managed backend, so backend requests obey the
# same request limits and cancel token as direct-LLM calls instead
# of bypassing both guards.
_cancel = cancel_token if cancel_token is not None else getattr(self, "interrupt_controller", None)
if _cancel is not None and getattr(_cancel, "is_set", lambda: False)():
reason = getattr(_cancel, "reason", None) or "cancelled before backend call"
raise InterruptedError(f"Agent chat cancelled: {reason}")
if self._rate_limiter is not None:
await self._rate_limiter.acquire_async()
_cancel = cancel_token if cancel_token is not None else getattr(self, "interrupt_controller", None)
if _cancel is not None and getattr(_cancel, "is_set", lambda: False)():
reason = getattr(_cancel, "reason", None) or "cancelled before backend call"
raise InterruptedError(f"Agent chat cancelled: {reason}")
@@ -4975,6 +5004,10 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
# Handle any tool calls that were accumulated
if tool_calls_data:
# Mark where the tool turns begin so the follow-up can
# append exactly the assistant tool-call and tool-result
# messages onto the original request context below.
tool_turn_start = len(self.chat_history)
# Add assistant message with tool calls to chat history
assistant_message = {"role": "assistant", "content": response_text}
if tool_calls_data:
@@ -5068,6 +5101,75 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
# Flush deferred media follow-ups after all tool replies.
for _m in _deferred_media_followups:
self._append_to_chat_history(_m)
# Ask the model for the answer now that the tools have
# run. Without this the generator ends here: the tool
# results sit in chat history, nothing is yielded, and
# the caller receives an empty stream with no error --
# indistinguishable from a model that had nothing to
# say. The non-streaming path already does this round
# trip, so the two disagreed about whether an answer
# existed at all.
#
# Reuse the original `messages` (system prompt, output
# constraints, memory context and the user turn) rather
# than `self.chat_history`, which omits the system turn
# and would let the follow-up ignore the agent role and
# output format. Append only the tool turns produced by
# this round -- everything added to chat history since
# the completion began.
followup_args = dict(completion_args)
followup_args['messages'] = (
list(messages) + self.chat_history[tool_turn_start:]
)
followup_args['stream'] = True
# No tools on the follow-up: the model has its results
# and should now answer, not call another tool. That
# also bounds the turn to a single round of tools.
followup_args.pop('tools', None)
followup_args.pop('tool_choice', None)
followup_args.pop('parallel_tool_calls', None)
# Bracket the follow-up with stream events so consumers
# see request/terminal telemetry for the answer too --
# the STREAM_END above referred only to the tool-call
# round, before any answer existed.
self.stream_emitter.emit(StreamEvent(
type=StreamEventType.REQUEST_START,
timestamp=time_module.perf_counter(),
metadata={
"model": self.llm,
"message_count": len(followup_args['messages']),
"phase": "post_tool_answer",
}
))
followup_text = ""
followup_last_content_time = None
for followup_chunk in self._openai_client.sync_client.chat.completions.create(
**followup_args
):
if not followup_chunk.choices:
continue
piece = followup_chunk.choices[0].delta.content
if piece:
followup_text += piece
followup_last_content_time = time_module.perf_counter()
yield piece
if followup_last_content_time:
self.stream_emitter.emit(StreamEvent(
type=StreamEventType.LAST_TOKEN,
timestamp=followup_last_content_time
))
self.stream_emitter.emit(StreamEvent(
type=StreamEventType.STREAM_END,
timestamp=time_module.perf_counter(),
metadata={"response_length": len(followup_text)}
))
if followup_text:
self._append_to_chat_history(
{"role": "assistant", "content": followup_text}
)
else:
# Add complete response to chat history (text-only response)
if response_text:
@@ -5368,18 +5470,111 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
agent_id=getattr(self, 'name', None),
)
def _chat_completion_with_retry(self, messages, temperature=None, tools=None, stream=None, reasoning_steps=False, task_name=None, task_description=None, task_id=None, response_format=None, stream_callback=None, emit_events=True):
def _get_model_middleware_manager(self):
"""Return a MiddlewareManager if user model-call hooks are registered.
Companion to ``_get_tool_middleware_manager`` for the model-call side of
the ``hooks/middleware.py`` surface (``before_model``/``after_model``/
``wrap_model_call``). Reuses the same lazily-built manager and returns
``None`` when no model hooks are present, preserving the zero-overhead
fast path.
"""
hooks = getattr(self, '_hooks', None)
if not hooks:
return None
manager = getattr(self, '_middleware_manager', None)
if manager is None:
from ..hooks import MiddlewareManager
manager = MiddlewareManager(hooks)
self._middleware_manager = manager
return manager if manager.has_model_hooks else None
@staticmethod
def _unwrap_model_response(response):
"""Unwrap a middleware ModelResponse back to the agent's return shape.
Preserves ``after_model``/``wrap_model_call`` mutations: if a hook
rewrote ``response.content``, that string wins. The original SDK object
stashed in ``extra["raw"]`` is only returned when the hook left content
untouched (so non-string provider payloads pass through unchanged on the
no-transform fast path).
"""
from ..hooks import ModelResponse
if not isinstance(response, ModelResponse):
return response
raw = response.extra.get("raw")
if isinstance(raw, str):
# Content was derived from a string raw; hook edits live on content.
return response.content
# Non-string raw: return raw only if content still matches the value we
# synthesized from it (i.e. hook didn't rewrite content).
synthesized = raw if isinstance(raw, str) else (raw or "")
if response.content == synthesized:
return raw
return response.content
def _chat_completion_with_retry(self, messages, temperature=None, tools=None, stream=None, reasoning_steps=False, task_name=None, task_description=None, task_id=None, response_format=None, stream_callback=None, emit_events=True, cancel_token=None):
"""
Wrapper for _execute_unified_chat_completion that adds jittered exponential backoff retry logic.
This method wraps the unified chat completion call and adds retry capability for
transient failures like rate limits, network errors, and service outages.
"""
# Route through user-supplied model middleware (Agent(hooks=[...])) when
# before_model/after_model/wrap_model_call hooks are registered. The
# retry/backoff logic below becomes the middleware chain's final handler.
manager = self._get_model_middleware_manager()
if manager is not None:
from ..hooks import ModelRequest, ModelResponse, InvocationContext
def _model_fn(req):
# Honor before_model/wrap_model_call mutations by threading the
# (possibly rewritten) request fields into the retry core rather
# than the outer captured values.
effective_temperature = req.temperature if req.temperature is not None else temperature
effective_tools = req.tools if req.tools is not None else tools
result = self._chat_completion_with_retry_core(
req.messages, effective_temperature, effective_tools, stream,
reasoning_steps, task_name, task_description, task_id,
response_format, stream_callback=stream_callback,
emit_events=emit_events, cancel_token=cancel_token,
)
return ModelResponse(
content=result if isinstance(result, str) else (result or ""),
model=str(getattr(self, 'llm', '') or ''),
extra={"raw": result},
)
request = ModelRequest(
messages=messages,
model=str(getattr(self, 'llm', '') or ''),
temperature=temperature if temperature is not None else 1.0,
tools=tools,
context=InvocationContext(
agent_id=self.name,
run_id=getattr(self, '_current_run_id', 'unknown'),
session_id=getattr(self, '_session_id', None) or 'default',
model_name=str(getattr(self, 'llm', '') or ''),
),
)
response = manager.execute_model_call(request, _model_fn)
return self._unwrap_model_response(response)
return self._chat_completion_with_retry_core(
messages, temperature, tools, stream, reasoning_steps,
task_name, task_description, task_id, response_format,
stream_callback=stream_callback, emit_events=emit_events,
cancel_token=cancel_token,
)
def _chat_completion_with_retry_core(self, messages, temperature=None, tools=None, stream=None, reasoning_steps=False, task_name=None, task_description=None, task_id=None, response_format=None, stream_callback=None, emit_events=True, cancel_token=None):
"""Retry/backoff core for chat completion (middleware-agnostic)."""
retry_config = getattr(self, '_retry_config', None)
if not retry_config:
return self._execute_unified_chat_completion(messages, temperature, tools, stream, reasoning_steps,
task_name, task_description, task_id, response_format,
stream_callback=stream_callback, emit_events=emit_events)
stream_callback=stream_callback, emit_events=emit_events,
cancel_token=cancel_token)
from .retry_utils import jittered_backoff
from ..hooks import HookEvent, OnRetryInput
@@ -5392,7 +5587,8 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
# Call the underlying unified chat completion directly to avoid infinite recursion
return self._execute_unified_chat_completion(messages, temperature, tools, stream, reasoning_steps,
task_name, task_description, task_id, response_format,
stream_callback=stream_callback, emit_events=emit_events)
stream_callback=stream_callback, emit_events=emit_events,
cancel_token=cancel_token)
except Exception as e:
from ..errors import LLMError
@@ -5462,6 +5658,62 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
This method wraps the async chat completion call and adds retry capability for
transient failures like rate limits, network errors, and service outages.
"""
# Route through user-supplied model middleware (Agent(hooks=[...])) when
# before_model/after_model/wrap_model_call hooks are registered, mirroring
# the sync path so achat() honors the same hooks. The middleware chain is
# synchronous; run it in an executor and bridge back to the async retry
# core via the running loop so we never block or nest event loops.
manager = self._get_model_middleware_manager()
if manager is not None:
import asyncio
from ..hooks import ModelRequest, ModelResponse, InvocationContext
loop = asyncio.get_running_loop()
def _model_fn(req):
effective_temperature = req.temperature if req.temperature is not None else temperature
effective_tools = req.tools if req.tools is not None else tools
fut = asyncio.run_coroutine_threadsafe(
self._achat_completion_with_retry_core(
req.messages, effective_temperature, effective_tools, stream,
reasoning_steps, task_name, task_description, task_id,
response_format, stream_callback=stream_callback,
emit_events=emit_events,
),
loop,
)
result = fut.result()
return ModelResponse(
content=result if isinstance(result, str) else (result or ""),
model=str(getattr(self, 'llm', '') or ''),
extra={"raw": result},
)
request = ModelRequest(
messages=messages,
model=str(getattr(self, 'llm', '') or ''),
temperature=temperature if temperature is not None else 1.0,
tools=tools,
context=InvocationContext(
agent_id=self.name,
run_id=getattr(self, '_current_run_id', 'unknown'),
session_id=getattr(self, '_session_id', None) or 'default',
model_name=str(getattr(self, 'llm', '') or ''),
),
)
response = await loop.run_in_executor(
None, lambda: manager.execute_model_call(request, _model_fn)
)
return self._unwrap_model_response(response)
return await self._achat_completion_with_retry_core(
messages, temperature, tools, stream, reasoning_steps,
task_name, task_description, task_id, response_format,
stream_callback=stream_callback, emit_events=emit_events,
)
async def _achat_completion_with_retry_core(self, messages, temperature=None, tools=None, stream=None, reasoning_steps=False, task_name=None, task_description=None, task_id=None, response_format=None, stream_callback=None, emit_events=True):
"""Async retry/backoff core for chat completion (middleware-agnostic)."""
retry_config = getattr(self, '_retry_config', None)
if not retry_config:
return await self._execute_unified_achat_completion(
@@ -1568,7 +1568,18 @@ Write the complete compiled report:"""
result.get("_praison_retryable") is False):
result.pop("_praison_retryable", None)
return result
# The body already ran and may have completed its side effect
# before failing. Honour an explicit restart_safe=False /
# idempotent=False declaration, exactly as the sync loops do
# (tool_execution.py:884 and :2115). Without this the async
# path accepts the declaration and silently re-runs the tool
# up to max_attempts times -- charging a card three times for
# a tool the author marked "must never be silently re-executed".
if self._tool_declares_not_idempotent(function_name, tools_override):
result.pop("_praison_retryable", None)
return result
# Determine error type for retry policy
error_type = self._classify_error_type(result, last_exception)
@@ -1958,7 +1958,7 @@ class ToolExecutionMixin:
)
return any(marker in name for marker in edit_markers)
def _check_permission_manager_deny(self, function_name):
def _check_permission_manager_deny(self, function_name, arguments=None):
"""Return an error dict if the PermissionManager hard-denies the tool.
Ensures the pattern-based ``PermissionManager`` (rules from
@@ -1966,18 +1966,51 @@ class ToolExecutionMixin:
uniformly — native functions **and** MCP tools — since both flow through
``_execute_tool_impl``. Returns ``None`` when no manager is attached or
the tool is not denied, preserving backward compatibility.
Both the tool *name* and the argument-scoped target (e.g.
``bash:rm -rf /tmp/x`` built from ``execute_command(command=...)``) are
checked so an argument-level ``deny`` rule — which the rule engine can
already match — actually fires. Without this the deny tier was a
name-only decision: once a shell tool was allowed by name, no rule could
narrow it. The argument target reuses ``build_permission_target`` so it
speaks the same vocabulary as the allow side's scoped grants.
"""
manager = getattr(self, "_permission_manager", None)
if manager is None:
return None
try:
if manager.is_denied(function_name, getattr(self, "name", None)):
return {
"error": f"Tool '{function_name}' blocked by permission policy",
"permission_denied": True,
}
except Exception as e:
logging.debug("permission manager is_denied failed for %s: %s", function_name, e)
agent_name = getattr(self, "name", None)
targets = [function_name]
if arguments:
try:
from ..approval.utils import build_permission_target
# Normalise argument keys the same way ``_cast_arguments`` does
# before dispatch (strip trailing '='/whitespace LLMs hallucinate)
# so ``command=`` still resolves to the ``command`` command-key
# and an argument-scoped deny (``bash:rm *``) cannot be evaded by
# a malformed kwarg name that only normalises *after* this gate.
normalized_args = arguments
if isinstance(arguments, dict):
normalized_args = {
(k.strip().rstrip('=').strip() if isinstance(k, str) else k): v
for k, v in arguments.items()
}
scoped_target = build_permission_target(function_name, normalized_args)
if scoped_target and scoped_target != function_name:
targets.append(scoped_target)
except Exception as e: # noqa: BLE001
logging.debug(
"could not build argument-scoped permission target for %s: %s",
function_name, e,
)
for target in targets:
try:
if manager.is_denied(target, agent_name):
return {
"error": f"Tool '{function_name}' blocked by permission policy",
"permission_denied": True,
}
except Exception as e:
logging.debug("permission manager is_denied failed for %s: %s", target, e)
return None
def _is_bypass_mode(self) -> bool:
@@ -2012,7 +2045,7 @@ class ToolExecutionMixin:
# Pattern-based PermissionManager deny gate (native + MCP, uniform).
# BYPASS mode skips this gate as its contract requires.
if not self._is_bypass_mode():
manager_denial = self._check_permission_manager_deny(function_name)
manager_denial = self._check_permission_manager_deny(function_name, arguments)
if manager_denial is not None:
return manager_denial
@@ -2032,6 +2065,14 @@ class ToolExecutionMixin:
if decision.modified_args:
arguments = decision.modified_args
logging.info(f"Using modified arguments: {arguments}")
# Re-authorize: an approval backend may have rewritten the arguments
# into a command that an argument-scoped deny rule prohibits. The
# earlier gate only saw the original args, so re-run it against the
# final ones before dispatch. BYPASS still opts out entirely.
if not self._is_bypass_mode():
manager_denial = self._check_permission_manager_deny(function_name, arguments)
if manager_denial is not None:
return manager_denial
from ..approval import get_approval_registry
get_approval_registry().mark_approved(
@@ -2050,7 +2091,7 @@ class ToolExecutionMixin:
# Pattern-based PermissionManager deny gate (native + MCP, uniform).
# BYPASS mode skips this gate as its contract requires.
if not self._is_bypass_mode():
manager_denial = self._check_permission_manager_deny(function_name)
manager_denial = self._check_permission_manager_deny(function_name, arguments)
if manager_denial is not None:
return manager_denial
@@ -2071,6 +2112,12 @@ class ToolExecutionMixin:
if decision.modified_args:
arguments = decision.modified_args
logging.info(f"Using modified arguments: {arguments}")
# Re-authorize modified args against the deny gate — see the sync
# path for rationale. BYPASS still opts out entirely.
if not self._is_bypass_mode():
manager_denial = self._check_permission_manager_deny(function_name, arguments)
if manager_denial is not None:
return manager_denial
from ..approval import get_approval_registry
get_approval_registry().mark_approved(
@@ -2996,7 +3043,7 @@ class ToolExecutionMixin:
else:
return f"Unknown bridge tool: {function_name}"
def _tool_declares_not_idempotent(self, tool_name):
def _tool_declares_not_idempotent(self, tool_name, tools_override=None):
"""True only when the tool *explicitly* declares itself unsafe to re-run.
Deliberately narrower than ``_is_tool_idempotent``, which answers False
@@ -3008,10 +3055,19 @@ class ToolExecutionMixin:
73-name list maintained for the escalation loop guard, and using it here
silently removed the retry from any *undeclared* tool that happened to
share a name with an entry (``write_file``, ``store_memory``, ``mkdir``...).
``tools_override`` mirrors the resolution order in ``_execute_tool_impl`` /
``_execute_tool_async_impl``: a task-scoped tool passed via ``tools_override``
shadows an agent-level tool of the same name, so its declaration must win.
Without this an override-only ``restart_safe=False`` tool would be missed
here and silently retried.
"""
tools = getattr(self, 'tools', [])
if not isinstance(tools, (list, tuple)):
tools = []
if isinstance(tools_override, (list, tuple)):
# Override tools take precedence and are searched first.
tools = list(tools_override) + list(tools)
for tool in tools:
name_attr = getattr(tool, '__name__', None) or getattr(tool, 'name', None)
if name_attr == tool_name:
@@ -1546,6 +1546,20 @@ class AgentTeam(SpawnAnnounceProtocol):
return False
def _should_continue_on_dep_failure(self, task_id):
"""True if the task opted into graceful degradation via its own flags.
Honors the documented per-task flow-control knobs ``skip_on_failure``
and ``on_error`` (``"continue"``). When set, a dependent task still runs
(in degraded mode) instead of being force-failed when an upstream
dependency fails.
"""
task = self.tasks.get(task_id)
if task is None:
return False
return bool(getattr(task, 'skip_on_failure', False)) or \
getattr(task, 'on_error', 'stop') == 'continue'
async def arun_all_tasks(self):
"""Async version of run_all_tasks method"""
process = Process(
@@ -1568,8 +1582,9 @@ class AgentTeam(SpawnAnnounceProtocol):
await self._gather_with_isolation([c for _, c in tasks_to_run])
tasks_to_run = []
# A just-flushed prerequisite may now be failed; don't
# queue a dependent that would run with missing context.
if self._deps_failed(task_id):
# queue a dependent that would run with missing context
# unless it opted into graceful degradation.
if self._deps_failed(task_id) and not self._should_continue_on_dep_failure(task_id):
self.tasks[task_id].status = "failed"
continue
tasks_to_run.append((task_id, self.arun_task(task_id)))
@@ -1608,8 +1623,9 @@ class AgentTeam(SpawnAnnounceProtocol):
if self._depends_on_pending(task, async_tasks_to_run):
await flush_async_tasks()
# A just-flushed prerequisite may now be failed; don't
# queue a dependent that would run with missing context.
if self._deps_failed(task_id):
# queue a dependent that would run with missing context
# unless it opted into graceful degradation.
if self._deps_failed(task_id) and not self._should_continue_on_dep_failure(task_id):
self.tasks[task_id].status = "failed"
continue
# Collect async tasks to run in parallel
@@ -1618,8 +1634,9 @@ class AgentTeam(SpawnAnnounceProtocol):
# Before running a sync task, execute all pending async tasks
await flush_async_tasks()
# A just-flushed async prerequisite may now be failed; skip
# the dependent so we don't run it with missing upstream context.
if self._deps_failed(task_id):
# the dependent so we don't run it with missing upstream
# context, unless it opted into graceful degradation.
if self._deps_failed(task_id) and not self._should_continue_on_dep_failure(task_id):
self.tasks[task_id].status = "failed"
continue
# Run sync task in an executor to avoid blocking the event loop
@@ -32,6 +32,12 @@ class ApprovalRequest:
approval_id: Stable correlation id that survives a process restart
and can be matched to an inbound channel callback.
Auto-generated when not supplied.
authorized_reviewers: Optional set of reviewer identities allowed to
resolve this request. ``None`` (default) keeps today's
behaviour (any scope-holder may resolve). When populated a
backend may bind resolution to these reviewers so that in a
multi-operator deployment a request addressed to one
operator cannot be resolved by another.
"""
tool_name: str
@@ -41,6 +47,7 @@ class ApprovalRequest:
session_id: Optional[str] = None
context: Dict[str, Any] = field(default_factory=dict)
approval_id: str = field(default_factory=_new_approval_id)
authorized_reviewers: Optional[List[str]] = None
@dataclass
@@ -89,6 +89,11 @@ class BotConfig:
# replaces it with done_emoji on completion (e.g. "✅").
ack_emoji: str = ""
done_emoji: str = "✅"
# Which inbound messages get an ack reaction. One of:
# "off" | "direct" | "group-mentions" | "group-all" | "all".
# Default "group-mentions": ack DMs and group mentions/replies, but stay
# quiet on ambient group chatter the bot won't answer.
ack_scope: str = "group-mentions"
# Session TTL in seconds. 0 = disabled (sessions never expire).
# When set, stale sessions older than this are auto-reaped.
@@ -158,6 +163,7 @@ class BotConfig:
"debounce_ms": self.debounce_ms,
"ack_emoji": self.ack_emoji,
"done_emoji": self.done_emoji,
"ack_scope": self.ack_scope,
"session_ttl": self.session_ttl,
"busy_mode": self.busy_mode,
"busy_ack": self.busy_ack,
@@ -263,7 +263,7 @@ def create_memory_flush_agent(
) -> Any:
"""Create a restricted child agent sharing only the parent's memory store."""
from ..agent.agent import Agent
from ..config.feature_configs import ExecutionConfig
from ..config.feature_configs import ExecutionConfig, OutputConfig
from ..tools.memory import search_memory, store_memory
llm = config.llm or getattr(parent_agent, "llm", None)
@@ -286,8 +286,7 @@ def create_memory_flush_agent(
else getattr(parent_agent, "_memory_instance", None)
),
execution=ExecutionConfig(context_compaction=False, max_steps=3),
verbose=False,
stream=False,
output=OutputConfig(verbose=False, stream=False),
)
@@ -1902,6 +1902,48 @@ class RuntimeConfig:
RuntimeParam = Union[bool, str, Dict[str, Any], RuntimeConfig, None]
# Known runtime names, keyed by their canonical spelling. Every alias maps to
# the runtime whose capability matrix the agent will actually use, so a name
# that resolves here can never silently drop capabilities downstream.
RUNTIME_ALIASES: Dict[str, str] = {
"native": "native",
"plugin_harness": "plugin-harness",
"plugin-harness": "plugin-harness",
"harness": "plugin-harness",
"plugin": "plugin-harness",
"reduced": "plugin-harness",
}
def canonical_runtime_name(value: str) -> str:
"""Normalise a runtime name; reject typos but allow genuine plugin names.
Matching is case-insensitive, ignores surrounding whitespace, and treats
``-``/``_`` as interchangeable, mirroring the other preset resolvers.
A value that names a known runtime resolves to its canonical spelling, so
``NATIVE``/`` native `` all map to ``native`` and pick up the full matrix
instead of silently dropping capabilities. A value that is a close typo of
a known runtime (e.g. ``nativ``) raises with a suggestion rather than
degrading silently. A genuinely unknown name (e.g. a third-party plugin
runtime registered later) is returned as-is so it can fall back to the
reduced harness — matching the runtime registry, not a closed literal.
"""
from .parse_utils import canonical_preset_key, make_preset_error, suggest_similar
key = canonical_preset_key(value)
if key in RUNTIME_ALIASES:
return RUNTIME_ALIASES[key]
known = sorted(set(RUNTIME_ALIASES))
if suggest_similar(key, known):
# Close enough to a known runtime that it is almost certainly a typo.
# Pass the full alias set so the error can suggest the matched name
# (e.g. "reducd" -> "reduced") rather than only canonical spellings.
raise make_preset_error("runtime", value, known)
# Unrecognised but not a near-miss: treat as an opaque plugin runtime name.
return value
def resolve_runtime(value: RuntimeParam) -> Optional[RuntimeConfig]:
"""Resolve runtime= parameter following precedence ladder."""
if value is None or value is False:
@@ -1909,13 +1951,24 @@ def resolve_runtime(value: RuntimeParam) -> Optional[RuntimeConfig]:
if value is True:
return RuntimeConfig()
if isinstance(value, str):
return RuntimeConfig(preferred_runtime=value)
return RuntimeConfig(preferred_runtime=canonical_runtime_name(value))
if isinstance(value, (list, set, tuple, frozenset)):
return RuntimeConfig(required_capabilities=list(value))
if isinstance(value, dict):
return RuntimeConfig(**value)
config = RuntimeConfig(**value)
if config.preferred_runtime is not None:
config.preferred_runtime = canonical_runtime_name(config.preferred_runtime)
return config
if isinstance(value, RuntimeConfig):
return value
if value.preferred_runtime is None:
return value
# Return a normalised copy rather than mutating the caller's object,
# so a config reused/compared after agent construction is unchanged.
from dataclasses import replace
return replace(
value,
preferred_runtime=canonical_runtime_name(value.preferred_runtime),
)
raise TypeError(
f"Invalid runtime parameter type: {type(value).__name__}. "
"Expected None/False, True, str, list, set, dict, or RuntimeConfig."
@@ -1986,6 +2039,7 @@ __all__ = [
"resolve_tool_search",
"resolve_tools",
"resolve_runtime",
"canonical_runtime_name",
]
@@ -6275,8 +6275,16 @@ def resolve_required_scope(
# status/read needs READ. Anything unregistered defaults to ADMIN (deny).
def _register_core_gateway_methods() -> None:
core: Dict[str, OperatorScope] = {
# Connection lifecycle. These run before a client can hold any scope, so
# they must not require one -- classifying them ADMIN by omission is what
# kept resolve_required_scope() from ever being wired into dispatch.
"hello": OperatorScope.READ,
"join": OperatorScope.READ,
"leave": OperatorScope.READ,
"agent.message": OperatorScope.WRITE,
"message": OperatorScope.WRITE,
# Aborting a turn mutates it, so it carries the same scope as sending one.
"abort": OperatorScope.WRITE,
"session.status": OperatorScope.READ,
"session.transcript": OperatorScope.READ,
"approvals.resolve": OperatorScope.APPROVALS,
@@ -512,7 +512,7 @@ class Knowledge:
# Check if input is URL
if isinstance(input_path, str) and (input_path.startswith('http://') or input_path.startswith('https://')):
self._log(f"Processing URL: {input_path}")
raise NotImplementedError("URL processing not yet implemented")
return self._process_url(input_path, user_id, agent_id, run_id, metadata)
# CRITICAL FIX: Check if input is a directory - recursively process all files
if os.path.isdir(input_path):
@@ -626,7 +626,65 @@ class Knowledge:
except Exception as e:
logger.error(f"Error processing input {input_path}: {str(e)}", exc_info=True)
raise
def _process_url(self, url, user_id=None, agent_id=None, run_id=None, metadata=None):
"""Fetch a URL, extract its content, and store it in memory.
Uses MarkItDown (already a dependency for document extraction) to fetch
and convert the remote resource, then chunks and stores it through the
same path as local files. Failures are surfaced to the caller so a
single bad URL in a batch is visible, matching local-file behaviour.
"""
result = self.markdown.convert(url)
content = getattr(result, 'text_content', None)
if not content:
raise ValueError(f"No content could be extracted from URL: {url}")
chunks = self.chunker.chunk(content)
memories = [
chunk.text.strip() if hasattr(chunk, 'text') else str(chunk).strip()
for chunk in chunks if chunk
]
if not metadata:
metadata = {}
metadata['source_type'] = 'url'
metadata['source'] = url
all_results = []
for memory in memories:
if not memory:
continue
memory_result = self.store(
memory, user_id=user_id, agent_id=agent_id,
run_id=run_id, metadata=metadata,
)
if not memory_result:
continue
if isinstance(memory_result, dict):
all_results.extend(memory_result.get('results', []))
elif isinstance(memory_result, list):
all_results.extend(memory_result)
else:
from .models import AddResult
if isinstance(memory_result, AddResult):
if not memory_result.success:
raise RuntimeError(
memory_result.message or "Failed to store chunk"
)
all_results.append(memory_result.id)
elif hasattr(memory_result, 'results'):
all_results.extend(memory_result.results)
elif isinstance(memory_result, str):
# Some backends (e.g. MongoDB) return a bare document id.
all_results.append(memory_result)
self._emit_knowledge_event(
"add", source=url, chunk_count=len(memories),
metadata=metadata, agent_id=agent_id,
)
return {'results': all_results, 'relations': []}
def index(
self,
path: str,
@@ -2410,8 +2410,8 @@ Respond with ONLY a valid JSON tool call in this format:
# Handle Gemini internal tools (e.g., {"googleSearch": {}}, {"urlContext": {}}, {"codeExecution": {}})
elif isinstance(tool, dict) and len(tool) == 1:
tool_name = next(iter(tool.keys()))
gemini_internal_tools = {'googleSearch', 'urlContext', 'codeExecution'}
if tool_name in gemini_internal_tools:
from .model_capabilities import GEMINI_INTERNAL_TOOLS
if tool_name in GEMINI_INTERNAL_TOOLS:
logging.debug(f"Using Gemini internal tool: {tool_name}")
formatted_tools.append(tool)
else:
@@ -1549,13 +1549,17 @@ class Memory(SearchMixin, MemoryCoreMixin):
elif memory_type == "long_term":
return self.delete_long_term(memory_id)
# Search both types
if self.delete_short_term(memory_id):
return True
if self.delete_long_term(memory_id):
return True
return False
# No tier given: delete from BOTH tiers, never short-circuit on the first
# hit. The SQLite adapter gives each tier its own AUTOINCREMENT sequence
# (sqlite_adapter.py:83 and :107), so the Nth short-term row and the Nth
# long-term row always share an id -- collision is the norm, not an edge
# case. Returning on the first hit therefore deleted an unrelated
# short-term row and left the caller's actual target in place, while
# reporting success. `forget()` exposes no tier argument at all, so a
# caller has no way to disambiguate.
deleted_short = self.delete_short_term(memory_id)
deleted_long = self.delete_long_term(memory_id)
return deleted_short or deleted_long
def delete_memories(self, memory_ids: List[str]) -> int:
"""
@@ -2294,6 +2298,26 @@ class Memory(SearchMixin, MemoryCoreMixin):
def get_all_memories(self) -> List[Dict[str, Any]]:
"""Get all memories from both short-term and long-term storage"""
# Delegate to the active adapter whenever one is configured, mirroring
# delete_short_term/reset_short_term. Data written through the adapter
# lives in short_term_memory/long_term_memory; the legacy SELECTs below
# target short_mem/long_mem, which nothing in the codebase creates, so
# they raise OperationalError, get swallowed, and return [] every time.
if (getattr(self, "memory_adapter", None)
and hasattr(self.memory_adapter, "get_all_memories")):
# Adapters expose the tier under different keys (the SQLite adapter
# uses `memory_type`, others may use `type`). The legacy path below
# returns `type`, so normalize to keep that public field populated
# for backward compatibility while retaining any adapter-specific
# keys the record already carries.
return [
{
**record,
"type": record.get("type") or record.get("memory_type"),
}
for record in self.memory_adapter.get_all_memories()
]
all_memories = []
try:
@@ -1022,7 +1022,13 @@ Workflow Finished: {self.workflow_finished} # ADDED: Workflow Finished Status
self.tasks[dep.id].status == "failed"
for dep in task.context if hasattr(dep, 'id') and dep.id in self.tasks
)
if deps_failed:
# Honor per-task graceful-degradation flags: a task that opted
# into skip_on_failure/on_error="continue" still runs even when
# an upstream dependency failed.
if deps_failed and not (
getattr(task, 'skip_on_failure', False)
or getattr(task, 'on_error', 'stop') == 'continue'
):
task.status = "failed"
continue
if task.status != "completed":
@@ -1627,7 +1633,13 @@ Workflow Finished: {self.workflow_finished} # ADDED: Workflow Finished Status
self.tasks[dep.id].status == "failed"
for dep in task.context if hasattr(dep, 'id') and dep.id in self.tasks
)
if deps_failed:
# Honor per-task graceful-degradation flags: a task that opted
# into skip_on_failure/on_error="continue" still runs even when
# an upstream dependency failed.
if deps_failed and not (
getattr(task, 'skip_on_failure', False)
or getattr(task, 'on_error', 'stop') == 'continue'
):
task.status = "failed"
continue
if task.status != "completed":
@@ -52,7 +52,6 @@ class DelegationTools:
role=role,
goal=f"Complete delegated {agent_type} tasks accurately.",
llm=llm,
verbose=False,
)
spawn = create_subagent_tool(agent_factory=_agent_factory)["function"]
@@ -105,6 +105,47 @@ def _validate_code_ast(code: str):
return None
def _make_resource_preexec(limits: ResourceLimits):
"""Build a POSIX preexec_fn that applies ResourceLimits via setrlimit.
Caps address space (memory), process count, open files, file size and
CPU time in the child process so untrusted code cannot exhaust host
resources. CPU time is capped in seconds (derived from timeout_seconds)
so sustained CPU-bound code is stopped by the kernel rather than only by
the wall-clock timeout. Each RLIMIT_* constant is resolved defensively:
constants absent on a given POSIX platform, and individual caps the
platform rejects, are skipped without aborting the launch (the wall-clock
timeout still applies as a backstop).
"""
import resource
def _limit():
# CPU-time cap in whole seconds; derived from timeout_seconds so the
# kernel terminates CPU-bound code near the wall-clock budget instead
# of letting it saturate a core until the parent times out.
cpu_seconds = max(1, int(limits.timeout_seconds)) if limits.timeout_seconds else 0
caps = (
("RLIMIT_AS", limits.memory_mb, 1024 * 1024),
("RLIMIT_NPROC", limits.max_processes, 1),
("RLIMIT_NOFILE", limits.max_open_files, 1),
("RLIMIT_FSIZE", limits.disk_write_mb, 1024 * 1024),
("RLIMIT_CPU", cpu_seconds, 1),
)
for res_name, value, scale in caps:
if not value:
continue
res = getattr(resource, res_name, None)
if res is None:
continue
cap = value * scale
try:
resource.setrlimit(res, (cap, cap))
except (ValueError, OSError):
pass
return _limit
def _execute_code_sandboxed(
code: str,
timeout: int = 30,
@@ -287,7 +328,20 @@ if __name__ == "__main__":
# Configure subprocess with resource limits
env = {} # Clean environment (no access to parent process env vars)
# Enforce the computed ResourceLimits in the child process. On POSIX we
# apply them via setrlimit in a preexec_fn so untrusted code cannot
# exhaust host memory/CPU/processes/files (see ResourceLimits.minimal()).
preexec_fn = None
if os.name == "posix":
preexec_fn = _make_resource_preexec(limits)
else:
logging.warning(
"Code sandbox: resource limits (memory/CPU/process/file caps) "
"are only enforced on POSIX; on this platform only the "
"wall-clock timeout applies."
)
# Run subprocess with timeout and resource limits
process = subprocess.Popen(
[sys.executable, temp_file],
@@ -296,7 +350,8 @@ if __name__ == "__main__":
env=env,
shell=False, # Security: prevent shell injection
text=True,
cwd=tempfile.gettempdir() # Run in temp dir, not current dir
cwd=tempfile.gettempdir(), # Run in temp dir, not current dir
preexec_fn=preexec_fn,
)
try:
@@ -28,6 +28,7 @@ from praisonaiagents.ui.agui.types import (
StateSnapshotEvent,
)
from praisonaiagents.ui.protocols import A2UI_MIME_TYPE
from praisonaiagents.streaming.events import StreamEventType as _SE
@dataclass
@@ -39,15 +40,35 @@ class EventBuffer:
current_text_message_id: str = ""
next_text_message_id: str = field(default_factory=lambda: str(uuid.uuid4()))
pending_tool_calls_parent_id: str = ""
current_tool_call_id: str = ""
def start_tool_call(self, tool_call_id: str) -> None:
"""Start a new tool call."""
self.active_tool_call_ids.add(tool_call_id)
self.current_tool_call_id = tool_call_id
def end_tool_call(self, tool_call_id: str) -> None:
"""End a tool call."""
self.active_tool_call_ids.discard(tool_call_id)
self.ended_tool_call_ids.add(tool_call_id)
if self.current_tool_call_id == tool_call_id:
self.current_tool_call_id = ""
def resolve_tool_call_id(self, provided_id: Optional[str]) -> str:
"""Resolve the tool_call_id for a delta that may omit it.
Providers commonly send the id only on the first chunk of a tool call
and omit it on subsequent argument deltas. Falling back to the last
started tool call keeps every chunk correlated to one invocation instead
of minting an orphan UUID per chunk.
"""
if provided_id:
return provided_id
if self.current_tool_call_id:
return self.current_tool_call_id
new_id = str(uuid.uuid4())
self.current_tool_call_id = new_id
return new_id
def start_text_message(self) -> str:
"""Start a new text message and return its ID."""
@@ -260,6 +281,38 @@ async def async_stream_response(
yield create_run_error_event(str(e))
# --- AG-UI disposition of every StreamEventType -------------------------------
# Membership in these tables is a decision, not an omission. The adapter raises on
# any type absent from all three, so adding a StreamEventType without choosing a
# disposition fails tests/agui/test_streaming_completeness.py rather than silently
# dropping the event at runtime.
# Process-local timing and lifecycle markers with no AG-UI counterpart.
# RUN_FINISHED is emitted by the enclosing run loop, so STREAM_END would double it.
NOT_WIRE_VISIBLE = frozenset({
_SE.REQUEST_START,
_SE.HEADERS_RECEIVED,
_SE.FIRST_TOKEN,
_SE.LAST_TOKEN,
_SE.STREAM_END,
})
# The run continues, but a client that is never told cannot explain a stall or a
# silently changed model. Carried as CustomEvent, not RunErrorEvent, because
# RunErrorEvent is terminal in AG-UI and these are recovered conditions.
_RECOVERABLE_EVENTS = {
_SE.RETRY: "praisonai.retry",
_SE.MODEL_FALLBACK: "praisonai.model_fallback",
_SE.STREAM_UNAVAILABLE: "praisonai.stream_unavailable",
}
# User-visible progress that AG-UI has no first-class frame for.
_PROGRESS_EVENTS = {
_SE.TOOL_PROGRESS: "praisonai.tool_progress",
_SE.TODO_UPDATED: "praisonai.todo_updated",
}
def stream_event_to_agui_events(
event: Any,
message_id: str,
@@ -313,7 +366,60 @@ def stream_event_to_agui_events(
)
return events
return []
if event.type == StreamEventType.DELTA_TOOL_CALL:
tool_call = event.tool_call or {}
tool_call_id = buffer.resolve_tool_call_id(tool_call.get("id"))
delta = tool_call.get("arguments", event.content or "")
return [
ToolCallArgsEvent(
tool_call_id=tool_call_id,
delta=delta if isinstance(delta, str) else json.dumps(delta),
)
]
if event.type == StreamEventType.TOOL_CALL_END:
tool_call = event.tool_call or {}
tool_call_id = buffer.resolve_tool_call_id(tool_call.get("id"))
buffer.end_tool_call(tool_call_id)
return [ToolCallEndEvent(tool_call_id=tool_call_id)]
if event.type == StreamEventType.ERROR:
return [create_run_error_event(event.error or event.content or "unknown error")]
# Recoverable conditions. These are not RunErrorEvent: the run continues, but
# a client that is never told cannot explain a stall or a changed model.
if event.type in _RECOVERABLE_EVENTS:
return [
CustomEvent(
name=_RECOVERABLE_EVENTS[event.type],
value={
"message": event.error or event.content or "",
"metadata": event.metadata or {},
},
)
]
if event.type in _PROGRESS_EVENTS:
return [
CustomEvent(
name=_PROGRESS_EVENTS[event.type],
value={
"content": event.content,
"tool_call": event.tool_call,
"metadata": event.metadata or {},
},
)
]
if event.type in NOT_WIRE_VISIBLE:
return []
# Fail loudly rather than dropping: an unmapped type reaching here means a new
# StreamEventType was added without deciding its disposition.
raise ValueError(
f"StreamEventType.{getattr(event.type, 'name', event.type)} has no AG-UI "
"mapping and is not declared in NOT_WIRE_VISIBLE"
)
def _infer_a2ui_surface_id(result: Dict[str, Any]) -> str:
@@ -3259,43 +3259,69 @@ CONCISE SUMMARY:"""
List of items, or [text] if no list found
"""
import json
import re
if not text or not text.strip():
return []
text = text.strip()
# 1. Try direct JSON parse (pure JSON array)
# 1. Try direct JSON parse (pure JSON array).
# RecursionError guards against deeply-nested bracket input, which the
# C/Python JSON scanner surfaces instead of JSONDecodeError (CWE-674).
try:
parsed = json.loads(text)
if isinstance(parsed, list):
return parsed
except json.JSONDecodeError:
except (json.JSONDecodeError, RecursionError):
pass
# 2. Try to extract JSON array from text using regex
# Match JSON arrays like ["item1", "item2"] or ['item1', 'item2']
json_array_pattern = r'\[(?:[^\[\]]*(?:"[^"]*"|\'[^\']*\')?[^\[\]]*)*\]'
matches = re.findall(json_array_pattern, text)
# 2. Search for an embedded list without a backtracking regex.
# The previous nested-repeat pattern could take exponential time on
# adversarial bracket/quote input (CWE-1333/CWE-730). JSONDecoder
# validates each candidate using the JSON grammar. raw_decode is
# anchored with the ``idx`` argument so we never allocate an
# overlapping ``text[start:]`` copy per bracket. To bound the total
# work on bracket-dense adversarial output (e.g. deeply nested "[[[["
# where each raw_decode still rescans toward the end, giving O(n^2)),
# we cap both the input size and the number of decode attempts.
decoder = json.JSONDecoder()
max_scan_chars = 1_000_000
if len(text) > max_scan_chars:
logger.warning("Loop list text exceeds the safe parsing limit")
return [text]
max_decode_attempts = 10_000
for match in matches:
start = text.find('[')
attempts = 0
while start != -1 and attempts < max_decode_attempts:
attempts += 1
try:
# Try parsing as JSON
parsed = json.loads(match)
if isinstance(parsed, list) and len(parsed) > 0:
return parsed
except json.JSONDecodeError:
# Try replacing single quotes with double quotes
try:
fixed = match.replace("'", '"')
parsed = json.loads(fixed)
if isinstance(parsed, list) and len(parsed) > 0:
return parsed
except json.JSONDecodeError:
continue
parsed, _ = decoder.raw_decode(text, start)
except (json.JSONDecodeError, RecursionError):
start = text.find('[', start + 1)
continue
if isinstance(parsed, list) and len(parsed) > 0:
return parsed
start = text.find('[', start + 1)
# 3. Fallback: wrap as single item
# 3. Fallback for Python-style lists with single quotes, e.g.
# "['topic1', 'topic2']", which strict JSON rejects. ast.literal_eval
# only evaluates literals (no code execution) so it is safe here and
# preserves the single-quoted parsing the previous regex supported.
# A single attempt on the outermost bracket span keeps this linear;
# LLM output places the list contiguously rather than fragmented.
import ast
first = text.find('[')
last = text.rfind(']')
if first != -1 and last > first:
try:
parsed = ast.literal_eval(text[first:last + 1])
if isinstance(parsed, (list, tuple)) and len(parsed) > 0:
return list(parsed)
except (ValueError, SyntaxError, RecursionError):
pass
# 4. Fallback: wrap as single item
return [text]
def _execute_repeat(
@@ -4710,6 +4736,94 @@ class WorkflowManager:
else:
current_step_idx += 1
def _prepare_step_context(
self,
step: Task,
results: List[Dict[str, Any]],
all_variables: Dict[str, Any],
original_input: str,
) -> Tuple[Optional["WorkflowContext"], Optional[Dict[str, Any]]]:
"""Shared prep for the sync/async single-step runners.
Builds the handler ``WorkflowContext`` (only when a consumer exists) and
evaluates ``should_run``. Returns ``(context, early_result)`` where
``early_result`` is the skip return-dict when ``should_run`` gates the
step out, otherwise ``None`` and ``context`` is ready for handler use.
"""
# Get previous step output
previous_output = results[-1].get("output") if results else None
# Create context for step handlers (only when a consumer exists)
context = None
if step.should_run or step.handler:
context = WorkflowContext(
input=original_input,
previous_result=str(previous_output) if previous_output else None,
current_step=step.name,
variables=all_variables.copy()
)
# Check should_run condition if provided
if step.should_run:
try:
if not step.should_run(context):
return context, {
"success": True,
"output": None,
"skipped": True,
"stop": False
}
except Exception as e:
self._log(f"should_run check for step '{step.name}' failed: {e}")
return context, None
def _build_step_action(
self,
step: Task,
step_idx: int,
results: List[Dict[str, Any]],
all_variables: Dict[str, Any],
) -> str:
"""Shared prep: build step context, substitute variables, prepend context."""
# Build context from previous steps
context = self._build_step_context(step, step_idx, results, all_variables)
# Substitute variables in action
action = self._substitute_variables(step.action, all_variables)
# Prepend context to action if available
if context:
action = f"{context}# Current Task:\n{action}"
return action
def _finalize_step_result(
self,
step: Task,
output: Optional[str],
step_success: bool,
error: Optional[str],
all_variables: Dict[str, Any],
results: List[Dict[str, Any]],
on_result: Optional[Callable[[Task, str], None]],
) -> Dict[str, Any]:
"""Shared finalize for the sync/async single-step runners."""
# Update variables with step output
if output and step_success:
self._update_variables_with_output(step, output, all_variables, results)
# Callback after step
if on_result and output:
on_result(step, output)
return {
"success": step_success,
"output": output,
"stop": False, # Normal execution doesn't request stop
"error": error
}
def _execute_single_step(
self,
step: Task,
@@ -4727,32 +4841,13 @@ class WorkflowManager:
original_input: str = ""
) -> Dict[str, Any]:
"""Execute a single workflow step."""
# Get previous step output
previous_output = results[-1].get("output") if results else None
# Create context for step handlers (only when a consumer exists)
context = None
if step.should_run or step.handler:
context = WorkflowContext(
input=original_input,
previous_result=str(previous_output) if previous_output else None,
current_step=step.name,
variables=all_variables.copy()
)
# Check should_run condition if provided
if step.should_run:
try:
if not step.should_run(context):
return {
"success": True,
"output": None,
"skipped": True,
"stop": False
}
except Exception as e:
self._log(f"should_run check for step '{step.name}' failed: {e}")
# Shared prep: handler context + should_run gating
context, early_result = self._prepare_step_context(
step, results, all_variables, original_input
)
if early_result is not None:
return early_result
# If step has a custom handler function
if step.handler:
try:
@@ -4783,15 +4878,8 @@ class WorkflowManager:
"error": str(e)
}
# Build context from previous steps
context = self._build_step_context(step, step_idx, results, all_variables)
# Substitute variables in action
action = self._substitute_variables(step.action, all_variables)
# Prepend context to action if available
if context:
action = f"{context}# Current Task:\n{action}"
# Shared prep: build step context + substitute variables
action = self._build_step_action(step, step_idx, results, all_variables)
# Get or create executor for this step
step_executor = executor
@@ -4844,20 +4932,10 @@ class WorkflowManager:
if retries <= step.max_retries:
self._log(f"Step '{step.name}' failed, retrying ({retries}/{step.max_retries})")
# Update variables with step output
if output and step_success:
self._update_variables_with_output(step, output, all_variables, results)
# Callback after step
if on_result and output:
on_result(step, output)
return {
"success": step_success,
"output": output,
"stop": False, # Normal execution doesn't request stop
"error": error
}
# Shared finalize: variable propagation + on_result callback + return dict
return self._finalize_step_result(
step, output, step_success, error, all_variables, results, on_result
)
async def aexecute(
self,
@@ -4967,31 +5045,12 @@ class WorkflowManager:
"""
import asyncio
# Get previous step output
previous_output = results[-1].get("output") if results else None
# Create context for step handlers (only when a consumer exists)
context = None
if step.should_run or step.handler:
context = WorkflowContext(
input=original_input,
previous_result=str(previous_output) if previous_output else None,
current_step=step.name,
variables=all_variables.copy()
)
# Check should_run condition if provided
if step.should_run:
try:
if not step.should_run(context):
return {
"success": True,
"output": None,
"skipped": True,
"stop": False
}
except Exception as e:
self._log(f"should_run check for step '{step.name}' failed: {e}")
# Shared prep: handler context + should_run gating
context, early_result = self._prepare_step_context(
step, results, all_variables, original_input
)
if early_result is not None:
return early_result
# If step has a custom handler function
if step.handler:
@@ -5024,15 +5083,8 @@ class WorkflowManager:
"error": str(e)
}
# Build context from previous steps
context = self._build_step_context(step, step_idx, results, all_variables)
# Substitute variables in action
action = self._substitute_variables(step.action, all_variables)
# Prepend context to action if available
if context:
action = f"{context}# Current Task:\n{action}"
# Shared prep: build step context + substitute variables
action = self._build_step_action(step, step_idx, results, all_variables)
# Get or create executor for this step
step_executor = executor
@@ -5094,20 +5146,10 @@ class WorkflowManager:
if retries <= step.max_retries:
self._log(f"Step '{step.name}' failed, retrying ({retries}/{step.max_retries})")
# Update variables with step output
if output and step_success:
self._update_variables_with_output(step, output, all_variables, results)
# Callback after step
if on_result and output:
on_result(step, output)
return {
"success": step_success,
"output": output,
"stop": False,
"error": error
}
# Shared finalize: variable propagation + on_result callback + return dict
return self._finalize_step_result(
step, output, step_success, error, all_variables, results, on_result
)
def _create_step_agent(
self,
@@ -0,0 +1,101 @@
"""Every StreamEventType must have a declared AG-UI disposition.
The adapter's fall-through `return []` means an unmapped event type is dropped
with no error and no log. That is invisible for cosmetic events and data loss
for ERROR, RETRY, MODEL_FALLBACK and STREAM_UNAVAILABLE: the client sees a run
that ends cleanly having never been told anything went wrong.
This suite pins the disposition of all sixteen members. Adding a new
StreamEventType without deciding whether it crosses the wire fails the first
test, by construction.
"""
import pytest
from praisonaiagents.streaming.events import StreamEvent, StreamEventType
from praisonaiagents.ui.agui.streaming import (
EventBuffer,
NOT_WIRE_VISIBLE,
stream_event_to_agui_events,
)
def _event(event_type: StreamEventType) -> StreamEvent:
"""A populated event, so a drop is never merely an empty payload."""
return StreamEvent(
type=event_type,
content="text",
tool_call={"id": "call-1", "name": "f", "arguments": {}, "result": "r"},
error="boom",
)
def test_every_event_type_has_a_declared_disposition():
undeclared = [
t.name
for t in StreamEventType
if not stream_event_to_agui_events(_event(t), "m1", EventBuffer())
and t not in NOT_WIRE_VISIBLE
]
assert not undeclared, (
"these StreamEventTypes are silently dropped by the AG-UI adapter and are "
f"not declared in NOT_WIRE_VISIBLE: {undeclared}"
)
@pytest.mark.parametrize(
"event_type",
[
StreamEventType.ERROR,
StreamEventType.STREAM_UNAVAILABLE,
StreamEventType.RETRY,
StreamEventType.MODEL_FALLBACK,
],
)
def test_failure_events_reach_the_client(event_type):
"""A failure the engine recovered from is still news the client must get."""
out = stream_event_to_agui_events(_event(event_type), "m1", EventBuffer())
assert out, f"{event_type.name} produced no AG-UI event: the client is never told"
def test_not_wire_visible_is_a_closed_set_of_real_members():
"""Guards against silencing a type by adding a typo to the allowlist."""
for member in NOT_WIRE_VISIBLE:
assert isinstance(member, StreamEventType)
def test_mapped_events_still_map():
"""Positive control: the three handled types must keep working."""
for event_type in (
StreamEventType.DELTA_TEXT,
StreamEventType.TOOL_CALL_START,
StreamEventType.TOOL_CALL_RESULT,
):
assert stream_event_to_agui_events(_event(event_type), "m1", EventBuffer())
def test_tool_call_delta_id_survives_id_less_chunks():
"""Providers send the id once; later argument chunks must keep that id.
A fresh UUID per chunk would orphan the arguments from the invocation the
AG-UI client is assembling.
"""
buffer = EventBuffer()
start = StreamEvent(
type=StreamEventType.TOOL_CALL_START,
tool_call={"id": "call-42", "name": "f", "arguments": {}},
)
(start_out := stream_event_to_agui_events(start, "m1", buffer))
assert any(getattr(e, "tool_call_id", None) == "call-42" for e in start_out)
chunk = StreamEvent(
type=StreamEventType.DELTA_TOOL_CALL,
tool_call={"arguments": '{"x":'},
)
chunk_out = stream_event_to_agui_events(chunk, "m1", buffer)
assert [e.tool_call_id for e in chunk_out] == ["call-42"]
end = StreamEvent(type=StreamEventType.TOOL_CALL_END, tool_call={})
end_out = stream_event_to_agui_events(end, "m1", buffer)
assert [e.tool_call_id for e in end_out] == ["call-42"]
@@ -241,6 +241,36 @@ These topics are trending this week.
assert len(result) == 1
assert result == ['just a plain string']
def test_parse_single_quoted_python_list(self):
"""Python-style single-quoted lists must not collapse to one item."""
from praisonaiagents.workflows import Workflow
workflow = Workflow(steps=[])
result = workflow._parse_list_from_string("['topic1', 'topic2', 'topic3']")
assert result == ['topic1', 'topic2', 'topic3']
def test_parse_single_quoted_list_embedded_in_text(self):
"""Single-quoted list embedded in surrounding text is extracted."""
from praisonaiagents.workflows import Workflow
workflow = Workflow(steps=[])
result = workflow._parse_list_from_string("Topics: ['a', 'b'] found")
assert result == ['a', 'b']
def test_parse_adversarial_bracket_input_is_bounded(self):
"""Deeply nested/bracket-dense input must not hang or crash."""
import time
from praisonaiagents.workflows import Workflow
workflow = Workflow(steps=[])
text = '[' * 100_000 + 'x'
start = time.time()
result = workflow._parse_list_from_string(text)
assert time.time() - start < 15
assert result == [text]
class TestBeginnerFriendlyYAML:
"""Tests for beginner-friendly YAML syntax."""
@@ -0,0 +1,72 @@
"""Streaming must still produce an answer when a tool is invoked.
The streaming path collects tool calls, executes them, appends the results to
chat history -- and then ends. It never asks the model for the final answer, so
`start(stream=True)` yields nothing while the identical non-streaming call
answers normally.
There is no error. The caller sees an empty generator and cannot tell "the model
had nothing to say" from "the answer was never requested".
Exercised against the real path rather than a mocked client: the defect lives in
the interaction between tool execution and the follow-up completion, which is
precisely what a hand-built mock of that client would define away.
"""
import os
import pytest
from praisonaiagents import Agent
pytestmark = pytest.mark.skipif(
not os.environ.get("OPENAI_API_KEY"),
reason="exercises the real streaming path; needs a provider credential",
)
SENTINEL = "PRAISONAI-TOOL-SENTINEL"
def sentinel_tool() -> str:
"""Return a fixed marker so a passing assertion cannot be a coincidence."""
return SENTINEL
def _agent():
return Agent(name="T", role="Assistant", goal="Answer using your tools.",
llm="gpt-4o-mini", tools=[sentinel_tool])
def test_a_plain_turn_streams():
"""Positive control: without tools the path must be unaffected."""
chunks = [c for c in Agent(name="P", role="Assistant", goal="Answer.",
llm="gpt-4o-mini").start("Say hi", stream=True) if c]
assert chunks, "a plain streamed turn produced nothing; the harness is broken"
def test_a_tool_using_turn_still_streams_an_answer():
"""The regression: a tool round-trip must not swallow the reply.
Asserting the sentinel flows through the streamed answer proves the tool
branch actually ran -- a plain answer that never called the tool cannot
reproduce the marker, so a green test here cannot be a coincidence.
"""
streamed = "".join(c for c in _agent().start(
"Call sentinel_tool and tell me exactly what it returned.", stream=True) if c)
assert streamed, (
"streaming yielded nothing after a tool call; the follow-up completion "
"that produces the final answer was never requested"
)
assert SENTINEL in streamed, (
"the sentinel never reached the streamed answer; the tool result was "
"not carried into the follow-up completion's request context"
)
def test_streaming_and_non_streaming_agree_when_a_tool_runs():
"""The two paths must not disagree about whether an answer exists."""
blocking = _agent().start("Call sentinel_tool and report its output.")
streamed = "".join(c for c in _agent().start(
"Call sentinel_tool and report its output.", stream=True) if c)
assert blocking, "non-streaming produced nothing; the harness is broken"
assert streamed, "streaming produced nothing while non-streaming answered"
@@ -0,0 +1,86 @@
"""runtime= must normalise spelling and reject typos, not degrade silently.
Regression for #4229: an unrecognised runtime name was stored verbatim and
silently dropped nine of twelve capabilities. Spelling variants of a known
runtime must all resolve to the same canonical name; a close typo must raise;
a genuinely unknown plugin runtime name must still be accepted (it falls back
to the reduced harness through the runtime registry, not a closed literal).
"""
import pytest
from praisonaiagents.config.feature_configs import (
RuntimeConfig,
canonical_runtime_name,
resolve_runtime,
)
NATIVE_SPELLINGS = ["native", "NATIVE", " native ", "Native", "nAtIvE"]
@pytest.mark.parametrize("spelling", NATIVE_SPELLINGS)
def test_native_spelling_variants_canonicalise(spelling):
assert canonical_runtime_name(spelling) == "native"
assert resolve_runtime(spelling).preferred_runtime == "native"
@pytest.mark.parametrize(
"spelling", ["plugin-harness", "plugin_harness", "harness", "plugin", "reduced"]
)
def test_harness_aliases_canonicalise(spelling):
assert canonical_runtime_name(spelling) == "plugin-harness"
assert resolve_runtime(spelling).preferred_runtime == "plugin-harness"
@pytest.mark.parametrize("typo", ["nativ", "natve", "reducd", "harnes"])
def test_close_typo_raises_with_suggestion(typo):
with pytest.raises(ValueError):
canonical_runtime_name(typo)
def test_unknown_plugin_runtime_is_accepted_verbatim():
"""A genuinely unknown name is an opaque plugin runtime, not a typo."""
assert canonical_runtime_name("my-plugin-runtime") == "my-plugin-runtime"
cfg = resolve_runtime("my-plugin-runtime")
assert cfg.preferred_runtime == "my-plugin-runtime"
def test_runtime_config_input_is_normalised():
cfg = resolve_runtime(RuntimeConfig(preferred_runtime="NATIVE"))
assert cfg.preferred_runtime == "native"
def test_dict_input_is_normalised():
cfg = resolve_runtime({"preferred_runtime": " native "})
assert cfg.preferred_runtime == "native"
def test_runtime_config_input_is_not_mutated():
"""resolve_runtime returns a normalised copy; the caller's object stands."""
original = RuntimeConfig(preferred_runtime="NATIVE")
resolved = resolve_runtime(original)
assert original.preferred_runtime == "NATIVE"
assert resolved is not original
assert resolved.preferred_runtime == "native"
def test_close_typo_error_suggests_matched_alias():
with pytest.raises(ValueError) as exc:
canonical_runtime_name("reducd")
assert "reduced" in str(exc.value)
def test_agent_normalises_runtime_spelling():
"""Smoke: Agent construction canonicalises a spelling variant."""
from praisonaiagents import Agent
agent = Agent(instructions="test", runtime="NATIVE")
assert agent._runtime_config.preferred_runtime == "native"
def test_agent_rejects_runtime_config_typo():
"""A typo in a direct RuntimeConfig must raise, not degrade silently."""
from praisonaiagents import Agent
with pytest.raises(ValueError):
Agent(instructions="test", runtime=RuntimeConfig(preferred_runtime="nativ"))
@@ -0,0 +1,92 @@
"""`Memory` must not fall through to legacy SQL that bypasses the active adapter.
Two public methods still target the pre-adapter schema:
* `get_all_memories()` selects from `short_mem` / `long_mem`. Nothing in the
codebase creates those tables -- the adapter creates `short_term_memory` /
`long_term_memory` -- so the OperationalError is swallowed and it returns []
for every provider, always.
* `delete_memory(id)` with no `memory_type` returns on the first tier that
reports a hit. The SQLite adapter gives each tier its own AUTOINCREMENT
sequence, so the Nth short-term row and the Nth long-term row always share an
id. Deleting the id `remember()` handed back therefore destroys an unrelated
short-term row and leaves the caller's target in place, returning success.
Sibling methods (`delete_short_term`, `reset_short_term`) already delegate to the
adapter with explicit comments saying why; these two were missed.
"""
import pytest
from praisonaiagents.memory.memory import Memory
@pytest.fixture
def store(tmp_path, monkeypatch):
"""A Memory backed by a SQLite store isolated to this test.
The adapter resolves its database under ``./.praisonai``, so isolation means
changing the working directory -- passing ``storage_path`` in the config does
not move it, and without the chdir every test shares one store.
"""
monkeypatch.setenv("PRAISONAI_HOME", str(tmp_path / "home"))
monkeypatch.chdir(tmp_path)
return Memory(config={"provider": "sqlite"})
def _tier(mem, which):
return [r for r in mem.memory_adapter.get_all_memories()
if (r.get("memory_type") or r.get("type")) == which]
def test_adapter_holds_the_data(store):
"""Control: without this passing, the other assertions prove nothing."""
store.store_short_term("STM: user prefers dark mode")
store.store_long_term("LTM: user is in Chennai")
assert len(store.memory_adapter.get_all_memories()) == 2
def test_get_all_memories_returns_what_is_stored(store):
store.store_short_term("STM: user prefers dark mode")
store.store_long_term("LTM: user is in Chennai")
got = store.get_all_memories()
assert got, (
"get_all_memories() returned nothing while the adapter holds "
f"{len(store.memory_adapter.get_all_memories())} rows"
)
kinds = {r.get("memory_type") or r.get("type") for r in got}
assert kinds == {"short_term", "long_term"}
# Backward compatibility: the legacy path exposed each record's tier under
# the public `type` key, so adapter-backed records must too.
assert all(r.get("type") for r in got), (
"adapter-backed records dropped the public `type` field callers rely on"
)
assert {r["type"] for r in got} == {"short_term", "long_term"}
def test_delete_by_id_erases_the_long_term_record(store):
"""The id `remember()` returns must delete the record it refers to."""
short_id = store.store_short_term("turn 1: hello there")
long_id = store.remember("User SSN is 123-45-6789")
# Precondition. If the ids do not collide this test cannot detect the bug.
assert short_id == long_id, (
f"expected an id collision across tiers, got {short_id!r} / {long_id!r}"
)
store.forget(memory_id=long_id)
assert _tier(store, "long_term") == [], (
"the record the caller asked to erase is still present"
)
assert _tier(store, "short_term") == [], (
"the colliding short-term record was left behind"
)
def test_delete_without_collision_still_works(store):
"""Control: the no-collision case must keep behaving as before."""
long_id = store.remember("SECRET fact")
assert store.forget(memory_id=long_id) == 1
assert _tier(store, "long_term") == []
@@ -0,0 +1,57 @@
"""An unknown Agent kwarg must say what to do instead, not just that it is wrong.
`Agent` deliberately rejects unknown keyword arguments rather than swallowing
them -- that part is correct and worth keeping. But several names are near
universal in this ecosystem (`verbose` is accepted by six sibling classes here,
and by crewai and langchain), so users reach for them constantly. A bare
"unexpected keyword argument" leaves them with no next step, and the answer is
not discoverable from the 41-parameter signature.
"""
import pytest
from praisonaiagents import Agent
def _err(**kwargs) -> str:
with pytest.raises(TypeError) as exc:
Agent(name="A", role="R", goal="G", **kwargs)
return str(exc.value)
def test_unknown_kwargs_are_still_rejected():
"""Positive control: the fail-fast behaviour must not be softened into a warning."""
assert "definitely_not_a_param" in _err(definitely_not_a_param=1)
def test_verbose_explains_where_verbosity_actually_lives():
"""Verbosity was consolidated into `output=`; the error must say so.
Naming the wrong destination is worse than naming none: it sends the user
to a setting that does not control this.
"""
message = _err(verbose=False)
assert "verbose" in message
assert "output" in message.lower(), (
"the error names the bad argument but not the supported alternative; "
f"got: {message}"
)
def test_stream_also_points_at_output():
"""`stream` moved the same way, and internal callers were passing both."""
assert "output" in _err(stream=False).lower()
def test_the_guidance_names_the_offending_argument_not_a_generic_hint():
"""Guidance must be specific, or it is noise on every unrelated typo."""
generic = _err(some_typo=1)
assert "some_typo" in generic
assert "output=" not in generic, (
"an unrelated typo received the migration guidance meant for `verbose`"
)
def test_multiple_unknown_kwargs_are_all_listed():
message = _err(verbose=True, another_bad_one=2)
assert "verbose" in message and "another_bad_one" in message
@@ -95,6 +95,29 @@ class TestConfigurableModelProvider:
)
assert agent is not None
def test_agent_with_evolink_openai_compatible_config(self):
"""Agent should pass EvoLink OpenAI-compatible settings to LLM."""
from praisonaiagents import Agent
with patch("praisonaiagents.llm.llm.LLM") as mock_llm:
agent = Agent(
name="Test",
instructions="Test agent",
llm={
"model": "openai/gpt-5.2",
"api_key": "test-evolink-key",
"base_url": "https://direct.evolink.ai/v1",
},
)
assert agent is not None
assert agent.llm == "openai/gpt-5.2"
assert agent._llm_init_params["model"] == "openai/gpt-5.2"
assert agent._llm_init_params["api_key"] == "test-evolink-key"
assert agent._llm_init_params["base_url"] == "https://direct.evolink.ai/v1"
assert agent._using_custom_llm is True
mock_llm.assert_not_called()
class TestConfigurableModelThreadSafety:
"""Test thread safety of agent model usage."""
@@ -0,0 +1,137 @@
"""Every WebSocket message type the gateway dispatches must be scope-classified.
`resolve_required_scope` fails closed to ADMIN for anything it does not know, which
is the right default and is why it cannot simply be switched on: four of the six
message types the dispatcher handles today are unregistered, so enabling it would
demand ADMIN for the connection handshake and lock out every client.
This suite pins the two lists together. It is a lockstep test in jan's style --
it does not exercise behaviour, it asserts that two independently-maintained lists
agree, which is a failure mode no behavioural test can see: the code compiles, the
unit tests pass, and the omission only appears as a permission error at runtime.
"""
import re
from pathlib import Path
import pytest
from praisonaiagents.gateway.protocols import (
GATEWAY_METHODS,
EventType,
OperatorScope,
resolve_required_scope,
)
_SERVER = (
Path(__file__).resolve().parents[3]
/ "praisonai-bot"
/ "praisonai_bot"
/ "gateway"
/ "server.py"
)
# Types handled by the transport itself, before any authorization decision.
_TRANSPORT_TYPES = {"pong", "ping"}
_OPERAND = r'"[a-z_.]+"|EventType\.[A-Z_]+\.value'
def _resolve_operand(token: str) -> str | None:
"""Map one ``msg_type`` comparison operand to the wire string it matches.
A string literal is its own wire value; an ``EventType.<NAME>.value``
reference resolves through the enum exactly as Python would at runtime.
Anything else (an unknown enum member, a non-literal expression) yields
``None`` so it can be surfaced rather than silently dropped.
"""
literal = re.fullmatch(r'"([a-z_.]+)"', token)
if literal:
return literal.group(1)
enum_ref = re.fullmatch(r"EventType\.([A-Z_]+)\.value", token)
if enum_ref:
member = getattr(EventType, enum_ref.group(1), None)
if member is not None:
return member.value
return None
def _dispatched_message_types() -> set[str]:
"""Extract the msg_type branches the gateway's WS dispatcher acts on.
The dispatcher compares ``msg_type`` in two interchangeable styles:
* string literals — ``msg_type == "hello"``, ``msg_type in ("abort", ...)``;
* enum-derived — ``msg_type == EventType.PING.value``,
``msg_type in ("abort", EventType.MESSAGE_ABORT.value)``.
Recognising only the first style would let a branch written purely in the
second (as ``ping``/``pong`` already are) drift past this lockstep — a
classified method could go unregistered and surface only as a runtime
permission error. So every ``EventType.<NAME>.value`` operand is resolved
through the enum to its wire value and considered alongside the literals.
Within a single ``in (...)`` branch the operands are *aliases* for one
dispatch path (``"abort"`` and ``EventType.MESSAGE_ABORT.value`` route to
the same handler). The registry classifies the canonical name, so each
branch contributes one representative — the literal if present, else the
resolved enum value — rather than every alias as a distinct method.
"""
source = _SERVER.read_text()
found: set[str] = set()
for token in re.findall(rf"msg_type == ({_OPERAND})", source):
resolved = _resolve_operand(token)
if resolved is not None:
found.add(resolved)
for group in re.findall(r"msg_type in \(([^)]*)\)", source):
operands = [_resolve_operand(t) for t in re.findall(_OPERAND, group)]
resolved = [o for o in operands if o is not None]
if not resolved:
continue
literals = [
re.fullmatch(r'"([a-z_.]+)"', t).group(1)
for t in re.findall(_OPERAND, group)
if re.fullmatch(r'"[a-z_.]+"', t)
]
found.add(literals[0] if literals else resolved[0])
return found - _TRANSPORT_TYPES
@pytest.mark.skipif(not _SERVER.exists(), reason="praisonai-bot not present in this checkout")
def test_the_dispatcher_handles_types_this_test_can_see():
"""Positive control: if the extraction breaks, every other test here passes vacuously."""
types = _dispatched_message_types()
assert len(types) >= 4, f"extraction found only {types}; the regex has drifted from the source"
assert "message" in types, "the most basic message type was not extracted"
# ``abort`` is dispatched via a mixed-style branch
# (``msg_type in ("abort", EventType.MESSAGE_ABORT.value)``); seeing it
# proves the enum-derived operand resolution works, so a future branch
# written enum-only cannot silently drift past this lockstep.
assert "abort" in types, "the enum-derived branch resolution has drifted from the source"
@pytest.mark.skipif(not _SERVER.exists(), reason="praisonai-bot not present in this checkout")
def test_every_dispatched_message_type_is_scope_classified():
unregistered = sorted(t for t in _dispatched_message_types() if t not in GATEWAY_METHODS)
assert not unregistered, (
"these WebSocket message types are dispatched but carry no scope classification, "
f"so resolve_required_scope() would fail them closed to ADMIN: {unregistered}. "
"Register each via register_gateway_method() before wiring the resolver into dispatch."
)
def test_an_unknown_method_still_fails_closed():
"""The property the whole design rests on. Must survive any registration change."""
for unknown in ("sessions.list", "config.write", "definitely.not.a.method", ""):
assert resolve_required_scope(unknown) is OperatorScope.ADMIN
def test_the_handshake_is_not_accidentally_admin():
"""`hello` runs before a client can hold any scope; requiring ADMIN would lock everyone out."""
if "hello" not in GATEWAY_METHODS:
pytest.skip("hello not yet registered")
assert resolve_required_scope("hello") is not OperatorScope.ADMIN
@@ -468,3 +468,204 @@ class TestRegistryPermissionResolver:
defs = reg.get_tool_definitions()
names = {d.get("function", {}).get("name") for d in defs}
assert "read_file" in names
class TestApprovalConfigNeverWeakensDefault:
"""Issue #4228 (a): configuring approval must not drop the default denials."""
def test_approval_config_keeps_default_denials(self):
"""ApprovalConfig(timeout=30) is a configuration change, not a security decision."""
from praisonaiagents import Agent
from praisonaiagents.approval.protocols import ApprovalConfig
default = Agent(name="test", instructions="test")._perm_deny
configured = Agent(
name="test", instructions="test", approval=ApprovalConfig(timeout=30)
)._perm_deny
assert configured >= default
assert "execute_command" in configured
assert "delete_file" in configured
def test_dict_config_keeps_default_denials(self):
from praisonaiagents import Agent
default = Agent(name="test", instructions="test")._perm_deny
configured = Agent(
name="test", instructions="test", approval={"timeout": 30}
)._perm_deny
assert configured >= default
assert "execute_command" in configured
def test_config_with_permissions_defers_to_manager(self):
"""An explicit declarative policy owns denial; the preset must not shadow it."""
from praisonaiagents import Agent
from praisonaiagents.approval.protocols import ApprovalConfig
agent = Agent(
name="test",
instructions="test",
approval=ApprovalConfig(permissions={"bash:rm *": "deny"}),
)
assert agent._perm_deny == frozenset()
assert agent._permission_manager is not None
def test_env_off_switch_still_disables_denials(self, monkeypatch):
"""PRAISONAI_TOOL_SAFETY=off remains a full bypass even with a config object."""
from praisonaiagents import Agent
from praisonaiagents.approval.protocols import ApprovalConfig
monkeypatch.setenv("PRAISONAI_TOOL_SAFETY", "off")
agent = Agent(
name="test", instructions="test", approval=ApprovalConfig(timeout=30)
)
assert agent._perm_deny == frozenset()
def test_explicit_empty_permissions_policy_owns_denial(self):
"""``permissions={}`` is an intentional (empty) policy, not an absent one.
An empty mapping must NOT inherit the default deny set — the caller
explicitly opted into a declarative policy that happens to allow all.
Only a genuinely absent policy (``permissions=None``) falls back to the
env-driven default denials.
"""
from praisonaiagents import Agent
from praisonaiagents.approval.protocols import ApprovalConfig
empty_config = Agent(
name="test", instructions="test",
approval=ApprovalConfig(permissions={}),
)._perm_deny
empty_dict = Agent(
name="test", instructions="test",
approval={"permissions": {}},
)._perm_deny
assert empty_config == frozenset()
assert empty_dict == frozenset()
class TestArgumentScopedDeny:
"""Issue #4228 (b): argument-level deny rules must reach the enforcement path."""
def _agent_with_rule(self, tmp_path, pattern, action="deny"):
from praisonaiagents import Agent
from praisonaiagents.permissions import PermissionManager
mgr = PermissionManager(storage_dir=str(tmp_path))
mgr.load_rules_from_config({pattern: action})
agent = Agent(name="test", instructions="test")
agent._perm_deny = frozenset()
agent._perm_allow = None
agent._permission_manager = mgr
return agent
def test_argument_scoped_deny_blocks_only_matching_call(self, tmp_path):
"""The rule engine matches arguments; the enforcement path must ask it."""
agent = self._agent_with_rule(tmp_path, "bash:rm *")
blocked = agent._check_tool_approval_sync(
"execute_command", {"command": "rm -rf /tmp/x"}
)
assert isinstance(blocked, dict)
assert blocked.get("permission_denied") is True
def test_argument_scoped_deny_allows_non_matching_call(self, tmp_path):
from praisonaiagents.approval.protocols import ApprovalDecision
agent = self._agent_with_rule(tmp_path, "bash:rm *")
with patch(
"praisonaiagents.approval.get_approval_registry"
) as get_reg:
reg = MagicMock()
reg.approve_sync.return_value = ApprovalDecision(
approved=True, reason="mock"
)
reg.mark_approved = MagicMock()
get_reg.return_value = reg
result = agent._check_tool_approval_sync(
"execute_command", {"command": "ls -la"}
)
assert result is None or (isinstance(result, tuple) and len(result) == 2)
def test_argument_scoped_deny_async(self, tmp_path):
import asyncio
agent = self._agent_with_rule(tmp_path, "bash:rm *")
blocked = asyncio.run(
agent._check_tool_approval_async(
"execute_command", {"command": "rm -rf /tmp/x"}
)
)
assert isinstance(blocked, dict)
assert blocked.get("permission_denied") is True
def test_name_level_deny_still_blocks_all(self, tmp_path):
"""Backward compat: a bare name rule keeps its all-or-nothing behaviour."""
agent = self._agent_with_rule(tmp_path, "execute_command")
for command in ("rm -rf /tmp/x", "ls -la"):
result = agent._check_tool_approval_sync(
"execute_command", {"command": command}
)
assert isinstance(result, dict)
assert result.get("permission_denied") is True
def test_malformed_argument_key_cannot_evade_scoped_deny(self, tmp_path):
"""A trailing-'=' kwarg (``command=``) normalises before dispatch, so the
deny gate must normalise it too — otherwise the scoped rule is bypassed
while the command still runs after ``_cast_arguments`` cleans the key."""
agent = self._agent_with_rule(tmp_path, "bash:rm *")
blocked = agent._check_tool_approval_sync(
"execute_command", {"command=": "rm -rf /tmp/x"}
)
assert isinstance(blocked, dict)
assert blocked.get("permission_denied") is True
def test_modified_args_rechecked_against_scoped_deny(self, tmp_path):
"""An approval backend rewriting args into a denied command must be
re-authorized — the first gate only saw the original (allowed) args."""
from praisonaiagents.approval.protocols import ApprovalDecision
agent = self._agent_with_rule(tmp_path, "bash:rm *")
with patch("praisonaiagents.approval.get_approval_registry") as get_reg:
reg = MagicMock()
reg.mark_approved = MagicMock()
get_reg.return_value = reg
with patch.object(
agent, "_resolve_approval_decision",
return_value=ApprovalDecision(
approved=True, reason="mock",
modified_args={"command": "rm -rf /tmp/x"},
),
):
result = agent._check_tool_approval_sync(
"execute_command", {"command": "ls -la"}
)
assert isinstance(result, dict)
assert result.get("permission_denied") is True
def test_modified_args_rechecked_async(self, tmp_path):
import asyncio
from praisonaiagents.approval.protocols import ApprovalDecision
agent = self._agent_with_rule(tmp_path, "bash:rm *")
async def _run():
with patch("praisonaiagents.approval.get_approval_registry") as get_reg:
reg = MagicMock()
reg.mark_approved = MagicMock()
get_reg.return_value = reg
with patch.object(
agent, "_resolve_approval_decision"
) as resolve:
async def _decide(*a, **k):
return ApprovalDecision(
approved=True, reason="mock",
modified_args={"command": "rm -rf /tmp/x"},
)
resolve.side_effect = _decide
return await agent._check_tool_approval_async(
"execute_command", {"command": "ls -la"}
)
result = asyncio.run(_run())
assert isinstance(result, dict)
assert result.get("permission_denied") is True
@@ -0,0 +1,100 @@
"""Async twin of test_non_idempotent_tool_not_retried.py.
`@tool(restart_safe=False)` is the documented contract for "an effectful tool
that must never be silently re-executed" (tools/decorator.py:300-307). The sync
retry loops honour it; `_execute_tool_async_with_retry` never consulted the
declaration, so an async agent re-ran the tool body up to `max_attempts` times.
"""
import asyncio
import pytest
from praisonaiagents import Agent
from praisonaiagents.config.feature_configs import ToolConfig
from praisonaiagents.errors import ToolExecutionError
from praisonaiagents.tools.retry import RetryPolicy
# The effectful tool raises ``RuntimeError``. The async path flattens it into an
# error dict (nothing surfaces), while the sync path re-raises it wrapped in a
# ``ToolExecutionError``. Catch *only* those two expected failures so an unrelated
# setup error can never masquerade as a valid execution count.
EXPECTED_FAILURE = (RuntimeError, ToolExecutionError)
ATTEMPTS = 3
FAST = ToolConfig(retry_policy=RetryPolicy(max_attempts=ATTEMPTS, initial_delay_ms=0))
def _charges(declare, is_async, attr="restart_safe"):
"""Run a failing effectful tool once; return how many times the body ran."""
charged = []
def charge_card(amount: str) -> str:
"""Charge a customer's card."""
charged.append(amount)
raise RuntimeError("gateway 502 from acquirer")
if declare is not None:
setattr(charge_card, attr, declare)
agent = Agent(instructions="x", tools=[charge_card], tool_config=FAST)
try:
if is_async:
asyncio.run(agent.execute_tool_async("charge_card", {"amount": "100"}))
else:
agent.execute_tool("charge_card", {"amount": "100"})
except EXPECTED_FAILURE:
pass
return len(charged)
@pytest.mark.parametrize("attr", ["restart_safe", "idempotent"])
def test_async_honours_the_declaration(attr):
assert _charges(False, is_async=True, attr=attr) == 1, (
f"a tool declared {attr}=False was re-run on the async path"
)
@pytest.mark.parametrize("attr", ["restart_safe", "idempotent"])
def test_async_matches_sync(attr):
assert _charges(False, is_async=True, attr=attr) == _charges(False, is_async=False, attr=attr)
@pytest.mark.parametrize("declare", [None, True])
def test_undeclared_and_safe_tools_still_retry_on_async(declare):
"""Controls: the veto must not blanket-disable async retry."""
assert _charges(declare, is_async=True) == ATTEMPTS
@pytest.mark.parametrize("declare", [None, True])
def test_sync_controls_unchanged(declare):
assert _charges(declare, is_async=False) == ATTEMPTS
@pytest.mark.parametrize("attr", ["restart_safe", "idempotent"])
def test_async_honours_declaration_on_override_only_tool(attr):
"""A task-scoped tool supplied *only* via ``tools_override`` (never registered
on the agent) must still be vetoed. ``_execute_tool_async_impl`` resolves it
from the override list, so the idempotency check has to see it there too --
otherwise the effectful body is retried ``max_attempts`` times.
"""
charged = []
def charge_card(amount: str) -> str:
"""Charge a customer's card."""
charged.append(amount)
raise RuntimeError("gateway 502 from acquirer")
setattr(charge_card, attr, False)
# Agent has NO tools of its own; the effectful tool arrives only at call time.
agent = Agent(instructions="x", tools=[], tool_config=FAST)
try:
asyncio.run(agent.execute_tool_async(
"charge_card", {"amount": "100"}, tools_override=[charge_card]
))
except EXPECTED_FAILURE:
pass
assert len(charged) == 1, (
f"an override-only tool declared {attr}=False was re-run on the async path"
)
+80 -2
View File
@@ -17,11 +17,44 @@ from __future__ import annotations
import logging
from dataclasses import dataclass
from enum import Enum
from typing import Any, Callable, Coroutine, Optional
logger = logging.getLogger(__name__)
class AckScope(str, Enum):
"""Scope gate for ack reactions.
Controls which inbound messages get an ack reaction so busy groups
aren't spammed with ⏳ on ambient chatter the bot never answers.
- ``off``: never ack.
- ``direct``: ack direct/DM messages only.
- ``group_mentions``: ack DMs plus group messages that mention/reply the
bot (default). Ambient group chatter is not acked.
- ``group_all``: ack DMs plus any non-ambient group message.
- ``all``: ack every message (legacy all-or-nothing behaviour).
"""
off = "off"
direct = "direct"
group_mentions = "group-mentions"
group_all = "group-all"
all = "all"
@classmethod
def coerce(cls, value: Any, default: "AckScope") -> "AckScope":
"""Best-effort parse from a string/enum, falling back to ``default``."""
if isinstance(value, cls):
return value
if isinstance(value, str):
normalized = value.strip().lower().replace("_", "-")
for member in cls:
if member.value == normalized:
return member
return default
@dataclass
class AckContext:
"""Tracks state for a single ack-reaction lifecycle."""
@@ -35,15 +68,60 @@ class AckReactor:
Args:
ack_emoji: Emoji to react with on message receipt (empty = disabled).
done_emoji: Emoji to react with on completion (default "✅").
scope: Which messages to ack (see :class:`AckScope`). Defaults to
``group-mentions`` so ambient group chatter is not acked.
"""
def __init__(self, ack_emoji: str = "", done_emoji: str = "✅") -> None:
def __init__(
self,
ack_emoji: str = "",
done_emoji: str = "✅",
scope: Any = AckScope.group_mentions,
) -> None:
self._ack_emoji = ack_emoji
self._done_emoji = done_emoji
self._scope = AckScope.coerce(scope, AckScope.group_mentions)
@property
def enabled(self) -> bool:
return bool(self._ack_emoji)
return bool(self._ack_emoji) and self._scope is not AckScope.off
@property
def scope(self) -> AckScope:
return self._scope
def should_ack(
self, *, is_direct: bool, is_mention: bool, is_ambient: bool
) -> bool:
"""Whether a message matching this addressing context should be acked."""
if not self._ack_emoji or self._scope is AckScope.off:
return False
if self._scope is AckScope.all:
return True
if self._scope is AckScope.direct:
return is_direct
if self._scope is AckScope.group_mentions:
return is_direct or is_mention
if self._scope is AckScope.group_all:
return is_direct or not is_ambient
return False
def should_ack_message(self, message: Any, *, is_mention: bool = False) -> bool:
"""Derive addressing context from a ``BotMessage`` and gate the ack.
Direct-vs-group is read from ``message.channel.channel_type``; a
mention/reply is either passed explicitly by the adapter or inferred
from ``message.reply_to``. Ambient == a group message that neither
mentions nor replies the bot.
"""
channel = getattr(message, "channel", None)
channel_type = getattr(channel, "channel_type", "") if channel else ""
is_direct = str(channel_type).lower() in ("dm", "private", "direct", "")
mentioned = bool(is_mention) or bool(getattr(message, "reply_to", None))
is_ambient = not is_direct and not mentioned
return self.should_ack(
is_direct=is_direct, is_mention=mentioned, is_ambient=is_ambient
)
async def ack(
self,
@@ -60,6 +60,7 @@ def _serialize_request(request: ApprovalRequest) -> str:
"session_id": request.session_id,
"context": request.context,
"approval_id": request.approval_id,
"authorized_reviewers": request.authorized_reviewers,
},
default=str,
)
@@ -75,6 +76,7 @@ def _deserialize_request(raw: str) -> ApprovalRequest:
session_id=data.get("session_id"),
context=data.get("context", {}),
approval_id=data.get("approval_id"),
authorized_reviewers=data.get("authorized_reviewers"),
)
+94 -1
View File
@@ -107,12 +107,105 @@ def chunk_message(
return chunks if chunks else [""]
def enforce_hard_cap(
chunks: List[str],
max_length: int,
length_unit: str = "codepoints",
) -> List[str]:
"""Guarantee every chunk fits within *max_length*.
``chunk_message`` intentionally emits an over-cap chunk when a single code
fence exceeds *max_length* (``preserve_fences``). No platform client splits
an oversized payload, so sending such a chunk raises "message too long" and
the whole reply is lost. Callers apply this final pass so delivery always
succeeds: any over-cap chunk is hard-split, and a split fence has its
``` markers re-balanced so each piece stays valid markdown (Issue #4319).
Returns:
A list of chunks each at most *max_length* long.
"""
result: List[str] = []
for chunk in chunks:
if _calculate_length(chunk, length_unit) <= max_length:
result.append(chunk)
continue
if _is_pure_fence_block(chunk):
result.extend(_split_fence_block(chunk, max_length, length_unit))
else:
result.extend(_split_long_paragraph(chunk, max_length, length_unit))
return result
def _split_fence_block(
text: str, max_length: int, length_unit: str = "codepoints"
) -> List[str]:
"""Hard-split an over-cap code fence, re-balancing ``` on each piece.
Only ever called on a *pure* fence block (see ``_is_pure_fence_block``):
a single opening fence, an optional trailing close, and no interior fence
markers. Wrapping the body in a fresh fence per piece is therefore safe and
never turns prose into code (Issue #4319).
"""
stripped = text.strip()
lines = stripped.split("\n")
# Opening fence line carries the optional language hint (e.g. ```python).
fence_open = lines[0] if lines and lines[0].startswith("```") else "```"
fence_lang = fence_open[3:].strip()
reopen = "```" + fence_lang if fence_lang else "```"
# Body excludes the opening fence and a trailing closing fence if present.
body_lines = lines[1:]
if body_lines and body_lines[-1].strip() == "```":
body_lines = body_lines[:-1]
body = "\n".join(body_lines)
# Reserve room for the wrapping fences on each emitted piece.
wrapper = _calculate_length(reopen + "\n" + "\n```", length_unit)
inner_cap = max(1, max_length - wrapper)
pieces = _split_long_paragraph(body, inner_cap, length_unit)
return [f"{reopen}\n{piece}\n```" for piece in pieces if piece]
def _is_fence_block(text: str) -> bool:
"""Check if text is (or contains) a code fence block."""
"""Check if text starts with a code fence marker."""
stripped = text.strip()
return stripped.startswith("```")
def _is_pure_fence_block(text: str) -> bool:
"""True only for a single, self-contained code fence.
A pure fence opens with ``` on its first line and contains no further fence
markers except an optional closing ``` on the last line. Mixed content —
a closed fence followed by prose, or multiple fences — is NOT pure: wrapping
it in a fresh fence would render prose as code and corrupt the reply. Such
chunks are hard-split as plain text instead (Issue #4319).
"""
stripped = text.strip()
if not stripped.startswith("```"):
return False
lines = stripped.split("\n")
# Count lines that are exactly a fence marker (``` optionally + language).
fence_marker_lines = [
i for i, line in enumerate(lines) if line.strip().startswith("```")
]
# Opening fence must be the first line.
if not fence_marker_lines or fence_marker_lines[0] != 0:
return False
# Allowed: just the opener (unterminated fence), or opener + a closing
# marker on the final line. Anything else means interior/extra fences.
if len(fence_marker_lines) == 1:
return True
if len(fence_marker_lines) == 2 and fence_marker_lines[1] == len(lines) - 1:
return True
return False
def _split_long_paragraph(text: str, max_length: int, length_unit: str = "codepoints") -> List[str]:
"""Split a long paragraph at sentence boundaries, then hard-split."""
# Try sentence boundaries first
@@ -128,7 +128,6 @@ class CommandRegistry:
self.register("new", {"description": "Reset conversation session", "builtin": True})
self.register("stop", {"description": "Cancel current agent task", "builtin": True})
self.register("whoami", {"description": "Show your user info and permissions", "builtin": True})
self.register("sethome", {"description": "Set this chat as the home channel for scheduled deliveries", "builtin": True})
# New runtime control commands
self.register("model", {"description": "Switch LLM model for this session", "builtin": True})
self.register("usage", {"description": "Show token usage and estimated cost", "builtin": True})
@@ -3,9 +3,10 @@ Durable inbound webhook/trigger idempotency store for the PraisonAI gateway.
The gateway's inbound HTTP hook surface deduplicates provider redeliveries so a
webhook that re-POSTs an already-processed event does not start a second agent
run (a duplicate reply, or a duplicate tool action). The core in-memory default
run (a duplicate reply, or a duplicate tool action). The core in-memory store
(``InMemoryIdempotencyStore``) is per-process and empty after a restart, so this
SQLite-backed store persists the dedup key — mirroring the durability the
SQLite-backed store — selected by default (Issue #4339) — persists the dedup key,
mirroring the durability the
``InboundJournal`` and ``OutboundQueue`` already provide — so the dedup window
survives a restart within a provider's retry window and, when the same DB file
is shared, across replicas.
@@ -191,7 +192,7 @@ class SqliteIdempotencyStore:
def build_idempotency_store(
backend: str,
backend: Union[str, None] = None,
*,
path: Union[str, Path, None] = None,
max_size: int = _DEFAULT_MAX_SIZE,
@@ -200,10 +201,15 @@ def build_idempotency_store(
):
"""Build an idempotency store for the given ``backend``.
``"memory"`` (default) returns the core in-memory store; ``"sqlite"``
returns the durable :class:`SqliteIdempotencyStore`. On any failure to build
the durable store, falls back to the in-memory default so inbound delivery
keeps working (dedup is best-effort within the process, as before).
Durable by default (Issue #4339): when ``backend`` is unset (``None``) a
durable :class:`SqliteIdempotencyStore` is chosen so an out-of-box gateway
survives a restart within a provider's retry window rather than silently
re-processing a redelivered webhook. ``"memory"`` stays available as an
**explicit** opt-in for tests / ephemeral runs, and ``"sqlite"`` forces the
durable store. If the durable store genuinely cannot be initialised, this
records a *durability-degraded* fact (surfaced by ``gateway doctor`` /
``gateway status``) and falls back to in-memory so inbound delivery keeps
working — degradation is reported, not silent.
"""
from praisonaiagents.gateway import InMemoryIdempotencyStore
@@ -212,26 +218,47 @@ def build_idempotency_store(
max_entries=max_size, ttl_seconds=ttl_seconds
)
def _default_path() -> Path:
return Path.home() / ".praisonai" / "state" / "hook_idempotency.sqlite"
# ``None`` means "no explicit choice" → durable by default. ``"memory"``
# must be asked for; only then do we run non-durably by intent.
if backend is None:
backend = "sqlite"
backend = (backend or "memory").lower()
if backend == "memory":
return _memory()
if backend == "sqlite":
try:
store_path = path or (
Path.home() / ".praisonai" / "state" / "hook_idempotency.sqlite"
)
return SqliteIdempotencyStore(
store_path = Path(path) if path else _default_path()
store = SqliteIdempotencyStore(
store_path,
max_size=max_size,
ttl_seconds=ttl_seconds,
inflight_lease_seconds=inflight_lease_seconds,
)
except Exception as e: # pragma: no cover - defensive
# The durable store came up (fresh start, or a recovery after a
# prior failure/hot-reload): clear any stale degraded fact so a
# recovered gateway stops reporting non-durable idempotency (#4339).
from ._session import clear_durability_degraded
clear_durability_degraded("idempotency")
return store
except Exception as e:
# Keep the raw exception (which may embed a filesystem path) in the
# log only; the operator-facing reason stays redacted so health()
# and ``gateway status`` never echo backend paths (#4339).
logger.warning(
"SqliteIdempotencyStore unavailable, falling back to in-memory "
"inbound dedup: %s",
e,
)
from ._session import record_durability_degraded
record_durability_degraded(
"idempotency",
reason="durable idempotency store unavailable (running in-memory)",
)
return _memory()
# "redis" (multi-replica) is not yet implemented in the wrapper; fall back
# to the durable SQLite store if a path is available, else in-memory.
+377 -10
View File
@@ -16,6 +16,7 @@ calls from the same user; different users run in parallel.
from __future__ import annotations
import asyncio
import contextvars
import logging
import re
import time
@@ -30,9 +31,95 @@ from ._admission import AdmissionRejected
if TYPE_CHECKING:
from praisonaiagents import Agent
from praisonaiagents.gateway import WarmSession, MemoryPressureProtocol
logger = logging.getLogger(__name__)
# --------------------------------------------------------------------------
# Durability degradation registry (Issue #4339)
#
# Gateway state (webhook idempotency, session history) is durable by default.
# When a durable store genuinely cannot be initialised the boundary that owns
# it records a *durability-degraded* fact here instead of only logging a
# warning, so the gateway can surface it through the surfaces operators already
# read (``gateway doctor`` / ``gateway status``) — the same treatment channel
# credential degradation already gets. This reuses the core degraded-owner
# vocabulary (``owner_kind="gateway"``) so no new protocol is introduced; the
# gateway's ``health()`` merges this process-local registry alongside its own.
# --------------------------------------------------------------------------
try:
from praisonaiagents.gateway import DegradedCapabilityRegistry as _DegRegistry
_DURABILITY_REGISTRY: Optional[Any] = _DegRegistry()
except Exception: # pragma: no cover - core registry unavailable
_DURABILITY_REGISTRY = None
def record_durability_degraded(
component: str,
*,
reason: str,
recovery: str = "praisonai gateway doctor --fix",
) -> None:
"""Record that a durable store fell back to non-durable, in-memory operation.
``component`` is the owned state that lost durability (e.g. ``"session"`` or
``"idempotency"``); ``reason`` is a redacted, operator-safe explanation.
Best-effort and never raises into the caller's hot path — if the core
registry is unavailable this is a no-op (behaviour then matches the old
log-only path).
"""
registry = _DURABILITY_REGISTRY
if registry is None:
return
try:
from praisonaiagents.gateway import DegradedOwner
registry.mark(
DegradedOwner(
owner_kind="gateway",
owner_id=f"durability:{component}",
state="cold",
reason=reason,
retry_hint=recovery,
)
)
except Exception: # pragma: no cover - defensive
pass
def clear_durability_degraded(component: str) -> None:
"""Clear a previously-recorded durability degradation for ``component``."""
registry = _DURABILITY_REGISTRY
if registry is None:
return
try:
registry.clear("gateway", f"durability:{component}")
except Exception: # pragma: no cover - defensive
pass
def durability_degraded_owners() -> List[Any]:
"""Return the current durability-degraded owners (empty when all durable)."""
registry = _DURABILITY_REGISTRY
if registry is None:
return []
try:
return list(registry.list_degraded())
except Exception: # pragma: no cover - defensive
return []
# Turn-local resolved profile namespace (Issue #4341). A ``chat()`` turn binds
# the profile it resolved into this ContextVar for the duration of that turn, so
# every ``_storage_key`` recomputation after an await point uses *this* turn's
# tenant scope — never a value another concurrent route staged in the meantime.
# ContextVars are copied per task, so overlapping routed turns each see their own
# binding. ``None`` means unscoped (fail-closed: an unscoped turn never borrows
# another tenant's namespace).
_ACTIVE_PROFILE_NS: "contextvars.ContextVar[Optional[str]]" = contextvars.ContextVar(
"praisonai_bot_active_profile_ns", default=None
)
# Matches one or more leading arrival-time prefixes of the form
# ``[… YYYY-MM-DD HH:MM …]`` (Issue #2834). Kept deliberately narrow — it only
# strips bracketed groups that contain a ``YYYY-MM-DD HH:MM`` date-time — so a
@@ -139,6 +226,17 @@ class BotSessionManager:
self._platform = platform
self._last_active: Dict[str, float] = {}
self._last_reset: Dict[str, float] = {} # Track last reset time per user
# Issue #3804: per-session in-flight turn counter (storage_key -> depth).
# A key is present with a positive count while a turn is executing
# against that cache; the memory-pressure sweep skips any such key so a
# live turn is never aborted. Empty in normal steady state.
self._in_flight: Dict[str, int] = {}
# Issue #3804: per-session durable-flush marker (storage_key present iff
# the in-memory cache is known to match a completed store write). Cleared
# before each store write and set only after it fully succeeds, so a
# failed/partial persist leaves the key absent and the memory-pressure
# sweep never evicts the only current copy (would lose data).
self._flushed: set[str] = set()
# N4: optional inbound DLQ — when set, failed agent.chat() calls
# are persisted for later replay. Default ``None`` preserves
# legacy behaviour (exception bubbles up untouched).
@@ -205,6 +303,19 @@ class BotSessionManager:
# Keyed by ``id(agent)`` so a shared session serving multiple agents
# never crosses policies.
self._pending_tool_policies: Dict[int, Any] = {}
# Per-tenant profile namespace staged by a routing handler (Issue #4341).
# When a route resolves to a ``profile``, the gateway stages its
# namespace here (keyed by ``id(agent)``, exactly like the tool-policy
# seam) so the very next ``chat()`` for that agent enters the tenant
# scope for the turn. ``chat()`` consumes-and-clears it and binds the
# value into a per-turn ContextVar, so overlapping routed turns never
# share a namespace. Empty/``None`` means unscoped — a route with no
# profile never borrows another tenant's memory (fail-closed).
self._pending_profile_namespaces: Dict[int, str] = {}
# Fallback staging for callers that stage without an agent (or read
# ``_storage_key`` outside a ``chat()`` turn): the most-recently staged
# namespace, consumed once by the next ``chat()`` on any agent.
self._staged_profile_namespace: Optional[str] = None
# Session reset policy for automatic lifecycle management
self._reset_policy = reset_policy or SessionResetPolicy(mode="none")
# Per-user last agent-emitted presentation. ``chat()`` keeps the return
@@ -506,13 +617,34 @@ class BotSessionManager:
if self._scope_for(effective_chat_type) == "per_chat" and chat_id:
prefix = self._platform or "bot"
account_key = account or "default"
return f"{prefix}:acct:{account_key}:chat:{chat_id}:{thread_id}"
if self._identity_resolver is not None and self._platform:
base = f"{prefix}:acct:{account_key}:chat:{chat_id}:{thread_id}"
elif self._identity_resolver is not None and self._platform:
try:
return self._identity_resolver.resolve(self._platform, user_id)
base = self._identity_resolver.resolve(self._platform, user_id)
except Exception as e: # pragma: no cover — defensive
logger.warning("identity resolver failed: %s", e)
return user_id
base = user_id
else:
base = user_id
# Per-tenant memory isolation (Issue #4341): when a route resolved to a
# ``profile``, its namespace is bound turn-locally in ``_ACTIVE_PROFILE_NS``
# so every storage key is prefixed with the tenant scope. Reading the
# ContextVar (not a shared field) means a key recomputed after an await
# point always uses *this* turn's tenant — an overlapping route for
# another tenant can never rewrite it. This keys memory/session state per
# profile so two routes on one process never share a transcript. A route
# with no profile stays unprefixed and never falls into a tenant's scope.
# Prefer the turn-local binding; fall back to the instance staging only
# when no turn is active (e.g. a direct ``_storage_key`` read before
# ``chat()`` runs). The ContextVar never leaks across turns because
# ``chat()`` resets it in ``finally``; the staged field is consumed by
# the next ``chat()`` so it cannot silently persist either.
profile_ns = _ACTIVE_PROFILE_NS.get()
if profile_ns is None:
profile_ns = self._staged_profile_namespace
if profile_ns:
return f"profile:{profile_ns}:{base}"
return base
def _persist_key(self, storage_key: str) -> str:
"""Derive the persistent-store key from an in-memory storage key.
@@ -717,9 +849,14 @@ class BotSessionManager:
history = history[-self._max_history:]
# Always update in-memory cache (keyed by storage key)
self._histories[self._storage_key(user_id, **route)] = history
storage_key = self._storage_key(user_id, **route)
self._histories[storage_key] = history
if self._store is not None:
# The in-memory copy now diverges from the store until the write
# below fully succeeds; drop the flush marker so a concurrent
# pressure sweep cannot evict this (now sole) current copy.
self._flushed.discard(storage_key)
key = self._session_key(user_id, **route)
try:
if hasattr(self._store, "set_chat_history"):
@@ -735,6 +872,9 @@ class BotSessionManager:
)
except Exception as e:
logger.warning("Failed to persist session to store: %s", e)
else:
# Store now durably holds this exact history — safe to evict.
self._flushed.add(storage_key)
def set_pending_tool_policy(
self, agent: "Agent", tool_policy: Optional[Any]
@@ -757,6 +897,40 @@ class BotSessionManager:
else:
self._pending_tool_policies[key] = tool_policy
def set_profile_namespace(
self, profile: Optional[str], agent: Optional["Agent"] = None
) -> None:
"""Enter (or clear) a per-tenant profile scope for the next turn.
Used by the gateway's routing handler when a route resolves to a
``profile`` (Issue #4341). The handler runs synchronously right before
the adapter's own ``_session.chat()`` in the same dispatch. The resolved
namespace is *staged* on the instance (keyed by ``id(agent)`` when an
agent is given, so a shared session serving multiple agents never
crosses tenants). ``chat()`` then consumes-and-clears the staged value
and binds it turn-locally in a per-task ContextVar for the duration of
that turn — so overlapping routed turns never share a namespace and a
stale scope never leaks into a later turn. Every ``_storage_key`` for
the turn is prefixed with ``profile:<name>:`` so two routes multiplexed
on one process never share a transcript. Passing ``None``/blank clears
the scope so a route with no profile never inherits another tenant's
namespace (fail-closed: unscoped routes stay unscoped, never borrowed).
"""
cleaned = str(profile).strip() if profile is not None else ""
# Stage on the instance (not a process-wide ContextVar) so a stage never
# leaks into an unrelated session or a later turn: ``chat()`` consumes
# this and binds it turn-locally, resetting on exit. Keep both a
# per-agent slot (so a shared session serving multiple agents never
# crosses tenants) and a last-staged fallback for agentless callers and
# direct ``_storage_key`` reads before ``chat()`` runs.
self._staged_profile_namespace = cleaned or None
if agent is not None:
key = id(agent)
if cleaned:
self._pending_profile_namespaces[key] = cleaned
else:
self._pending_profile_namespaces.pop(key, None)
@staticmethod
def _apply_tool_policy(
agent: "Agent", tool_policy: Optional[Any]
@@ -884,6 +1058,22 @@ class BotSessionManager:
if tool_policy is None:
tool_policy = staged_policy
# Per-tenant profile scope (Issue #4341): bind the resolved namespace
# turn-locally for the whole turn so every ``_storage_key`` recomputed
# after an await point below uses *this* turn's tenant — an overlapping
# route for another tenant cannot rewrite it. Prefer the value staged by
# the routing handler for this agent (popped so it applies exactly once);
# otherwise the last-staged instance fallback (for agentless callers).
# Reset in the finally so it never leaks into an unrelated later turn
# sharing this task (fail-closed: unscoped stays unscoped).
staged_profile = self._pending_profile_namespaces.pop(id(agent), None)
if staged_profile is None:
staged_profile = self._staged_profile_namespace
# Consume the last-staged fallback so it applies to exactly one turn and
# never silently persists into a later unscoped call (fail-closed).
self._staged_profile_namespace = None
profile_token = _ACTIVE_PROFILE_NS.set(staged_profile or None)
# Mint/adopt an end-to-end correlation id and bind it for this turn so
# ingress, the agent run, and outbound delivery share one stable id in
# structured logs. Adopts an explicit correlation_id, else the platform
@@ -930,6 +1120,13 @@ class BotSessionManager:
reset_correlation_id(cid_token)
except Exception as e: # pragma: no cover — defensive
logger.debug("Failed to reset correlation id: %s", e)
# Reset the turn-local profile scope on this early return too so
# a deduped duplicate never leaves a tenant namespace bound for a
# later unrelated call sharing this task (Issue #4341).
try:
_ACTIVE_PROFILE_NS.reset(profile_token)
except Exception as e: # pragma: no cover — defensive
logger.debug("Failed to reset profile namespace: %s", e)
return ""
# Resolve the routing context once so per_chat scope (Issue #2376)
@@ -1062,6 +1259,11 @@ class BotSessionManager:
# finally below regardless of outcome). ``None`` when no gate is set.
_admission_cm = None
_admission_active = False
# Issue #3804: mark this session in-flight for the duration of the turn
# so the memory-pressure sweep never soft-evicts a cache with a live
# turn executing against it. Set inside the user_lock below, cleared in
# the outer finally on every exit path.
_inflight_key: Optional[str] = None
try:
# Claim journal entry if we have one
@@ -1096,10 +1298,19 @@ class BotSessionManager:
# The gate is held for the full turn; released in the outer
# finally below so a slot is never leaked on any exit path.
async with user_lock:
# Issue #3804: this session now has a live turn; record it so
# the pressure sweep skips it (never abort in-flight work).
_inflight_key = self._storage_key(user_id, **route)
self._in_flight[_inflight_key] = self._in_flight.get(_inflight_key, 0) + 1
# Load history (may hit disk via run_in_executor for async safety)
loop = asyncio.get_running_loop()
# Run in the current context so the turn-local profile scope
# (Issue #4341) is visible to ``_storage_key`` on the executor
# thread — ``run_in_executor`` does not copy ContextVars, so
# without this the history key would drop the tenant prefix.
_load_ctx = contextvars.copy_context()
user_history = await loop.run_in_executor(
None, partial(self._load_history, user_id, **route)
None, partial(_load_ctx.run, partial(self._load_history, user_id, **route))
)
# Update last active timestamp AFTER history load and reset check
@@ -1221,7 +1432,6 @@ class BotSessionManager:
emitter.remove_callback(bridged_stream_callback)
else:
# Legacy non-streaming path: use agent.chat() in executor with cancel_token and timeout
import contextvars
import inspect
_ctx = contextvars.copy_context()
@@ -1319,10 +1529,17 @@ class BotSessionManager:
# Persist outside the agent_lock — it's session-scoped and
# the agent is no longer touched. ``route`` keys the shared
# per_chat session when active (empty otherwise).
# per_chat session when active (empty otherwise). Run in the
# current context so the turn-local profile scope (Issue
# #4341) reaches ``_storage_key`` on the executor thread —
# otherwise the save would land in the unscoped key.
_save_ctx = contextvars.copy_context()
await loop.run_in_executor(
None,
partial(self._save_history, user_id, updated_history, **route),
partial(
_save_ctx.run,
partial(self._save_history, user_id, updated_history, **route),
),
)
# Normalise an agent-emitted presentation (if any) into
@@ -1385,6 +1602,15 @@ class BotSessionManager:
await claim_ctx.__aexit__(None, None, None)
return result or ""
finally:
# Issue #3804: clear this turn's in-flight mark so the pressure
# sweep can once again consider this cache for eviction. Guarded by
# ``_inflight_key`` so a turn shed before the lock never underflows.
if _inflight_key is not None:
_n = self._in_flight.get(_inflight_key, 0) - 1
if _n > 0:
self._in_flight[_inflight_key] = _n
else:
self._in_flight.pop(_inflight_key, None)
# Issue #2454: release the admission slot for this turn (frees a
# global concurrency slot and lets a queued waiter proceed). Done
# first so capacity is returned promptly on every exit path.
@@ -1414,6 +1640,13 @@ class BotSessionManager:
reset_correlation_id(cid_token)
except Exception as e: # pragma: no cover — defensive
logger.debug("Failed to reset correlation id: %s", e)
# Reset the turn-local profile scope so this turn's tenant namespace
# never leaks into an unrelated later call sharing this task, and a
# subsequent unscoped route always fails closed (Issue #4341).
try:
_ACTIVE_PROFILE_NS.reset(profile_token)
except Exception as e: # pragma: no cover — defensive
logger.debug("Failed to reset profile namespace: %s", e)
async def chat_with_run_control(
self,
@@ -1601,6 +1834,7 @@ class BotSessionManager:
# a fresh one instead of emitting a false invalidation (Issue #3352).
self._prefix_sig.pop(storage_key, None)
self._last_active.pop(storage_key, None)
self._flushed.discard(storage_key)
self._locks.drop(storage_key)
if self._store is not None:
key = self._persist_key(storage_key)
@@ -1612,6 +1846,116 @@ class BotSessionManager:
logger.debug("BotSessionManager: reaped %d stale sessions", len(stale))
return len(stale)
def warm_sessions(self) -> "List[WarmSession]":
"""Snapshot the warm per-session caches as :class:`WarmSession` facts.
Issue #3804: carries just enough state for the pure core planner
(:func:`praisonaiagents.gateway.plan_pressure_evictions`) to name the
coldest *rebuildable* caches to soft-evict under memory pressure:
* ``last_activity`` — the LRU key (coldest evicted first).
* ``in_flight`` — a turn is executing against the cache (never evict).
* ``flushed`` — the transcript is durably persisted and therefore
rebuildable. Only true when a persistent ``store`` is configured *and*
this session's last write completed (tracked per key in
``_flushed``). Without a store, or after a failed/partial persist,
the in-memory copy is the only current one, so it is treated as
unflushed and never evicted (evicting it would lose data).
"""
from praisonaiagents.gateway import WarmSession # lazy, core contract
has_store = self._store is not None
return [
WarmSession(
session_id=key,
last_activity=self._last_active.get(key, 0.0),
in_flight=self._in_flight.get(key, 0) > 0,
flushed=has_store and key in self._flushed,
)
for key in list(self._histories.keys())
]
def _soft_evict(self, storage_key: str) -> bool:
"""Drop only the *in-memory* history for *storage_key* (no store touch).
The durable copy in the session store is left intact, so the next turn
transparently reloads the transcript — soft eviction is a lossless,
self-healing pressure valve, not a session teardown. Unlike
``reap_stale`` it does NOT fire ``SESSION_END`` or clear the store.
Returns ``True`` if an in-memory cache was actually dropped.
"""
if storage_key not in self._histories:
return False
self._histories.pop(storage_key, None)
# Reset the prompt-prefix baseline so the reloaded session starts a
# fresh baseline rather than firing a false invalidation (Issue #3352).
self._prefix_sig.pop(storage_key, None)
# The in-memory copy is gone; its flush marker no longer applies (the
# next load re-establishes state from the store).
self._flushed.discard(storage_key)
return True
def sweep_under_pressure(
self,
memory_pressure: "MemoryPressureProtocol",
headroom: float = 0.9,
) -> int:
"""Soft-evict the coldest rebuildable caches under memory pressure.
Issue #3804: the runtime enactor for the core planner. Reads the real
container budget/RSS from *memory_pressure* (a
:class:`praisonaiagents.gateway.MemoryPressureProtocol`), asks
:func:`plan_pressure_evictions` which caches to shed, and drops their
in-memory history LRU-first — re-sampling RSS as it goes so it stops as
soon as RSS is back within ``headroom`` of the budget (avoiding
over-shedding). In-flight and unflushed caches are never touched.
A no-op (returns ``0``) when the platform cannot report a budget, so a
host without a cgroup limit keeps today's behaviour exactly. Call it on
the gateway's existing periodic tick, alongside ``reap_stale``.
"""
from praisonaiagents.gateway import plan_pressure_evictions # lazy
try:
budget = memory_pressure.cgroup_limit_mb()
rss = memory_pressure.anon_rss_mb()
except Exception as e: # pragma: no cover — defensive
logger.debug("Memory-pressure probe failed; skipping sweep: %s", e)
return 0
plan = plan_pressure_evictions(
budget, rss, self.warm_sessions(), headroom_ratio=headroom
)
if not plan:
return 0
try:
target = float(budget) * float(headroom) # type: ignore[arg-type]
except (TypeError, ValueError):
target = 0.0
evicted = 0
for storage_key in plan:
# Re-check in-flight at enact time: a turn may have started against
# this cache after the plan was computed. Never abort live work.
if self._in_flight.get(storage_key, 0) > 0:
continue
if self._soft_evict(storage_key):
evicted += 1
# Re-sample and stop once back within budget so we shed the
# minimum number of caches (planner returns all, coldest-first).
try:
if memory_pressure.anon_rss_mb() <= target:
break
except Exception: # pragma: no cover — defensive
break
if evicted:
logger.info(
"BotSessionManager: soft-evicted %d cold session cache(s) "
"under memory pressure", evicted
)
return evicted
def complete_last_journal_entry(self) -> bool:
"""Complete the last journal entry if one exists.
@@ -1766,6 +2110,12 @@ class BotSessionManager:
self._clear_session_data(storage_key, self._persist_key(storage_key))
self._last_active.pop(storage_key, None)
self._last_reset[storage_key] = time.monotonic()
# Clear any per-user model override (set via /model) so a "start fresh"
# actually remedies a bad/unavailable model instead of every later turn
# silently failing at the provider (Issue #4318). Overrides are keyed by
# the per-user storage key (no route), exactly as /model and chat() key
# them, so a group /new still clears the issuing user's override.
self._model_overrides.pop(self._storage_key(user_id), None)
# Fire SESSION_END lifecycle hook (no-op when no hooks registered),
# then forget the session so a subsequent message re-opens it.
@@ -1916,6 +2266,9 @@ class BotSessionManager:
# Clear prompt-prefix baselines alongside history so reopened sessions
# each establish a fresh baseline (Issue #3352).
self._prefix_sig.clear()
# Clear per-user model overrides too so a global reset actually starts
# every user fresh on the agent's configured model (Issue #4318).
self._model_overrides.clear()
return count
@property
@@ -2026,7 +2379,10 @@ def build_session_manager(config, platform: str, *, run_control=None) -> BotSess
Returns:
Configured BotSessionManager instance
"""
# Try to get the default session store
# Try to get the default session store. Sessions are durable by default;
# a genuine store-init failure is recorded as a durability-degraded fact
# (Issue #4339) — surfaced by ``gateway doctor`` / ``gateway status`` — not
# just logged, so an operator is told their bot is now running non-durably.
try:
from praisonaiagents.session import get_default_session_store
except ImportError:
@@ -2036,11 +2392,22 @@ def build_session_manager(config, platform: str, *, run_control=None) -> BotSess
try:
store = get_default_session_store()
except Exception as exc:
# Raw exception (which may embed a filesystem path) stays in the log
# only; the operator-facing reason is redacted so health() and
# ``gateway status`` never echo backend paths (#4339).
logger.warning(
"Default session store unavailable; falling back to in-memory store: %s",
exc,
)
record_durability_degraded(
"session",
reason="session store unavailable (running in-memory)",
)
store = None
else:
# Store came up (or was restored): clear any prior degraded fact so
# a recovered gateway no longer reports non-durable sessions.
clear_durability_degraded("session")
# Extract reset policy from config
reset_policy = None
@@ -126,6 +126,7 @@ class DiscordBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
self._ack: AckReactor = AckReactor(
ack_emoji=self.config.ack_emoji,
done_emoji=self.config.done_emoji,
scope=getattr(self.config, "ack_scope", "group-mentions"),
)
# Pairing system
@@ -386,7 +387,11 @@ class DiscordBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
handler(bot_message)
except Exception as e:
logger.error(f"Command handler error: {e}")
return
return
# Unknown/unimplemented slash command: fall through to normal
# agent dispatch below rather than a silent no-op, matching the
# Slack reference adapter so a misspelled or not-yet-implemented
# command reaches the agent instead of vanishing (Issue #4318).
should_respond = False
if message.guild is None:
@@ -408,8 +413,15 @@ class DiscordBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
user_id = str(message.author.id)
# Ack reaction - show processing indicator
# (scope-gated: quiet on ambient guild chatter)
_is_mention = (
message.guild is None
or self._client.user.mentioned_in(message)
)
ack_ctx = None
if self._ack.enabled:
if self._ack.enabled and self._ack.should_ack_message(
bot_message, is_mention=_is_mention
):
async def _discord_react(emoji, **kw):
try:
await message.add_reaction(emoji)
@@ -553,7 +565,7 @@ class DiscordBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
async def _send_long_message(self, channel, text: str, reference=None) -> None:
"""Send a long message, splitting with markdown-aware chunking."""
from ._chunk import chunk_message
from ._chunk import chunk_message, enforce_hard_cap
max_len = min(self.config.max_message_length, 2000)
@@ -564,6 +576,10 @@ class DiscordBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
await channel.send(text)
else:
chunks = chunk_message(text, max_length=max_len, preserve_fences=True)
# Guarantee no chunk exceeds the 2000-char cap; an over-cap code
# fence would otherwise raise a 400 and drop the entire reply
# (Issue #4319).
chunks = enforce_hard_cap(chunks, max_len)
for i, chunk in enumerate(chunks):
if i == 0 and reference:
await channel.send(chunk, reference=reference)
@@ -108,6 +108,7 @@ class LinearBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
self._ack: AckReactor = AckReactor(
ack_emoji=self.config.ack_emoji,
done_emoji=self.config.done_emoji,
scope=getattr(self.config, "ack_scope", "group-mentions"),
)
# ChatCommandMixin setup
@@ -60,9 +60,14 @@ def split_media_from_output(text: str) -> Dict[str, Any]:
# Remove MEDIA: lines from text
clean = MEDIA_REGEX.sub("", clean)
# Clean up whitespace
clean = "\n".join(line for line in clean.split("\n") if line.strip())
# Clean up whitespace.
# NOTE: only *excess* blank runs left behind by stripped MEDIA lines are
# collapsed. Legitimate blank lines are load-bearing on the outbound path —
# they are the paragraph separators for chunking and the only visual break in
# plain-text replies — so a single blank line between blocks is preserved
# (Issue #4319).
clean = re.sub(r"\n[ \t]*\n(?:[ \t]*\n)+", "\n\n", clean)
clean = clean.strip()
return {
@@ -146,6 +146,7 @@ class SignalBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
self._ack: AckReactor = AckReactor(
ack_emoji=self.config.ack_emoji,
done_emoji=self.config.done_emoji,
scope=getattr(self.config, "ack_scope", "group-mentions"),
)
self._command_handlers: Dict[str, Callable] = {}
@@ -306,7 +307,14 @@ class SignalBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
if not isinstance(envelopes, list):
return
self._note_inbound()
# Only refresh inbound liveness when the bridge actually delivered an
# envelope. A successfully-empty poll must NOT count as activity: a
# silently half-open transport returns ``[]`` forever without raising,
# and noting that as inbound traffic would keep ``last_activity`` fresh
# and defeat the health monitor's STALE_SOCKET detection — leaving a
# "deaf" channel reported HEALTHY with no operator signal.
if envelopes:
self._note_inbound()
for item in envelopes:
try:
await self._handle_envelope(item)
+26 -2
View File
@@ -170,6 +170,7 @@ class SlackBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
self._ack: AckReactor = AckReactor(
ack_emoji=self.config.ack_emoji,
done_emoji=self.config.done_emoji,
scope=getattr(self.config, "ack_scope", "group-mentions"),
)
# Pairing system
@@ -562,8 +563,27 @@ class SlackBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
msg_ts = event.get("ts", "")
# Ack reaction - show processing indicator
# (scope-gated: quiet on ambient channel chatter). A reply
# inside a thread (thread_ts present) counts as engaging the
# bot even without a fresh @mention, so a follow-up in an
# ongoing thread is acked under group-mentions instead of being
# misclassified as ambient (Greptile #4293).
_in_thread = bool(event.get("thread_ts"))
_is_mention = channel_type == "im" or _in_thread or (
bool(self._bot_user)
and f"<@{self._bot_user.user_id}>"
in (event.get("text", "") or "")
)
ack_ctx = None
if self._ack.enabled and self._client:
if (
self._ack.enabled
and self._client
and self._ack.should_ack(
is_direct=channel_type == "im",
is_mention=_is_mention,
is_ambient=channel_type != "im" and not _is_mention,
)
):
async def _slack_react(emoji, **kw):
try:
# Slack uses emoji names without colons
@@ -944,7 +964,7 @@ class SlackBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
async def _send_long_message(self, say, text: str, thread_ts: Optional[str] = None) -> None:
"""Send a long message, splitting with markdown-aware chunking."""
from ._chunk import chunk_message
from ._chunk import chunk_message, enforce_hard_cap
max_len = min(self.config.max_message_length, 4000)
@@ -956,6 +976,10 @@ class SlackBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
await say(text=text.strip(), thread_ts=thread_ts)
else:
chunks = chunk_message(text, max_length=max_len, preserve_fences=True)
# Guarantee no chunk exceeds the platform cap; an over-cap code
# fence would otherwise raise "msg_too_long" and drop the entire
# reply (Issue #4319).
chunks = enforce_hard_cap(chunks, max_len)
for chunk in chunks:
if chunk and chunk.strip():
await say(text=chunk.strip(), thread_ts=thread_ts)
@@ -181,6 +181,7 @@ class TelegramBot(ChatCommandMixin, MessageHookMixin):
self._ack: AckReactor = AckReactor(
ack_emoji=self.config.ack_emoji,
done_emoji=self.config.done_emoji,
scope=getattr(self.config, "ack_scope", "group-mentions"),
)
# Pairing system
@@ -647,9 +648,9 @@ class TelegramBot(ChatCommandMixin, MessageHookMixin):
logger.error(f"Message handler error: {e}")
if self._agent and not message.is_command:
# Ack reaction
# Ack reaction (scope-gated: quiet on ambient group chatter)
ack_ctx = None
if self._ack.enabled:
if self._ack.enabled and self._ack.should_ack_message(message):
async def _tg_react(emoji, **kw):
try:
from telegram import ReactionTypeEmoji
@@ -934,12 +935,16 @@ class TelegramBot(ChatCommandMixin, MessageHookMixin):
# File-based custom slash commands (Issue #3729): resolve
# ``.praisonai/commands/{command}.md`` and submit the rendered
# body as a normal chat turn. Falls through to chat on a miss.
# body as a normal chat turn. On a miss (no such custom command)
# the raw command text is dispatched to the agent so an unknown
# or misspelled slash command is never a silent no-op — matching
# the Slack reference adapter's fall-through (Issue #4318).
text = update.message.text or ""
parts = text.split(maxsplit=1)
arguments = parts[1] if len(parts) > 1 else ""
rendered = self.render_custom_command(command, arguments)
if rendered is not None:
prompt = rendered if rendered is not None else text
if prompt is not None:
user_name = (
update.message.from_user.username
or update.message.from_user.first_name
@@ -947,7 +952,7 @@ class TelegramBot(ChatCommandMixin, MessageHookMixin):
) if update.message.from_user else ""
try:
response = await self._session.chat(
self._agent, user_id, rendered,
self._agent, user_id, prompt,
chat_id=str(update.message.chat_id) if update.message.chat_id else "",
user_name=user_name,
message_id=str(update.message.message_id),
@@ -1567,11 +1572,17 @@ class TelegramBot(ChatCommandMixin, MessageHookMixin):
reply_to: Optional[int] = None,
) -> None:
"""Send a long message, splitting with markdown-aware chunking."""
from ._chunk import chunk_message
from ._chunk import chunk_message, enforce_hard_cap, _calculate_length
max_len = self.config.max_message_length
# Telegram measures message length in UTF-16 code units, not Python
# codepoints — a single emoji is 2 UTF-16 units. Measuring in
# codepoints lets an emoji-heavy reply exceed 4096 UTF-16 units and be
# rejected as "message too long", so use the platform's declared unit
# (Issue #4319).
length_unit = self.platform_capabilities().length_unit
if len(text) <= max_len:
if _calculate_length(text, length_unit) <= max_len:
kwargs = {"chat_id": chat_id, "text": text}
if reply_to:
kwargs["reply_to_message_id"] = reply_to
@@ -1591,7 +1602,16 @@ class TelegramBot(ChatCommandMixin, MessageHookMixin):
}
)
else:
chunks = chunk_message(text, max_length=max_len, preserve_fences=True)
chunks = chunk_message(
text,
max_length=max_len,
preserve_fences=True,
length_unit=length_unit,
)
# Guarantee no chunk exceeds the platform cap; an over-cap code
# fence would otherwise raise "Message is too long" and drop the
# entire reply (Issue #4319).
chunks = enforce_hard_cap(chunks, max_len, length_unit=length_unit)
for i, chunk in enumerate(chunks):
kwargs = {"chat_id": chat_id, "text": chunk}
if i == 0 and reply_to:
@@ -227,6 +227,7 @@ class WhatsAppBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
self._ack: AckReactor = AckReactor(
ack_emoji=self.config.ack_emoji,
done_emoji=self.config.done_emoji,
scope=getattr(self.config, "ack_scope", "group-mentions"),
)
# Web mode adapter (lazy initialized)
@@ -902,8 +903,9 @@ class WhatsAppBot(OutboundResilienceMixin, ChatCommandMixin, MessageHookMixin):
# Route to agent
if self._agent and (content or attachments):
# Ack reaction - show processing indicator
# (scope-gated: quiet on ambient group chatter)
ack_ctx = None
if self._ack.enabled:
if self._ack.enabled and self._ack.should_ack_message(bot_message):
async def _wa_react(emoji, **kw):
try:
await self.send_reaction(sender_id, msg_id, emoji)
@@ -842,6 +842,24 @@ class GatewayHandler:
f"{_time.strftime('%H:%M:%S', _time.localtime(data['last_inbound_at']))} "
f"({age:.0f}s ago)"
)
# Issue #4339: surface degraded owners (channels, providers, and
# now durability — a session/idempotency store that fell back to
# in-memory) so an operator is told when the bot is running
# non-durably instead of it being a silent log line. Always
# printed (not gated on ``deep``) since a degraded state is
# exactly what an at-a-glance status check should reveal.
degraded_owners = data.get("degraded_owners") or []
if degraded_owners:
print(" Degraded:")
for owner in degraded_owners:
kind = owner.get("owner_kind", "?")
oid = owner.get("owner_id", "?")
reason = owner.get("reason", "—")
hint = owner.get("retry_hint", "")
line = f" ✗ {kind}:{oid} — {reason}"
if hint:
line += f" fix: {hint}"
print(line)
channels = data.get("channels") or {}
if channels and deep:
print(" Channels:")
@@ -429,6 +429,7 @@ def _generate_bot_yaml(platforms: List[str], agent_name: str = "assistant", agen
lines.append(" # Set to false only if you add destructive tools AND have a chat-level approval flow.")
lines.append(' ack_emoji: "⏳" # Show processing indicator during long agent operations')
lines.append(' done_emoji: "✅" # Replace ack_emoji when operation completes')
lines.append(' ack_scope: "group-mentions" # Who gets acked: off | direct | group-mentions | group-all | all')
if plat == "whatsapp":
lines.append(" phone_number_id: ${WHATSAPP_PHONE_NUMBER_ID}")
# Gateway-style routing (ignored by `bot start` which uses top-level routing).
@@ -15,6 +15,7 @@ if TYPE_CHECKING:
from .exec_approval import ExecApprovalManager, get_exec_approval_manager
from .gateway_approval import GatewayApprovalBackend
from .home_channels import HomeChannelRegistry, DeliveryResolver
from .memory_pressure import CgroupMemoryPressure
def __getattr__(name: str):
"""Lazy loading of gateway components."""
@@ -58,6 +59,9 @@ def __getattr__(name: str):
if name == "DeliveryResolver":
from .home_channels import DeliveryResolver
return DeliveryResolver
if name == "CgroupMemoryPressure":
from .memory_pressure import CgroupMemoryPressure
return CgroupMemoryPressure
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
__all__ = [
@@ -74,4 +78,5 @@ __all__ = [
"GatewayApprovalBackend",
"HomeChannelRegistry",
"DeliveryResolver",
"CgroupMemoryPressure",
]
@@ -43,7 +43,7 @@ import threading
from contextlib import closing
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, TYPE_CHECKING, Union
from typing import Any, Dict, Iterable, List, Optional, Set, TYPE_CHECKING, Union
if TYPE_CHECKING: # pragma: no cover - typing only
from praisonaiagents.approval import ApprovalStoreProtocol
@@ -102,6 +102,10 @@ class PendingRequest:
agent_name: str = ""
risk_level: str = "medium"
created_at: float = field(default_factory=time.time)
# Optional per-request custody: reviewer identities authorised to resolve
# this request. ``None`` (default) keeps today's behaviour where any holder
# of the APPROVALS scope may resolve any request.
authorized_reviewers: Optional[frozenset] = None
# Resolved via the asyncio Future
_future: Optional[asyncio.Future] = field(default=None, repr=False)
# Store the event loop at registration time for thread-safe resolution
@@ -121,6 +125,11 @@ class Resolution:
scope_to_agent: bool = True
# If True, also key the grant by the request's argument signature.
scope_to_args: bool = False
# Identity of the principal resolving this request (operator/device/actor
# id). Recorded on the durable audit trail and the allow-always grant so a
# decision is attributable to a person. ``None`` (default) keeps today's
# anonymous ``"gateway"`` attribution.
resolver: Optional[str] = None
# ── Durable, scoped allow-list store ─────────────────────────────────
@@ -638,6 +647,7 @@ class ExecApprovalManager:
logger.debug(
"Could not read stored expiry for %s", approval_id
)
reviewers = getattr(req, "authorized_reviewers", None)
self._pending[approval_id] = PendingRequest(
request_id=approval_id,
tool_name=req.tool_name,
@@ -645,6 +655,7 @@ class ExecApprovalManager:
agent_name=req.agent_name or "",
risk_level=req.risk_level,
created_at=created_at,
authorized_reviewers=frozenset(reviewers) if reviewers else None,
)
count += 1
if count:
@@ -659,12 +670,17 @@ class ExecApprovalManager:
arguments: Dict[str, Any],
agent_name: str = "",
risk_level: str = "medium",
authorized_reviewers: Optional[Iterable[str]] = None,
) -> tuple:
"""Create a pending request and return ``(request_id, future)``.
If a matching allow-always grant exists (scoped to this agent, or a
legacy global grant), the future is resolved immediately with
``approved=True``.
``authorized_reviewers`` optionally binds the request to a set of
reviewer identities; only those principals may resolve it. ``None``
(default) leaves resolution open to any holder of the APPROVALS scope.
"""
# Fast path: already permanently allowed (scoped to this agent, its
# argument signature, or a legacy global grant).
@@ -682,12 +698,14 @@ class ExecApprovalManager:
loop = asyncio.get_running_loop()
future = loop.create_future()
reviewers = frozenset(authorized_reviewers) if authorized_reviewers else None
req = PendingRequest(
request_id=request_id,
tool_name=tool_name,
arguments=arguments,
agent_name=agent_name,
risk_level=risk_level,
authorized_reviewers=reviewers,
_future=future,
_loop=loop, # capture loop at registration for thread-safe resolve
)
@@ -709,6 +727,9 @@ class ExecApprovalManager:
risk_level=risk_level,
agent_name=agent_name or None,
approval_id=request_id,
authorized_reviewers=(
sorted(reviewers) if reviewers else None
),
),
expires_at=time.time() + self._ttl,
)
@@ -737,6 +758,22 @@ class ExecApprovalManager:
if req is None:
return False
# Per-request custody: if the request is bound to a set of authorised
# reviewers, only one of them may resolve it. Re-insert the request on
# rejection so an authorised reviewer can still resolve it later (the
# atomic pop above preserves first-answer-wins between authorised
# reviewers).
if req.authorized_reviewers and resolution.resolver not in req.authorized_reviewers:
with self._lock:
self._pending.setdefault(request_id, req)
logger.warning(
"Rejected approval resolution for %s: resolver %r not authorised",
request_id, resolution.resolver,
)
return False
approver = resolution.resolver or "gateway"
if resolution.allow_always:
# Scope the grant safely-by-default: to the requesting agent unless
# the resolver explicitly widened it, and optionally to its
@@ -761,7 +798,7 @@ class ExecApprovalManager:
agent_id=agent_id,
tool_name=req.tool_name,
arg_signature=arg_sig,
approver="gateway:human",
approver=approver,
)
logger.info(
"Tool '%s' added to allow-always list (agent=%s, args=%s)",
@@ -775,7 +812,7 @@ class ExecApprovalManager:
)
# Record the decision to the durable audit trail (best-effort).
self._record_resolution(request_id, resolution)
self._record_resolution(request_id, resolution, approver=approver)
logger.info(
"Approval resolved: %s -> %s",
@@ -784,12 +821,22 @@ class ExecApprovalManager:
)
return True
def _record_resolution(self, request_id: str, resolution: Resolution) -> None:
def _record_resolution(
self,
request_id: str,
resolution: Resolution,
*,
approver: str = "gateway",
) -> None:
"""Persist a decision to the durable store (sync, best-effort).
Called from :meth:`resolve`, which may run on any thread. The store's
``resolve`` is async, so we schedule it on a running loop when one is
available and otherwise run it synchronously.
``approver`` is the resolving principal's identity (defaulting to the
anonymous ``"gateway"`` when no resolver was supplied) so the audit
trail can attribute the decision to a person.
"""
if self._store is None:
return
@@ -799,7 +846,7 @@ class ExecApprovalManager:
decision = ApprovalDecision(
approved=resolution.approved,
reason=resolution.reason,
approver="gateway",
approver=approver,
)
coro = self._store.resolve(request_id, decision)
except Exception:
@@ -89,6 +89,7 @@ class GatewayApprovalBackend:
arguments=request.arguments,
agent_name=request.agent_name or "",
risk_level=request.risk_level,
authorized_reviewers=request.authorized_reviewers,
)
# Fire optional notification (fire-and-forget)
@@ -0,0 +1,154 @@
"""Concrete cgroup-aware memory-pressure reader for the gateway (Issue #3804).
The core SDK ships the *decision* — :func:`praisonaiagents.gateway.plan_pressure_evictions`
plus the :class:`praisonaiagents.gateway.MemoryPressureProtocol` contract — but the
*mechanism* (reading the container's real memory ceiling and the process's own
anonymous RSS) belongs in the running gateway. This module is that mechanism: a
tiny, stdlib-only implementation of ``MemoryPressureProtocol`` so the eviction
budget tracks the actual cgroup limit the process runs under (``memory.high`` /
``memory.max`` on cgroup v2, ``memory.limit_in_bytes`` on v1) rather than total
host RAM or a hard-coded number.
Every probe degrades to ``None``/``0.0`` when the platform cannot report it, so
``BotSessionManager.sweep_under_pressure`` becomes a safe no-op on hosts without
a cgroup limit — preserving today's behaviour exactly.
"""
from __future__ import annotations
import logging
import os
from typing import Optional
logger = logging.getLogger(__name__)
_MIB = 1024.0 * 1024.0
# cgroup v2 reports "no limit" as this literal; v1 uses a very large sentinel.
_V2_MAX = "max"
# A cgroup v1 limit at/above this many bytes means "unlimited" in practice
# (kernels expose PAGE_COUNTER_MAX * PAGE_SIZE, ~8 EiB); treat as no limit.
_V1_UNLIMITED_BYTES = 1 << 62
class CgroupMemoryPressure:
"""Read the container memory budget and anonymous RSS via cgroup + procfs.
Implements :class:`praisonaiagents.gateway.MemoryPressureProtocol`. All I/O
is best-effort and swallowed: a probe that cannot read its source returns
``None`` (budget) or ``0.0`` (RSS) so the pressure sweep never crashes the
gateway it is meant to protect.
"""
def cgroup_limit_mb(self) -> Optional[float]:
"""Return the container memory limit in MiB, or ``None`` if unknown.
Prefers cgroup v2 ``memory.high`` (the soft throttle ceiling the kernel
reclaims against) then ``memory.max`` (the hard OOM ceiling); falls back
to cgroup v1 ``memory.limit_in_bytes``. Returns ``None`` when no finite
limit is configured (unconstrained host).
The process may live in a *nested* cgroup whose limit is lower than the
filesystem root's, so each controller file is probed at the process's
own cgroup path first (resolved from ``/proc/self/cgroup``) and only
then at the root — the root remaining the fallback preserves behaviour
on hosts where the nested path is unreadable.
"""
v2_rel, v1_rel = self._self_cgroup_paths()
# cgroup v2: nested dir first, then filesystem root.
v2_dirs = []
if v2_rel:
v2_dirs.append("/sys/fs/cgroup/" + v2_rel.strip("/"))
v2_dirs.append("/sys/fs/cgroup")
for base in v2_dirs:
for leaf in ("memory.high", "memory.max"):
mb = self._read_v2_limit(base.rstrip("/") + "/" + leaf)
if mb is not None:
return mb
# cgroup v1 fallback: nested memory-controller dir first, then root.
v1_paths = []
if v1_rel:
v1_paths.append(
"/sys/fs/cgroup/memory/" + v1_rel.strip("/") + "/memory.limit_in_bytes"
)
v1_paths.append("/sys/fs/cgroup/memory/memory.limit_in_bytes")
for path in v1_paths:
mb = self._read_v1_limit(path)
if mb is not None:
return mb
return None
@staticmethod
def _self_cgroup_paths() -> "tuple[Optional[str], Optional[str]]":
"""Resolve this process's (v2, v1-memory) cgroup-relative paths.
Parses ``/proc/self/cgroup``. A v2 line is ``0::/<path>``; the v1 memory
controller is the entry whose second field contains ``memory``. Returns
``(None, None)`` when unreadable so the caller falls back to the root.
"""
v2: Optional[str] = None
v1: Optional[str] = None
try:
with open("/proc/self/cgroup", "r") as fh:
for line in fh:
parts = line.strip().split(":", 2)
if len(parts) != 3:
continue
_hid, controllers, path = parts
if controllers == "" and v2 is None:
v2 = path or "/"
elif "memory" in controllers.split(",") and v1 is None:
v1 = path or "/"
except OSError:
return None, None
return v2, v1
def anon_rss_mb(self) -> float:
"""Return current anonymous (non-reclaimable) RSS in MiB.
Reads ``RssAnon`` from ``/proc/self/status`` (Linux). Anonymous RSS is
the memory that *cannot* be reclaimed by dropping caches — the pages
that actually push a container toward its cgroup ceiling. Returns
``0.0`` when unavailable (non-Linux / unreadable), which makes the
planner a no-op.
"""
try:
with open("/proc/self/status", "r") as fh:
for line in fh:
if line.startswith("RssAnon:"):
# Format: "RssAnon:\t 12345 kB"
parts = line.split()
if len(parts) >= 2:
return float(parts[1]) / 1024.0
except (OSError, ValueError):
pass
return 0.0
@staticmethod
def _read_v2_limit(path: str) -> Optional[float]:
try:
with open(path, "r") as fh:
raw = fh.read().strip()
except OSError:
return None
if not raw or raw == _V2_MAX:
return None
try:
return float(int(raw)) / _MIB
except ValueError:
return None
@staticmethod
def _read_v1_limit(path: str) -> Optional[float]:
try:
with open(path, "r") as fh:
raw = fh.read().strip()
except OSError:
return None
try:
value = int(raw)
except ValueError:
return None
if value <= 0 or value >= _V1_UNLIMITED_BYTES:
return None
return float(value) / _MIB
@@ -1379,6 +1379,35 @@ class WebSocketGateway:
status_code=403,
)
def _resolve_operator_identity(request) -> str:
"""Derive a stable, non-secret operator identity for the audit trail.
Resolution order (most to least specific), so multi-operator
deployments can attribute decisions to an *individual* rather than
to a shared credential:
1. An explicit ``X-Operator-Id`` header (a non-secret, caller-supplied
operator handle). This distinguishes operators even when they
share a gateway token — a proxy/SSO layer can stamp it per user.
2. A short digest of the operator token. The token is a secret and
must never be logged, so we hash it; distinct tokens yield
distinct identities. Holders of the *same* token collapse to one
identity by design (that is the identity that token represents).
3. The client IP, then ``"gateway"`` when nothing else is available
(e.g. loopback bypass).
"""
explicit = request.headers.get("x-operator-id", "").strip()
if explicit:
# Bound + sanitised so a header value can't bloat or corrupt the
# audit row; it is a handle, not a secret.
return f"operator:{explicit[:64]}"
token = _extract_request_token(request)
if token:
digest = hashlib.sha256(token.encode("utf-8")).hexdigest()[:12]
return f"operator:{digest}"
client_ip = request.client.host if request.client else None
return f"operator:{client_ip}" if client_ip else "gateway"
async def info(request):
auth_err = _check_auth(request)
if auth_err:
@@ -1708,12 +1737,14 @@ class WebSocketGateway:
allow_always=bool(body.get("allow_always", False)),
scope_to_agent=bool(body.get("scope_to_agent", True)),
scope_to_args=bool(body.get("scope_to_args", False)),
resolver=_resolve_operator_identity(request),
)
found = _approval_mgr.resolve(request_id, resolution)
if not found:
return JSONResponse(
{"error": "Request not found or already resolved"},
{"error": "Request not found, already resolved, or "
"resolver not authorised"},
status_code=404,
)
@@ -4237,8 +4268,12 @@ class WebSocketGateway:
return self._hook_idem
# Backend is captured from the parsed ``hooks.idempotency`` config in
# ``_apply_hooks_from_config`` (``GatewayConfig`` carries no field for
# it). Default to the in-memory store when no config selected one.
backend = self._hook_idempotency_backend or "memory"
# it). Durable by default (Issue #4339): when no config selected a
# backend, ``None`` lets ``build_idempotency_store`` pick the durable
# SQLite store so an out-of-box gateway survives a restart rather than
# silently re-processing a redelivered webhook. ``"memory"`` stays an
# explicit opt-in for ephemeral runs.
backend = self._hook_idempotency_backend
try:
from pathlib import Path
from praisonai_bot.bots import build_idempotency_store
@@ -5187,6 +5222,21 @@ class WebSocketGateway:
if shared is not None:
for owner in shared.list_degraded():
registry.mark(owner)
# Issue #4339: surface durability degradation (a durable session or
# idempotency store that could not initialise and fell back to
# in-memory) the same way as channel degradation, so an operator is
# told their bot is running non-durably instead of it being a silent
# log line. Recorded by the boundaries that own that state in
# ``bots/_session.py`` / ``bots/_idempotency.py``.
try:
from praisonai_bot.bots._session import (
durability_degraded_owners,
)
for owner in durability_degraded_owners():
registry.mark(owner)
except Exception:
pass
degraded_owners = registry.to_list()
if degraded_owners:
result["degraded_owners"] = degraded_owners
@@ -6590,6 +6640,39 @@ class WebSocketGateway:
)
return None
def _resolve_profile_for_message(
self,
channel_name: str,
facts: Optional[Any] = None,
) -> Optional[str]:
"""Resolve the per-route tenant profile for an inbound message (#4341).
Returns the ``profile`` name declared by the matching route binding, or
``None`` when no binding matched or the matched route names no profile.
The gateway stages this on the session so memory/session state is keyed
per tenant for the turn — without it, ``RouteMatch.profile`` resolves but
has no runtime effect and every route silently shares one memory store.
A ``None`` return fails closed: an unscoped route never borrows another
tenant's namespace.
"""
if facts is None:
return None
bindings = self._routing_bindings.get(channel_name) or []
if not bindings:
return None
try:
from praisonaiagents.gateway import resolve_route
match = resolve_route(bindings, facts)
if getattr(match, "binding", None) is None:
return None
return getattr(match, "profile", None)
except Exception as exc: # pragma: no cover - defensive
logger.warning(
f"Profile resolution failed for {channel_name}: {exc}"
)
return None
@staticmethod
def _build_route_facts(
chat_type: str,
@@ -7607,6 +7690,19 @@ class WebSocketGateway:
channel_name, facts=facts
)
session.set_pending_tool_policy(agent, tool_policy)
# Per-tenant profile scope (Issue #4341): enter the resolved
# route's profile namespace so this turn's memory/session state
# is keyed per tenant. Staged right before the adapter's own
# ``_session.chat()`` in the same synchronous dispatch. ``None``
# clears any prior scope so an unscoped route never inherits
# another tenant's namespace (fail-closed).
if session is not None and hasattr(session, "set_profile_namespace"):
session.set_profile_namespace(
gateway._resolve_profile_for_message(
channel_name, facts=facts
),
agent,
)
logger.info(f"Injected routing handler for channel '{channel_name}'")
@@ -7693,10 +7789,25 @@ class WebSocketGateway:
tool_policy = gateway._resolve_tool_policy_for_message(
channel_name, facts=facts
)
# Per-tenant profile scope (Issue #4341): enter the resolved route's
# profile namespace on the session so this turn's memory/session
# state is keyed per tenant. ``None`` clears any prior scope so an
# unscoped route never inherits another tenant's namespace.
_tg_session = getattr(bot, "_session", None)
if _tg_session is not None and hasattr(
_tg_session, "set_profile_namespace"
):
_tg_session.set_profile_namespace(
gateway._resolve_profile_for_message(channel_name, facts=facts),
agent,
)
# Ack reaction — show processing indicator
# Ack reaction — show processing indicator.
# Scope-gated like the standalone Telegram adapter so ambient group
# chatter the bot won't answer is not acked (Greptile #4293): a
# private chat is direct; a reply to the bot counts as a mention.
ack_ctx = None
if bot._ack.enabled:
if bot._ack.enabled and bot._ack.should_ack_message(message):
async def _tg_react(emoji, **kw):
try:
from telegram import ReactionTypeEmoji
@@ -152,12 +152,33 @@ class TestGatewayConfigWiring:
)
assert isinstance(srv._get_hook_idem_store(), SqliteIdempotencyStore)
def test_default_is_memory(self):
from praisonaiagents.gateway import InMemoryIdempotencyStore
def test_default_is_durable(self, tmp_path, monkeypatch):
# Issue #4339: an out-of-box gateway (no ``store_backend`` configured)
# must be durable by default so a redelivered webhook after a restart is
# suppressed rather than re-processed. ``memory`` is now an explicit
# opt-in only.
from praisonai_bot.bots import SqliteIdempotencyStore
monkeypatch.setattr(
"pathlib.Path.home", lambda: tmp_path, raising=True
)
srv = self._bare_server()
srv._apply_hooks_from_config({"hooks": []})
assert srv._hook_idempotency_backend is None
assert isinstance(srv._get_hook_idem_store(), SqliteIdempotencyStore)
def test_explicit_memory_opt_in(self, tmp_path, monkeypatch):
# ``memory`` stays available as an explicit choice for ephemeral runs.
from praisonaiagents.gateway import InMemoryIdempotencyStore
monkeypatch.setattr(
"pathlib.Path.home", lambda: tmp_path, raising=True
)
srv = self._bare_server()
srv._apply_hooks_from_config(
{"hooks": {"idempotency": {"store_backend": "memory"}, "hooks": []}}
)
assert srv._hook_idempotency_backend == "memory"
assert isinstance(srv._get_hook_idem_store(), InMemoryIdempotencyStore)
def test_backend_change_rebuilds_store(self, tmp_path, monkeypatch):
@@ -168,7 +189,10 @@ class TestGatewayConfigWiring:
"pathlib.Path.home", lambda: tmp_path, raising=True
)
srv = self._bare_server()
srv._apply_hooks_from_config({"hooks": []})
# Explicit ``memory`` opt-in, then hot-reload to durable sqlite.
srv._apply_hooks_from_config(
{"hooks": {"idempotency": {"store_backend": "memory"}, "hooks": []}}
)
assert isinstance(srv._get_hook_idem_store(), InMemoryIdempotencyStore)
# Hot-reload flips to sqlite: the cached in-memory store is discarded.
srv._apply_hooks_from_config(
@@ -195,3 +219,85 @@ class TestBuildIdempotencyStore:
from praisonai_bot.bots import build_idempotency_store
assert isinstance(build_idempotency_store("redis"), InMemoryIdempotencyStore)
def test_none_backend_is_durable_by_default(self, tmp_path):
# Issue #4339: unset backend -> durable SQLite store, not in-memory.
from praisonai_bot.bots import SqliteIdempotencyStore, build_idempotency_store
store = build_idempotency_store(None, path=tmp_path / "idem.sqlite")
assert isinstance(store, SqliteIdempotencyStore)
class TestDurabilityDegradation:
"""Issue #4339: a durable store that cannot init is a *reported* fact."""
def test_idempotency_fallback_records_degraded(self, tmp_path):
from praisonai_bot.bots import build_idempotency_store
from praisonai_bot.bots._session import (
clear_durability_degraded,
durability_degraded_owners,
)
from praisonaiagents.gateway import InMemoryIdempotencyStore
clear_durability_degraded("idempotency")
# A path under a file (not a dir) makes the SQLite store fail to open,
# exercising the recorded-degradation fallback.
bad_parent = tmp_path / "afile"
bad_parent.write_text("x")
bad_path = bad_parent / "idem.sqlite"
store = build_idempotency_store("sqlite", path=bad_path)
assert isinstance(store, InMemoryIdempotencyStore)
owners = durability_degraded_owners()
match = [o for o in owners if o.owner_id == "durability:idempotency"]
assert match
# #4339: the operator-facing reason must be redacted — the raw store
# path (and any backend detail) stays in logs, never in health/status.
assert str(bad_path) not in match[0].reason
clear_durability_degraded("idempotency")
def test_successful_build_clears_stale_degradation(self, tmp_path):
# #4339: a same-process rebuild (e.g. a config hot-reload) that restores
# the durable store must clear a prior degradation so health/status stop
# reporting non-durable operation after recovery.
from praisonai_bot.bots import (
SqliteIdempotencyStore,
build_idempotency_store,
)
from praisonai_bot.bots._session import (
clear_durability_degraded,
durability_degraded_owners,
record_durability_degraded,
)
clear_durability_degraded("idempotency")
record_durability_degraded("idempotency", reason="store unavailable")
assert any(
o.owner_id == "durability:idempotency"
for o in durability_degraded_owners()
)
store = build_idempotency_store("sqlite", path=tmp_path / "idem.sqlite")
assert isinstance(store, SqliteIdempotencyStore)
assert not any(
o.owner_id == "durability:idempotency"
for o in durability_degraded_owners()
)
clear_durability_degraded("idempotency")
def test_record_and_clear_roundtrip(self):
from praisonai_bot.bots._session import (
clear_durability_degraded,
durability_degraded_owners,
record_durability_degraded,
)
clear_durability_degraded("session")
record_durability_degraded("session", reason="store unavailable")
owners = durability_degraded_owners()
match = [o for o in owners if o.owner_id == "durability:session"]
assert match and match[0].owner_kind == "gateway"
assert match[0].retry_hint == "praisonai gateway doctor --fix"
clear_durability_degraded("session")
assert not any(
o.owner_id == "durability:session"
for o in durability_degraded_owners()
)
@@ -0,0 +1,151 @@
"""Issue #4341 — Per-tenant profile scope namespaces memory/session state.
A route (or channel) can name a ``profile``; the gateway stages it on the
session via ``set_profile_namespace`` so every ``_storage_key`` for the turn is
prefixed with the tenant scope. Two routes multiplexed on one process must never
share a transcript, and a route with no profile must stay unscoped (fail-closed)
rather than borrow another tenant's namespace.
"""
from __future__ import annotations
import asyncio
import threading
import pytest
from praisonai_bot.bots._session import BotSessionManager
class FakeAgent:
def __init__(self):
self.chat_history = []
self.calls = []
def chat(self, prompt):
self.calls.append((list(self.chat_history), prompt))
self.chat_history.append({"role": "user", "content": prompt})
reply = f"reply to {prompt}"
self.chat_history.append({"role": "assistant", "content": reply})
return reply
class GatedAgent:
"""Agent whose sync ``chat`` blocks on a threading gate.
``chat`` runs on an executor thread, so a ``threading.Event`` lets a test
hold one tenant's turn suspended while a second route stages a different
profile — proving one turn never persists into the other's namespace.
"""
def __init__(self, gate: "threading.Event"):
self.chat_history = []
self._gate = gate
def chat(self, prompt):
# Block (on the executor thread) until released so a second turn can
# stage its own profile while this turn is in-flight.
self._gate.wait(timeout=5)
self.chat_history.append({"role": "user", "content": prompt})
self.chat_history.append({"role": "assistant", "content": f"re:{prompt}"})
return f"re:{prompt}"
class TestProfileStorageKey:
def test_unscoped_by_default(self):
mgr = BotSessionManager(platform="discord")
assert mgr._storage_key("u1") == "u1"
def test_profile_prefixes_storage_key(self):
mgr = BotSessionManager(platform="discord")
mgr.set_profile_namespace("acme")
assert mgr._storage_key("u1") == "profile:acme:u1"
def test_distinct_profiles_isolate_same_user(self):
mgr = BotSessionManager(platform="discord")
mgr.set_profile_namespace("acme")
acme_key = mgr._storage_key("u1")
mgr.set_profile_namespace("globex")
globex_key = mgr._storage_key("u1")
assert acme_key != globex_key
assert acme_key == "profile:acme:u1"
assert globex_key == "profile:globex:u1"
def test_clear_fails_closed_to_unscoped(self):
mgr = BotSessionManager(platform="discord")
mgr.set_profile_namespace("acme")
mgr.set_profile_namespace(None)
assert mgr._storage_key("u1") == "u1"
def test_blank_profile_is_unscoped(self):
mgr = BotSessionManager(platform="discord")
mgr.set_profile_namespace(" ")
assert mgr._storage_key("u1") == "u1"
def test_profile_applies_to_per_chat_scope(self):
mgr = BotSessionManager(platform="telegram", session_scope="per_chat")
mgr.set_profile_namespace("acme")
key = mgr._storage_key("alice", chat_id="-100123", chat_type="group")
assert key.startswith("profile:acme:")
assert ":chat:-100123:" in key
class TestProfileMemoryIsolation:
@pytest.mark.asyncio
async def test_two_tenants_do_not_share_history(self):
agent = FakeAgent()
mgr = BotSessionManager(platform="discord")
mgr.set_profile_namespace("acme")
await mgr.chat(agent, "u1", "acme secret")
mgr.set_profile_namespace("globex")
await mgr.chat(agent, "u1", "globex secret")
keys = list(mgr._histories)
assert "profile:acme:u1" in keys
assert "profile:globex:u1" in keys
# Same raw user id, but the two tenants never collapse into one key.
assert "u1" not in keys
@pytest.mark.asyncio
async def test_overlapping_turns_do_not_cross_namespaces(self):
"""Two concurrent routed turns must keep their own tenant namespace.
Regression for the shared-mutable-state race (CodeRabbit/Greptile):
turn A stages ``acme`` and suspends inside ``chat()``; turn B then
stages ``globex``. The pre-fix code recomputed the storage key from a
shared field, so A's save could land in ``globex``. With turn-local
(ContextVar) scoping, A persists to ``acme`` and B to ``globex``.
"""
gate_a = threading.Event()
gate_b = threading.Event()
agent_a = GatedAgent(gate_a)
agent_b = GatedAgent(gate_b)
mgr = BotSessionManager(platform="discord")
async def run(agent, profile):
# Passing ``agent`` stages the namespace per-agent, so ``chat()``
# re-binds it turn-locally regardless of the parent context.
mgr.set_profile_namespace(profile, agent)
return await mgr.chat(agent, "u1", f"{profile} msg")
# Start both turns; each blocks on its gate inside chat() (on an
# executor thread), so they are genuinely overlapped and in-flight.
task_a = asyncio.create_task(run(agent_a, "acme"))
await asyncio.sleep(0.05) # let A stage + enter chat() and block
task_b = asyncio.create_task(run(agent_b, "globex"))
await asyncio.sleep(0.05) # let B stage + enter chat() and block
# Release B first, then A — the interleaving that would corrupt keys
# under a shared field (A's save would follow B's "globex").
gate_b.set()
gate_a.set()
await asyncio.gather(task_a, task_b)
keys = set(mgr._histories)
assert "profile:acme:u1" in keys
assert "profile:globex:u1" in keys
# Neither turn leaked into the other tenant's namespace, and no unscoped
# key was ever written.
assert "u1" not in keys
@@ -0,0 +1,62 @@
"""Regression test for Signal inbound idle-stall detection (Issue #4289).
A silently half-open Signal transport keeps returning a successfully-empty
``[]`` from the bridge without ever raising. If an empty poll refreshed the
inbound-liveness timestamp, ``health.last_activity`` would stay fresh forever
and the shared ``evaluate_channel_health`` STALE_SOCKET seam could never flag a
"deaf" channel. These tests pin that only a *delivered* envelope advances
inbound liveness, so a stalled poll loop remains detectable.
"""
import time
from unittest.mock import AsyncMock, MagicMock
import pytest
from praisonai_bot.bots.signal import SignalBot
class _FakeResponse:
def __init__(self, payload):
self.status = 200
self._payload = payload
async def __aenter__(self):
return self
async def __aexit__(self, *exc):
return False
async def json(self):
return self._payload
def _bot_with_response(payload):
bot = SignalBot(account="+15551234567")
session = MagicMock()
session.get = MagicMock(return_value=_FakeResponse(payload))
bot._http_session = session
return bot
@pytest.mark.asyncio
async def test_empty_poll_does_not_refresh_inbound_liveness():
bot = _bot_with_response([])
bot._last_inbound_activity = None
await bot._poll_once()
assert getattr(bot, "_last_inbound_activity", None) is None
@pytest.mark.asyncio
async def test_delivered_envelope_refreshes_inbound_liveness():
envelope = {"envelope": {"source": "+15550001111", "dataMessage": {"message": "hi"}}}
bot = _bot_with_response([envelope])
bot._last_inbound_activity = None
before = time.time()
await bot._poll_once()
assert bot._last_inbound_activity is not None
assert bot._last_inbound_activity >= before
@@ -194,6 +194,111 @@ def test_prune_marks_store_row_expired(tmp_path):
assert row["status"] == "expired"
def test_resolver_identity_recorded_in_audit_trail(tmp_path):
"""resolve() records the resolving principal on the durable audit row."""
store = ApprovalStore(path=tmp_path / "approvals.sqlite")
mgr = ExecApprovalManager(
ttl=300, store=store, allowlist_path=tmp_path / "allow.sqlite"
)
async def go():
rid, future = await mgr.register(tool_name="shell_exec", arguments={})
mgr.resolve(rid, Resolution(approved=True, resolver="operator:alice"))
for _ in range(100):
if store.pending_count() == 0:
break
await asyncio.sleep(0.01)
return rid, await future
rid, _ = asyncio.run(go())
row = store.get(rid)
assert row is not None
assert row["approver"] == "operator:alice"
def test_resolver_identity_defaults_to_gateway(tmp_path):
"""No resolver keeps today's anonymous ``gateway`` attribution."""
store = ApprovalStore(path=tmp_path / "approvals.sqlite")
mgr = ExecApprovalManager(
ttl=300, store=store, allowlist_path=tmp_path / "allow.sqlite"
)
async def go():
rid, future = await mgr.register(tool_name="shell_exec", arguments={})
mgr.resolve(rid, Resolution(approved=True))
for _ in range(100):
if store.pending_count() == 0:
break
await asyncio.sleep(0.01)
return rid, await future
rid, _ = asyncio.run(go())
assert store.get(rid)["approver"] == "gateway"
def test_authorized_reviewer_binding_rejects_other_operator(tmp_path):
"""A bound request cannot be resolved by an unauthorised operator."""
mgr = ExecApprovalManager(ttl=300, durable=False)
async def go():
rid, future = await mgr.register(
tool_name="deploy",
arguments={},
authorized_reviewers=["operator:alice"],
)
# Wrong operator is rejected and the request stays pending.
assert mgr.resolve(rid, Resolution(approved=True, resolver="operator:bob")) is False
assert mgr.get_pending(rid) is not None
# Authorised operator resolves (first-answer-wins preserved).
assert mgr.resolve(rid, Resolution(approved=True, resolver="operator:alice")) is True
return await future
res = asyncio.run(go())
assert res.approved is True
def test_unbound_request_resolvable_by_anyone(tmp_path):
"""Without a reviewer binding any resolver may resolve (legacy behaviour)."""
mgr = ExecApprovalManager(ttl=300, durable=False)
async def go():
rid, future = await mgr.register(tool_name="deploy", arguments={})
assert mgr.resolve(rid, Resolution(approved=True, resolver="operator:bob")) is True
return await future
assert asyncio.run(go()).approved is True
def test_authorized_reviewers_survive_restart(tmp_path):
"""Per-request custody binding is durable across a rehydrate."""
store = ApprovalStore(path=tmp_path / "approvals.sqlite")
allow_path = tmp_path / "allow.sqlite"
async def register_only():
mgr = ExecApprovalManager(ttl=300, store=store, allowlist_path=allow_path)
rid, _ = await mgr.register(
tool_name="deploy",
arguments={},
authorized_reviewers=["operator:alice"],
)
return rid
rid = asyncio.run(register_only())
async def restart_and_check():
mgr2 = ExecApprovalManager(ttl=300, store=store, allowlist_path=allow_path)
await mgr2.rehydrate()
req = mgr2.get_pending(rid)
# Wrong operator still rejected after restart.
assert mgr2.resolve(rid, Resolution(approved=True, resolver="operator:bob")) is False
assert mgr2.resolve(rid, Resolution(approved=True, resolver="operator:alice")) is True
return req
req = asyncio.run(restart_and_check())
assert req is not None
assert req.authorized_reviewers == frozenset({"operator:alice"})
def test_allow_always_grant_persists_across_manager_restart(tmp_path):
"""allow_always via resolve() persists and short-circuits after restart."""
store = ApprovalStore(path=tmp_path / "approvals.sqlite")
@@ -225,3 +330,51 @@ def test_allow_always_grant_persists_across_manager_restart(tmp_path):
rid, res = asyncio.run(restart_fast_path())
assert rid == "auto"
assert res.approved is True
def test_gateway_backend_forwards_authorized_reviewers(tmp_path):
"""The production backend must bind per-request reviewer custody.
Regression guard: ``GatewayApprovalBackend`` previously dropped
``ApprovalRequest.authorized_reviewers`` on the way to ``register()``,
leaving every request unbound. This verifies the custody set reaches the
manager so an unauthorised operator is rejected end-to-end.
"""
from praisonaiagents.approval.protocols import ApprovalRequest
from praisonai_bot.gateway.gateway_approval import GatewayApprovalBackend
mgr = ExecApprovalManager(ttl=300, allowlist_path=tmp_path / "allow.sqlite")
backend = GatewayApprovalBackend(manager=mgr, timeout=5)
async def go():
request = ApprovalRequest(
tool_name="deploy",
arguments={"target": "prod"},
risk_level="high",
agent_name="deployer",
authorized_reviewers=["operator:alice"],
)
task = asyncio.create_task(backend.request_approval(request))
# Let the backend register the pending request.
await asyncio.sleep(0)
pending = mgr.list_pending()
assert len(pending) == 1
rid = pending[0]["request_id"]
# The bound pending request carries the custody set.
req = mgr.get_pending(rid)
assert req is not None
assert req.authorized_reviewers == frozenset({"operator:alice"})
# An unauthorised operator cannot resolve it.
assert mgr.resolve(
rid, Resolution(approved=True, resolver="operator:bob")
) is False
# The authorised operator can, completing the awaited request.
assert mgr.resolve(
rid, Resolution(approved=True, resolver="operator:alice")
) is True
decision = await task
assert decision.approved is True
asyncio.run(go())
@@ -97,3 +97,130 @@ class TestChunkMessage:
assert len(chunks) >= 5
for chunk in chunks:
assert len(chunk) <= 100
class TestEnforceHardCap:
"""Tests for enforce_hard_cap — the outbound over-cap guard (Issue #4319)."""
def _cap(self, chunks, max_length):
from praisonai_bot.bots._chunk import enforce_hard_cap
return enforce_hard_cap(chunks, max_length)
def test_under_cap_chunks_unchanged(self):
"""Chunks already within the cap pass through untouched."""
chunks = ["short one", "short two"]
assert self._cap(chunks, 100) == chunks
def test_over_cap_prose_is_split(self):
"""An over-cap plain chunk is split so every piece fits."""
big = "word " * 200 # ~1000 chars
result = self._cap([big], 100)
assert len(result) >= 2
for piece in result:
assert len(piece) <= 100
def test_over_cap_fence_is_split_and_rebalanced(self):
"""An over-cap code fence is split with each piece a valid fence."""
body = "\n".join(f"line {i}" for i in range(400))
fence = "```python\n" + body + "\n```"
assert len(fence) > 100
result = self._cap([fence], 100)
assert len(result) >= 2
for piece in result:
assert len(piece) <= 100
# Each emitted piece is a balanced, valid fence.
assert piece.count("```") == 2
assert piece.startswith("```")
assert piece.rstrip().endswith("```")
def test_over_cap_fence_preserves_language_hint(self):
"""The language hint is carried onto every re-opened fence piece."""
body = "\n".join(f"row{i}" for i in range(300))
fence = "```json\n" + body + "\n```"
result = self._cap([fence], 80)
assert len(result) >= 2
for piece in result:
assert piece.startswith("```json")
def test_no_content_lost_across_split_fence(self):
"""The source characters all survive the fence split (order preserved)."""
body = "".join(f"payload-{i}\n" for i in range(50)).rstrip()
fence = "```\n" + body + "\n```"
result = self._cap([fence], 40)
# Reassemble the inner bodies (strip the ``` wrappers each piece adds).
inner = "".join(
piece[3:].split("\n", 1)[1].rsplit("\n```", 1)[0]
for piece in result
)
# Every non-whitespace character of the body is delivered, in order.
assert inner.replace("\n", "") == body.replace("\n", "")
def test_matches_the_reported_pure_fence_failure(self):
"""A ~9.8k pure fence is delivered as multiple capped chunks."""
body = "\n".join("x" * 60 for _ in range(160))
fence = "```\n" + body + "\n```"
assert len(fence) > 4096
result = self._cap([fence], 4096)
assert len(result) >= 2
for piece in result:
assert len(piece) <= 4096
def test_mixed_fence_and_prose_not_corrupted(self):
"""A closed fence followed by prose must NOT be re-wrapped as code.
Greptile P1: ``_is_fence_block`` matched anything starting with ``` so a
chunk of "closed fence + trailing prose" was wrapped in a fresh fence,
rendering the prose as code. The chunk must instead be hard-split as
plain text (Issue #4319).
"""
prose = "This is prose that must stay prose. " * 10
chunk = "```python\nprint('hi')\n```\n\n" + prose
assert len(chunk) > 100
result = self._cap([chunk], 100)
combined = "\n".join(result)
# The original prose survives verbatim somewhere in the output.
assert "This is prose that must stay prose." in combined
# No fabricated language-tagged fence wraps the prose.
assert combined.count("```python") <= 1
for piece in result:
assert len(piece) <= 100
def test_multiple_fences_in_one_chunk_not_rewrapped(self):
"""Two fences in one over-cap chunk are hard-split, not merged as code."""
chunk = (
"```\n" + ("a" * 60) + "\n```\n\n"
"prose between fences\n\n"
"```\n" + ("b" * 60) + "\n```"
)
result = self._cap([chunk], 50)
combined = "\n".join(result)
assert "prose between fences" in combined
for piece in result:
assert len(piece) <= 50
class TestUtf16LengthUnit:
"""Telegram measures length in UTF-16 units, not codepoints (Issue #4319)."""
def test_emoji_chunks_respect_utf16_cap(self):
"""Emoji-heavy text is capped by UTF-16 units so Telegram won't reject it."""
from praisonai_bot.bots._chunk import (
chunk_message,
enforce_hard_cap,
_calculate_length,
)
# Each 😀 is 1 codepoint but 2 UTF-16 units. 200 emoji = 400 UTF-16.
text = "😀" * 200
max_len = 100
chunks = chunk_message(text, max_length=max_len, length_unit="utf16")
chunks = enforce_hard_cap(chunks, max_len, length_unit="utf16")
for chunk in chunks:
assert _calculate_length(chunk, "utf16") <= max_len
def test_calculate_length_utf16_counts_surrogate_pairs(self):
"""A supplementary-plane char counts as 2 UTF-16 units, 1 codepoint."""
from praisonai_bot.bots._chunk import _calculate_length
assert _calculate_length("😀", "codepoints") == 1
assert _calculate_length("😀", "utf16") == 2
@@ -310,6 +310,145 @@ class TestAckReactorIntegration:
assert "self._ack.done" in source
class TestAckScopeGating:
"""Ack reactions must be scope-gated so busy groups aren't spammed."""
def test_default_scope_is_group_mentions(self):
from praisonai_bot.bots._ack import AckReactor, AckScope
reactor = AckReactor(ack_emoji="⏳")
assert reactor.scope is AckScope.group_mentions
def test_ambient_group_message_not_acked_by_default(self):
from praisonai_bot.bots._ack import AckReactor
reactor = AckReactor(ack_emoji="⏳")
assert not reactor.should_ack(
is_direct=False, is_mention=False, is_ambient=True
)
def test_direct_and_mention_acked_by_default(self):
from praisonai_bot.bots._ack import AckReactor
reactor = AckReactor(ack_emoji="⏳")
assert reactor.should_ack(is_direct=True, is_mention=False, is_ambient=False)
assert reactor.should_ack(is_direct=False, is_mention=True, is_ambient=False)
def test_scope_off_never_acks_and_disables(self):
from praisonai_bot.bots._ack import AckReactor, AckScope
reactor = AckReactor(ack_emoji="⏳", scope=AckScope.off)
assert not reactor.enabled
assert not reactor.should_ack(
is_direct=True, is_mention=True, is_ambient=False
)
def test_scope_all_acks_ambient(self):
from praisonai_bot.bots._ack import AckReactor, AckScope
reactor = AckReactor(ack_emoji="⏳", scope=AckScope.all)
assert reactor.should_ack(
is_direct=False, is_mention=False, is_ambient=True
)
def test_scope_direct_only(self):
from praisonai_bot.bots._ack import AckReactor, AckScope
reactor = AckReactor(ack_emoji="⏳", scope=AckScope.direct)
assert reactor.should_ack(is_direct=True, is_mention=False, is_ambient=False)
assert not reactor.should_ack(
is_direct=False, is_mention=True, is_ambient=False
)
def test_scope_group_all_acks_non_ambient(self):
from praisonai_bot.bots._ack import AckReactor, AckScope
reactor = AckReactor(ack_emoji="⏳", scope=AckScope.group_all)
assert reactor.should_ack(
is_direct=False, is_mention=False, is_ambient=False
)
assert not reactor.should_ack(
is_direct=False, is_mention=False, is_ambient=True
)
def test_empty_emoji_never_acks(self):
from praisonai_bot.bots._ack import AckReactor, AckScope
reactor = AckReactor(ack_emoji="", scope=AckScope.all)
assert not reactor.enabled
assert not reactor.should_ack(
is_direct=True, is_mention=True, is_ambient=False
)
def test_scope_coerce_from_string_and_default(self):
from praisonai_bot.bots._ack import AckReactor, AckScope
assert AckReactor(ack_emoji="⏳", scope="group-all").scope is AckScope.group_all
assert AckReactor(ack_emoji="⏳", scope="group_all").scope is AckScope.group_all
assert AckReactor(ack_emoji="⏳", scope="bogus").scope is AckScope.group_mentions
def test_should_ack_message_derives_from_bot_message(self):
from praisonai_bot.bots._ack import AckReactor
from praisonaiagents.bots.protocols import BotChannel, BotMessage
reactor = AckReactor(ack_emoji="⏳")
dm = BotMessage(content="hi", channel=BotChannel(channel_id="c", channel_type="dm"))
assert reactor.should_ack_message(dm)
group = BotMessage(
content="ambient", channel=BotChannel(channel_id="g", channel_type="group")
)
assert not reactor.should_ack_message(group)
assert reactor.should_ack_message(group, is_mention=True)
reply = BotMessage(
content="re",
channel=BotChannel(channel_id="g", channel_type="group"),
reply_to="123",
)
assert reactor.should_ack_message(reply)
def test_gateway_telegram_path_scope_gates_ack(self):
"""Gateway-hosted Telegram must gate acks by scope, not just ``enabled``.
Greptile #4293: the gateway's own Telegram message handler previously
acked whenever the reactor was ``enabled``, bypassing ``ack_scope`` so
ambient group chatter still got a reaction. Assert the handler routes
through ``should_ack_message`` like the standalone adapter.
"""
import inspect
from praisonai_bot.gateway import server
source = inspect.getsource(server)
assert "bot._ack.should_ack_message(message)" in source
def test_slack_thread_reply_treated_as_engaged(self):
"""A Slack thread reply is engaged, not ambient, under group-mentions.
Greptile #4293: when ``mention_required`` is off, a follow-up reply in
an ongoing thread (no fresh @mention) is still processed; it must not be
classified as ambient or it would silently lose its ack. The adapter
treats ``thread_ts`` as a mention; verify the shared contract acks it.
"""
from praisonai_bot.bots._ack import AckReactor
reactor = AckReactor(ack_emoji="⏳")
# thread reply -> adapter sets is_mention=True, is_ambient=False
assert reactor.should_ack(
is_direct=False, is_mention=True, is_ambient=False
)
def test_slack_adapter_uses_thread_ts_for_ack(self):
"""Slack ack gate must consider ``thread_ts`` when deriving mention."""
import inspect
from praisonai_bot.bots.slack import SlackBot
source = inspect.getsource(SlackBot.start)
assert 'event.get("thread_ts")' in source
assert "_in_thread" in source
class TestWhatsAppRateLimiterIntegration:
"""Tests for WhatsApp rate limiter integration."""
@@ -69,6 +69,49 @@ class TestSplitMediaFromOutput:
result = split_media_from_output("MEDIA:https://example.com/audio.mp3")
assert "https://example.com/audio.mp3" in result["media_urls"]
def test_blank_line_between_paragraphs_preserved(self):
"""A single blank line paragraph separator is preserved (Issue #4319)."""
result = split_media_from_output("Paragraph one.\n\nParagraph two.")
assert result["text"] == "Paragraph one.\n\nParagraph two."
def test_load_bearing_blank_line_preserved(self):
"""The HTTP header/body separator blank line survives (Issue #4319)."""
text = (
"POST /v1/messages HTTP/1.1\n"
"Host: api.example.com\n"
"Content-Type: application/json\n"
"\n"
'{"model": "x", "messages": []}'
)
result = split_media_from_output(text)
assert result["text"] == text
def test_multiple_blank_lines_collapsed(self):
"""Excess blank runs collapse to one paragraph break (Issue #4319)."""
result = split_media_from_output("A\n\n\n\n\nB")
assert result["text"] == "A\n\nB"
def test_no_blank_lines_stripped_without_media(self):
"""A reply with no MEDIA: keeps all its blank lines (Issue #4319)."""
text = "Line1\n\nLine2\n\nLine3"
result = split_media_from_output(text)
assert result["text"].count("\n\n") == 2
def test_blank_lines_preserved_around_media(self):
"""Stripping a MEDIA line does not destroy surrounding paragraphs."""
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
temp_path = f.name
try:
text = f"Intro paragraph.\n\nMEDIA:{temp_path}\n\nOutro paragraph."
result = split_media_from_output(text)
assert temp_path in result["media_urls"]
assert "Intro paragraph." in result["text"]
assert "Outro paragraph." in result["text"]
# The two real paragraphs remain separated by a blank line.
assert "\n\n" in result["text"]
finally:
os.unlink(temp_path)
class TestIsAudioFile:
"""Tests for is_audio_file function."""
@@ -0,0 +1,232 @@
"""Tests for gateway memory-pressure cache eviction wiring (Issue #3804).
The core planner ``plan_pressure_evictions`` is unit-tested in the core SDK; here
we verify the *wrapper enactor* — ``BotSessionManager.sweep_under_pressure`` and
``warm_sessions`` — plus the concrete stdlib ``CgroupMemoryPressure`` reader.
"""
from __future__ import annotations
import pytest
from praisonai_bot.bots._session import BotSessionManager
from praisonai_bot.gateway.memory_pressure import CgroupMemoryPressure
class _Store:
"""Minimal in-memory SessionStore so sessions count as 'flushed'."""
def __init__(self):
self.saved = {}
def set_chat_history(self, key, history):
self.saved[key] = list(history)
def get_chat_history(self, key):
return list(self.saved.get(key, []))
def clear_session(self, key):
self.saved.pop(key, None)
class _Pressure:
"""Scripted MemoryPressureProtocol: fixed budget, decreasing RSS samples."""
def __init__(self, budget, rss_samples):
self._budget = budget
self._rss = list(rss_samples)
def cgroup_limit_mb(self):
return self._budget
def anon_rss_mb(self):
# First call returns the initial reading; subsequent calls pop the next
# (simulating RSS dropping as caches are evicted).
return self._rss.pop(0) if len(self._rss) > 1 else self._rss[0]
def _warm(mgr, key, last_active, flushed=True):
"""Seed a warm in-memory cache with a given LRU timestamp.
``flushed`` mirrors a completed store write; when a store is configured the
cache is only rebuildable (and thus evictable) once its last persist
succeeded, so tests opt in explicitly.
"""
mgr._histories[key] = [{"role": "user", "content": "hi"}]
mgr._last_active[key] = last_active
if flushed and mgr._store is not None:
mgr._flushed.add(key)
def test_warm_sessions_reports_flushed_when_store_present():
store = _Store()
mgr = BotSessionManager(store=store)
_warm(mgr, "a", 1.0)
warm = mgr.warm_sessions()
assert len(warm) == 1
assert warm[0].session_id == "a"
assert warm[0].flushed is True
assert warm[0].in_flight is False
def test_warm_sessions_unflushed_without_store():
mgr = BotSessionManager(store=None)
_warm(mgr, "a", 1.0)
assert mgr.warm_sessions()[0].flushed is False
def test_sweep_evicts_coldest_first_until_within_budget():
store = _Store()
mgr = BotSessionManager(store=store)
_warm(mgr, "cold", 1.0)
_warm(mgr, "warm", 100.0)
# Budget 1000 MiB, target = 900. Start at 1000 (over), drop to 850 after the
# first (coldest) eviction so the sweep stops having shed exactly one cache.
mp = _Pressure(1000.0, [1000.0, 850.0])
evicted = mgr.sweep_under_pressure(mp, headroom=0.9)
assert evicted == 1
assert "cold" not in mgr._histories # coldest dropped
assert "warm" in mgr._histories # warmest kept
def test_sweep_never_evicts_unflushed_cache():
mgr = BotSessionManager(store=None) # no store => nothing rebuildable
_warm(mgr, "a", 1.0)
mp = _Pressure(100.0, [1000.0])
assert mgr.sweep_under_pressure(mp) == 0
assert "a" in mgr._histories
def test_sweep_never_evicts_in_flight_cache():
store = _Store()
mgr = BotSessionManager(store=store)
_warm(mgr, "a", 1.0)
mgr._in_flight["a"] = 1 # a live turn is executing
mp = _Pressure(100.0, [1000.0])
assert mgr.sweep_under_pressure(mp) == 0
assert "a" in mgr._histories
def test_sweep_noop_when_no_budget():
store = _Store()
mgr = BotSessionManager(store=store)
_warm(mgr, "a", 1.0)
mp = _Pressure(None, [1000.0]) # host without a cgroup limit
assert mgr.sweep_under_pressure(mp) == 0
assert "a" in mgr._histories
def test_sweep_noop_when_within_budget():
store = _Store()
mgr = BotSessionManager(store=store)
_warm(mgr, "a", 1.0)
mp = _Pressure(1000.0, [500.0]) # RSS well under 900 target
assert mgr.sweep_under_pressure(mp) == 0
assert "a" in mgr._histories
def test_soft_evict_leaves_store_intact():
store = _Store()
store.set_chat_history("a", [{"role": "user", "content": "persisted"}])
mgr = BotSessionManager(store=store)
_warm(mgr, "a", 1.0)
assert mgr._soft_evict("a") is True
assert "a" not in mgr._histories # in-memory dropped
assert store.get_chat_history("a") # durable copy remains
def test_cgroup_reader_degrades_gracefully(monkeypatch):
reader = CgroupMemoryPressure()
def _boom(*_a, **_k):
raise OSError("no cgroup here")
monkeypatch.setattr("builtins.open", _boom)
assert reader.cgroup_limit_mb() is None
assert reader.anon_rss_mb() == 0.0
def test_cgroup_reader_parses_v2_max(tmp_path, monkeypatch):
reader = CgroupMemoryPressure()
limit_file = tmp_path / "memory.max"
limit_file.write_text("536870912\n") # 512 MiB
real_open = open
def _fake_open(path, *a, **k):
if path == "/sys/fs/cgroup/memory.high":
raise OSError
if path == "/sys/fs/cgroup/memory.max":
return real_open(limit_file, *a, **k)
raise OSError
monkeypatch.setattr("builtins.open", _fake_open)
assert reader.cgroup_limit_mb() == pytest.approx(512.0)
def test_cgroup_reader_v2_unlimited_is_none(tmp_path, monkeypatch):
reader = CgroupMemoryPressure()
hi = tmp_path / "memory.high"
hi.write_text("max\n")
real_open = open
def _fake_open(path, *a, **k):
if path == "/sys/fs/cgroup/memory.high":
return real_open(hi, *a, **k)
raise OSError
monkeypatch.setattr("builtins.open", _fake_open)
assert reader.cgroup_limit_mb() is None
def test_cgroup_reader_prefers_nested_cgroup_limit(tmp_path, monkeypatch):
"""A nested cgroup's lower limit is used instead of the root's (#4296 review)."""
reader = CgroupMemoryPressure()
proc = tmp_path / "cgroup"
proc.write_text("0::/mygroup\n") # v2: process lives in /mygroup
nested = tmp_path / "nested_memory.max"
nested.write_text("268435456\n") # 256 MiB (the real, lower ceiling)
real_open = open
def _fake_open(path, *a, **k):
if path == "/proc/self/cgroup":
return real_open(proc, *a, **k)
if path == "/sys/fs/cgroup/mygroup/memory.high":
raise OSError # no soft limit at the nested level
if path == "/sys/fs/cgroup/mygroup/memory.max":
return real_open(nested, *a, **k)
raise OSError # root files must NOT be consulted once nested resolves
monkeypatch.setattr("builtins.open", _fake_open)
assert reader.cgroup_limit_mb() == pytest.approx(256.0)
def test_sweep_skips_cache_when_last_store_write_failed():
"""A caught store-write failure marks the cache unflushed → never evicted.
Regression for the CodeRabbit data-loss finding: soft-evicting a cache
whose only current copy is in memory would restore a stale/empty transcript.
"""
class _FlakyStore(_Store):
def __init__(self):
super().__init__()
self.fail = False
def set_chat_history(self, key, history):
if self.fail:
raise RuntimeError("backend down")
super().set_chat_history(key, history)
store = _FlakyStore()
mgr = BotSessionManager(store=store)
# First successful save marks the session flushed (rebuildable).
mgr._save_history("a", [{"role": "user", "content": "one"}])
assert mgr.warm_sessions()[0].flushed is True
# Next save fails to persist: in-memory diverges, marker must be cleared.
store.fail = True
mgr._save_history("a", [{"role": "user", "content": "two"}])
assert mgr.warm_sessions()[0].flushed is False
# Pressure sweep must therefore leave the only current copy intact.
mp = _Pressure(100.0, [1000.0])
assert mgr.sweep_under_pressure(mp) == 0
assert "a" in mgr._histories
@@ -604,10 +604,16 @@ class CDPBrowserAgent:
return {"success": True}
elif action_type == "type":
# Focus element and type
await self._send("Runtime.evaluate", {
"expression": f"document.querySelector('{selector}')?.focus()"
# Focus element and type; verify the selector matched an element
focus_result = await self._send("Runtime.evaluate", {
"expression": (
f"(function(){{const el=document.querySelector('{selector}');"
f"if(!el)return false;el.focus();return true;}})()"
),
"returnByValue": True
})
if not focus_result.get("result", {}).get("value"):
return {"success": False, "error": f"selector matched no element: {selector}"}
await asyncio.sleep(0.2)
# Clear existing text
@@ -670,10 +676,16 @@ class CDPBrowserAgent:
await asyncio.sleep(1)
return {"success": True}
else:
# Fallback to JS click
await self._send("Runtime.evaluate", {
"expression": f"document.querySelector('{selector}')?.click()"
# Fallback to JS click; verify the selector matched an element
click_result = await self._send("Runtime.evaluate", {
"expression": (
f"(function(){{const el=document.querySelector('{selector}');"
f"if(!el)return false;el.click();return true;}})()"
),
"returnByValue": True
})
if not click_result.get("result", {}).get("value"):
return {"success": False, "error": f"selector matched no element: {selector}"}
await asyncio.sleep(1)
return {"success": True}
@@ -689,14 +701,17 @@ class CDPBrowserAgent:
return {"success": True}
elif action_type == "clear_input":
# Focus element and clear its value entirely
await self._send("Runtime.evaluate", {
# Focus element and clear its value entirely; verify it matched
clear_result = await self._send("Runtime.evaluate", {
"expression": (
f"(function(){{const el=document.querySelector('{selector}');"
f"if(el){{el.focus();el.value='';"
f"el.dispatchEvent(new Event('input',{{bubbles:true}}));}}}})()"
)
f"if(!el)return false;el.focus();el.value='';"
f"el.dispatchEvent(new Event('input',{{bubbles:true}}));return true;}})()"
),
"returnByValue": True
})
if not clear_result.get("result", {}).get("value"):
return {"success": False, "error": f"selector matched no element: {selector}"}
await asyncio.sleep(0.2)
return {"success": True}
@@ -361,14 +361,36 @@ def _run_alternative_engine(
if enable_vision:
console.print(f" [cyan]Vision mode: ON[/cyan]")
# Auto-create screenshot dir when record_video is enabled
# Auto-create screenshot dir when record_video is enabled. Only the cdp
# engine implements screencast/screenshots, so restrict the auto-dir and
# the "Recording to" notice to cdp; playwright/hybrid get a warning below.
actual_screenshot_dir = screenshot_dir
if record_video and not actual_screenshot_dir:
if record_video and not actual_screenshot_dir and engine == "cdp":
actual_screenshot_dir = str(Path.home() / ".praisonai" / "browser_screenshots" / datetime.now().strftime("%Y%m%d_%H%M%S"))
Path(actual_screenshot_dir).mkdir(parents=True, exist_ok=True)
console.print(f" [green]📹 Recording to: {actual_screenshot_dir}[/green]")
elif actual_screenshot_dir:
elif actual_screenshot_dir and engine == "cdp":
console.print(f" [dim]Screenshots: {actual_screenshot_dir}[/dim]")
# playwright/hybrid honour goal, url, model and max_steps but do not
# implement the CDP-only extras. Warn instead of silently discarding them.
if engine in ("playwright", "hybrid"):
cdp_only = []
if enable_vision:
cdp_only.append("--vision")
if screenshot_dir:
cdp_only.append("--screenshots")
if record_video:
cdp_only.append("--record-video")
if max_retries != 3:
cdp_only.append("--max-retries")
if not record_session:
cdp_only.append("--no-record")
if cdp_only:
console.print(
f"[yellow]Ignored on the {engine} engine:[/yellow] {', '.join(cdp_only)}"
)
console.print("[dim] These options require --engine cdp.[/dim]")
console.print()
async def run():
@@ -405,6 +427,7 @@ def _run_alternative_engine(
enable_vision=enable_vision or record_video,
record_session=record_session,
screenshot_dir=actual_screenshot_dir,
record_video=record_video,
debug=debug,
)
@@ -478,17 +501,19 @@ def run_agent(
playwright: Cross-browser automation via Playwright
hybrid: Auto-select best available engine
Features:
Features (require --engine cdp; ignored on the default extension engine,
which only forwards goal, model and max-steps. Playwright/hybrid honour
goal, url, model and max-steps but do not implement these extras):
--max-retries: Automatic retry with alternative selectors on failures
--vision: Enable vision-based element detection (requires gpt-4o)
--screenshots: Save screenshots of each step for debugging
--no-record: Disable session recording to database
--no-record: Disable session recording (CDP engine only)
Example:
praisonai browser run "Search for PraisonAI on Google"
praisonai browser run "task" --engine cdp --headless
praisonai browser run "task" --engine hybrid --vision
praisonai browser run "task" --screenshots ./screenshots
praisonai browser run "task" --engine cdp --screenshots ./screenshots
"""
import logging
import json
@@ -519,10 +544,39 @@ def run_agent(
)
return
# The extension engine forwards only goal, model and max_steps to the bridge;
# the extension drives the tab the user already has open. Warn (rather than
# silently discard) when engine-only options are set on the default path, so
# the user isn't misled into thinking they took effect. Use --engine cdp for
# these features.
ignored = []
if enable_vision:
ignored.append("--vision")
if screenshot_dir:
ignored.append("--screenshots")
if record_video:
ignored.append("--record-video")
if max_retries != 3:
ignored.append("--max-retries")
if headless:
ignored.append("--headless")
if extension_path:
ignored.append("--extension")
if not record:
ignored.append("--no-record")
if ignored:
console.print(
f"[yellow]Ignored on the extension engine:[/yellow] {', '.join(ignored)}"
)
console.print(
"[dim] These options require --engine cdp (or playwright/hybrid).[/dim]"
)
console.print("[bold blue]Starting browser agent[/bold blue]")
console.print(f" Goal: {goal}")
console.print(f" URL: {url}")
console.print(f" URL (context only; extension uses the active tab): {url}")
console.print(f" Model: {model}")
console.print(f" Max steps: {max_steps}")
if debug:
console.print(f" [dim]Debug mode: ON[/dim]")
console.print()
@@ -548,7 +602,7 @@ def run_agent(
console.print("[dim]Connected to server[/dim]")
# Send start_session
msg = json.dumps({"type": "start_session", "goal": goal, "model": model})
msg = json.dumps({"type": "start_session", "goal": goal, "model": model, "max_steps": max_steps})
await ws.send(msg)
console.print(f"[blue]Sent:[/blue] start_session")
@@ -647,7 +701,7 @@ def run_agent(
try:
async with websockets.connect(ws_url, ping_interval=30) as ws:
# Start session
msg = json.dumps({"type": "start_session", "goal": goal, "model": model})
msg = json.dumps({"type": "start_session", "goal": goal, "model": model, "max_steps": max_steps})
await ws.send(msg)
# Wait for session_id
@@ -536,6 +536,10 @@ class BrowserServer:
}
# Per-session step limit (set from the start_session message) so a
# per-run --max-steps is honoured, falling back to the server default.
session_max_steps = getattr(agent, "max_steps", self.max_steps)
# Update session
if self._sessions:
step_number = message.get("step_number", 0)
@@ -551,8 +555,8 @@ class BrowserServer:
thought=action.get("thought", ""),
)
# Check if done or max steps
if action.get("done") or step_number >= self.max_steps:
# Check if done or max steps (per-session limit computed above).
if action.get("done") or step_number >= session_max_steps:
status = "completed" if action.get("done") else "stopped"
self._sessions.update_session(session_id, status=status)
@@ -575,7 +579,7 @@ class BrowserServer:
logger.error(f"[SERVER][ERROR] broadcast:server.py → {e}")
# Also send completion status if done
if action.get("done") or message.get("step_number", 0) >= self.max_steps:
if action.get("done") or message.get("step_number", 0) >= session_max_steps:
status = "completed" if action.get("done") else "stopped"
status_msg = {
"type": "status",
+17 -8
View File
@@ -377,13 +377,22 @@ class LazyCommandGroup(TyperGroup):
def get_command(self, ctx: click.Context, name: str) -> Optional[click.Command]:
"""Lazily import and return the command, tagged with its help category."""
command = self._resolve_command(ctx, name)
if command is not None and _help_panel_unset(command):
# Group the command into a categorised ``--help`` panel. This is the
# single hook Typer's rich renderer reads; setting it lazily here keeps
# the registry the source of truth without importing every command up
# front just to categorise it. Commands that explicitly declare their
# own ``rich_help_panel`` keep it.
command.rich_help_panel = category_for(name)
if command is not None:
# ``typer_get_command`` yields ``""`` for a ``typer.Typer(help=…)``
# declared without ``name=``, and collapses a single-command app to
# that command's own name. Either way the resulting ``command.name``
# does not match the registry key the user must actually type, so the
# ``--help`` panel renders a blank or wrong name. The registry key is
# the source of truth for the typeable name — pin it here.
if command.name != name:
command.name = name
if _help_panel_unset(command):
# Group the command into a categorised ``--help`` panel. This is
# the single hook Typer's rich renderer reads; setting it lazily
# here keeps the registry the source of truth without importing
# every command up front just to categorise it. Commands that
# explicitly declare their own ``rich_help_panel`` keep it.
command.rich_help_panel = category_for(name)
return command
def _resolve_command(self, ctx: click.Context, name: str) -> Optional[click.Command]:
@@ -945,7 +954,7 @@ def get_command_names():
names.update({"app", "standardise", "standardize"})
# Dynamically registered retrieval commands (no static module entry)
# NOTE: retrieval_module.register_commands(app) adds these commands dynamically
names.update({"index", "query"})
names.update({"index", "query", "search"})
return names
def register_commands():
@@ -1,41 +1,164 @@
"""
Rules command group for PraisonAI CLI.
Provides rules management commands.
Provides native rules management (list/add/clear) for the standalone
``praisonai-code`` terminal product. The underlying storage is core's
``praisonaiagents.memory.RulesManager`` (PRAISON.md / CLAUDE.md / AGENTS.md and
``.praison/rules/*.md``), so these commands work without the optional
``praisonai`` wrapper. The wrapper bridge is retained only as a fallback for
environments where core is unexpectedly unavailable.
"""
from __future__ import annotations
import os
import typer
app = typer.Typer(help="Rules management")
def _rules_manager():
"""Return a core ``RulesManager`` for the current workspace, or ``None``.
Core (``praisonaiagents``) is a hard dependency of ``praisonai-code``, so
this normally succeeds on a standalone install. ``None`` is returned only
when core is genuinely unavailable, letting the caller fall back to the
wrapper bridge with a single clear notice rather than crashing.
"""
try:
from praisonaiagents.memory import RulesManager
except ImportError:
return None
return RulesManager(workspace_path=os.getcwd())
def _fallback(argv: list[str]) -> None:
"""Delegate to the wrapper bridge for a rules subcommand (last resort)."""
from praisonai_code._wrapper_bridge import run_wrapper_command
run_wrapper_command(argv, feature="rules")
@app.command("list")
def rules_list():
"""List active rules."""
from praisonai_code._wrapper_bridge import run_wrapper_command
argv = ['rules', 'list']
run_wrapper_command(argv, feature="rules")
manager = _rules_manager()
if manager is None:
_fallback(['rules', 'list'])
return
try:
all_rules = manager.get_all_rules()
except Exception as exc: # noqa: BLE001 - never crash the CLI on rules read
typer.echo(f"Failed to load rules: {exc}", err=True)
raise typer.Exit(1)
if not all_rules:
typer.echo(
"No rules found. Create PRAISON.md, CLAUDE.md, AGENTS.md, "
"or files in .praison/rules/"
)
return
for rule in all_rules:
desc = rule.description or ""
if len(desc) > 60:
desc = desc[:57] + "..."
suffix = f" - {desc}" if desc else ""
typer.echo(f"{rule.name} [{rule.activation}]{suffix}")
@app.command("add")
def rules_add(
rule: str = typer.Argument(..., help="Rule to add"),
rule: str = typer.Argument(..., help="Rule name"),
content: str = typer.Argument(None, help="Rule content"),
):
"""Add a rule."""
from praisonai_code._wrapper_bridge import run_wrapper_command
argv = ['rules', 'add', rule]
run_wrapper_command(argv, feature="rules")
"""Add a rule (workspace scope)."""
manager = _rules_manager()
if manager is None:
argv = ['rules', 'add', rule]
if content:
argv.append(content)
_fallback(argv)
return
if not content:
typer.echo(
"Rule content required. Usage: praisonai rules add <name> <content>",
err=True,
)
raise typer.Exit(1)
try:
manager.create_rule(
name=rule,
content=content,
description=f"Rule created via CLI: {rule}",
activation="always",
scope="workspace",
)
except Exception as exc: # noqa: BLE001 - surface a clean error, no traceback
typer.echo(f"Failed to add rule '{rule}': {exc}", err=True)
raise typer.Exit(1)
typer.echo(f"Rule added: {rule}")
def _workspace_rule_names(manager) -> list[str]:
"""Names of rules stored under the workspace ``.praison/rules/`` directory.
``rules add`` creates ``workspace``-scoped rules; the manager keys every
loaded rule as ``"{scope}:{name}"`` and also surfaces discovered project
files (``AGENTS.md``/``CLAUDE.md``/``PRAISON.md``), global rules, git-root
rules and nested subdir rules. Clearing must therefore be constrained to the
``workspace:`` scope so it only removes CLI-created rules and never unlinks
the user's hand-authored instruction files or global/nested rules.
Reads the manager's rule registry directly (public accessors do not expose
the scope key) and returns the names under the ``workspace:`` prefix,
de-duplicated in insertion order.
"""
names: list[str] = []
seen: set[str] = set()
for key in getattr(manager, "_rules", {}):
scope, _, name = key.partition(":")
if scope == "workspace" and name and name not in seen:
seen.add(name)
names.append(name)
return names
@app.command("clear")
def rules_clear():
"""Clear all rules."""
from praisonai_code._wrapper_bridge import run_wrapper_command
argv = ['rules', 'clear']
run_wrapper_command(argv, feature="rules")
"""Clear workspace rules created via the CLI.
Only ``workspace``-scoped rules (those created by ``rules add`` under
``.praison/rules/``) are removed. Discovered project files
(``AGENTS.md``/``CLAUDE.md``/``PRAISON.md``), global rules, git-root rules
and nested subdirectory rules are intentionally left untouched so this
command can never destroy hand-authored instructions outside its scope.
"""
manager = _rules_manager()
if manager is None:
_fallback(['rules', 'clear'])
return
try:
# Ensure the registry reflects on-disk state before enumerating.
names = _workspace_rule_names(manager)
except Exception as exc: # noqa: BLE001
typer.echo(f"Failed to load rules: {exc}", err=True)
raise typer.Exit(1)
removed = 0
for name in names:
try:
# Scope the delete to ``workspace`` so a same-named global/subdir
# rule is never removed as a side effect.
if manager.delete_rule(name, scope="workspace"):
removed += 1
except Exception: # noqa: BLE001 - skip undeletable rules, keep going
continue
typer.echo(f"Cleared {removed} rule(s).")
@@ -2284,8 +2284,10 @@ def _wire_subtree_context_hook(
``already_loaded`` so nested files are not duplicated.
No-op (leaving the default run unchanged) when the up-front rules are
disabled via ``--no-rules`` / ``PRAISON_NO_RULES`` or when the optional
helper is unavailable. Honours the existing ``PRAISON_CONTEXT_BUDGET``
disabled via ``--no-rules`` / ``PRAISON_NO_RULES``. The subtree-context
helper is now resident in ``praisonai_code`` so it works on a standalone
``pip install praisonai-code`` without the optional ``praisonai_bot`` /
``praisonai`` packages. Honours the existing ``PRAISON_CONTEXT_BUDGET``
character budget shared with the interactive path.
"""
import os
@@ -2300,15 +2302,18 @@ def _wire_subtree_context_hook(
try:
import importlib
# The subtree-context helper is optional. Its canonical home is the
# ``praisonai_bot`` support package; the ``praisonai`` wrapper only
# re-exports it via a compat shim. Prefer ``praisonai_bot`` so the hook
# is available on any install that ships it (matching ``chat``) without
# forcing the full wrapper, then fall back to the wrapper shim. Either
# way ``run.py`` avoids a module-level ``praisonai`` import (C7 gate).
# The subtree-context helper is now resident in ``praisonai_code`` so
# the standalone terminal product keeps first-class subtree instruction
# loading and ``--instructions`` resolution without the optional
# ``praisonai_bot`` support package or the ``praisonai`` wrapper (C7
# standalone contract). For backward compatibility we still prefer an
# installed ``praisonai_bot``/``praisonai`` copy when present (so a host
# that customised it keeps its behaviour), falling back to the resident
# implementation otherwise.
context_files = None
for _mod in ("praisonai_bot.integration.context_files",
"praisonai.integration.context_files"):
"praisonai.integration.context_files",
"praisonai_code.integration.context_files"):
try:
context_files = importlib.import_module(_mod)
break
@@ -796,9 +796,10 @@ def _render_share_html(session_id: str, transcript_md: str) -> str:
def session_share(
session_id: str = typer.Argument(..., help="Session ID to share"),
redact_level: str = typer.Option(
"standard",
"strict",
"--redact-level",
help="Redaction level: 'standard' or 'strict'.",
help="Redaction level: 'standard' or 'strict'. Defaults to 'strict' "
"because share publishes a transcript for wider distribution.",
),
):
"""Publish a redacted, read-only transcript and return a shareable link.
@@ -144,6 +144,7 @@ COMMAND_CATEGORIES: dict[str, str] = {
"plugins": CATEGORY_ADVANCED,
"sandbox": CATEGORY_ADVANCED,
"registry": CATEGORY_ADVANCED,
"backends": CATEGORY_ADVANCED,
"package": CATEGORY_ADVANCED,
"command": CATEGORY_ADVANCED,
"standardise": CATEGORY_ADVANCED,
@@ -453,6 +453,12 @@ class PraisonAI:
elif args.command.startswith("tests.test") or args.command.startswith("tests/test"): # Argument used for testing purposes
print("test")
return "test"
elif args.command == 'train':
# 'train' is a real command handled by the dedicated block below
# (see ``if args.command == 'train':``). Do not fall through to the
# bare-prompt handler, which would forward the literal word "train"
# to an LLM as a chat prompt and never launch the trainer.
pass
else:
# Handle --compare flag for CLI mode comparison
if hasattr(args, 'compare') and args.compare:
@@ -286,13 +286,21 @@ def apply_cli_session_continuity(agent, session_id: str, project_path: Optional[
history = store.get_chat_history(session_id) or []
if history:
existing = {(m.get("role"), m.get("content")) for m in agent.chat_history}
for msg in history:
entry = {"role": msg["role"], "content": msg["content"]}
key = (entry["role"], entry["content"])
if key not in existing:
agent.chat_history.append(entry)
existing.add(key)
# Restore by position, not by content. Two identical user turns (e.g. a
# bare "yes" confirmation repeated across steps) are two distinct turns;
# a content-based filter silently deletes legitimate repeats and hands
# the model a conversation that never happened (Issue #4243). Only skip
# restoration when the stored history is already present verbatim as a
# prefix of chat_history, so a double-injection does not duplicate it.
stored = [
{"role": msg["role"], "content": msg["content"]} for msg in history
]
current = [
{"role": m.get("role"), "content": m.get("content")}
for m in agent.chat_history[: len(stored)]
]
if current != stored:
agent.chat_history.extend(stored)
agent._auto_save_last_index = len(agent.chat_history)
# Persist model/agent so a later resume is deterministic regardless of the
@@ -17,11 +17,14 @@ Design (deliberately lightweight — stdlib only, no new dependencies):
* Order matters: process-registered secrets first (most sensitive), then any
detected key/token patterns, then absolute paths and the cwd, so a path that
is part of a secret is not half-masked.
* ``level="standard"`` redacts secrets and absolute paths in every string.
* ``level="standard"`` redacts secrets and absolute paths in every string. This
includes any ``key: value`` / ``key = value`` pair whose key *contains* a
secret word (``api_key``, ``secret``, ``token``, ``password``, ``access_key``),
so shapes like ``AWS_SECRET_ACCESS_KEY=…`` and ``STRIPE_SECRET_KEY=…`` are
caught, not only keys that end in one.
``level="strict"`` additionally masks values that look like credentials in a
broader set of shapes (bearer tokens, PEM private-key blocks) and treats any
``key: value`` / ``key = value`` pair whose key names a secret as sensitive —
trading a little readability for a stronger guarantee when sharing widely.
broader set of shapes (bearer tokens, PEM private-key blocks) — trading a
little readability for a stronger guarantee when sharing widely.
"""
from __future__ import annotations
@@ -38,13 +41,21 @@ REDACT_LEVELS: Tuple[str, ...] = ("standard", "strict")
# Token/secret shapes worth masking even when never registered as a resolved
# secret. Kept intentionally small and high-signal to avoid over-redaction.
_SECRET_PATTERNS: Tuple[re.Pattern, ...] = (
# Stripe live/test keys (sk_live_/sk_test_) and OpenAI-style sk- keys. The
# underscore form must precede the hyphen form so it is matched whole.
re.compile(r"sk_(?:live|test)_[A-Za-z0-9]{16,}"), # Stripe secret keys
re.compile(r"sk-[A-Za-z0-9_-]{16,}"), # OpenAI-style keys
re.compile(r"xox[baprs]-[A-Za-z0-9-]{10,}"), # Slack tokens
re.compile(r"gh[pousr]_[A-Za-z0-9]{20,}"), # GitHub tokens
re.compile(r"AKIA[0-9A-Z]{16}"), # AWS access key id
re.compile(r"AIza[0-9A-Za-z_-]{30,}"), # Google API key
re.compile(r"eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}"), # JWT
re.compile(r"(?i)(?:api[_-]?key|secret|token|password)\s*[:=]\s*['\"]?([A-Za-z0-9_\-]{12,})"),
# key=value / key: value where the key *contains* a secret word anywhere
# (e.g. AWS_SECRET_ACCESS_KEY, STRIPE_SECRET_KEY), not only ends in one.
re.compile(
r"(?i)[A-Za-z0-9_.\-]*(?:api[_-]?key|secret|token|password|access[_-]?key)"
r"[A-Za-z0-9_.\-]*\s*[:=]\s*['\"]?([^\s'\"]{8,})"
),
)
# Extra high-signal shapes only masked under ``strict`` — broader by design, so
@@ -0,0 +1 @@
"""Resident integration helpers for the standalone praisonai-code CLI."""
@@ -0,0 +1,707 @@
"""AGENTS.md-style context file injection, resident in ``praisonai-code``.
This is the dependency-free home of the subtree-instruction helper so the
terminal-native CLI (``pip install praisonai-code``) can load project
instructions and resolve ``--instructions`` sources without importing the
optional ``praisonai_bot`` support package or the umbrella ``praisonai``
wrapper (C7 standalone contract).
It relies only on the standard library plus ``praisonaiagents.hooks`` (already a
core dependency of ``praisonai-code``). The ``praisonai_bot`` copy remains a
backward-compatible mirror for host apps that import it directly.
"""
from __future__ import annotations
import glob as _glob
import logging
import os
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Set
logger = logging.getLogger(__name__)
DEFAULT_CANDIDATES = ["AGENTS.md", "agents.md", ".agents/AGENTS.md", "CLAUDE.md"]
# Upper bound on the bytes read from a single remote instruction source. Keeps a
# stray large URL from ballooning the system context; oversized bodies are
# truncated with a marker rather than failing the run.
_REMOTE_MAX_BYTES = 256 * 1024
# Timeout (seconds) for a single remote instruction fetch. Best-effort: a slow
# or unreachable URL is skipped with a warning, never blocking the run.
_REMOTE_TIMEOUT = 5.0
# Opt-in to allow fetching instruction URLs that resolve to private, loopback,
# or link-local addresses. Off by default so an auto-loaded project config from
# an untrusted checkout cannot make the host contact internal services (SSRF).
_ALLOW_LOCAL_URL_ENV = "PRAISONAI_INSTRUCTIONS_ALLOW_LOCAL_URLS"
# Tool-argument keys that carry a file path across the various file tools
# (praisonaiagents ``read_file`` uses ``filepath``; praisonai-code tools use
# ``path``/``file_path``; ACP edit tools use ``file_path``). The first present,
# string-valued key wins.
_PATH_ARG_KEYS = ("filepath", "file_path", "path", "filename", "target_file")
def _get_git_root(start: Path) -> Optional[Path]:
"""Resolve the git/project root using the resident CLI resolver.
Falls back to None when the helper or git is unavailable so discovery
simply walks up to the filesystem root.
"""
try:
from praisonai_code.cli.utils import project
return project.get_git_root(str(start))
except (ImportError, OSError):
return None
# When no git root is found, cap the walk-up to avoid scanning arbitrary
# system directories up to the filesystem root.
_MAX_WALK_UP_DEPTH = 10
def _discover_search_dirs(base: Path, walk_up: bool) -> List[Path]:
"""Build directories to search, ordered root -> cwd (nearest last).
Walks up from ``base`` to the git root so nearer, more specific
instruction files take precedence by appearing last. When no git root
is found, the walk-up is capped at ``_MAX_WALK_UP_DEPTH`` levels to avoid
scanning arbitrary system directories.
"""
base = base.resolve()
if not walk_up:
return [base]
git_root = _get_git_root(base)
if git_root is not None:
git_root = git_root.resolve()
dirs: List[Path] = []
current = base
depth = 0
while True:
dirs.append(current)
if git_root and current == git_root:
break
if current == current.parent:
break
if git_root is None and depth >= _MAX_WALK_UP_DEPTH:
break
current = current.parent
depth += 1
# Reverse so root is first and cwd (nearest) is last.
dirs.reverse()
return dirs
def load_context_files(
paths: Optional[List[str]] = None,
*,
cwd: Optional[Path] = None,
walk_up: bool = True,
) -> str:
"""Load context from AGENTS.md-style files and return combined text.
Discovery mirrors the configuration resolver: it walks up from ``cwd``
to the project boundary (git root) collecting instruction files at each
level, layers a user-global file (``~/.praisonai/AGENTS.md``) as the
lowest-precedence source, and concatenates root -> cwd so nearer files
take precedence (appear last).
Args:
paths: Explicit relative file names to load from ``cwd`` only. When
provided, discovery/walk-up is skipped (backward compatible).
cwd: Directory to start discovery from (defaults to ``Path.cwd()``).
walk_up: When True (default), walk up to the git/project root and
layer files. Ignored when ``paths`` is given.
Returns:
Combined instruction text, blank-line separated.
"""
base = cwd or Path.cwd()
seen: set = set()
chunks: List[str] = []
def _add(path: Path) -> None:
if not path.is_file():
return
# De-duplicate by filesystem identity so the same physical file is
# read once even via different paths or on case-insensitive volumes.
try:
stat = path.stat()
key = (stat.st_dev, stat.st_ino)
except OSError:
key = path.resolve()
if key in seen:
return
try:
text = path.read_text(encoding="utf-8")
except OSError:
return
seen.add(key)
chunks.append(text)
# Explicit paths override discovery and remain cwd-only for compatibility.
if paths is not None:
for name in paths:
_add(base / name)
return "\n\n".join(chunks)
# Lowest-precedence layer: user-global instructions
# (skipped when walk_up is False to honour cwd-only semantics).
if walk_up:
_add(Path.home() / ".praisonai" / "AGENTS.md")
# Walk-up layers: root -> cwd so nearer files take precedence (last).
for search_dir in _discover_search_dirs(base, walk_up):
for name in DEFAULT_CANDIDATES:
_add(search_dir / name)
return "\n\n".join(chunks)
def _is_remote_source(entry: str) -> bool:
"""Whether an instruction entry is an ``http(s)://`` URL."""
return entry.startswith(("http://", "https://"))
def _allow_local_urls() -> bool:
"""Whether fetching URLs that resolve to internal addresses is opted in."""
return os.environ.get(_ALLOW_LOCAL_URL_ENV, "").strip().lower() in (
"1",
"true",
"yes",
)
def _is_blocked_ip(addr: str) -> bool:
"""Whether a resolved address string is in a non-global (blocked) range."""
import ipaddress
try:
ip = ipaddress.ip_address(addr.split("%", 1)[0])
except ValueError:
return True
return (
ip.is_private
or ip.is_loopback
or ip.is_link_local
or ip.is_reserved
or ip.is_multicast
or ip.is_unspecified
)
def _resolve_public_addrs(host: str) -> Optional[List[str]]:
"""Resolve ``host`` and return its addresses only if *all* are global.
Returns the list of resolved address strings when every resolution is a
public/global address, ``None`` when the host is unresolvable (let urlopen
surface the failure downstream), or an empty list when any resolved address
is in a blocked (private/loopback/link-local/reserved) range so the caller
fails closed. Returning the concrete addresses lets the fetch *pin* the
connection to a validated IP, closing the DNS-rebinding (TOCTOU) window
where a re-resolution at connect time could return an internal address.
"""
import socket
if not host:
return []
try:
infos = socket.getaddrinfo(host, None)
except OSError:
return None
addrs: List[str] = []
for info in infos:
addr = info[4][0]
if _is_blocked_ip(addr):
return []
addrs.append(addr)
return addrs
def _is_blocked_host(host: str) -> bool:
"""Whether ``host`` resolves to a private/loopback/link-local/reserved IP.
SSRF guard: an instruction URL can arrive from an auto-loaded project config
in an untrusted checkout, so a URL that resolves to an internal service must
not be fetched. Any resolved address in a non-global range blocks the fetch
(fail-closed on resolution errors). Bypassable via ``_ALLOW_LOCAL_URL_ENV``
for trusted internal setups.
"""
addrs = _resolve_public_addrs(host)
if addrs is None:
# Unresolvable — let urlopen surface the failure/warning downstream.
return False
return len(addrs) == 0
def _url_is_fetchable(url: str) -> bool:
"""Validate scheme and destination of a remote instruction URL (SSRF guard)."""
from urllib.parse import urlparse
if _allow_local_urls():
return True
parsed = urlparse(url)
if parsed.scheme not in ("http", "https"):
return False
if _is_blocked_host(parsed.hostname or ""):
logger.warning(
"Skipping remote instruction source %s: resolves to a "
"private/loopback/link-local address (set %s=1 to allow)",
url,
_ALLOW_LOCAL_URL_ENV,
)
return False
return True
class _PinnedSSRFError(Exception):
"""Raised at connect time when a host re-resolves to a blocked address."""
def _build_pinned_opener():
"""Build a urllib opener that pins each connection to a validated IP.
Closes the DNS-rebinding (TOCTOU) window: the up-front
:func:`_url_is_fetchable` check resolves the host once, but a plain
``urlopen`` re-resolves at connect time, so an attacker-controlled hostname
can rebind to an internal address between the two resolutions. Here each
``HTTP(S)Connection`` re-resolves *and re-validates* the host at connect
time and connects to that exact validated IP, so the address checked is the
address contacted. The original hostname is preserved for the ``Host``
header and TLS SNI so virtual-hosted and HTTPS endpoints still work.
Redirects are additionally re-validated by a guarded redirect handler, and
the ``_allow_local_urls`` opt-in bypasses pinning entirely for trusted
internal setups.
"""
import http.client
import socket
import urllib.request
def _pinned_connect(conn):
# Re-validate at the moment of connection so a rebind since the
# up-front check is caught, then connect to the validated address.
addrs = _resolve_public_addrs(conn.host)
if not addrs: # empty (blocked) or None (unresolvable) -> refuse
raise _PinnedSSRFError(
f"host {conn.host!r} did not resolve to a public address"
)
last_err: Optional[Exception] = None
for addr in addrs:
try:
conn.sock = socket.create_connection(
(addr, conn.port), timeout=conn.timeout
)
if getattr(conn, "_tunnel_host", None):
conn._tunnel()
return
except OSError as exc: # try the next validated address
last_err = exc
raise last_err or OSError(f"could not connect to {conn.host!r}")
class _PinnedHTTPConnection(http.client.HTTPConnection):
def connect(self):
_pinned_connect(self)
class _PinnedHTTPSConnection(http.client.HTTPSConnection):
def connect(self):
_pinned_connect(self)
# Wrap the raw socket with TLS using the original hostname for SNI
# and certificate verification (not the pinned IP).
self.sock = self._context.wrap_socket(
self.sock, server_hostname=self.host
)
class _PinnedHTTPHandler(urllib.request.HTTPHandler):
def http_open(self, req):
return self.do_open(_PinnedHTTPConnection, req)
class _PinnedHTTPSHandler(urllib.request.HTTPSHandler):
def https_open(self, req):
return self.do_open(_PinnedHTTPSConnection, req)
class _GuardedRedirectHandler(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
if not _url_is_fetchable(newurl):
return None
return super().redirect_request(req, fp, code, msg, headers, newurl)
return urllib.request.build_opener(
_PinnedHTTPHandler(),
_PinnedHTTPSHandler(),
_GuardedRedirectHandler(),
)
def _fetch_remote_source(url: str) -> Optional[str]:
"""Fetch a remote instruction source, best-effort and size-bounded.
Uses the stdlib ``urllib`` (lazily imported) so no heavy dependency is
added. Destinations are validated against an SSRF guard (see
:func:`_url_is_fetchable`) that rejects private/loopback/link-local hosts,
the connection is *pinned* to the validated IP (see
:func:`_build_pinned_opener`) so a DNS rebind cannot redirect it to an
internal service, and redirects are re-validated so a public URL cannot
bounce to an internal one. A slow, unreachable, or oversized response is
handled gracefully: failures return ``None`` (with a warning) so the run
continues, and bodies larger than ``_REMOTE_MAX_BYTES`` are truncated with a
marker.
"""
if not _url_is_fetchable(url):
return None
try:
if _allow_local_urls():
# Trusted internal setup: skip IP pinning so a legitimately private
# instruction host is reachable.
import urllib.request
opener = urllib.request.build_opener()
else:
opener = _build_pinned_opener()
with opener.open(url, timeout=_REMOTE_TIMEOUT) as resp: # nosec B310
raw = resp.read(_REMOTE_MAX_BYTES + 1)
except Exception as exc: # noqa: BLE001 - best-effort; never block the run
logger.warning("Skipping remote instruction source %s: %s", url, exc)
return None
truncated = len(raw) > _REMOTE_MAX_BYTES
if truncated:
raw = raw[:_REMOTE_MAX_BYTES]
try:
text = raw.decode("utf-8", errors="replace")
except Exception: # noqa: BLE001 - decode is defensive
return None
if truncated:
text += "\n... [remote instruction source truncated]"
return text
def resolve_instruction_sources(
entries: Optional[List[str]] = None,
*,
cwd: Optional[Path] = None,
) -> str:
"""Resolve config-declared instruction sources into combined text.
Each entry may be:
* a plain file path (``docs/rules.md``),
* a glob (``docs/standards/*.md``),
* a ``~``-prefixed path (``~/company/ai-rules.md``), or
* a remote ``http(s)://`` URL.
Entries are resolved in order and their contents concatenated (blank-line
separated), so callers can layer org-wide sources before project-specific
ones. Local globs expand to their matches sorted for determinism; a
``~``/env-var path is expanded; remote URLs are fetched lazily, best-effort
and size-bounded (see :func:`_fetch_remote_source`). Missing local paths and
failed fetches are skipped with a warning rather than aborting the run.
Args:
entries: Ordered list of source specifiers. ``None``/empty returns "".
cwd: Base directory for resolving relative paths (defaults to CWD).
Returns:
Combined instruction text, blank-line separated (possibly empty).
"""
if not entries:
return ""
base = Path(cwd) if cwd else Path.cwd()
seen: Set = set()
chunks: List[str] = []
def _add_file(path: Path) -> None:
if not path.is_file():
return
try:
stat = path.stat()
key = (stat.st_dev, stat.st_ino)
except OSError:
key = path.resolve()
if key in seen:
return
try:
text = path.read_text(encoding="utf-8")
except OSError as exc:
logger.warning("Skipping instruction source %s: %s", path, exc)
return
seen.add(key)
chunks.append(text)
for entry in entries:
if not isinstance(entry, str) or not entry.strip():
continue
entry = entry.strip()
if _is_remote_source(entry):
if entry in seen:
continue
text = _fetch_remote_source(entry)
if text is not None:
seen.add(entry)
chunks.append(text)
continue
expanded = os.path.expanduser(os.path.expandvars(entry))
candidate = Path(expanded)
if not candidate.is_absolute():
candidate = base / candidate
# Expand globs (including brace-free ``*``/``?``/``[]`` patterns). When
# the entry contains no glob metacharacter this yields the single path.
pattern = str(candidate)
if any(ch in pattern for ch in "*?["):
for match in sorted(_glob.glob(pattern, recursive=True)):
_add_file(Path(match))
else:
if candidate.is_file():
_add_file(candidate)
else:
logger.warning("Instruction source not found: %s", entry)
return "\n\n".join(chunks)
def _identity_key(path: Path):
"""Filesystem identity for dedup (device+inode), falling back to resolved path."""
try:
stat = path.stat()
return (stat.st_dev, stat.st_ino)
except OSError:
return path.resolve()
class PathContextAttacher:
"""Attach the nearest governing instruction files as files are touched.
Up-front ``load_context_files`` discovery walks ``cwd -> project root`` once
at session start. In a monorepo, an agent that later reads/edits a file in a
*sibling or deeper* subtree (e.g. ``packages/foo/AGENTS.md``) never sees that
subtree's conventions. This attacher augments the up-front load: on the first
touch of a directory it walks up to the project root collecting
``DEFAULT_CANDIDATES``, deduplicates against files already loaded (both the
up-front rules and earlier touches), bounds the result by a character budget,
and caches per directory so repeated touches never re-walk disk.
Session-scoped: create one instance per session/agent run so cache and dedup
state stay isolated (multi-agent safe).
"""
def __init__(
self,
already_loaded: Optional[str] = None,
*,
max_chars: int = 8000,
) -> None:
"""Initialise the attacher.
Args:
already_loaded: Text already injected up front (used only to seed
dedup so its files are not re-attached). Its identity is tracked
by content so duplicate physical files are skipped.
max_chars: Character budget for the total text this attacher emits
across the session. ``0`` disables the budget.
"""
self._seen: Set = set()
self._seen_texts: Set[str] = set()
self._dir_cache: Dict[Path, str] = {}
self._max_chars = max_chars
self._emitted_chars = 0
# Seed dedup with the identities of already-loaded files so the same
# physical instruction file discovered up front is not re-attached.
if already_loaded:
self._seed_from_text(already_loaded)
def _seed_from_text(self, text: str) -> None:
# We cannot recover file identities from text alone, so we record the
# text content to skip re-emitting identical bodies. Physical-identity
# dedup below still handles the same file reached via different paths.
#
# ``load_context_files`` joins multiple instruction files with "\n\n",
# so seed the whole blob *and* each chunk. This lets an individual file
# discovered later match even when it was originally injected inside a
# larger concatenated up-front load.
self._seen_texts.add(text)
for chunk in text.split("\n\n"):
if chunk:
self._seen_texts.add(chunk)
def attach_for_path(self, file_path) -> str:
"""Return nearest instruction text for ``file_path``'s directory.
Walks up from the file's directory to the project root collecting
instruction files (root first, nearest last), deduplicated against
everything already emitted/loaded and bounded by the char budget. The
first touch of a directory does the disk walk; later touches of the same
directory return the cached (already-deduped) result cheaply.
Returns an empty string when nothing new is found or the budget is
exhausted.
"""
p = Path(file_path)
directory = (p if p.is_dir() else p.parent).resolve()
if directory in self._dir_cache:
return self._dir_cache[directory]
# Collect newly-discovered files without mutating dedup state yet: if
# the char budget discards this result we must not permanently lock the
# files out of future touches (they were never actually emitted).
chunks: List[str] = []
new_keys: List = []
local_seen_texts: Set[str] = set()
for search_dir in _discover_search_dirs(directory, walk_up=True):
for name in DEFAULT_CANDIDATES:
candidate = search_dir / name
if not candidate.is_file():
continue
key = _identity_key(candidate)
if key in self._seen:
continue
try:
text = candidate.read_text(encoding="utf-8")
except OSError:
continue
if text in self._seen_texts or text in local_seen_texts:
self._seen.add(key)
continue
local_seen_texts.add(text)
new_keys.append(key)
chunks.append(text)
result = "\n\n".join(chunks)
# Enforce the character budget across the whole session, keeping the
# truncation marker inside the remaining budget so max_chars holds.
if self._max_chars and result:
remaining = self._max_chars - self._emitted_chars
if remaining <= 0:
result = ""
elif len(result) > remaining:
suffix = "\n... [subtree context truncated]"
if remaining > len(suffix):
result = result[: remaining - len(suffix)] + suffix
else:
result = result[:remaining]
# Only commit dedup state when something was actually emitted, so a
# budget-suppressed discovery can still surface if budget frees up.
if result:
for key in new_keys:
self._seen.add(key)
self._seen_texts |= local_seen_texts
self._emitted_chars += len(result)
self._dir_cache[directory] = result
return result
def load_context_files_for_path(
file_path,
*,
already_loaded: Optional[str] = None,
max_chars: int = 8000,
) -> str:
"""Stateless one-shot discovery of nearest instruction files for a path.
Convenience wrapper around :class:`PathContextAttacher` for callers that do
not maintain session state. Prefer the class when touching many files so
dedup and per-directory caching persist across touches.
"""
return PathContextAttacher(
already_loaded, max_chars=max_chars
).attach_for_path(file_path)
# Tool names that operate on a file and should trigger subtree-rule discovery.
# Covers the real interactive tool surface (praisonaiagents ``read_file``/
# ``write_file`` and the ACP ``acp_create_file``/``acp_edit_file``/
# ``acp_delete_file`` tools) plus common generic aliases so custom file tools
# are matched too.
_FILE_TOOL_MATCHER = (
r"^(read_file|write_file|edit_file|list_files"
r"|acp_create_file|acp_edit_file|acp_delete_file"
r"|read|edit|write|apply_patch|str_replace|multi_edit|search_replace)$"
)
def _extract_tool_path(tool_input: Any) -> Optional[str]:
"""Pull the target file path out of a tool's input arguments.
Returns the first present, non-empty string value among ``_PATH_ARG_KEYS``
so it works across the differing file-tool argument conventions.
"""
if not isinstance(tool_input, dict):
return None
for key in _PATH_ARG_KEYS:
value = tool_input.get(key)
if isinstance(value, str) and value:
return value
return None
def build_subtree_context_hook(
already_loaded: Optional[str] = None,
*,
max_chars: int = 8000,
) -> Callable[[Any], Any]:
"""Build an ``AFTER_TOOL`` hook that lazily attaches subtree instructions.
Wires the :class:`PathContextAttacher` into the neutral core hook
substrate: on each file-touching tool call (``read_file``/``edit_file``/
``write_file`` and friends) it resolves the nearest instruction file for the
touched directory and, if not already loaded/emitted this session, returns it
as ``additional_context`` so the agent sees locally-relevant rules the first
time it works in that subtree.
The returned callable is session-scoped (its attacher owns the dedup and
per-directory cache), so create one hook per session/agent run. Register it
on an agent's hook registry for ``HookEvent.AFTER_TOOL`` (optionally with
``matcher=file_tool_matcher()`` to skip non-file tools cheaply).
Args:
already_loaded: Up-front instruction text (used only to seed dedup so
files already injected at session start are not re-attached).
max_chars: Character budget for the total text emitted across the
session. ``0`` disables the budget.
Returns:
A function ``hook(hook_input) -> HookResult | None`` suitable for
``HookRegistry.register_function``. Returns ``None`` (no-op) when the
tool is not a file tool or nothing new is discovered.
"""
attacher = PathContextAttacher(already_loaded, max_chars=max_chars)
def _hook(hook_input: Any):
tool_input = getattr(hook_input, "tool_input", None)
path = _extract_tool_path(tool_input)
if not path:
return None
try:
context = attacher.attach_for_path(path)
except OSError:
return None
if not context:
return None
try:
from praisonaiagents.hooks import HookResult
except ImportError:
return None
return HookResult(decision="allow", additional_context=context)
_hook.__name__ = "subtree_instruction_injection"
return _hook
def file_tool_matcher() -> str:
"""Regex matching file-touching tool names for the subtree-context hook."""
return _FILE_TOOL_MATCHER
@@ -0,0 +1,24 @@
"""Routing parity guard for the CLI command registry.
Regression test for the ``search`` drift (issue #4326) and the broader #2191
guarantee that routing can never drift from the advertised command set: every
command surfaced in ``--help`` (``LazyCommandGroup.list_commands``) must also be
recognised by the routing oracle (``get_command_names``), otherwise a real,
registered command is misclassified as a bare prompt and billed as an LLM call.
"""
from praisonai_code.cli import app as app_mod
def test_advertised_commands_are_all_routed():
group = app_mod.LazyCommandGroup()
advertised = set(group.list_commands(None))
routed = app_mod.get_command_names()
missing = advertised - routed
assert not missing, f"advertised but not routed: {sorted(missing)}"
def test_search_command_is_routed():
group = app_mod.LazyCommandGroup()
assert "search" in group.list_commands(None)
assert "search" in app_mod.get_command_names()
@@ -83,6 +83,26 @@ def test_get_command_tags_help_panel():
assert cmd.rich_help_panel == expected
def test_resolved_commands_keep_their_typeable_name():
"""Every resolvable command must render under the name a user can type.
Regression for issue #4323: ``typer_get_command`` yields ``""`` for a
``typer.Typer(help=…)`` declared without ``name=`` and collapses a
single-command app to that command's own name, so the ``--help`` panel
printed blank or wrong names. ``get_command`` must pin ``command.name`` to
the registry key.
"""
ctx = _root_context()
root = ctx.command
for name in root.list_commands(ctx):
cmd = root.get_command(ctx, name)
if cmd is None:
continue
assert cmd.name == name, (
f"command {name!r} resolved with mismatched name {cmd.name!r}"
)
def test_help_output_is_grouped_not_flat():
"""The rendered help groups commands into categorised panels."""
result = _invoke_help()
@@ -0,0 +1,87 @@
"""Regression tests for `run --continue` history restoration (Issue #4243).
`apply_cli_session_continuity` restored history using a content-based filter, so
repeated turns (the common case being a bare "yes" confirmation) were silently
dropped and the model resumed onto a conversation that never happened. History
must be restored by position: two identical turns are two turns.
"""
import pytest
@pytest.fixture
def project(tmp_path, monkeypatch):
"""Isolated project dir + sessions home under the test sandbox."""
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("PRAISONAI_HOME", str(tmp_path / ".praison_home"))
import praisonaiagents.paths as _paths
sessions_dir = tmp_path / "sessions"
def _fake_get_sessions_dir():
sessions_dir.mkdir(parents=True, exist_ok=True)
return sessions_dir
monkeypatch.setattr(_paths, "get_sessions_dir", _fake_get_sessions_dir)
monkeypatch.setattr(
"praisonaiagents.session.store.get_sessions_dir", _fake_get_sessions_dir
)
return tmp_path
class _FakeAgent:
def __init__(self):
self.chat_history = []
self.name = "tester"
self.llm = "gpt-4o-mini"
def _store(session_id, contents):
from praisonai_code.cli.state.project_sessions import get_project_session_store
store = get_project_session_store()
for i, content in enumerate(contents):
role = "user" if i % 2 == 0 else "assistant"
store.add_message(session_id, role, content)
return store
def test_repeated_confirmations_survive_a_continue(project):
"""'yes' three times is three turns; collapsing them corrupts the history."""
from praisonai_code.cli.state.project_sessions import apply_cli_session_continuity
_store(
"sess-yes",
["run the tests", "ok", "yes", "done 1", "yes", "done 2", "yes", "done 3"],
)
agent = _FakeAgent()
apply_cli_session_continuity(agent, "sess-yes")
assert len(agent.chat_history) == 8
assert [m["content"] for m in agent.chat_history].count("yes") == 3
def test_auto_save_index_matches_full_restored_length(project):
"""The auto-save offset must equal the full restored length, not a deduped one."""
from praisonai_code.cli.state.project_sessions import apply_cli_session_continuity
_store("sess-idx", ["a", "b", "a", "b"])
agent = _FakeAgent()
apply_cli_session_continuity(agent, "sess-idx")
assert agent._auto_save_last_index == len(agent.chat_history) == 4
def test_no_duplicate_when_history_already_present(project):
"""If chat_history already holds the stored history verbatim, do not re-add it."""
from praisonai_code.cli.state.project_sessions import apply_cli_session_continuity
_store("sess-dup", ["x", "y"])
agent = _FakeAgent()
agent.chat_history = [
{"role": "user", "content": "x"},
{"role": "assistant", "content": "y"},
]
apply_cli_session_continuity(agent, "sess-dup")
assert len(agent.chat_history) == 2
@@ -120,6 +120,56 @@ def test_strict_masks_bearer_and_pem_but_standard_does_not():
assert "[redacted:secret:" in strict
def test_stripe_live_and_test_keys_are_redacted():
"""sk_live_/sk_test_ shapes must be masked, not only sk- (Issue #4243)."""
# Build the credential shapes at runtime so no scannable secret literal
# lives in source (keeps secret scanners quiet) while still exercising the
# redaction regex with a matching string.
live_key = "sk_" + "live_" + "51H8xQ2KZvB9nLmPqRsTuVwXy"
test_key = "sk_" + "test_" + "ABCDEF0123456789ABCDEF"
payload = {
"chat_history": [
{"role": "user", "content": f"key {live_key} here"},
{"role": "user", "content": f"and {test_key} too"},
]
}
dumped = json.dumps(redact_transcript(payload))
assert live_key not in dumped
assert test_key not in dumped
assert "[redacted:secret:" in dumped
def test_secret_named_keys_are_redacted_when_key_contains_secret_word():
"""key=value where the key *contains* a secret word (not only ends in one)
must be masked — AWS_SECRET_ACCESS_KEY / STRIPE_SECRET_KEY (Issue #4243)."""
# Assemble fixture values at runtime so no literal secret is committed.
aws_value = "wJalrXUtnFEMI" + "/" + "K7MDENGbPxRfiCY"
stripe_value = "sk_" + "live_" + "51H8xQ2KZvB9nLmPqRsTuVwXy"
payload = {
"chat_history": [
{"role": "user", "content": f"AWS_SECRET_ACCESS_KEY={aws_value}"},
{"role": "user", "content": f"STRIPE_SECRET_KEY={stripe_value}"},
]
}
dumped = json.dumps(redact_transcript(payload))
assert "wJalrXUtnFEMI" not in dumped
assert stripe_value not in dumped
assert "[redacted:secret:" in dumped
def test_aws_secret_value_is_not_half_masked_by_path():
"""A secret value containing '/' must be masked whole, not leave a visible
prefix with the tail eaten by the path regex (Issue #4243)."""
aws_value = "wJalrXUtnFEMI" + "/" + "K7MDENGbPxRfiCY"
payload = {
"chat_history": [
{"role": "user", "content": f"AWS_SECRET_ACCESS_KEY={aws_value}"}
]
}
dumped = json.dumps(redact_transcript(payload, level="strict"))
assert "wJalrXUtnFEMI" not in dumped
def test_invalid_level_raises():
with pytest.raises(ValueError):
redact_transcript(_fixture_payload(), level="bogus")
@@ -0,0 +1,246 @@
"""Standalone regression tests for project instructions & rules (issue #4337).
On a lean ``pip install praisonai-code`` (no ``praisonai`` wrapper, no
``praisonai_bot`` support package) the terminal product must still:
* attach subtree ``AGENTS.md`` on cross-directory file reads,
* resolve ``--instructions`` globs/URLs up front, and
* run ``praisonai rules list/add/clear`` natively.
These tests simulate the absence of the optional packages by blocking their
import so the resident ``praisonai_code`` implementation is exercised.
"""
from types import SimpleNamespace
import pytest
# Import the command modules up front (with the optional packages available) so
# the ``no_optional_packages`` fixture only affects the *runtime* helper lookup
# inside ``_wire_subtree_context_hook`` / the rules handler, not their import.
from praisonai_code.cli.commands.run import _wire_subtree_context_hook
from praisonai_code.cli.commands import rules as rules_cmd
from praisonai_code.integration import context_files as _resident_ctx # noqa: F401
@pytest.fixture
def no_optional_packages(monkeypatch):
"""Make ``praisonai_bot`` and the ``praisonai`` wrapper un-importable.
Simulates a standalone ``praisonai-code`` install so the resident helper
(``praisonai_code.integration.context_files``) is the only one available at
runtime. ``praisonai_code`` itself is a distinct top-level package and is
unaffected.
"""
import importlib
import sys
for name in list(sys.modules):
if name == "praisonai_bot" or name.startswith("praisonai_bot."):
monkeypatch.delitem(sys.modules, name, raising=False)
if name == "praisonai" or name.startswith("praisonai."):
monkeypatch.delitem(sys.modules, name, raising=False)
real_import_module = importlib.import_module
def _blocked_import_module(name, *args, **kwargs):
if name == "praisonai_bot" or name.startswith("praisonai_bot."):
raise ImportError(f"blocked in standalone test: {name}")
if name == "praisonai" or name.startswith("praisonai."):
raise ImportError(f"blocked in standalone test: {name}")
return real_import_module(name, *args, **kwargs)
monkeypatch.setattr(importlib, "import_module", _blocked_import_module)
@pytest.fixture
def monorepo(tmp_path, monkeypatch):
"""A monorepo with a root and a nested package, with a fake git root."""
root = tmp_path / "repo"
foo = root / "packages" / "foo"
foo.mkdir(parents=True)
(root / "AGENTS.md").write_text("ROOT RULES")
(foo / "AGENTS.md").write_text("FOO RULES")
# Force deterministic git-root detection on the *resident* helper.
monkeypatch.setattr(
"praisonai_code.integration.context_files._get_git_root",
lambda start: root,
)
return root, foo
def _fire_after_tool(registry, tool_input):
from praisonaiagents.hooks import HookEvent
event = SimpleNamespace(tool_input=tool_input)
contexts = []
for hook in registry.get_hooks(HookEvent.AFTER_TOOL):
func = getattr(hook, "func", hook)
result = func(event)
if result is not None and getattr(result, "additional_context", None):
contexts.append(result.additional_context)
return "\n".join(contexts)
def test_subtree_hook_wired_without_optional_packages(
monorepo, no_optional_packages
):
"""Subtree AGENTS.md attaches on cross-dir reads with no wrapper/bot."""
_root, foo = monorepo
agent_config = {"name": "RunAgent"}
_wire_subtree_context_hook(agent_config)
registry = agent_config.get("hooks")
assert registry is not None, "resident subtree hook must register standalone"
context = _fire_after_tool(registry, {"file_path": str(foo / "bar.py")})
assert "FOO RULES" in context
def test_instruction_sources_resolved_without_optional_packages(
tmp_path, monkeypatch, no_optional_packages
):
"""``--instructions`` globs resolve natively on a standalone install."""
monkeypatch.setattr(
"praisonai_code.integration.context_files._get_git_root",
lambda start: tmp_path,
)
(tmp_path / "a.md").write_text("ORG STANDARD A")
(tmp_path / "b.md").write_text("ORG STANDARD B")
agent_config = {"name": "RunAgent"}
_wire_subtree_context_hook(
agent_config, instructions=[str(tmp_path / "*.md")]
)
backstory = agent_config.get("backstory") or ""
assert "ORG STANDARD A" in backstory
assert "ORG STANDARD B" in backstory
assert "# Project Instructions" in backstory
def test_resident_context_files_is_dependency_free(no_optional_packages):
"""The resident helper imports and exposes the expected API standalone."""
from praisonai_code.integration import context_files
assert callable(context_files.resolve_instruction_sources)
assert callable(context_files.build_subtree_context_hook)
assert callable(context_files.file_tool_matcher)
# --- Native rules command --------------------------------------------------
def test_rules_list_native_without_wrapper(tmp_path, monkeypatch, capsys):
"""``rules list`` uses core RulesManager and never touches the wrapper."""
(tmp_path / "AGENTS.md").write_text("be concise")
monkeypatch.chdir(tmp_path)
def _boom(*_a, **_k):
raise AssertionError("wrapper fallback must not be used standalone")
monkeypatch.setattr(rules_cmd, "_fallback", _boom)
rules_cmd.rules_list()
out = capsys.readouterr().out
# AGENTS.md is discovered as a root instruction rule.
assert "AGENTS" in out or "agents" in out.lower()
def test_rules_add_then_list_native(tmp_path, monkeypatch, capsys):
"""``rules add`` creates a workspace rule surfaced by ``rules list``."""
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(
rules_cmd,
"_fallback",
lambda *_a, **_k: (_ for _ in ()).throw(AssertionError("no fallback")),
)
rules_cmd.rules_add("style", "use tabs")
add_out = capsys.readouterr().out
assert "style" in add_out
rules_cmd.rules_list()
list_out = capsys.readouterr().out
assert "style" in list_out
def test_rules_clear_native(tmp_path, monkeypatch, capsys):
"""``rules clear`` removes CLI-created rules."""
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(
rules_cmd,
"_fallback",
lambda *_a, **_k: (_ for _ in ()).throw(AssertionError("no fallback")),
)
rules_cmd.rules_add("style", "use tabs")
capsys.readouterr()
rules_cmd.rules_clear()
clear_out = capsys.readouterr().out
assert "Cleared" in clear_out
def test_rules_clear_preserves_project_instruction_files(
tmp_path, monkeypatch, capsys
):
"""``rules clear`` must NOT delete hand-authored AGENTS.md/CLAUDE.md.
Only ``workspace``-scoped rules created via ``rules add`` should be removed;
discovered project instruction files and global/nested rules are left
untouched (regression for the destructive over-clear bug).
"""
agents = tmp_path / "AGENTS.md"
claude = tmp_path / "CLAUDE.md"
agents.write_text("be concise")
claude.write_text("prefer stdlib")
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(
rules_cmd,
"_fallback",
lambda *_a, **_k: (_ for _ in ()).throw(AssertionError("no fallback")),
)
rules_cmd.rules_add("style", "use tabs")
capsys.readouterr()
rules_cmd.rules_clear()
# The CLI-created workspace rule is gone, but the user's instruction files
# must survive.
assert agents.exists(), "rules clear deleted a hand-authored AGENTS.md"
assert claude.exists(), "rules clear deleted a hand-authored CLAUDE.md"
def test_remote_fetch_pins_to_validated_ip(monkeypatch):
"""The pinned opener re-validates at connect time (DNS-rebinding guard).
Simulate a rebind: the up-front check sees a public address, but the
connect-time resolution returns a loopback address. The pinned connection
must refuse rather than contact the internal address.
"""
from praisonai_code.integration import context_files as ctx
calls = {"n": 0}
def _rebinding_resolver(host):
# First resolution (up-front validation) is public; a later resolution
# (connect time) rebinds to loopback -> must be refused.
calls["n"] += 1
if calls["n"] == 1:
return ["93.184.216.34"]
return []
monkeypatch.setattr(ctx, "_resolve_public_addrs", _rebinding_resolver)
monkeypatch.setattr(ctx, "_allow_local_urls", lambda: False)
# Up-front guard passes on the first (public) resolution.
assert ctx._url_is_fetchable("http://rebind.example/instructions.md")
# But the pinned connect re-resolves and refuses the rebound address.
result = ctx._fetch_remote_source(
"http://rebind.example/instructions.md"
)
assert result is None
@@ -0,0 +1,84 @@
"""Regression test for issue #4314: ``train`` must reach the trainer, not chat.
The legacy dispatcher's ``if args.command:`` chain had no ``train`` case, so
``args.command == 'train'`` fell through to the bare-prompt handler and the
literal word "train" was forwarded to an LLM as a chat prompt — the real
training block was never reached and no trainer subprocess was launched.
This drives the *real* router (unlike the wholesale-``PraisonAI`` stub in
``praisonai-train``) and asserts the dataset config is generated and the trainer
invocation is dispatched, with ``handle_direct_prompt`` never called.
"""
import pytest
def _load_module():
try:
import praisonai_code.cli.legacy.praison_ai as pa
except ImportError as exc: # pragma: no cover - depends on optional wrapper
pytest.skip(f"legacy dispatcher unavailable: {exc}")
return pa
def test_train_command_reaches_trainer_not_direct_prompt(monkeypatch, tmp_path):
pa = _load_module()
# parse_args() needs the wrapper's argparse builder; skip if absent.
try:
from praisonai_code._wrapper_bridge import import_wrapper_module
import_wrapper_module("praisonai.cli.legacy.dispatch.argparse_builder")
except ImportError as exc: # pragma: no cover - depends on optional wrapper
pytest.skip(f"wrapper argparse builder unavailable: {exc}")
calls = {"hdp": [], "train_argv": None, "config": None}
def fake_hdp(self, *a, **k):
calls["hdp"].append(a)
return "PROMPTED"
def fake_stream(cmd, env=None):
calls["train_argv"] = list(cmd)
def fake_gen_config(**kw):
calls["config"] = kw
return {"model_name": kw.get("model_name") or ""}
monkeypatch.setattr(pa, "TRAIN_AVAILABLE", True)
monkeypatch.setattr(pa.PraisonAI, "handle_direct_prompt", fake_hdp)
monkeypatch.setattr(pa, "stream_subprocess", fake_stream)
monkeypatch.setattr(pa, "_get_generate_config", lambda: fake_gen_config)
# No conda: force the direct-python execution branch deterministically.
import subprocess as _sp
monkeypatch.setattr(_sp, "check_output", lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError()))
dataset = tmp_path / "my_sft.jsonl"
dataset.write_text('{"instruction": "x"}\n')
monkeypatch.chdir(tmp_path)
# parse_args() short-circuits to empty argv under a detected test env
# (argparse_builder parses ``[]`` when in_test_env). Present a genuine
# ``praisonai train ...`` argv so the real router parses ``command='train'``.
import sys as _sys
monkeypatch.setattr(
_sys, "argv",
["praisonai", "train", "--dataset", str(dataset), "--model", "llama-3.1"],
)
monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)
pa.PraisonAI().main()
# The literal word "train" was never sent to an LLM as a chat prompt.
assert calls["hdp"] == []
# The user's dataset drove config generation (not yahma/alpaca-cleaned).
assert calls["config"] is not None
assert calls["config"]["dataset"] == [{"name": str(dataset)}]
assert calls["config"]["model_name"] == "llama-3.1"
# The trainer subprocess was actually dispatched.
assert calls["train_argv"] is not None
assert "praisonai_train.train.llm.trainer" in calls["train_argv"]
assert "train" in calls["train_argv"]
if __name__ == "__main__":
pytest.main([__file__, "-v"])
@@ -303,6 +303,8 @@ class DeployHandler:
console.print(f" Provider: {provider_name}")
console.print(f" Region: {config.cloud.region}")
console.print(f" Service: {config.cloud.service_name}")
if config.cloud.cluster_name:
console.print(f" Cluster: {config.cloud.cluster_name}")
except ValueError as e:
if args.json:
Loaded 100 of 247 files, more files were not shown because too many files have changed in this diff. Show more