Agentic Sessions and Protocols ⏱ 9 min read 📁 examples/09_memory_and_todos.mjs

Lesson 17: Todo and Task State Management

Empower agents to plan, track, and execute multi-step initiatives using structured todos with opt-in history compaction and loss markers.

1. Why This Feature Exists

Long-running agents often drift or lose track of subtasks. Attaching a TodoComponent gives the agent an explicit checklist that it updates during execution while preventing context window explosion through compaction.

2. Key Symbols and Exports

session.withTodos(todoStore)
Registers todo management tools (add_todo, update_todo, list_todos) on the session.
Todo Lifecycle
pending -> in_progress -> completed | failed
History Compaction
Folds intermediate task chatter into a concise summary once tasks complete.
Loss Marker Injection
Explicitly flags pruned conversation context to keep turn bookkeeping honest.

3. Implementation Walkthrough

import {
    ProviderType,
    resolveProvider,
    initializeClient,
    loadCredentials,
    getDefaultModel
} from 'merci-sdk';

const PROVIDER = resolveProvider();
const { client } = await initializeClient({ provider: PROVIDER, options: loadCredentials({ provider: PROVIDER }) });

const session = client.session(getDefaultModel({ provider: PROVIDER }))
    .withTodos();

const prompt = `
We need to refactor our user authentication module:
1. Audit current bcrypt password hashing.
2. Draft a migration script to Argon2id.
3. Update unit test suite.
Track progress using your todo tools.
`;

const result = await session.run(prompt);
console.log('Result:', result);

4. Wire and Telemetry Mechanics

The SDK injects the current todo list state into the system message. Tool invocations for add_todo, update_todo, etc. update the internal store and emit 'todo:update' telemetry events.

5. Run the Example

Execute the runnable example directly from the repository root:

node examples/09_memory_and_todos.mjs

Read the full example: examples/09_memory_and_todos.mjs →

Expected Output: Observes the agent creating tasks, transitioning items to in_progress and completed, and outputting the final consolidated deliverable.