Skip to content

Commit f5704bc

Browse files
rcursoragent
andcommitted
refactor(time): centralize RFC-3339-UTC formatting; drop hand-rolled date math in forget
Phase A of the senior-engineer cleanup sweep. Two related wins: 1. forget.rs hand-rolled a Howard-Hinnant date algorithm + custom RFC-3339 formatter with the comment "so we don't pull in chrono here" — but chrono is already a workspace dependency of morph-core (see morph-core/Cargo.toml). 40 lines of duplicate civil-calendar logic deleted. 2. New morph-core/src/time.rs exposes a single now_rfc3339_utc() helper, re-exported as morph_core::now_rfc3339_utc. Twelve scattered chrono::Utc::now().to_rfc3339() / Utc::now().to_rfc3339() call sites across forget.rs, commit.rs (×4), record.rs (×2), merge.rs, merge_flow.rs, annotate.rs, revert.rs, run_breadcrumb.rs, and one test in extract.rs all funnel through it. Why this is worth it: - One grep target the day we want to swap precision, mock the clock for tests, or enforce a canonical sub-second policy. - Removes the only file in the repo that had its own civil-calendar arithmetic. (stash.rs still uses chrono::Utc::now() directly to get a DateTime<Utc> for next_stash_id; that's a legitimate use.) - forget.rs is brand new in v0.41.0; the timestamp format change (full-precision RFC-3339 instead of seconds-Z) is a no-op for any tombstone in the wild — there are none yet. Workspace tests still 1171 / 1187 passing, 16 ignored, 0 failed. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 65a640f commit f5704bc

11 files changed

Lines changed: 41 additions & 57 deletions

File tree

‎morph-core/src/annotate.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::objects::{Annotation, MorphObject};
44
use crate::store::{MorphError, ObjectType, Store};
5+
use crate::time::now_rfc3339_utc;
56
use crate::Hash;
6-
use chrono::Utc;
77
use std::collections::BTreeMap;
88

99
/// Create an Annotation object (caller puts it). Returns the object so store.put can be used.
@@ -14,7 +14,7 @@ pub fn create_annotation(
1414
data: BTreeMap<String, serde_json::Value>,
1515
author: Option<String>,
1616
) -> MorphObject {
17-
let timestamp = Utc::now().to_rfc3339();
17+
let timestamp = now_rfc3339_utc();
1818
let author = author.unwrap_or_else(|| "morph".to_string());
1919
MorphObject::Annotation(Annotation {
2020
target: target.to_string(),

‎morph-core/src/commit.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::objects::{Blob, Commit, CommitContributor, EvalContract, EvalSuite, HumanEdit, MorphObject};
44
use crate::store::{MorphError, Store};
5+
use crate::time::now_rfc3339_utc;
56
use crate::Hash;
6-
use chrono::Utc;
77
use std::collections::BTreeMap;
88
use std::path::Path;
99

@@ -282,7 +282,7 @@ pub fn create_commit(
282282
let parent_list: Vec<String> = resolve_head(store)?
283283
.map(|h| vec![h.to_string()])
284284
.unwrap_or_default();
285-
let timestamp = Utc::now().to_rfc3339();
285+
let timestamp = now_rfc3339_utc();
286286
let author = author.unwrap_or_else(|| "morph".to_string());
287287
let commit = MorphObject::Commit(Commit {
288288
tree: None,
@@ -380,7 +380,7 @@ pub fn create_tree_commit_with_provenance(
380380
let parent_list: Vec<String> = resolve_head(store)?
381381
.map(|h| vec![h.to_string()])
382382
.unwrap_or_default();
383-
let timestamp = Utc::now().to_rfc3339();
383+
let timestamp = now_rfc3339_utc();
384384
let author = author.unwrap_or_else(|| "morph".to_string());
385385

386386
let (contributors, env_constraints, evidence_refs) = match provenance {
@@ -659,7 +659,7 @@ pub fn create_merge_commit_with_retirement(
659659
let merged_contributors = merge_contributors(&head_commit, &other_commit);
660660

661661
let parents = vec![head_hash.to_string(), other_hash.to_string()];
662-
let timestamp = chrono::Utc::now().to_rfc3339();
662+
let timestamp = now_rfc3339_utc();
663663
let author = author.unwrap_or_else(|| "morph".to_string());
664664
let commit = MorphObject::Commit(Commit {
665665
tree: tree_hash,
@@ -741,7 +741,7 @@ pub fn rollup(
741741
};
742742

743743
let message = message.unwrap_or_else(|| format!("Rollup to {}", tip_hash));
744-
let timestamp = chrono::Utc::now().to_rfc3339();
744+
let timestamp = now_rfc3339_utc();
745745
let commit = MorphObject::Commit(Commit {
746746
tree: tip_commit.tree.clone(),
747747
pipeline: tip_commit.pipeline.clone(),

‎morph-core/src/extract.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ mod tests {
215215
}
216216

217217
fn store_session_run_with_reviewer(store: &dyn Store) -> (Hash, Hash) {
218-
let now = chrono::Utc::now().to_rfc3339();
218+
let now = crate::time::now_rfc3339_utc();
219219
let mut prompt_payload = BTreeMap::new();
220220
prompt_payload.insert("text".into(), serde_json::Value::String("Build feature X".into()));
221221
let mut response_payload = BTreeMap::new();

‎morph-core/src/forget.rs‎

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use crate::hash::Hash;
4141
use crate::objects::{MorphObject, Tombstone};
4242
use crate::store::{FsStore, MorphError, ObjectType};
43+
use crate::time::now_rfc3339_utc;
4344

4445
/// Identifies the result of a forget operation. Used by the CLI to
4546
/// surface a one-line confirmation to the operator.
@@ -171,7 +172,7 @@ pub fn forget_local(
171172
let tombstone = Tombstone {
172173
original_hash: target.to_string(),
173174
original_kind: kind.to_string(),
174-
forgotten_at: now_iso8601(),
175+
forgotten_at: now_rfc3339_utc(),
175176
actor: actor.to_string(),
176177
reason: reason.map(|s| s.to_string()),
177178
};
@@ -218,47 +219,6 @@ pub const RETROACTIVE_NOTE: &str =
218219
before the deletion. If a teammate previously pulled this hash, \
219220
ask them to fetch from the remote again or delete it by hand.";
220221

221-
fn now_iso8601() -> String {
222-
use std::time::{SystemTime, UNIX_EPOCH};
223-
let secs = SystemTime::now()
224-
.duration_since(UNIX_EPOCH)
225-
.map(|d| d.as_secs())
226-
.unwrap_or(0);
227-
chrono_strftime_rfc3339_utc(secs)
228-
}
229-
230-
/// Tiny self-contained RFC-3339 (UTC) formatter so we don't pull
231-
/// in `chrono` here. Equivalent of `chrono::DateTime::<Utc>::from_timestamp(secs, 0).format(...)`.
232-
fn chrono_strftime_rfc3339_utc(secs: u64) -> String {
233-
let days = (secs / 86400) as i64;
234-
let secs_of_day = secs % 86400;
235-
let h = secs_of_day / 3600;
236-
let m = (secs_of_day % 3600) / 60;
237-
let s = secs_of_day % 60;
238-
239-
let (y, mo, d) = days_to_ymd(days + 719468);
240-
format!(
241-
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
242-
y, mo, d, h, m, s
243-
)
244-
}
245-
246-
/// Howard Hinnant's date algorithm — converts days since
247-
/// 0000-03-01 (proleptic Gregorian) to a (year, month, day).
248-
fn days_to_ymd(days_since_epoch: i64) -> (i64, u32, u32) {
249-
let z = days_since_epoch;
250-
let era = if z >= 0 { z } else { z - 146096 } / 146097;
251-
let doe = (z - era * 146097) as u64;
252-
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
253-
let y = yoe as i64 + era * 400;
254-
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
255-
let mp = (5 * doy + 2) / 153;
256-
let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
257-
let m = if mp < 10 { (mp + 3) as u32 } else { (mp - 9) as u32 };
258-
let y = if m <= 2 { y + 1 } else { y };
259-
(y, m, d)
260-
}
261-
262222
trait ObjectTypeName {
263223
fn object_type_name(&self) -> &'static str;
264224
}

‎morph-core/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ pub mod ssh_proto;
3838
pub mod ssh_store;
3939
pub mod reference;
4040
pub mod forget;
41+
pub mod time;
4142

4243
pub use hash::{canonical_json, content_hash, content_hash_git, Hash};
4344
pub use objects::{CommitContributor, EvalContract, MorphObject, Tombstone};
4445
pub use forget::{
4546
apply_tombstone, commits_referencing, forget_local, kind_is_forgettable,
4647
ForgetReport, RETROACTIVE_NOTE,
4748
};
49+
pub use time::now_rfc3339_utc;
4850
#[allow(deprecated)]
4951
pub use store::GixStore;
5052
pub use store::{resolve_hash_prefix, resolve_revision, FsStore, MorphError, ObjectType, Store};

‎morph-core/src/merge.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::objects::{
1212
};
1313
use crate::policy::read_policy;
1414
use crate::store::{MorphError, ObjectType, Store};
15+
use crate::time::now_rfc3339_utc;
1516
use crate::Hash;
1617
use sha2::{Digest, Sha256};
1718
use std::collections::{BTreeMap, BTreeSet, VecDeque};
@@ -630,7 +631,7 @@ pub fn execute_merge(
630631
let merged_contributors = crate::commit::merge_contributors(&plan.head_commit, &plan.other_commit);
631632

632633
let parents = vec![plan.head_hash.to_string(), plan.other_hash.to_string()];
633-
let timestamp = chrono::Utc::now().to_rfc3339();
634+
let timestamp = now_rfc3339_utc();
634635
let author = author.unwrap_or_else(|| "morph".to_string());
635636
let morph_instance = repo_root
636637
.and_then(|r| crate::agent::read_instance_id(&r.join(".morph")).ok().flatten());

‎morph-core/src/merge_flow.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::path::Path;
2323
use crate::hash::Hash;
2424
use crate::pipemerge::NodeConflict;
2525
use crate::store::{MorphError, Store};
26+
use crate::time::now_rfc3339_utc;
2627
use std::collections::BTreeMap;
2728

2829
/// Options to [`start_merge`].
@@ -640,7 +641,7 @@ pub fn continue_merge(
640641
&morph_dir,
641642
opts.author.as_deref(),
642643
)?;
643-
let timestamp = chrono::Utc::now().to_rfc3339();
644+
let timestamp = now_rfc3339_utc();
644645
let contributors = crate::commit::merge_contributors(&head_commit, &other_commit);
645646

646647
// PR 6 stage C cycle 13: union evidence_refs from both parents

‎morph-core/src/record.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::identity::identity_pipeline;
44
use crate::objects::{AgentInfo, Blob, MorphObject, Run, RunEnvironment, Trace, TraceEvent};
55
use crate::store::{MorphError, Store};
6+
use crate::time::now_rfc3339_utc;
67
use crate::Hash;
78
use std::collections::BTreeMap;
89
use std::path::{Path, PathBuf};
@@ -97,7 +98,7 @@ pub fn record_conversation(
9798
model_name: Option<&str>,
9899
agent_id: Option<&str>,
99100
) -> Result<Hash, MorphError> {
100-
let now = chrono::Utc::now().to_rfc3339();
101+
let now = now_rfc3339_utc();
101102

102103
let events: Vec<TraceEvent> = messages
103104
.iter()
@@ -181,7 +182,7 @@ pub fn record_eval_run(
181182
captured_stdout: Option<&str>,
182183
exit_code: Option<i32>,
183184
) -> Result<Hash, MorphError> {
184-
let now = chrono::Utc::now().to_rfc3339();
185+
let now = now_rfc3339_utc();
185186

186187
let mut payload: BTreeMap<String, serde_json::Value> = BTreeMap::new();
187188
payload.insert(

‎morph-core/src/revert.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use crate::commit::resolve_head;
77
use crate::objects::{Commit, EvalContract, MorphObject};
88
use crate::store::{MorphError, Store};
9+
use crate::time::now_rfc3339_utc;
910
use crate::tree::empty_tree_hash;
1011
use crate::Hash;
11-
use chrono::Utc;
1212
use std::collections::BTreeMap;
1313

1414
/// Create a revert commit that undoes the given commit's changes.
@@ -48,7 +48,7 @@ pub fn revert_commit(
4848
pipeline: target.pipeline.clone(),
4949
parents: vec![head.to_string()],
5050
message: format!("Revert \"{}\"", target.message),
51-
timestamp: Utc::now().to_rfc3339(),
51+
timestamp: now_rfc3339_utc(),
5252
author: author.unwrap_or_else(|| target.author.clone()),
5353
contributors: None,
5454
eval_contract: EvalContract {

‎morph-core/src/run_breadcrumb.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub fn record_last_run(
118118
run: run_hash.to_string(),
119119
head,
120120
index_fingerprint: fingerprint_index(&index),
121-
recorded_at: chrono::Utc::now().to_rfc3339(),
121+
recorded_at: crate::time::now_rfc3339_utc(),
122122
};
123123
write_last_run(morph_dir, &last)?;
124124
Ok(last)

0 commit comments

Comments
 (0)