Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
84 changes: 84 additions & 0 deletions .codex/skills/lycode-agent-core/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
name: lycode-agent-core
description: Use when changing or debugging ly-code turn execution, context assembly, model stream handling, tool rounds, compaction, branch summaries, or memory extraction hooks.
---

# ly-code Agent Core

## Core Rule

`lycode-agent-core` orchestrates a turn through ports. It should not know about concrete terminal UI, concrete provider protocols, or concrete tool implementations.

## Main Responsibilities

- Append the user message and assistant/tool result messages through session ports.
- Build model context from session and resource runtime.
- Stream provider events into internal assistant message blocks.
- Map assistant tool calls to `ToolUseRequest`.
- Execute tool rounds through `ToolRuntimePort`.
- Run micro-compaction and preflight compaction before model calls.
- Publish turn lifecycle events with stable fork points; runtime owns background memory consolidation after turn end.

## Key Code

- `lycode-agent-core/src/main/java/cn/lycode/agent/DefaultTurnExecutor.java`
- `lycode-agent-core/src/main/java/cn/lycode/agent/DefaultContextAssembler.java`
- `lycode-agent-core/src/main/java/cn/lycode/agent/AgentCoreRuntimePorts.java`
- `lycode-agent-core/src/main/java/cn/lycode/agent/AssistantStreamAccumulator.java`
- `lycode-agent-core/src/main/java/cn/lycode/agent/ToolCallMapper.java`
- `lycode-agent-core/src/main/java/cn/lycode/agent/AgentCoreExceptionHandler.java`
- `lycode-agent-core/src/main/java/cn/lycode/agent/compact/DefaultCompactionCoordinator.java`
- `lycode-agent-core/src/main/java/cn/lycode/agent/compact/DefaultCompactionPlanner.java`
- `lycode-agent-core/src/main/java/cn/lycode/agent/branch/AiBranchSummarizer.java`

## Turn Flow

1. `DefaultTurnExecutor.execute()` opens the session and optionally switches parent entry.
2. It rejects unsafe continuation when the current leaf is an assistant tool-call message.
3. It appends the user message.
4. `DefaultContextAssembler.build()` loads resources, builds system prompt, projects session context and estimates budget.
5. Tool micro-compaction and `CompactionCoordinator.preflight()` may rewrite the context view.
6. Provider stream events are accumulated and mirrored as message events.
7. Tool calls are mapped and executed through `ToolRuntimePort`.
8. Tool results are appended, context is rebuilt, and the model continues.
9. Completed, failed and aborted turns publish `TurnEndEvent`; successful long-enough turns are eligible for runtime background memory consolidation.

## Invariants

- `DefaultTurnExecutor` returns tool results in the same order as tool requests.
- Incomplete tool-call deltas end the turn with an error message.
- User abort produces `TurnStatus.ABORTED`; successful completion is required before runtime memory consolidation can trigger.
- `TurnEndEvent.leafEntryId` is the stable fork point for background memory consolidation; runtime listeners must not depend on the mutable current session leaf.
- Tool runtime cwd must match agent cwd.
- Skill mentions are injected by `DefaultContextAssembler` only when present in the request.
- Compaction should preserve valid API round structure; do not cut through an open tool call/result pair.

## Tests To Check

- `lycode-agent-core/src/test/java/cn/lycode/agent/DefaultTurnExecutorTest.java`
- `lycode-agent-core/src/test/java/cn/lycode/agent/DefaultTurnExecutorPermissionTest.java`
- `lycode-agent-core/src/test/java/cn/lycode/agent/DefaultContextAssemblerTest.java`
- `lycode-agent-core/src/test/java/cn/lycode/agent/AssistantStreamAccumulatorTest.java`
- `lycode-agent-core/src/test/java/cn/lycode/agent/ToolCallMapperTest.java`
- `lycode-agent-core/src/test/java/cn/lycode/agent/DefaultCompactionCoordinatorTest.java`
- `lycode-agent-core/src/test/java/cn/lycode/agent/DefaultCompactionPlannerTest.java`

