mirror of
https://github.com/rennf93/roboco.git
synced 2026-08-03 07:23:24 +02:00
* fix(panel): cap dialog height and pin footer so actions stay reachable
Shared DialogContent now caps at max-h-[85vh] with overflow-y-auto, and the
footer is sticky to the bottom. Long content (e.g. a pasted change-request
note) no longer pushes the submit/cancel buttons past the viewport — the body
scrolls while the actions stay visible. No-op on dialogs that already fit.
* feat(notifications): suppress duplicate same-purpose notifications at send
A notification is not created when an unacknowledged one with the same purpose
— same sender, same type, same task, overlapping recipients — already exists.
Body text is not compared, so rewording cannot defeat it; a different type,
task, sender, or an already-acked recipient all still send through. Stops
agents that loop re-issuing the same signal from piling up unread that
soft-blocks the recipient's idle path.
* fix(gateway): stop board/PM lifecycle verbs from 500-crashing
Two unguarded crashes that wedged the org in respawn/escalate loops:
- escalate_to_ceo dereferenced None.status when the verb runner declined the
escalation (task not in awaiting_pm_review — e.g. a board agent escalating a
blocked task). It now returns a clean invalid_state. The message/remediate
build moved to a helper so the function stays within the complexity gate.
- The coordination-root git ops (pr_target, pr_merge, PR update, branch-token
resolve) called UUID(str(task.project_id)) directly, which raised on a
coordination/integration task (project_id is None — 'badly formed hexadecimal
UUID string'). They now resolve through _project_for_task, which falls back to
the product's repo for project-less roots.
* refactor(intake): split out _block_to_chunk per-block classifier
Extract the per-block classification from _blocks_to_chunks so each function
stays within the xenon cyclomatic-complexity gate (was rank C). Behaviour is
unchanged — verified by the existing intake_driver tests.
* feat(gateway): make the i_am_idle unread soft-block satisfiable
The soft-block on unread A2A / @mentions had no clearing path, so once those
briefing fields populated an agent could never idle — a whole-org deadlock.
Keep the guard (it is correct) and add the missing clear paths:
- New read_messages content verb (schema -> route -> handler ->
a2a.mark_all_read -> MCP tool -> role do_tools): bulk-zeroes the caller's
unread A2A and stamps read_at. The idle hint now points to it.
- list_unread_mentions returns UNACKED MENTION-type notifications (each @mention
already raises one via messaging._notify_mentions) instead of raw,
unconditional mentions, so they clear via the existing notify_ack. No schema
migration needed.
The soft-block is now satisfiable: A2A via read_messages, mentions and
notifications via notify_ack.
* fix(tests): repair notification-dedup db.scalar mocks + prompter agent seeding
The notification send-dedup added a db.scalar() purpose-lookup to
_create_notification; the two hand-rolled _FakeDb test stubs (test_notification,
test_a2a_priority_tristate) had no scalar() method → AttributeError. Add
scalar() returning None (no duplicate) so creation proceeds.
Separately, the prompter '& Start' route tests assign the draft to a fixed
product-owner / main-pm AGENT_UUID but only seeded system + CEO, so the
assigned_to FK failed in isolation (and main-pm flaked in the full suite). Seed
both via idempotent merge() in _seed_project_and_ceo.
* fix(git): gitignore .pnpm-store + flag GH001 push rejection as permanent
A dev once committed the ~115 MB pnpm store → GitHub GH001 (>100 MB) pre-receive
reject → open_pr retry-loop. Two root fixes:
- Add .pnpm-store/ to .gitignore — an ignored dir can't be staged by any git add.
- push() restates a GH001 / file-size rejection as an unmistakable PERMANENT
error pointing at i_am_blocked, so the agent stops blind-retrying a push that
can never succeed (it otherwise mis-reads the raw output as a transient timeout).
The per-verb retry cap (open_pr: 5) already bounded the burn; this ends it.
* fix(gateway): accept a PM decision note as satisfying the complete/submit_up reflect gate
A cell/main PM that wrote a fresh decision but no separate reflect note bounced
on the reflect tracing-gate indefinitely (re-confirmed live: cell PMs looped on
cell_pm_complete -> journal:reflect until reaped, burning tokens — worse because
each respawn resets the per-verb retry cap). For a PM closing/submitting a task
the decision note already documents the close; the separate reflect is the
redundant artifact weak-model PMs forget. Accept a fresh decision as satisfying
reflect for complete + submit_up — the gate still requires a decision +
substantive notes, so the close stays documented.
NOTE (enforcement tradeoff, flagged for CEO review): this intentionally relaxes
the PM complete/submit_up gate. It does NOT touch the developer i_am_done gate.
* feat(gateway): refuse i_am_idle when a PM still owns a task awaiting its review
A cell/main PM once tried to 'send work back' by DMing the developer and going
idle — but a DM changes no task state, so the task stayed awaiting_pm_review and
the orchestrator just re-dispatched the PM in a loop. i_am_idle now refuses (like
the pending-assignment guard) when a PM owns an awaiting_pm_review task, with a
clear remediation: complete() to finish, or reassign()/delegate() to route it
back. PM-only; devs/QA/doc unaffected. Pairs with the reflect-gate relaxation so
the PM can actually complete instead of looping.
* feat(gateway): push a prior-work handoff digest into task-scoped briefings
A freshly spawned or respawned agent previously started cold on every
lifecycle hand-off: the prior worker's PR, commits, acceptance status and
journal highlights lived in task evidence but were pull-on-demand, so each
new role agent re-explored the codebase from scratch — wasted tokens and
fragile context loss across respawns.
build_task_handoff() composes a compact, DB-only digest (no git diff) and
_briefing_for() now attaches it to context_briefing whenever the caller
already holds the task row. The digest is built only from a passed-in task,
so there are zero extra fetches: every resumption entry point (give_me_work
and pm_give_me_work, i_will_work_on, i_will_plan, triage/triage_all,
i_am_done, submit_up, escalate_up, complete) threads the loaded task, while
id-only correction/rejection paths cleanly omit it.
Every field is type-guarded so a partial row never leaks a non-serialisable
value into the envelope.
* docs(prompts): tell agents to resume from the briefing handoff before re-exploring
The base prompt described the success envelope but never told agents to act
on context_briefing, so a respawned or hand-off agent would re-scan the
whole repo and re-derive the plan even when the briefing already carried the
prior worker's PR, commits, acceptance status and journal highlights.
Adds a 'Resume from your briefing' section that walks each task_handoff
field and instructs the agent to continue from it — and to read the unread
A2A / mention / notification lists, which are messages addressed to them.
Pairs with the gateway change that now pushes task_handoff into every
task-scoped briefing.
* feat(tasks): remember cleared dependencies so the unblock briefing can surface them
When an upstream dependency completed, _unblock_dependents removed its id from
the dependent's dependency_ids to let it be claimed — destroying the only
record of which upstream task had just landed. The revived dependent then
re-discovered that work from cold.
Adds tasks.completed_dependency_ids (Alembic 026, uuid[] default '{}'):
_unblock_dependents now appends the cleared id there instead of only dropping
it, and the briefing handoff digest surfaces it so the agent picking the task
back up knows its blocker cleared because that upstream work shipped. The base
prompt documents the field.
Migration round-trip verified against postgres (upgrade adds the column,
downgrade drops it).
* docs(prompts): instruct PMs to split oversized tasks into per-concern subtasks
A subtask carrying a long acceptance list or spanning multiple layers/files
drove repeated QA failures and a PM revision loop — QA can't pass a partial,
and the dev keeps re-touching unrelated parts. Nothing in the PM prompts told
them to decompose by size/concern.
cell_pm gets a 'Sizing' rule: one subtask = one focused concern with ~2-4
criteria and its own dev->QA pass; decompose anything larger before
delegating, sequencing with dependencies. main_pm gets a matching reminder to
scope each cell's slice to that cell's layer rather than handing a cell a
cross-layer monolith that just pushes the problem down a level.
* fix(gateway): mirror the task= kwarg on ChoreographerHelpers helper signatures
The handoff-digest change added a keyword-only task= parameter to
_briefing_for and _build_tracing_gap in _impl, but the ChoreographerHelpers
base that the role mixins inherit still declared the old signatures, so the
composed Choreographer had two incompatible base definitions (mypy [misc]).
Sync the base declarations to match.
* fix(tasks): keep the owner on a substitute-out so the task isn't orphaned
build_substitute_update unconditionally nulled assigned_to, so any
substitute that routes to PENDING (max_retries, low_context, out_of_scope_*)
— the path a verb hitting repeated 500s or its retry limit takes — left the
task pending AND unassigned. The dispatcher only respawns a pending task when
it has an owner, so the task went dormant: no agent ever picked it back up.
Keep the task with its current owner instead. A substitute-out is almost
always a transient stall, so the task re-dispatches to the SAME agent, which
resumes from the briefing handoff. Only the task_complete -> PM-review handoff
changes owner (unchanged).
* feat(a2a): suppress duplicate unread A2A messages at send
A respawned or retrying agent could re-emit the same DM, stacking identical
copies on the recipient's inbox and re-bumping the unread count — noise that
the recipient then has to clear. The notification path already dedups; A2A did
not.
send_chat_message now suppresses a send when an identical message from the
same sender is still unread in the conversation, keyed on (conversation,
sender, message_kind, content). Genuinely different messages are never
collapsed (verified: distinct content still produces distinct rows), so this
avoids the earlier per-pair over-suppression. No migration.
* fix(panel): default the notifications view to Unread, not All
Landing on the All tab buried new notifications under everything already
seen — the most-reported annoyance. The Unread tab is the actionable view, so
make it the default; the All/Pending tabs are one click away.
* fix(panel): show clone progress during intake prep instead of a frozen pill
The first clone of a repo can take a few minutes, during which the intake
form showed only a static 'Preparing the agent…' button — indistinguishable
from a hang. Add a progress region while preparing: an elapsed timer, a
saturating progress bar (approaches but never reaches 100% until the agent
actually answers), and staged copy (spinning up → cloning → first-clone-takes-
a-while → reading the codebase) so the wait reads as work, not a freeze.
* feat(docs): index workspace-authored docs that never reached the RAG store
Docs written through roboco_docs_write land at /app/docs on the orchestrator
and index fine. But a documenter can also write docs with Edit/Write directly
in its own clone (README, CHANGELOG, workspace markdown); those resolve to a
/app/docs path that doesn't exist on the orchestrator, so the indexer reads
nothing and the docs never become searchable — a cross-container miss with no
shared mount to bridge it.
On docs completion, capture each listed doc's committed content out of the
branch (new GitService.read_file_at_branch, via git show) and write it
server-side under /app/docs before indexing, so workspace-authored docs reach
RAG too. Docs already present server-side are skipped; absolute paths and
unreadable/uncommitted files are passed over best-effort.
* feat(prompter): survive a browser reload by reconnecting to the live intake chat
The intake chat lived entirely in React state, so a page reload wiped it and
dropped the human back to the scope form — even though the agent container
outlives the page. Now the chat persists a small TTL'd slice (session id,
messages, scope, draft) to localStorage and, on mount, reconnects: it asks the
new GET /live/{id}/status whether the session is still running and, if so,
restores the history and reopens the SSE stream; if dead or expired it clears
and shows the form. A full reload doesn't run React effect cleanup, so the
navigate-away reap never fires on refresh and the session stays up.
Backend adds the status endpoint + PrompterLiveRegistry.is_alive; localStorage
is cleared on confirm, start-another, and SPA navigate-away.
* chore: remove internal session-bookkeeping refs from code comments (part 1)
Strip leaked task/finding numbers, Wave/Phase/cluster/audit labels from
docstrings and comments across services, foundation policy, runtime, mcp,
api schemas, and agent_sdk — they mean nothing to a repo reader and expose
process internals. Wording preserved; only the labels dropped. Done by hand,
one comment at a time (no scripted rewrite). _impl.py follows separately.
* chore: remove internal session-bookkeeping refs from code comments (part 2)
Finishes the manual scrub: the choreographer _impl.py docstrings/comments plus
the remaining dogfood-run ('smoke-N') labels across runtime, mcp, foundation,
api schemas, services, and agent factories. Reworded to describe the bug or
behaviour in plain words; every label dropped. The repo source is now free of
task/finding numbers, Wave/Phase/cluster/audit/smoke labels. By hand, one
comment at a time.
---------
Co-authored-by: Renn F <rennf93@users.noreply.github.com>
890 lines
31 KiB
Python
890 lines
31 KiB
Python
"""
|
|
Journal API Service
|
|
|
|
Manages agent personal journals for reflection, growth tracking, and debugging.
|
|
Each agent has their own journal with entries tied to tasks and sessions.
|
|
Integrates with the Optimal API for RAG indexing of entries.
|
|
"""
|
|
|
|
from datetime import UTC, datetime
|
|
from typing import Any, ClassVar
|
|
from uuid import UUID
|
|
|
|
from sqlalchemy import and_, func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from roboco.db.tables import JournalEntryTable, JournalTable
|
|
from roboco.foundation.policy.journaling import (
|
|
SCOPE_TO_TYPE as _FOUNDATION_SCOPE_TO_TYPE,
|
|
)
|
|
from roboco.models.base import JournalEntryType
|
|
from roboco.models.journal import (
|
|
DecisionLogParams,
|
|
GeneralEntryParams,
|
|
GrowthMetrics,
|
|
Journal,
|
|
JournalEntry,
|
|
JournalEntryCreate,
|
|
JournalStats,
|
|
LearningEntryParams,
|
|
ListEntriesFilter,
|
|
StruggleEntryParams,
|
|
TaskReflectionParams,
|
|
create_decision_log,
|
|
create_general_entry,
|
|
create_learning_entry,
|
|
create_struggle_entry,
|
|
create_task_reflection,
|
|
)
|
|
from roboco.models.optimal import IndexJournalEntryParams
|
|
from roboco.services.base import BaseService
|
|
from roboco.utils.converters import require_uuid, to_python_uuid
|
|
|
|
# Scope mapping is canonical in foundation.policy.journaling.
|
|
# Derived as string-keyed dict here because the service's call sites pass
|
|
# scope strings (from the gateway's content_actions layer) not Scope enums.
|
|
_SCOPE_TO_TYPE: dict[str, JournalEntryType] = {
|
|
scope.value: entry_type for scope, entry_type in _FOUNDATION_SCOPE_TO_TYPE.items()
|
|
}
|
|
|
|
|
|
class JournalService(BaseService):
|
|
"""
|
|
Service for managing agent journals.
|
|
|
|
Provides CRUD operations for journals and entries,
|
|
plus analytics and growth tracking.
|
|
"""
|
|
|
|
service_name: ClassVar[str] = "journal"
|
|
|
|
def __init__(self, db: AsyncSession) -> None:
|
|
super().__init__(db)
|
|
self._optimal_service: Any = None # Lazy loaded to avoid circular import
|
|
|
|
async def _get_optimal_service(self) -> Any:
|
|
"""Lazy load the OptimalService."""
|
|
if self._optimal_service is None:
|
|
from roboco.services.optimal import get_optimal_service
|
|
|
|
self._optimal_service = await get_optimal_service()
|
|
return self._optimal_service
|
|
|
|
async def resolve_agent_id(self, agent_id_or_slug: str) -> UUID | None:
|
|
"""
|
|
Resolve an agent identifier to a UUID.
|
|
|
|
Accepts either a UUID string or an agent slug (e.g., "be-dev-1").
|
|
|
|
Args:
|
|
agent_id_or_slug: UUID string or agent slug
|
|
|
|
Returns:
|
|
The agent's UUID, or None if not found
|
|
"""
|
|
from roboco.services.repositories import resolve_agent_uuid
|
|
|
|
return await resolve_agent_uuid(self.session, agent_id_or_slug)
|
|
|
|
async def get_agent_slug(self, agent_id: UUID) -> str | None:
|
|
"""
|
|
Get an agent's slug from their UUID.
|
|
|
|
Args:
|
|
agent_id: The agent's UUID
|
|
|
|
Returns:
|
|
The agent's slug (e.g., "be-dev-1"), or None if not found
|
|
"""
|
|
from roboco.services.repositories import get_agent_slug
|
|
|
|
return await get_agent_slug(self.session, agent_id)
|
|
|
|
# =========================================================================
|
|
# JOURNAL CRUD
|
|
# =========================================================================
|
|
|
|
async def get_or_create_journal(self, agent_id: UUID) -> Journal:
|
|
"""
|
|
Get or create a journal for an agent.
|
|
|
|
Args:
|
|
agent_id: The agent's UUID
|
|
|
|
Returns:
|
|
The agent's journal
|
|
"""
|
|
result = await self.session.execute(
|
|
select(JournalTable).where(JournalTable.agent_id == agent_id)
|
|
)
|
|
journal_row = result.scalar_one_or_none()
|
|
|
|
if journal_row:
|
|
return Journal(
|
|
id=require_uuid(journal_row.id),
|
|
agent_id=require_uuid(journal_row.agent_id),
|
|
total_entries=journal_row.total_entries,
|
|
last_entry_at=journal_row.last_entry_at,
|
|
latest_summary=journal_row.latest_summary,
|
|
summary_updated_at=journal_row.summary_updated_at,
|
|
entries_by_type=journal_row.entries_by_type,
|
|
created_at=journal_row.created_at,
|
|
updated_at=journal_row.updated_at,
|
|
)
|
|
|
|
# Create new journal
|
|
new_journal = JournalTable(agent_id=agent_id)
|
|
self.session.add(new_journal)
|
|
await self.session.commit()
|
|
await self.session.refresh(new_journal)
|
|
|
|
self.log.info(
|
|
"Created new journal",
|
|
agent_id=str(agent_id),
|
|
journal_id=str(new_journal.id),
|
|
)
|
|
|
|
return Journal(
|
|
id=require_uuid(new_journal.id),
|
|
agent_id=require_uuid(new_journal.agent_id),
|
|
total_entries=0,
|
|
created_at=new_journal.created_at,
|
|
)
|
|
|
|
async def get_journal(self, journal_id: UUID) -> Journal | None:
|
|
"""Get a journal by ID."""
|
|
result = await self.session.execute(
|
|
select(JournalTable).where(JournalTable.id == journal_id)
|
|
)
|
|
journal_row = result.scalar_one_or_none()
|
|
|
|
if not journal_row:
|
|
return None
|
|
|
|
return Journal(
|
|
id=require_uuid(journal_row.id),
|
|
agent_id=require_uuid(journal_row.agent_id),
|
|
total_entries=journal_row.total_entries,
|
|
last_entry_at=journal_row.last_entry_at,
|
|
latest_summary=journal_row.latest_summary,
|
|
summary_updated_at=journal_row.summary_updated_at,
|
|
entries_by_type=journal_row.entries_by_type,
|
|
created_at=journal_row.created_at,
|
|
updated_at=journal_row.updated_at,
|
|
)
|
|
|
|
async def get_journal_by_agent(self, agent_id: UUID) -> Journal | None:
|
|
"""Get a journal by agent ID."""
|
|
result = await self.session.execute(
|
|
select(JournalTable).where(JournalTable.agent_id == agent_id)
|
|
)
|
|
journal_row = result.scalar_one_or_none()
|
|
|
|
if not journal_row:
|
|
return None
|
|
|
|
return Journal(
|
|
id=require_uuid(journal_row.id),
|
|
agent_id=require_uuid(journal_row.agent_id),
|
|
total_entries=journal_row.total_entries,
|
|
last_entry_at=journal_row.last_entry_at,
|
|
latest_summary=journal_row.latest_summary,
|
|
summary_updated_at=journal_row.summary_updated_at,
|
|
entries_by_type=journal_row.entries_by_type,
|
|
created_at=journal_row.created_at,
|
|
updated_at=journal_row.updated_at,
|
|
)
|
|
|
|
# =========================================================================
|
|
# ENTRY CRUD
|
|
# =========================================================================
|
|
|
|
async def create_entry(
|
|
self, entry_create: JournalEntryCreate
|
|
) -> JournalEntry | None:
|
|
"""
|
|
Create a new journal entry.
|
|
|
|
Args:
|
|
entry_create: Entry creation schema
|
|
|
|
Returns:
|
|
The created entry, or None if the referenced task/session/journal
|
|
no longer exists (e.g. after a runtime reset).
|
|
"""
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
# Create entry in database
|
|
entry_row = JournalEntryTable(
|
|
journal_id=entry_create.journal_id,
|
|
type=entry_create.type,
|
|
title=entry_create.title,
|
|
content=entry_create.content,
|
|
task_id=entry_create.task_id,
|
|
session_id=entry_create.session_id,
|
|
tags=entry_create.tags,
|
|
sentiment=entry_create.sentiment,
|
|
is_private=entry_create.is_private,
|
|
)
|
|
self.session.add(entry_row)
|
|
|
|
# Update journal metadata
|
|
result = await self.session.execute(
|
|
select(JournalTable).where(JournalTable.id == entry_create.journal_id)
|
|
)
|
|
journal_row = result.scalar_one_or_none()
|
|
|
|
if journal_row:
|
|
journal_row.total_entries += 1
|
|
journal_row.last_entry_at = datetime.now(UTC)
|
|
type_key = (
|
|
entry_create.type.value
|
|
if isinstance(entry_create.type, JournalEntryType)
|
|
else entry_create.type
|
|
)
|
|
entries_by_type = dict(journal_row.entries_by_type)
|
|
entries_by_type[type_key] = entries_by_type.get(type_key, 0) + 1
|
|
journal_row.entries_by_type = entries_by_type
|
|
|
|
try:
|
|
await self.session.commit()
|
|
except IntegrityError as e:
|
|
await self.session.rollback()
|
|
self.log.warning(
|
|
"Journal entry skipped - referenced row was deleted",
|
|
journal_id=str(entry_create.journal_id),
|
|
task_id=str(entry_create.task_id) if entry_create.task_id else None,
|
|
session_id=str(entry_create.session_id)
|
|
if entry_create.session_id
|
|
else None,
|
|
error=str(e.orig),
|
|
)
|
|
return None
|
|
|
|
await self.session.refresh(entry_row)
|
|
|
|
self.log.info(
|
|
"Created journal entry",
|
|
entry_id=str(entry_row.id),
|
|
journal_id=str(entry_create.journal_id),
|
|
type=entry_create.type,
|
|
)
|
|
|
|
# Index in RAG - non-blocking
|
|
try:
|
|
optimal = await self._get_optimal_service()
|
|
# Convert SQLAlchemy UUIDs to Python UUIDs for the params
|
|
agent_id_for_index = (
|
|
require_uuid(journal_row.agent_id)
|
|
if journal_row
|
|
else entry_create.journal_id
|
|
)
|
|
await optimal.index_journal_entry(
|
|
IndexJournalEntryParams(
|
|
entry_id=require_uuid(entry_row.id),
|
|
agent_id=agent_id_for_index,
|
|
content=entry_create.content,
|
|
entry_type=type_key,
|
|
task_id=entry_create.task_id,
|
|
tags=entry_create.tags,
|
|
)
|
|
)
|
|
|
|
# Also index LEARNING entries to the learnings index for cross-agent sharing
|
|
if type_key == JournalEntryType.LEARNING.value:
|
|
from roboco.services.optimal_brain.indexes.learnings import (
|
|
RecordLearningParams,
|
|
)
|
|
|
|
await optimal.record_learning(
|
|
RecordLearningParams(
|
|
content=entry_create.content,
|
|
category="journal_learning",
|
|
agent_id=agent_id_for_index,
|
|
task_id=entry_create.task_id,
|
|
shareable=not entry_create.is_private,
|
|
tags=entry_create.tags,
|
|
)
|
|
)
|
|
except Exception as e:
|
|
self.log.warning("Failed to index journal entry in RAG", error=str(e))
|
|
|
|
return JournalEntry(
|
|
id=require_uuid(entry_row.id),
|
|
journal_id=require_uuid(entry_row.journal_id),
|
|
type=entry_row.type,
|
|
title=entry_row.title,
|
|
content=entry_row.content,
|
|
task_id=to_python_uuid(entry_row.task_id),
|
|
session_id=to_python_uuid(entry_row.session_id),
|
|
timestamp=entry_row.timestamp,
|
|
tags=entry_row.tags,
|
|
sentiment=entry_row.sentiment,
|
|
is_private=entry_row.is_private,
|
|
created_at=entry_row.created_at,
|
|
)
|
|
|
|
async def get_entry(self, entry_id: UUID) -> JournalEntry | None:
|
|
"""Get a journal entry by ID."""
|
|
result = await self.session.execute(
|
|
select(JournalEntryTable).where(JournalEntryTable.id == entry_id)
|
|
)
|
|
entry_row = result.scalar_one_or_none()
|
|
|
|
if not entry_row:
|
|
return None
|
|
|
|
return JournalEntry(
|
|
id=require_uuid(entry_row.id),
|
|
journal_id=require_uuid(entry_row.journal_id),
|
|
type=entry_row.type,
|
|
title=entry_row.title,
|
|
content=entry_row.content,
|
|
task_id=to_python_uuid(entry_row.task_id),
|
|
session_id=to_python_uuid(entry_row.session_id),
|
|
timestamp=entry_row.timestamp,
|
|
tags=entry_row.tags,
|
|
sentiment=entry_row.sentiment,
|
|
is_private=entry_row.is_private,
|
|
created_at=entry_row.created_at,
|
|
updated_at=entry_row.updated_at,
|
|
)
|
|
|
|
async def list_entries(
|
|
self,
|
|
journal_id: UUID,
|
|
filters: ListEntriesFilter | None = None,
|
|
) -> list[JournalEntry]:
|
|
"""
|
|
List journal entries with filtering.
|
|
|
|
Args:
|
|
journal_id: Journal to list entries from
|
|
filters: Optional filter parameters
|
|
|
|
Returns:
|
|
List of journal entries
|
|
"""
|
|
f = filters or ListEntriesFilter()
|
|
query = select(JournalEntryTable).where(
|
|
JournalEntryTable.journal_id == journal_id
|
|
)
|
|
|
|
if f.entry_type:
|
|
query = query.where(JournalEntryTable.type == f.entry_type)
|
|
|
|
if f.task_id:
|
|
query = query.where(JournalEntryTable.task_id == f.task_id)
|
|
|
|
if not f.include_private:
|
|
query = query.where(JournalEntryTable.is_private.is_(False))
|
|
|
|
query = query.order_by(JournalEntryTable.timestamp.desc())
|
|
query = query.limit(f.limit).offset(f.offset)
|
|
|
|
result = await self.session.execute(query)
|
|
rows = result.scalars().all()
|
|
|
|
return [
|
|
JournalEntry(
|
|
id=require_uuid(row.id),
|
|
journal_id=require_uuid(row.journal_id),
|
|
type=row.type,
|
|
title=row.title,
|
|
content=row.content,
|
|
task_id=to_python_uuid(row.task_id),
|
|
session_id=to_python_uuid(row.session_id),
|
|
timestamp=row.timestamp,
|
|
tags=row.tags,
|
|
sentiment=row.sentiment,
|
|
is_private=row.is_private,
|
|
created_at=row.created_at,
|
|
updated_at=row.updated_at,
|
|
)
|
|
for row in rows
|
|
]
|
|
|
|
async def delete_entry(self, entry_id: UUID) -> bool:
|
|
"""Delete a journal entry."""
|
|
result = await self.session.execute(
|
|
select(JournalEntryTable).where(JournalEntryTable.id == entry_id)
|
|
)
|
|
entry_row = result.scalar_one_or_none()
|
|
|
|
if not entry_row:
|
|
return False
|
|
|
|
# Update journal metadata
|
|
journal_result = await self.session.execute(
|
|
select(JournalTable).where(JournalTable.id == entry_row.journal_id)
|
|
)
|
|
journal_row = journal_result.scalar_one_or_none()
|
|
|
|
if journal_row:
|
|
journal_row.total_entries = max(0, journal_row.total_entries - 1)
|
|
type_key = (
|
|
entry_row.type.value
|
|
if isinstance(entry_row.type, JournalEntryType)
|
|
else entry_row.type
|
|
)
|
|
entries_by_type = dict(journal_row.entries_by_type)
|
|
if type_key in entries_by_type:
|
|
entries_by_type[type_key] = max(0, entries_by_type[type_key] - 1)
|
|
journal_row.entries_by_type = entries_by_type
|
|
|
|
await self.session.delete(entry_row)
|
|
await self.session.commit()
|
|
|
|
self.log.info("Deleted journal entry", entry_id=str(entry_id))
|
|
return True
|
|
|
|
# =========================================================================
|
|
# CONVENIENCE METHODS
|
|
# =========================================================================
|
|
|
|
async def add_task_reflection(
|
|
self,
|
|
agent_id: UUID,
|
|
params: TaskReflectionParams,
|
|
) -> JournalEntry | None:
|
|
"""Add a task reflection entry."""
|
|
journal = await self.get_or_create_journal(agent_id)
|
|
# Update journal_id in params
|
|
params_with_journal = TaskReflectionParams(
|
|
journal_id=journal.id,
|
|
task_id=params.task_id,
|
|
title=params.title,
|
|
what_done=params.what_done,
|
|
what_learned=params.what_learned,
|
|
what_struggled=params.what_struggled,
|
|
next_steps=params.next_steps,
|
|
tags=params.tags,
|
|
)
|
|
entry = create_task_reflection(params_with_journal)
|
|
return await self.create_entry(
|
|
JournalEntryCreate(
|
|
journal_id=entry.journal_id,
|
|
type=entry.type,
|
|
title=entry.title,
|
|
content=entry.content,
|
|
task_id=entry.task_id,
|
|
tags=entry.tags,
|
|
)
|
|
)
|
|
|
|
async def add_decision_log(
|
|
self,
|
|
agent_id: UUID,
|
|
params: DecisionLogParams,
|
|
) -> JournalEntry | None:
|
|
"""Add a decision log entry."""
|
|
journal = await self.get_or_create_journal(agent_id)
|
|
params_with_journal = DecisionLogParams(
|
|
journal_id=journal.id,
|
|
title=params.title,
|
|
context=params.context,
|
|
options=params.options,
|
|
chosen=params.chosen,
|
|
rationale=params.rationale,
|
|
consequences=params.consequences,
|
|
task_id=params.task_id,
|
|
tags=params.tags,
|
|
)
|
|
entry = create_decision_log(params_with_journal)
|
|
return await self.create_entry(
|
|
JournalEntryCreate(
|
|
journal_id=entry.journal_id,
|
|
type=entry.type,
|
|
title=entry.title,
|
|
content=entry.content,
|
|
task_id=entry.task_id,
|
|
tags=entry.tags,
|
|
)
|
|
)
|
|
|
|
async def add_learning(
|
|
self,
|
|
agent_id: UUID,
|
|
params: LearningEntryParams,
|
|
) -> JournalEntry | None:
|
|
"""Add a learning entry."""
|
|
journal = await self.get_or_create_journal(agent_id)
|
|
params_with_journal = LearningEntryParams(
|
|
journal_id=journal.id,
|
|
title=params.title,
|
|
what_learned=params.what_learned,
|
|
how_applied=params.how_applied,
|
|
source=params.source,
|
|
task_id=params.task_id,
|
|
tags=params.tags,
|
|
)
|
|
entry = create_learning_entry(params_with_journal)
|
|
return await self.create_entry(
|
|
JournalEntryCreate(
|
|
journal_id=entry.journal_id,
|
|
type=entry.type,
|
|
title=entry.title,
|
|
content=entry.content,
|
|
task_id=entry.task_id,
|
|
tags=entry.tags,
|
|
sentiment=entry.sentiment,
|
|
)
|
|
)
|
|
|
|
async def add_struggle(
|
|
self,
|
|
agent_id: UUID,
|
|
params: StruggleEntryParams,
|
|
) -> JournalEntry | None:
|
|
"""Add a struggle entry."""
|
|
journal = await self.get_or_create_journal(agent_id)
|
|
params_with_journal = StruggleEntryParams(
|
|
journal_id=journal.id,
|
|
title=params.title,
|
|
what_struggled=params.what_struggled,
|
|
attempted_solutions=params.attempted_solutions,
|
|
resolution=params.resolution,
|
|
help_needed=params.help_needed,
|
|
task_id=params.task_id,
|
|
tags=params.tags,
|
|
)
|
|
entry = create_struggle_entry(params_with_journal)
|
|
return await self.create_entry(
|
|
JournalEntryCreate(
|
|
journal_id=entry.journal_id,
|
|
type=entry.type,
|
|
title=entry.title,
|
|
content=entry.content,
|
|
task_id=entry.task_id,
|
|
tags=entry.tags,
|
|
sentiment=entry.sentiment,
|
|
)
|
|
)
|
|
|
|
async def add_general_entry(
|
|
self,
|
|
agent_id: UUID,
|
|
params: GeneralEntryParams,
|
|
) -> JournalEntry | None:
|
|
"""Add a general journal entry."""
|
|
journal = await self.get_or_create_journal(agent_id)
|
|
params_with_journal = GeneralEntryParams(
|
|
journal_id=journal.id,
|
|
title=params.title,
|
|
content=params.content,
|
|
task_id=params.task_id,
|
|
session_id=params.session_id,
|
|
tags=params.tags,
|
|
is_private=params.is_private,
|
|
)
|
|
entry = create_general_entry(params_with_journal)
|
|
return await self.create_entry(
|
|
JournalEntryCreate(
|
|
journal_id=entry.journal_id,
|
|
type=entry.type,
|
|
title=entry.title,
|
|
content=entry.content,
|
|
task_id=entry.task_id,
|
|
session_id=entry.session_id,
|
|
tags=entry.tags,
|
|
is_private=entry.is_private,
|
|
)
|
|
)
|
|
|
|
# =========================================================================
|
|
# ANALYTICS
|
|
# =========================================================================
|
|
|
|
async def get_journal_stats(self, journal_id: UUID) -> JournalStats | None:
|
|
"""Get statistics for a journal."""
|
|
result = await self.session.execute(
|
|
select(JournalTable).where(JournalTable.id == journal_id)
|
|
)
|
|
journal_row = result.scalar_one_or_none()
|
|
|
|
if not journal_row:
|
|
return None
|
|
|
|
return JournalStats(
|
|
total_entries=journal_row.total_entries,
|
|
entries_by_type=journal_row.entries_by_type,
|
|
last_entry_at=journal_row.last_entry_at,
|
|
has_summary=journal_row.latest_summary is not None,
|
|
)
|
|
|
|
async def get_growth_metrics(self, agent_id: UUID) -> GrowthMetrics | None:
|
|
"""
|
|
Calculate growth metrics for an agent.
|
|
|
|
Analyzes journal entries to track learning progress,
|
|
struggle resolution, and sentiment trends.
|
|
"""
|
|
journal = await self.get_journal_by_agent(agent_id)
|
|
if not journal:
|
|
return None
|
|
|
|
entries_by_type = journal.entries_by_type
|
|
|
|
# Count entries by type
|
|
reflections = entries_by_type.get("task_reflection", 0)
|
|
learnings = entries_by_type.get("learning", 0)
|
|
struggles = entries_by_type.get("struggle", 0)
|
|
decisions = entries_by_type.get("decision_log", 0)
|
|
|
|
# Calculate struggle resolution rate
|
|
# (struggles with "Resolution" section / total struggles)
|
|
resolution_rate = 0.0
|
|
if struggles > 0:
|
|
result = await self.session.execute(
|
|
select(func.count(JournalEntryTable.id)).where(
|
|
and_(
|
|
JournalEntryTable.journal_id == journal.id,
|
|
JournalEntryTable.type == JournalEntryType.STRUGGLE,
|
|
JournalEntryTable.content.contains("## Resolution"),
|
|
)
|
|
)
|
|
)
|
|
resolved_count = result.scalar() or 0
|
|
resolution_rate = resolved_count / struggles
|
|
|
|
# Calculate learning frequency (learnings per day since first entry)
|
|
learning_frequency = 0.0
|
|
if journal.created_at and learnings > 0:
|
|
days_active = max(1, (datetime.now(UTC) - journal.created_at).days)
|
|
learning_frequency = learnings / days_active
|
|
|
|
# Simple sentiment trend based on recent entries
|
|
# (in production, would use NLP sentiment analysis)
|
|
sentiment_trend = "stable"
|
|
|
|
return GrowthMetrics(
|
|
total_reflections=reflections,
|
|
total_learnings=learnings,
|
|
total_struggles=struggles,
|
|
total_decisions=decisions,
|
|
struggle_resolution_rate=resolution_rate,
|
|
learning_frequency=learning_frequency,
|
|
sentiment_trend=sentiment_trend,
|
|
)
|
|
|
|
async def search_entries(
|
|
self,
|
|
agent_id: UUID,
|
|
query: str,
|
|
top_k: int = 5,
|
|
) -> list[JournalEntry]:
|
|
"""
|
|
Semantic search through an agent's journal entries.
|
|
|
|
Uses the Optimal API for RAG-based search.
|
|
|
|
Args:
|
|
agent_id: Agent whose journal to search
|
|
query: Natural language query
|
|
top_k: Number of results to return
|
|
|
|
Returns:
|
|
List of relevant journal entries
|
|
"""
|
|
try:
|
|
optimal = await self._get_optimal_service()
|
|
from roboco.services.optimal import IndexType, QueryContext
|
|
|
|
results = await optimal.search(
|
|
query=query,
|
|
context=QueryContext(
|
|
agent_id=agent_id,
|
|
index_types=[IndexType.JOURNALS],
|
|
),
|
|
top_k=top_k,
|
|
)
|
|
|
|
# Extract entry IDs from results and fetch full entries
|
|
entries = []
|
|
for result in results:
|
|
# Get entry_id from metadata (stored during indexing)
|
|
entry_id_str = result.metadata.get("entry_id")
|
|
if entry_id_str:
|
|
try:
|
|
entry_id = UUID(entry_id_str)
|
|
entry = await self.get_entry(entry_id)
|
|
if entry:
|
|
# Filter by agent_id to ensure we only
|
|
# return this agent's entries
|
|
journal = await self.get_journal(agent_id)
|
|
if journal and entry.journal_id == journal.id:
|
|
entries.append(entry)
|
|
except ValueError:
|
|
self.log.warning(
|
|
"Invalid entry_id in search result",
|
|
entry_id=entry_id_str,
|
|
)
|
|
|
|
self.log.debug(
|
|
"Journal search completed",
|
|
agent_id=str(agent_id),
|
|
query=query[:50],
|
|
results_found=len(entries),
|
|
)
|
|
return entries[:top_k]
|
|
|
|
except Exception as e:
|
|
self.log.error(
|
|
"Journal search failed",
|
|
agent_id=str(agent_id),
|
|
query=query[:50] if query else "empty",
|
|
error=str(e),
|
|
error_type=type(e).__name__,
|
|
)
|
|
return []
|
|
|
|
# =========================================================================
|
|
# GATEWAY (CHOREOGRAPHER) BACKFILL
|
|
#
|
|
# Existence checks the gateway uses to enforce tracing requirements
|
|
# (every transition demands a journal entry of the appropriate type).
|
|
# =========================================================================
|
|
|
|
async def _has_entry_of_type(
|
|
self,
|
|
agent_id: UUID,
|
|
task_id: UUID,
|
|
entry_type: JournalEntryType,
|
|
) -> bool:
|
|
"""True iff agent has a journal entry of `entry_type` for this task."""
|
|
query = (
|
|
select(func.count(JournalEntryTable.id))
|
|
.join(JournalTable, JournalEntryTable.journal_id == JournalTable.id)
|
|
.where(
|
|
JournalTable.agent_id == agent_id,
|
|
JournalEntryTable.task_id == task_id,
|
|
JournalEntryTable.type == entry_type,
|
|
)
|
|
)
|
|
result = await self.session.execute(query)
|
|
return (result.scalar() or 0) > 0
|
|
|
|
async def has_decision_for_task(self, agent_id: UUID, task_id: UUID) -> bool:
|
|
"""True iff a DECISION_LOG entry exists for (agent, task)."""
|
|
return await self._has_entry_of_type(
|
|
agent_id, task_id, JournalEntryType.DECISION_LOG
|
|
)
|
|
|
|
async def latest_decision_at(
|
|
self, agent_id: UUID, task_id: UUID
|
|
) -> datetime | None:
|
|
"""Return ``created_at`` of the most recent DECISION_LOG entry for
|
|
(agent, task), or ``None`` if no decision exists.
|
|
|
|
Backs the windowed-satisfaction variant of the PM-decision
|
|
tracing gate: the choreographer treats decisions older than
|
|
``settings.pm_decision_window_seconds`` as missing so PMs write a
|
|
fresh decision around each decision point.
|
|
"""
|
|
query = (
|
|
select(func.max(JournalEntryTable.created_at))
|
|
.join(JournalTable, JournalEntryTable.journal_id == JournalTable.id)
|
|
.where(
|
|
JournalTable.agent_id == agent_id,
|
|
JournalEntryTable.task_id == task_id,
|
|
JournalEntryTable.type == JournalEntryType.DECISION_LOG,
|
|
)
|
|
)
|
|
result = await self.session.execute(query)
|
|
return result.scalar()
|
|
|
|
async def has_note_for_task(self, agent_id: UUID, task_id: UUID) -> bool:
|
|
"""True iff a GENERAL (scope='note') entry exists for (agent, task).
|
|
|
|
Backs the JOURNAL_NOTE_AT_CLAIM tracing requirement on
|
|
i_will_work_on (pre-gateway parity P1: developers wrote a
|
|
work_log/note entry on every claim).
|
|
"""
|
|
return await self._has_entry_of_type(
|
|
agent_id, task_id, JournalEntryType.GENERAL
|
|
)
|
|
|
|
async def has_learning_for_task(self, agent_id: UUID, task_id: UUID) -> bool:
|
|
"""True iff a LEARNING entry exists for (agent, task)."""
|
|
return await self._has_entry_of_type(
|
|
agent_id, task_id, JournalEntryType.LEARNING
|
|
)
|
|
|
|
async def has_reflect_for_task(self, agent_id: UUID, task_id: UUID) -> bool:
|
|
"""True iff a TASK_REFLECTION entry exists for (agent, task)."""
|
|
return await self._has_entry_of_type(
|
|
agent_id, task_id, JournalEntryType.TASK_REFLECTION
|
|
)
|
|
|
|
async def has_struggle_for_task(self, agent_id: UUID, task_id: UUID) -> bool:
|
|
"""True iff a STRUGGLE entry exists for (agent, task)."""
|
|
return await self._has_entry_of_type(
|
|
agent_id, task_id, JournalEntryType.STRUGGLE
|
|
)
|
|
|
|
async def write_struggle(
|
|
self,
|
|
*,
|
|
agent_id: UUID,
|
|
task_id: UUID,
|
|
content: str,
|
|
) -> JournalEntry | None:
|
|
"""Write a STRUGGLE entry for (agent, task) with `content` as body.
|
|
|
|
Title is derived from the first line of content (truncated to 100
|
|
chars) so the entry has a meaningful summary in lists.
|
|
"""
|
|
first_line = content.strip().splitlines()[0] if content.strip() else "Struggle"
|
|
title = first_line[:100]
|
|
params = StruggleEntryParams(
|
|
title=title,
|
|
what_struggled=content,
|
|
attempted_solutions=[],
|
|
task_id=task_id,
|
|
)
|
|
return await self.add_struggle(agent_id, params)
|
|
|
|
async def write_entry(
|
|
self,
|
|
*,
|
|
agent_id: UUID,
|
|
title: str,
|
|
content: str,
|
|
scope: str = "note",
|
|
task_id: UUID | None = None,
|
|
) -> JournalEntry | None:
|
|
"""Gateway adapter — write a journal entry by `scope` string.
|
|
|
|
The gateway speaks in scope strings (`note`, `decision`, `reflect`,
|
|
`learning`, `struggle`) while the service stores entries by
|
|
`JournalEntryType` enum keyed off a `journal_id` (which the agent
|
|
doesn't carry). This adapter resolves both: maps scope to the enum,
|
|
looks up or creates the agent's journal, then delegates to
|
|
`create_entry(JournalEntryCreate(...))`.
|
|
|
|
Raises:
|
|
ValueError: If `scope` is not one of the supported gateway
|
|
scopes. Caller (gateway) validates the scope set before
|
|
reaching here, but the guard is kept defensive.
|
|
"""
|
|
entry_type = _SCOPE_TO_TYPE.get(scope)
|
|
if entry_type is None:
|
|
raise ValueError(
|
|
f"unknown scope {scope!r}; expected one of {sorted(_SCOPE_TO_TYPE)}"
|
|
)
|
|
journal = await self.get_or_create_journal(agent_id)
|
|
return await self.create_entry(
|
|
JournalEntryCreate(
|
|
journal_id=journal.id,
|
|
type=entry_type,
|
|
title=title,
|
|
content=content,
|
|
task_id=task_id,
|
|
)
|
|
)
|
|
|
|
|
|
def get_journal_service(db: AsyncSession) -> JournalService:
|
|
"""Factory function for JournalService."""
|
|
return JournalService(db)
|