Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore: files changed crates/openhuman-core/src/platform/cost/tracker.rs,crates/openhuman-core/src/pl

Steven Enamakel0d5b9864b373
diff --git a/crates/openhuman-core/src/platform/cost/tracker.rs b/crates/openhuman-core/src/platform/cost/tracker.rs
index 3008191d7c..16e5c09402 100644
--- a/crates/openhuman-core/src/platform/cost/tracker.rs
+++ b/crates/openhuman-core/src/platform/cost/tracker.rs
@@ -492,6 +492,7 @@ impl CostStorage {
F: FnMut(CostRecord),
{
if let Some(docs) = super::tracker_documents::current()? {
+ self.import_legacy_once(&docs)?;
for record in docs.all()? {
if !is_legacy_host_duplicate(&record) {
on_record(record);
@@ -538,6 +539,23 @@ impl CostStorage {
Ok(())
}
+ /// The workspace's JSONL ledger predates the backend and belongs to the
+ /// single-user (`local`) scope; import it into that scope once.
+ fn import_legacy_once(&self, docs: &super::tracker_documents::CostDocs) -> Result<()> {
+ static DONE: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());
+ if crate::storage::current_scope().ok() != Some(crate::storage::Scope::local()) {
+ return Ok(());
+ }
+ {
+ let mut done = DONE.lock();
+ if done.contains(&self.path) {
+ return Ok(());
+ }
+ done.push(self.path.clone());
+ }
+ docs.import_legacy(&self.path).map(|_| ())
+ }
+
fn rebuild_aggregates(&mut self, day: NaiveDate, year: i32, month: u32) -> Result<()> {
let mut daily_cost = 0.0;
let mut monthly_cost = 0.0;
diff --git a/crates/openhuman-core/src/platform/cost/tracker_documents.rs b/crates/openhuman-core/src/platform/cost/tracker_documents.rs
index b693cc2cb8..1b1b0ad790 100644
--- a/crates/openhuman-core/src/platform/cost/tracker_documents.rs
+++ b/crates/openhuman-core/src/platform/cost/tracker_documents.rs
@@ -78,6 +78,33 @@ impl CostDocs {
})
}
+ /// Imports a legacy `state/costs.jsonl` into an empty ledger, then renames
+ /// it to `costs.jsonl.migrated` so it is not read twice. Returns how many
+ /// records were imported. Malformed lines are skipped, as readers skip them.
+ pub(super) fn import_legacy(&self, path: &std::path::Path) -> Result<usize> {
+ if !path.exists() || !self.all()?.is_empty() {
+ return Ok(0);
+ }
+ let raw = std::fs::read_to_string(path)
+ .with_context(|| format!("read legacy cost ledger {}", path.display()))?;
+ let mut imported = 0;
+ for line in raw.lines().map(str::trim).filter(|line| !line.is_empty()) {
+ match serde_json::from_str::<CostRecord>(line) {
+ Ok(record) => {
+ self.add(&record)?;
+ imported += 1;
+ }
+ Err(error) => log::warn!("[cost::tracker] legacy import: skipping line: {error}"),
+ }
+ }
+ let migrated = path.with_extension("jsonl.migrated");
+ if let Err(error) = std::fs::rename(path, &migrated) {
+ log::warn!("[cost::tracker] legacy import: could not rename ledger: {error}");
+ }
+ log::debug!("[cost::tracker] legacy import: imported={imported}");
+ Ok(imported)
+ }
+
/// Every record, oldest first. Documents that no longer parse are skipped
/// with a warning, as malformed ledger lines are.
pub(super) fn all(&self) -> Result<Vec<CostRecord>> {
diff --git a/crates/openhuman-core/src/platform/cost/tracker_documents_tests.rs b/crates/openhuman-core/src/platform/cost/tracker_documents_tests.rs
index 1c8e4b06ae..e947c7e6fd 100644
--- a/crates/openhuman-core/src/platform/cost/tracker_documents_tests.rs
+++ b/crates/openhuman-core/src/platform/cost/tracker_documents_tests.rs
@@ -48,3 +48,27 @@ fn scopes_do_not_see_each_others_costs() {
assert_eq!(alice.all().unwrap().len(), 1);
assert!(bob.all().unwrap().is_empty());
}
+
+#[test]
+fn a_legacy_jsonl_ledger_is_imported_once_and_set_aside() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("costs.jsonl");
+ let first = record("a/model", 0.5, 30);
+ let second = record("b/model", 0.25, 10);
+ std::fs::write(
+ &path,
+ format!(
+ "{}\n\nnot json\n{}\n",
+ serde_json::to_string(&first).unwrap(),
+ serde_json::to_string(&second).unwrap()
+ ),
+ )
+ .unwrap();
+ let docs = docs_in(&MemoryStorage::new(), "local");
+ assert_eq!(docs.import_legacy(&path).unwrap(), 2);
+ assert_eq!(docs.all().unwrap().len(), 2);
+ assert!(!path.exists());
+ assert!(dir.path().join("costs.jsonl.migrated").exists());
+ // Nothing left to import, and a ledger that already has records is not imported over.
+ assert_eq!(docs.import_legacy(&path).unwrap(), 0);
+}
diff --git a/scripts/__tests__/storage-bypass.test.mjs b/scripts/__tests__/storage-bypass.test.mjs
index 3c90710a70..b42ec82b15 100644
--- a/scripts/__tests__/storage-bypass.test.mjs
+++ b/scripts/__tests__/storage-bypass.test.mjs
@@ -216,3 +216,13 @@ test("a call split across lines is still found", () => {
],
);
});
+
+test("a verbatim copy of an allowlisted line is still a finding", () => {
+ const file = `${SRC}/security/approval/store.rs`;
+ const line = "let conn = Connection::open(&db_path).with_context(|| {";
+ const kept = withoutAllowed(file, scan(file, `${line}\n});\n${line}\n});\n`));
+ assert.deepEqual(
+ kept.map((f) => [f.line, f.occurrence]),
+ [[3, 2]],
+ );
+});
diff --git a/scripts/ci/check-storage-bypass.mjs b/scripts/ci/check-storage-bypass.mjs
index 3938ba8f2c..e8102ecdda 100755
--- a/scripts/ci/check-storage-bypass.mjs
+++ b/scripts/ci/check-storage-bypass.mjs
@@ -149,10 +149,12 @@ export const ALLOW = new Map(
]),
);
-/** Whether `finding` is the allowlisted site of its file. */
+/** Whether `finding` is the allowlisted site of its file (its first occurrence). */
export function allowed(rel, finding) {
const entry = ALLOW.get(rel);
- return entry?.rule === finding.rule && entry.site === finding.text;
+ return (
+ entry?.rule === finding.rule && entry.site === finding.text && finding.occurrence === 1
+ );
}
/** Drops the allowlisted site from `rel`'s findings; anything else stays. */
diff --git a/scripts/ci/storage-bypass-baseline.json b/scripts/ci/storage-bypass-baseline.json
index 58d1cd7055..b31c8dba3f 100644
--- a/scripts/ci/storage-bypass-baseline.json
+++ b/scripts/ci/storage-bypass-baseline.json
@@ -9,14 +9,14 @@
{
"rule": "json-write",
"path": "crates/openhuman-core/src/agent/artifacts/store.rs",
- "line": 122,
+ "line": 167,
"text": "tokio::fs::write(&tmp_path, json).await.map_err(|e| {",
"occurrence": 1
},
{
"rule": "json-write",
"path": "crates/openhuman-core/src/agent/artifacts/store.rs",
- "line": 295,
+ "line": 364,
"text": "tokio::fs::write(&args_path, json)",
"occurrence": 1
},