## Before Editing

- Identify whether the behavior belongs in core or in a port implementation.
- Add tests around `TurnState`, emitted events and session entries, not only return text.
- For provider event changes, update accumulator tests.
- For tool-call changes, test malformed and complete tool deltas.
- For compaction changes, test API-round boundaries and prompt-too-long recovery.

## Common Changes

| Need | Likely Files |
| --- | --- |
| Change turn lifecycle | `DefaultTurnExecutor`, turn executor tests |
| Change context inputs | `DefaultContextAssembler`, resource/session tests |
| Change tool-call parsing | `ToolCallMapper`, accumulator tests |
| Change compaction trigger | `DefaultCompactionCoordinator`, planner tests |
| Change memory hook | `TurnEventPublisher`, `MemoryConsolidationTurnEndListener`, turn completion tests |

After using this Skill, reverse-check that no new dependency on TUI, provider implementation, or concrete tool classes was introduced.
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
---
name: lypi-architecture
description: Use when changing or reviewing ly-pi module boundaries, Maven module dependencies, contracts, runtime ports, event contracts, or cross-cutting architecture.
name: lycode-architecture
description: Use when changing or reviewing ly-code module boundaries, Maven module dependencies, contracts, runtime ports, event contracts, or cross-cutting architecture.
---

# ly-pi Architecture
# ly-code Architecture

## Core Rule

Keep module boundaries explicit and interface-first. Upper layers should depend on contracts and ports, not concrete UI, provider, or tool implementations.

## Applies To

Use this skill for architecture decisions, new modules, dependency changes, shared contracts, event model changes, or behavior that crosses `lypi-session`, `lypi-agent-core`, `lypi-tool`, `lypi-resource`, runtime, and transports.
Use this skill for architecture decisions, new modules, dependency changes, shared contracts, event model changes, or behavior that crosses `lycode-session`, `lycode-agent-core`, `lycode-tool`, `lycode-resource`, runtime, and transports.

Skip it for isolated implementation details inside a single module unless the change leaks across public contracts.

## Current Modules

The root `pom.xml` defines these Maven modules:

- `lypi-contracts`: shared records, ports, events, session entries, errors, security enums, TUI views, Skill and MCP contracts.
- `lypi-session`: append-only session storage, branch/replay queries, fork and child session creation.
- `lypi-agent-core`: turn execution, context assembly, stream accumulation, tool rounds, compaction, branch summaries.
- `lypi-ai`: provider adapters, model registry, request building, stream normalization and fallback.
- `lypi-tool`: tool registry/runtime, built-in tools, MCP adapter, permission gate integration and shell executors.
- `lypi-security`: policy engine, path safety, Bash normalization, risk analysis and rule matching.
- `lypi-resource`: context files, memory, Skill index, prompt templates, MCP config and system prompt construction.
- `lypi-runtime`: event bus, memory consolidation, subagent center, mailbox and process runner.
- `lypi-transport-headless`: headless subagent JSON protocol.
- `lypi-transport-tui`: JLine TUI, input loop, event reducer, renderer, slash commands and overlays.
- `lypi-boot`: Spring Boot assembly, config binding and startup modes.
- `lycode-contracts`: shared records, ports, events, session entries, errors, security enums, TUI views, Skill and MCP contracts.
- `lycode-session`: append-only session storage, branch/replay queries, fork and child session creation.
- `lycode-agent-core`: turn execution, context assembly, stream accumulation, tool rounds, compaction, branch summaries.
- `lycode-ai`: provider adapters, model registry, request building, stream normalization and fallback.
- `lycode-tool`: tool registry/runtime, built-in tools, MCP adapter, permission gate integration and shell executors.
- `lycode-security`: policy engine, path safety, Bash normalization, risk analysis and rule matching.
- `lycode-resource`: context files, memory, Skill index, prompt templates, MCP config and system prompt construction.
- `lycode-runtime`: event bus, memory consolidation, subagent center, mailbox and process runner.
- `lycode-transport-headless`: headless subagent JSON protocol.
- `lycode-transport-tui`: JLine TUI, input loop, event reducer, renderer, slash commands and overlays.
- `lycode-boot`: Spring Boot assembly, config binding and startup modes.

