Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
Kanban Internal Replenishment Level — implementation plan
Spec: .ai/specs/2026-10-06-kanban-internal-replenishment-level.md Research: .ai/research/kanban-internal-replenishment-level.md Run record: .ai/runs/2026-10-06-kanban-internal-replenishment-level.md Branch: naveenkash/kanban-circuit-replenishment-spec
Ground rules for the executor
- 🛑 Ask the user before each step that writes to the local database. These steps are marked (DB write — ask first). This includes
pnpm db:migrateand every live-database test. - 🛑 Ask the user before each commit. Commit only through
/check-and-commit. Never push. - Start every new source file with its SPDX header. Run
pnpm --filter @carbon/checks license-headersto write it. Do not type it by hand. - Wrap every new UI string in Lingui
tor<Trans>./check-and-commitruns/translate. - Never use JavaScript
Date. Never useMath.roundor.toFixedon a quantity. - If a fact in this plan turns out false in the code, STOP and report. Do not improvise.
- Do not change the demo datasets (
packages/database/src/datasets/). Their Transfer kanbans stay scan-only.
Corrections to the spec found while planning
The spec is updated with each of these. The plan follows the updated spec.
| # | Spec said | Code says | Plan does |
|---|---|---|---|
| 1 | The kanbans view selects k.*, so the new column appears |
Postgres expands k.* when it creates the view |
Recreate the view in the migration |
| 2 | Scalar get_kanban_projected_quantity(kanban_id, company_id) |
The kanbans list needs one query for the page | One set-returning function, get_kanban_projected_quantities(company_id, kanban_ids) |
| 3 | The sweep sends one event with no companyId |
An Inngest concurrency key on a missing field is unsafe | util.sweep_kanban_levels() sends one event per company, with kanbanIds |
| 4 | Inngest function in functions/inventory/ |
That directory does not exist | packages/jobs/src/inngest/functions/tasks/kanban-level-check.ts |
| 5 | Statement handler in event-system/functions/ |
The only table statement handler, apply_item_stock_quantities, is in handlers/ |
Both new trigger bodies go in event-system/handlers/ |
| 6 | The handler sends every armed kanban it touches | An event with no work still costs a function run | The handler and the interceptor send only kanbans below their level |
| 7 | A pgTAP test captures send_inngest_event |
The DB tests are plain psql scripts; none captures a net.http_post call |
Test the SQL selection (util.kanban_level_breaches()); prove the send path in the browser test |
| 8 | getKanbanLevelStatus |
One concept, one name | getKanbanProjectedQuantities |
| 9 | The header loader reads the kanban's names for the badge | getStockTransfer selects *, so kanbanId arrives with no new read |
The badge reads kanbanId only |
| 10 | A blank level on save clears the level | zod omits a blank optional field, so the UPDATE keeps the old value | upsertKanban writes null when the field is blank or the system is not Transfer |
Progress
- Task 1: Write the schema migration (verified, not committed)
- Task 2: Add the two trigger bodies and ship them with
authz migration(verified, not committed) - Task 3: Apply the migrations and regenerate types (DB write — ask first) (verified, not committed)
- Task 4: Add the SQL test script (verified, not committed)
- Task 5: Add the pure stock-transfer helpers in
@carbon/database(verified, not committed) - Task 6: Switch
insertStockTransferto the shared helper (verified, not committed) - Task 7: Declare the
carbon/kanban.level-checkevent (verified, not committed) - Task 8: Add the
kanban-replenishserver function (verified, not committed) - Task 9: Add the live-database test for
kanban-replenish(verified, not committed) - Task 10: Add the Inngest function
kanbanLevelCheckFunction(verified, not committed) - Task 11: Route the scan through
kanban-replenish(verified, not committed) - Task 12: Extend
kanbanValidatorandupsertKanban(verified, not committed) - Task 13: Add the glossary term (verified, not committed)
- Task 14: Add the Replenishment Level field to
KanbanForm(verified, not committed) - Task 15: Show the level and projected quantity on the kanbans list (verified, not committed)
- Task 16: Show the origin on stock transfers (verified, not committed)
- Task 17: Print the level on the kanban label (verified, not committed)
- Task 18: Update the docs, the kanban rule and the AGENTS files (verified, not committed)
- Task 19: Run the final gates and the browser test (gates green; browser test passed 2026-10-09, playbook .ai/playbooks/kanban-replenishment-level.md)
Dependencies
- Task 2 needs Task 1 (the trigger bodies call
get_kanban_projected_quantities). - Task 3 needs Tasks 1 and 2.
- Tasks 4, 5, 7, 12, 13 and 17 need Task 3 only. They are independent of each other.
- Task 6 needs Task 5.
- Task 8 needs Tasks 5 and 7.
- Tasks 9, 10 and 11 need Task 8. They are independent of each other.
- Task 14 needs Tasks 12 and 13.
- Tasks 15 and 16 need Task 3. They are independent of each other.
- Task 18 needs Tasks 8–17.
- Task 19 needs every other task.
Task 1: Write the schema migration
Depends on: none Files:
- Create:
packages/database/supabase/migrations/<timestamp>_kanban-replenishment-level.sql(the command makes the name) - Copy from (precedent):
packages/database/supabase/migrations/20260908143722_kanban-transfer.sql(view),packages/database/supabase/migrations/20261004183512_scheduled-jobs-from-database.sql:156-193(sweep and cron)
Steps:
- Run
pnpm db:migrate:new kanban-replenishment-level. - Run this query against the local database. It is read-only.
SELECT count(*) FROM "kanban" WHERE "replenishmentSystem" = 'Transfer' AND "fromStorageUnitId" = "storageUnitId"; - If the count is above 0, STOP and report. The new CHECK would fail on those rows.
- Write this SQL into the new file, exactly:
-- Kanban replenishment level: a Transfer kanban fires when the projected
-- quantity at its To storage unit is below this level.
-- Spec: .ai/specs/2026-10-06-kanban-internal-replenishment-level.md
-- 1. The level on the kanban
ALTER TABLE "kanban" ADD COLUMN IF NOT EXISTS "replenishmentLevel" NUMERIC;
ALTER TABLE "kanban" DROP CONSTRAINT IF EXISTS "kanban_replenishmentLevel_check";
ALTER TABLE "kanban"
ADD CONSTRAINT "kanban_replenishmentLevel_check"
CHECK ("replenishmentLevel" IS NULL OR "replenishmentLevel" >= 0);
COMMENT ON COLUMN "kanban"."replenishmentLevel" IS
'Transfer kanbans only. When the projected quantity at the To storage unit is below this level, Carbon creates a Released stock transfer for "quantity". NULL = scan-only.';
-- A Transfer kanban must not feed itself. The zod refine exists; this stops API writes.
ALTER TABLE "kanban" DROP CONSTRAINT IF EXISTS "kanban_transfer_distinct_storage_units_check";
ALTER TABLE "kanban"
ADD CONSTRAINT "kanban_transfer_distinct_storage_units_check"
CHECK (
"replenishmentSystem" <> 'Transfer'
OR "fromStorageUnitId" IS NULL
OR "storageUnitId" IS NULL
OR "fromStorageUnitId" <> "storageUnitId"
);
-- From a ledger row to its armed kanbans
CREATE INDEX IF NOT EXISTS "kanban_level_lookup_idx"
ON "kanban" ("companyId", "itemId", "storageUnitId")
WHERE "replenishmentSystem" = 'Transfer' AND "replenishmentLevel" IS NOT NULL;
-- 2. The kanbans view expands k.* at creation, so recreate it to expose the new column.
DROP VIEW IF EXISTS "kanbans";
CREATE VIEW "kanbans" WITH(SECURITY_INVOKER=true) AS
SELECT
k.*,
i.name,
i."readableIdWithRevision",
j."jobId" as "jobReadableId",
l.name as "locationName",
s.name as "storageUnitName",
fs.name as "fromStorageUnitName",
su.name as "supplierName",
CASE
WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"
ELSE i."thumbnailPath"
END AS "thumbnailPath"
FROM "kanban" k
JOIN "item" i ON k."itemId" = i."id"
LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"
JOIN "location" l ON k."locationId" = l."id"
LEFT JOIN "storageUnit" s ON k."storageUnitId" = s."id"
LEFT JOIN "storageUnit" fs ON k."fromStorageUnitId" = fs."id"
LEFT JOIN "supplier" su ON k."supplierId" = su."id"
LEFT JOIN "job" j ON k."jobId" = j."id";
-- 3. The origin on the stock transfer
ALTER TABLE "stockTransfer" ADD COLUMN IF NOT EXISTS "kanbanId" TEXT;
ALTER TABLE "stockTransfer" DROP CONSTRAINT IF EXISTS "stockTransfer_kanbanId_fkey";
ALTER TABLE "stockTransfer"
ADD CONSTRAINT "stockTransfer_kanbanId_fkey"
FOREIGN KEY ("kanbanId", "companyId") REFERENCES "kanban" ("id", "companyId")
ON DELETE SET NULL ("kanbanId");
CREATE INDEX IF NOT EXISTS "stockTransfer_kanbanId_idx"
ON "stockTransfer" ("kanbanId") WHERE "kanbanId" IS NOT NULL;
-- 4. Projected quantity for the armed kanbans of one company.
-- kanban_ids NULL = every armed kanban of the company.
-- On-hand uses item_ledger_on_hand_contribution, the itemStockQuantities definition.
CREATE OR REPLACE FUNCTION get_kanban_projected_quantities(
company_id TEXT,
kanban_ids TEXT[] DEFAULT NULL
)
RETURNS TABLE ("kanbanId" TEXT, "replenishmentLevel" NUMERIC, "projectedQuantity" NUMERIC)
LANGUAGE sql
STABLE
SECURITY INVOKER
SET search_path = public, pg_temp
AS $$
SELECT
k."id",
k."replenishmentLevel",
COALESCE(on_hand."quantity", 0) + COALESCE(inbound."quantity", 0)
FROM "kanban" k
LEFT JOIN LATERAL (
SELECT SUM(item_ledger_on_hand_contribution(il."quantity", il."trackedEntityStatus")) AS "quantity"
FROM "itemLedger" il
WHERE il."companyId" = k."companyId"
AND il."locationId" = k."locationId"
AND il."itemId" = k."itemId"
AND il."storageUnitId" = k."storageUnitId"
) on_hand ON true
LEFT JOIN LATERAL (
SELECT SUM(stl."outstandingQuantity") AS "quantity"
FROM "stockTransferLine" stl
JOIN "stockTransfer" st
ON st."id" = stl."stockTransferId" AND st."companyId" = stl."companyId"
WHERE stl."companyId" = k."companyId"
AND st."status" IN ('Released', 'In Progress')
AND stl."itemId" = k."itemId"
AND stl."toStorageUnitId" = k."storageUnitId"
) inbound ON true
WHERE k."companyId" = company_id
AND (kanban_ids IS NULL OR k."id" = ANY (kanban_ids))
AND k."replenishmentSystem" = 'Transfer'
AND k."replenishmentLevel" IS NOT NULL
AND k."fromStorageUnitId" IS NOT NULL
AND k."storageUnitId" IS NOT NULL;
$$;
-- 5. Backstop sweep: every armed kanban below its level, one row per company.
CREATE OR REPLACE FUNCTION util.kanban_level_breaches()
RETURNS TABLE ("companyId" TEXT, "kanbanIds" TEXT[])
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path = public
AS $$
SELECT c."companyId", array_agg(p."kanbanId" ORDER BY p."kanbanId")
FROM (
SELECT DISTINCT "companyId" FROM "kanban"
WHERE "replenishmentSystem" = 'Transfer' AND "replenishmentLevel" IS NOT NULL
) c
CROSS JOIN LATERAL get_kanban_projected_quantities(c."companyId") p
WHERE p."projectedQuantity" < p."replenishmentLevel"
GROUP BY c."companyId";
$$;
CREATE OR REPLACE FUNCTION util.sweep_kanban_levels()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
DECLARE
breach RECORD;
BEGIN
FOR breach IN SELECT * FROM util.kanban_level_breaches() LOOP
PERFORM util.send_inngest_event(
'carbon/kanban.level-check',
jsonb_build_object('companyId', breach."companyId", 'kanbanIds', to_jsonb(breach."kanbanIds"))
);
END LOOP;
END;
$$;
REVOKE ALL ON FUNCTION util.kanban_level_breaches() FROM PUBLIC;
REVOKE ALL ON FUNCTION util.sweep_kanban_levels() FROM PUBLIC;
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM cron.job WHERE jobname = 'kanban-level-sweeper') THEN
PERFORM cron.unschedule('kanban-level-sweeper');
END IF;
PERFORM cron.schedule('kanban-level-sweeper', '7 * * * *', 'SELECT util.sweep_kanban_levels();');
END;
$$;
- Do not add the
itemLedgerstatement handler or thekanbaninterceptor here. Task 2 ships them.
Verify:
ls packages/database/supabase/migrations | grep kanban-replenishment-level
# Expected: one file, <timestamp>_kanban-replenishment-level.sql, newer than every other migration
pnpm --filter @carbon/checks test
# Expected: no failure that names the new migration
If no-authz-ddl-in-migrations flags get_kanban_projected_quantities or a util.* function, STOP and report.
Out of scope: the kanbanReplenishmentSystem enum (no rename to Internal); kanban.quantity (stays INTEGER); RLS policies (no new table).
Task 2: Add the two trigger bodies and ship them with authz migration
Depends on: Task 1 Files:
- Create:
packages/database/src/event-system/handlers/queue_kanban_level_checks.sql - Create:
packages/database/src/event-system/handlers/sync_kanban_level_check.sql - Modify:
packages/database/src/event-system/attachments.ts— theitemLedgerentry and a newkanbanentry - Create:
packages/database/supabase/migrations/<timestamp>_kanban-level-check-triggers.sql(generated) - Copy from (precedent):
packages/database/src/event-system/handlers/apply_item_stock_quantities.sql(statement handler),packages/database/src/event-system/handlers/sync_job_complete_or_canceled.sql(interceptor that callsutil.send_inngest_event)
Steps:
- Create
queue_kanban_level_checks.sqlwith exactly this content:
CREATE OR REPLACE FUNCTION public.queue_kanban_level_checks()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path TO 'public', 'pg_temp'
AS $function$
DECLARE
touched JSONB;
target RECORD;
below TEXT[];
BEGIN
-- A seed, a restore or a test fixture loads ledger rows in bulk.
-- The hourly sweep (util.sweep_kanban_levels) covers those.
IF current_setting('app.sync_in_progress', true) = 'true' THEN
RETURN NULL;
END IF;
-- Only batched_new exists on INSERT and only batched_old on DELETE.
IF TG_OP = 'DELETE' THEN
SELECT jsonb_agg(DISTINCT jsonb_build_object(
'companyId', "companyId", 'itemId', "itemId", 'storageUnitId', "storageUnitId"))
INTO touched
FROM batched_old
WHERE "storageUnitId" IS NOT NULL;
ELSE
SELECT jsonb_agg(DISTINCT jsonb_build_object(
'companyId', "companyId", 'itemId', "itemId", 'storageUnitId', "storageUnitId"))
INTO touched
FROM batched_new
WHERE "storageUnitId" IS NOT NULL;
END IF;
IF touched IS NULL THEN
RETURN NULL;
END IF;
FOR target IN
SELECT k."companyId", array_agg(DISTINCT k."id") AS "kanbanIds"
FROM jsonb_to_recordset(touched) AS t("companyId" TEXT, "itemId" TEXT, "storageUnitId" TEXT)
JOIN "kanban" k
ON k."companyId" = t."companyId"
AND k."itemId" = t."itemId"
AND k."storageUnitId" = t."storageUnitId"
WHERE k."replenishmentSystem" = 'Transfer'
AND k."replenishmentLevel" IS NOT NULL
GROUP BY k."companyId"
LOOP
SELECT array_agg(p."kanbanId" ORDER BY p."kanbanId")
INTO below
FROM get_kanban_projected_quantities(target."companyId", target."kanbanIds") p
WHERE p."projectedQuantity" < p."replenishmentLevel";
IF below IS NOT NULL THEN
PERFORM util.send_inngest_event(
'carbon/kanban.level-check',
jsonb_build_object('companyId', target."companyId", 'kanbanIds', to_jsonb(below))
);
END IF;
END LOOP;
RETURN NULL;
END;
$function$;
- Create
sync_kanban_level_check.sqlwith exactly this content:
CREATE OR REPLACE FUNCTION public.sync_kanban_level_check(p_table text, p_operation text, p_new jsonb, p_old jsonb)
RETURNS void
LANGUAGE plpgsql
SET search_path TO 'public', 'extensions'
AS $function$
DECLARE
is_below BOOLEAN;
BEGIN
IF current_setting('app.sync_in_progress', true) = 'true' THEN
RETURN;
END IF;
IF p_operation NOT IN ('INSERT', 'UPDATE') THEN
RETURN;
END IF;
IF (p_new->>'replenishmentSystem') IS DISTINCT FROM 'Transfer'
OR (p_new->>'replenishmentLevel') IS NULL
THEN
RETURN;
END IF;
-- An UPDATE that changes nothing the level check reads does not re-check.
IF p_operation = 'UPDATE'
AND (p_new->'replenishmentLevel') IS NOT DISTINCT FROM (p_old->'replenishmentLevel')
AND (p_new->'replenishmentSystem') IS NOT DISTINCT FROM (p_old->'replenishmentSystem')
AND (p_new->'quantity') IS NOT DISTINCT FROM (p_old->'quantity')
AND (p_new->'itemId') IS NOT DISTINCT FROM (p_old->'itemId')
AND (p_new->'locationId') IS NOT DISTINCT FROM (p_old->'locationId')
AND (p_new->'storageUnitId') IS NOT DISTINCT FROM (p_old->'storageUnitId')
AND (p_new->'fromStorageUnitId') IS NOT DISTINCT FROM (p_old->'fromStorageUnitId')
THEN
RETURN;
END IF;
SELECT EXISTS (
SELECT 1
FROM get_kanban_projected_quantities(p_new->>'companyId', ARRAY[p_new->>'id']) p
WHERE p."projectedQuantity" < p."replenishmentLevel"
) INTO is_below;
IF is_below THEN
PERFORM util.send_inngest_event(
'carbon/kanban.level-check',
jsonb_build_object('companyId', p_new->>'companyId', 'kanbanIds', jsonb_build_array(p_new->>'id'))
);
END IF;
END;
$function$;
- In
attachments.ts, replace theitemLedgerentry (lines 112-114) with:itemLedger: { statement: [ "apply_item_stock_quantities", "broadcast_table_changes", "queue_kanban_level_checks" ] }, - Add a
kanbanentry betweenjobOperationStepRecordandjournal:kanban: { after: ["sync_kanban_level_check"] }, - Run
pnpm --filter @carbon/database authz migration kanban-level-check-triggers. - Open the generated file. Confirm it holds both
CREATE OR REPLACE FUNCTIONbodies. Confirm it holdsset_event_triggers('itemLedger', …)with all three statement functions. Confirm it holdsset_event_triggers('kanban', …). - Do not edit the generated file.
Verify:
pnpm --filter @carbon/database test
# Expected: pass; migration.test.ts reports no unshipped helper or attachment
If authz migration refuses because a new file would create an overload, STOP and report.
Out of scope: the stockTransfer and stockTransferLine attachments (no change); util.send_inngest_event (not managed).
Task 3: Apply the migrations and regenerate types (DB write — ask first)
Depends on: Tasks 1, 2 Files:
- Modify (generated):
packages/database/src/types.ts, swagger files written bypnpm db:migrate
Steps:
- 🛑 Ask the user for permission to run
pnpm db:migrateagainst the local database. - Run
pnpm db:migrate. It applies both migrations, runsauthz sync, and regenerates the types. - Run
pnpm run generate:types. - Never edit
packages/database/src/types.tsby hand.
Verify:
grep -n '"replenishmentLevel"' packages/database/src/types.ts | head -3
# Expected: at least one line (kanban Row and the kanbans view Row)
grep -n 'kanbanId: string | null' packages/database/src/types.ts | head -3
# Expected: at least one line (stockTransfer Row)
grep -n 'get_kanban_projected_quantities' packages/database/src/types.ts
# Expected: one Functions entry
pnpm --filter @carbon/database authz check
# Expected: 0 tables, 0 functions out of sync
Out of scope: pnpm db:check:backups (Task 19 runs it).
Task 4: Add the SQL test script
Depends on: Task 3 Files:
- Create:
packages/database/supabase/tests/kanban-replenishment-level.test.sql - Copy from (precedent):
packages/database/supabase/tests/scheduled-job-sweeps.test.sql(structure,pg_temp.expect,BEGIN … ROLLBACK)
Steps:
- Start the file with the header comment and run command of the precedent, with this file's name.
- Open with
\set ON_ERROR_STOP on,BEGIN;,SET LOCAL "app.sync_in_progress" = 'true';,SET LOCAL statement_timeout = '30s';. - Copy
pg_temp.expect(label text, actual boolean, expected boolean)from the precedent. - Borrow fixture rows in one
DO $proof$ … $proof$;block:- Select one pair of distinct
storageUnitrowsaandbwith the samecompanyIdandlocationId. - Select one
itemrow of that company. - If either select finds nothing,
RAISE EXCEPTION 'test needs a seeded company'.
- Select one pair of distinct
- Insert a
kanban:replenishmentSystem = 'Transfer', From =a, To =b,quantity = 5,replenishmentLevel = 10,createdBy = 'system'. - Insert an
itemLedgerrow:entryType = 'Positive Adjmt.',documentType = 'Inventory Receipt', atb, quantity 12. - Assert these, in order, with
pg_temp.expect:get_kanban_projected_quantities(companyId, ARRAY[kanbanId])returnsprojectedQuantity = 12.util.kanban_level_breaches()has no row naming the kanban.- Insert a ledger row of −3 at
b. The breach set now names the kanban (projected 9). - Insert a
stockTransfer(status = 'Released') with onestockTransferLinea → b, quantity 5. The breach set no longer names the kanban (projected 14). - Set the line's
pickedQuantity = 5. Insert ledger rows −5 ataand +5 atb. Projected stays 14. - Set the transfer
status = 'Completed'. Projected stays 14. - Insert a second kanban with
replenishmentLevel = NULL. The function returns no row for it. - In a nested
BEGIN … EXCEPTION WHEN check_violationblock, update the first kanban tofromStorageUnitId = storageUnitId. Expect the exception. - In a nested block, update the first kanban to
replenishmentLevel = -1. Expectcheck_violation.
- End the file with
ROLLBACK;.
Verify: (DB write — ask first; the script rolls back)
pnpm exec tsx scripts/run-local-accounting-check.ts psql -X -v ON_ERROR_STOP=1 -f packages/database/supabase/tests/kanban-replenishment-level.test.sql
# Expected: exit 0, ROLLBACK, no ERROR line
If an insert fails on a missing NOT NULL column, read the table's Insert type in packages/database/src/types.ts. Add the column.
Out of scope: capturing util.send_inngest_event calls (no precedent; the browser test covers the send).
Task 5: Add the pure stock-transfer helpers in @carbon/database
Depends on: Task 3 Files:
- Create:
packages/database/src/stock-transfer.ts - Create:
packages/database/src/stock-transfer.test.ts - Modify:
packages/database/package.json— add the export"./stock-transfer": "./src/stock-transfer.ts"next to"./sequence" - Copy from (precedent):
packages/database/src/supersession-pick.tsand its test (pure module shape, relative.tsimports,vitest)
Steps:
- Export this type and four values from
stock-transfer.ts:export type StockTransferLineDraft = { itemId: string; fromStorageUnitId?: string | null; toStorageUnitId?: string | null; quantity?: number; requiresSerialTracking?: boolean; requiresBatchTracking?: boolean; }; export function expandSerialTrackedLines<T extends StockTransferLineDraft>(lines: T[]): T[]; export function isBelowReplenishmentLevel(projectedQuantity: number, replenishmentLevel: number): boolean; export const KANBAN_REPLENISHMENT_NOTE_PREFIX = "Kanban replenishment"; export function kanbanReplenishmentNote(input: KanbanReplenishmentNoteInput): KanbanReplenishmentNote; - Copy the body of
expandSerialTrackedLinesfrom thereduceininsertStockTransfer(apps/erp/app/modules/inventory/inventory.service.ts, thelinesWithExpandedSerialTrackingblock). Keep its behavior exactly:- Drop a line whose
quantityis set and not an integer. - Split a serial-tracked line of quantity > 1 into that many lines of quantity 1.
- Keep every other line as it is.
- Drop a line whose
- Make
isBelowReplenishmentLevelreturnprojectedQuantity < replenishmentLevel. Strict, per spec Open Question 2. - Define the note input as a discriminated union on the signal:
export type KanbanReplenishmentNoteInput = { itemReadableId: string; fromStorageUnitName: string; toStorageUnitName: string; quantity: number; unitOfMeasureCode: string | null; signal: | { type: "scan"; userName: string } | { type: "level"; replenishmentLevel: number; projectedQuantity: number }; }; - Return the Tiptap document
{ type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text }] }] }. - Build
textin this form. Leave out the unit whenunitOfMeasureCodeis null.- Scan:
Kanban replenishment — MAT-KAPTON, A1-L3 → CleanRoom, 10 EA. Signal: scan by Jane Doe. - Level:
Kanban replenishment — MAT-KAPTON, A1-L3 → CleanRoom, 10 EA. Signal: level 4 (projected 3).
- Scan:
- Write
String(n)for each number. Do not round. - In the test file, cover these cases:
- Serial line of quantity 3 → 3 lines of quantity 1.
- Batch line of quantity 3 → 1 line of quantity 3.
- Untracked line of quantity 2.5 → dropped.
isBelowReplenishmentLevel(10, 10)→ false;(9, 10)→ true.- The scan note text and the level note text, each as one exact string.
- The note with
unitOfMeasureCode: nullhas no trailing unit.
Verify:
pnpm --filter @carbon/database test -- stock-transfer
# Expected: all stock-transfer tests pass
pnpm exec turbo run typecheck --filter=@carbon/database
# Expected: 0 errors
Out of scope: formatting numbers by locale (the note is English free text, like the scan route's strings).
Task 6: Switch insertStockTransfer to the shared helper
Depends on: Task 5 Files:
- Modify:
apps/erp/app/modules/inventory/inventory.service.ts—insertStockTransfer
Steps:
- Import
expandSerialTrackedLinesfrom@carbon/database/stock-transfer. - Replace the
linesWithExpandedSerialTrackingreducewithconst linesWithExpandedSerialTracking = expandSerialTrackedLines(lines);. - Change nothing else in the function.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: 0 errors
Out of scope: moving insertStockTransfer into a server function (the wizard keeps its RLS path).
Task 7: Declare the carbon/kanban.level-check event
Depends on: Task 3 Files:
- Modify:
packages/lib/src/events.ts— theEventstype - Copy from (precedent):
packages/lib/src/events.ts:563-570(events sent by Postgres)
Steps:
- Add this entry next to
"carbon/workflow-run-retention.process":// Sent by the database (queue_kanban_level_checks, sync_kanban_level_check, // util.sweep_kanban_levels), never by app code. Every id is a Transfer // kanban whose projected quantity was below its replenishment level. "carbon/kanban.level-check": { data: { companyId: string; kanbanIds: string[] }; };
Verify:
pnpm exec turbo run typecheck --filter=@carbon/lib
# Expected: 0 errors
Out of scope: packages/lib/src/trigger.ts (taskToEvent); no app code sends this event.
Task 8: Add the kanban-replenish server function
Depends on: Tasks 5, 7 Files:
- Create:
packages/server-functions/src/kanban-replenish/index.ts - Modify:
packages/server-functions/src/invoke.ts— add"kanban-replenish": () => import("./kanban-replenish"),in alphabetical order - Modify (generated):
packages/server-functions/src/__snapshots__/permissions-manifest.test.ts.snap - Copy from (precedent):
packages/server-functions/src/seed-company/index.ts(permissions: "system"),packages/server-functions/src/issue/index.ts:802-819(getNextSequenceinside a transaction),packages/server-functions/src/assign-serial-numbers/index.ts:85-99(.forUpdate())
Steps:
- Declare the input schema:
export const kanbanReplenishInput = z.discriminatedUnion("mode", [ z.object({ mode: z.literal("scan"), kanbanId: z.string().min(1) }), z.object({ mode: z.literal("level"), kanbanIds: z.array(z.string().min(1)).optional() }) ]); - Declare the reasons. The first three strings are the scan route's current error strings; keep them exact.
export const KANBAN_REPLENISH_REASONS = { missingStorageUnit: "Kanban is missing a from or to storage unit", foreignStorageUnit: "Storage unit does not belong to the kanban location", missingItem: "Failed to get item", notTransfer: "Kanban is not a transfer kanban", notArmed: "Kanban has no replenishment level" } as const; - Declare the result type:
export type KanbanReplenishOutcome = | { kanbanId: string; outcome: "created"; id: string; stockTransferId: string } | { kanbanId: string; outcome: "at-level"; projectedQuantity: number } | { kanbanId: string; outcome: "invalid"; reason: (typeof KANBAN_REPLENISH_REASONS)[keyof typeof KANBAN_REPLENISH_REASONS] }; export type KanbanReplenishResult = { results: KanbanReplenishOutcome[] }; - Define the function with
name: "kanban-replenish",input: kanbanReplenishInput,permissions: "system". Add the comment:// Both callers elevate: the scan route after requirePermissions, the job with no user. - In
run(ctx, input), do the steps below in ONEctx.db.transaction().execute(async (trx) => { … }). Read only ontrx. - Lock the kanbans:
- Select
id, itemId, locationId, replenishmentSystem, replenishmentLevel, quantity, fromStorageUnitId, storageUnitIdfromkanban. - Filter
companyId = ctx.companyId. - In scan mode, filter
id = input.kanbanId. - In level mode with
kanbanIds, filterid in kanbanIds. IfkanbanIdsis an empty array, return{ results: [] }before the query. - In level mode with no
kanbanIds, filterreplenishmentSystem = 'Transfer'andreplenishmentLevel is not null. - Add
.orderBy("id").forUpdate(). The order stops two calls from deadlocking.
- Select
- If scan mode found no row, throw
NotFoundError("Kanban not found"). - Classify each locked kanban. Stop at the first rule that matches:
- If
replenishmentSystem !== "Transfer"→invalid,notTransfer. - If either storage unit is null →
invalid,missingStorageUnit. - If level mode and
replenishmentLevelis null →invalid,notArmed.
- If
- Read the storage units of the remaining kanbans in one query:
storageUnitid, name, locationId,companyId = ctx.companyId,id in [...]. - If a kanban's From or To storage unit is absent, or its
locationIddiffers from the kanban's, mark itinvalid,foreignStorageUnit. This is the CWE-639 guard moved from the route. - In level mode, read projected quantity in one query:
sql<{ kanbanId: string; replenishmentLevel: number; projectedQuantity: number }>` SELECT * FROM get_kanban_projected_quantities(${ctx.companyId}, ${ids}::text[]) `.execute(trx) - For each remaining kanban, if
!isBelowReplenishmentLevel(projectedQuantity, replenishmentLevel), mark itat-level. Write nothing for it. - Read the items in one query:
itemid, readableIdWithRevision, itemTrackingType, unitOfMeasureCode,companyId = ctx.companyId,id in [...]. A missing item →invalid,missingItem. - In scan mode, read
user.fullNameforctx.userId. Use"unknown user"if it is null. - For each kanban to replenish, in
idorder:- Take
stockTransferId = await getNextSequence(trx, "stockTransfer", ctx.companyId). - Build the note with
kanbanReplenishmentNote. UsereadableIdWithRevision ?? itemIdfor the item.
- Take
- Insert every header in one statement into
stockTransfer:stockTransferId, locationId, status: "Released", companyId, kanbanId, notes, createdBy: ctx.userId. Return["id", "kanbanId", "stockTransferId"]. - Build the lines with
expandSerialTrackedLines, one draft per kanban:itemId,fromStorageUnitId,toStorageUnitId: storageUnitId,quantityrequiresSerialTracking: itemTrackingType === "Serial"requiresBatchTracking: itemTrackingType === "Batch"
- Insert every line in one statement into
stockTransferLine, withstockTransferId(the headerid),companyIdandcreatedBy: ctx.userId. - Return
{ results }with one outcome per locked kanban, inidorder. - Log each
createdoutcome withgetLogger("server-functions", "kanban-replenish"). - Export the function as the module default.
- Run
pnpm --filter @carbon/server-functions exec vitest run -u src/permissions-manifest.test.ts. - Review the snapshot diff. It must add only
"kanban-replenish": "system".
If Kysely refuses the Tiptap object for the notes JSON column, STOP and report.
If getNextSequence throws because the company has no stockTransfer sequence row, STOP and report.
Verify:
pnpm --filter @carbon/server-functions test
# Expected: pass, including permissions-manifest and invoke tests
pnpm exec turbo run typecheck --filter=@carbon/server-functions --concurrency=1
# Expected: 0 errors
Out of scope: storage-rule evaluation (spec Open Question 7); a shortage check on the From bin (Open Question 8); n × quantity catch-up (Open Question 3).
Task 9: Add the live-database test for kanban-replenish
Depends on: Task 8 Files:
- Create:
packages/server-functions/src/kanban-replenish/kanban-test-fixture.ts - Create:
packages/server-functions/src/kanban-replenish/kanban-replenish.test.ts - Copy from (precedent):
packages/server-functions/src/post-payment/payment-test-fixture.ts(scratch company,cleanup()),packages/server-functions/src/post-memo/post-memo-transaction.test.ts(databaseTest,try … finally cleanup)
Steps:
- In the fixture, call
connectLocalTestDatabase(). - Build a scratch company with a random prefix, as the precedent does.
- Insert, in one transaction with
SET LOCAL "app.sync_in_progress" = 'true':companyGroupandcompany(copy from the precedent).- One
location. - Two
storageUnitrows at that location,A(From) andB(To). - One untracked
itemwithreadableId = "KB-TEST-ITEM". - One
sequencerow forstockTransfer. - One
kanban: Transfer, A → B,quantity = 5,replenishmentLevel = 10.
- Read every NOT NULL column without a default from the
Inserttypes inpackages/database/src/types.ts. - Return
{ db, companyId, kanbanId, fromId, toId, itemId, adjustToBin(delta), cleanup }.adjustToBininserts oneitemLedgerrow ofdeltaatB. - Copy
cleanup()from the precedent: delete the company, the company group, and the per-company tables, thendb.destroy(). - Call the function as the callers do:
serverFns.system({ db, companyId, userId: "system" }).invoke("kanban-replenish", …). - Write one
databaseTestper case. Each one callscleanup()infinally.adjustToBin(12), level mode →at-level, nostockTransferrow.adjustToBin(9), level mode → onecreated. Check the header:kanbanIdset,status = "Released",createdBy = "system". Check the notes text startsKanban replenishment. Check one line A → B for 5.- Case 2, then level mode again →
at-level(projected 14), still one header. - Case 2, then set
pickedQuantity = 5and post ledger rows −5 at A and +5 at B → level mode givesat-level. - Case 4, then
adjustToBin(-6)(projected 8) → one morecreated. adjustToBin(12), scan mode →created; notes text containsSignal: scan by.- Update the kanban to From = To → the update throws a check violation.
- A level-mode call with a
kanbanIdsentry from another company → no outcome for it.
Verify: (DB write — ask first; the fixture creates and deletes its own company)
pnpm --filter @carbon/server-functions test -- kanban-replenish
# Expected: 8 tests pass when SUPABASE_DB_URL is local; 8 skipped otherwise
Out of scope: two concurrent calls (the fixture has one connection; the FOR UPDATE lock is the guard).
Task 10: Add the Inngest function kanbanLevelCheckFunction
Depends on: Task 8 Files:
- Create:
packages/jobs/src/inngest/functions/tasks/kanban-level-check.ts - Modify:
packages/jobs/src/inngest/functions/tasks/index.ts— export it - Modify:
packages/jobs/src/inngest/index.ts— import it and add it tofunctionsunder// Tasks - Copy from (precedent):
packages/jobs/src/inngest/functions/tasks/recalculate.ts
Steps:
- Write the function:
export const kanbanLevelCheckFunction = inngest.createFunction( { id: "kanban-level-check", retries: 3, concurrency: { limit: 1, scope: "env", key: '"kanban-level:" + event.data.companyId' } }, { event: "carbon/kanban.level-check" }, async ({ event, step, logger }) => { const { companyId, kanbanIds } = event.data; const result = await step.run("replenish", () => serverFns .system({ db: getJobDatabaseClient(), companyId, userId: "system" }) .invokeOrThrow("kanban-replenish", { mode: "level", kanbanIds }) ); // log each created and each invalid outcome; return result } ); - Do not add
debounce. The local Inngest dev server cannot run it (.ai/lessons.md:541). - Log each
createdoutcome withlogger.info. Log eachinvalidoutcome withlogger.warn.
Verify:
pnpm exec turbo run typecheck --filter=@carbon/jobs
# Expected: 0 errors
pnpm --filter @carbon/jobs test
# Expected: pass
Out of scope: a cron trigger in Inngest (pg_cron owns the hourly sweep).
Task 11: Route the scan through kanban-replenish
Depends on: Task 8 Files:
- Modify:
apps/erp/app/routes/api+/kanban.$id.tsx— theTransferbranch (lines 351-428) and the imports
Steps:
- Keep the
!kanban.data.itemIdguard and its error string. - Delete the storage-unit query, the item query and the
insertStockTransfercall in this branch. - Call:
const replenish = await serverFns .system({ db: getDatabaseClient(), companyId, userId }) .invoke("kanban-replenish", { mode: "scan", kanbanId: id }); - If
replenish.erroris set, log it and return{ data: null, error: "Failed to create stock transfer" }. - Read
const outcome = replenish.data.results[0]. - If
outcome.outcome === "invalid", return{ data: null, error: outcome.reason }. - If
outcome.outcome === "created", return{ data: path.to.stockTransfer(outcome.id), error: null }. - Otherwise return
{ data: null, error: "Failed to create stock transfer" }. - Remove
insertStockTransferfrom the~/modules/inventoryimport if nothing else in the file uses it. - Import
serverFnsfrom@carbon/server-functions.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: 0 errors
Out of scope: the Buy and Make branches; the role: "employee" check in the loader.
Task 12: Extend kanbanValidator and upsertKanban
Depends on: Task 3 Files:
- Modify:
apps/erp/app/modules/inventory/inventory.models.ts—kanbanValidator - Modify:
apps/erp/app/modules/inventory/inventory.service.ts—upsertKanban - Modify:
apps/erp/app/modules/inventory/inventory.models.test.ts
Steps:
- Add to the object:
replenishmentLevel: zfd.numeric(z.number().min(0, { message: "Replenishment level must be 0 or more" }).optional()),. - Add one refine after the existing ones:
.refine( (data) => data.replenishmentSystem === "Transfer" || data.replenishmentLevel === undefined, { message: "Only a transfer kanban can have a replenishment level", path: ["replenishmentLevel"] } ) - In
upsertKanban, set the level on the row before the insert or update:const replenishmentLevel = row.replenishmentSystem === "Transfer" ? (row.replenishmentLevel ?? null) : null; - Spread
{ ...row, replenishmentLevel }into both theinsertand theupdate. A blank field then clears the level. - Add tests to
inventory.models.test.ts:- Transfer with level 10 → valid.
- Transfer with no level → valid.
- Buy with level 10 → error on
replenishmentLevel. - Transfer with level −1 → error on
replenishmentLevel.
Verify:
pnpm --filter erp test -- inventory.models
# Expected: all tests pass
pnpm exec turbo run typecheck --filter=erp
# Expected: 0 errors
Out of scope: the kanbans.new.tsx and kanbans.$id.tsx actions (they pass the validated row through unchanged).
Task 13: Add the glossary term
Depends on: Task 3 Files:
- Modify:
docs/content/src/glossary/terms.ts— the// ── Kanbans (KanbanForm) ──block (around lines 916-935) - Copy from (precedent): the
"kanban-auto-release"entry in the same block
Steps:
- Add:
"kanban-replenishment-level": { term: msg`Replenishment Level`, definition: msg`On a transfer kanban, the minimum quantity for the To storage unit. When on-hand plus open inbound transfers drops below it, Carbon creates a Released stock transfer for the kanban quantity.`, href: "/docs/reference/kanban" },
Verify:
pnpm exec turbo run typecheck --filter=@carbon/content
# Expected: 0 errors
Out of scope: the "replenishment-system" term (Task 18 fixes its text).
Task 14: Add the Replenishment Level field to KanbanForm
Depends on: Tasks 12, 13 Files:
- Modify:
apps/erp/app/modules/inventory/ui/Kanbans/KanbanForm.tsx - Copy from (precedent): the
Numberfield withtermIdinapps/erp/app/modules/accounting/ui/PaymentTerms/PaymentTermForm.tsx:127-133
Steps:
- Render the current Quantity field (lines 227-232) only when
!isTransfer. - After the To Storage Unit field, when
isTransfer, render:- Quantity:
name="quantity",minValue={1}, helper textThe quantity moved from the From storage unit on each signal. - Replenishment Level:
Number,name="replenishmentLevel",label={t\Replenishment Level`},termId="kanban-replenishment-level",minValue={0}, helper textWhen the projected quantity in the To storage unit drops below this level, a Released stock transfer for Quantity is created. Leave blank for scan-only.`
- Quantity:
- In
onItemChange(lines 103-107), keepTransferwhen the user already chose it:setSelectedReplenishmentSystem((current) => current === "Transfer" ? current : item.data?.replenishmentSystem === "Make" ? "Make" : "Buy" ); - Make sure
initialValuescarriesreplenishmentLevel. Check the two routes that render the form (x+/inventory+/kanbans.new.tsx,kanbans.$id.tsx). PassreplenishmentLevel: kanban.replenishmentLevel ?? undefinedwhere the edit route buildsinitialValues.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: 0 errors
pnpm run lint
# Expected: no new Biome errors
Out of scope: the field order of Buy and Make kanbans.
Task 15: Show the level and projected quantity on the kanbans list
Depends on: Task 3 Files:
- Modify:
apps/erp/app/modules/inventory/inventory.service.ts— addgetKanbanProjectedQuantities - Modify:
apps/erp/app/routes/x+/inventory+/kanbans.tsx— the loader and the component - Modify:
apps/erp/app/modules/inventory/ui/Kanbans/KanbansTable.tsx— the props and two columns - Copy from (precedent): the
quantitycolumn (KanbansTable.tsx:388-405), theStatuspill inapps/erp/app/modules/sales/ui/SalesRules/SalesRulesTable.tsx:114-126,useQuantityFormatterfrom~/hooks/useQuantityFormatter
Steps:
- Add the service function after
getKanban:/** @mcp read */ export async function getKanbanProjectedQuantities( client: SupabaseClient<Database>, companyId: string, kanbanIds: string[] ) { return client.rpc("get_kanban_projected_quantities", { company_id: companyId, kanban_ids: kanbanIds }); } - In the loader, after
getKanbans, collect the ids of rows withreplenishmentSystem === "Transfer"and a non-nullreplenishmentLevel. - If the list is not empty, call
getKanbanProjectedQuantitiesonce. - If that call fails, log it and use an empty map. Do not fail the page.
- Return
projectedQuantities: Record<string, number>(kanban id →projectedQuantity). - Pass
projectedQuantitiestoKanbansTableas a new prop. - Add a column after the
quantitycolumn:accessorKey: "replenishmentLevel", headert\Replenishment Level`, blank when null,meta: { icon: }`. - Add a column after it:
id: "projectedQuantity", headert\Projected (To)``.- If the row has no entry in
projectedQuantities, render nothing. - Otherwise render the number with
useQuantityFormatter. - If the number is below
replenishmentLevel, also render<Status color="red"><Trans>Below level</Trans></Status>.
- If the row has no entry in
- Run
pnpm run generate:mcp. Keep the generated diff.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: 0 errors
git status --short | grep -i mcp
# Expected: the generated MCP metadata changed only by the new read tool
Out of scope: sorting or filtering on the projected column (it is not a table column).
Task 16: Show the origin on stock transfers
Depends on: Task 3 Files:
- Modify:
apps/erp/app/modules/inventory/inventory.service.ts—getStockTransfers - Modify:
apps/erp/app/modules/inventory/ui/StockTransfers/StockTransfersTable.tsx— a Source column - Modify:
apps/erp/app/modules/inventory/ui/StockTransfers/StockTransferHeader.tsx— a Kanban badge - Copy from (precedent):
apps/erp/app/modules/workflows/workflows.service.ts:31-50(derived filter),apps/erp/app/modules/workflows/ui/WorkflowsTable.tsx:98-122(derived column),KanbansTable.tsx:141-158(Badge around a Link)
Steps:
- In
getStockTransfers, splitargs.filtersintosourcefilters and column filters, as the precedent does. - For a
sourcefilter with valueKanban, addquery.not("kanbanId", "is", null). - For value
Manual, addquery.is("kanbanId", null). - Pass only the column filters to
setGenericQueryFilters. - Import
Filterfrom~/utils/query. - In
StockTransfersTable, add a column afterstatus:id: "source", headert\Source``- cell:
kanbanIdset →<Badge variant="blue"><Trans>Kanban</Trans></Badge>, else<Badge variant="gray"><Trans>Manual</Trans></Badge> meta.filter: static optionsKanbanandManual;meta.filterHeader: t\Source``meta.exportValue:row.kanbanId ? "Kanban" : "Manual"
- In
StockTransferHeader, after<StockTransferStatus … />(line 171), whenrouteData?.stockTransfer?.kanbanIdis set, render:<Badge variant="outline"> <Link to={path.to.kanban(kanbanId)} className="flex flex-row items-center gap-1"> <LuScanQrCode /> <Trans>Kanban</Trans> </Link> </Badge> - Add
Linkto thereact-routerimport. ImportLuScanQrCodefromreact-icons/lu, the icon the Kanbans nav entry uses.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: 0 errors
Out of scope: StockTransferNotes.tsx (the note renders as any note); MES stock-transfer screens.
Task 17: Print the level on the kanban label
Depends on: Task 3 Files:
- Modify:
packages/documents/src/pdf/KanbanLabelPDF.tsx—KanbanLabeland the QTY block (lines 216-225) - Modify:
apps/erp/app/routes/file+/kanban+/labels.$action[.]pdf.tsx— the mapping (lines 93-106) - Modify:
packages/jobs/src/inngest/functions/tasks/print-job/resolvers.ts—KanbanCardItem(lines 10-23) andresolveKanbanData(lines 58-91) - Modify:
packages/jobs/src/inngest/functions/tasks/print-job/renderers.tsx—renderKanbanCardPDF(lines 220-240)
Steps:
- Add
replenishmentLevel?: number | null;toKanbanLabel. - If
label.replenishmentLevelis not null, render a<Text>under theQTY:text. Use the same style. - Print
MIN: {label.replenishmentLevel}plus the same unit suffix asQTY:. - In the label route mapping, add
replenishmentLevel: kanban.replenishmentLevel. - In
KanbanCardItem, addreplenishmentLevel: number | null;. - In
resolveKanbanData, mapreplenishmentLevel: kanban.replenishmentLevel ?? null. - In
renderKanbanCardPDF, passreplenishmentLevel: item.replenishmentLevel.
Verify:
pnpm exec turbo run typecheck --filter=@carbon/documents --filter=@carbon/jobs --filter=erp
# Expected: 0 errors
Out of scope: the ZPL path (it throws for kanban cards today).
Task 18: Update the docs, the kanban rule and the AGENTS files
Depends on: Tasks 8–17 Files:
- Modify:
docs/content/docs/reference/kanban.mdx - Modify:
docs/content/src/glossary/terms.ts— the"replenishment-system"definition only if it names kanbans (leave it if it is about items only) - Modify:
.claude/rules/kanban-system.md - Modify:
packages/server-functions/AGENTS.mdonly if it lists functions by name - Modify:
apps/erp/app/modules/inventory/AGENTS.md— the kanban section, if it has one
Steps:
- Load the
carbon-docsskill before you editkanban.mdx. - In
kanban.mdx, fix the stale text:- Line 3 description: name the stock transfer as the third outcome.
- Line 17: list
Buy,MakeandTransfer. - Line 28 callout: delete the claim that the form offers only Buy and Make.
- The
StatusFlow(lines 37-40): addTransfer.
- Add fields: From storage unit, To storage unit, Replenishment level (
<Term>kanban replenishment level</Term>). - Add one section, "Replenishment level", in at most 6 sentences. Cover these 5 facts:
- Projected quantity = on-hand + open inbound transfers.
- The signal fires strictly below the level.
- One signal creates one transfer of Quantity.
- Three paths wake the check: a stock movement, a kanban save, the hourly sweep.
- The transfer carries the "Kanban replenishment" note and shows Source
Kanban.
- State that the level path needs the Vault secret
inngest_event_url, as every database-sent event does. - Add the label's
MIN:line to the Output table row for Label. - Add the troubleshooting errors
"Kanban is missing a from or to storage unit","Storage unit does not belong to the kanban location"and"Only a transfer kanban can have a replenishment level". - In
.claude/rules/kanban-system.md:- Add
replenishmentLevelto the data model andkanban_transfer_distinct_storage_units_checkto its constraints. - Add
stockTransfer.kanbanId,get_kanban_projected_quantities, the two handlers and the sweep. - Replace the Transfer scan bullet: the scan calls
kanban-replenishwithmode: "scan". - Add a "Level signal" section: the three wake paths,
kanbanLevelCheckFunction, strict<, supply netting. - Fix the stale
getKanban(client, kanbanId)signature togetKanban(client, kanbanId, companyId). - Add the new files to the
paths:frontmatter.
- Add
- Do the STE-80 review pass on every changed
.ai/file.
Verify:
pnpm --filter docs typecheck
# Expected: 0 errors
cd docs && for l in $(grep -oh "](/docs/[^)]*)" content/docs/reference/kanban.mdx | sed 's|](/docs/||;s|)||' | sort -u); do [ -f "content/docs/$l.mdx" ] || [ -f "content/docs/$l/index.mdx" ] || echo "MISSING $l"; done
# Expected: no MISSING line
Out of scope: a changelog entry (not requested); the MES docs.
Task 19: Run the final gates and the browser test
Depends on: every other task Files: none
Steps:
- Run the scoped gates below.
- 🛑 Ask the user before the browser test. Run it with
/testonly on a yes (memory: no browser unless asked). - Browser test, after
/auth:- Create a Transfer kanban: From bin A, To bin B, Quantity 5, Replenishment Level 10. Confirm the list shows the level and Projected (To).
- Post an inventory adjustment of +12 at B. Confirm no stock transfer appears.
- Post an adjustment of −3 at B. Within 1 minute, one Released stock transfer appears: Source
Kanban, a note starting "Kanban replenishment", one line A → B for 5. - Open it. Confirm the Kanban badge opens the kanban.
- Post a second −3 at B. Confirm no second transfer appears.
- Pick and post the line in full. Confirm no new transfer appears.
- Scan the kanban's order URL. Confirm a transfer appears with "Signal: scan by".
- Print the label. Confirm
MIN: 10underQTY: 5.
- If step 3.3 shows no transfer, check the Inngest dev server for
kanban-level-check. If no event arrived, check the Vault secretinngest_event_url. Report; do not change code to work around it. - Run
/self-reviewon the branch.
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=@carbon/database --filter=@carbon/server-functions --filter=@carbon/jobs --filter=@carbon/documents --filter=@carbon/lib
# Expected: 0 errors
pnpm run lint
# Expected: 0 errors
pnpm --filter @carbon/database test && pnpm --filter @carbon/server-functions test && pnpm --filter @carbon/jobs test && pnpm --filter erp test
# Expected: all pass
pnpm --filter @carbon/checks test
# Expected: pass (spdx-license-header, no-authz-ddl-in-migrations, no-raw-rounding)
pnpm db:check:backups
# Expected: existing backups still restore (DB read — ask first)
Out of scope: pushing the branch; opening the PR; the demo datasets (their Transfer kanbans stay scan-only).