Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(import): keep Notion imports out of Home by keeping their Notion dates (#8897)

Jacob Beckerman499197f97eb7
diff --git a/crates/ai_tools/AGENTS.md b/crates/ai_tools/AGENTS.md
index 7c9843ba1f..266d84ad22 100644
--- a/crates/ai_tools/AGENTS.md
+++ b/crates/ai_tools/AGENTS.md
@@ -24,9 +24,13 @@ execution, display) that don't belong to a single domain.
user), sharing its document/task paths with `EntityCreator`; Notion bodies
go through `DocumentCreator::create_notion_markdown`. `share_with_team`
shares a folder through `ProjectService::edit_project` (edit level) and a
- document through `DocumentService::set_team_share`; Notion tasks are created
- unshared and reached through their shared folder, while single tracked tasks
- (Linear) stay team-shared at creation.
+ document through `DocumentService::set_team_share`. Notion pages and tasks
+ are created with their page's dates (`NewDocumentMetadata::created_at` /
+ `updated_at`), and folders through
+ `ProjectService::create_project_dated_by_contents`, so the copy keeps
+ Notion's dates. Notion tasks are created unshared and reached through their
+ shared folder, while single tracked tasks (Linear) stay team-shared at
+ creation.
- `src/import_jobs.rs`: `ImportJobs`, the shutdown token and tracker every
host passes to `build_tool_service_context_from_env` (or its own import
service) and calls `hand_over` on at shutdown, so running Notion imports
diff --git a/crates/ai_tools/src/import_trackers.rs b/crates/ai_tools/src/import_trackers.rs
index de9f4d3064..2df421f52c 100644
--- a/crates/ai_tools/src/import_trackers.rs
+++ b/crates/ai_tools/src/import_trackers.rs
@@ -60,6 +60,7 @@ impl ToolEntityCreator {
ImportedContent::Markdown(markdown.to_string()),
properties,
None,
+ None,
shared,
)
.await;
diff --git a/crates/ai_tools/src/import_workspace.rs b/crates/ai_tools/src/import_workspace.rs
index 8ce3af23a7..a2f48313f7 100644
--- a/crates/ai_tools/src/import_workspace.rs
+++ b/crates/ai_tools/src/import_workspace.rs
@@ -14,7 +14,7 @@ use entity_access::domain::models::{EditAccessLevel, OwnerAccessLevel, ViewAcces
use entity_access::domain::ports::EntityAccessService as _;
use import::domain::ports::{
ImportedDocumentProperties, ImportedTaskProperties, NewDatabase, NotionLinks, SharedEntity,
- TeamMember,
+ SourceDates, TeamMember,
};
use macro_user_id::user_id::MacroUserIdStr;
use model::project::request::{CreateProjectRequest, PatchProjectRequestV2};
@@ -155,7 +155,7 @@ impl import::domain::ports::WorkspaceWriter for ToolEntityCreator {
}
let project = self
.project_service
- .create_project(
+ .create_project_dated_by_contents(
&Self::creation_principal(user),
CreateProjectRequest {
name: name.to_string(),
@@ -177,6 +177,7 @@ impl import::domain::ports::WorkspaceWriter for ToolEntityCreator {
links: &NotionLinks,
properties: &ImportedDocumentProperties,
folder: Option<Uuid>,
+ dates: Option<SourceDates>,
) -> anyhow::Result<String> {
let created = self
.create_imported_document(
@@ -186,6 +187,7 @@ impl import::domain::ports::WorkspaceWriter for ToolEntityCreator {
self::notion_markdown(notion_markdown, links),
properties,
folder,
+ dates,
)
.await;
already_created(created, id)
@@ -201,6 +203,7 @@ impl import::domain::ports::WorkspaceWriter for ToolEntityCreator {
links: &NotionLinks,
properties: &ImportedTaskProperties,
folder: Option<Uuid>,
+ dates: Option<SourceDates>,
) -> anyhow::Result<String> {
let created = self
.create_imported_task(
@@ -210,6 +213,7 @@ impl import::domain::ports::WorkspaceWriter for ToolEntityCreator {
ImportedContent::Notion(self::notion_markdown(notion_markdown, links)),
properties,
folder,
+ dates,
// A shared task list shares its folder, which reaches its tasks.
false,
)
diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rs
index 87a286af5e..ddae90aaae 100644
--- a/crates/ai_tools/src/tool_context.rs
+++ b/crates/ai_tools/src/tool_context.rs
@@ -1561,12 +1561,18 @@ impl ToolEntityCreator {
content: ImportedContent,
subtype: documents::domain::create::MarkdownSubtype,
folder: Option<uuid::Uuid>,
+ dates: Option<import::domain::ports::SourceDates>,
) -> anyhow::Result<String> {
use std::str::FromStr as _;
let mut metadata = documents::domain::create::NewDocumentMetadata::builder(name);
if let Some(id) = id {
metadata = metadata.id(id);
}
+ if let Some(dates) = dates {
+ metadata = metadata
+ .created_at(dates.created)
+ .updated_at(dates.last_edited);
+ }
if let Some(folder) = folder {
self.authorize_folder(user, folder).await?;
metadata = metadata.project_id(folder);
@@ -1645,7 +1651,7 @@ impl ToolEntityCreator {
/// `share_with_team`, then apply its properties best-effort.
#[expect(
clippy::too_many_arguments,
- reason = "a task carries its id, content, properties, folder and sharing"
+ reason = "a task carries its id, content, properties, folder, dates and sharing"
)]
pub(crate) async fn create_imported_task(
&self,
@@ -1655,6 +1661,7 @@ impl ToolEntityCreator {
content: ImportedContent,
properties: &import::domain::ports::ImportedTaskProperties,
folder: Option<uuid::Uuid>,
+ dates: Option<import::domain::ports::SourceDates>,
share_with_team: bool,
) -> anyhow::Result<String> {
// Same team resolution as the CreateDocument tool, so imported tasks
@@ -1667,15 +1674,19 @@ impl ToolEntityCreator {
team_id,
};
let task_id = self
- .create_doc(user, id, name, content, subtype, folder)
+ .create_doc(user, id, name, content, subtype, folder, dates)
.await?;
self.apply_task_properties(user, &task_id, properties, &roster)
.await;
Ok(task_id)
}
- /// Create an imported markdown document, in `folder` when given, then
- /// apply its properties best-effort.
+ /// Create an imported markdown document, in `folder` when given and with
+ /// its source's `dates` when known, then apply its properties best-effort.
+ #[expect(
+ clippy::too_many_arguments,
+ reason = "an imported document carries its id, name, content, properties, folder and dates"
+ )]
pub(crate) async fn create_imported_document(
&self,
user: &MacroUserIdStr<'static>,
@@ -1684,6 +1695,7 @@ impl ToolEntityCreator {
notion: documents::domain::notion_markdown::NotionMarkdown,
properties: &import::domain::ports::ImportedDocumentProperties,
folder: Option<uuid::Uuid>,
+ dates: Option<import::domain::ports::SourceDates>,
) -> anyhow::Result<String> {
let document_id = self
.create_doc(
@@ -1693,6 +1705,7 @@ impl ToolEntityCreator {
ImportedContent::Notion(notion),
documents::domain::create::MarkdownSubtype::Note,
folder,
+ dates,
)
.await?;
let access = match self
@@ -1843,6 +1856,7 @@ impl import::domain::ports::EntityCreator for ToolEntityCreator {
ImportedContent::Markdown(markdown.to_string()),
properties,
None,
+ None,
// Single tracked tasks (Linear) are shared with the team, as
// tasks the user makes are.
true,
@@ -1865,6 +1879,7 @@ impl import::domain::ports::EntityCreator for ToolEntityCreator {
crate::import_workspace::notion_markdown(notion_markdown, links),
properties,
None,
+ None,
)
.await
}
diff --git a/crates/import/AGENTS.md b/crates/import/AGENTS.md
index 01027a33ca..1e0458ed0c 100644
--- a/crates/import/AGENTS.md
+++ b/crates/import/AGENTS.md
@@ -33,7 +33,12 @@ undecided rows as `deciding`, and the write cannot start until the run is
`ready`. A resumed plan stages the tree again, which is idempotent. On
approval the harness starts the write:
folders mirroring the page tree, documents, databases with rows, row bodies and
-relations, and task lists as Macro tasks. The Notion run row tracks it
+relations, and task lists as Macro tasks. Each page, row body and task is
+created with its Notion page's created and last-edited times (`SourceDates`),
+and folders are created dated by what is written into them
+(`ProjectService::create_project_dated_by_contents`), so an import of old
+pages stays out of Home, which lists documents and folders by `updatedAt`.
+The Notion run row tracks it
(`running` → `ready` → `importing` → `completed`/`failed`/`dismissed`).
Every plan carries the user's `sharing` (`team`, `personal`, or `ai`, where
diff --git a/crates/import/src/domain/ports.rs b/crates/import/src/domain/ports.rs
index a755dbc356..1fc427c81f 100644
--- a/crates/import/src/domain/ports.rs
+++ b/crates/import/src/domain/ports.rs
@@ -8,7 +8,7 @@ use super::models::{
};
use agent_session::domain::model::AgentSessionId;
use agent_session::domain::notifications::PendingNotification;
-use import_notion::domain::model::NotionId;
+use import_notion::domain::model::{NotionId, NotionPage};
use macro_user_id::user_id::MacroUserIdStr;
use std::collections::HashMap;
use std::time::Duration;
@@ -386,6 +386,25 @@ pub struct ImportedTaskProperties {
pub assignee_email: Option<String>,
}
+/// When a source page was created and last edited, so its copy sorts by
+/// when it was written there rather than by when it was copied in.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub struct SourceDates {
+ /// When the page was created.
+ pub created: chrono::DateTime<chrono::Utc>,
+ /// When the page was last edited.
+ pub last_edited: chrono::DateTime<chrono::Utc>,
+}
+
+impl From<&NotionPage> for SourceDates {
+ fn from(page: &NotionPage) -> Self {
+ Self {
+ created: page.created,
+ last_edited: page.last_edited,
+ }
+ }
+}
+
/// Properties recovered from a Notion page and attached to the imported
/// Macro document. The creator applies these best-effort after the document
/// exists, so unsupported or invalid values never prevent the body import.
@@ -591,7 +610,8 @@ pub trait WorkspaceWriter: Send + Sync + 'static {
relations: &TrackerTaskRelations,
) -> impl Future<Output = anyhow::Result<()>> + Send;
- /// Create a folder, inside `parent` when given. Returns the folder id.
+ /// Create a folder, inside `parent` when given, dated by what is written
+ /// into it rather than by when it was made. Returns the folder id.
fn create_folder(
&self,
user: &MacroUserIdStr<'static>,
@@ -608,9 +628,10 @@ pub trait WorkspaceWriter: Send + Sync + 'static {
/// Create document `id` from a Notion page (Notion's enhanced markdown,
/// references resolved through `links`) inside `folder` when given,
- /// applying `properties` best-effort. Returns the document id. Safe to
- /// repeat: when document `id` exists already (an interrupted earlier
- /// attempt), it is left as it is and its id returned.
+ /// applying `properties` best-effort and keeping the page's `dates` when
+ /// known. Returns the document id. Safe to repeat: when document `id`
+ /// exists already (an interrupted earlier attempt), it is left as it is
+ /// and its id returned.
#[allow(clippy::too_many_arguments)]
fn create_document(
&self,
@@ -621,14 +642,16 @@ pub trait WorkspaceWriter: Send + Sync + 'static {
links: &NotionLinks,
properties: &ImportedDocumentProperties,
folder: Option<Uuid>,
+ dates: Option<SourceDates>,
) -> impl Future<Output = anyhow::Result<String>> + Send;
/// Create task `id` from a Notion page (as for [`Self::create_document`],
/// and as safe to repeat) inside `folder` when given, applying
- /// `properties` best-effort. Returns the task id.
+ /// `properties` best-effort and keeping the page's `dates`. Returns the
+ /// task id.
#[expect(
clippy::too_many_arguments,
- reason = "a task carries its id, content, links, properties and folder"
+ reason = "a task carries its id, content, links, properties, folder and dates"
)]
fn create_task(
&self,
@@ -639,6 +662,7 @@ pub trait WorkspaceWriter: Send + Sync + 'static {
links: &NotionLinks,
properties: &ImportedTaskProperties,
folder: Option<Uuid>,
+ dates: Option<SourceDates>,
) -> impl Future<Output = anyhow::Result<String>> + Send;
/// Create an empty database named `name`.
diff --git a/crates/import/src/domain/service/notion_workspace/object.rs b/crates/import/src/domain/service/notion_workspace/object.rs
index 6de3c47d79..bd6670a088 100644
--- a/crates/import/src/domain/service/notion_workspace/object.rs
+++ b/crates/import/src/domain/service/notion_workspace/object.rs
@@ -19,7 +19,8 @@ use crate::domain::models::{
};
use crate::domain::ports::{
CanonicalImportRepo, EntityCreator, ImportError, ImportRepo, NotionLinks, NotionPlanRepo,
- NotionRunRepo, Result, SharedEntity, SlackWorkspaceSource, WorkspaceWriter, WrittenObjects,
+ NotionRunRepo, Result, SharedEntity, SlackWorkspaceSource, SourceDates, WorkspaceWriter,
+ WrittenObjects,
};
/// Times a waiting object is checked again before it is given up on: each
@@ -180,11 +181,14 @@ where
.ok_or_else(|| {
ImportError::Other(anyhow::anyhow!("page {} has no id", item.notion_id))
})?;
- let properties = match api.page(&item.notion_id).await {
- Ok(page) => values::document_properties(&page),
+ let (properties, dates) = match api.page(&item.notion_id).await {
+ Ok(page) => (
+ values::document_properties(&page),
+ Some(SourceDates::from(&page)),
+ ),
Err(error) => {
tracing::warn!(error = ?error, "notion page properties were not read");
- Default::default()
+ (Default::default(), None)
}
};
let document = self
@@ -197,6 +201,7 @@ where
target.links,
&properties,
Some(folder),
+ dates,
)
.await?;
if share {
diff --git a/crates/import/src/domain/service/notion_workspace/write.rs b/crates/import/src/domain/service/notion_workspace/write.rs
index 31cddf7f49..f76642730c 100644
--- a/crates/import/src/domain/service/notion_workspace/write.rs
+++ b/crates/import/src/domain/service/notion_workspace/write.rs
@@ -22,7 +22,8 @@ use crate::domain::models::{
WrittenObject,
};
use crate::domain::ports::{
- NotionLinks, NotionPageLink, NotionUserLink, SharedEntity, WorkspaceWriter, WrittenObjects,
+ NotionLinks, NotionPageLink, NotionUserLink, SharedEntity, SourceDates, WorkspaceWriter,
+ WrittenObjects,
};
use super::values::{PeopleMap, capped_name, derived_id, task_body, task_properties};
@@ -415,6 +416,7 @@ pub(super) async fn write_tasks<Api: NotionApi, Writer: WorkspaceWriter, Store:
links,
&properties,
Some(tasks_folder),
+ Some(SourceDates::from(&row)),
)
.await?;
progress.record_for(WrittenKind::Task, &row.id, id).await?;
diff --git a/crates/import/src/domain/service/notion_workspace/write/database.rs b/crates/import/src/domain/service/notion_workspace/write/database.rs
index 1f388dcfac..8c89fd4146 100644
--- a/crates/import/src/domain/service/notion_workspace/write/database.rs
+++ b/crates/import/src/domain/service/notion_workspace/write/database.rs
@@ -21,8 +21,8 @@ use uuid::Uuid;
use super::{BODY_CONCURRENCY, Cancel, Progress, WriteItem, folder_once};
use crate::domain::models::{WrittenKind, WrittenObject};
use crate::domain::ports::{
- ImportedDocumentProperties, NewDatabase, NotionLinks, SharedEntity, WorkspaceWriter,
- WrittenObjects,
+ ImportedDocumentProperties, NewDatabase, NotionLinks, SharedEntity, SourceDates,
+ WorkspaceWriter, WrittenObjects,
};
use super::super::values::{
@@ -558,6 +558,7 @@ async fn write_row_bodies<Api: NotionApi, Writer: WorkspaceWriter, Store: Writte
links,
&ImportedDocumentProperties::default(),
Some(folder),
+ Some(SourceDates::from(&row)),
)
.await?
.parse()?;
diff --git a/crates/import/src/domain/service/notion_workspace/write/database/test.rs b/crates/import/src/domain/service/notion_workspace/write/database/test.rs
index 746750e6d8..7382ffdb4e 100644
--- a/crates/import/src/domain/service/notion_workspace/write/database/test.rs
+++ b/crates/import/src/domain/service/notion_workspace/write/database/test.rs
@@ -153,6 +153,7 @@ struct WrittenDocument {
name: String,
markdown: String,
links: NotionLinks,
+ dates: Option<SourceDates>,
}
/// Records what the import asks Macro to create. `fail_at_batch` makes that
@@ -241,6 +242,7 @@ impl WorkspaceWriter for FakeWriter {
links: &NotionLinks,
_properties: &ImportedDocumentProperties,
_folder: Option<Uuid>,
+ dates: Option<SourceDates>,
) -> anyhow::Result<String> {
let mut documents = self.documents.lock().unwrap();
// Like Macro: a document id already in use is the earlier attempt's.
@@ -250,6 +252,7 @@ impl WorkspaceWriter for FakeWriter {
name: name.to_string(),
markdown: markdown.to_string(),
links: links.clone(),
+ dates,
});
}
Ok(id.to_string())
@@ -264,6 +267,7 @@ impl WorkspaceWriter for FakeWriter {
_links: &NotionLinks,
_properties: &ImportedTaskProperties,
_folder: Option<Uuid>,
+ _dates: Option<SourceDates>,
) -> anyhow::Result<String> {
unimplemented!("a copied database writes no tasks")
}
@@ -493,6 +497,14 @@ async fn a_data_source_becomes_a_database_with_typed_columns_rows_bodies_and_pen
documents[0].links, links,
"row bodies resolve the import's pages"
);
+ assert_eq!(
+ documents[0].dates,
+ Some(SourceDates {
+ created: Utc.with_ymd_and_hms(2026, 9, 1, 9, 0, 0).unwrap(),
+ last_edited: Utc.with_ymd_and_hms(2026, 10, 1, 9, 0, 0).unwrap(),
+ }),
+ "a row body keeps its Notion page's dates"
+ );
// The Page column is created first in the table...
let DatabaseOp::Column {
column: page_column,
diff --git a/crates/import/src/domain/service/workspace/run/test.rs b/crates/import/src/domain/service/workspace/run/test.rs
index ead9286a56..88c9c8fd74 100644
--- a/crates/import/src/domain/service/workspace/run/test.rs
+++ b/crates/import/src/domain/service/workspace/run/test.rs
@@ -3,6 +3,8 @@
use std::sync::{Arc, Mutex};
+use chrono::{DateTime, TimeZone, Utc};
+
use agent_runtime_protocol::domain::notification::{
ImportOutcome, ImportWorkspace, NotificationEvent, ReviewSettlement,
};
@@ -28,7 +30,7 @@ use crate::domain::models::{
};
use crate::domain::ports::{
ImportedDocumentProperties, ImportedTaskProperties, NewDatabase, NotionLinks, PlanRow,
- SharedEntity, TeamMember,
+ SharedEntity, SourceDates, TeamMember,
};
use crate::domain::service::NotionWorkspaceImporter;
use crate::domain::service::test::admission::NoConnector;
@@ -38,6 +40,19 @@ fn page_id(hex: char) -> NotionId {
NotionId::parse(&hex.to_string().repeat(32)).unwrap()
}
+/// When page `e` (the wiki) and `f` (a page under it) were created and last
+/// edited; other pages cannot be read.
+fn page_times(id: &NotionId) -> Option<(DateTime<Utc>, DateTime<Utc>)> {
+ let at = |month| Utc.with_ymd_and_hms(2024, month, 1, 0, 0, 0).unwrap();
+ if *id == page_id('e') {
+ Some((at(1), at(3)))
+ } else if *id == page_id('f') {
+ Some((at(2), at(6)))
+ } else {
+ None
+ }
+}
+
/// A workspace of plain pages, each with a body.
struct Pages;
@@ -50,8 +65,18 @@ impl NotionApi for Pages {
unimplemented!("writing never searches")
}
- async fn page(&self, _: &NotionId) -> std::result::Result<NotionPage, NotionError> {
- Err(NotionError::NotFound)
+ async fn page(&self, id: &NotionId) -> std::result::Result<NotionPage, NotionError> {
+ let (created, last_edited) = page_times(id).ok_or(NotionError::NotFound)?;
+ Ok(NotionPage {
+ id: id.clone(),
+ title: String::new(),
+ url: id.url(),
+ parent: import_notion::domain::model::NotionParent::Workspace,
+ last_edited,
+ created,
+ in_trash: false,
+ properties: Default::default(),
+ })
}
async fn page_markdown(&self, id: &NotionId) -> std::result::Result<PageMarkdown, NotionError> {
@@ -122,6 +147,8 @@ struct Writer {
tracker_links: Mutex<Vec<Uuid>>,
reject_tracker_links: std::sync::atomic::AtomicBool,
reject_tracker_projects: std::sync::atomic::AtomicBool,
+ /// Each document written with the source dates it was given.
+ document_dates: Mutex<Vec<(Uuid, Option<SourceDates>)>>,
}
impl WorkspaceWriter for Writer {
@@ -209,7 +236,9 @@ impl WorkspaceWriter for Writer {
links: &NotionLinks,
_: &ImportedDocumentProperties,
_: Option<Uuid>,
+ dates: Option<SourceDates>,
) -> anyhow::Result<String> {
+ self.document_dates.lock().unwrap().push((id, dates));
if let Some((entered, shutdown)) = &self.hold {
entered.notify_one();
shutdown.cancelled().await;
@@ -231,6 +260,7 @@ impl WorkspaceWriter for Writer {
_: &NotionLinks,
_: &ImportedTaskProperties,
_: Option<Uuid>,
+ _: Option<SourceDates>,
) -> anyhow::Result<String> {
unimplemented!("only pages here")
}
@@ -988,3 +1018,52 @@ async fn a_plan_presented_while_planning_fills_in_batch_by_batch_and_restages_on
/// The session events the import produces, in order.
mod events;
mod tracker;
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn written_pages_keep_their_notion_dates(pool: PgPool) {
+ let owner = user("dates");
+ let mut spec = page('f', "Spec");
+ spec.parent_foreign_id = Some(page_id('e').as_str().to_string());
+ let (review, rows) = approved(&pool, &owner, &[page('e', "Wiki"), spec]).await;
+ let repo = PgImportRepo::new(pool.clone());
+ planned(&repo, &owner).await;
+ let tracker = TaskTracker::new();
+ let writer = Arc::new(Writer::default());
+ let service = ImportServiceImpl::new(
+ PgImportRepo::new(pool.clone()),
+ Arc::new(NoConnector),
+ Arc::clone(&writer),
+ Arc::new(ai_usage::NoOpUsageRecorder),
+ )
+ .with_notion_connections(Arc::new(Connected))
+ .with_job_shutdown(tokio_util::sync::CancellationToken::new(), tracker.clone());
+
+ service
+ .start_notion_write(&owner, review, NotionWriteChoices::default())
+ .await
+ .unwrap();
+ tracker.close();
+ tracker.wait().await;
+
+ let at = |month| Utc.with_ymd_and_hms(2024, month, 1, 0, 0, 0).unwrap();
+ let mut documents = writer.document_dates.lock().unwrap().clone();
+ documents.sort_by_key(|(id, _)| *id);
+ let mut expected = vec![
+ (
+ write::document_id(rows[0]),
+ Some(SourceDates {
+ created: at(1),
+ last_edited: at(3),
+ }),
+ ),
+ (
+ write::document_id(rows[1]),
+ Some(SourceDates {
+ created: at(2),
+ last_edited: at(6),
+ }),
+ ),
+ ];
+ expected.sort_by_key(|(id, _)| *id);
+ assert_eq!(documents, expected, "each page keeps its own dates");
+}