## Permission Architecture

- `PermissionRuntimeState` in `lypi-contracts` is the canonical cross-module permission state. It carries approval policy, active profile, legacy behavior and legacy mode compatibility.
- `PermissionRuntimeState` in `lycode-contracts` is the canonical cross-module permission state. It carries approval policy, active profile, legacy behavior and legacy mode compatibility.
- `PermissionMode` remains only for legacy constructors, old JSON, UI compatibility and fallback mapping. New cross-module contracts should include `PermissionRuntimeState`.
- `lypi-security` compiles permission profiles and evaluates filesystem/network/hard-safety policy.
- `lypi-tool` coordinates approval prompts, permission amendments, `request_permissions`, sandbox projection and tool execution.
- `lypi-session`, `lypi-agent-core`, `lypi-resource`, `lypi-runtime`, `lypi-transport-headless`, `lypi-transport-tui` and `lypi-boot` consume the same canonical runtime state instead of reinterpreting legacy modes.
- `lycode-security` compiles permission profiles and evaluates filesystem/network/hard-safety policy.
- `lycode-tool` coordinates approval prompts, permission amendments, `request_permissions`, sandbox projection and tool execution.
- `lycode-session`, `lycode-agent-core`, `lycode-resource`, `lycode-runtime`, `lycode-transport-headless`, `lycode-transport-tui` and `lycode-boot` consume the same canonical runtime state instead of reinterpreting legacy modes.
- Headless/subagent JSON writes `permissionRuntimeState` for new protocol and reads legacy `permissionMode` for compatibility.

## Invariants

- `lypi-contracts` is the shared boundary; other modules should not expose their internals as cross-module state.
- `lypi-agent-core` must not directly bind to TUI, concrete providers, or concrete tools. Use ports in `cn.lypi.contracts.runtime`.
- `lycode-contracts` is the shared boundary; other modules should not expose their internals as cross-module state.
- `lycode-agent-core` must not directly bind to TUI, concrete providers, or concrete tools. Use ports in `cn.lycode.contracts.runtime`.
- Transports adapt input/output and display. They should not own durable session or tool state.
- Session history is append-only JSONL; branch movement changes the leaf, not old entries.
- Permission runtime changes are represented by session entries and replayed into `SessionContext`; do not mutate historical entries to change permission state.
Expand All @@ -58,32 +58,32 @@ The root `pom.xml` defines these Maven modules:
- Background memory consolidation is best-effort and auditable: runtime records threshold/session/direct-write/coalesced states, boot runner runs preflight memory scan and injects the scan summary into the hidden settlement turn, then records post-turn lint diagnostics without blocking the main turn.
- Background memory consolidation must preserve the parent tool schema for prompt-cache prefix stability; restrict actual execution with a can-use-tool style runtime gate and memory write policy instead of filtering the visible tool snapshot.
- Memory lint is an automatic background diagnostic only; do not expose product slash commands or default user resources for manual `/memory-lint`.
- Product runtime Skill discovery is under `skills/` and `.ly-pi/skills/`; repository Codex knowledge under `.codex/skills/` is not a product resource root.
- Product runtime Skill discovery is under `skills/` and `.ly-code/skills/`; repository Codex knowledge under `.codex/skills/` is not a product resource root.

## Key Anchors

