lastlight-core 0.24.2 → 0.24.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/config/default.yaml +19 -0
  2. package/deploy/slack/README.md +15 -0
  3. package/deploy/slack/slack-manifest.json +4 -1
  4. package/dist/admin/routes.js +40 -0
  5. package/dist/admin/routes.js.map +1 -1
  6. package/dist/config/config.d.ts +42 -0
  7. package/dist/config/config.js +36 -0
  8. package/dist/config/config.js.map +1 -1
  9. package/dist/connectors/messaging/base.d.ts +15 -0
  10. package/dist/connectors/messaging/base.js +29 -1
  11. package/dist/connectors/messaging/base.js.map +1 -1
  12. package/dist/connectors/messaging/types.d.ts +25 -0
  13. package/dist/connectors/slack/connector.d.ts +27 -0
  14. package/dist/connectors/slack/connector.js +52 -0
  15. package/dist/connectors/slack/connector.js.map +1 -1
  16. package/dist/cron/feedback-poll.d.ts +72 -0
  17. package/dist/cron/feedback-poll.js +161 -0
  18. package/dist/cron/feedback-poll.js.map +1 -0
  19. package/dist/engine/feedback/ingest.d.ts +61 -0
  20. package/dist/engine/feedback/ingest.js +150 -0
  21. package/dist/engine/feedback/ingest.js.map +1 -0
  22. package/dist/engine/feedback/reactions.d.ts +58 -0
  23. package/dist/engine/feedback/reactions.js +153 -0
  24. package/dist/engine/feedback/reactions.js.map +1 -0
  25. package/dist/engine/feedback/slack.d.ts +61 -0
  26. package/dist/engine/feedback/slack.js +73 -0
  27. package/dist/engine/feedback/slack.js.map +1 -0
  28. package/dist/engine/github/github.d.ts +51 -0
  29. package/dist/engine/github/github.js +111 -0
  30. package/dist/engine/github/github.js.map +1 -1
  31. package/dist/engine/review-check.js +1 -1
  32. package/dist/index.js +95 -4
  33. package/dist/index.js.map +1 -1
  34. package/dist/notify/transports/slack.d.ts +9 -0
  35. package/dist/notify/transports/slack.js +7 -2
  36. package/dist/notify/transports/slack.js.map +1 -1
  37. package/dist/state/db.d.ts +5 -0
  38. package/dist/state/db.js +5 -0
  39. package/dist/state/db.js.map +1 -1
  40. package/dist/state/feedback-store.d.ts +181 -0
  41. package/dist/state/feedback-store.js +345 -0
  42. package/dist/state/feedback-store.js.map +1 -0
  43. package/dist/state/migrate.js +123 -0
  44. package/dist/state/migrate.js.map +1 -1
  45. package/dist/state/workflow-run-store.d.ts +37 -9
  46. package/dist/state/workflow-run-store.js +41 -12
  47. package/dist/state/workflow-run-store.js.map +1 -1
  48. package/dist/telemetry/feedback.d.ts +18 -0
  49. package/dist/telemetry/feedback.js +115 -0
  50. package/dist/telemetry/feedback.js.map +1 -0
  51. package/dist/telemetry/index.js +8 -0
  52. package/dist/telemetry/index.js.map +1 -1
  53. package/dist/telemetry/openinference.d.ts +7 -0
  54. package/dist/telemetry/openinference.js +7 -0
  55. package/dist/telemetry/openinference.js.map +1 -1
  56. package/dist/workflows/runner.js +38 -1
  57. package/dist/workflows/runner.js.map +1 -1
  58. package/package.json +3 -3
