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.

12.2 KB
Raw Blame
//! The single catalog of local AWS resources (SQS queues, S3 buckets, DynamoDB
//! tables).
//!
//! Two places need these names: [`localstack`](super::localstack) *creates* the
//! resources, and [`local_env`](super::local_env) emits the env vars services
//! use to *find* them. Before this catalog the lists lived in both files and
//! could silently drift — a queue created but not exported (or vice-versa) is a
//! service that can't reach a queue that exists. Here they share one list, so
//! adding a resource in one place is impossible: creation and env both follow
//! from a single entry.
//!
//! Rule of thumb: a name is a `const` only when something *outside* the catalog
//! iteration also references it (the upload-finalizer wiring, the seed env);
//! everything else is an inline literal in its entry.

/// LocalStack's fixed account id, used in queue URLs and ARNs.
const ACCOUNT_ID: &str = "000000000000";

/// The doc-storage bucket — referenced by the upload-finalizer wiring and the
/// seed env, so it is named rather than inlined.
pub const DOC_STORAGE_BUCKET: &str = "doc-storage";

/// The queue doc-storage ObjectCreated events publish to — referenced by the
/// upload-finalizer wiring, so it is named.
pub const UPLOAD_FINALIZER_QUEUE: &str = macro_queues::DocumentUploadFinalizerQueue::LOCAL;

// DynamoDB table names: referenced both by their bespoke create-table schema in
// `localstack` and by the env binding below, so they are named.
/// The bulk-upload requests table.
pub const BULK_UPLOAD_TABLE: &str = "bulk-upload";
/// The connection-gateway (websocket) table.
pub const CONNECTION_GATEWAY_TABLE: &str = "connection-gateway-table";
/// The static-file metadata table.
pub const STATIC_FILE_TABLE: &str = "static-file-metadata";

/// Alias for the KMS key that encrypts users' Cursor API keys.
///
/// An alias rather than a key id: `CreateKey` mints a random id on every run,
/// so a key id could not be baked into the compose env, while an alias is
/// stable and KMS accepts one anywhere a key id goes.
pub const CURSOR_API_KEY_KMS_ALIAS: &str = "alias/macro-local-cursor-api-key";

/// Dedicated envelope-encryption key for local ChatGPT account connections.
pub const CODEX_OAUTH_KMS_ALIAS: &str = "alias/macro-local-codex-oauth";

/// Key the public MCP OAuth broker encrypts its authorization codes' tokens with.
pub const MCP_OAUTH_KMS_ALIAS: &str = "alias/macro-local-mcp-oauth";

/// The WhatsApp session service's key for per-account data keys. The service
/// computes this name from `ENVIRONMENT` (`SessionKmsKey` in its `config.rs`).
pub const WHATSAPP_SESSION_KMS_ALIAS: &str = "alias/whatsapp-session-service-local";

/// The full LocalStack URL for `queue` (docker-network host — services run in
/// containers and reach LocalStack by its compose alias).
pub fn queue_url(queue: &str) -> String {
    format!("http://localstack:4566/{ACCOUNT_ID}/{queue}")
}

/// The ARN for `queue`.
pub fn queue_arn(queue: &str) -> String {
    format!("arn:aws:sqs:us-east-1:{ACCOUNT_ID}:{queue}")
}

/// How an env var refers to a queue: the bare name, or the full LocalStack URL.
#[derive(Clone, Copy)]
pub enum QueueForm {
    /// The env value is the bare queue name.
    Name,
    /// The env value is the full LocalStack queue URL.
    Url,
}

impl QueueForm {
    /// Resolve the env value for `queue` in this form.
    pub fn value(self, queue: &str) -> String {
        match self {
            QueueForm::Name => queue.to_string(),
            QueueForm::Url => queue_url(queue),
        }
    }
}

/// An SQS queue: the name created in LocalStack plus the env vars that point at
/// it. A queue may be referenced by several keys in different forms (e.g. the
/// backfill queue is exported both as a bare name and as a URL).
pub struct Queue {
    /// The queue name created in LocalStack.
    pub name: &'static str,
    /// `(env key, value form)` pairs the env builder emits for this queue.
    pub bindings: &'static [(&'static str, QueueForm)],
}