- `pom.xml`
- `README.md`
- `docs/permission-system-codex-alignment-design.md`
- `docs/permission-system-codex-alignment-plan.md`
- `lypi-contracts/src/main/java/cn/lypi/contracts/runtime/`
- `lypi-contracts/src/main/java/cn/lypi/contracts/event/`
- `lypi-contracts/src/main/java/cn/lypi/contracts/event/TurnEndEvent.java`
- `lypi-runtime/src/main/java/cn/lypi/runtime/memory/MemoryConsolidationTurnEndListener.java`
- `lypi-runtime/src/main/java/cn/lypi/runtime/memory/MemoryWriteDetector.java`
- `lypi-runtime/src/main/java/cn/lypi/runtime/memory/MemoryLintScanner.java`
- `lypi-runtime/src/main/java/cn/lypi/runtime/memory/MemoryPreflightScan.java`
- `lypi-boot/src/main/java/cn/lypi/boot/runtime/BootMemoryConsolidationRunner.java`
- `lypi-contracts/src/main/java/cn/lypi/contracts/security/PermissionRuntimeState.java`
- `lypi-contracts/src/main/java/cn/lypi/contracts/security/PermissionProfiles.java`
- `lypi-contracts/src/main/java/cn/lypi/contracts/session/PermissionRuntimeStateChangeEntry.java`
- `lypi-contracts/src/test/java/cn/lypi/contracts/ArchitectureBoundaryTest.java`
- `lypi-boot/src/main/java/cn/lypi/boot/LyPiApplication.java`
- `lycode-contracts/src/main/java/cn/lycode/contracts/runtime/`
- `lycode-contracts/src/main/java/cn/lycode/contracts/event/`
- `lycode-contracts/src/main/java/cn/lycode/contracts/event/TurnEndEvent.java`
- `lycode-runtime/src/main/java/cn/lycode/runtime/memory/MemoryConsolidationTurnEndListener.java`
- `lycode-runtime/src/main/java/cn/lycode/runtime/memory/MemoryWriteDetector.java`
- `lycode-runtime/src/main/java/cn/lycode/runtime/memory/MemoryLintScanner.java`
- `lycode-runtime/src/main/java/cn/lycode/runtime/memory/MemoryPreflightScan.java`
- `lycode-boot/src/main/java/cn/lycode/boot/runtime/BootMemoryConsolidationRunner.java`
- `lycode-contracts/src/main/java/cn/lycode/contracts/security/PermissionRuntimeState.java`
- `lycode-contracts/src/main/java/cn/lycode/contracts/security/PermissionProfiles.java`
- `lycode-contracts/src/main/java/cn/lycode/contracts/session/PermissionRuntimeStateChangeEntry.java`
- `lycode-contracts/src/test/java/cn/lycode/contracts/ArchitectureBoundaryTest.java`
- `lycode-boot/src/main/java/cn/lycode/boot/LyCodeApplication.java`

## Before Changing Architecture

- Check whether a contract already exists before adding a new dependency.
- Search imports to confirm dependency direction with `rg -n "import cn\\.lypi\\." <module>`.
- Search imports to confirm dependency direction with `rg -n "import cn\\.lycode\\." <module>`.
- Add or update a boundary test if a public dependency rule changes.
- Prefer adding a port or contract type over reaching into another module implementation.
- For permission work, keep `PermissionRuntimeState` in contracts and pass it through ports instead of adding module-local permission state shapes.
Expand All @@ -93,12 +93,12 @@ The root `pom.xml` defines these Maven modules:

