Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

Persist completed trajectories independently of redaction backlog

Ezra Ellettebc9af1fbb740
diff --git a/apps/screenpipe-app-tauri/lib/trajectories/full-trajectory.test.ts b/apps/screenpipe-app-tauri/lib/trajectories/full-trajectory.test.ts
index ccb553686..f7b98c98e 100644
--- a/apps/screenpipe-app-tauri/lib/trajectories/full-trajectory.test.ts
+++ b/apps/screenpipe-app-tauri/lib/trajectories/full-trajectory.test.ts
@@ -81,6 +81,33 @@ describe("full Workflows trajectory collection", () => {
expect(JSON.stringify([...f.jobs])).not.toContain("SECRET_BEARER");
expect(f.jobs.size).toBe(0);
});
+ it("persists later conversations while earlier redaction is pending", async () => {
+ const f = fixture(); let release!: () => void, written!: () => void;
+ f.ports.store.put.mockImplementation(async job => {
+ f.jobs.set(job.id, structuredClone(job));
+ if (job.id.startsWith("2") && !job.cursor && !written) await new Promise<void>(resolve => { written = resolve; });
+ });
+ f.ports.redact.mockImplementationOnce(async text => {
+ await new Promise<void>(resolve => { release = resolve; }); return text;
+ });
+ const first = f.collector.completeFull(f.ticket, [{ text: "First" }]);
+ await vi.waitFor(() => expect(release).toBeTypeOf("function"));
+ const second = f.collector.completeFull({ ...f.ticket, id: "22345678-1234-4234-8234-123456789abc" }, [{ text: "Second" }]);
+ await vi.waitFor(() => expect(f.jobs.size).toBe(2));
+ const resumed = f.collector.resume(); release(); await resumed; written();
+ await Promise.all([first, second]); expect(f.jobs.size).toBe(0);
+ expect(f.posts).toHaveLength(2);
+ });
+ it("purges a source write that finishes after withdrawal", async () => {
+ const f = fixture(); let finish!: () => void;
+ f.ports.store.put.mockImplementationOnce(async job => {
+ await new Promise<void>(resolve => { finish = resolve; }); f.jobs.set(job.id, structuredClone(job));
+ });
+ const pending = f.collector.completeFull(f.ticket, [{ text: "PRIVATE" }]);
+ await vi.waitFor(() => expect(finish).toBeTypeOf("function"));
+ await f.collector.stop("user_one"); f.setLocal(null); finish(); await pending;
+ expect(f.jobs.size).toBe(0); expect(f.posts).toHaveLength(0);
+ });
it("never queues or sends anything without opt-in and rechecks server consent", async () => {
const f = fixture(); await f.collector.completeFull(null, [{ text: "PRIVATE" }]);
expect(f.ports.store.put).not.toHaveBeenCalled(); expect(f.ports.request).not.toHaveBeenCalled();
diff --git a/apps/screenpipe-app-tauri/lib/trajectories/full-trajectory.ts b/apps/screenpipe-app-tauri/lib/trajectories/full-trajectory.ts
index f310aa3da..7d17212f3 100644
--- a/apps/screenpipe-app-tauri/lib/trajectories/full-trajectory.ts
+++ b/apps/screenpipe-app-tauri/lib/trajectories/full-trajectory.ts
@@ -90,7 +90,9 @@ function transferParts(text: string, job: Contribution, capture: NonNullable<Sha
export function createFullTrajectoryCollector(ports: Ports) {
let work: Promise<void> = Promise.resolve();
+ let generation = 0;
const active = new Map<string, AbortController>();
+ const scheduled = new Set<string>();
const enqueue = (task: () => Promise<void>) => {
const result = work.then(task, task); work = result.catch(() => {}); return result;
};
@@ -168,15 +170,28 @@ export function createFullTrajectoryCollector(ports: Ports) {
} finally { active.delete(job.id); }
}
return {
- completeFull(ticket: TrajectoryTicket | null, records: unknown[]) {
- if (!ticket) return Promise.resolve();
- // Persist before any external await. Credentials never enter the outbox.
+ async completeFull(ticket: TrajectoryTicket | null, records: unknown[]) {
+ if (!ticket) return;
+ const started = generation, local = await ports.local();
+ if (started !== generation || local?.accountId !== ticket.local.accountId || local.epoch !== ticket.local.epoch) return;
+ // Persist independently of earlier redaction/upload work. Credentials
+ // never enter the outbox; withdrawal during this write wins afterward.
const job: Contribution = { id: ticket.id, local: ticket.local, createdAt: Date.now(),
records: records.map(record => JSON.parse(contributionText(record))), redacted: [], cursor: 0, nextPart: 0 };
- return enqueue(async () => { await ports.store.put(job); await process(job); });
+ scheduled.add(job.id);
+ try {
+ await ports.store.put(job);
+ await enqueue(async () => {
+ if (started !== generation) await ports.store.remove(job.id);
+ else await process(job);
+ });
+ } finally { scheduled.delete(job.id); }
},
- resume() { return enqueue(async () => { for (const job of await ports.store.list()) await process(job); }); },
+ resume() { return enqueue(async () => {
+ for (const job of await ports.store.list()) if (!scheduled.has(job.id)) await process(job);
+ }); },
stop(accountId?: string) {
+ generation++;
for (const controller of active.values()) controller.abort();
if (accountId) return enqueue(async () => {
for (const job of await ports.store.list()) if (job.local.accountId === accountId) await ports.store.remove(job.id);