mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-20 18:26:40 +01:00
feat(telemetry): send product lifecycle events and agent run failure cause (#22250)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - First-party telemetry tells the team which product steps people finish > - Telemetry did not record when a model is connected, when a task is created, completed, or blocked, when a plan is created or approved, or when an artifact is created > - Telemetry also did not tell why an agent run failed, so failures could not be grouped by cause > - This pull request syncs the new telemetry contract and sends these events from the server once per real change > - The benefit is a product funnel and a failure breakdown that use only enums, booleans, and counts ## Linked Issues or Issue Description **Subsystem affected** Server telemetry (`packages/shared/src/telemetry`, `server/src/services/product-telemetry.ts`) and the emit sites in the issue, document, work product, interaction, and AI connection services. **Problem or motivation** The team cannot measure the main product path. There is no event for a new model connection, a new task, a completed task, a blocked task, a new plan, an approved plan, or a new artifact. `agent.task_run` tells that a run failed, but not why. The signup funnel also must know when the onboarding wizard created the first task. **Proposed solution** Register the events in the telemetry backend. Sync the generated contract here. Add typed helpers. Send each event once, after the transaction that makes the change commits. Use fixed enums for every kind, reason, and cause. **Alternatives considered** A post-commit action queue on the issue service only. The native status path and many other callers use an external transaction without that queue, so most agent completions would not send an event. A generic after-commit hook on the database client covers every caller. **Roadmap alignment** This is telemetry for existing features. It adds no new product feature. **Additional context** This change depends on paperclipai/telemetry-backend#150, which is merged. Deploy that backend change before this change ships. Until then, the collector rejects the new event names. ## What Changed - Sync `generated/paperclip-telemetry.ts` from the backend branch as a direct copy. Regenerate `posthog-direct/enum-fallbacks.generated.ts` with `scripts/generate-posthog-direct-fixtures.mjs`. The loader golden file did not change. - Add `afterTransactionCommit()` to `@paperclipai/db`. It runs a callback after the outermost transaction commits. It drops the callback on rollback. Outside a transaction it runs the callback at once. A callback that throws never fails the committed work. - Add helpers `trackModelConnected`, `trackTaskCreated`, `trackTaskCompleted`, `trackTaskBlocked`, `trackPlanCreated`, `trackPlanApproved`, and `trackArtifactCreated`. Add `failureCause` to `trackAgentTaskRun`. Assign the new events to `operational_enum_count` retention. - Add `server/src/services/product-telemetry.ts`. It sends each event after commit inside `withTelemetryCompany()`, so Cloud direct delivery gets `company_id` for background writes. - Emit sites: - `model.connected`: `aiConnectionService.save`, for a new connection only. A reconnect sends nothing. `first_for_company` is true when the company had no earlier model connection. - `task.created`: `issueService.create`, after the insert. A deduplicated create, a company import, and a chat conversation send nothing. `created_by_kind` is `routine` for routine runs, `onboarding` for the onboarding wizard's first task, and otherwise `user`, `agent`, or `system`. - `task.completed` and `task.blocked`: `issueService.update`, when the status changes to `done` or `blocked`. The same status again sends nothing. - `plan.created`: `documentService.upsertIssueDocument`, for the first revision of the `plan` document. - `plan.approved`: the accept path of a `request_confirmation` that targets the task's `plan` document. - `artifact.created`: `workProductService.createForIssue` (`direct`), the promotion route (`promotion`), and a run file deliverable (`run_deliverable`). - `agent.task_run.failure_cause`: `emitAgentTaskRun`, for `failed`, `timed_out`, and `interrupted` runs only. - `blocked_reason` mapping: unfinished blocking tasks give `waiting_on_task`. Otherwise an unblock owner of a person or the board gives `waiting_for_person`, and an agent owner gives `waiting_for_agent`. A system write with neither gives `needs_recovery`. A person or agent write with neither gives `unspecified`. - `failure_cause` mapping (stored `errorCode`, with `resultJson.providerFailure.category` for generic provider and adapter codes): - `missing_access`: auth and permission codes (`acpx_auth_required`, `provider_auth_failed`, `user_secret_missing`, `github_access_denied`, `github_repo_not_accessible`, `permission_denied`, `project_access_denied`, `configuration_incomplete`, `connection_unavailable`, `signing_secret_unconfigured`), and provider category `authentication` or `permission`. - `provider_limit`: `provider_quota`, `native_provider_usage_limit`, `native_provider_overloaded`, `github_rate_limited`, `ai_connection_busy`, and provider category `rate_limit`, `quota_exhausted`, or `overloaded`. - `provider_error`: `native_provider_model_rejected`, `provider_transport_failed`, `codex_transient_upstream`, `provider_failed`, `provider_tool_definition_invalid`, and the other provider categories. - `waiting_for_person`: `execution_reconciliation_required`, every code in `EXECUTION_RECONCILIATION_CAUSES`, `issue_waiting_for_response`, and `issue_continuation_waiting_on_review`. - `task_changed`: the task was reassigned, paused, closed, blocked, or stopped by a person (`operator_interrupted`, `cancelled`, `interrupted`, `issue_*` lifecycle codes, `agent_paused`, `queued_comment_discarded`). - `platform_error`: process, server, workspace, sandbox, runner, and storage codes (`process_lost`, `server_shutdown_interrupted`, `setup_failed`, `workspace_*`, `environment_provider_unavailable`, `controller_lease_lost`, and others). - `agent_error`: `adapter_failed` without a provider failure, `invalid_output`, `invalid_response`, `tool_error`, `acpx_turn_failed`, `test_run_failed`, `prompt_injection_blocked`, `tool_execution_outcome_unknown`, `timeout`. - `unknown`: any other code, or no code. - On Cloud direct delivery, every new emit site names the responsible person with `withTelemetryActor()` and `resolveTelemetryPerson()`, as `agent.task_run` does: the task creator, else the task's responsible user, else the run owner. `task.created` uses the creating user, else the agent's on-behalf user. `model.connected` uses the user who owns the connection. A live request still wins. The self-hosted collector envelope does not change. - `failure_cause` builds on the run failure code sets in `continuation-cause.ts`. A test fails when a code in those sets has no telemetry code. The provider failure category decides only for `adapter_failed`, `provider_failed`, or no code. - Work product providers outside `paperclip`, `github`, `vercel`, and `s3` are sent as `custom`. - The dispatch workspace check and execution recovery write `blocked` directly. They call the same after-commit helper, with the task's real blockers and unblock owner. The blocker read runs in a savepoint in the issue dependency layer, so a failed read never aborts the block write. - Update the telemetry README with the emit rules, the after-commit rule, and the `failure_cause` source. - Refresh `tests/runner-e2e/source-sdk-public-inputs.json`, because its check reported a change. ## Privacy Review Privacy review requested. Every new dimension is a fixed enum, a boolean, or a count. No dimension carries a title, a body, a plan step, a connection or account name, an email, a URL, a repository name, a prompt, an error message, or an endpoint address. `failure_cause` is a code that the server picks. The run's error text is never sent. The tests check that a provider message and a connection name do not reach the event. ## Verification - `pnpm vitest run packages/shared/src/telemetry/`: 7 files pass, including the new `product-events.test.ts` (helper payloads, omitted optional values, collector envelope with only registered dimensions, retention class). - `packages/db/src/after-transaction-commit.test.ts`: 4 pass (after commit, rollback drop, savepoint, outside a transaction). - `server/src/__tests__/product-telemetry.test.ts`: 14 pass. They check that each event fires once with the right kind and company scope, that a rollback sends nothing, that a repeated status sends nothing, that a later plan revision sends nothing, and that a reconnect sends nothing. - `server/src/__tests__/agent-task-run-telemetry.test.ts`: 35 pass, including the `failure_cause` mapping table and the emit path. - `server/src/__tests__/issue-thread-interactions-service.test.ts -t Plan`: `plan.approved` fires for a plan confirmation and not for an unrelated confirmation. - `server/src/__tests__/posthog-direct-telemetry.test.ts`: the Cloud direct path sends `company_id`, `user_id`, and `attributed_via` for background lifecycle events, an event with no person stays on the workspace, and `failure_cause` passes through. A self-hosted test checks that a person scope leaves the collector events unchanged. - Existing telemetry tests (connector, interaction, run finalizer, heartbeat, AI connection): 299 pass. - `pnpm --filter @paperclipai/shared typecheck`, `pnpm --filter @paperclipai/db typecheck`, and `pnpm --filter @paperclipai/server typecheck`: pass. - `pnpm verify` returned exit 2 (global CI input changed). Required CI supplies the broad coverage. ## Risks - Merge order: merge and deploy paperclipai/telemetry-backend#150 first. If this change ships first, the collector quarantines the new events. The product behavior does not change. - `afterTransactionCommit` uses the existing open-transaction marker in the database client. A callback that a savepoint registers runs when the outer transaction commits, even if that savepoint rolled back. - `task.blocked` adds one small indexed read inside the update transaction, only when a task enters `blocked`. - `failure_cause` adds one small read for a failed run when the caller did not pass the error code. It runs off the run's critical path, as before. - Low risk otherwise. No schema migration. No change to existing event dimensions. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Anthropic Claude Opus 5.5 (`claude-opus-5-5`), through Claude Code, with tool use (shell, Git, tests). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7a0ac92fdf
commit
067f7a0815
28 files changed
+1597
-32
No files matched your search
@@ -0,0 +1,96 @@
|
||||
import { afterAll, describe, expect, it } from "vitest";
|
||||
import { afterTransactionCommit, createDb, type Db } from "./client.js";
|
||||
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./test-embedded-postgres.js";
|
||||
|
||||
const support = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = support.supported ? describe : describe.skip;
|
||||
|
||||
describeEmbeddedPostgres("afterTransactionCommit", () => {
|
||||
let db: Db;
|
||||
let cleanup: (() => Promise<void>) | undefined;
|
||||
|
||||
afterAll(async () => {
|
||||
await db?.$client.end({ timeout: 1 });
|
||||
await cleanup?.();
|
||||
});
|
||||
|
||||
async function database() {
|
||||
if (db) return db;
|
||||
const started = await startEmbeddedPostgresTestDatabase("paperclip-db-after-commit-");
|
||||
cleanup = started.cleanup;
|
||||
db = createDb(started.connectionString);
|
||||
return db;
|
||||
}
|
||||
|
||||
it("runs a callback only after the enclosing transaction commits", async () => {
|
||||
const client = await database();
|
||||
const order: string[] = [];
|
||||
await client.transaction(async (tx) => {
|
||||
afterTransactionCommit(() => order.push("callback"));
|
||||
await tx.execute("select 1");
|
||||
order.push("transaction body");
|
||||
});
|
||||
order.push("returned");
|
||||
expect(order).toEqual(["transaction body", "callback", "returned"]);
|
||||
}, 30_000);
|
||||
|
||||
it("drops the callback when the transaction rolls back", async () => {
|
||||
const client = await database();
|
||||
const calls: string[] = [];
|
||||
await expect(client.transaction(async () => {
|
||||
afterTransactionCommit(() => calls.push("callback"));
|
||||
throw new Error("rolled back");
|
||||
})).rejects.toThrow("rolled back");
|
||||
expect(calls).toEqual([]);
|
||||
}, 30_000);
|
||||
|
||||
it("registers callbacks from a savepoint on the outer transaction", async () => {
|
||||
const client = await database();
|
||||
const order: string[] = [];
|
||||
await client.transaction(async (tx) => {
|
||||
await tx.transaction(async () => {
|
||||
afterTransactionCommit(() => order.push("savepoint callback"));
|
||||
});
|
||||
order.push("outer body");
|
||||
});
|
||||
expect(order).toEqual(["outer body", "savepoint callback"]);
|
||||
}, 30_000);
|
||||
|
||||
it("drops callbacks from a savepoint that rolls back while the outer transaction commits", async () => {
|
||||
const client = await database();
|
||||
const order: string[] = [];
|
||||
await client.transaction(async (tx) => {
|
||||
await tx.transaction(async () => {
|
||||
afterTransactionCommit(() => order.push("rolled back savepoint"));
|
||||
throw new Error("savepoint failed");
|
||||
}).catch(() => order.push("savepoint caught"));
|
||||
await tx.transaction(async (inner) => {
|
||||
await inner.transaction(async () => {
|
||||
afterTransactionCommit(() => order.push("nested savepoint"));
|
||||
});
|
||||
});
|
||||
afterTransactionCommit(() => order.push("outer"));
|
||||
});
|
||||
expect(order).toEqual(["savepoint caught", "nested savepoint", "outer"]);
|
||||
}, 30_000);
|
||||
|
||||
it("drops callbacks from a committed savepoint when the outer transaction rolls back", async () => {
|
||||
const client = await database();
|
||||
const calls: string[] = [];
|
||||
await expect(client.transaction(async (tx) => {
|
||||
await tx.transaction(async () => {
|
||||
afterTransactionCommit(() => calls.push("savepoint"));
|
||||
});
|
||||
throw new Error("outer rolled back");
|
||||
})).rejects.toThrow("outer rolled back");
|
||||
expect(calls).toEqual([]);
|
||||
}, 30_000);
|
||||
|
||||
it("runs at once outside a transaction and isolates a throwing callback", async () => {
|
||||
await database();
|
||||
const calls: string[] = [];
|
||||
afterTransactionCommit(() => { throw new Error("observer failed"); });
|
||||
afterTransactionCommit(() => calls.push("outside"));
|
||||
expect(calls).toEqual(["outside"]);
|
||||
}, 30_000);
|
||||
});
|
||||
@@ -301,7 +301,8 @@ export type DedicatedDbConnectionStats = { limit: number; inUse: number; peakInU
|
||||
const dedicatedConnectionSets = new WeakMap<object, DedicatedConnectionSet>();
|
||||
type DedicatedConnectionHold = { set: DedicatedConnectionSet; active: boolean };
|
||||
const heldDedicatedConnections = new AsyncLocalStorage<readonly DedicatedConnectionHold[]>();
|
||||
const openTransactions = new AsyncLocalStorage<{ active: boolean }>();
|
||||
type OpenTransaction = { active: boolean; afterCommit: Array<() => void> };
|
||||
const openTransactions = new AsyncLocalStorage<OpenTransaction>();
|
||||
|
||||
function dedicatedConnectionSet(create: DedicatedConnectionSet["create"], resolved: DatabaseClientOptions): DedicatedConnectionSet {
|
||||
const limit = resolved.dedicatedConnectionMax ?? DEFAULT_DEDICATED_CONNECTION_MAX;
|
||||
@@ -353,13 +354,54 @@ async function admitDedicatedConnection(set: DedicatedConnectionSet, nested: boo
|
||||
});
|
||||
}
|
||||
|
||||
function markOpenTransactions(db: Db) {
|
||||
const transaction = db.transaction.bind(db) as (...args: unknown[]) => Promise<unknown>;
|
||||
db.transaction = ((fn: (tx: unknown) => Promise<unknown>, ...rest: unknown[]) => transaction(async (tx: unknown) => {
|
||||
const open = { active: true };
|
||||
export function afterTransactionCommit(callback: () => void): void {
|
||||
const open = openTransactions.getStore();
|
||||
if (open?.active) open.afterCommit.push(callback);
|
||||
else runAfterCommitCallback(callback);
|
||||
}
|
||||
|
||||
function runAfterCommitCallback(callback: () => void) {
|
||||
try { callback(); } catch (error) { void error; }
|
||||
}
|
||||
|
||||
type TransactionRunner = { transaction: (...args: unknown[]) => Promise<unknown> };
|
||||
|
||||
function runInOpenTransaction(
|
||||
transaction: (...args: unknown[]) => Promise<unknown>,
|
||||
fn: (tx: unknown) => Promise<unknown>,
|
||||
rest: unknown[],
|
||||
settle: (open: OpenTransaction) => void,
|
||||
) {
|
||||
const open: OpenTransaction = { active: true, afterCommit: [] };
|
||||
return transaction(async (tx: unknown) => {
|
||||
bufferSavepointCallbacks(tx);
|
||||
try { return await openTransactions.run(open, () => fn(tx)); }
|
||||
finally { open.active = false; }
|
||||
}, ...rest)) as Db["transaction"];
|
||||
}, ...rest).then((result) => {
|
||||
settle(open);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
function bufferSavepointCallbacks(tx: unknown) {
|
||||
const runner = tx as TransactionRunner;
|
||||
if (typeof runner?.transaction !== "function") return;
|
||||
const savepoint = runner.transaction.bind(runner);
|
||||
runner.transaction = (fn, ...rest) => {
|
||||
const parent = openTransactions.getStore();
|
||||
return runInOpenTransaction(savepoint, fn as (tx: unknown) => Promise<unknown>, rest, (open) => {
|
||||
if (parent?.active) parent.afterCommit.push(...open.afterCommit);
|
||||
else for (const callback of open.afterCommit) runAfterCommitCallback(callback);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function markOpenTransactions(db: Db) {
|
||||
const transaction = db.transaction.bind(db) as (...args: unknown[]) => Promise<unknown>;
|
||||
db.transaction = ((fn: (tx: unknown) => Promise<unknown>, ...rest: unknown[]) =>
|
||||
runInOpenTransaction(transaction, fn, rest, (open) => {
|
||||
for (const callback of open.afterCommit) runAfterCommitCallback(callback);
|
||||
})) as Db["transaction"];
|
||||
}
|
||||
|
||||
export const DRIVER_DEFAULT_POOL_MAX_CONNECTIONS = 10;
|
||||
|
||||
@@ -36,6 +36,7 @@ export {
|
||||
migratePostgresIfEmpty,
|
||||
type MigrationBootstrapResult,
|
||||
type Db,
|
||||
afterTransactionCommit,
|
||||
} from "./client.js";
|
||||
export {
|
||||
getEmbeddedPostgresTestSupport,
|
||||
|
||||
@@ -120,6 +120,69 @@ value with a salted hash of the per-installation secret.
|
||||
Use `trackAgentTaskRun()` to emit this event. The generated contract remains
|
||||
the authority for its exact dimensions and optionality.
|
||||
|
||||
Failed, timed-out, and interrupted runs also carry `failure_cause`. The server
|
||||
picks this code from the run's stored error code and its provider failure
|
||||
category, in `server/src/services/agent-task-run-telemetry.ts`. The code is a
|
||||
fixed enum. It never contains error text, provider messages, or SQL. An error
|
||||
code that the mapping does not know is sent as `unknown`. The provider failure
|
||||
category decides the code only for the generic `adapter_failed` and
|
||||
`provider_failed` codes, or when there is no error code. The mapping reuses the
|
||||
run failure code sets in `server/src/services/continuation-cause.ts`, and a
|
||||
test fails when a code in those sets has no telemetry code.
|
||||
|
||||
## Product Lifecycle Events
|
||||
|
||||
`model.connected`, `task.created`, `task.completed`, `task.blocked`,
|
||||
`plan.created`, `plan.approved`, and `artifact.created` describe the main
|
||||
product path. Their dimensions are enums, booleans, and counts only. They never
|
||||
contain a task title, a document body, a plan step, a connection or account
|
||||
name, an email, a URL, a repository name, or an endpoint address.
|
||||
|
||||
The server emits these events through
|
||||
`server/src/services/product-telemetry.ts`. Each emit site runs inside the
|
||||
transaction that writes the change, and the helper sends the event only after
|
||||
that transaction commits (`afterTransactionCommit` in `@paperclipai/db`). A
|
||||
rolled-back write sends nothing. This includes a write in a savepoint that
|
||||
rolls back while the outer transaction commits. The
|
||||
helper also wraps the track call in `withTelemetryCompany()`, so Cloud direct
|
||||
delivery gets the company for background writes.
|
||||
|
||||
These rules keep one event per real change:
|
||||
|
||||
- `task.created` is sent for a newly inserted task only. Chat conversations,
|
||||
company imports, and create requests that return an existing task do not send
|
||||
it. `created_by_kind` is `routine` for a routine-run task and `onboarding` for
|
||||
the first task from the onboarding wizard. Otherwise it is the acting kind.
|
||||
- `task.completed` and `task.blocked` are sent when a task's status changes
|
||||
from another value to `done` or `blocked`. Writing the same status again
|
||||
sends nothing. `issueService.update` covers most writers. The two direct
|
||||
writers that block a task (the dispatch check for unrunnable workspace
|
||||
settings and execution recovery) call the same helper as a system write.
|
||||
A new direct status writer must call it too.
|
||||
- `blocked_reason` is derived at the transition. A blocking task that is not
|
||||
done gives `waiting_on_task`. A cancelled blocker counts, because it stays
|
||||
unresolved until someone changes the relation. Otherwise, the unblock owner gives `waiting_for_person` or
|
||||
`waiting_for_agent`. A system write with neither gives `needs_recovery`. A
|
||||
person or agent write with neither gives `unspecified`.
|
||||
- `plan.created` is sent for the first revision of a task's `plan` document
|
||||
only.
|
||||
- `plan.approved` is sent when a person or agent accepts a confirmation request
|
||||
whose target is the task's `plan` document.
|
||||
- `model.connected` is sent for a new AI provider connection. A reconnect of an
|
||||
existing connection does not send it.
|
||||
- `artifact.created` is sent for a new task work product. A work product
|
||||
provider outside `paperclip`, `github`, `vercel`, and `s3` is sent as
|
||||
`custom`, so a private service name never leaves the installation.
|
||||
|
||||
The actor kind is `agent` when an agent acts, `user` when a person acts, and
|
||||
`system` when neither does.
|
||||
|
||||
On Cloud direct delivery, each of these events also names the responsible
|
||||
person with `withTelemetryActor()` and `resolveTelemetryPerson()`: the task
|
||||
creator, else the task's responsible user, else the run owner (the acting
|
||||
person, the plan author, or the user who owns the model connection). A live
|
||||
request still wins. The self-hosted collector envelope does not change.
|
||||
|
||||
## Cloud Direct Delivery
|
||||
|
||||
Self-hosted installs are unchanged. They post every batch to the collector,
|
||||
|
||||
@@ -148,6 +148,7 @@ export function trackAgentTaskRun(
|
||||
outputTokens?: number;
|
||||
cachedTokens?: number;
|
||||
taskId?: string;
|
||||
failureCause?: RawDimension<EventDimensionsMap["agent.task_run"]["failure_cause"]>;
|
||||
},
|
||||
): void {
|
||||
client.track("agent.task_run", {
|
||||
@@ -161,6 +162,106 @@ export function trackAgentTaskRun(
|
||||
...(dims.outputTokens === undefined ? {} : { output_tokens: dims.outputTokens }),
|
||||
...(dims.cachedTokens === undefined ? {} : { cached_tokens: dims.cachedTokens }),
|
||||
...(dims.taskId ? { task_id: emitPrivateRef(client, dims.taskId) } : {}),
|
||||
...(dims.failureCause ? { failure_cause: asEventDimension(dims.failureCause) } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
export function trackModelConnected(
|
||||
client: TelemetryClient,
|
||||
dims: {
|
||||
provider: RawDimension<EventDimensionsMap["model.connected"]["provider"]>;
|
||||
connectionMethod: RawDimension<EventDimensionsMap["model.connected"]["connection_method"]>;
|
||||
ownership: RawDimension<EventDimensionsMap["model.connected"]["ownership"]>;
|
||||
routingKind?: RawDimension<EventDimensionsMap["model.connected"]["routing_kind"]> | null;
|
||||
firstForCompany: boolean;
|
||||
},
|
||||
): void {
|
||||
client.track("model.connected", {
|
||||
provider: asEventDimension(dims.provider),
|
||||
connection_method: asEventDimension(dims.connectionMethod),
|
||||
ownership: asEventDimension(dims.ownership),
|
||||
...(dims.routingKind ? { routing_kind: asEventDimension(dims.routingKind) } : {}),
|
||||
first_for_company: dims.firstForCompany,
|
||||
});
|
||||
}
|
||||
|
||||
export function trackTaskCreated(
|
||||
client: TelemetryClient,
|
||||
dims: {
|
||||
createdByKind: RawDimension<EventDimensionsMap["task.created"]["created_by_kind"]>;
|
||||
isSubtask: boolean;
|
||||
},
|
||||
): void {
|
||||
client.track("task.created", {
|
||||
created_by_kind: asEventDimension(dims.createdByKind),
|
||||
is_subtask: dims.isSubtask,
|
||||
});
|
||||
}
|
||||
|
||||
export function trackTaskCompleted(
|
||||
client: TelemetryClient,
|
||||
dims: {
|
||||
completedByKind: RawDimension<EventDimensionsMap["task.completed"]["completed_by_kind"]>;
|
||||
isSubtask: boolean;
|
||||
},
|
||||
): void {
|
||||
client.track("task.completed", {
|
||||
completed_by_kind: asEventDimension(dims.completedByKind),
|
||||
is_subtask: dims.isSubtask,
|
||||
});
|
||||
}
|
||||
|
||||
export function trackTaskBlocked(
|
||||
client: TelemetryClient,
|
||||
dims: {
|
||||
blockedReason: RawDimension<EventDimensionsMap["task.blocked"]["blocked_reason"]>;
|
||||
blockedByKind: RawDimension<EventDimensionsMap["task.blocked"]["blocked_by_kind"]>;
|
||||
},
|
||||
): void {
|
||||
client.track("task.blocked", {
|
||||
blocked_reason: asEventDimension(dims.blockedReason),
|
||||
blocked_by_kind: asEventDimension(dims.blockedByKind),
|
||||
});
|
||||
}
|
||||
|
||||
export function trackPlanCreated(
|
||||
client: TelemetryClient,
|
||||
dims: {
|
||||
createdByKind: RawDimension<EventDimensionsMap["plan.created"]["created_by_kind"]>;
|
||||
atTaskCreation: boolean;
|
||||
},
|
||||
): void {
|
||||
client.track("plan.created", {
|
||||
created_by_kind: asEventDimension(dims.createdByKind),
|
||||
at_task_creation: dims.atTaskCreation,
|
||||
});
|
||||
}
|
||||
|
||||
export function trackPlanApproved(
|
||||
client: TelemetryClient,
|
||||
dims: {
|
||||
approvedByKind: RawDimension<EventDimensionsMap["plan.approved"]["approved_by_kind"]>;
|
||||
startsExecution: boolean;
|
||||
},
|
||||
): void {
|
||||
client.track("plan.approved", {
|
||||
approved_by_kind: asEventDimension(dims.approvedByKind),
|
||||
starts_execution: dims.startsExecution,
|
||||
});
|
||||
}
|
||||
|
||||
export function trackArtifactCreated(
|
||||
client: TelemetryClient,
|
||||
dims: {
|
||||
artifactType: RawDimension<EventDimensionsMap["artifact.created"]["artifact_type"]>;
|
||||
provider: RawDimension<EventDimensionsMap["artifact.created"]["provider"]>;
|
||||
creationFlow: RawDimension<EventDimensionsMap["artifact.created"]["creation_flow"]>;
|
||||
},
|
||||
): void {
|
||||
client.track("artifact.created", {
|
||||
artifact_type: asEventDimension(dims.artifactType),
|
||||
provider: asEventDimension(dims.provider),
|
||||
creation_flow: asEventDimension(dims.creationFlow),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,13 @@ input_tokens?: number
|
||||
output_tokens?: number
|
||||
cached_tokens?: number
|
||||
task_id?: string
|
||||
failure_cause?: ("missing_access" | "provider_limit" | "provider_error" | "waiting_for_person" | "task_changed" | "platform_error" | "agent_error" | "unknown")
|
||||
}
|
||||
|
||||
export interface PaperclipArtifactCreatedDimensions {
|
||||
artifact_type: ("preview_url" | "runtime_service" | "pull_request" | "branch" | "commit" | "artifact" | "document" | "other")
|
||||
provider: ("paperclip" | "github" | "vercel" | "s3" | "custom" | "other")
|
||||
creation_flow: ("direct" | "promotion" | "run_deliverable" | "other")
|
||||
}
|
||||
|
||||
export interface PaperclipCompanyImportedDimensions {
|
||||
@@ -109,6 +116,24 @@ source_run_id?: string
|
||||
legacy_inherited_restriction: boolean
|
||||
}
|
||||
|
||||
export interface PaperclipModelConnectedDimensions {
|
||||
provider: ("anthropic" | "openai" | "openrouter" | "xai" | "github" | "google" | "other")
|
||||
connection_method: ("subscription" | "api_key" | "other")
|
||||
ownership: ("personal" | "shared" | "other")
|
||||
routing_kind?: ("openrouter" | "bedrock" | "gateway" | "local" | "other")
|
||||
first_for_company: boolean
|
||||
}
|
||||
|
||||
export interface PaperclipPlanApprovedDimensions {
|
||||
approved_by_kind: ("user" | "agent" | "system" | "other")
|
||||
starts_execution: boolean
|
||||
}
|
||||
|
||||
export interface PaperclipPlanCreatedDimensions {
|
||||
created_by_kind: ("user" | "agent" | "system" | "other")
|
||||
at_task_creation: boolean
|
||||
}
|
||||
|
||||
export interface PaperclipProjectCreatedDimensions {
|
||||
|
||||
}
|
||||
@@ -131,11 +156,27 @@ source_type: ("local_path" | "github" | "url" | "catalog" | "skills_sh" | "unkno
|
||||
skill_ref?: string
|
||||
}
|
||||
|
||||
export interface PaperclipTaskBlockedDimensions {
|
||||
blocked_reason: ("waiting_on_task" | "waiting_for_person" | "waiting_for_agent" | "needs_recovery" | "unspecified" | "other")
|
||||
blocked_by_kind: ("user" | "agent" | "system" | "other")
|
||||
}
|
||||
|
||||
export interface PaperclipTaskCompletedDimensions {
|
||||
completed_by_kind: ("user" | "agent" | "system" | "other")
|
||||
is_subtask: boolean
|
||||
}
|
||||
|
||||
export interface PaperclipTaskCreatedDimensions {
|
||||
created_by_kind: ("user" | "agent" | "routine" | "onboarding" | "system" | "other")
|
||||
is_subtask: boolean
|
||||
}
|
||||
|
||||
export type PaperclipEventName =
|
||||
| "agent.created"
|
||||
| "agent.first_heartbeat"
|
||||
| "agent.task_completed"
|
||||
| "agent.task_run"
|
||||
| "artifact.created"
|
||||
| "company.imported"
|
||||
| "connection.created"
|
||||
| "connection.invoked"
|
||||
@@ -146,17 +187,24 @@ export type PaperclipEventName =
|
||||
| "install.started"
|
||||
| "interaction.created"
|
||||
| "interaction.resolved"
|
||||
| "model.connected"
|
||||
| "plan.approved"
|
||||
| "plan.created"
|
||||
| "project.created"
|
||||
| "routine.created"
|
||||
| "routine.run"
|
||||
| "server.started"
|
||||
| "skill.imported";
|
||||
| "skill.imported"
|
||||
| "task.blocked"
|
||||
| "task.completed"
|
||||
| "task.created";
|
||||
|
||||
export interface EventDimensionsMap {
|
||||
"agent.created": PaperclipAgentCreatedDimensions;
|
||||
"agent.first_heartbeat": PaperclipAgentFirstHeartbeatDimensions;
|
||||
"agent.task_completed": PaperclipAgentTaskCompletedDimensions;
|
||||
"agent.task_run": PaperclipAgentTaskRunDimensions;
|
||||
"artifact.created": PaperclipArtifactCreatedDimensions;
|
||||
"company.imported": PaperclipCompanyImportedDimensions;
|
||||
"connection.created": PaperclipConnectionCreatedDimensions;
|
||||
"connection.invoked": PaperclipConnectionInvokedDimensions;
|
||||
@@ -167,11 +215,17 @@ export interface EventDimensionsMap {
|
||||
"install.started": PaperclipInstallStartedDimensions;
|
||||
"interaction.created": PaperclipInteractionCreatedDimensions;
|
||||
"interaction.resolved": PaperclipInteractionResolvedDimensions;
|
||||
"model.connected": PaperclipModelConnectedDimensions;
|
||||
"plan.approved": PaperclipPlanApprovedDimensions;
|
||||
"plan.created": PaperclipPlanCreatedDimensions;
|
||||
"project.created": PaperclipProjectCreatedDimensions;
|
||||
"routine.created": PaperclipRoutineCreatedDimensions;
|
||||
"routine.run": PaperclipRoutineRunDimensions;
|
||||
"server.started": PaperclipServerStartedDimensions;
|
||||
"skill.imported": PaperclipSkillImportedDimensions;
|
||||
"task.blocked": PaperclipTaskBlockedDimensions;
|
||||
"task.completed": PaperclipTaskCompletedDimensions;
|
||||
"task.created": PaperclipTaskCreatedDimensions;
|
||||
}
|
||||
|
||||
export const PAPERCLIP_EVENTS = {
|
||||
@@ -179,6 +233,7 @@ export const PAPERCLIP_EVENTS = {
|
||||
"agent.first_heartbeat": "agent.first_heartbeat",
|
||||
"agent.task_completed": "agent.task_completed",
|
||||
"agent.task_run": "agent.task_run",
|
||||
"artifact.created": "artifact.created",
|
||||
"company.imported": "company.imported",
|
||||
"connection.created": "connection.created",
|
||||
"connection.invoked": "connection.invoked",
|
||||
@@ -189,11 +244,17 @@ export const PAPERCLIP_EVENTS = {
|
||||
"install.started": "install.started",
|
||||
"interaction.created": "interaction.created",
|
||||
"interaction.resolved": "interaction.resolved",
|
||||
"model.connected": "model.connected",
|
||||
"plan.approved": "plan.approved",
|
||||
"plan.created": "plan.created",
|
||||
"project.created": "project.created",
|
||||
"routine.created": "routine.created",
|
||||
"routine.run": "routine.run",
|
||||
"server.started": "server.started",
|
||||
"skill.imported": "skill.imported",
|
||||
"task.blocked": "task.blocked",
|
||||
"task.completed": "task.completed",
|
||||
"task.created": "task.created",
|
||||
} as const;
|
||||
|
||||
export const PAPERCLIP_ENUM_DESCRIPTIONS = {
|
||||
@@ -306,6 +367,42 @@ export const PAPERCLIP_ENUM_DESCRIPTIONS = {
|
||||
"failed": "The agent run reached a terminal state because of an unrecoverable error.",
|
||||
"cancelled": "The agent run was cancelled before it reached a successful terminal state.",
|
||||
"timed_out": "The agent run reached its time limit before it finished."
|
||||
},
|
||||
"failure_cause": {
|
||||
"missing_access": "The run lacked a sign-in, permission, secret, or repository access.",
|
||||
"provider_limit": "The model provider reported a rate limit, quota, or capacity limit.",
|
||||
"provider_error": "The model provider rejected the request or failed for another reason.",
|
||||
"waiting_for_person": "The run stopped because a person must review or reconcile its result.",
|
||||
"task_changed": "The task was reassigned, paused, closed, blocked, or stopped by a person while the run was active.",
|
||||
"platform_error": "Paperclip infrastructure, the workspace, or the sandbox failed or restarted.",
|
||||
"agent_error": "The agent's own output, tool use, or time limit ended the run.",
|
||||
"unknown": "The stored failure did not match a tracked code."
|
||||
}
|
||||
},
|
||||
"artifact.created": {
|
||||
"artifact_type": {
|
||||
"preview_url": "A preview deployment link.",
|
||||
"runtime_service": "A running workspace service.",
|
||||
"pull_request": "A pull request.",
|
||||
"branch": "A source branch.",
|
||||
"commit": "A source commit.",
|
||||
"artifact": "A file or other deliverable stored with the task.",
|
||||
"document": "A document work product.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
},
|
||||
"provider": {
|
||||
"paperclip": "Paperclip stores the artifact.",
|
||||
"github": "GitHub hosts the artifact.",
|
||||
"vercel": "Vercel hosts the artifact.",
|
||||
"s3": "An S3-compatible store hosts the artifact.",
|
||||
"custom": "Another system hosts the artifact.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
},
|
||||
"creation_flow": {
|
||||
"direct": "A person, agent, or integration recorded the artifact on the task.",
|
||||
"promotion": "A reviewer promoted an untrusted task file or document to an approved artifact.",
|
||||
"run_deliverable": "An agent run uploaded a file deliverable to the task.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
}
|
||||
},
|
||||
"company.imported": {
|
||||
@@ -502,6 +599,50 @@ export const PAPERCLIP_ENUM_DESCRIPTIONS = {
|
||||
"other": "Fallback when the target type is unknown or not represented by the tracked enum."
|
||||
}
|
||||
},
|
||||
"model.connected": {
|
||||
"provider": {
|
||||
"anthropic": "The connection signs in to Anthropic.",
|
||||
"openai": "The connection signs in to OpenAI.",
|
||||
"openrouter": "The connection signs in to OpenRouter.",
|
||||
"xai": "The connection signs in to xAI.",
|
||||
"github": "The connection signs in to GitHub Copilot.",
|
||||
"google": "The connection signs in to Google.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
},
|
||||
"connection_method": {
|
||||
"subscription": "The connection uses a provider subscription sign-in.",
|
||||
"api_key": "The connection uses a provider API key.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
},
|
||||
"ownership": {
|
||||
"personal": "The connection belongs to the person who connected it.",
|
||||
"shared": "The connection is shared with the company.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
},
|
||||
"routing_kind": {
|
||||
"openrouter": "The connection routes through OpenRouter.",
|
||||
"bedrock": "The connection routes through Amazon Bedrock.",
|
||||
"gateway": "The connection routes through a custom model gateway.",
|
||||
"local": "The connection routes to a model server on the same machine.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
}
|
||||
},
|
||||
"plan.approved": {
|
||||
"approved_by_kind": {
|
||||
"user": "A board member or other person accepted the plan confirmation.",
|
||||
"agent": "An agent accepted the plan confirmation.",
|
||||
"system": "Paperclip accepted the plan confirmation automatically, with no acting person or agent.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
}
|
||||
},
|
||||
"plan.created": {
|
||||
"created_by_kind": {
|
||||
"user": "A board member or other person wrote the first plan revision.",
|
||||
"agent": "An agent wrote the first plan revision.",
|
||||
"system": "Paperclip wrote the first plan revision automatically, with no acting person or agent.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
}
|
||||
},
|
||||
"routine.run": {
|
||||
"source": {
|
||||
"schedule": "Routine was triggered by a scheduled trigger.",
|
||||
@@ -545,6 +686,40 @@ export const PAPERCLIP_ENUM_DESCRIPTIONS = {
|
||||
"skills_sh": "Import source came from a Skills.sh-compatible source.",
|
||||
"unknown": "Source type could not be classified."
|
||||
}
|
||||
},
|
||||
"task.blocked": {
|
||||
"blocked_reason": {
|
||||
"waiting_on_task": "The task waits on one or more unfinished blocking tasks.",
|
||||
"waiting_for_person": "The task names a person or the board as the owner of the next action.",
|
||||
"waiting_for_agent": "The task names an agent as the owner of the next action.",
|
||||
"needs_recovery": "Paperclip blocked the task automatically for recovery, with no blocking task or unblock owner.",
|
||||
"unspecified": "A person or agent blocked the task without a blocking task or unblock owner.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
},
|
||||
"blocked_by_kind": {
|
||||
"user": "A board member or other person moved the task to blocked.",
|
||||
"agent": "An agent moved the task to blocked.",
|
||||
"system": "Paperclip moved the task to blocked automatically, with no acting person or agent.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
}
|
||||
},
|
||||
"task.completed": {
|
||||
"completed_by_kind": {
|
||||
"user": "A board member or other person moved the task to done.",
|
||||
"agent": "An agent moved the task to done.",
|
||||
"system": "Paperclip moved the task to done automatically, with no acting person or agent.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
}
|
||||
},
|
||||
"task.created": {
|
||||
"created_by_kind": {
|
||||
"user": "A board member or other person created the task.",
|
||||
"agent": "An agent created the task.",
|
||||
"routine": "A routine run created the task.",
|
||||
"onboarding": "The onboarding wizard created the workspace's first task when a person launched it.",
|
||||
"system": "Paperclip created the task automatically, with no acting person or agent.",
|
||||
"other": "Fallback when the value is unknown or not represented by the tracked enum."
|
||||
}
|
||||
}
|
||||
} as const;
|
||||
|
||||
|
||||
@@ -23,6 +23,13 @@ export {
|
||||
trackConnectionUpdated,
|
||||
trackConnectionInvoked,
|
||||
trackServerStarted,
|
||||
trackModelConnected,
|
||||
trackTaskCreated,
|
||||
trackTaskCompleted,
|
||||
trackTaskBlocked,
|
||||
trackPlanCreated,
|
||||
trackPlanApproved,
|
||||
trackArtifactCreated,
|
||||
} from "./events.js";
|
||||
export type {
|
||||
TelemetryConfig,
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { TelemetryClient } from "./client.js";
|
||||
import {
|
||||
trackAgentTaskRun,
|
||||
trackArtifactCreated,
|
||||
trackModelConnected,
|
||||
trackPlanApproved,
|
||||
trackPlanCreated,
|
||||
trackTaskBlocked,
|
||||
trackTaskCompleted,
|
||||
trackTaskCreated,
|
||||
} from "./events.js";
|
||||
import { PAPERCLIP_EVENTS } from "./generated/paperclip-telemetry.js";
|
||||
import { EVENT_RETENTION_CLASS } from "./retention.js";
|
||||
import type { TelemetryState } from "./types.js";
|
||||
|
||||
const TEST_STATE: TelemetryState = {
|
||||
installId: "test-install",
|
||||
salt: "test-salt",
|
||||
createdAt: "2026-01-01T00:00:00Z",
|
||||
firstSeenVersion: "0.0.0",
|
||||
};
|
||||
|
||||
const NEW_EVENTS = [
|
||||
"model.connected",
|
||||
"task.created",
|
||||
"task.completed",
|
||||
"task.blocked",
|
||||
"plan.created",
|
||||
"plan.approved",
|
||||
"artifact.created",
|
||||
] as const;
|
||||
|
||||
function makeClient() {
|
||||
return new TelemetryClient({ enabled: true, endpoint: "http://localhost:9999/ingest" }, () => TEST_STATE, "0.0.0-test", () => 0.5);
|
||||
}
|
||||
|
||||
function emitAll(client: TelemetryClient) {
|
||||
trackModelConnected(client, { provider: "openrouter", connectionMethod: "api_key", ownership: "shared", routingKind: "openrouter", firstForCompany: true });
|
||||
trackModelConnected(client, { provider: "anthropic", connectionMethod: "subscription", ownership: "personal", routingKind: null, firstForCompany: false });
|
||||
trackTaskCreated(client, { createdByKind: "onboarding", isSubtask: false });
|
||||
trackTaskCompleted(client, { completedByKind: "agent", isSubtask: true });
|
||||
trackTaskBlocked(client, { blockedReason: "waiting_on_task", blockedByKind: "user" });
|
||||
trackPlanCreated(client, { createdByKind: "agent", atTaskCreation: false });
|
||||
trackPlanApproved(client, { approvedByKind: "user", startsExecution: true });
|
||||
trackArtifactCreated(client, { artifactType: "pull_request", provider: "github", creationFlow: "direct" });
|
||||
trackAgentTaskRun(client, { agentId: "agent-1", state: "failed", failureCause: "provider_limit" });
|
||||
trackAgentTaskRun(client, { agentId: "agent-1", state: "succeeded" });
|
||||
}
|
||||
|
||||
describe("product lifecycle telemetry helpers", () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("registers every new event in the generated contract with enum/count retention", () => {
|
||||
for (const name of NEW_EVENTS) {
|
||||
expect(Object.hasOwn(PAPERCLIP_EVENTS, name)).toBe(true);
|
||||
expect(EVENT_RETENTION_CLASS[name]).toBe("operational_enum_count");
|
||||
}
|
||||
});
|
||||
|
||||
it("maps helper input to the registered dimension names and omits absent optional values", () => {
|
||||
const client = makeClient();
|
||||
const track = vi.spyOn(client, "track");
|
||||
emitAll(client);
|
||||
expect(track.mock.calls).toEqual([
|
||||
["model.connected", { provider: "openrouter", connection_method: "api_key", ownership: "shared", routing_kind: "openrouter", first_for_company: true }],
|
||||
["model.connected", { provider: "anthropic", connection_method: "subscription", ownership: "personal", first_for_company: false }],
|
||||
["task.created", { created_by_kind: "onboarding", is_subtask: false }],
|
||||
["task.completed", { completed_by_kind: "agent", is_subtask: true }],
|
||||
["task.blocked", { blocked_reason: "waiting_on_task", blocked_by_kind: "user" }],
|
||||
["plan.created", { created_by_kind: "agent", at_task_creation: false }],
|
||||
["plan.approved", { approved_by_kind: "user", starts_execution: true }],
|
||||
["artifact.created", { artifact_type: "pull_request", provider: "github", creation_flow: "direct" }],
|
||||
["agent.task_run", { agent_id: "agent-1", state: "failed", failure_cause: "provider_limit" }],
|
||||
["agent.task_run", { agent_id: "agent-1", state: "succeeded" }],
|
||||
]);
|
||||
});
|
||||
|
||||
it("sends a collector envelope with only the registered dimensions", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok: true }));
|
||||
const client = makeClient();
|
||||
emitAll(client);
|
||||
await client.flush();
|
||||
const body = JSON.parse(String(vi.mocked(fetch).mock.calls[0]?.[1]?.body));
|
||||
expect(body.events.map((event: { name: string }) => event.name)).toEqual([
|
||||
"model.connected", "model.connected", "task.created", "task.completed", "task.blocked",
|
||||
"plan.created", "plan.approved", "artifact.created", "agent.task_run", "agent.task_run",
|
||||
]);
|
||||
for (const event of body.events as Array<{ dimensions: Record<string, unknown> }>) {
|
||||
for (const value of Object.values(event.dimensions)) {
|
||||
expect(["string", "boolean", "number"]).toContain(typeof value);
|
||||
}
|
||||
expect(Object.keys(event.dimensions).every((key) => /^[a-z_]+$/.test(key))).toBe(true);
|
||||
}
|
||||
expect(body.events[2].dimensions).toEqual({ created_by_kind: "onboarding", is_subtask: false });
|
||||
});
|
||||
});
|
||||
@@ -48,4 +48,11 @@ export const EVENT_RETENTION_CLASS: Partial<Record<string, RetentionClass>> = {
|
||||
"interaction.created": "operational_enum_count",
|
||||
"interaction.resolved": "operational_enum_count",
|
||||
"server.started": "operational_enum_count",
|
||||
"model.connected": "operational_enum_count",
|
||||
"task.created": "operational_enum_count",
|
||||
"task.completed": "operational_enum_count",
|
||||
"task.blocked": "operational_enum_count",
|
||||
"plan.created": "operational_enum_count",
|
||||
"plan.approved": "operational_enum_count",
|
||||
"artifact.created": "operational_enum_count",
|
||||
};
|
||||
@@ -7,6 +7,8 @@ import {
|
||||
startEmbeddedPostgresTestDatabase,
|
||||
} from "./helpers/embedded-postgres.js";
|
||||
import { scopedTelemetryCompanyId, scopedTelemetryPerson } from "../telemetry-company-scope.js";
|
||||
import { EXECUTION_RECONCILIATION_CAUSES } from "@paperclipai/shared";
|
||||
import { PERMANENT_FAILURE_CODES, PERSON_OWNED_FAILURE_CODES, RESTART_CLASS_FAILURE_CODES, SETUP_FAILURE_CODES } from "../services/continuation-cause.ts";
|
||||
|
||||
const mockTelemetryClient = vi.hoisted(() => ({ track: vi.fn() }));
|
||||
const mockTrackAgentTaskRun = vi.hoisted(() => vi.fn());
|
||||
@@ -25,7 +27,7 @@ vi.mock("@paperclipai/shared/telemetry", async () => {
|
||||
};
|
||||
});
|
||||
|
||||
import { emitAgentTaskRun, emitAgentTaskRunById } from "../services/agent-task-run-telemetry.ts";
|
||||
import { agentRunFailureCause, emitAgentTaskRun, emitAgentTaskRunById } from "../services/agent-task-run-telemetry.ts";
|
||||
|
||||
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
||||
@@ -393,4 +395,62 @@ describeEmbeddedPostgres("emitAgentTaskRun", () => {
|
||||
expect(dims).not.toHaveProperty("adapterType");
|
||||
expect(dims).not.toHaveProperty("agentRole");
|
||||
});
|
||||
|
||||
it("sends a fixed failure_cause from the stored error code and provider category for failed runs", async () => {
|
||||
await seedCompanyAndAgent();
|
||||
const [quota] = await db.insert(heartbeatRuns).values({ companyId, agentId, status: "failed", errorCode: "adapter_failed",
|
||||
error: "provider said: secret detail", resultJson: { providerFailure: { category: "rate_limit", message: "secret detail" } } }).returning();
|
||||
const [lost] = await db.insert(heartbeatRuns).values({ companyId, agentId, status: "interrupted", errorCode: "process_lost" }).returning();
|
||||
const [done] = await db.insert(heartbeatRuns).values({ companyId, agentId, status: "succeeded", errorCode: null }).returning();
|
||||
|
||||
await emitAgentTaskRun(db, quota);
|
||||
await emitAgentTaskRunById(db, { companyId, runId: lost.id });
|
||||
await emitAgentTaskRun(db, done);
|
||||
|
||||
const sent = mockTrackAgentTaskRun.mock.calls.map((call) => call[1]);
|
||||
expect(sent[0].failureCause).toBe("provider_limit");
|
||||
expect(sent[1].failureCause).toBe("platform_error");
|
||||
expect(sent[2]).not.toHaveProperty("failureCause");
|
||||
expect(JSON.stringify(sent)).not.toContain("secret detail");
|
||||
});
|
||||
});
|
||||
|
||||
describe("agentRunFailureCause", () => {
|
||||
it.each([
|
||||
[{ errorCode: "provider_auth_failed", providerFailureCategory: null }, "missing_access"],
|
||||
[{ errorCode: "adapter_failed", providerFailureCategory: "authentication" }, "missing_access"],
|
||||
[{ errorCode: "provider_quota", providerFailureCategory: null }, "provider_limit"],
|
||||
[{ errorCode: "adapter_failed", providerFailureCategory: "overloaded" }, "provider_limit"],
|
||||
[{ errorCode: "native_provider_model_rejected", providerFailureCategory: null }, "provider_error"],
|
||||
[{ errorCode: "adapter_failed", providerFailureCategory: "model_unavailable" }, "provider_error"],
|
||||
[{ errorCode: "provider_transport_failed", providerFailureCategory: "rate_limit" }, "provider_error"],
|
||||
[{ errorCode: "native_provider_usage_limit", providerFailureCategory: "authentication" }, "provider_limit"],
|
||||
[{ errorCode: "budget_hard_stop", providerFailureCategory: null }, "waiting_for_person"],
|
||||
[{ errorCode: "agent_not_invokable", providerFailureCategory: null }, "platform_error"],
|
||||
[{ errorCode: "execution_reconciliation_required", providerFailureCategory: null }, "waiting_for_person"],
|
||||
[{ errorCode: "native_session_cleanup_quarantined", providerFailureCategory: null }, "waiting_for_person"],
|
||||
[{ errorCode: "issue_reassigned", providerFailureCategory: null }, "task_changed"],
|
||||
[{ errorCode: "operator_interrupted", providerFailureCategory: null }, "task_changed"],
|
||||
[{ errorCode: "process_lost", providerFailureCategory: null }, "platform_error"],
|
||||
[{ errorCode: "setup_failed", providerFailureCategory: "rate_limit" }, "platform_error"],
|
||||
[{ errorCode: "adapter_failed", providerFailureCategory: null }, "agent_error"],
|
||||
[{ errorCode: "timeout", providerFailureCategory: null }, "agent_error"],
|
||||
[{ errorCode: "some_future_code", providerFailureCategory: null }, "unknown"],
|
||||
[{ errorCode: null, providerFailureCategory: null }, "unknown"],
|
||||
] as const)("maps %o to %s", (input, expected) => {
|
||||
expect(agentRunFailureCause(input)).toBe(expected);
|
||||
});
|
||||
|
||||
it("classifies every canonical run failure code", () => {
|
||||
const canonical = [
|
||||
...PERSON_OWNED_FAILURE_CODES,
|
||||
...PERMANENT_FAILURE_CODES,
|
||||
...RESTART_CLASS_FAILURE_CODES,
|
||||
...SETUP_FAILURE_CODES,
|
||||
...EXECUTION_RECONCILIATION_CAUSES,
|
||||
];
|
||||
const unclassified = canonical.filter((errorCode) =>
|
||||
agentRunFailureCause({ errorCode, providerFailureCategory: null }) === "unknown");
|
||||
expect(unclassified).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -48,6 +48,7 @@ import { buildEffectiveRunWorkspaceConfigMetadata, heartbeatService } from "../s
|
||||
import { resolveManagedProjectWorkspaceDir } from "../home-paths.js";
|
||||
import { noticeMetadataReferencesRecoveryAction } from "../services/recovery/index.ts";
|
||||
import { instanceSettingsService } from "../services/instance-settings.ts";
|
||||
import * as productTelemetry from "../services/product-telemetry.ts";
|
||||
import { waitForPendingRunFailureReports } from "../services/run-failure-report.ts";
|
||||
import {
|
||||
WORKSPACE_WORKTREE_REQUIRES_PROJECT_CODE,
|
||||
@@ -1178,11 +1179,15 @@ describeEmbeddedPostgres("heartbeat workspace branch containment", () => {
|
||||
const routineEvents: string[] = [];
|
||||
const unsubscribeRoutine = subscribeDatabaseWork(db, DELIVERY_QUEUES.routineExecutionStatus, event => routineEvents.push(event));
|
||||
const heartbeat = heartbeatService(db);
|
||||
const blockedTelemetry = vi.spyOn(productTelemetry, "emitSystemTaskBlockedAfterCommit");
|
||||
let run;
|
||||
try {
|
||||
run = await heartbeat.wakeup(agentId, { source: "assignment", triggerDetail: "system", reason: "issue_assigned",
|
||||
payload: { issueId }, contextSnapshot: { issueId, wakeReason: "issue_assigned" } });
|
||||
} finally { unsubscribeRoutine(); }
|
||||
expect(blockedTelemetry.mock.calls.map(([, task, previousStatus]) => [task.id, task.companyId, previousStatus]))
|
||||
.toEqual([[issueId, companyId, "todo"]]);
|
||||
blockedTelemetry.mockRestore();
|
||||
expect(routineEvents).toContain("intent"); expect(routineEvents).toContain("settled");
|
||||
expect(run).toBeNull();
|
||||
expect(adapterExecute).not.toHaveBeenCalled();
|
||||
|
||||
@@ -44,6 +44,7 @@ import {
|
||||
} from "@paperclipai/shared";
|
||||
import { instanceSettingsService } from "../services/instance-settings.js";
|
||||
import { issueService } from "../services/issues.js";
|
||||
import * as productTelemetry from "../services/product-telemetry.js";
|
||||
import { issueThreadInteractionService } from "../services/issue-thread-interactions.js";
|
||||
import { agentService } from "../services/agents.js";
|
||||
|
||||
@@ -3235,8 +3236,16 @@ describeEmbeddedPostgres("issueThreadInteractionService", () => {
|
||||
payload: { version: 1, prompt: "Accept this plan?", target },
|
||||
}, { agentId });
|
||||
|
||||
const planApproved = vi.spyOn(productTelemetry, "emitPlanApprovedAfterCommit");
|
||||
const accepted = await interactionsSvc.acceptInteraction(acceptIssueScope(issueId, companyId, goalId), created.id, {}, { userId: "local-board" });
|
||||
|
||||
expect(planApproved).toHaveBeenCalledExactlyOnceWith({
|
||||
companyId,
|
||||
actor: { userId: "local-board" },
|
||||
startsExecution: true,
|
||||
task: { createdByUserId: null, responsibleUserId: null },
|
||||
});
|
||||
planApproved.mockRestore();
|
||||
expect(accepted.interaction).toMatchObject({
|
||||
id: created.id,
|
||||
status: "accepted",
|
||||
@@ -3267,6 +3276,7 @@ describeEmbeddedPostgres("issueThreadInteractionService", () => {
|
||||
assigneeAgentId: agentId,
|
||||
}).where(eq(issues.id, issueId));
|
||||
|
||||
const planApproved = vi.spyOn(productTelemetry, "emitPlanApprovedAfterCommit");
|
||||
const nonPlan = await interactionsSvc.create({ id: issueId, companyId }, {
|
||||
kind: "request_confirmation",
|
||||
payload: { version: 1, prompt: "Accept this unrelated decision?" },
|
||||
@@ -3274,6 +3284,8 @@ describeEmbeddedPostgres("issueThreadInteractionService", () => {
|
||||
await interactionsSvc.acceptInteraction(acceptIssueScope(issueId, companyId, goalId), nonPlan.id, {}, {
|
||||
userId: "local-board",
|
||||
});
|
||||
expect(planApproved).not.toHaveBeenCalled();
|
||||
planApproved.mockRestore();
|
||||
await expect(db.select().from(issues).where(eq(issues.id, issueId)).then((rows) => rows[0]?.workMode))
|
||||
.resolves.toBe("planning");
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import path from "node:path";
|
||||
import express from "express";
|
||||
import request from "supertest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { trackAgentTaskRun, trackCompanyImported, trackRoutineCreated, trackRoutineRun } from "@paperclipai/shared/telemetry";
|
||||
import { trackAgentTaskRun, trackCompanyImported, trackRoutineCreated, trackRoutineRun, trackPlanApproved, trackTaskBlocked, trackTaskCompleted, trackTaskCreated } from "@paperclipai/shared/telemetry";
|
||||
|
||||
const hoisted = vi.hoisted(() => ({ instanceRoot: "", posthogNodeLoads: 0 }));
|
||||
|
||||
@@ -193,6 +193,26 @@ describe("server telemetry sender selection", () => {
|
||||
expect(body).not.toContain("attributed_via");
|
||||
});
|
||||
|
||||
it("self-hosted: a person scope leaves product lifecycle collector events unchanged", async () => {
|
||||
const client = initTelemetry({ enabled: true }, { env: {} })!;
|
||||
const person = resolveTelemetryPerson({ taskCreatedByUserId: "user-creator" });
|
||||
withTelemetryActor({ companyId: "company-bg", person }, () => {
|
||||
trackTaskCompleted(client, { completedByKind: "agent", isSubtask: false });
|
||||
trackTaskCreated(client, { createdByKind: "onboarding", isSubtask: false });
|
||||
});
|
||||
await shutdownTelemetry();
|
||||
|
||||
const body = String((fetchMock.mock.calls[0]![1] as RequestInit).body);
|
||||
const events = JSON.parse(body).events.filter((event: { name: string }) => event.name !== "server.started");
|
||||
expect(events.map((event: { name: string; dimensions: unknown }) => [event.name, event.dimensions])).toEqual([
|
||||
["task.completed", { completed_by_kind: "agent", is_subtask: false }],
|
||||
["task.created", { created_by_kind: "onboarding", is_subtask: false }],
|
||||
]);
|
||||
expect(body).not.toContain("user-creator");
|
||||
expect(body).not.toContain("attributed_via");
|
||||
expect(body).not.toContain("company-bg");
|
||||
});
|
||||
|
||||
it("self-hosted: ignores posthog-js tracing headers", async () => {
|
||||
expect(postHogRequestContextMiddlewareForEnv({})).toBeNull();
|
||||
expect(postHogRequestContextMiddlewareForEnv({ PAPERCLIP_POSTHOG_PROJECT_TOKEN: "phc_test" })).toBeNull();
|
||||
@@ -442,6 +462,32 @@ describe("server telemetry sender selection", () => {
|
||||
expect(unscoped.properties).not.toHaveProperty("company_id");
|
||||
});
|
||||
|
||||
it("Cloud: background product lifecycle events carry the company in scope", async () => {
|
||||
const posthog = fakePostHog();
|
||||
const client = initTelemetry({ enabled: true }, { env: CLOUD_ENV, postHogFactory: posthog.factory })!;
|
||||
const person = resolveTelemetryPerson({ taskCreatedByUserId: "user-creator" });
|
||||
withTelemetryActor({ companyId: "company-bg", person }, () => {
|
||||
trackTaskCreated(client, { createdByKind: "routine", isSubtask: false });
|
||||
trackTaskCompleted(client, { completedByKind: "agent", isSubtask: true });
|
||||
trackTaskBlocked(client, { blockedReason: "waiting_on_task", blockedByKind: "system" });
|
||||
trackAgentTaskRun(client, { agentId: "agent-1", state: "failed", failureCause: "provider_limit" });
|
||||
});
|
||||
withTelemetryCompany("company-bg", () => trackPlanApproved(client, { approvedByKind: "system", startsExecution: false }));
|
||||
await shutdownTelemetry();
|
||||
|
||||
for (const event of ["task.created", "task.completed", "task.blocked", "agent.task_run"]) {
|
||||
const message = posthog.captured.find((captured) => captured.event === event)!;
|
||||
expect(message.distinctId).toBe("user-creator");
|
||||
expect(message.properties).toMatchObject({ company_id: "company-bg", stack_id: "stack-123",
|
||||
user_id: "user-creator", attributed_via: "task_creator" });
|
||||
}
|
||||
const unattributed = posthog.captured.find((captured) => captured.event === "plan.approved")!;
|
||||
expect(unattributed.distinctId).toBe("workspace:stack-123");
|
||||
expect(unattributed.properties).toMatchObject({ company_id: "company-bg", attributed_via: "none", $process_person_profile: false });
|
||||
expect(posthog.captured.find((captured) => captured.event === "agent.task_run")!.properties)
|
||||
.toMatchObject({ failure_cause: "provider_limit", normalized_properties: [] });
|
||||
});
|
||||
|
||||
it("Cloud: a live request's company wins over a background company scope", async () => {
|
||||
const posthog = fakePostHog();
|
||||
initTelemetry({ enabled: true }, { env: CLOUD_ENV, postHogFactory: posthog.factory });
|
||||
|
||||
@@ -0,0 +1,274 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { mkdtemp, realpath, rm } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { agents, companies, companyMemberships, createDb, issues } from "@paperclipai/db";
|
||||
import {
|
||||
getEmbeddedPostgresTestSupport,
|
||||
startEmbeddedPostgresTestDatabase,
|
||||
} from "./helpers/embedded-postgres.js";
|
||||
import { scopedTelemetryCompanyId, scopedTelemetryPerson } from "../telemetry-company-scope.js";
|
||||
|
||||
const mockTelemetryClient = vi.hoisted(() => ({ track: vi.fn() }));
|
||||
const calls = vi.hoisted(() => [] as Array<{ event: string; dims: Record<string, unknown>; companyId: string | undefined }>);
|
||||
const people = vi.hoisted(() => [] as Array<{ event: string; person: unknown }>);
|
||||
|
||||
vi.mock("../telemetry.js", () => ({
|
||||
getTelemetryClient: () => mockTelemetryClient,
|
||||
}));
|
||||
|
||||
vi.mock("@paperclipai/shared/telemetry", async () => {
|
||||
const actual = await vi.importActual<typeof import("@paperclipai/shared/telemetry")>("@paperclipai/shared/telemetry");
|
||||
const record = (event: string) => (_client: unknown, dims: Record<string, unknown>) => {
|
||||
calls.push({ event, dims, companyId: scopedTelemetryCompanyId() });
|
||||
people.push({ event, person: scopedTelemetryPerson() ?? null });
|
||||
};
|
||||
return {
|
||||
...actual,
|
||||
trackTaskCreated: record("task.created"),
|
||||
trackTaskCompleted: record("task.completed"),
|
||||
trackTaskBlocked: record("task.blocked"),
|
||||
trackPlanCreated: record("plan.created"),
|
||||
trackPlanApproved: record("plan.approved"),
|
||||
trackArtifactCreated: record("artifact.created"),
|
||||
trackModelConnected: record("model.connected"),
|
||||
};
|
||||
});
|
||||
|
||||
import { issueService } from "../services/issues.ts";
|
||||
import { documentService } from "../services/documents.ts";
|
||||
import { workProductService } from "../services/work-products.ts";
|
||||
import { aiConnectionService } from "../services/ai-connections.ts";
|
||||
import { hasUnfinishedBlockers } from "../services/issue-dependency-wakeups.ts";
|
||||
import { emitSystemTaskBlockedAfterCommit, taskBlockedReason, telemetryActorKind } from "../services/product-telemetry.ts";
|
||||
|
||||
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
||||
|
||||
describe("product telemetry classification", () => {
|
||||
it("classifies the acting kind", () => {
|
||||
expect(telemetryActorKind({ agentId: "a", userId: "u" })).toBe("agent");
|
||||
expect(telemetryActorKind({ userId: "u" })).toBe("user");
|
||||
expect(telemetryActorKind({})).toBe("system");
|
||||
});
|
||||
|
||||
it.each([
|
||||
[{ hasOpenBlockers: true, unblockDescriptor: { owner: "board" as const, action: "x" }, actorKind: "agent" as const }, "waiting_on_task"],
|
||||
[{ hasOpenBlockers: false, unblockDescriptor: { owner: "board" as const, action: "x" }, actorKind: "agent" as const }, "waiting_for_person"],
|
||||
[{ hasOpenBlockers: false, unblockDescriptor: { owner: { userId: "u" }, action: "x" }, actorKind: "agent" as const }, "waiting_for_person"],
|
||||
[{ hasOpenBlockers: false, unblockDescriptor: { owner: { agentId: "a" }, action: "x" }, actorKind: "user" as const }, "waiting_for_agent"],
|
||||
[{ hasOpenBlockers: false, unblockDescriptor: null, actorKind: "system" as const }, "needs_recovery"],
|
||||
[{ hasOpenBlockers: false, unblockDescriptor: null, actorKind: "user" as const }, "unspecified"],
|
||||
])("maps %o to %s", (input, expected) => {
|
||||
expect(taskBlockedReason(input)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describeEmbeddedPostgres("product telemetry emit sites", () => {
|
||||
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
|
||||
let db: ReturnType<typeof createDb>;
|
||||
let companyId: string;
|
||||
let agentId: string;
|
||||
|
||||
let home: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
home = await realpath(await mkdtemp(path.join(os.tmpdir(), "paperclip-product-telemetry-")));
|
||||
vi.stubEnv("PAPERCLIP_HOME", home);
|
||||
vi.stubEnv("PAPERCLIP_INSTANCE_ID", "product-telemetry-fixture");
|
||||
tempDb = await startEmbeddedPostgresTestDatabase("paperclip-product-telemetry-");
|
||||
db = createDb(tempDb.connectionString);
|
||||
}, 60_000);
|
||||
|
||||
afterAll(async () => {
|
||||
await tempDb?.cleanup();
|
||||
vi.unstubAllEnvs();
|
||||
if (home) await rm(home, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
calls.length = 0;
|
||||
people.length = 0;
|
||||
companyId = randomUUID();
|
||||
agentId = randomUUID();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Paperclip",
|
||||
issuePrefix: `P${companyId.replace(/-/g, "").slice(0, 5).toUpperCase()}`,
|
||||
requireBoardApprovalForNewAgents: false,
|
||||
});
|
||||
await db.insert(agents).values({
|
||||
id: agentId, companyId, name: "Coder", role: "engineer", status: "active",
|
||||
adapterType: "codex_local", adapterConfig: {}, runtimeConfig: {}, permissions: {},
|
||||
});
|
||||
});
|
||||
|
||||
async function seedIssue(values: Partial<typeof issues.$inferInsert> = {}) {
|
||||
const id = randomUUID();
|
||||
await db.insert(issues).values({ id, companyId, title: `Task ${id.slice(0, 6)}`, status: "todo", priority: "medium", ...values });
|
||||
return id;
|
||||
}
|
||||
|
||||
const sent = (event: string) => calls.filter((call) => call.event === event);
|
||||
|
||||
it("emits task.created once per committed task in the company scope, with the creator kind", async () => {
|
||||
const parent = await issueService(db).create(companyId, { title: "Parent", createdByUserId: "user-1" });
|
||||
await issueService(db).create(companyId, { title: "Child", parentId: parent.id, createdByAgentId: agentId });
|
||||
await issueService(db).create(companyId, { title: "First task", createdByUserId: "user-1", originKind: "onboarding_first_task" });
|
||||
|
||||
expect(sent("task.created")).toEqual([
|
||||
{ event: "task.created", dims: { createdByKind: "user", isSubtask: false }, companyId },
|
||||
{ event: "task.created", dims: { createdByKind: "agent", isSubtask: true }, companyId },
|
||||
{ event: "task.created", dims: { createdByKind: "onboarding", isSubtask: false }, companyId },
|
||||
]);
|
||||
expect(scopedTelemetryCompanyId()).toBeUndefined();
|
||||
expect(people.map((entry) => entry.person)).toEqual([
|
||||
{ userId: "user-1", attributedVia: "task_creator" },
|
||||
{ userId: "user-1", attributedVia: "task_assigner" },
|
||||
{ userId: "user-1", attributedVia: "task_creator" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("attributes agent-driven lifecycle events to the task's person on the direct path", async () => {
|
||||
const fromPerson = await seedIssue({ status: "in_progress", createdByUserId: "creator-1", responsibleUserId: "owner-1" });
|
||||
const fromOwner = await seedIssue({ status: "in_progress", responsibleUserId: "owner-1" });
|
||||
const unowned = await seedIssue({ status: "in_progress" });
|
||||
people.length = 0;
|
||||
await issueService(db).update(fromPerson, { status: "done", actorAgentId: agentId });
|
||||
await issueService(db).update(fromOwner, { status: "done", actorAgentId: agentId });
|
||||
await issueService(db).update(unowned, { status: "done", actorAgentId: agentId });
|
||||
|
||||
expect(people).toEqual([
|
||||
{ event: "task.completed", person: { userId: "creator-1", attributedVia: "task_creator" } },
|
||||
{ event: "task.completed", person: { userId: "owner-1", attributedVia: "task_assigner" } },
|
||||
{ event: "task.completed", person: null },
|
||||
]);
|
||||
});
|
||||
|
||||
it("emits nothing for a create whose transaction rolls back", async () => {
|
||||
await expect(db.transaction(async (tx) => {
|
||||
await issueService(tx as unknown as typeof db).create(companyId, { title: "Rolled back", createdByUserId: "user-1" }, tx);
|
||||
throw new Error("rollback");
|
||||
})).rejects.toThrow("rollback");
|
||||
expect(sent("task.created")).toEqual([]);
|
||||
});
|
||||
|
||||
it("emits task.completed once per transition to done", async () => {
|
||||
const id = await seedIssue({ status: "in_progress" });
|
||||
await issueService(db).update(id, { status: "done", actorAgentId: agentId });
|
||||
await issueService(db).update(id, { status: "done", actorAgentId: agentId });
|
||||
await issueService(db).update(id, { status: "todo", actorUserId: "user-1" });
|
||||
await issueService(db).update(id, { status: "done" });
|
||||
|
||||
expect(sent("task.completed").map((call) => [call.dims, call.companyId])).toEqual([
|
||||
[{ completedByKind: "agent", isSubtask: false }, companyId],
|
||||
[{ completedByKind: "system", isSubtask: false }, companyId],
|
||||
]);
|
||||
});
|
||||
|
||||
it("emits task.blocked with the blocking reason at the transition", async () => {
|
||||
const blocker = await seedIssue({ status: "todo" });
|
||||
const waiting = await seedIssue({ status: "todo" });
|
||||
const recovering = await seedIssue({ status: "in_progress" });
|
||||
const forPerson = await seedIssue({ status: "in_progress" });
|
||||
const cancelledBlocker = await seedIssue({ status: "cancelled" });
|
||||
const waitingOnCancelled = await seedIssue({ status: "todo" });
|
||||
await issueService(db).update(waiting, { status: "blocked", blockedByIssueIds: [blocker], actorAgentId: agentId });
|
||||
await issueService(db).update(recovering, { status: "blocked" });
|
||||
await issueService(db).update(forPerson, { status: "blocked", unblockDescriptor: { owner: "board", action: "Approve" }, actorAgentId: agentId });
|
||||
await issueService(db).update(forPerson, { status: "blocked", actorAgentId: agentId });
|
||||
await issueService(db).update(waitingOnCancelled, { status: "blocked", blockedByIssueIds: [cancelledBlocker], actorUserId: "user-1" });
|
||||
|
||||
expect(sent("task.blocked").map((call) => call.dims)).toEqual([
|
||||
{ blockedReason: "waiting_on_task", blockedByKind: "agent" },
|
||||
{ blockedReason: "needs_recovery", blockedByKind: "system" },
|
||||
{ blockedReason: "waiting_for_person", blockedByKind: "agent" },
|
||||
{ blockedReason: "waiting_on_task", blockedByKind: "user" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("emits a direct system block with the task's real blockers and unblock owner", async () => {
|
||||
const blocker = await seedIssue({ status: "cancelled" });
|
||||
const dependent = await seedIssue({ status: "blocked" });
|
||||
const ownerWait = await seedIssue({ status: "blocked", unblockDescriptor: { owner: { userId: "user-1" }, action: "Share access" } });
|
||||
const recovery = await seedIssue({ status: "blocked" });
|
||||
await issueService(db).update(dependent, { blockedByIssueIds: [blocker] });
|
||||
calls.length = 0;
|
||||
const load = async (id: string) => (await db.select().from(issues).where(eq(issues.id, id)))[0]!;
|
||||
await db.transaction(async (tx) => {
|
||||
await emitSystemTaskBlockedAfterCommit(tx, await load(dependent), "in_progress");
|
||||
await emitSystemTaskBlockedAfterCommit(tx, await load(ownerWait), "todo");
|
||||
await emitSystemTaskBlockedAfterCommit(tx, await load(recovery), "in_progress");
|
||||
await emitSystemTaskBlockedAfterCommit(tx, await load(recovery), "blocked");
|
||||
expect(sent("task.blocked")).toEqual([]);
|
||||
});
|
||||
|
||||
expect(sent("task.blocked").map((call) => [call.dims, call.companyId])).toEqual([
|
||||
[{ blockedReason: "waiting_on_task", blockedByKind: "system" }, companyId],
|
||||
[{ blockedReason: "waiting_for_person", blockedByKind: "system" }, companyId],
|
||||
[{ blockedReason: "needs_recovery", blockedByKind: "system" }, companyId],
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps the block write when the blocker read fails", async () => {
|
||||
const id = await seedIssue({ status: "in_progress" });
|
||||
await db.transaction(async (tx) => {
|
||||
await tx.update(issues).set({ status: "blocked" }).where(eq(issues.id, id));
|
||||
expect(await hasUnfinishedBlockers(tx, { companyId: "not-a-uuid", id })).toBe(false);
|
||||
await tx.update(issues).set({ updatedAt: new Date() }).where(eq(issues.id, id));
|
||||
});
|
||||
expect((await db.select({ status: issues.status }).from(issues).where(eq(issues.id, id)))[0]?.status).toBe("blocked");
|
||||
});
|
||||
|
||||
it("emits plan.created for the first plan revision only", async () => {
|
||||
const id = await seedIssue();
|
||||
const documents = documentService(db);
|
||||
const first = await documents.upsertIssueDocument({ issueId: id, key: "plan", format: "markdown", body: "1. Do it", createdByAgentId: agentId });
|
||||
await documents.upsertIssueDocument({ issueId: id, key: "plan", format: "markdown", body: "1. Do it well",
|
||||
baseRevisionId: first.document.latestRevisionId, createdByAgentId: agentId });
|
||||
await documents.upsertIssueDocument({ issueId: id, key: "notes", format: "markdown", body: "Not a plan", createdByUserId: "user-1" });
|
||||
|
||||
expect(sent("plan.created")).toEqual([
|
||||
{ event: "plan.created", dims: { createdByKind: "agent", atTaskCreation: false }, companyId },
|
||||
]);
|
||||
});
|
||||
|
||||
it("emits artifact.created with the work product type and provider", async () => {
|
||||
const id = await seedIssue();
|
||||
await workProductService(db).createForIssue(id, companyId, {
|
||||
type: "pull_request", provider: "github", title: "Fix", status: "open", url: "https://example.invalid/pr/1",
|
||||
});
|
||||
await workProductService(db).createForIssue(id, companyId, {
|
||||
type: "preview_url", provider: "acme-private-deploys", title: "Preview", status: "active",
|
||||
});
|
||||
expect(sent("artifact.created")).toEqual([
|
||||
{ event: "artifact.created", dims: { artifactType: "pull_request", provider: "github", creationFlow: "direct" }, companyId },
|
||||
{ event: "artifact.created", dims: { artifactType: "preview_url", provider: "custom", creationFlow: "direct" }, companyId },
|
||||
]);
|
||||
expect(JSON.stringify(sent("artifact.created"))).not.toContain("acme");
|
||||
expect(people.filter((entry) => entry.event === "artifact.created").map((entry) => entry.person)).toEqual([null, null]);
|
||||
const owned = await seedIssue({ createdByUserId: "creator-1" });
|
||||
people.length = 0;
|
||||
await workProductService(db).createForIssue(owned, companyId, { type: "branch", provider: "github", title: "Branch", status: "active" });
|
||||
expect(people).toEqual([{ event: "artifact.created", person: { userId: "creator-1", attributedVia: "task_creator" } }]);
|
||||
});
|
||||
|
||||
it("emits model.connected for a new model connection and not for its reconnect", async () => {
|
||||
await db.insert(companyMemberships).values({ companyId, principalId: "alice", principalType: "user", status: "active", membershipRole: "member" });
|
||||
const service = aiConnectionService(db);
|
||||
const first = await service.save(companyId, "alice", { provider: "anthropic", method: "api_key", ownership: "personal", name: "Alice key",
|
||||
apiKey: "fixture-key", agentIds: [], allAgents: true }, "fixture-key");
|
||||
await service.save(companyId, "alice", { provider: "openrouter", method: "api_key", ownership: "shared", name: "Shared router",
|
||||
apiKey: "fixture-router", agentIds: [], allAgents: true, routing: { kind: "openrouter", protocol: "chat", auth: "bearer", models: [] } }, "fixture-router");
|
||||
await service.save(companyId, "alice", { provider: "anthropic", method: "api_key", ownership: "personal", name: "Alice key",
|
||||
apiKey: "fixture-key-2", agentIds: [], allAgents: true, connectionId: first.connectionId }, "fixture-key-2");
|
||||
|
||||
expect(sent("model.connected")).toEqual([
|
||||
{ event: "model.connected", dims: { provider: "anthropic", connectionMethod: "api_key", ownership: "personal", routingKind: null, firstForCompany: true }, companyId },
|
||||
{ event: "model.connected", dims: { provider: "openrouter", connectionMethod: "api_key", ownership: "shared", routingKind: "openrouter", firstForCompany: false }, companyId },
|
||||
]);
|
||||
expect(JSON.stringify(sent("model.connected"))).not.toContain("Alice");
|
||||
});
|
||||
});
|
||||
@@ -173,11 +173,16 @@ describe("workProductService", () => {
|
||||
const insertValues = vi.fn(() => ({ returning: insertReturning }));
|
||||
const txInsert = vi.fn(() => ({ values: insertValues }));
|
||||
|
||||
const ownerWhere = vi.fn(async () => [{ createdByUserId: "user-1", responsibleUserId: null }]);
|
||||
const txSelect = vi.fn(() => ({ from: vi.fn(() => ({ where: ownerWhere })) }));
|
||||
|
||||
let transactionOpen = false;
|
||||
const tx = {
|
||||
execute: retentionReads(() => transactionOpen),
|
||||
update: txUpdate,
|
||||
insert: txInsert,
|
||||
select: txSelect,
|
||||
transaction: async (read: (input: unknown) => Promise<unknown>) => read(tx),
|
||||
};
|
||||
const transaction = vi.fn(async (callback: (input: typeof tx) => Promise<unknown>) => {
|
||||
transactionOpen = true;
|
||||
@@ -197,6 +202,30 @@ describe("workProductService", () => {
|
||||
expect(transaction).toHaveBeenCalledTimes(1);
|
||||
expect(txUpdate).toHaveBeenCalledTimes(1);
|
||||
expect(txInsert).toHaveBeenCalledTimes(1);
|
||||
expect(ownerWhere).toHaveBeenCalledTimes(1);
|
||||
expect(result?.id).toBe("work-product-1");
|
||||
});
|
||||
|
||||
it("keeps the new work product when the telemetry owner read fails", async () => {
|
||||
const insertedRow = createWorkProductRow();
|
||||
const txInsert = vi.fn(() => ({ values: vi.fn(() => ({ returning: vi.fn(async () => [insertedRow]) })) }));
|
||||
let transactionOpen = false;
|
||||
const tx = {
|
||||
execute: retentionReads(() => transactionOpen),
|
||||
update: vi.fn(() => ({ set: vi.fn(() => ({ where: vi.fn(async () => undefined) })) })),
|
||||
insert: txInsert,
|
||||
transaction: vi.fn(async () => { throw new Error("owner read failed"); }),
|
||||
};
|
||||
const transaction = vi.fn(async (callback: (input: typeof tx) => Promise<unknown>) => {
|
||||
transactionOpen = true;
|
||||
try { return await callback(tx); } finally { transactionOpen = false; }
|
||||
});
|
||||
|
||||
const result = await workProductService({ transaction } as any).createForIssue("issue-1", "company-1", {
|
||||
type: "pull_request", provider: "github", title: "PR 1", status: "open", reviewState: "draft", isPrimary: false,
|
||||
});
|
||||
|
||||
expect(tx.transaction).toHaveBeenCalledTimes(1);
|
||||
expect(result?.id).toBe("work-product-1");
|
||||
});
|
||||
|
||||
|
||||
@@ -209,6 +209,7 @@ import {
|
||||
import { trackAgentTaskCompleted } from "@paperclipai/shared/telemetry";
|
||||
import { getTelemetryClient } from "../telemetry.js";
|
||||
import { resolveTelemetryPerson, withTelemetryActor } from "../telemetry-company-scope.js";
|
||||
import { emitArtifactCreatedAfterCommit } from "../services/product-telemetry.js";
|
||||
import { isUniqueViolation } from "../db-errors.js";
|
||||
import type { StorageService } from "../storage/types.js";
|
||||
import { validate, validateIssueMutationBody } from "../middleware/validate.js";
|
||||
@@ -12797,6 +12798,8 @@ export function issueRoutes(
|
||||
});
|
||||
return;
|
||||
}
|
||||
emitArtifactCreatedAfterCommit({ companyId: product.companyId, type: product.type, provider: product.provider, creationFlow: "promotion",
|
||||
task: issue, runOwnerUserId: actor.actorType === "user" ? actor.actorId : null });
|
||||
|
||||
await logActivity(db, {
|
||||
companyId: issue.companyId,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { and, eq, sql } from "drizzle-orm";
|
||||
import { agents, heartbeatRuns, issues, type Db } from "@paperclipai/db";
|
||||
import { requiresExecutionReconciliation } from "@paperclipai/shared";
|
||||
import { PERMANENT_FAILURE_CODES, RESTART_CLASS_FAILURE_CODES, SETUP_FAILURE_CODES } from "./continuation-cause.js";
|
||||
import { trackAgentTaskRun } from "@paperclipai/shared/telemetry";
|
||||
import { parseObject } from "../adapters/utils.js";
|
||||
import { logger } from "../middleware/logger.js";
|
||||
@@ -8,11 +10,149 @@ import { resolveTelemetryPerson, withTelemetryActor } from "../telemetry-company
|
||||
|
||||
type HeartbeatRun = typeof heartbeatRuns.$inferSelect;
|
||||
type TelemetryRun = Pick<HeartbeatRun,
|
||||
"id" | "companyId" | "agentId" | "status" | "startedAt" | "finishedAt" | "usageJson" | "contextSnapshot">
|
||||
"id" | "companyId" | "agentId" | "status" | "startedAt" | "finishedAt" | "usageJson" | "contextSnapshot"
|
||||
| "errorCode" | "resultJson">
|
||||
& Partial<Pick<HeartbeatRun, "responsibleUserId">>;
|
||||
|
||||
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
export type AgentRunFailureCause =
|
||||
| "missing_access"
|
||||
| "provider_limit"
|
||||
| "provider_error"
|
||||
| "waiting_for_person"
|
||||
| "task_changed"
|
||||
| "platform_error"
|
||||
| "agent_error"
|
||||
| "unknown";
|
||||
|
||||
const FAILURE_STATES = new Set(["failed", "timed_out", "interrupted"]);
|
||||
|
||||
const PLATFORM_FAILURE_CODES = new Set<string>([
|
||||
...RESTART_CLASS_FAILURE_CODES,
|
||||
...SETUP_FAILURE_CODES,
|
||||
...PERMANENT_FAILURE_CODES,
|
||||
"workspace_validation_failed",
|
||||
"workspace_restore_failed",
|
||||
"remote_workspace_restore_failed",
|
||||
"workspace_busy",
|
||||
"runner_revision_mismatch",
|
||||
"storage_unavailable",
|
||||
"native_execution_ownership_unverified",
|
||||
"lease_released_before_terminal",
|
||||
"plugin_worker_unavailable",
|
||||
"native_workspace_sync_out_retry_exhausted",
|
||||
"queued_run_claim_rejected",
|
||||
]);
|
||||
|
||||
const FAILURE_CAUSE_BY_ERROR_CODE: Record<string, AgentRunFailureCause> = {
|
||||
configuration_incomplete: "missing_access",
|
||||
slack_configuration_incomplete: "missing_access",
|
||||
signing_secret_unconfigured: "missing_access",
|
||||
user_secret_missing: "missing_access",
|
||||
user_secret_definition_missing: "missing_access",
|
||||
user_secret_definition_inactive: "missing_access",
|
||||
github_access_denied: "missing_access",
|
||||
github_repo_not_accessible: "missing_access",
|
||||
github_auth_required: "missing_access",
|
||||
project_access_denied: "missing_access",
|
||||
permission_denied: "missing_access",
|
||||
responsible_user_unauthorized: "missing_access",
|
||||
oauth_access_projection_disabled: "missing_access",
|
||||
acpx_auth_required: "missing_access",
|
||||
codex_auth_required: "missing_access",
|
||||
claude_auth_required: "missing_access",
|
||||
provider_auth_failed: "missing_access",
|
||||
hermes_gateway_auth_failed: "missing_access",
|
||||
connection_unavailable: "missing_access",
|
||||
provider_quota: "provider_limit",
|
||||
provider_limit_wait: "provider_limit",
|
||||
native_provider_usage_limit: "provider_limit",
|
||||
native_provider_overloaded: "provider_limit",
|
||||
github_rate_limited: "provider_limit",
|
||||
ai_connection_busy: "provider_limit",
|
||||
native_provider_model_rejected: "provider_error",
|
||||
provider_tool_definition_invalid: "provider_error",
|
||||
provider_transport_failed: "provider_error",
|
||||
codex_transient_upstream: "provider_error",
|
||||
provider_failed: "provider_error",
|
||||
issue_waiting_for_response: "waiting_for_person",
|
||||
issue_continuation_waiting_on_review: "waiting_for_person",
|
||||
approval_required: "waiting_for_person",
|
||||
formal_approval_unavailable: "waiting_for_person",
|
||||
budget_blocked: "waiting_for_person",
|
||||
budget_hard_stop: "waiting_for_person",
|
||||
budget_exceeded: "waiting_for_person",
|
||||
cancelled: "task_changed",
|
||||
operator_cancelled: "task_changed",
|
||||
operator_interrupted: "task_changed",
|
||||
interrupted: "task_changed",
|
||||
issue_cancelled: "task_changed",
|
||||
issue_paused: "task_changed",
|
||||
agent_paused: "task_changed",
|
||||
issue_reassigned: "task_changed",
|
||||
issue_assignee_changed: "task_changed",
|
||||
issue_terminal_status: "task_changed",
|
||||
issue_not_in_progress: "task_changed",
|
||||
issue_execution_lock_changed: "task_changed",
|
||||
issue_dependencies_blocked: "task_changed",
|
||||
issue_blocked: "task_changed",
|
||||
issue_not_found: "task_changed",
|
||||
issue_review_participant_changed: "task_changed",
|
||||
queued_comment_discarded: "task_changed",
|
||||
invalid_output: "agent_error",
|
||||
invalid_response: "agent_error",
|
||||
tool_error: "agent_error",
|
||||
acpx_turn_failed: "agent_error",
|
||||
test_run_failed: "agent_error",
|
||||
prompt_injection_blocked: "agent_error",
|
||||
tool_execution_outcome_unknown: "agent_error",
|
||||
turn_limit_exhausted: "agent_error",
|
||||
timeout: "agent_error",
|
||||
adapter_failed: "agent_error",
|
||||
};
|
||||
|
||||
const FAILURE_CAUSE_BY_PROVIDER_CATEGORY: Record<string, AgentRunFailureCause> = {
|
||||
authentication: "missing_access",
|
||||
permission: "missing_access",
|
||||
rate_limit: "provider_limit",
|
||||
quota_exhausted: "provider_limit",
|
||||
overloaded: "provider_limit",
|
||||
model_unavailable: "provider_error",
|
||||
invalid_request: "provider_error",
|
||||
timeout: "provider_error",
|
||||
unknown: "provider_error",
|
||||
};
|
||||
|
||||
const PROVIDER_CATEGORY_CODES = new Set(["adapter_failed", "provider_failed"]);
|
||||
|
||||
export function agentRunFailureCause(input: {
|
||||
errorCode: string | null | undefined;
|
||||
providerFailureCategory: string | null | undefined;
|
||||
}): AgentRunFailureCause {
|
||||
const code = input.errorCode ?? null;
|
||||
if (code && (code === "execution_reconciliation_required" || requiresExecutionReconciliation(code))) {
|
||||
return "waiting_for_person";
|
||||
}
|
||||
const providerCause = input.providerFailureCategory
|
||||
? FAILURE_CAUSE_BY_PROVIDER_CATEGORY[input.providerFailureCategory]
|
||||
: undefined;
|
||||
if (providerCause && (!code || PROVIDER_CATEGORY_CODES.has(code))) return providerCause;
|
||||
if (!code) return "unknown";
|
||||
if (FAILURE_CAUSE_BY_ERROR_CODE[code]) return FAILURE_CAUSE_BY_ERROR_CODE[code];
|
||||
return PLATFORM_FAILURE_CODES.has(code) ? "platform_error" : "unknown";
|
||||
}
|
||||
|
||||
function providerFailureCategory(resultJson: unknown): string | null {
|
||||
const providerFailure = parseObject(parseObject(resultJson).providerFailure);
|
||||
return typeof providerFailure.category === "string" ? providerFailure.category : null;
|
||||
}
|
||||
|
||||
function resolveFailureCause(run: TelemetryRun): AgentRunFailureCause | undefined {
|
||||
if (!FAILURE_STATES.has(run.status)) return undefined;
|
||||
return agentRunFailureCause({ errorCode: run.errorCode, providerFailureCategory: providerFailureCategory(run.resultJson) });
|
||||
}
|
||||
|
||||
function readNonEmptyString(value: unknown): string | null {
|
||||
return typeof value === "string" && value.trim().length > 0
|
||||
? value.trim()
|
||||
@@ -97,6 +237,7 @@ export async function emitAgentTaskRun(db: Db, run: TelemetryRun): Promise<void>
|
||||
taskAssignedByUserId: agent?.taskResponsibleUserId,
|
||||
runOwnerUserId: run.responsibleUserId,
|
||||
});
|
||||
const failureCause = resolveFailureCause(run);
|
||||
|
||||
withTelemetryActor({ companyId: run.companyId, person }, () => trackAgentTaskRun(client, {
|
||||
agentId: run.agentId,
|
||||
@@ -135,6 +276,7 @@ export async function emitAgentTaskRun(db: Db, run: TelemetryRun): Promise<void>
|
||||
}
|
||||
: {}),
|
||||
...(taskId ? { taskId } : {}),
|
||||
...(failureCause ? { failureCause } : {}),
|
||||
}));
|
||||
} catch (err) {
|
||||
logger.warn(
|
||||
@@ -161,6 +303,8 @@ export async function emitAgentTaskRunById(
|
||||
usageJson: heartbeatRuns.usageJson,
|
||||
responsibleUserId: heartbeatRuns.responsibleUserId,
|
||||
contextSnapshot: sql<Record<string, unknown>>`jsonb_build_object('issueId', ${heartbeatRuns.contextSnapshot}->'issueId')`,
|
||||
errorCode: heartbeatRuns.errorCode,
|
||||
resultJson: sql<Record<string, unknown> | null>`jsonb_build_object('providerFailure', jsonb_build_object('category', ${heartbeatRuns.resultJson}->'providerFailure'->'category'))`,
|
||||
})
|
||||
.from(heartbeatRuns)
|
||||
.where(and(eq(heartbeatRuns.id, input.runId), eq(heartbeatRuns.companyId, input.companyId)))
|
||||
|
||||
@@ -5,7 +5,7 @@ import { withAccountHomeSecretMutationLock } from "@paperclipai/adapter-codex-lo
|
||||
import { syncConnectionCredentialBindings } from "./connection-credential-bindings.js";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { and, eq, inArray, or, sql } from "drizzle-orm";
|
||||
import { and, eq, inArray, ne, or, sql } from "drizzle-orm";
|
||||
import {
|
||||
type Db,
|
||||
authUsers,
|
||||
@@ -49,6 +49,7 @@ import { logActivity } from "./activity-log.js";
|
||||
import { secretService } from "./secrets.js";
|
||||
import { probeAiConnectionUsage } from "./ai-connection-usage.js";
|
||||
import { probeAiAccountIdentity } from "./ai-account-identity.js";
|
||||
import { emitModelConnectedAfterCommit } from "./product-telemetry.js";
|
||||
|
||||
/** Same human audience displayed by the existing Connections identity controls. */
|
||||
function canUseCredential(
|
||||
@@ -959,6 +960,22 @@ export function aiConnectionService(db: Db) {
|
||||
entityId: id,
|
||||
details: { provider: input.provider, method: input.method, grantId },
|
||||
});
|
||||
if (!reconnect) {
|
||||
const [earlierModel] = await tx
|
||||
.select({ id: toolConnections.id })
|
||||
.from(toolConnections)
|
||||
.where(and(eq(toolConnections.companyId, companyId), eq(toolConnections.connectionPurpose, "ai"), ne(toolConnections.id, id)))
|
||||
.limit(1);
|
||||
emitModelConnectedAfterCommit({
|
||||
companyId,
|
||||
provider: input.provider,
|
||||
method: input.method,
|
||||
ownership: input.ownership,
|
||||
routingKind: routing?.kind,
|
||||
firstForCompany: !earlierModel,
|
||||
ownerUserId: userId,
|
||||
});
|
||||
}
|
||||
return { connectionId: id, grantId };
|
||||
}));
|
||||
// Quota refresh can hold this lock through a 60-second token exchange.
|
||||
|
||||
@@ -10,6 +10,7 @@ import { conflict, notFound, payloadTooLarge, unprocessable } from "../errors.js
|
||||
import { isUniqueViolation } from "../db-errors.js";
|
||||
import { insertRowsInChunks } from "./batch-insert.js";
|
||||
import type { ImportIssueDocumentRow } from "./import-write-types.js";
|
||||
import { emitPlanCreatedAfterCommit } from "./product-telemetry.js";
|
||||
|
||||
// Stay below download_artifact's 10 MiB limit, including JSON escaping and metadata.
|
||||
export const ISSUE_DOCUMENT_BATCH_MAX_BYTES = 8 * 1024 * 1024;
|
||||
@@ -263,7 +264,8 @@ export function documentService(db: Db) {
|
||||
}, options: { initialTaskCreation?: boolean } = {}) => {
|
||||
const key = normalizeDocumentKey(input.key);
|
||||
const issue = await db
|
||||
.select({ id: issues.id, companyId: issues.companyId })
|
||||
.select({ id: issues.id, companyId: issues.companyId, createdByUserId: issues.createdByUserId,
|
||||
responsibleUserId: issues.responsibleUserId })
|
||||
.from(issues)
|
||||
.where(eq(issues.id, input.issueId))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
@@ -539,6 +541,15 @@ export function documentService(db: Db) {
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
if (key === "plan") {
|
||||
emitPlanCreatedAfterCommit({
|
||||
companyId: issue.companyId,
|
||||
createdByAgentId: input.createdByAgentId,
|
||||
createdByUserId: input.createdByUserId,
|
||||
atTaskCreation: options.initialTaskCreation === true,
|
||||
task: issue,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
created: true as const,
|
||||
|
||||
@@ -51,6 +51,7 @@ import { parseHeartbeatPolicy } from "./heartbeat/timer-policy.js";
|
||||
|
||||
import { RECOVERY_CONTINUATIONS_PER_COMPANY_PER_PASS, RECOVERY_CONTINUATION_BACKLOG_SHARE_PER_PASS, RECOVERY_CONTINUATION_MAX_HOLD_MS,
|
||||
recoveryContinuationHeldSince } from "./recovery-continuation-pacing.js";
|
||||
import { emitSystemTaskBlockedAfterCommit } from "./product-telemetry.js";
|
||||
|
||||
export { RECOVERY_CONTINUATIONS_PER_COMPANY_PER_PASS, RECOVERY_CONTINUATION_DELIVERY_BOUND_MS,
|
||||
RECOVERY_CONTINUATION_MAX_HOLD_MS } from "./recovery-continuation-pacing.js";
|
||||
@@ -910,6 +911,7 @@ export async function settleUnrecoverableExecutions(
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(issues.id, task.id)).returning();
|
||||
await emitSystemTaskBlockedAfterCommit(tx, projected!, task.status);
|
||||
// Only a transition owned by this failure grants a recovery receipt.
|
||||
// An already-blocked task may have a separate human/dependency hold.
|
||||
if (task.status !== "blocked" && run.runtimeMode === "native") {
|
||||
|
||||
@@ -220,6 +220,7 @@ import type { instanceSettingsService } from "../instance-settings.js";
|
||||
import type { createRunDispatch } from "../../modules/run-dispatch/index.js";
|
||||
import { hasRetainedExternalChatRetry, type PostCommitEffect as WakeQueuePostCommitEffect, type createWakeQueue } from "../../modules/wake-queue/index.js";
|
||||
import { crashPoint } from "@paperclipai/shared/crash-points";
|
||||
import { emitSystemTaskBlockedAfterCommit } from "../product-telemetry.js";
|
||||
|
||||
type HeartbeatRun = typeof heartbeatRuns.$inferSelect;
|
||||
type QueuedRunCandidate = Pick<HeartbeatRun, "id" | "companyId" | "agentId" | "status" | "invocationSource" | "createdAt"
|
||||
@@ -4663,17 +4664,21 @@ export function createHeartbeatQueue(db: Db, dependencies: HeartbeatQueueDepende
|
||||
const blocked = await issuesSvc.updateForCompany(issue.id, issue.companyId,
|
||||
{ status: "blocked", executionState: null }, tx, issueActivityPublications, issuePostCommitActions);
|
||||
if (!blocked) throw conflict("The task changed before its workspace settings could be checked");
|
||||
} else await tx
|
||||
.update(issues)
|
||||
.set({
|
||||
status: "blocked",
|
||||
checkoutRunId: null,
|
||||
executionRunId: null,
|
||||
executionAgentNameKey: null,
|
||||
executionLockedAt: null,
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(issues.id, issue.id));
|
||||
} else {
|
||||
const [blocked] = await tx
|
||||
.update(issues)
|
||||
.set({
|
||||
status: "blocked",
|
||||
checkoutRunId: null,
|
||||
executionRunId: null,
|
||||
executionAgentNameKey: null,
|
||||
executionLockedAt: null,
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(issues.id, issue.id))
|
||||
.returning();
|
||||
if (blocked) await emitSystemTaskBlockedAfterCommit(tx, blocked, issue.status);
|
||||
}
|
||||
await insertIssueComment(tx, {
|
||||
companyId: issue.companyId,
|
||||
issueId: issue.id,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { and, eq, inArray, sql } from "drizzle-orm";
|
||||
import { and, eq, inArray, ne, sql } from "drizzle-orm";
|
||||
import { createHash } from "node:crypto";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { agentWakeupRequests } from "@paperclipai/db";
|
||||
import { agentWakeupRequests, issueRelations, issues } from "@paperclipai/db";
|
||||
import { logger } from "../middleware/logger.js";
|
||||
|
||||
export const ISSUE_BLOCKERS_RESOLVED_WAKE_REASON = "issue_blockers_resolved";
|
||||
|
||||
@@ -283,3 +284,24 @@ export async function findExistingIssueBlockersResolvedWakeForReadyState(
|
||||
);
|
||||
return covering ?? null;
|
||||
}
|
||||
|
||||
type DbTransaction = Parameters<Parameters<Db["transaction"]>[0]>[0];
|
||||
|
||||
export async function hasUnfinishedBlockers(dbOrTx: Db | DbTransaction, task: { companyId: string; id: string }): Promise<boolean> {
|
||||
try {
|
||||
return await dbOrTx.transaction(async (read) => (await read
|
||||
.select({ blockerId: issueRelations.issueId })
|
||||
.from(issueRelations)
|
||||
.innerJoin(issues, eq(issueRelations.issueId, issues.id))
|
||||
.where(and(
|
||||
eq(issueRelations.companyId, task.companyId),
|
||||
eq(issueRelations.relatedIssueId, task.id),
|
||||
eq(issueRelations.type, "blocks"),
|
||||
ne(issues.status, "done"),
|
||||
))
|
||||
.limit(1)).length > 0);
|
||||
} catch (err) {
|
||||
logger.warn({ err, issueId: task.id }, "failed to read task blockers");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -148,6 +148,7 @@ import { logger } from "../middleware/logger.js";
|
||||
import { conflict, forbidden, notFound, unprocessable } from "../errors.js";
|
||||
import { getTelemetryClient } from "../telemetry.js";
|
||||
import { withTelemetryCompany } from "../telemetry-company-scope.js";
|
||||
import { emitPlanApprovedAfterCommit } from "./product-telemetry.js";
|
||||
import { authorizationService } from "./authorization.js";
|
||||
import { isCloudManagedInstance } from "./cloud-instance.js";
|
||||
import { resolveDeploymentMode } from "../config-file.js";
|
||||
@@ -559,6 +560,7 @@ type IssueResolutionContext = {
|
||||
reviewPolicy: IssueReviewPolicy | null;
|
||||
createdByAgentId: string | null;
|
||||
createdByUserId: string | null;
|
||||
responsibleUserId?: string | null;
|
||||
executionPolicy?: unknown;
|
||||
executionState?: unknown;
|
||||
};
|
||||
@@ -2730,6 +2732,7 @@ export function issueThreadInteractionService(
|
||||
reviewPolicy: issues.reviewPolicy,
|
||||
createdByAgentId: issues.createdByAgentId,
|
||||
createdByUserId: issues.createdByUserId,
|
||||
responsibleUserId: issues.responsibleUserId,
|
||||
executionPolicy: issues.executionPolicy,
|
||||
executionState: issues.executionState,
|
||||
})
|
||||
@@ -2839,6 +2842,15 @@ export function issueThreadInteractionService(
|
||||
acceptedPlanTarget?.issueId === issueContext.id &&
|
||||
acceptedPlanTarget.key === "plan" &&
|
||||
issueContext.workMode === "planning";
|
||||
if (lockedCurrent.kind === "request_confirmation" && acceptedPlanTarget?.issueId === issueContext.id
|
||||
&& acceptedPlanTarget.key === "plan") {
|
||||
emitPlanApprovedAfterCommit({
|
||||
companyId: issueContext.companyId,
|
||||
actor: args.actor,
|
||||
startsExecution: acceptedPlanStartsExecution,
|
||||
task: { createdByUserId: issueContext.createdByUserId ?? null, responsibleUserId: issueContext.responsibleUserId ?? null },
|
||||
});
|
||||
}
|
||||
if (isNativeCompletionReview(lockedCurrent)) {
|
||||
const otherPending = await tx.select({ id: issueThreadInteractions.id })
|
||||
.from(issueThreadInteractions).where(and(
|
||||
|
||||
@@ -53,6 +53,8 @@ import { assertHandedOffRunTaskOwnership } from "../run-task-ownership.js";
|
||||
import { retryIdempotentDatabaseOperation } from "../database-retry.js";
|
||||
import { externalConversationStateSql, nonIdleSlackIssueCondition, resumeSlackConversation } from "./slack-conversation-state.js";
|
||||
import { documentService } from "./documents.js";
|
||||
import { emitArtifactCreatedAfterCommit, emitTaskCreatedAfterCommit, emitTaskStatusTransitionAfterCommit } from "./product-telemetry.js";
|
||||
import { hasUnfinishedBlockers } from "./issue-dependency-wakeups.js";
|
||||
import { parseTaskSearch, taskSearchCtes, taskSearchScore } from "./task-search.js";
|
||||
import { createdFromIssueCondition } from "./issue-creation-origin.js";
|
||||
import { loadRunBoundIssue } from "./issue-creation-source.js";
|
||||
@@ -11220,6 +11222,7 @@ export function issueService(db: Db) {
|
||||
await notifyIssueAttentionRetentionWork(tx, { companyId, issueId: values.id, parentId: values.parentId, originKind: values.originKind, originId: values.originId });
|
||||
if (values.originKind === "routine_execution") await notifyRoutineExecutionStatus(tx, values.id, companyId);
|
||||
const [issue] = await tx.insert(issues).values(values).returning();
|
||||
emitTaskCreatedAfterCommit(issue);
|
||||
await attachOwnedDraftImages(tx, issue, issue.description, { userId: issueData.createdByUserId, agentId: issueData.createdByAgentId });
|
||||
if (issue.visibility === "private" && !inheritedPrivateSubtree && issueData.createdByAgentId) {
|
||||
await tx.insert(issueAccessGrants).values({
|
||||
@@ -12519,6 +12522,21 @@ export function issueService(db: Db) {
|
||||
tx,
|
||||
);
|
||||
}
|
||||
const enteredBlocked = updated.status === "blocked" && receiptExisting.status !== "blocked";
|
||||
emitTaskStatusTransitionAfterCommit({
|
||||
companyId: updated.companyId,
|
||||
previousStatus: receiptExisting.status,
|
||||
nextStatus: updated.status,
|
||||
parentId: updated.parentId,
|
||||
conversationAgentId: updated.conversationAgentId,
|
||||
actorAgentId,
|
||||
actorUserId,
|
||||
unblockDescriptor: updated.unblockDescriptor,
|
||||
taskCreatedByUserId: updated.createdByUserId,
|
||||
taskResponsibleUserId: updated.responsibleUserId,
|
||||
runOwnerUserId: actorUserId,
|
||||
hasOpenBlockers: enteredBlocked && await hasUnfinishedBlockers(tx, updated),
|
||||
});
|
||||
if (
|
||||
issueData.executionWorkspaceSettings !== undefined &&
|
||||
nextExecutionWorkspaceId &&
|
||||
@@ -14360,7 +14378,7 @@ export function issueService(db: Db) {
|
||||
createdByRunId?: string | null;
|
||||
}) => {
|
||||
const issue = await db
|
||||
.select({ id: issues.id, companyId: issues.companyId })
|
||||
.select({ id: issues.id, companyId: issues.companyId, createdByUserId: issues.createdByUserId, responsibleUserId: issues.responsibleUserId })
|
||||
.from(issues)
|
||||
.where(eq(issues.id, input.issueId))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
@@ -14534,6 +14552,10 @@ export function issueService(db: Db) {
|
||||
})
|
||||
.returning({ id: issueWorkProducts.id })
|
||||
: [];
|
||||
if (artifactWorkProduct) {
|
||||
emitArtifactCreatedAfterCommit({ companyId: issue.companyId, type: "artifact", provider: "paperclip", creationFlow: "run_deliverable",
|
||||
task: issue, runOwnerUserId: input.createdByUserId });
|
||||
}
|
||||
|
||||
if (
|
||||
input.createdByAgentId &&
|
||||
|
||||
@@ -33,6 +33,24 @@ export const UNKNOWN_ENUM_FALLBACKS = {
|
||||
"agent_role": {
|
||||
"fallback": "other",
|
||||
"required": false
|
||||
},
|
||||
"failure_cause": {
|
||||
"fallback": "unknown",
|
||||
"required": false
|
||||
}
|
||||
},
|
||||
"artifact.created": {
|
||||
"artifact_type": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
},
|
||||
"provider": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
},
|
||||
"creation_flow": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
}
|
||||
},
|
||||
"company.imported": {
|
||||
@@ -93,6 +111,36 @@ export const UNKNOWN_ENUM_FALLBACKS = {
|
||||
"required": false
|
||||
}
|
||||
},
|
||||
"model.connected": {
|
||||
"provider": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
},
|
||||
"connection_method": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
},
|
||||
"ownership": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
},
|
||||
"routing_kind": {
|
||||
"fallback": "other",
|
||||
"required": false
|
||||
}
|
||||
},
|
||||
"plan.approved": {
|
||||
"approved_by_kind": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
}
|
||||
},
|
||||
"plan.created": {
|
||||
"created_by_kind": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
}
|
||||
},
|
||||
"routine.run": {
|
||||
"source": {
|
||||
"fallback": "other",
|
||||
@@ -114,5 +162,27 @@ export const UNKNOWN_ENUM_FALLBACKS = {
|
||||
"fallback": "unknown",
|
||||
"required": true
|
||||
}
|
||||
},
|
||||
"task.blocked": {
|
||||
"blocked_reason": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
},
|
||||
"blocked_by_kind": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
}
|
||||
},
|
||||
"task.completed": {
|
||||
"completed_by_kind": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
}
|
||||
},
|
||||
"task.created": {
|
||||
"created_by_kind": {
|
||||
"fallback": "other",
|
||||
"required": true
|
||||
}
|
||||
}
|
||||
} as const;
|
||||
@@ -0,0 +1,224 @@
|
||||
import { afterTransactionCommit } from "@paperclipai/db";
|
||||
import { hasUnfinishedBlockers } from "./issue-dependency-wakeups.js";
|
||||
import {
|
||||
ONBOARDING_FIRST_TASK_ORIGIN_KIND,
|
||||
type IssueUnblockDescriptor,
|
||||
} from "@paperclipai/shared";
|
||||
import {
|
||||
trackArtifactCreated,
|
||||
trackModelConnected,
|
||||
trackPlanApproved,
|
||||
trackPlanCreated,
|
||||
trackTaskBlocked,
|
||||
trackTaskCompleted,
|
||||
trackTaskCreated,
|
||||
type TelemetryClient,
|
||||
} from "@paperclipai/shared/telemetry";
|
||||
import { logger } from "../middleware/logger.js";
|
||||
import { getTelemetryClient } from "../telemetry.js";
|
||||
import { resolveTelemetryPerson, withTelemetryActor } from "../telemetry-company-scope.js";
|
||||
|
||||
export type TelemetryActorKind = "user" | "agent" | "system";
|
||||
|
||||
export function telemetryActorKind(actor: { agentId?: string | null; userId?: string | null }): TelemetryActorKind {
|
||||
if (actor.agentId) return "agent";
|
||||
if (actor.userId) return "user";
|
||||
return "system";
|
||||
}
|
||||
|
||||
export type TelemetryPersonSources = Parameters<typeof resolveTelemetryPerson>[0];
|
||||
|
||||
function emitAfterCommit(
|
||||
companyId: string,
|
||||
personSources: TelemetryPersonSources,
|
||||
event: string,
|
||||
emit: (client: TelemetryClient) => void,
|
||||
): void {
|
||||
const person = resolveTelemetryPerson(personSources);
|
||||
afterTransactionCommit(() => {
|
||||
try {
|
||||
const client = getTelemetryClient();
|
||||
if (client) withTelemetryActor({ companyId, person }, () => emit(client));
|
||||
} catch (err) {
|
||||
logger.warn({ err, event }, "failed to emit product telemetry");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function emitTaskCreatedAfterCommit(issue: {
|
||||
companyId: string;
|
||||
originKind: string | null;
|
||||
parentId: string | null;
|
||||
conversationAgentId: string | null;
|
||||
createdByAgentId: string | null;
|
||||
createdByUserId: string | null;
|
||||
responsibleUserId: string | null;
|
||||
}): void {
|
||||
if (issue.conversationAgentId) return;
|
||||
const createdByKind = issue.originKind === "routine_execution"
|
||||
? "routine"
|
||||
: issue.originKind === ONBOARDING_FIRST_TASK_ORIGIN_KIND
|
||||
? "onboarding"
|
||||
: telemetryActorKind({ agentId: issue.createdByAgentId, userId: issue.createdByUserId });
|
||||
const personSources = { taskCreatedByUserId: issue.createdByUserId, taskAssignedByUserId: issue.responsibleUserId };
|
||||
emitAfterCommit(issue.companyId, personSources, "task.created", (client) =>
|
||||
trackTaskCreated(client, { createdByKind, isSubtask: Boolean(issue.parentId) }));
|
||||
}
|
||||
|
||||
export type TaskBlockedReason = "waiting_on_task" | "waiting_for_person" | "waiting_for_agent" | "needs_recovery" | "unspecified";
|
||||
|
||||
export function taskBlockedReason(input: {
|
||||
hasOpenBlockers: boolean;
|
||||
unblockDescriptor: IssueUnblockDescriptor | null | undefined;
|
||||
actorKind: TelemetryActorKind;
|
||||
}): TaskBlockedReason {
|
||||
if (input.hasOpenBlockers) return "waiting_on_task";
|
||||
const owner = input.unblockDescriptor?.owner;
|
||||
if (owner === "board" || (owner && typeof owner === "object" && "userId" in owner)) return "waiting_for_person";
|
||||
if (owner && typeof owner === "object" && "agentId" in owner) return "waiting_for_agent";
|
||||
return input.actorKind === "system" ? "needs_recovery" : "unspecified";
|
||||
}
|
||||
|
||||
export function emitTaskStatusTransitionAfterCommit(input: {
|
||||
companyId: string;
|
||||
previousStatus: string;
|
||||
nextStatus: string;
|
||||
parentId: string | null;
|
||||
conversationAgentId: string | null;
|
||||
actorAgentId: string | null | undefined;
|
||||
actorUserId: string | null | undefined;
|
||||
hasOpenBlockers: boolean;
|
||||
unblockDescriptor: IssueUnblockDescriptor | null | undefined;
|
||||
taskCreatedByUserId: string | null;
|
||||
taskResponsibleUserId: string | null;
|
||||
runOwnerUserId: string | null | undefined;
|
||||
}): void {
|
||||
if (input.previousStatus === input.nextStatus || input.conversationAgentId) return;
|
||||
const actorKind = telemetryActorKind({ agentId: input.actorAgentId, userId: input.actorUserId });
|
||||
const personSources = {
|
||||
taskCreatedByUserId: input.taskCreatedByUserId,
|
||||
taskAssignedByUserId: input.taskResponsibleUserId,
|
||||
runOwnerUserId: input.runOwnerUserId,
|
||||
};
|
||||
if (input.nextStatus === "done") {
|
||||
emitAfterCommit(input.companyId, personSources, "task.completed", (client) =>
|
||||
trackTaskCompleted(client, { completedByKind: actorKind, isSubtask: Boolean(input.parentId) }));
|
||||
} else if (input.nextStatus === "blocked") {
|
||||
const blockedReason = taskBlockedReason({
|
||||
hasOpenBlockers: input.hasOpenBlockers,
|
||||
unblockDescriptor: input.unblockDescriptor,
|
||||
actorKind,
|
||||
});
|
||||
emitAfterCommit(input.companyId, personSources, "task.blocked", (client) =>
|
||||
trackTaskBlocked(client, { blockedReason, blockedByKind: actorKind }));
|
||||
}
|
||||
}
|
||||
|
||||
export async function emitSystemTaskBlockedAfterCommit(
|
||||
tx: Parameters<typeof hasUnfinishedBlockers>[0],
|
||||
task: {
|
||||
id: string;
|
||||
companyId: string;
|
||||
parentId: string | null;
|
||||
conversationAgentId: string | null;
|
||||
unblockDescriptor: IssueUnblockDescriptor | null;
|
||||
createdByUserId: string | null;
|
||||
responsibleUserId: string | null;
|
||||
},
|
||||
previousStatus: string,
|
||||
): Promise<void> {
|
||||
if (previousStatus === "blocked") return;
|
||||
emitTaskStatusTransitionAfterCommit({
|
||||
companyId: task.companyId,
|
||||
previousStatus,
|
||||
nextStatus: "blocked",
|
||||
parentId: task.parentId,
|
||||
conversationAgentId: task.conversationAgentId,
|
||||
actorAgentId: null,
|
||||
actorUserId: null,
|
||||
hasOpenBlockers: await hasUnfinishedBlockers(tx, task),
|
||||
unblockDescriptor: task.unblockDescriptor,
|
||||
taskCreatedByUserId: task.createdByUserId,
|
||||
taskResponsibleUserId: task.responsibleUserId,
|
||||
runOwnerUserId: null,
|
||||
});
|
||||
}
|
||||
|
||||
export function emitPlanCreatedAfterCommit(input: {
|
||||
companyId: string;
|
||||
createdByAgentId: string | null | undefined;
|
||||
createdByUserId: string | null | undefined;
|
||||
atTaskCreation: boolean;
|
||||
task: { createdByUserId: string | null; responsibleUserId: string | null };
|
||||
}): void {
|
||||
const createdByKind = telemetryActorKind({ agentId: input.createdByAgentId, userId: input.createdByUserId });
|
||||
const personSources = {
|
||||
taskCreatedByUserId: input.task.createdByUserId,
|
||||
taskAssignedByUserId: input.task.responsibleUserId,
|
||||
runOwnerUserId: input.createdByUserId,
|
||||
};
|
||||
emitAfterCommit(input.companyId, personSources, "plan.created", (client) =>
|
||||
trackPlanCreated(client, { createdByKind, atTaskCreation: input.atTaskCreation }));
|
||||
}
|
||||
|
||||
export function emitPlanApprovedAfterCommit(input: {
|
||||
companyId: string;
|
||||
actor: { agentId?: string | null; userId?: string | null };
|
||||
startsExecution: boolean;
|
||||
task: { createdByUserId: string | null; responsibleUserId: string | null };
|
||||
}): void {
|
||||
const approvedByKind = telemetryActorKind(input.actor);
|
||||
const personSources = {
|
||||
taskCreatedByUserId: input.task.createdByUserId,
|
||||
taskAssignedByUserId: input.task.responsibleUserId,
|
||||
runOwnerUserId: input.actor.userId,
|
||||
};
|
||||
emitAfterCommit(input.companyId, personSources, "plan.approved", (client) =>
|
||||
trackPlanApproved(client, { approvedByKind, startsExecution: input.startsExecution }));
|
||||
}
|
||||
|
||||
const CATALOG_ARTIFACT_PROVIDERS = new Set(["paperclip", "github", "vercel", "s3"]);
|
||||
|
||||
export function artifactTelemetryProvider(provider: string): "paperclip" | "github" | "vercel" | "s3" | "custom" {
|
||||
return CATALOG_ARTIFACT_PROVIDERS.has(provider) ? provider as "paperclip" | "github" | "vercel" | "s3" : "custom";
|
||||
}
|
||||
|
||||
export function emitArtifactCreatedAfterCommit(input: {
|
||||
companyId: string;
|
||||
type: string;
|
||||
provider: string;
|
||||
creationFlow: "direct" | "promotion" | "run_deliverable";
|
||||
task: { createdByUserId: string | null; responsibleUserId: string | null } | null;
|
||||
runOwnerUserId: string | null | undefined;
|
||||
}): void {
|
||||
const personSources = {
|
||||
taskCreatedByUserId: input.task?.createdByUserId,
|
||||
taskAssignedByUserId: input.task?.responsibleUserId,
|
||||
runOwnerUserId: input.runOwnerUserId,
|
||||
};
|
||||
emitAfterCommit(input.companyId, personSources, "artifact.created", (client) =>
|
||||
trackArtifactCreated(client, {
|
||||
artifactType: input.type,
|
||||
provider: artifactTelemetryProvider(input.provider),
|
||||
creationFlow: input.creationFlow,
|
||||
}));
|
||||
}
|
||||
|
||||
export function emitModelConnectedAfterCommit(input: {
|
||||
companyId: string;
|
||||
provider: string;
|
||||
method: string;
|
||||
ownership: string;
|
||||
routingKind: string | null | undefined;
|
||||
firstForCompany: boolean;
|
||||
ownerUserId: string;
|
||||
}): void {
|
||||
emitAfterCommit(input.companyId, { runOwnerUserId: input.ownerUserId }, "model.connected", (client) =>
|
||||
trackModelConnected(client, {
|
||||
provider: input.provider,
|
||||
connectionMethod: input.method,
|
||||
ownership: input.ownership,
|
||||
routingKind: input.routingKind ?? null,
|
||||
firstForCompany: input.firstForCompany,
|
||||
}));
|
||||
}
|
||||
@@ -19,6 +19,8 @@ import {
|
||||
type GitHubCommitDiffDetailsResolver,
|
||||
} from "./github-commit-details.js";
|
||||
import type { ImportIssueWorkProductRow } from "./import-write-types.js";
|
||||
import { emitArtifactCreatedAfterCommit } from "./product-telemetry.js";
|
||||
import { logger } from "../middleware/logger.js";
|
||||
|
||||
type IssueWorkProductRow = typeof issueWorkProducts.$inferSelect;
|
||||
type WorkProductTransaction = Parameters<Parameters<Db["transaction"]>[0]>[0];
|
||||
@@ -304,7 +306,7 @@ export function workProductService(
|
||||
),
|
||||
);
|
||||
}
|
||||
return await tx
|
||||
const created = await tx
|
||||
.insert(issueWorkProducts)
|
||||
.values({
|
||||
...data,
|
||||
@@ -313,6 +315,18 @@ export function workProductService(
|
||||
})
|
||||
.returning()
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (created) {
|
||||
const task = await tx.transaction(async (read) => (await read
|
||||
.select({ createdByUserId: issues.createdByUserId, responsibleUserId: issues.responsibleUserId })
|
||||
.from(issues).where(and(eq(issues.id, issueId), eq(issues.companyId, companyId))))[0] ?? null)
|
||||
.catch((err: unknown) => {
|
||||
logger.warn({ err, issueId }, "failed to read task owner for artifact telemetry");
|
||||
return null;
|
||||
});
|
||||
emitArtifactCreatedAfterCommit({ companyId, type: created.type, provider: created.provider, creationFlow: "direct",
|
||||
task, runOwnerUserId: null });
|
||||
}
|
||||
return created;
|
||||
});
|
||||
return row ? toIssueWorkProduct(row) : null;
|
||||
},
|
||||
|
||||
@@ -370,18 +370,19 @@
|
||||
"packages/shared/src/standing-checks.ts": "7ab71e4b6984e41e1c1dfd1bddf0116041d5abf1",
|
||||
"packages/shared/src/subscriptions.ts": "ef70e63395ca967bbc30776b71560e96eb354a75",
|
||||
"packages/shared/src/summary-slot.test.ts": "5f8e222dea425f0d5bff0adb5c683087bd7cf7fb",
|
||||
"packages/shared/src/telemetry/README.md": "99c1d1eddf1e5133bed005fcdef6dbfe12d83460",
|
||||
"packages/shared/src/telemetry/README.md": "36b4a57d187218ec141256d7f093c96c793ff901",
|
||||
"packages/shared/src/telemetry/client-types.test.ts": "3b630edda4972b929ba4decd7457f226eb9cfc5b",
|
||||
"packages/shared/src/telemetry/client.test.ts": "5a95f2305a393c5b2e7750b10e0ed65da0f30cf8",
|
||||
"packages/shared/src/telemetry/client.ts": "d6534ce8aaa6f69fb2dd03e12487db024e250a96",
|
||||
"packages/shared/src/telemetry/config.ts": "dfcd4d692cc06e71cb77f70b44fc05721eb330d1",
|
||||
"packages/shared/src/telemetry/connector-events.test.ts": "c060017c7a6a26e67aea56e5c9e36059be527070",
|
||||
"packages/shared/src/telemetry/events.ts": "5a8bd387ad993db1b9dcbd0919200edb58eb68bd",
|
||||
"packages/shared/src/telemetry/generated/paperclip-telemetry.ts": "b44d37d34283fefa4fba8e9ac4d87682ba694926",
|
||||
"packages/shared/src/telemetry/index.ts": "39ba76a46c323a30e40c0590dcbc31edebefbe9a",
|
||||
"packages/shared/src/telemetry/events.ts": "b7c7fa27da4bec5e207785bfa6793d9ceffaf4db",
|
||||
"packages/shared/src/telemetry/generated/paperclip-telemetry.ts": "3d0f4e7c22a12937a07ca617e4e9093de6fb90e9",
|
||||
"packages/shared/src/telemetry/index.ts": "69e507a3d178c2daaa609f75b79f4cadfdc720af",
|
||||
"packages/shared/src/telemetry/product-events.test.ts": "d3182c7e18fbe9c2915497fc1e2048fb2a250a04",
|
||||
"packages/shared/src/telemetry/readme-contract.test.ts": "eec9d4511ddc3085a9af1a2dab6bf8487e49dfd7",
|
||||
"packages/shared/src/telemetry/retention.test.ts": "9d54db1371bf20f22b8d2f8373f84433107a7408",
|
||||
"packages/shared/src/telemetry/retention.ts": "023004d4e49f52cfb6fa8c7d32e674640704ebb4",
|
||||
"packages/shared/src/telemetry/retention.ts": "d82be134d62e0afbbde84b9a9851180a4b0bb694",
|
||||
"packages/shared/src/telemetry/server-started-event.test.ts": "3d0e0e2289badb41c92ba0d69e3668d00b3b5fdb",
|
||||
"packages/shared/src/telemetry/state.ts": "a060af26a8e9a82a8afb0e2fccbfe6a59e9d0a8a",
|
||||
"packages/shared/src/telemetry/types.ts": "083d4341a72ac47bc1a3bbff163965de8b06f90a",
|
||||
|
||||
Reference in new issue
Block a user