---
name: trace-code-flow
description: Produce a compact, syntax-highlighted pseudo-code gist that explains how a specific code path, class, module, feature, or subsystem works. Use when the user asks to understand code flow, generate pseudo-code documentation, create a flow-first gist, explain event-driven or async execution, or asks how a file, class, function, feature, or subsystem works with an emphasis on reading the code path rather than a prose architecture document.
title: trace-code-flow
canonical_url: https://skilld.dev/gh/mohasarc/mo-skills/trace-code-flow
last_updated: 2026-09-25T02:17:12.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [README.md](https://skilld.dev/api/skills-raw/mohasarc/mo-skills/trace-code-flow/README.md).
>
> If the user asked to install this Skill, run `npx skilld install mohasarc/mo-skills/trace-code-flow`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/trace-code-flow`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/mohasarc/mo-skills/trace-code-flow) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/trace-code-flow`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/trace-code-flow --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/trace-code-flow --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Trace Code Flow

## Overview

Generate a small pseudo-code artifact that lets the user follow the real execution path from entry point to exit. Preserve source-code truth: loops stay in their owning objects, event buses expand into representative subscribers, and async callbacks are shown as re-entry points.

## Workflow

1. Identify the requested target and output location.
2. Trace the source before writing.
3. Write a short syntax-highlighted pseudo-code gist.
4. Validate source ownership, event fan-out, and async re-entry.

Ask a clarifying question only when target or output location is ambiguous enough to risk writing the wrong artifact. Otherwise infer a conservative output path, usually near existing docs or beside source-related documentation.

## Investigation

Start with structure, then read bodies:

- Use symbol overview tools first when available for typed source files.
- Use `rg` to find constructors, call sites, event registrations, subscribers, emitters, timers, callbacks, and external entry points.
- Read only source ranges needed to confirm flow and ownership.
- For event-driven code, trace both sides:
  - where listeners subscribe
  - where events/actions dispatch
  - which representative listeners continue the flow
- For async code, trace where execution leaves and how it re-enters:
  - timers
  - promises/callbacks
  - socket or request handlers
  - queues/jobs
  - delayed bot or retry actions

Do not stop at a pub-sub loop like `listeners.forEach(...)`. Expand it into concrete subscriber examples so the reader knows where to jump next.

## Output Shape

Prefer a Markdown file with a fenced code block using the closest real language for syntax coloring:

````markdown
# Target Gist

```ts
// Read top to bottom.
// Event dispatch expands below, so it is not a dead end.

function mainFlow(input) {
  // 1. Entry point creates/loads runtime object.
  const runtime = createRuntime(input)

  // 2. Plugins/listeners/mechanics attach.
  attachSubscribers(runtime)

  // 3. Runtime starts and owns its loop.
  runtime.start()

  // 4. External events can re-enter later.
  runtime.handleExternalAction(action)
}
```
````

Keep the final artifact small. Aim for 50-140 lines unless the user explicitly asks for depth. Omit data fields, helper methods, validation branches, and edge cases that do not help the user follow the main flow.

## Required Content

Include:

- a clear starting function such as `mainFlow`
- numbered comments for the main path
- real owner of loops and lifecycle methods
- separate external entry points such as socket/request actions
- event dispatch boundaries expanded into representative subscribers
- async re-entry points at the bottom when they matter
- only the most important domain-specific branch examples

Do not include:

- exhaustive method lists
- long prose explanations
- every subscriber in a large event bus
- every guard clause
- full type definitions
- implementation details that do not change the mental model

## Event-Based Logic

Represent event systems as continuations, not dead ends:

```ts
gotAction(action) {
  if (!isAllowed(action)) return
  actionEngine.sendPlayerRequestedAction(action) // jump to ActionEngine
}

class ActionEngine {
  sendPlayerRequestedAction(action) {
    // Every subscriber sees it. Most return immediately.
    ImportantRule.onActionRequest(action)  // action -> next real call
    OtherRules.onActionRequest(action)
  }
}
```

Then show representative subscriber classes:

```ts
class ImportantRule {
  onActionRequest(action) {
    if (action == ImportantAction) runtime.doNextImportantThing()
  }
}
```

If subscribers can trigger more events, show the callback:

```ts
class ImportantRule {
  onActionCompletion(event) {
    if (event == TimerExpired) runtime.handleExternalAction(autoAction)
  }
}
```

## Async Logic

Mark async boundaries explicitly:

```ts
// Async re-entry points.
// socket action -> runtime.handleExternalAction(action)
// timer -> runtime.nextLoop()
// delayed bot/retry/job -> runtime.handleExternalAction(generatedAction)
```

Do not rewrite callback-driven code as a synchronous `while` loop unless the source actually owns such a loop. If the source uses `setTimeout`, promises, queues, or callbacks, show `wait(() => owner.nextLoop())` or the equivalent re-entry.

## Loyalty Rules

- Keep method names close to source names when they are important navigation anchors.
- Group unimportant collaborators under names like `OtherMechanics`, `OtherSubscribers`, or `createControllers`.
- Use comments to explain jump points, not paragraphs.
- If a source call fans out to dozens of handlers, show 2-4 representative handlers and label the rest as grouped.
- If a user says a step is confusing, refine the pseudo-code at that step instead of adding prose around it.
- If the artifact starts feeling large, delete details before adding sections.

## Final Check

Before finishing, verify:

- reader knows exactly where to start
- every jump point has a next place to go
- event dispatch expands into concrete continuations
- async callbacks have re-entry points
- loop ownership matches source
- output renders with syntax coloring
- artifact is shorter than the source explanation it replaces
