brain-framework 21.0.0__tar.gz → 21.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. {brain_framework-21.0.0 → brain_framework-21.1.0}/PKG-INFO +1 -1
  2. {brain_framework-21.0.0 → brain_framework-21.1.0}/pyproject.toml +1 -1
  3. {brain_framework-21.0.0 → brain_framework-21.1.0}/pyproject.toml.orig +1 -1
  4. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/__init__.py +1 -1
  5. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/assistants.py +2 -1
  6. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/models.py +7 -2
  7. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/runtime.py +25 -9
  8. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/storage.py +97 -3
  9. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/watch.py +1 -2
  10. {brain_framework-21.0.0 → brain_framework-21.1.0}/LICENSE +0 -0
  11. {brain_framework-21.0.0 → brain_framework-21.1.0}/README.md +0 -0
  12. {brain_framework-21.0.0 → brain_framework-21.1.0}/THIRD_PARTY_NOTICES.md +0 -0
  13. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/__main__.py +0 -0
  14. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/channels.py +0 -0
  15. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/cli.py +0 -0
  16. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/collect.py +0 -0
  17. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/config.py +0 -0
  18. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/evaluate.py +0 -0
  19. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/graph.py +0 -0
  20. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/health.py +0 -0
  21. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/history.py +0 -0
  22. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/index.py +0 -0
  23. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/install.py +0 -0
  24. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/links.py +0 -0
  25. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/markdown.py +0 -0
  26. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/mcp.py +0 -0
  27. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/messages.py +0 -0
  28. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/ontology.py +0 -0
  29. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/pages.py +0 -0
  30. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/py.typed +0 -0
  31. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/records.py +0 -0
  32. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/replies.py +0 -0
  33. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/retrieve.py +0 -0
  34. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/schemas.py +0 -0
  35. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/README.md +0 -0
  36. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-action/SKILL.md +0 -0
  37. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-action/references/actions.md +0 -0
  38. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-action/references/context.md +0 -0
  39. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-action/references/handoff.md +0 -0
  40. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-action/scripts/check-handoff.py +0 -0
  41. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-action/scripts/new-action.py +0 -0
  42. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-action/templates/action.md +0 -0
  43. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-maintain/SKILL.md +0 -0
  44. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-maintain/references/conflicts.md +0 -0
  45. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-maintain/references/integrations.md +0 -0
  46. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-maintain/references/operations.md +0 -0
  47. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-setup/SKILL.md +0 -0
  48. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-setup/references/discovery.md +0 -0
  49. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-setup/references/import.md +0 -0
  50. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-setup/references/scan.md +0 -0
  51. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-setup/scripts/inventory.py +0 -0
  52. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/SKILL.md +0 -0
  53. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/references/consolidate.md +0 -0
  54. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/references/decisions.md +0 -0
  55. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/references/evidence.md +0 -0
  56. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/references/learn.md +0 -0
  57. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/references/links.md +0 -0
  58. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/references/retrieval.md +0 -0
  59. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/references/review.md +0 -0
  60. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/references/share.md +0 -0
  61. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/scripts/evidence.py +0 -0
  62. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/scripts/guarded-write.py +0 -0
  63. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/templates/concept.md +0 -0
  64. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/skills/bf-use/templates/project.md +0 -0
  65. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/starter/AGENTS.md +0 -0
  66. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/starter/concepts/index.md +0 -0
  67. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/starter/concepts/welcome.md +0 -0
  68. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/starter/evals/retrieval.yaml +0 -0
  69. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/starter/gitignore +0 -0
  70. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/update.py +0 -0
  71. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/usage.py +0 -0
  72. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/validate.py +0 -0
  73. {brain_framework-21.0.0 → brain_framework-21.1.0}/src/bf/watch_settings.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: brain-framework
3
- Version: 21.0.0
3
+ Version: 21.1.0
4
4
  Summary: 🧠 Brain Framework: from information to informed actions.
5
5
  Keywords: ai,ai-agents,agent-memory,context-engineering,knowledge-management,local-first,mcp,model-context-protocol,python,second-brain
6
6
  Author: Médéric Hurier
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "brain-framework"
3
- version = "21.0.0"
3
+ version = "21.1.0"
4
4
  description = "🧠 Brain Framework: from information to informed actions."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.14"
@@ -1,7 +1,7 @@
1
1
  # https://packaging.python.org/en/latest/specifications/pyproject-toml/
2
2
  [project]