| Change | Preferred Location |
| --- | --- |
| New shared data shape | `lypi-contracts` |
| New durable session fact | `lypi-contracts/src/main/java/cn/lypi/contracts/session/` plus `lypi-session` projector/store tests |
| New turn orchestration behavior | `lypi-agent-core` through runtime ports |
| New UI affordance | `lypi-transport-tui` consuming events or contracts |
| New tool execution capability | `lypi-tool` plus security/runtime port checks |
| New resource type | `lypi-resource` and prompt builder tests |
| New permission runtime field | `lypi-contracts` plus session/headless/boot compatibility tests |
| New shared data shape | `lycode-contracts` |
| New durable session fact | `lycode-contracts/src/main/java/cn/lycode/contracts/session/` plus `lycode-session` projector/store tests |
| New turn orchestration behavior | `lycode-agent-core` through runtime ports |
| New UI affordance | `lycode-transport-tui` consuming events or contracts |
| New tool execution capability | `lycode-tool` plus security/runtime port checks |
| New resource type | `lycode-resource` and prompt builder tests |
| New permission runtime field | `lycode-contracts` plus session/headless/boot compatibility tests |

After using this Skill, reverse-check one listed invariant against current code and report stale knowledge if found.
76 changes: 76 additions & 0 deletions .codex/skills/lycode-knowledge-maintenance/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
name: lycode-knowledge-maintenance
description: Use when maintaining ly-code project knowledge, Codex skills, module facts, corrections, or stale design notes under .codex/skills, docs/active, or project memory.
---

# ly-code Knowledge Maintenance

## Core Rule

Project knowledge is useful only when it matches current code. Treat existing notes as leads, not proof.

## Sources

Use this precedence when facts conflict:

1. Current source code and tests.
2. User instructions in `AGENTS.md` or the active conversation.
3. `docs/active/ly-code 最终总体设计书.md`.
4. `docs/active/subagent-current-design.md`.
5. `docs/deactive` only as history, never as current fact unless explicitly marked.

## Scope

Use this skill for:

- Adding or updating repository Codex skills under `.codex/skills/`.
- Correcting stale module knowledge, paths, call chains, or invariants.
- Auditing whether active docs still match code.
- Preparing durable project knowledge after repeated discoveries.

Do not use it for product runtime skills under `.ly-code/skills/` unless the task explicitly targets ly-code resource runtime behavior.

## Maintenance Procedure

1. Read the relevant current Skill before editing it.
2. Re-read the code paths and tests that justify the change.
3. Prefer small patches over rewrites.
4. If a doc and code disagree, record the code-backed fact and report the doc as stale.
5. Keep `SKILL.md` frontmatter limited to `name` and `description` for repository Codex skills.
6. Keep descriptions focused on trigger conditions, not workflow summaries.
7. Before finishing, run metadata and path checks for all touched skills.

## Reverse Verification

After using any ly-code project Skill:

- Check at least one listed code path still exists.
- Check whether the stated module boundary still matches imports and tests.
- If a fact is inconsistent, do not silently follow the stale Skill. Report a correction suggestion with the file path that proves it.

## Commit-Time Diff Check

Before staging or committing knowledge changes:

- Review `git diff -- .codex/skills`.
- Confirm no `.codex/history.jsonl`, `.codex/shell_snapshots/`, session logs, `target/`, `docs/`, `.worktrees/`, or `.ly-code/` runtime files are included by accident.
- If adding tracked knowledge, decide separately whether `.gitignore` needs a narrow exception. Do not change ignore rules as a side effect.

## Baseline Patrol

For broad audits, sample these anchors:

- Root module list: `pom.xml`.
- Architecture summary: `README.md`.
- Boundary tests: `lycode-contracts/src/test/java/cn/lycode/contracts/ArchitectureBoundaryTest.java`.
- Active docs: `docs/active/`.
- Module test directories matching the Skill being updated.

## Common Corrections

| Symptom | Action |
| --- | --- |
| Active doc mentions a capability that source removed | Mark the doc stale in your response and write the source-backed behavior in the Skill. |
| Source has a new tool or entry type missing from docs | Update the relevant Skill with the code path and test path. |
| Skill lists a missing path | Remove or replace the path after verifying with `rg --files` or `find`. |
| Skill description no longer triggers correctly | Rewrite only the description and keep body changes minimal. |
Loading
Loading