File size: 5,291 Bytes
afa0cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::path::Path;

use anyhow::Result;
use codex_state::StateRuntime;
use codex_utils_absolute_path::test_support::PathExt;
use predicates::str::contains;
use tempfile::TempDir;

fn codex_command(codex_home: &Path) -> Result<assert_cmd::Command> {
    let mut cmd = assert_cmd::Command::new(codex_utils_cargo_bin::cargo_bin("codex")?);
    cmd.env("CODEX_HOME", codex_home);
    Ok(cmd)
}

#[tokio::test]
async fn debug_clear_memories_resets_state_and_removes_memory_dir() -> Result<()> {
    let codex_home = TempDir::new()?;
    let sqlite = codex_state::SqliteConfig::new_for_testing(codex_home.path().abs());
    let runtime = StateRuntime::init(sqlite.clone(), "test-provider".to_string()).await?;
    runtime.close().await;

    let thread_id = "00000000-0000-0000-0000-000000000123";
    let db_path = sqlite.state_db_path();
    let pool = sqlite.open_read_write_pool(&db_path).await?;
    let memories_db_path = sqlite.memories_db_path();
    let memories_pool = sqlite.open_read_write_pool(&memories_db_path).await?;

    sqlx::query(
        r#"
INSERT INTO threads (
    id,
    rollout_path,
    created_at,
    updated_at,
    source,
    agent_nickname,
    agent_role,
    model_provider,
    cwd,
    cli_version,
    title,
    sandbox_policy,
    approval_mode,
    tokens_used,
    first_user_message,
    archived,
    archived_at,
    git_sha,
    git_branch,
    git_origin_url,
    memory_mode
) VALUES (?, ?, 1, 1, 'cli', NULL, NULL, 'test-provider', ?, '', '', 'read-only', 'on-request', 0, '', 0, NULL, NULL, NULL, NULL, 'enabled')
        "#,
    )
    .bind(thread_id)
    .bind(codex_home.path().join("session.jsonl").display().to_string())
    .bind(codex_home.path().display().to_string())
    .execute(&pool)
    .await?;

    sqlx::query(
        r#"
INSERT INTO stage1_outputs (
    thread_id,
    source_updated_at,
    raw_memory,
    rollout_summary,
    generated_at,
    rollout_slug,
    usage_count,
    last_usage,
    selected_for_phase2,
    selected_for_phase2_source_updated_at
) VALUES (?, 1, 'raw', 'summary', 1, NULL, 0, NULL, 0, NULL)
        "#,
    )
    .bind(thread_id)
    .execute(&memories_pool)
    .await?;

    sqlx::query(
        r#"
INSERT INTO jobs (
    kind,
    job_key,
    status,
    worker_id,
    ownership_token,
    started_at,
    finished_at,
    lease_until,
    retry_at,
    retry_remaining,
    last_error,
    input_watermark,
    last_success_watermark
) VALUES
    ('memory_stage1', ?, 'completed', NULL, NULL, NULL, NULL, NULL, NULL, 3, NULL, NULL, 1),
    ('memory_consolidate_global', 'global', 'completed', NULL, NULL, NULL, NULL, NULL, NULL, 3, NULL, NULL, 1)
        "#,
    )
    .bind(thread_id)
    .execute(&memories_pool)
    .await?;

    let memory_root = codex_home.path().join("memories");
    std::fs::create_dir_all(&memory_root)?;
    std::fs::write(memory_root.join("memory_summary.md"), "stale memory")?;
    pool.close().await;
    memories_pool.close().await;

    let mut cmd = codex_command(codex_home.path())?;
    cmd.args(["debug", "clear-memories"])
        .assert()
        .success()
        .stdout(contains("Cleared memory state"));

    let pool = sqlite.open_read_write_pool(&memories_db_path).await?;
    let stage1_outputs_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM stage1_outputs")
        .fetch_one(&pool)
        .await?;
    assert_eq!(stage1_outputs_count, 0);

    let memory_jobs_count: i64 = sqlx::query_scalar(
        "SELECT COUNT(*) FROM jobs WHERE kind = 'memory_stage1' OR kind = 'memory_consolidate_global'",
    )
    .fetch_one(&pool)
    .await?;
    assert_eq!(memory_jobs_count, 0);
    assert!(memory_root.exists());
    assert_eq!(std::fs::read_dir(memory_root)?.count(), 0);
    pool.close().await;

    Ok(())
}

#[tokio::test]
async fn debug_clear_memories_resets_memories_db_without_state_db() -> Result<()> {
    let codex_home = TempDir::new()?;
    let sqlite = codex_state::SqliteConfig::new_for_testing(codex_home.path().abs());
    let runtime = StateRuntime::init(sqlite.clone(), "test-provider".to_string()).await?;
    runtime.close().await;

    let db_path = sqlite.state_db_path();
    let memories_db_path = sqlite.memories_db_path();
    let memories_pool = sqlite.open_read_write_pool(&memories_db_path).await?;

    sqlx::query(
        r#"
INSERT INTO stage1_outputs (
    thread_id,
    source_updated_at,
    raw_memory,
    rollout_summary,
    generated_at,
    rollout_slug,
    usage_count,
    last_usage,
    selected_for_phase2,
    selected_for_phase2_source_updated_at
) VALUES ('00000000-0000-0000-0000-000000000123', 1, 'raw', 'summary', 1, NULL, 0, NULL, 0, NULL)
        "#,
    )
    .execute(&memories_pool)
    .await?;

    memories_pool.close().await;
    std::fs::remove_file(&db_path)?;

    let mut cmd = codex_command(codex_home.path())?;
    cmd.args(["debug", "clear-memories"])
        .assert()
        .success()
        .stdout(contains("Cleared memory state"));

    let pool = sqlite.open_read_write_pool(&memories_db_path).await?;
    let stage1_outputs_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM stage1_outputs")
        .fetch_one(&pool)
        .await?;
    assert_eq!(stage1_outputs_count, 0);
    pool.close().await;
    assert!(!db_path.exists());

    Ok(())
}