impl Queue {
    /// Non-default attributes, applied after all queues exist so redrive targets resolve.
    pub fn attributes(
        &self,
    ) -> std::collections::HashMap<aws_sdk_sqs::types::QueueAttributeName, String> {
        use aws_sdk_sqs::types::QueueAttributeName;

        match self.name {
            macro_queues::SlackImportQueue::LOCAL => [
                (QueueAttributeName::VisibilityTimeout, "900".to_string()),
                (
                    QueueAttributeName::RedrivePolicy,
                    serde_json::json!({
                        "deadLetterTargetArn": queue_arn(macro_queues::SlackImportDlq::LOCAL),
                        "maxReceiveCount": 5,
                    })
                    .to_string(),
                ),
            ]
            .into(),
            macro_queues::SlackImportDlq::LOCAL => [(
                QueueAttributeName::MessageRetentionPeriod,
                (14 * 24 * 60 * 60).to_string(),
            )]
            .into(),
            _ => Default::default(),
        }
    }
}

/// An S3 bucket: the name created in LocalStack and the env var pointing at it.
pub struct Bucket {
    /// The bucket name created in LocalStack.
    pub name: &'static str,
    /// The env var services read to find this bucket.
    pub env_key: &'static str,
}

/// A DynamoDB table: the name and its env var. The table *schema* lives with the
/// provisioner (it is bespoke per table); only the name is shared here.
pub struct Table {
    /// The table name created in LocalStack.
    pub name: &'static str,
    /// The env var services read to find this table.
    pub env_key: &'static str,
}

use QueueForm::{Name, Url};