@@ -0,0 +1,72 @@
1
+ import type { WorkflowRun } from "../state/db.js";
2
+ import type { FeedbackCandidate, ReactionRead } from "../engine/github/github.js";
3
+ import { type FeedbackIngestDeps } from "../engine/feedback/ingest.js";
4
+ /**
5
+ * The GitHub half of feedback signals (issue #255).
6
+ *
7
+ * GitHub delivers **no webhook for reactions** — there is no `reaction` event,
8
+ * and no action on `issue_comment` covers one. So unlike Slack, a 👍 on a bot
9
+ * comment has to be discovered by asking. The design exists to make that asking
10
+ * cheap enough to be uncontroversial, in two halves:
11
+ *
12
+ * 1. **Discovery, once per run.** When a run finishes we list what it posted
13
+ * and record each comment as an anchor. This is the only per-run cost, it is
14
+ * scoped by `since` to the run's own window, and it is where attribution
15
+ * happens — while we still know which run wrote what.
16
+ *
17
+ * 2. **Refresh, on a cron.** The recurring half reads reactions for up to
18
+ * `maxAnchorsPerTick` anchors, 100 at a time, through ONE batched GraphQL
19
+ * query per 100 — measured at **one rate-limit point per request**, with the
20
+ * reactors included. A steady state of ~840 live anchors is therefore ~9
21
+ * points a tick against a 5,000/hour budget.
22
+ *
23
+ * What bounds it is the *data*, not the schedule: we poll individual bot
24
+ * comments, never issues; an anchor ages out of the rotation after
25
+ * `windowDays`; and the per-tick cap is a hard number of requests rather than a
26
+ * hope. That is the difference between this and "poll GitHub forever".
27
+ */
28
+ /** The narrow slice of {@link import("../engine/github/github.js").GitHubClient} this needs. */
29
+ export interface FeedbackGitHubClient {
30
+ listBotComments(owner: string, repo: string, issueNumber: number, opts: {
31
+ botLogin: string;
32
+ isPr?: boolean;
33
+ since?: string;
34
+ }): Promise<FeedbackCandidate[]>;
35
+ fetchReactions(owner: string, nodeIds: string[]): Promise<Map<string, ReactionRead[]>>;
36
+ }
37
+ export interface FeedbackPollDeps extends FeedbackIngestDeps {
38
+ github: FeedbackGitHubClient;
39
+ windowDays: number;
40
+ maxAnchorsPerTick: number;
41
+ retentionDays: number;
42
+ }
43
+ export interface PollResult {
44
+ anchorsPolled: number;
45
+ requests: number;
46
+ added: number;
47
+ retracted: number;
48
+ pruned: number;
49
+ }
50
+ /**
51
+ * Discover what a finished run posted, and register each piece as an anchor.
52
+ *
53
+ * Attribution happens HERE and nowhere else, because this is the only moment
54
+ * the association is free: we are holding the run. Later — at reaction time, on
55
+ * a cron tick, in the dashboard — the only way back would be to guess from
56
+ * timestamps.
57
+ *
58
+ * Best-effort and fire-and-forget: it hangs off the terminal-run observer,
59
+ * whose contract is that the transition is already persisted and no projection
60
+ * of it may fail the run.
61
+ */
62
+ export declare function discoverRunAnchors(deps: Pick<FeedbackPollDeps, "db" | "github" | "botLogin">, run: WorkflowRun): Promise<number>;
63
+ /**
64
+ * One poll tick: refresh reactions for the least-recently-polled anchors.
65
+ *
66
+ * Grouped by owner because each installation has its own token and its own
67
+ * rate-limit budget — batching across owners into one query is not possible,
68
+ * and would hide whose budget was being spent.
69
+ */
70
+ export declare function pollFeedbackReactions(deps: FeedbackPollDeps): Promise<PollResult>;
71
+ /** Convenience for the boot wiring — the terminal-run observer's body. */
72
+ export declare function feedbackAnchorObserver(deps: Pick<FeedbackPollDeps, "db" | "github" | "botLogin">): (run: WorkflowRun) => void;
@@ -0,0 +1,161 @@
1
+ import { REACTION_BATCH_SIZE } from "../engine/github/github.js";
2
+ import { canonicalReaction, isSelfReactor } from "../engine/feedback/reactions.js";
3
+ import { ingestReaction } from "../engine/feedback/ingest.js";
4
+ import { logger } from "../logging/logger.js";
5
+ const log = logger("feedback-poll");
6
+ /**
7
+ * Discover what a finished run posted, and register each piece as an anchor.
8
+ *
9
+ * Attribution happens HERE and nowhere else, because this is the only moment
10
+ * the association is free: we are holding the run. Later — at reaction time, on
11
+ * a cron tick, in the dashboard — the only way back would be to guess from
12
+ * timestamps.
13
+ *
14
+ * Best-effort and fire-and-forget: it hangs off the terminal-run observer,
15
+ * whose contract is that the transition is already persisted and no projection
16
+ * of it may fail the run.
17
+ */
18
+ export async function discoverRunAnchors(deps, run) {
19
+ const { owner, repo, issueNumber } = run;
20
+ if (!owner || !repo || !issueNumber || !deps.botLogin)
21
+ return 0;
22
+ try {
23
+ const candidates = await deps.github.listBotComments(owner, repo, issueNumber, {
24
+ botLogin: deps.botLogin,
25
+ // A `review_comment` can only exist on a PR, and asking for one on a
26
+ // plain issue is a guaranteed 404. `pr-review` is the workflow whose
27
+ // findings we most want scored, so getting this right matters.
28
+ isPr: isPullRequestRun(run),
29
+ // Only what THIS run wrote. Without it, a PR with a long comment history
30
+ // re-lists everything on every run, and every old comment is re-attributed
31
+ // to whichever run happened to finish last.
32
+ since: run.startedAt,
33
+ });
34
+ let created = 0;
35
+ for (const candidate of candidates) {
36
+ // A comment posted before the run started is not this run's output —
37
+ // `since` filters on `updated_at`, so an EDITED old comment still shows up.
38
+ if (candidate.createdAt < run.startedAt)
39
+ continue;
40
+ deps.db.feedback.upsertAnchor({
41
+ source: "github",
42
+ kind: candidate.kind,
43
+ externalId: candidate.externalId,
44
+ nodeId: candidate.nodeId,
45
+ channel: null,
46
+ owner,
47
+ repo,
48
+ issueNumber,
49
+ workflowRunId: run.id,
50
+ workflowName: run.workflowName,
51
+ messagingSessionId: null,
52
+ createdAt: candidate.createdAt,
53
+ });
54
+ created += 1;
55
+ }
56
+ if (created > 0) {
57
+ log.debug("Registered feedback anchors for a finished run", {
58
+ runId: run.id,
59
+ workflow: run.workflowName,
60
+ anchors: created,
61
+ });
62
+ }
63
+ return created;
64
+ }
65
+ catch (err) {
66
+ log.warn("Anchor discovery failed", { runId: run.id, repo: `${owner}/${repo}`, err });
67
+ return 0;
68
+ }
69
+ }
70
+ /**
71
+ * Is this run's subject a pull request? Read off the run's own context rather
72
+ * than asked of GitHub — `prNumber` is set by every PR-scoped dispatch, and an
73
+ * extra API call to re-learn what we already recorded would defeat the point.
74
+ */
75
+ function isPullRequestRun(run) {
76
+ const ctx = run.context;
77
+ return typeof ctx?.prNumber === "number";
78
+ }
79
+ /**
80
+ * One poll tick: refresh reactions for the least-recently-polled anchors.
81
+ *
82
+ * Grouped by owner because each installation has its own token and its own
83
+ * rate-limit budget — batching across owners into one query is not possible,
84
+ * and would hide whose budget was being spent.
85
+ */
86
+ export async function pollFeedbackReactions(deps) {
87
+ const result = { anchorsPolled: 0, requests: 0, added: 0, retracted: 0, pruned: 0 };
88
+ const anchors = deps.db.feedback.anchorsToPoll({
89
+ windowDays: deps.windowDays,
90
+ limit: deps.maxAnchorsPerTick,
91
+ });
92
+ const byOwner = new Map();
93
+ for (const anchor of anchors) {
94
+ if (!anchor.owner || !anchor.nodeId)
95
+ continue;
96
+ const list = byOwner.get(anchor.owner) ?? [];
97
+ list.push(anchor);
98
+ byOwner.set(anchor.owner, list);
99
+ }
100
+ for (const [owner, ownerAnchors] of byOwner) {
101
+ for (let i = 0; i < ownerAnchors.length; i += REACTION_BATCH_SIZE) {
102
+ const batch = ownerAnchors.slice(i, i + REACTION_BATCH_SIZE);
103
+ let reads;
104
+ try {
105
+ reads = await deps.github.fetchReactions(owner, batch.map((a) => a.nodeId));
106
+ result.requests += 1;
107
+ }
108
+ catch (err) {
109
+ // Leave `last_polled_at` untouched so this batch is first in line next
110
+ // tick rather than rotating to the back of the queue.
111
+ log.warn("Reaction batch failed", { owner, anchors: batch.length, err });
112
+ continue;
113
+ }
114
+ for (const anchor of batch) {
115
+ const reactions = reads.get(anchor.nodeId);
116
+ // A node GitHub didn't return is a deleted comment. Its anchor ages out
117
+ // on its own; retracting its signals would misreport "they changed
118
+ // their mind" when the comment simply went away.
119
+ if (!reactions)
120
+ continue;
121
+ const live = [];
122
+ for (const reaction of reactions) {
123
+ if (isSelfReactor(reaction.reactor, deps.botLogin))
124
+ continue;
125
+ const emoji = canonicalReaction(reaction.content, "github");
126
+ live.push({ reactor: reaction.reactor, emoji });
127
+ if (ingestReaction(deps, {
128
+ anchor,
129
+ source: "github",
130
+ emoji: reaction.content,
131
+ reactor: reaction.reactor,
132
+ })) {
133
+ result.added += 1;
134
+ }
135
+ }
136
+ // Absence from a fresh read is the ONLY evidence a reaction was
137
+ // removed — there is no event for it.
138
+ result.retracted += deps.db.feedback.reconcileAnchor(anchor.id, live);
139
+ }
140
+ deps.db.feedback.markPolled(batch.map((a) => a.id));
141
+ result.anchorsPolled += batch.length;
142
+ }
143
+ }
144
+ result.pruned = deps.db.feedback.pruneAnchors(deps.retentionDays);
145
+ if (result.added || result.retracted) {
146
+ log.info("Feedback poll", { ...result });
147
+ }
148
+ else {
149
+ log.debug("Feedback poll", { ...result });
150
+ }
151
+ return result;
152
+ }
153
+ /** Convenience for the boot wiring — the terminal-run observer's body. */
154
+ export function feedbackAnchorObserver(deps) {
155
+ return (run) => {
156
+ void discoverRunAnchors(deps, run).catch((err) => {
157
+ log.warn("Anchor discovery rejected", { runId: run.id, err });
158
+ });
159
+ };
160
+ }
161
+ //# sourceMappingURL=feedback-poll.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feedback-poll.js","sourceRoot":"","sources":["../../src/cron/feedback-poll.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAC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,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAElE,IAAI,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrC,GAAG,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;IAC3C,CAAC;SAAM,CAAC;QACN,GAAG,CAAC,KAAK,CAAC,eAAe,EAAE,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;IAC5C,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,sBAAsB,CACpC,IAA0D;IAE1D,OAAO,CAAC,GAAG,EAAE,EAAE;QACb,KAAK,kBAAkB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAY,EAAE,EAAE;YACxD,GAAG,CAAC,IAAI,CAAC,2BAA2B,EAAE,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;QAChE,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,61 @@
1
+ import type { StateDb } from "../../state/db.js";
2
+ import type { FeedbackAnchor, FeedbackSignal } from "../../state/feedback-store.js";
3
+ import { type FeedbackSource } from "./reactions.js";
4
+ /**
5
+ * The one way a reaction becomes a signal (issue #255).
6
+ *
7
+ * Both surfaces funnel through here — Slack's live `reaction_added` and the
8
+ * GitHub poller's batch reconciliation — so scoring, self-reaction filtering,
9
+ * persistence and telemetry happen once, in one order, with one set of rules.
10
+ * A second copy of this sequence is how the two surfaces would quietly start
11
+ * disagreeing about what a 👍 is worth.
12
+ *
13
+ * Everything here is best-effort by contract: a reaction is a nicety, and
14
+ * nothing about recording one may throw into a webhook handler or fail a cron
15
+ * tick.
16
+ */
17
+ export interface FeedbackIngestDeps {
18
+ db: StateDb;
19
+ /** `<botName>[bot]`. Reactions from the bot itself are never feedback. */
20
+ botLogin?: string;
21
+ /** `feedback.otel` — export each new signal as a span on the run's trace. */
22
+ otel?: boolean;
23
+ }
24
+ export interface ReactionInput {
25
+ anchor: FeedbackAnchor;
26
+ source: FeedbackSource;
27
+ /** Raw name as the platform spelled it (`+1::skin-tone-3`, `THUMBS_UP`, …). */
28
+ emoji: string;
29
+ reactor?: string;
30
+ reactedAt?: string;
31
+ }
32
+ /**
33
+ * Record a reaction. Returns the signal when it was NEW (so the caller can log
34
+ * it), and null when it was scored away, self-authored, or already known.
35
+ */
36
+ export declare function ingestReaction(deps: FeedbackIngestDeps, input: ReactionInput): FeedbackSignal | null;
37
+ /** Retract a reaction that has been taken away. Returns true when one was live. */
38
+ export declare function retractReaction(deps: FeedbackIngestDeps, input: ReactionInput): boolean;
39
+ /**
40
+ * Export one signal and stamp the watermark. The run row supplies the trace to
41
+ * attach to; a run whose trace we can't read (it ran with telemetry off) exports
42
+ * as its own root span rather than not at all.
43
+ *
44
+ * **The watermark is only stamped when a span was actually emitted.** With
45
+ * telemetry disabled `recordFeedbackSignal` is a no-op, and marking those rows
46
+ * exported would quietly discard them: turning OTel on later would find an
47
+ * empty backlog and the whole pre-OTel history would be unrepresented in the
48
+ * backend forever. Leaving them unmarked is what makes {@link drainFeedbackExport}
49
+ * able to catch up.
50
+ */
51
+ export declare function exportSignal(db: StateDb, signal: FeedbackSignal, anchor: FeedbackAnchor): void;
52
+ /**
53
+ * Export every signal recorded while telemetry was off. Called once at boot,
54
+ * after the SDK starts.
55
+ *
56
+ * This is the payoff for the watermark being honest: an operator who enables
57
+ * OTel today gets the feedback they already collected, on the traces it belongs
58
+ * to, instead of a backend that starts from zero. Bounded per boot so a long
59
+ * backlog drains over a few restarts rather than stalling startup.
60
+ */
61
+ export declare function drainFeedbackExport(db: StateDb, limit?: number): number;
@@ -0,0 +1,150 @@
1
+ import { recordFeedbackSignal } from "../../telemetry/feedback.js";
2
+ import { isTelemetryEnabled } from "../../telemetry/index.js";
3
+ import { logger } from "../../logging/logger.js";
4
+ import { canonicalReaction, isSelfReactor, scoreReaction } from "./reactions.js";
5
+ const log = logger("feedback");
6
+ /**
7
+ * Record a reaction. Returns the signal when it was NEW (so the caller can log
8
+ * it), and null when it was scored away, self-authored, or already known.
9
+ */
10
+ export function ingestReaction(deps, input) {
11
+ try {
12
+ if (isSelfReactor(input.reactor, deps.botLogin))
13
+ return null;
14
+ const scored = scoreReaction(input.emoji, input.source);
15
+ if (!scored) {
16
+ log.debug("Ignoring a reaction outside the feedback vocabulary", {
17
+ emoji: input.emoji,
18
+ source: input.source,
19
+ });
20
+ return null;
21
+ }
22
+ const signal = deps.db.feedback.recordSignal({
23
+ anchor: input.anchor,
24
+ emoji: scored.emoji,
25
+ score: scored.score,
26
+ sentiment: scored.sentiment,
27
+ reactor: input.reactor,
28
+ reactedAt: input.reactedAt,
29
+ });
30
+ // Already live — a Slack redelivery or the poller re-reading the same
31
+ // reaction. Nothing happened, so nothing is exported.
32
+ if (!signal)
33
+ return null;
34
+ log.info("Feedback signal", {
35
+ source: signal.source,
36
+ emoji: signal.emoji,
37
+ score: signal.score,
38
+ workflow: signal.workflowName ?? undefined,
39
+ workflowRunId: signal.workflowRunId ?? undefined,
40
+ repo: signal.repo ?? undefined,
41
+ });
42
+ if (deps.otel !== false)
43
+ exportSignal(deps.db, signal, input.anchor);
44
+ return signal;
45
+ }
46
+ catch (err) {
47
+ log.warn("Failed to ingest a reaction", { source: input.source, err });
48
+ return null;
49
+ }
50
+ }
51
+ /** Retract a reaction that has been taken away. Returns true when one was live. */
52
+ export function retractReaction(deps, input) {
53
+ try {
54
+ const emoji = canonicalReaction(input.emoji, input.source);
55
+ const removed = deps.db.feedback.removeSignal(input.anchor.id, input.reactor ?? null, emoji, input.reactedAt);
56
+ if (removed) {
57
+ log.info("Feedback signal retracted", {
58
+ source: input.source,
59
+ emoji,
60
+ workflowRunId: input.anchor.workflowRunId ?? undefined,
61
+ });
62
+ }
63
+ return removed;
64
+ }
65
+ catch (err) {
66
+ log.warn("Failed to retract a reaction", { source: input.source, err });
67
+ return false;
68
+ }
69
+ }
70
+ /**
71
+ * Export one signal and stamp the watermark. The run row supplies the trace to
72
+ * attach to; a run whose trace we can't read (it ran with telemetry off) exports
73
+ * as its own root span rather than not at all.
74
+ *
75
+ * **The watermark is only stamped when a span was actually emitted.** With
76
+ * telemetry disabled `recordFeedbackSignal` is a no-op, and marking those rows
77
+ * exported would quietly discard them: turning OTel on later would find an
78
+ * empty backlog and the whole pre-OTel history would be unrepresented in the
79
+ * backend forever. Leaving them unmarked is what makes {@link drainFeedbackExport}
80
+ * able to catch up.
81
+ */
82
+ export function exportSignal(db, signal, anchor) {
83
+ if (!isTelemetryEnabled())
84
+ return;
85
+ let parent;
86
+ if (signal.workflowRunId) {
87
+ const run = db.runs.getRun(signal.workflowRunId);
88
+ if (run?.traceId && run.spanId)
89
+ parent = { traceId: run.traceId, spanId: run.spanId };
90
+ }
91
+ recordFeedbackSignal({ signal, anchor, anchorUrl: anchorUrl(anchor), parent });
92
+ db.feedback.markExported([signal.id]);
93
+ }
94
+ /**
95
+ * Export every signal recorded while telemetry was off. Called once at boot,
96
+ * after the SDK starts.
97
+ *
98
+ * This is the payoff for the watermark being honest: an operator who enables
99
+ * OTel today gets the feedback they already collected, on the traces it belongs
100
+ * to, instead of a backend that starts from zero. Bounded per boot so a long
101
+ * backlog drains over a few restarts rather than stalling startup.
102
+ */
103
+ export function drainFeedbackExport(db, limit = 500) {
104
+ if (!isTelemetryEnabled())
105
+ return 0;
106
+ let exported = 0;
107
+ try {
108
+ for (const signal of db.feedback.pendingExport(limit)) {
109
+ const anchor = db.feedback.getAnchor(signal.anchorId);
110
+ // The anchor was pruned past `retentionDays` while the signal outlived it
111
+ // (signals are kept forever). Mark it done rather than re-reading it on
112
+ // every boot for a span we can no longer describe.
113
+ if (!anchor) {
114
+ db.feedback.markExported([signal.id]);
115
+ continue;
116
+ }
117
+ exportSignal(db, signal, anchor);
118
+ exported += 1;
119
+ }
120
+ if (exported > 0)
121
+ log.info("Exported backlogged feedback signals", { exported });
122
+ }
123
+ catch (err) {
124
+ log.warn("Feedback export backlog drain failed", { err });
125
+ }
126
+ return exported;
127
+ }
128
+ /**
129
+ * A deep link to the thing that was reacted to, when the platform gives us one
130
+ * that resolves without a workspace-specific base. GitHub comment permalinks
131
+ * are derivable from the ids we already hold; a Slack permalink needs the
132
+ * team's domain and an API round-trip, so those go unlinked rather than wrong.
133
+ */
134
+ function anchorUrl(anchor) {
135
+ if (anchor.source !== "github" || !anchor.owner || !anchor.repo || !anchor.issueNumber) {
136
+ return undefined;
137
+ }
138
+ const base = `https://github.com/${anchor.owner}/${anchor.repo}`;
139
+ switch (anchor.kind) {
140
+ case "issue_comment":
141
+ return `${base}/issues/${anchor.issueNumber}#issuecomment-${anchor.externalId}`;
142
+ case "review_comment":
143
+ return `${base}/pull/${anchor.issueNumber}#discussion_r${anchor.externalId}`;
144
+ case "issue":
145
+ return `${base}/issues/${anchor.issueNumber}`;
146
+ default:
147
+ return undefined;
148
+ }
149
+ }
150
+ //# sourceMappingURL=ingest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ingest.js","sourceRoot":"","sources":["../../../src/engine/feedback/ingest.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,aAAa,EAAuB,MAAM,gBAAgB,CAAC;AAEtG,MAAM,GAAG,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AAiC/B;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,IAAwB,EAAE,KAAoB;IAC3E,IAAI,CAAC;QACH,IAAI,aAAa,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC;Y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AAsC,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;GAKG;AACH,SAAS,SAAS,CAAC,MAAsB;IACvC,IAAI,MAAM,CAAC,MAAM,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;QACvF,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,MAAM,IAAI,GAAG,sBAAsB,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;IACjE,QAAQ,MAAM,CAAC,IAAI,EAAE,CAAC;QACpB,KAAK,eAAe;YAClB,OAAO,GAAG,IAAI,WAAW,MAAM,CAAC,WAAW,iBAAiB,MAAM,CAAC,UAAU,EAAE,CAAC;QAClF,KAAK,gBAAgB;YACnB,OAAO,GAAG,IAAI,SAAS,MAAM,CAAC,WAAW,gBAAgB,MAAM,CAAC,UAAU,EAAE,CAAC;QAC/E,KAAK,OAAO;YACV,OAAO,GAAG,IAAI,WAAW,MAAM,CAAC,WAAW,EAAE,CAAC;QAChD;YACE,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * The emoji → score vocabulary (issue #255).
3
+ *
4
+ * A 👍 or 👎 on something Last Light wrote is a human grading one workflow run,
5
+ * and this module is the single place that decides what a given reaction is
6
+ * worth. It is pure — no IO, no config — so both surfaces normalize through the
7
+ * same table and a Slack `+1` and a GitHub `THUMBS_UP` aggregate as one thing.
8
+ *
9
+ * Three normalizations happen before the lookup, because the same reaction
10
+ * arrives spelled three different ways:
11
+ *
12
+ * - **Slack skin tones.** Slack sends `+1::skin-tone-3`; the tone is a rendering
13
+ * choice, not a different opinion.
14
+ * - **GitHub GraphQL vs REST.** The `reactionGroups` batch query answers
15
+ * `THUMBS_UP` / `CONFUSED`, while the REST endpoints say `+1` / `confused`.
16
+ * - **Slack aliases.** `thumbsup`, `tada` and friends are Slack's names for
17
+ * reactions GitHub calls `+1` and `hooray`. Folding them to the GitHub
18
+ * spelling is what lets one query average across both surfaces.
19
+ *
20
+ * Anything outside the table is **not** a signal — `scoreReaction` returns null
21
+ * and the caller drops it. A 🍕 is not feedback, and recording it would only
22
+ * dilute the dataset it would sit in.
23
+ */
24
+ /** Where a reaction came from. Slack has a wider vocabulary than GitHub's 8. */
25
+ export type FeedbackSource = "slack" | "github";
26
+ /** The five buckets a reaction can land in. */
27
+ export type FeedbackSentiment = "very_good" | "good" | "neutral" | "bad" | "very_bad";
28
+ export interface ScoredReaction {
29
+ /** Canonical reaction name, stored on the signal row. */
30
+ emoji: string;
31
+ score: -2 | -1 | 0 | 1 | 2;
32
+ sentiment: FeedbackSentiment;
33
+ }
34
+ /**
35
+ * Normalize a raw reaction name to its canonical form. Exported for the poller,
36
+ * which stores the canonical name on the anchor's snapshot and has to compare
37
+ * like with like across a REST discovery and a GraphQL refresh.
38
+ */
39
+ export declare function canonicalReaction(raw: string, source: FeedbackSource): string;
40
+ /**
41
+ * Score one reaction, or null when it carries no meaning we understand.
42
+ *
43
+ * A GitHub reaction outside the API's own 8 values is impossible, so it is
44
+ * treated as a decoding bug rather than a signal; a Slack reaction outside the
45
+ * table is just somebody being expressive.
46
+ */
47
+ export declare function scoreReaction(raw: string, source: FeedbackSource): ScoredReaction | null;
48
+ /**
49
+ * Is this reactor the bot itself? Last Light reacts 👀 on the comments it acts
50
+ * on, and a `<bot>[bot]` login reacting to a `<bot>[bot]` comment is the harness
51
+ * talking to itself — never feedback.
52
+ *
53
+ * Compared loosely on purpose: GitHub's REST API answers `last-light[bot]` while
54
+ * the GraphQL `author.login` on the same account answers `last-light`, so an
55
+ * exact match against either spelling alone would let half the self-reactions
56
+ * through.
57
+ */
58
+ export declare function isSelfReactor(reactor: string | undefined, botLogin: string | undefined): boolean;