3
3
  name = "brain-framework"
4
- version = "21.0.0"
4
+ version = "21.1.0"
5
5
  description = "🧠 Brain Framework: from information to informed actions."
6
6
  readme = "README.md"
7
7
  requires-python = ">=3.14"
@@ -2,7 +2,7 @@
2
2
 
3
3
  import os
4
4
 
5
- __version__ = "21.0.0"
5
+ __version__ = "21.1.0"
6
6
 
7
7
 
8
8
  def main() -> None:
@@ -48,6 +48,7 @@ from bf.models import (
48
48
  decode,
49
49
  digest,
50
50
  encode,
51
+ retried,
51
52
  timestamp,
52
53
  )
53
54
  from bf.replies import ASSISTANT
@@ -491,7 +492,7 @@ def _execute(store: Store, key: str, runner: Runner) -> dict[str, object]:
491
492
  key,
492
493
  message,
493
494
  uncertain=current["state"] == "running" and not isinstance(error, NotStartedError),
494
- idempotent=work.settings.idempotent,
495
+ idempotent=retried(work.settings.idempotent, work.kind),
495
496
  retry=isinstance(error, GateError),
496
497
  )
497
498
  with suppress(Error, OSError):
@@ -991,6 +991,11 @@ class AssistantInput(Model):
991
991
  Invocation = Literal["input", "schedule", "manual", "wake"]
992
992
 
993
993
 
994
+ def retried(idempotent: object, kind: str) -> bool:
995
+ """Whether a failed attempt of a run of this kind retries by itself: `idempotent` is true or lists the kind."""
996
+ return idempotent is True or (isinstance(idempotent, list) and kind in idempotent)
997
+
998
+
994
999
  class Gate(Command):
995
1000
  """A deterministic check that runs before the harness and lists the events worth a model run."""
996
1001
 
@@ -1140,10 +1145,10 @@ class Assistant(Command):
1140
1145
  description="Keep this many newest run records in memories/NAME/; older ones are removed after a run. Unset "
1141
1146
  "keeps every run.",
1142
1147
  )