/// Every local SQS queue and the env var(s) that reference it.
pub const QUEUES: &[Queue] = &[
    Queue {
        name: macro_queues::SlackImportQueue::LOCAL,
        bindings: &[(macro_queues::SlackImportQueue::OVERRIDE_ENV_VAR_NAME, Url)],
    },
    Queue {
        name: macro_queues::SlackImportDlq::LOCAL,
        bindings: &[(macro_queues::SlackImportDlq::OVERRIDE_ENV_VAR_NAME, Url)],
    },
    Queue {
        name: macro_queues::NotificationQueue::LOCAL,
        bindings: &[("NOTIFICATION_QUEUE", Url)],
    },
    Queue {
        name: macro_queues::NotificationIngressQueue::LOCAL,
        bindings: &[("NOTIFICATION_INGRESS_QUEUE", Url)],
    },
    Queue {
        name: macro_queues::PushNotificationEventHandlerQueue::LOCAL,
        bindings: &[("PUSH_NOTIFICATION_EVENT_HANDLER_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::WebhookEventQueue::LOCAL,
        bindings: &[(macro_queues::WebhookEventQueue::OVERRIDE_ENV_VAR_NAME, Url)],
    },
    Queue {
        name: macro_queues::EmailBackfillQueue::LOCAL,
        bindings: &[("BACKFILL_QUEUE", Name), ("EMAIL_BACKFILL_QUEUE", Url)],
    },
    Queue {
        // Consumed by email_service's nightly CRM-cleanup workers; without the
        // queue existing in LocalStack they tight-loop on receive errors.
        name: macro_queues::EmailCrmCleanupQueue::LOCAL,
        bindings: &[(
            macro_queues::EmailCrmCleanupQueue::OVERRIDE_ENV_VAR_NAME,
            Url,
        )],
    },
    Queue {
        name: macro_queues::ChatDeleteQueue::LOCAL,
        bindings: &[("CHAT_DELETE_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::ContactsQueue::LOCAL,
        bindings: &[("CONTACTS_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::ConvertQueue::LOCAL,
        bindings: &[("CONVERT_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::DocumentDeleteQueue::LOCAL,
        bindings: &[("DOCUMENT_DELETE_QUEUE", Name)],
    },
    Queue {
        name: UPLOAD_FINALIZER_QUEUE,
        bindings: &[("DOCUMENT_UPLOAD_FINALIZER_QUEUE_URL", Url)],
    },
    Queue {
        name: macro_queues::DocumentTextExtractorQueue::LOCAL,
        bindings: &[("DOCUMENT_TEXT_EXTRACTOR_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::EmailScheduledQueue::LOCAL,
        bindings: &[("EMAIL_SCHEDULED_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::GmailInboxSyncQueue::LOCAL,
        bindings: &[("GMAIL_INBOX_SYNC_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::GmailInboxSyncRetryQueue::LOCAL,
        bindings: &[("GMAIL_INBOX_SYNC_RETRY_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::GmailOpsQueue::LOCAL,
        bindings: &[("GMAIL_OPS_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::GmailOpsRetryQueue::LOCAL,
        bindings: &[("GMAIL_OPS_RETRY_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::LinkManagerQueue::LOCAL,
        bindings: &[("LINK_MANAGER_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::SearchEventQueue::LOCAL,
        bindings: &[("SEARCH_EVENT_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::AiProjectionQueue::LOCAL,
        bindings: &[("AI_PROJECTION_QUEUE", Url)],
    },
    Queue {
        name: macro_queues::SfsDeleteQueue::LOCAL,
        bindings: &[("SFS_DELETE_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::SfsUploaderQueue::LOCAL,
        bindings: &[("SFS_UPLOADER_QUEUE", Name)],
    },
    Queue {
        name: macro_queues::StaticFileServiceS3EventQueueUrl::LOCAL,
        bindings: &[("STATIC_FILE_SERVICE_S3_EVENT_QUEUE_URL", Url)],
    },
    Queue {
        // Carries both the reminder sweep tick and the per-firing fan-out it
        // publishes. Consumed by cloud-storage-service's dispatch worker, which
        // tight-loops on receive errors if the queue is not there.
        //
        // No EventBridge locally — LocalStack has `events` disabled — so nothing
        // puts the minutely tick on this queue. `just poke_reminder_sweep` (or
        // `just tick_reminder_sweeps`) stands in for the schedule.
        name: macro_queues::ReminderDispatchQueue::LOCAL,
        bindings: &[(
            macro_queues::ReminderDispatchQueue::OVERRIDE_ENV_VAR_NAME,
            Url,
        )],
    },
    Queue {
        // Same EventBridge stand-in as the reminders queue above:
        // `just poke_calendar_reminder_sweep` (or
        // `just tick_calendar_reminder_sweeps`) supplies the minutely tick.
        name: macro_queues::CalendarReminderDispatchQueue::LOCAL,
        bindings: &[(
            macro_queues::CalendarReminderDispatchQueue::OVERRIDE_ENV_VAR_NAME,
            Url,
        )],
    },
    Queue {
        // calendar_service's backfill queue, consumed by its always-on backfill
        // workers (which tight-loop on receive errors if the queue is absent).
        // Bound through the queue's own override var in URL form so the service
        // dials the full LocalStack URL rather than the bare name.
        name: macro_queues::CalendarServiceBackfillQueue::LOCAL,
        bindings: &[(
            macro_queues::CalendarServiceBackfillQueue::OVERRIDE_ENV_VAR_NAME,
            Url,
        )],
    },
];

/// Every local S3 bucket and the env var that references it.
pub const BUCKETS: &[Bucket] = &[
    Bucket {
        name: "macro-email-attachments",
        env_key: "ATTACHMENT_BUCKET",
    },
    Bucket {
        name: DOC_STORAGE_BUCKET,
        env_key: "DOCUMENT_STORAGE_BUCKET",
    },
    Bucket {
        name: "docx-upload",
        env_key: "DOCX_DOCUMENT_UPLOAD_BUCKET",
    },
    Bucket {
        name: "static-file-storage",
        env_key: "STATIC_STORAGE_BUCKET",
    },
    Bucket {
        name: "bulk-upload-staging",
        env_key: "UPLOAD_STAGING_BUCKET",
    },
    Bucket {
        name: "macro-call-recording-local",
        env_key: "CALL_RECORDING_BUCKET_NAME",
    },
    Bucket {
        // The patch behind each agent session's Changes pane.
        name: "agent-session-changes",
        env_key: "AGENT_SESSION_CHANGES_BUCKET",
    },
    Bucket {
        // The patch behind each pull request's Changes pane, shared with the
        // agent sessions that work on that pull request.
        name: "github-pull-request-patches",
        env_key: "GITHUB_PULL_REQUEST_PATCH_BUCKET",
    },
];

/// Every local DynamoDB table and the env var that references it.
pub const TABLES: &[Table] = &[
    Table {
        name: BULK_UPLOAD_TABLE,
        env_key: "BULK_UPLOAD_REQUESTS_TABLE",
    },
    Table {
        name: CONNECTION_GATEWAY_TABLE,
        env_key: "CONNECTION_GATEWAY_TABLE",
    },
    Table {
        name: STATIC_FILE_TABLE,
        env_key: "STATIC_FILE_SERVICE_DYNAMODB_TABLE_NAME",
    },
];

#[cfg(test)]
mod test;