1143
- idempotent: bool = Field(
1148
+ idempotent: bool | Annotated[list[Invocation], Field(max_length=4)] = Field(
1144
1149
  default=False,
1145
1150
  description="Retry failed or interrupted pre-answer harness attempts automatically with backoff instead of "
1146
- "leaving the job uncertain.",
1151
+ "leaving the job uncertain: true for every run, or the run kinds to retry, such as [schedule, wake].",
1147
1152
  )
1148
1153
  inputs: Annotated[list[AssistantInput], Field(max_length=32)] = Field(default_factory=list)
1149
1154
  post: Name | None = Field(
@@ -17,10 +17,23 @@ from pydantic import Field, JsonValue, ValidationError, field_validator
17
17
 
18
18
  from bf.config import load
19
19
  from bf.messages import Message, Outgoing, Receipt
20
- from bf.models import Assistant, Error, Invocation, Model, Name, Record, Send, decode, digest, encode, timestamp
21
- from bf.storage import BusyError, Store, collecting, lock_file, runtime_store
22
-
23
- DATABASE = "queue.sqlite3"
20
+ from bf.models import (
21
+ Assistant,
22
+ Error,
23
+ Invocation,
24
+ Model,
25
+ Name,
26
+ Record,
27
+ Send,
28
+ decode,
29
+ digest,
30
+ encode,
31
+ retried,
32
+ timestamp,
33
+ )
34
+ from bf.storage import QUEUE, BusyError, Store, collecting, lock_file, runtime_store
35
+
36
+ DATABASE = QUEUE
24
37
  # The queue format this release reads and writes; any other is preserved, never adapted.
25
38
  VERSION = 2
26
39
  MAX_PENDING = 10_000
@@ -740,12 +753,15 @@ class Queue:
740
753
  """
741
754
  with self.connect() as db:
742
755
  # No job or delivery has an empty id: an omitted one matches nothing.
743
- job_row = db.execute(
744
- "SELECT coalesce(json_extract(payload,'$.settings.idempotent'),0) FROM jobs WHERE id=? AND state='running'",
745
- (job,),
746
- ).fetchone()
756
+ job_row = db.execute("SELECT payload,kind FROM jobs WHERE id=? AND state='running'", (job,)).fetchone()
747
757
  if job_row is not None:
748
- if bool(job_row[0]):
758
+ # A payload BF cannot read retries nothing: the attempt waits for inspection.
759
+ try:
760
+ payload = decode(job_row[0])
761
+ except Error:
762
+ payload = {}
763
+ settings = payload.get("settings") if isinstance(payload, dict) else None
764
+ if isinstance(settings, dict) and retried(settings.get("idempotent"), job_row[1]):
749
765
  outcome, message = self._retry_attempt(db, job, "interrupted harness attempt")
750
766
  db.execute(
751
767
  "UPDATE jobs SET state=?,error=?,updated=? WHERE id=? AND state='running'",
@@ -6,13 +6,14 @@ import errno
6
6
  import fcntl
7
7
  import os
8
8
  import re
9
+ import sqlite3
9
10
  import stat
10
11
  import time
11
12
  from collections.abc import Callable, Generator
12
13
  from contextlib import ExitStack, contextmanager, suppress
13
14
  from pathlib import Path
14
15
 
15
- from bf.models import MAX_FILE, MAX_FILES, Error, digest
16
+ from bf.models import MAX_FILE, MAX_FILES, Error, decode, digest
16
17
 
17
18
 
18
19
  class BusyError(Error):
@@ -30,6 +31,8 @@ _TEMPORARY = re.compile(r"\.write-[0-9a-f]{32}")
30
31
  UNNAMED = "file name is not valid UTF-8 or contains a backslash; rename it"
31
32
  # A link BF never follows, to a file or to a folder such as memories/: its owner puts the real one in its place.
32
33
  LINKED = "symlinks are not followed; replace the link with the file or folder itself"
34
+ # The assistant queue's file in its runtime folder (bf.runtime owns its schema).
35
+ QUEUE = "queue.sqlite3"
33
36
 
34
37
 
35
38
  def temporary(path: str) -> bool:
@@ -455,8 +458,8 @@ def xdg(name: str, default: str) -> Path:
455
458
  raise Error(f"cannot find the home directory; set HOME or {name}") from error
456
459
 
457
460
 
458
- def _private(*parts: str, root: Path) -> Store:
459
- """A private directory below XDG_STATE_HOME, or ~/.local/state, outside the evidence brain."""
461
+ def _state(*parts: str, root: Path) -> tuple[Path, Path]:
462
+ """The state home and a directory below its bf/ root, checked but not created."""
460
463
  base = xdg("XDG_STATE_HOME", ".local/state")
461
464
  if base.resolve().is_relative_to(root):
462
465
  raise Error("state directory must be outside the brain")
@@ -466,6 +469,12 @@ def _private(*parts: str, root: Path) -> Store:
466
469
  # Reject redirected state roots before creating any descendant: check each level from home down.
467
470
  if any(path.is_symlink() for path in (directory, *directory.parents[: len(parts) + 1])):
468
471
  raise Error("state directory may not contain symlinks")
472
+ return home, directory
473
+
474
+
475
+ def _private(*parts: str, root: Path) -> Store:
476
+ """A private directory below XDG_STATE_HOME, or ~/.local/state, outside the evidence brain."""
477
+ home, directory = _state(*parts, root=root)
469
478
  # mkdir applies its mode to the last directory only: create the shared bf/ root explicitly too.
470
479
  top = home / "bf"
471
480
  try:
@@ -500,9 +509,92 @@ def runtime_store(store: Store) -> Store:
500
509
  A drained queue follows a move or rename; unresolved work refuses execution until the brain is restored. Never
501
510
  discard this directory as a search cache.
502
511
  """
512
+ found, unread = _renumbered(store)
513
+ if unread:
514
+ names = ", ".join(sorted(unread))
515
+ raise Error(
516
+ f"assistant queue in the state folder runtime/ ({names}) may belong to this brain but cannot be read, "
517
+ f"such as after an interrupted write; if it is this brain's, rename it {store.identity}, otherwise run "
518
+ "bf queue in its own brain"
519
+ )
520
+ if len(found) > 1:
521
+ names = ", ".join(sorted(found))
522
+ raise Error(
523
+ f"several assistant queues in the state folder runtime/ are bound to this brain ({names}); move all but "
524
+ f"the one to keep out of runtime/, then rename it {store.identity}"
525
+ )
503
526
  return _private("runtime", store.identity, root=store.root)
504
527
 
505
528
 
529
+ _UNREAD = "\x00" # _bound's result for a queue that exists but cannot be read, never a brain path
530
+
531
+
532
+ def _bound(folder: Path) -> str | None:
533
+ """The brain path a runtime folder's queue was last bound to, read without changing it.
534
+
535
+ None when the folder holds no bound queue; _UNREAD when its queue cannot be read, such as one holding the journal
536
+ of an interrupted write, which SQLite refuses to open read-only.
537
+ """
538
+ database = folder / QUEUE
539
+ try:
540
+ info = database.lstat()
541
+ except FileNotFoundError:
542
+ return None
543
+ except OSError:
544
+ return _UNREAD
545
+ if not stat.S_ISREG(info.st_mode):
546
+ return _UNREAD
547
+ if not info.st_size:
548
+ return None # created, schema not committed yet: it holds nothing
549
+ try:
550
+ connection = sqlite3.connect(f"{database.as_uri()}?mode=ro", uri=True, timeout=1)
551
+ try:
552
+ if not connection.execute("SELECT 1 FROM sqlite_master WHERE name='checkpoints'").fetchone():
553
+ return _UNREAD
554
+ row = connection.execute("SELECT value FROM checkpoints WHERE key='binding'").fetchone()
555
+ finally:
556
+ connection.close()
557
+ binding = decode(row[0]) if row is not None else None
558
+ except sqlite3.Error, Error:
559
+ return _UNREAD
560
+ path = binding.get("path") if isinstance(binding, dict) else None
561
+ return path if isinstance(path, str) else None
562
+
563
+
564
+ def _renumbered(store: Store) -> tuple[list[str], list[str]]:
565
+ """Keep the queue and program state when only the brain's device number changed.
566
+
567
+ Return the old names that block it: several bound to this path, and queues that cannot be read.
568
+
569
+ Some filesystems, such as btrfs, can number a device anew at boot, which renames the identity of the same folder.
570
+ When the brain has no runtime folder yet, the one folder with the same inode whose queue is bound to this path,
571
+ and its program state, take the new name. A copy or a move to another filesystem has another path or inode, so
572
+ its old queue stays where it is. Several matches, or a queue that cannot be read, move nothing.
573
+ """
574
+ # Only the per-brain folders are private: mkdir gave their shared parents the default mode.
575
+ runtime = _state("runtime", root=store.root)[1]
576
+ if not runtime.is_dir() or os.path.lexists(runtime / store.identity):
577
+ return [], []
578
+ inode = "-" + store.identity.rsplit("-", 1)[1]
579
+ with _lock(store, "renumber.lock", 30, busy="another process is checking this brain's state; retry"):
580
+ if os.path.lexists(runtime / store.identity):
581
+ return [], []
582
+ bound = {
583
+ entry.name: _bound(Path(entry.path))
584
+ for entry in os.scandir(runtime)
585
+ if entry.name.endswith(inode) and entry.is_dir(follow_symlinks=False)
586
+ }
587
+ found = [name for name, path in bound.items() if path == str(store.root)]
588
+ unread = [name for name, path in bound.items() if path == _UNREAD]
589
+ if len(found) != 1 or unread:
590
+ return found, unread
591
+ programs = _state("programs", root=store.root)[1]
592
+ if (programs / found[0]).is_dir() and not os.path.lexists(programs / store.identity):
593
+ (programs / found[0]).rename(programs / store.identity)
594
+ (runtime / found[0]).rename(runtime / store.identity)
595
+ return [], []
596
+
597
+
506
598
  def program_state(store: Store, name: str) -> Store:
507
599
  """A program's private directory on this machine, for cursors and tokens kept between runs.
508
600
 
@@ -511,6 +603,8 @@ def program_state(store: Store, name: str) -> Store:
511
603
  """
512
604
  if not name or name.startswith(".") or "/" in name or "\x00" in name:
513
605
  raise Error("invalid program name")
606
+ # Collection never waits for the queue: an ambiguous queue is runtime_store's error, not a program's.
607
+ _renumbered(store)
514
608
  return _private("programs", store.identity, name, root=store.root)
515
609
 
516
610
 
@@ -659,9 +659,8 @@ def _settle(store: Store) -> None:
659
659
 
660
660
  Never creates the queue; a queue execution refuses keeps its entries for bf status to show.
661
661
  """
662
- queue = Queue(store)
663
662
  with suppress(Error, OSError):
664
- if queue.exists():
663
+ if (queue := Queue(store)).exists():
665
664
  with queue.connect() as db:
666
665
  db.execute("DELETE FROM checkpoints WHERE key LIKE 'health:%'")
667
666